Skip to content

Commit

Permalink
Get IM State root from receipt for data stream. (0xPolygonHermez#3400) (
Browse files Browse the repository at this point in the history
0xPolygonHermez#3403)

* Get IM State root from receipt for data stream. (0xPolygonHermez#3400)

* get im state root from receipt

* get im state root from receipt

* get im state root from receipt

* optimize tool
  • Loading branch information
ToniRamirezM authored Feb 29, 2024
1 parent 24d21a2 commit 0671dde
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
20 changes: 11 additions & 9 deletions state/datastream.go
Original file line number Diff line number Diff line change
Expand Up @@ -553,16 +553,18 @@ func GenerateDataStreamerFile(ctx context.Context, streamServer *datastreamer.St
}

for _, tx := range l2Block.Txs {
// Populate intermediate state root
if imStateRoots == nil || (*imStateRoots)[blockStart.L2BlockNumber] == nil {
position := GetSystemSCPosition(l2Block.L2BlockNumber)
imStateRoot, err := stateDB.GetStorageAt(ctx, common.HexToAddress(SystemSC), big.NewInt(0).SetBytes(position), l2Block.StateRoot)
if err != nil {
return err
if l2Block.ForkID < FORKID_ETROG {
// Populate intermediate state root with information from the system SC (or cache if available)
if imStateRoots == nil || (*imStateRoots)[blockStart.L2BlockNumber] == nil {
position := GetSystemSCPosition(l2Block.L2BlockNumber)
imStateRoot, err := stateDB.GetStorageAt(ctx, common.HexToAddress(SystemSC), big.NewInt(0).SetBytes(position), l2Block.StateRoot)
if err != nil {
return err
}
tx.StateRoot = common.BigToHash(imStateRoot)
} else {
tx.StateRoot = common.BytesToHash((*imStateRoots)[blockStart.L2BlockNumber])
}
tx.StateRoot = common.BigToHash(imStateRoot)
} else {
tx.StateRoot = common.BytesToHash((*imStateRoots)[blockStart.L2BlockNumber])
}

_, err = streamServer.AddStreamEntry(EntryTypeL2Tx, tx.Encode())
Expand Down
5 changes: 4 additions & 1 deletion state/pgstatestorage/datastream.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func scanL2Block(row pgx.Row) (*state.DSL2Block, error) {

// GetDSL2Transactions returns the L2 transactions
func (p *PostgresStorage) GetDSL2Transactions(ctx context.Context, firstL2Block, lastL2Block uint64, dbTx pgx.Tx) ([]*state.DSL2Transaction, error) {
const l2TxSQL = `SELECT l2_block_num, t.effective_percentage, t.encoded
const l2TxSQL = `SELECT l2_block_num, t.effective_percentage, t.encoded, r.post_state
FROM state.transaction t, state.receipt r
WHERE l2_block_num BETWEEN $1 AND $2 AND r.tx_hash = t.hash
ORDER BY t.l2_block_num ASC, r.tx_index ASC`
Expand Down Expand Up @@ -119,10 +119,12 @@ func (p *PostgresStorage) GetDSL2Transactions(ctx context.Context, firstL2Block,
func scanDSL2Transaction(row pgx.Row) (*state.DSL2Transaction, error) {
l2Transaction := state.DSL2Transaction{}
encoded := []byte{}
postState := []byte{}
if err := row.Scan(
&l2Transaction.L2BlockNumber,
&l2Transaction.EffectiveGasPricePercentage,
&encoded,
&postState,
); err != nil {
return nil, err
}
Expand All @@ -139,6 +141,7 @@ func scanDSL2Transaction(row pgx.Row) (*state.DSL2Transaction, error) {
l2Transaction.Encoded = binaryTxData
l2Transaction.EncodedLength = uint32(len(l2Transaction.Encoded))
l2Transaction.IsValid = 1
l2Transaction.StateRoot = common.BytesToHash(postState)
return &l2Transaction, nil
}

Expand Down
5 changes: 5 additions & 0 deletions tools/datastreamer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,11 @@ func getImStateRoots(ctx context.Context, start, end uint64, isStateRoots *map[u
log.Errorf("Error: %v\n", err)
os.Exit(1)
}

if common.BytesToHash(imStateRoot.Bytes()) == state.ZeroHash && x != 0 {
break
}

imStateRootMux.Lock()
(*isStateRoots)[x] = imStateRoot.Bytes()
imStateRootMux.Unlock()
Expand Down

0 comments on commit 0671dde

Please sign in to comment.