Skip to content

Commit

Permalink
check if forkid8 is on
Browse files Browse the repository at this point in the history
  • Loading branch information
V-Staykov committed Aug 28, 2024
1 parent 26ea489 commit 7e10d02
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 4 deletions.
25 changes: 24 additions & 1 deletion core/rawdb/accessors_chain_zkevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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())
Expand Down
5 changes: 2 additions & 3 deletions core/types/receipt_zkevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions zk/hermez_db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 7e10d02

Please sign in to comment.