Skip to content

Commit

Permalink
follow spec (#6)
Browse files Browse the repository at this point in the history
* follow spec

* improve log
  • Loading branch information
ToniRamirezM authored Sep 6, 2023
1 parent 4c0f9e4 commit bac1043
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 23 deletions.
24 changes: 15 additions & 9 deletions tool/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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
}
Expand Down
21 changes: 10 additions & 11 deletions tool/db/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions tool/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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()
Expand Down

0 comments on commit bac1043

Please sign in to comment.