Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check if forkid8 is on #1048

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
5 changes: 3 additions & 2 deletions core/types/receipt_zkevm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@
package types

import (
"math"
"math/big"
"testing"

"github.com/holiman/uint256"
libcommon "github.com/gateway-fm/cdk-erigon-lib/common"
"github.com/holiman/uint256"

"github.com/ledgerwatch/erigon/common/u256"
"github.com/ledgerwatch/erigon/crypto"
Expand Down Expand Up @@ -105,7 +106,7 @@ func TestDeriveFields_zkEvm_preForkId8(t *testing.T) {
hash := libcommon.BytesToHash([]byte{0x03, 0x14})

clearComputedFieldsOnReceipts(t, receipts)
if err := receipts.DeriveFields_zkEvm(0, hash, number.Uint64(), txs, []libcommon.Address{libcommon.BytesToAddress([]byte{0x0}), libcommon.BytesToAddress([]byte{0x0}), libcommon.BytesToAddress([]byte{0x0})}); err != nil {
if err := receipts.DeriveFields_zkEvm(math.MaxUint64, hash, number.Uint64(), txs, []libcommon.Address{libcommon.BytesToAddress([]byte{0x0}), libcommon.BytesToAddress([]byte{0x0}), libcommon.BytesToAddress([]byte{0x0})}); err != nil {
t.Fatalf("DeriveFields_zkEvm(...) = %v, want <nil>", err)
}
// Iterate over all the computed fields and check that they're correct
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 @@ -1080,6 +1080,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
Loading