Skip to content

Commit

Permalink
Add LER to stream (#3410)
Browse files Browse the repository at this point in the history
* add LER to stream

* fix test
  • Loading branch information
ToniRamirezM authored Mar 4, 2024
1 parent 6d8d4e6 commit 2394a66
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 17 deletions.
4 changes: 3 additions & 1 deletion sequencer/datastreamer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package sequencer

import (
"github.com/0xPolygonHermez/zkevm-node/state"
"github.com/ethereum/go-ethereum/common"
)

func (f *finalizer) DSSendL2Block(batchNumber uint64, blockResponse *state.ProcessBlockResponse, l1InfoTreeIndex uint32) error {
func (f *finalizer) DSSendL2Block(batchNumber uint64, blockResponse *state.ProcessBlockResponse, l1InfoTreeIndex uint32, localExitRoot common.Hash) error {
forkID := f.stateIntf.GetForkIDByBatchNumber(batchNumber)

// Send data to streamer
Expand All @@ -20,6 +21,7 @@ func (f *finalizer) DSSendL2Block(batchNumber uint64, blockResponse *state.Proce
ForkID: uint16(forkID),
BlockHash: blockResponse.BlockHash,
StateRoot: blockResponse.BlockHash, //From etrog, the blockhash is the block root
LocalExitRoot: localExitRoot,
}

l2Transactions := []state.DSL2Transaction{}
Expand Down
2 changes: 1 addition & 1 deletion sequencer/forcedbatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ func (f *finalizer) handleProcessForcedBatchResponse(ctx context.Context, newBat
}

// Send L2 block to data streamer
err = f.DSSendL2Block(newBatchNumber, forcedL2BlockResponse, 0)
err = f.DSSendL2Block(newBatchNumber, forcedL2BlockResponse, 0, batchResponse.NewLocalExitRoot)
if err != nil {
//TODO: we need to halt/rollback the L2 block if we had an error sending to the data streamer?
log.Errorf("error sending L2 block %d to data streamer, error: %v", forcedL2BlockResponse.BlockNumber, err)
Expand Down
2 changes: 1 addition & 1 deletion sequencer/l2block.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ func (f *finalizer) storeL2Block(ctx context.Context, l2Block *L2Block) error {
}

// Send L2 block to data streamer
err = f.DSSendL2Block(f.wipBatch.batchNumber, blockResponse, l2Block.getL1InfoTreeIndex())
err = f.DSSendL2Block(f.wipBatch.batchNumber, blockResponse, l2Block.getL1InfoTreeIndex(), l2Block.batchResponse.NewLocalExitRoot)
if err != nil {
//TODO: we need to halt/rollback the L2 block if we had an error sending to the data streamer?
log.Errorf("error sending L2 block %d [%d] to data streamer, error: %v", blockResponse.BlockNumber, l2Block.trackingNum, err)
Expand Down
8 changes: 6 additions & 2 deletions state/datastream.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ type DSL2Block struct {
Coinbase common.Address // 20 bytes
ForkID uint16 // 2 bytes
ChainID uint32 // 4 bytes
LocalExitRoot common.Hash // 32 bytes
BlockHash common.Hash // 32 bytes
StateRoot common.Hash // 32 bytes
}
Expand All @@ -80,7 +81,7 @@ type DSL2BlockStart struct {
Coinbase common.Address // 20 bytes
ForkID uint16 // 2 bytes
ChainID uint32 // 4 bytes

LocalExitRoot common.Hash // 32 bytes
}

// Encode returns the encoded DSL2BlockStart as a byte slice
Expand All @@ -96,6 +97,7 @@ func (b DSL2BlockStart) Encode() []byte {
bytes = append(bytes, b.Coinbase.Bytes()...)
bytes = binary.BigEndian.AppendUint16(bytes, b.ForkID)
bytes = binary.BigEndian.AppendUint32(bytes, b.ChainID)
bytes = append(bytes, b.LocalExitRoot.Bytes()...)
return bytes
}

Expand All @@ -111,7 +113,7 @@ func (b DSL2BlockStart) Decode(data []byte) DSL2BlockStart {
b.Coinbase = common.BytesToAddress(data[96:116])
b.ForkID = binary.BigEndian.Uint16(data[116:118])
b.ChainID = binary.BigEndian.Uint32(data[118:122])

b.LocalExitRoot = common.BytesToHash(data[122:154])
return b
}

Expand Down Expand Up @@ -290,6 +292,7 @@ func GenerateDataStreamerFile(ctx context.Context, streamServer *datastreamer.St
Coinbase: genesisL2Block.Coinbase,
ForkID: genesisL2Block.ForkID,
ChainID: uint32(chainID),
LocalExitRoot: genesisL2Block.LocalExitRoot,
}

log.Infof("Genesis block: %+v", genesisBlock)
Expand Down Expand Up @@ -527,6 +530,7 @@ func GenerateDataStreamerFile(ctx context.Context, streamServer *datastreamer.St
Coinbase: l2Block.Coinbase,
ForkID: l2Block.ForkID,
ChainID: uint32(chainID),
LocalExitRoot: l2Block.LocalExitRoot,
}

previousTimestamp = l2Block.Timestamp
Expand Down
19 changes: 11 additions & 8 deletions state/pgstatestorage/datastream.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

// GetDSGenesisBlock returns the genesis block
func (p *PostgresStorage) GetDSGenesisBlock(ctx context.Context, dbTx pgx.Tx) (*state.DSL2Block, error) {
const genesisL2BlockSQL = `SELECT 0 as batch_num, l2b.block_num, l2b.received_at, '0x0000000000000000000000000000000000000000' as global_exit_root, '0x0000000000000000000000000000000000000000' as block_global_exit_root, l2b.header->>'miner' AS coinbase, 0 as fork_id, l2b.block_hash, l2b.state_root
const genesisL2BlockSQL = `SELECT 0 as batch_num, l2b.block_num, l2b.received_at, '0x0000000000000000000000000000000000000000' as global_exit_root, '0x0000000000000000000000000000000000000000' as block_global_exit_root, l2b.header->>'miner' AS coinbase, 0 as fork_id, l2b.block_hash, l2b.state_root, '0x0000000000000000000000000000000000000000' as local_exit_root
FROM state.l2block l2b
WHERE l2b.block_num = 0`

Expand All @@ -29,7 +29,7 @@ func (p *PostgresStorage) GetDSGenesisBlock(ctx context.Context, dbTx pgx.Tx) (*

// GetDSL2Blocks returns the L2 blocks
func (p *PostgresStorage) GetDSL2Blocks(ctx context.Context, firstBatchNumber, lastBatchNumber uint64, dbTx pgx.Tx) ([]*state.DSL2Block, error) {
const l2BlockSQL = `SELECT l2b.batch_num, l2b.block_num, l2b.received_at, b.global_exit_root, COALESCE(l2b.header->>'globalExitRoot', '') AS block_global_exit_root, l2b.header->>'miner' AS coinbase, f.fork_id, l2b.block_hash, l2b.state_root
const l2BlockSQL = `SELECT l2b.batch_num, l2b.block_num, l2b.received_at, b.global_exit_root, COALESCE(l2b.header->>'globalExitRoot', '') AS block_global_exit_root, l2b.header->>'miner' AS coinbase, f.fork_id, l2b.block_hash, l2b.state_root, b.local_exit_root
FROM state.l2block l2b, state.batch b, state.fork_id f
WHERE l2b.batch_num BETWEEN $1 AND $2 AND l2b.batch_num = b.batch_num AND l2b.batch_num between f.from_batch_num AND f.to_batch_num
ORDER BY l2b.block_num ASC`
Expand All @@ -56,12 +56,13 @@ func (p *PostgresStorage) GetDSL2Blocks(ctx context.Context, firstBatchNumber, l
func scanL2Block(row pgx.Row) (*state.DSL2Block, error) {
l2Block := state.DSL2Block{}
var (
gerStr string
blockGERStr string
coinbaseStr string
timestamp time.Time
blockHashStr string
stateRootStr string
gerStr string
blockGERStr string
coinbaseStr string
timestamp time.Time
blockHashStr string
stateRootStr string
localExitRootStr string
)
if err := row.Scan(
&l2Block.BatchNumber,
Expand All @@ -73,6 +74,7 @@ func scanL2Block(row pgx.Row) (*state.DSL2Block, error) {
&l2Block.ForkID,
&blockHashStr,
&stateRootStr,
&localExitRootStr,
); err != nil {
return &l2Block, err
}
Expand All @@ -81,6 +83,7 @@ func scanL2Block(row pgx.Row) (*state.DSL2Block, error) {
l2Block.Timestamp = timestamp.Unix()
l2Block.BlockHash = common.HexToHash(blockHashStr)
l2Block.StateRoot = common.HexToHash(stateRootStr)
l2Block.LocalExitRoot = common.HexToHash(localExitRootStr)

if l2Block.ForkID >= state.FORKID_ETROG {
l2Block.GlobalExitRoot = common.HexToHash(blockGERStr)
Expand Down
4 changes: 3 additions & 1 deletion state/test/datastream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func TestL2BlockStartEncode(t *testing.T) {
Coinbase: common.HexToAddress("0x08"),
ForkID: 9,
ChainID: 10,
LocalExitRoot: common.HexToHash("0x0a"),
}

encoded := l2BlockStart.Encode()
Expand All @@ -35,7 +36,8 @@ func TestL2BlockStartEncode(t *testing.T) {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8,
0, 9,
0, 0, 0, 10}
0, 0, 0, 10,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10}

assert.Equal(t, expected, encoded)
}
Expand Down
2 changes: 1 addition & 1 deletion test/config/debug.node.config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ StateConsistencyCheckInterval = "5s"
[Sequencer.StreamServer]
Port = 6900
Filename = "/datastreamer/datastream.bin"
Version = 1
Version = 3
ChainID = 1337
Enabled = false

Expand Down
2 changes: 1 addition & 1 deletion test/config/test.node.config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ StateConsistencyCheckInterval = "5s"
[Sequencer.StreamServer]
Port = 6900
Filename = "/datastreamer/datastream.bin"
Version = 1
Version = 3
ChainID = 1337
Enabled = true

Expand Down
2 changes: 1 addition & 1 deletion tools/datastreamer/config/tool.config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ StreamType = 1
[Offline]
Port = 6901
Filename = "datastream.bin"
Version = 1
Version = 3
ChainID = 1440
UpgradeEtrogBatchNumber = 0

Expand Down
2 changes: 2 additions & 0 deletions tools/datastreamer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,8 @@ func printEntry(entry datastreamer.FileEntry) {
printColored(color.FgHiWhite, fmt.Sprintf("%d\n", blockStart.ForkID))
printColored(color.FgGreen, "Chain ID........: ")
printColored(color.FgHiWhite, fmt.Sprintf("%d\n", blockStart.ChainID))
printColored(color.FgGreen, "Local Exit Root.: ")
printColored(color.FgHiWhite, fmt.Sprintf("%s\n", blockStart.LocalExitRoot))
case state.EntryTypeL2Tx:
dsTx := state.DSL2Transaction{}.Decode(entry.Data)
printColored(color.FgGreen, "Entry Type......: ")
Expand Down

0 comments on commit 2394a66

Please sign in to comment.