Skip to content
This repository was archived by the owner on Feb 17, 2025. It is now read-only.

wip: adding some implementation of the finalizer, defining needed int…#1481

Merged
Psykepro merged 11 commits intofeature/newSequencerfrom
feature/newSequencer-finalizer
Jan 10, 2023
Merged

wip: adding some implementation of the finalizer, defining needed int…#1481
Psykepro merged 11 commits intofeature/newSequencerfrom
feature/newSequencer-finalizer

Conversation

@Psykepro
Copy link
Contributor

@Psykepro Psykepro commented Dec 22, 2022

…erfaces by the finalizer from the dbmanager, and implementing the remaining resourses Sub.

Signed-off-by: Nikolay Nedkov nikolai_nedkov@yahoo.com

Related to issue #1478.

What does this PR do?

Adding some of the implementations (still WIP). Mostly to define needed interfaces by the dbManager to unblock @ToniRamirezM. I've added to the proto the TouchedAddresses and generated the proto files.
I got carried away and implemented the remainingResources.Sub method but I think the implementation is very clean. :)

Reviewers

Main reviewers:

…erfaces by the finalizer from the dbmanager, and implementing the remaining resourses Sub.

Signed-off-by: Nikolay Nedkov <nikolai_nedkov@yahoo.com>
Copy link
Collaborator

@arnaubennassar arnaubennassar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Sorry for too many comments saying You can do this in a later PR as here the priority is to define interfaces ASAP. It was to help identifying what needs to be done now and what can be done later... Was lazy so just copy pasted it 🙃
  • Also not to be done now, but in some parts of the code there already too many identation levels (at least I've counted 5...). As a rule of thumb when this happens it's time to start thinking about splitting into multiple functions

Comment on lines 116 to 117
r.remainingBytes -= other.remainingBytes
r.remainingGas -= other.remainingGas
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also needs underflow check

Copy link
Contributor Author

@Psykepro Psykepro Dec 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It has it in line 112 this check everything. It is first checking every field for underflow before doing any changes.

return nil
}

func (r *RemainingResources) checkForResourcesOverflow(other RemainingResources) error {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
func (r *RemainingResources) checkForResourcesOverflow(other RemainingResources) error {
func (r *RemainingResources) checkForResourcesUnderflow(other RemainingResources) error {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ops, yep mistake here.

}

// SubZkCounters subtract zk counters with passed zk counters (not safe)
func (zkc *ZkCounters) SubZkCounters(txZkCounters ZkCounters) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not very happy about this (because for how it used to be, not your changes). Things that IMO could imporve current situation:

  • Move the struct to state package
  • Ideally reuse the counters from the proto interface so we don't need to reparse (maybe by wrapping)
  • ZkCounters => ZKCounters 🙏
  • Include the underflow logic already in the SubZkCounters method
  • SubZkCounters => Sub

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only actual request here is the renamings of the functions, otherwise it's just suggestions up to you to implement if you consider them legit

GER: lastBatch.GlobalExitRoot,
initialStateRoot: lastBatch.StateRoot,
intermediaryStateRoot: lastBatch.StateRoot,
txs: make([]TxTracker, 0, f.maxTxsPerBatch),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exisitng transactions in the batch should be also loaded from DB. You can do this in a later PR as here the priority is to define interfaces ASAP

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This I had in mind just wanted to make quick PR for the interfaces.

wipBatch := wipBatch{}
wipBatch := wipBatch{
GER: lastBatch.GlobalExitRoot,
initialStateRoot: lastBatch.StateRoot,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The initial state root is actually yhe state root stored in the previous batch (the last closed batch). You can do this in a later PR as here the priority is to define interfaces ASAP

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This I had in mind just wanted to make quick PR for the interfaces.

tx := f.worker.GetBestFittingTx(wipBatch.remainingResources)
if tx != nil {
lenOfTxs := len(wipBatch.txs)
isFirstTx := lenOfTxs == 1
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
isFirstTx := lenOfTxs == 1
isFirstTx := lenOfTxs == 0

At this point, tx is not added yet into wipBatch.txs. If wipBatch.txs == 1 it means that in previous iterations there has been a tx added into the batch, and that tx was actually the first one.

Beyond that we may consider to execute a batch with no txs at all to produce the GER update, and here use always empty hash. We can talk about this later. You can do this in a later PR as here the priority is to define interfaces ASAP

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ops, yep mistake here.. from hurrying :D

Comment on lines 132 to 135
from, err := types.Sender(types.NewEIP155Signer(processedTx.Tx.ChainId()), &processedTx.Tx)
if err != nil {
from, err = types.Sender(types.HomesteadSigner{}, &processedTx.Tx)
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should be able to get the from from the TxTracker. You can do this in a later PR as here the priority is to define interfaces ASAP

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this I guess to be provided by the DbManager to the struct form the database?

processBatchRequest.OldAccInputHash = result.NewAccInputHash
// We store the processed transaction, add it to the batch and delete from the pool atomically.
go f.dbManager.StoreProcessedTxAndDeleteFromPool(ctx, wipBatch.batchNumber, processedTx)
go f.worker.UpdateAfterSingleSuccessfulTxExecution(from, result.TouchedAddresses)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
go f.worker.UpdateAfterSingleSuccessfulTxExecution(from, result.TouchedAddresses)
f.worker.UpdateAfterSingleSuccessfulTxExecution(from, result.TouchedAddresses)

We decided to do this synchronously. The idea is to compromise a bit of performance but to gain more security (improve "hit ratio")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, sorry mistake here.

AccInputHash: processBatchRequest.OldAccInputHash,
StateRoot: wipBatch.intermediaryStateRoot,
LocalExitRoot: processBatchRequest.GlobalExitRoot,
Txs: ethTxs,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to review this to see if we can directly pass wipBatch.txs here. Also it would be nice if we can stop working with types.Transaction in the first place and use the TxTracker for the sequencer and "raw txs" (just the encoded tx as []byte) in the state.

I'm not sure about how feasible is this, but at least is worth exploring

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, will check if possible to use everywhere the raw tx.

state/types.go Outdated
TxData []byte
SequencerAddress common.Address
Timestamp uint64
IsFirstTx bool
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is not needed here. This should be used for the GER empty hack, but we're already taking into consideration this in the finalizer.

With that being said, since this is something related to how the executor works, it will make a lot of sense to take care bout this using the isFirstTx, but then we don't need to do it in the finalizer. Also if it's the state who needs to do that, then it also needs to know if the inteeractions is meant to be done in the tx by tx mode or in the full batch mode...

Up to you, essentially you have to either:

  • Remove IsFirstTx from the struct
  • Remoe the GER = empty hash from finalizer, and add a tx by tx flag in the process request struct

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay

@@ -43,11 +47,17 @@ type txManager interface {

type workerInterface interface {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to add also "func (w *Worker) AddTx(tx TxTracker)" function that is used by the Pool Loader to load txs from the Pool to the Worker

Copy link
Contributor Author

@Psykepro Psykepro Dec 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR is just for the interfaces needed in the dbManager by the Finalizer. But after I get back from the vacation, because you will be still on vacation I can take a look at adding some of the needed in the Worker no probs! :))

@Psykepro Psykepro force-pushed the feature/newSequencer-finalizer branch 2 times, most recently from 37c0052 to a2ecd09 Compare December 23, 2022 16:31
Signed-off-by: Nikolay Nedkov <nikolai_nedkov@yahoo.com>
@Psykepro Psykepro force-pushed the feature/newSequencer-finalizer branch from a2ecd09 to ada278d Compare December 23, 2022 16:54
}

// The dbManager will need to handle the errors inside the functions which don't return error as they will be used async in the other abstractions.
// Also if dbTx is missing this needs also to be handled ion the dbManager
Copy link
Contributor

@ToniRamirezM ToniRamirezM Jan 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Also if dbTx is missing this needs also to be handled ion the dbManager
// Also if dbTx is missing this needs also to be handled in the dbManager

Signed-off-by: Nikolay Nedkov <nikolai_nedkov@yahoo.com>
@Psykepro Psykepro requested a review from ToniRamirezM January 5, 2023 08:29
Signed-off-by: Nikolay Nedkov <nikolai_nedkov@yahoo.com>
@Psykepro Psykepro force-pushed the feature/newSequencer-finalizer branch 2 times, most recently from 969fa8a to 86a7241 Compare January 9, 2023 12:36
Signed-off-by: Nikolay Nedkov <nikolai_nedkov@yahoo.com>
@Psykepro Psykepro force-pushed the feature/newSequencer-finalizer branch from 86a7241 to bfa53aa Compare January 9, 2023 12:37
Signed-off-by: Nikolay Nedkov <nikolai_nedkov@yahoo.com>
@Psykepro Psykepro force-pushed the feature/newSequencer-finalizer branch from 5cab1b3 to b3c3ccf Compare January 9, 2023 12:39
* WIP

* WIP

* WIP
…ng small refactor.

Signed-off-by: Nikolay Nedkov <nikolai_nedkov@yahoo.com>
@Psykepro Psykepro force-pushed the feature/newSequencer-finalizer branch from 975f1d5 to ec6ee2e Compare January 10, 2023 08:25
Signed-off-by: Nikolay Nedkov <nikolai_nedkov@yahoo.com>
Signed-off-by: Nikolay Nedkov <nikolai_nedkov@yahoo.com>
Signed-off-by: Nikolay Nedkov <nikolai_nedkov@yahoo.com>
@Psykepro Psykepro merged commit 59dbd49 into feature/newSequencer Jan 10, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants