From 7e10d02aa428920999ab99bd6bedb3cb681a1675 Mon Sep 17 00:00:00 2001 From: Valentin Staykov Date: Wed, 28 Aug 2024 11:33:25 +0000 Subject: [PATCH] check if forkid8 is on --- core/rawdb/accessors_chain_zkevm.go | 25 ++++++++++++++++++++++++- core/types/receipt_zkevm.go | 5 ++--- zk/hermez_db/db.go | 23 +++++++++++++++++++++++ 3 files changed, 49 insertions(+), 4 deletions(-) diff --git a/core/rawdb/accessors_chain_zkevm.go b/core/rawdb/accessors_chain_zkevm.go index 905a9e71696..19b59b99d8c 100644 --- a/core/rawdb/accessors_chain_zkevm.go +++ b/core/rawdb/accessors_chain_zkevm.go @@ -10,6 +10,7 @@ import ( "github.com/gateway-fm/cdk-erigon-lib/kv" "github.com/gateway-fm/cdk-erigon-lib/kv/kvcfg" "github.com/ledgerwatch/erigon/common/dbutils" + "github.com/ledgerwatch/erigon/common/math" "github.com/ledgerwatch/erigon/core/types" ethTypes "github.com/ledgerwatch/erigon/core/types" "github.com/ledgerwatch/erigon/rlp" @@ -193,7 +194,29 @@ func ReadReceipts_zkEvm(db kv.Tx, block *types.Block, senders []libcommon.Addres //[hack] there was a cumulativeGasUsed bug priod to forkid8, so we need to check for it hermezDb := hermez_db.NewHermezDbReader(db) - forkid8BlockNum, _, _ := hermezDb.GetForkIdBlock(8) + forkBlocks, err := hermezDb.GetAllForkBlocks() + if err != nil { + log.Error("Failed to get fork blocks", "err", err, "stack", dbg.Stack()) + return nil + } + + forkid8BlockNum := uint64(0) + highestForkId := uint64(0) + for forkId, forkBlock := range forkBlocks { + if forkId > highestForkId { + highestForkId = forkId + } + if forkId == 8 { + forkid8BlockNum = forkBlock + break + } + } + + // if we don't have forkid8 and highest saved is lower, then we are lower than forkid + // otherwise we are higher than forkid8 but don't have it saved so it should be treated as if it was 0 + if forkid8BlockNum == 0 && highestForkId < 8 { + forkid8BlockNum = math.MaxUint64 + } if err := receipts.DeriveFields_zkEvm(forkid8BlockNum, block.Hash(), block.NumberU64(), block.Transactions(), senders); err != nil { log.Error("Failed to derive block receipts fields", "hash", block.Hash(), "number", block.NumberU64(), "err", err, "stack", dbg.Stack()) diff --git a/core/types/receipt_zkevm.go b/core/types/receipt_zkevm.go index bd245611a32..81b0ea3645a 100644 --- a/core/types/receipt_zkevm.go +++ b/core/types/receipt_zkevm.go @@ -94,9 +94,8 @@ func (r Receipts) DeriveFields_zkEvm(forkId8BlockNum uint64, hash common.Hash, n } // The used gas can be calculated based on previous r // [hack] there was a cumulativeGasUsed bug priod to forkid8, so we need to check for it - // if the block is before forkId8 or forkid8 is not even on yet - // comuluative is equal to gas used - if i == 0 || number < forkId8BlockNum || forkId8BlockNum == 0 { + // if the block is before forkId8 comuluative is equal to gas used + if i == 0 || number < forkId8BlockNum { r[i].GasUsed = r[i].CumulativeGasUsed } else { r[i].GasUsed = r[i].CumulativeGasUsed - r[i-1].CumulativeGasUsed diff --git a/zk/hermez_db/db.go b/zk/hermez_db/db.go index 2adbf23cee7..aa1a3bcc319 100644 --- a/zk/hermez_db/db.go +++ b/zk/hermez_db/db.go @@ -1084,6 +1084,29 @@ func (db *HermezDbReader) GetForkIdBlock(forkId uint64) (uint64, bool, error) { return blockNum, found, err } +func (db *HermezDbReader) GetAllForkBlocks() (map[uint64]uint64, error) { + c, err := db.tx.Cursor(FORKID_BLOCK) + if err != nil { + return nil, err + } + defer c.Close() + + forkBlocks := make(map[uint64]uint64) + var k, v []byte + + for k, v, err = c.First(); k != nil; k, v, err = c.Next() { + if err != nil { + break + } + currentForkId := BytesToUint64(k) + blockNum := BytesToUint64(v) + + forkBlocks[currentForkId] = blockNum + } + + return forkBlocks, err +} + func (db *HermezDb) DeleteForkIdBlock(fromBlockNo, toBlockNo uint64) error { return db.deleteFromBucketWithUintKeysRange(FORKID_BLOCK, fromBlockNo, toBlockNo) }