From fc45daeb1e3b197e3f07c8cba2e391c585678002 Mon Sep 17 00:00:00 2001 From: Alonso Rodriguez Date: Wed, 19 Apr 2023 19:51:27 +0200 Subject: [PATCH] fix wrong state root when prevlastbatch & lastbatch are empty + restart (#2028) --- jsonrpc/endpoints_eth_test.go | 4 ++-- sequencer/dbmanager.go | 22 ++++++++++++++++++---- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/jsonrpc/endpoints_eth_test.go b/jsonrpc/endpoints_eth_test.go index cfcaab2419..ed688b9032 100644 --- a/jsonrpc/endpoints_eth_test.go +++ b/jsonrpc/endpoints_eth_test.go @@ -626,7 +626,7 @@ func TestEstimateGas(t *testing.T) { Once() m.State. On("EstimateGas", txMatchBy, *txArgs.From, nilUint64, m.DbTx). - Return(*testCase.expectedResult, nil). + Return(*testCase.expectedResult, nil, nil). Once() }, }, @@ -667,7 +667,7 @@ func TestEstimateGas(t *testing.T) { m.State. On("EstimateGas", txMatchBy, common.HexToAddress(c.DefaultSenderAddress), nilUint64, m.DbTx). - Return(*testCase.expectedResult, nil). + Return(*testCase.expectedResult, nil, nil). Once() }, }, diff --git a/sequencer/dbmanager.go b/sequencer/dbmanager.go index 7f3acfd246..31ec924e92 100644 --- a/sequencer/dbmanager.go +++ b/sequencer/dbmanager.go @@ -260,12 +260,26 @@ func (d *dbManager) GetWIPBatch(ctx context.Context) (*WipBatch, error) { if err != nil { return nil, err } - lastBatch.Transactions = lastBatchTxs - lastStateRoot, err := d.state.GetLastStateRoot(ctx, dbTx) - if err != nil { - return nil, err + var prevLastBatchTxs []types.Transaction + if previousLastBatch != nil { + prevLastBatchTxs, _, err = state.DecodeTxs(previousLastBatch.BatchL2Data) + if err != nil { + return nil, err + } + } + + var lastStateRoot common.Hash + // If the last two batches have no txs, the stateRoot can not be retrieved from the l2block because there is no tx. + // In this case, the stateRoot must be gotten from the previousLastBatch + if len(lastBatchTxs) == 0 && previousLastBatch != nil && len(prevLastBatchTxs) == 0 { + lastStateRoot = previousLastBatch.StateRoot + } else { + lastStateRoot, err = d.state.GetLastStateRoot(ctx, dbTx) + if err != nil { + return nil, err + } } wipBatch := &WipBatch{