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

keep im state root in db #3427

Merged
merged 11 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
keep im state root in db
  • Loading branch information
ToniRamirezM committed Mar 7, 2024
commit 666e11dd0a6da1ebd74f6d90b426fd387f97df6d
2 changes: 1 addition & 1 deletion state/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ func (s *State) SetGenesis(ctx context.Context, block Block, genesis Genesis, m
storeTxsEGPData := []StoreTxEGPData{}
txsL2Hash := []common.Hash{}

err = s.AddL2Block(ctx, batch.BatchNumber, l2Block, receipts, txsL2Hash, storeTxsEGPData, dbTx)
err = s.AddL2Block(ctx, batch.BatchNumber, l2Block, receipts, txsL2Hash, storeTxsEGPData, root, dbTx)
if err != nil {
return common.Hash{}, err
}
Expand Down
4 changes: 2 additions & 2 deletions state/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ func DecodeTx(encodedTx string) (*types.Transaction, error) {
}

// GenerateReceipt generates a receipt from a processed transaction
func GenerateReceipt(blockNumber *big.Int, processedTx *ProcessTransactionResponse, txIndex uint, forkID uint64) *types.Receipt {
func GenerateReceipt(blockNumber *big.Int, processedTx *ProcessTransactionResponse, txIndex uint, forkID uint64) (*types.Receipt, common.Hash) {
receipt := &types.Receipt{
Type: uint8(processedTx.Type),
CumulativeGasUsed: processedTx.GasUsed,
Expand Down Expand Up @@ -322,7 +322,7 @@ func GenerateReceipt(blockNumber *big.Int, processedTx *ProcessTransactionRespon
receipt.Status = uint64(processedTx.Status)
}

return receipt
return receipt, processedTx.StateRoot
}

// IsPreEIP155Tx checks if the tx is a tx that has a chainID as zero and
Expand Down
2 changes: 1 addition & 1 deletion state/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ type storage interface {
GetL2BlockTransactionCountByHash(ctx context.Context, blockHash common.Hash, dbTx pgx.Tx) (uint64, error)
GetL2BlockTransactionCountByNumber(ctx context.Context, blockNumber uint64, dbTx pgx.Tx) (uint64, error)
GetTransactionEGPLogByHash(ctx context.Context, transactionHash common.Hash, dbTx pgx.Tx) (*EffectiveGasPriceLog, error)
AddL2Block(ctx context.Context, batchNumber uint64, l2Block *L2Block, receipts []*types.Receipt, txsL2Hash []common.Hash, txsEGPData []StoreTxEGPData, dbTx pgx.Tx) error
AddL2Block(ctx context.Context, batchNumber uint64, l2Block *L2Block, receipts []*types.Receipt, txsL2Hash []common.Hash, txsEGPData []StoreTxEGPData, imStateRoot common.Hash, dbTx pgx.Tx) error
GetLastVirtualizedL2BlockNumber(ctx context.Context, dbTx pgx.Tx) (uint64, error)
GetLastConsolidatedL2BlockNumber(ctx context.Context, dbTx pgx.Tx) (uint64, error)
GetLastVerifiedL2BlockNumberUntilL1Block(ctx context.Context, l1FinalizedBlockNumber uint64, dbTx pgx.Tx) (uint64, error)
Expand Down
21 changes: 11 additions & 10 deletions state/mocks/mock_storage.go

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

6 changes: 3 additions & 3 deletions state/pgstatestorage/l2block.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ func (p *PostgresStorage) GetL2BlockTransactionCountByNumber(ctx context.Context
}

// AddL2Block adds a new L2 block to the State Store
func (p *PostgresStorage) AddL2Block(ctx context.Context, batchNumber uint64, l2Block *state.L2Block, receipts []*types.Receipt, txsL2Hash []common.Hash, txsEGPData []state.StoreTxEGPData, dbTx pgx.Tx) error {
//TODO: Optmize this function using only one SQL (with several values) to insert all the txs, receips and logs
func (p *PostgresStorage) AddL2Block(ctx context.Context, batchNumber uint64, l2Block *state.L2Block, receipts []*types.Receipt, txsL2Hash []common.Hash, txsEGPData []state.StoreTxEGPData, imStateRoot common.Hash, dbTx pgx.Tx) error {
// TODO: Optimize this function using only one SQL (with several values) to insert all the txs, receipts and logs
log.Debugf("[AddL2Block] adding L2 block %d", l2Block.NumberU64())
start := time.Now()

Expand Down Expand Up @@ -201,7 +201,7 @@ func (p *PostgresStorage) AddL2Block(ctx context.Context, batchNumber uint64, l2
log.Debugf("[AddL2Block] adding L2 block %d", l2blockNumber)
if _, err := e.Exec(ctx, addL2BlockSQL,
l2Block.Number().Uint64(), l2Block.Hash().String(), header, uncles,
l2Block.ParentHash().String(), l2Block.Root().String(),
l2Block.ParentHash().String(), imStateRoot.String(),
l2Block.ReceivedAt, batchNumber, time.Now().UTC()); err != nil {
return err
}
Expand Down
8 changes: 4 additions & 4 deletions state/pgstatestorage/pgstatestorage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func TestGetBatchByL2BlockNumber(t *testing.T) {
txsL2Hash[i] = common.HexToHash(fmt.Sprintf("0x%d", i))
}

err = pgStateStorage.AddL2Block(ctx, batchNumber, l2Block, receipts, txsL2Hash, storeTxsEGPData, dbTx)
err = pgStateStorage.AddL2Block(ctx, batchNumber, l2Block, receipts, txsL2Hash, storeTxsEGPData, state.ZeroHash, dbTx)
require.NoError(t, err)
result, err := pgStateStorage.BatchNumberByL2BlockNumber(ctx, l2Block.Number().Uint64(), dbTx)
require.NoError(t, err)
Expand Down Expand Up @@ -724,7 +724,7 @@ func TestGetLastVerifiedL2BlockNumberUntilL1Block(t *testing.T) {
txsL2Hash[i] = common.HexToHash(fmt.Sprintf("0x%d", i))
}

err = testState.AddL2Block(ctx, batchNumber, l2Block, []*types.Receipt{}, txsL2Hash, storeTxsEGPData, dbTx)
err = testState.AddL2Block(ctx, batchNumber, l2Block, []*types.Receipt{}, txsL2Hash, storeTxsEGPData, state.ZeroHash, dbTx)
require.NoError(t, err)

virtualBatch := state.VirtualBatch{BlockNumber: blockNumber, BatchNumber: batchNumber, Coinbase: addr, SequencerAddr: addr, TxHash: hash}
Expand Down Expand Up @@ -948,7 +948,7 @@ func TestGetLogs(t *testing.T) {
txsL2Hash[i] = common.HexToHash(fmt.Sprintf("0x%d", i))
}

err = testState.AddL2Block(ctx, batchNumber, l2Block, receipts, txsL2Hash, storeTxsEGPData, dbTx)
err = testState.AddL2Block(ctx, batchNumber, l2Block, receipts, txsL2Hash, storeTxsEGPData, state.ZeroHash, dbTx)
require.NoError(t, err)
}

Expand Down Expand Up @@ -1079,7 +1079,7 @@ func TestGetNativeBlockHashesInRange(t *testing.T) {
txsL2Hash[i] = common.HexToHash(fmt.Sprintf("0x%d", i))
}

err = testState.AddL2Block(ctx, batchNumber, l2Block, receipts, txsL2Hash, storeTxsEGPData, dbTx)
err = testState.AddL2Block(ctx, batchNumber, l2Block, receipts, txsL2Hash, storeTxsEGPData, state.ZeroHash, dbTx)
require.NoError(t, err)

nativeBlockHashes = append(nativeBlockHashes, l2Block.Header().Root)
Expand Down
2 changes: 1 addition & 1 deletion state/test/forkid_dragonfruit/dragonfruit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1505,7 +1505,7 @@ func TestExecutorRevert(t *testing.T) {
txsL2Hash[i] = common.HexToHash(fmt.Sprintf("0x%d", i))
}

err = testState.AddL2Block(ctx, 0, l2Block, receipts, txsL2Hash, storeTxsEGPData, dbTx)
err = testState.AddL2Block(ctx, 0, l2Block, receipts, txsL2Hash, storeTxsEGPData, common.BytesToHash(processBatchResponse.NewStateRoot), dbTx)
require.NoError(t, err)
l2Block, err = testState.GetL2BlockByHash(ctx, l2Block.Hash(), dbTx)
require.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion state/test/forkid_independent/independent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@ func TestAddGetL2Block(t *testing.T) {
txsL2Hash[i] = common.HexToHash(fmt.Sprintf("0x%d", i))
}

err = testState.AddL2Block(ctx, batchNumber, l2Block, receipts, txsL2Hash, storeTxsEGPData, dbTx)
err = testState.AddL2Block(ctx, batchNumber, l2Block, receipts, txsL2Hash, storeTxsEGPData, state.ZeroHash, dbTx)
require.NoError(t, err)
result, err := testState.GetL2BlockByHash(ctx, l2Block.Hash(), dbTx)
require.NoError(t, err)
Expand Down
5 changes: 5 additions & 0 deletions state/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ func (s *State) DebugTransaction(ctx context.Context, transactionHash common.Has

forkId := s.GetForkIDByBatchNumber(batch.BatchNumber)

// To maintain compatibility with etrog, we need to set the post state to 0x0
if forkId >= FORKID_ELDERBERRY {
receipt.PostState = ZeroHash.Bytes()
}

var response *ProcessTransactionResponse
var startTime, endTime time.Time
if forkId < FORKID_ETROG {
Expand Down
14 changes: 8 additions & 6 deletions state/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func (s *State) StoreTransactions(ctx context.Context, batchNumber uint64, proce
header.BlockInfoRoot = processedBlock.BlockInfoRoot
transactions := []*types.Transaction{&processedTx.Tx}

receipt := GenerateReceipt(header.Number, processedTx, uint(i), forkID)
receipt, imStateRoot := GenerateReceipt(header.Number, processedTx, uint(i), forkID)
if !CheckLogOrder(receipt.Logs) {
return fmt.Errorf("error: logs received from executor are not in order")
}
Expand All @@ -189,7 +189,7 @@ func (s *State) StoreTransactions(ctx context.Context, batchNumber uint64, proce
txsL2Hash := []common.Hash{processedTx.TxHashL2_V2}

// Store L2 block and its transaction
if err := s.AddL2Block(ctx, batchNumber, l2Block, receipts, txsL2Hash, storeTxsEGPData, dbTx); err != nil {
if err := s.AddL2Block(ctx, batchNumber, l2Block, receipts, txsL2Hash, storeTxsEGPData, imStateRoot, dbTx); err != nil {
return err
}
}
Expand Down Expand Up @@ -238,6 +238,8 @@ func (s *State) StoreL2Block(ctx context.Context, batchNumber uint64, l2Block *P
storeTxsEGPData := make([]StoreTxEGPData, 0, numTxs)
receipts := make([]*types.Receipt, 0, numTxs)
txsL2Hash := make([]common.Hash, 0, numTxs)
imStateRoot := common.Hash{}
var receipt *types.Receipt

for i, txResponse := range l2Block.TransactionResponses {
// if the transaction has an intrinsic invalid tx error it means
Expand All @@ -260,7 +262,7 @@ func (s *State) StoreL2Block(ctx context.Context, batchNumber uint64, l2Block *P

storeTxsEGPData = append(storeTxsEGPData, storeTxEGPData)

receipt := GenerateReceipt(header.Number, txResponse, uint(i), forkID)
receipt, imStateRoot = GenerateReceipt(header.Number, txResponse, uint(i), forkID)
receipts = append(receipts, receipt)
}

Expand All @@ -274,7 +276,7 @@ func (s *State) StoreL2Block(ctx context.Context, batchNumber uint64, l2Block *P
}

// Store L2 block and its transactions
if err := s.AddL2Block(ctx, batchNumber, block, receipts, txsL2Hash, storeTxsEGPData, dbTx); err != nil {
if err := s.AddL2Block(ctx, batchNumber, block, receipts, txsL2Hash, storeTxsEGPData, imStateRoot, dbTx); err != nil {
return err
}

Expand Down Expand Up @@ -661,7 +663,7 @@ func (s *State) StoreTransaction(ctx context.Context, batchNumber uint64, proces
header.BlockInfoRoot = blockInfoRoot
transactions := []*types.Transaction{&processedTx.Tx}

receipt := GenerateReceipt(header.Number, processedTx, 0, forkID)
receipt, imStateRoot := GenerateReceipt(header.Number, processedTx, 0, forkID)
receipts := []*types.Receipt{receipt}

// Create l2Block to be able to calculate its hash
Expand All @@ -675,7 +677,7 @@ func (s *State) StoreTransaction(ctx context.Context, batchNumber uint64, proces
txsL2Hash := []common.Hash{processedTx.TxHashL2_V2}

// Store L2 block and its transaction
if err := s.AddL2Block(ctx, batchNumber, l2Block, receipts, txsL2Hash, storeTxsEGPData, dbTx); err != nil {
if err := s.AddL2Block(ctx, batchNumber, l2Block, receipts, txsL2Hash, storeTxsEGPData, imStateRoot, dbTx); err != nil {
return nil, err
}

Expand Down
87 changes: 87 additions & 0 deletions synchronizer/l2_sync/l2_shared/mocks/post_closed_batch_checker.go

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

Loading
Loading