Skip to content

Commit

Permalink
fix wrong state root when prevlastbatch & lastbatch are empty + resta…
Browse files Browse the repository at this point in the history
…rt (#2028)
  • Loading branch information
ARR552 committed Apr 19, 2023
1 parent 3d8be69 commit fc45dae
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
4 changes: 2 additions & 2 deletions jsonrpc/endpoints_eth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
},
},
Expand Down Expand Up @@ -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()
},
},
Expand Down
22 changes: 18 additions & 4 deletions sequencer/dbmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down

0 comments on commit fc45dae

Please sign in to comment.