diff --git a/tool/db/db.go b/tool/db/db.go index b996649..29a1ef9 100644 --- a/tool/db/db.go +++ b/tool/db/db.go @@ -41,7 +41,10 @@ func NewSQLDB(cfg Config) (*pgxpool.Pool, error) { } func (db *StateDB) GetL2Blocks(ctx context.Context, limit, offset uint) ([]*L2Block, error) { - const l2BlockSQL = "SELECT batch_num, block_num, created_at, state_root, header->>'miner' AS coinbase FROM state.l2block ORDER BY block_num ASC limit $1 offset $2" + const l2BlockSQL = `SELECT l2b.batch_num, l2b.block_num, l2b.created_at, b.global_exit_root, l2b.header->>'miner' AS coinbase + FROM state.l2block l2b, state.batch b + WHERE l2b.batch_num = b.batch_num + ORDER BY l2b.block_num ASC limit $1 offset $2` rows, err := db.Query(ctx, l2BlockSQL, limit, offset) if err != nil { @@ -65,25 +68,28 @@ func (db *StateDB) GetL2Blocks(ctx context.Context, limit, offset uint) ([]*L2Bl func scanL2Block(row pgx.Row) (*L2Block, error) { l2Block := L2Block{} var ( - rootStr string + gerStr string coinbaseStr string ) if err := row.Scan( - &l2Block.BatchNum, - &l2Block.BlockNum, + &l2Block.BatchNumber, + &l2Block.L2BlockNumber, &l2Block.Timestamp, - &rootStr, + &gerStr, &coinbaseStr, ); err != nil { return &l2Block, err } - l2Block.StateRoot = common.HexToHash(rootStr) + l2Block.GlobalExitRoot = common.HexToHash(gerStr) l2Block.Coinbase = common.HexToAddress(coinbaseStr) return &l2Block, nil } func (db *StateDB) GetL2Transactions(ctx context.Context, minL2Block, maxL2Block uint64) ([]*L2Transaction, error) { - const l2TxSQL = "SELECT effective_percentage, encoded, l2_block_num from state.transaction WHERE l2_block_num between $1 and $2 ORDER BY l2_block_num ASC" + const l2TxSQL = `SELECT t.effective_percentage, t.encoded, b.batch_num + FROM state.transaction t, state.batch b, state.l2block l2b + WHERE l2_block_num BETWEEN $1 AND $2 AND t.l2_block_num = l2b.block_num AND l2b.batch_num = b.batch_num + ORDER BY t.l2_block_num ASC` rows, err := db.Query(ctx, l2TxSQL, minL2Block, maxL2Block) if err != nil { @@ -110,9 +116,9 @@ func scanL2Transaction(row pgx.Row) (*L2Transaction, error) { encodedStr string ) if err := row.Scan( - &l2Transaction.EffectivePercentage, + &l2Transaction.EffectiveGasPricePercentage, &encodedStr, - &l2Transaction.BlockNum, + &l2Transaction.BatchNumber, ); err != nil { return &l2Transaction, err } diff --git a/tool/db/types.go b/tool/db/types.go index 3e8eae4..f611def 100644 --- a/tool/db/types.go +++ b/tool/db/types.go @@ -8,11 +8,11 @@ import ( ) type L2Block struct { - BatchNum uint64 - BlockNum uint64 - Timestamp time.Time - StateRoot common.Hash - Coinbase common.Address + BatchNumber uint64 + L2BlockNumber uint64 + Timestamp time.Time + GlobalExitRoot common.Hash + Coinbase common.Address } // Encode returns the encoded L2Block as a byte slice @@ -22,12 +22,11 @@ func (b L2Block) Encode() []byte { } type L2Transaction struct { - BatchNum uint64 - BlockNum uint64 - EffectivePercentage uint8 - IsValid uint8 - EncodedLength uint32 - Encoded []byte + BatchNumber uint64 + EffectiveGasPricePercentage uint8 + IsValid uint8 + EncodedLength uint32 + Encoded []byte } // Encode returns the encoded L2Transaction as a byte slice diff --git a/tool/main.go b/tool/main.go index 0bc882a..4a851bf 100644 --- a/tool/main.go +++ b/tool/main.go @@ -121,7 +121,7 @@ func start(cliCtx *cli.Context) error { break } // Get transactions for all the retrieved l2 blocks - l2Transactions, err := stateDB.GetL2Transactions(cliCtx.Context, l2blocks[0].BlockNum, l2blocks[len(l2blocks)-1].BlockNum) + l2Transactions, err := stateDB.GetL2Transactions(cliCtx.Context, l2blocks[0].L2BlockNumber, l2blocks[len(l2blocks)-1].L2BlockNumber) if err != nil { log.Fatal(err) } @@ -137,13 +137,13 @@ func start(cliCtx *cli.Context) error { log.Fatal(err) } - if l2Transactions[x].BlockNum == l2block.BlockNum { + if l2Transactions[x].BatchNumber == l2block.BatchNumber { entry, err = streamServer.AddStreamEntry(2, l2Transactions[x].Encode()) if err != nil { log.Fatal(err) } } else { - log.Fatalf("Mismatch between l2 block and transaction: %d != %d", l2Transactions[x].BlockNum, l2block.BlockNum) + log.Fatalf("Mismatch between transaction and l2block batch numbers: %d != %d", l2Transactions[x].BatchNumber, l2block.BatchNumber) } } err = streamServer.CommitStreamTx()