Skip to content

Commit

Permalink
fix for using an info tree index with a re-used L1 block hash (#1041)
Browse files Browse the repository at this point in the history
  • Loading branch information
hexoscott authored Aug 28, 2024
1 parent 8c3b1fa commit 13fab2c
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 48 deletions.
1 change: 0 additions & 1 deletion core/state/intra_block_state_zkevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ type ReadOnlyHermezDb interface {
GetBatchGlobalExitRoots(fromBatchNum, toBatchNum uint64) (*[]dstypes.GerUpdate, error)
GetBlockGlobalExitRoot(l2BlockNo uint64) (libcommon.Hash, error)
GetBlockL1BlockHash(l2BlockNo uint64) (libcommon.Hash, error)
GetGerForL1BlockHash(l1BlockHash libcommon.Hash) (libcommon.Hash, error)
GetIntermediateTxStateRoot(blockNum uint64, txhash libcommon.Hash) (libcommon.Hash, error)
GetReusedL1InfoTreeIndex(blockNum uint64) (bool, error)
GetSequenceByBatchNo(batchNo uint64) (*zktypes.L1BatchInfo, error)
Expand Down
25 changes: 0 additions & 25 deletions zk/hermez_db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -694,19 +694,6 @@ func (db *HermezDb) DeleteReusedL1InfoTreeIndexes(fromBlock, toBlock uint64) err
return nil
}

func (db *HermezDb) WriteGerForL1BlockHash(l1BlockHash common.Hash, ger common.Hash) error {
return db.tx.Put(L1_BLOCK_HASH_GER, l1BlockHash.Bytes(), ger.Bytes())
}

func (db *HermezDbReader) GetGerForL1BlockHash(l1BlockHash common.Hash) (common.Hash, error) {
bytes, err := db.tx.GetOne(L1_BLOCK_HASH_GER, l1BlockHash.Bytes())
if err != nil {
return common.Hash{}, err
}

return common.BytesToHash(bytes), nil
}

func (db *HermezDb) DeleteL1BlockHashGers(l1BlockHashes *[]common.Hash) error {
for _, l1BlockHash := range *l1BlockHashes {
err := db.tx.Delete(L1_BLOCK_HASH_GER, l1BlockHash.Bytes())
Expand All @@ -718,18 +705,6 @@ func (db *HermezDb) DeleteL1BlockHashGers(l1BlockHashes *[]common.Hash) error {
return nil
}

func (db *HermezDb) WriteL1BlockHash(l1BlockHash common.Hash) error {
return db.tx.Put(L1_BLOCK_HASHES, l1BlockHash.Bytes(), []byte{1})
}

func (db *HermezDbReader) CheckL1BlockHashWritten(l1BlockHash common.Hash) (bool, error) {
bytes, err := db.tx.GetOne(L1_BLOCK_HASHES, l1BlockHash.Bytes())
if err != nil {
return false, err
}
return len(bytes) > 0, nil
}

func (db *HermezDb) DeleteL1BlockHashes(l1BlockHashes *[]common.Hash) error {
for _, l1BlockHash := range *l1BlockHashes {
err := db.tx.Delete(L1_BLOCK_HASHES, l1BlockHash.Bytes())
Expand Down
24 changes: 2 additions & 22 deletions zk/stages/stage_batches.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,7 @@ type HermezDb interface {
DeleteReusedL1InfoTreeIndexes(fromBlockNum, toBlockNum uint64) error
WriteBlockL1BlockHash(l2BlockNo uint64, l1BlockHash common.Hash) error
DeleteBlockL1BlockHashes(fromBlockNum, toBlockNum uint64) error
WriteL1BlockHash(l1BlockHash common.Hash) error
CheckL1BlockHashWritten(l1BlockHash common.Hash) (bool, error)
DeleteL1BlockHashes(l1BlockHashes *[]common.Hash) error
WriteGerForL1BlockHash(l1BlockHash, ger common.Hash) error
DeleteL1BlockHashGers(l1BlockHashes *[]common.Hash) error
WriteBatchGlobalExitRoot(batchNumber uint64, ger *types.GerUpdate) error
WriteIntermediateTxStateRoot(l2BlockNumber uint64, txHash common.Hash, rpcRoot common.Hash) error
Expand Down Expand Up @@ -816,25 +813,8 @@ func writeL2Block(eriDb ErigonDb, hermezDb HermezDb, l2Block *types.FullL2Block,
}

if l2Block.L1BlockHash != emptyHash {
l1BlockHashWritten, err := hermezDb.CheckL1BlockHashWritten(l2Block.L1BlockHash)
if err != nil {
return fmt.Errorf("get global exit root error: %v", err)
}

if !l1BlockHashWritten {
if err := hermezDb.WriteBlockL1BlockHash(l2Block.L2BlockNumber, l2Block.L1BlockHash); err != nil {
return fmt.Errorf("write block global exit root error: %v", err)
}

if err := hermezDb.WriteL1BlockHash(l2Block.L1BlockHash); err != nil {
return fmt.Errorf("write global exit root error: %v", err)
}

if l2Block.GlobalExitRoot != emptyHash {
if err := hermezDb.WriteGerForL1BlockHash(l2Block.L1BlockHash, l2Block.GlobalExitRoot); err != nil {
return fmt.Errorf("write ger for l1 block hash error: %v", err)
}
}
if err := hermezDb.WriteBlockL1BlockHash(l2Block.L2BlockNumber, l2Block.L1BlockHash); err != nil {
return fmt.Errorf("write block global exit root error: %v", err)
}
}

Expand Down

0 comments on commit 13fab2c

Please sign in to comment.