Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/#1833 execute batch #1834

Merged
merged 7 commits into from
Mar 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions jsonrpc/client/zkevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"github.com/0xPolygonHermez/zkevm-node/hex"
"github.com/0xPolygonHermez/zkevm-node/jsonrpc/types"
"github.com/0xPolygonHermez/zkevm-node/log"
)

// BatchNumber returns the latest batch number
Expand Down Expand Up @@ -47,7 +46,6 @@ func (c *Client) BatchByNumber(ctx context.Context, number *big.Int) (*types.Bat
}

var result *types.Batch
log.Debugf(string(response.Result))
err = json.Unmarshal(response.Result, &result)
if err != nil {
return nil, err
Expand Down
16 changes: 8 additions & 8 deletions state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -782,24 +782,24 @@ func (s *State) ProcessAndStoreClosedBatch(
encodedTxs []byte,
dbTx pgx.Tx,
caller CallerLabel,
) error {
) (common.Hash, error) {
// Decode transactions
decodedTransactions, _, err := DecodeTxs(encodedTxs)
if err != nil && !errors.Is(err, InvalidData) {
log.Debugf("error decoding transactions: %v", err)
return err
return common.Hash{}, err
}

// Open the batch and process the txs
if dbTx == nil {
return ErrDBTxNil
return common.Hash{}, ErrDBTxNil
}
if err := s.OpenBatch(ctx, processingCtx, dbTx); err != nil {
return err
return common.Hash{}, err
}
processed, err := s.processBatch(ctx, processingCtx.BatchNumber, encodedTxs, caller, dbTx)
if err != nil {
return err
return common.Hash{}, err
}

// Sanity check
Expand Down Expand Up @@ -830,19 +830,19 @@ func (s *State) ProcessAndStoreClosedBatch(

processedBatch, err := s.convertToProcessBatchResponse(decodedTransactions, processed)
if err != nil {
return err
return common.Hash{}, err
}

if len(processedBatch.Responses) > 0 {
// Store processed txs into the batch
err = s.StoreTransactions(ctx, processingCtx.BatchNumber, processedBatch.Responses, dbTx)
if err != nil {
return err
return common.Hash{}, err
}
}

// Close batch
return s.closeBatch(ctx, ProcessingReceipt{
return common.BytesToHash(processed.NewStateRoot), s.closeBatch(ctx, ProcessingReceipt{
BatchNumber: processingCtx.BatchNumber,
StateRoot: processedBatch.NewStateRoot,
LocalExitRoot: processedBatch.NewLocalExitRoot,
Expand Down
2 changes: 1 addition & 1 deletion synchronizer/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type stateInterface interface {
AddVirtualBatch(ctx context.Context, virtualBatch *state.VirtualBatch, dbTx pgx.Tx) error
GetNextForcedBatches(ctx context.Context, nextForcedBatches int, dbTx pgx.Tx) ([]state.ForcedBatch, error)
AddVerifiedBatch(ctx context.Context, verifiedBatch *state.VerifiedBatch, dbTx pgx.Tx) error
ProcessAndStoreClosedBatch(ctx context.Context, processingCtx state.ProcessingContext, encodedTxs []byte, dbTx pgx.Tx, caller state.CallerLabel) error
ProcessAndStoreClosedBatch(ctx context.Context, processingCtx state.ProcessingContext, encodedTxs []byte, dbTx pgx.Tx, caller state.CallerLabel) (common.Hash, error)
SetGenesis(ctx context.Context, block state.Block, genesis state.Genesis, dbTx pgx.Tx) ([]byte, error)
OpenBatch(ctx context.Context, processingContext state.ProcessingContext, dbTx pgx.Tx) error
CloseBatch(ctx context.Context, receipt state.ProcessingReceipt, dbTx pgx.Tx) error
Expand Down
22 changes: 17 additions & 5 deletions synchronizer/mock_state.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 19 additions & 16 deletions synchronizer/synchronizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -697,27 +697,15 @@ func (s *ClientSynchronizer) processSequenceBatches(sequencedBatches []etherman.
ForcedBatchNum: batch.ForcedBatchNum,
}

// Reprocess batch to compare the stateRoot with tBatch.StateRoot and get accInputHash
p, err := s.state.ExecuteBatch(s.ctx, batch, false, dbTx)
if err != nil {
log.Errorf("error executing L1 batch: %+v, error: %v", batch, err)
rollbackErr := dbTx.Rollback(s.ctx)
if rollbackErr != nil {
log.Errorf("error rolling back state. BatchNumber: %d, BlockNumber: %d, rollbackErr: %s, error : %v", batch.BatchNumber, blockNumber, rollbackErr.Error(), err)
return rollbackErr
}
return err
}
newRoot := common.BytesToHash(p.NewStateRoot)
accumulatedInputHash := common.BytesToHash(p.NewAccInputHash)
var newRoot common.Hash

// First get trusted batch from db
tBatch, err := s.state.GetBatchByNumber(s.ctx, batch.BatchNumber, dbTx)
if err != nil {
if errors.Is(err, state.ErrNotFound) || errors.Is(err, state.ErrStateNotSynchronized) {
log.Debugf("BatchNumber: %d, not found in trusted state. Storing it...", batch.BatchNumber)
// If it is not found, store batch
err = s.state.ProcessAndStoreClosedBatch(s.ctx, processCtx, batch.BatchL2Data, dbTx, state.SynchronizerCallerLabel)
newStateRoot, err := s.state.ProcessAndStoreClosedBatch(s.ctx, processCtx, batch.BatchL2Data, dbTx, state.SynchronizerCallerLabel)
if err != nil {
log.Errorf("error storing trustedBatch. BatchNumber: %d, BlockNumber: %d, error: %v", batch.BatchNumber, blockNumber, err)
rollbackErr := dbTx.Rollback(s.ctx)
Expand All @@ -728,6 +716,7 @@ func (s *ClientSynchronizer) processSequenceBatches(sequencedBatches []etherman.
log.Errorf("error storing batch. BatchNumber: %d, BlockNumber: %d, error: %v", batch.BatchNumber, blockNumber, err)
return err
}
newRoot = newStateRoot
tBatch = &batch
tBatch.StateRoot = newRoot
} else {
Expand All @@ -740,6 +729,20 @@ func (s *ClientSynchronizer) processSequenceBatches(sequencedBatches []etherman.
return err
}
} else {
// Reprocess batch to compare the stateRoot with tBatch.StateRoot and get accInputHash
p, err := s.state.ExecuteBatch(s.ctx, batch, false, dbTx)
if err != nil {
log.Errorf("error executing L1 batch: %+v, error: %v", batch, err)
rollbackErr := dbTx.Rollback(s.ctx)
if rollbackErr != nil {
log.Errorf("error rolling back state. BatchNumber: %d, BlockNumber: %d, rollbackErr: %s, error : %v", batch.BatchNumber, blockNumber, rollbackErr.Error(), err)
return rollbackErr
}
return err
}
newRoot = common.BytesToHash(p.NewStateRoot)
accumulatedInputHash := common.BytesToHash(p.NewAccInputHash)

//AddAccumulatedInputHash
err = s.state.AddAccumulatedInputHash(s.ctx, batch.BatchNumber, accumulatedInputHash, dbTx)
if err != nil {
Expand Down Expand Up @@ -782,7 +785,7 @@ func (s *ClientSynchronizer) processSequenceBatches(sequencedBatches []etherman.
log.Errorf("error resetting trusted state. BatchNumber: %d, BlockNumber: %d, error: %v", batch.BatchNumber, blockNumber, err)
return err
}
err = s.state.ProcessAndStoreClosedBatch(s.ctx, processCtx, batch.BatchL2Data, dbTx, state.SynchronizerCallerLabel)
_, err = s.state.ProcessAndStoreClosedBatch(s.ctx, processCtx, batch.BatchL2Data, dbTx, state.SynchronizerCallerLabel)
if err != nil {
log.Errorf("error storing trustedBatch. BatchNumber: %d, BlockNumber: %d, error: %v", batch.BatchNumber, blockNumber, err)
rollbackErr := dbTx.Rollback(s.ctx)
Expand Down Expand Up @@ -906,7 +909,7 @@ func (s *ClientSynchronizer) processSequenceForceBatch(sequenceForceBatch []ethe
ForcedBatchNum: &forcedBatches[i].ForcedBatchNumber,
}
// Process batch
err := s.state.ProcessAndStoreClosedBatch(s.ctx, batch, forcedBatches[i].RawTxsData, dbTx, state.SynchronizerCallerLabel)
_, err := s.state.ProcessAndStoreClosedBatch(s.ctx, batch, forcedBatches[i].RawTxsData, dbTx, state.SynchronizerCallerLabel)
if err != nil {
log.Errorf("error processing batch in processSequenceForceBatch. BatchNumber: %d, BlockNumber: %d, error: %v", batch.BatchNumber, block.BlockNumber, err)
rollbackErr := dbTx.Rollback(s.ctx)
Expand Down
5 changes: 3 additions & 2 deletions synchronizer/synchronizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type mocks struct {
ZKEVMClient *zkEVMClientMock
}

// Test commented until we remove the fatal in checkTrustedReorg function
// func TestTrustedStateReorg(t *testing.T) {
// type testCase struct {
// Name string
Expand Down Expand Up @@ -210,7 +211,7 @@ type mocks struct {

// m.State.
// On("ProcessAndStoreClosedBatch", ctx, processingContext, sequencedBatch.Transactions, m.DbTx, state.SynchronizerCallerLabel).
// Return(nil).
// Return(trustedBatch.StateRoot, nil).
// Once()

// virtualBatch := &state.VirtualBatch{
Expand Down Expand Up @@ -717,7 +718,7 @@ func TestSequenceForcedBatch(t *testing.T) {

m.State.
On("ProcessAndStoreClosedBatch", ctx, processingContext, sequencedForceBatch.Transactions, m.DbTx, state.SynchronizerCallerLabel).
Return(nil).
Return(common.Hash{}, nil).
Once()

virtualBatch := &state.VirtualBatch{
Expand Down