diff --git a/jsonrpc/client/zkevm.go b/jsonrpc/client/zkevm.go index 26a2119e74..f2e78aa4af 100644 --- a/jsonrpc/client/zkevm.go +++ b/jsonrpc/client/zkevm.go @@ -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 @@ -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 diff --git a/state/state.go b/state/state.go index 6f26586333..0fd9bca5c8 100644 --- a/state/state.go +++ b/state/state.go @@ -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 @@ -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, diff --git a/synchronizer/interfaces.go b/synchronizer/interfaces.go index adc908d2d9..3ce6e5eb1e 100644 --- a/synchronizer/interfaces.go +++ b/synchronizer/interfaces.go @@ -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 diff --git a/synchronizer/mock_state.go b/synchronizer/mock_state.go index db075dd450..0175e5eec1 100644 --- a/synchronizer/mock_state.go +++ b/synchronizer/mock_state.go @@ -470,17 +470,29 @@ func (_m *stateMock) OpenBatch(ctx context.Context, processingContext state.Proc } // ProcessAndStoreClosedBatch provides a mock function with given fields: ctx, processingCtx, encodedTxs, dbTx, caller -func (_m *stateMock) ProcessAndStoreClosedBatch(ctx context.Context, processingCtx state.ProcessingContext, encodedTxs []byte, dbTx pgx.Tx, caller state.CallerLabel) error { +func (_m *stateMock) ProcessAndStoreClosedBatch(ctx context.Context, processingCtx state.ProcessingContext, encodedTxs []byte, dbTx pgx.Tx, caller state.CallerLabel) (common.Hash, error) { ret := _m.Called(ctx, processingCtx, encodedTxs, dbTx, caller) - var r0 error - if rf, ok := ret.Get(0).(func(context.Context, state.ProcessingContext, []byte, pgx.Tx, state.CallerLabel) error); ok { + var r0 common.Hash + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, state.ProcessingContext, []byte, pgx.Tx, state.CallerLabel) (common.Hash, error)); ok { + return rf(ctx, processingCtx, encodedTxs, dbTx, caller) + } + if rf, ok := ret.Get(0).(func(context.Context, state.ProcessingContext, []byte, pgx.Tx, state.CallerLabel) common.Hash); ok { r0 = rf(ctx, processingCtx, encodedTxs, dbTx, caller) } else { - r0 = ret.Error(0) + if ret.Get(0) != nil { + r0 = ret.Get(0).(common.Hash) + } } - return r0 + if rf, ok := ret.Get(1).(func(context.Context, state.ProcessingContext, []byte, pgx.Tx, state.CallerLabel) error); ok { + r1 = rf(ctx, processingCtx, encodedTxs, dbTx, caller) + } else { + r1 = ret.Error(1) + } + + return r0, r1 } // ProcessSequencerBatch provides a mock function with given fields: ctx, batchNumber, batchL2Data, caller, dbTx diff --git a/synchronizer/synchronizer.go b/synchronizer/synchronizer.go index de3079c1e0..b86cccedfb 100644 --- a/synchronizer/synchronizer.go +++ b/synchronizer/synchronizer.go @@ -697,19 +697,7 @@ 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) @@ -717,7 +705,7 @@ func (s *ClientSynchronizer) processSequenceBatches(sequencedBatches []etherman. 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) @@ -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 { @@ -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 { @@ -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) @@ -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) diff --git a/synchronizer/synchronizer_test.go b/synchronizer/synchronizer_test.go index b24a4c3795..6018e5ce53 100644 --- a/synchronizer/synchronizer_test.go +++ b/synchronizer/synchronizer_test.go @@ -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 @@ -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{ @@ -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{