From 9239ac3312a6689f272ab417b3517d6eb924a800 Mon Sep 17 00:00:00 2001 From: Jerry Date: Mon, 28 Oct 2024 00:46:17 -0700 Subject: [PATCH] Backport 2.2.0 fixes to zkevm (#1364) * batches support fork 13 * Add ImpossibleForkId * Add IsForkID13Durian for chain rules (#1362) --------- Co-authored-by: Scott Fairclough Co-authored-by: zhangkai --- core/vm/zk_counters.go | 6 +++--- core/vm/zk_transaction_counters.go | 4 ++-- erigon-lib/chain/chain_config.go | 4 ++++ erigon-lib/chain/zk_constants.go | 6 +++++- zk/stages/stage_batches.go | 1 - zk/stages/stage_batches_processor.go | 2 +- 6 files changed, 15 insertions(+), 8 deletions(-) diff --git a/core/vm/zk_counters.go b/core/vm/zk_counters.go index 934bacf5c1a..258a911b413 100644 --- a/core/vm/zk_counters.go +++ b/core/vm/zk_counters.go @@ -6,9 +6,9 @@ import ( "math/big" "github.com/holiman/uint256" + "github.com/ledgerwatch/erigon-lib/chain" "github.com/ledgerwatch/erigon/core/types" "github.com/ledgerwatch/erigon/zk/hermez_db" - "github.com/ledgerwatch/erigon-lib/chain" ) const ( @@ -610,7 +610,7 @@ func (cc *CounterCollector) finishBatchProcessing() { func (cc *CounterCollector) isColdAddress() { cc.Deduct(S, 100) - if cc.forkId >= chain.ForkId13Durian { + if cc.forkId >= uint16(chain.ForkId13Durian) { cc.Deduct(B, 3+1) } else { cc.Deduct(B, 2+1) @@ -850,7 +850,7 @@ func (cc *CounterCollector) preModExp(callDataLength, returnDataLength, bLen, mL func (cc *CounterCollector) modExp(bLen, mLen, eLen int, base, exponent, modulus *big.Int) { var steps, binary, arith *big.Int - if cc.forkId >= chain.ForkId13Durian { + if cc.forkId >= uint16(chain.ForkId13Durian) { steps, binary, arith = expectedModExpCounters( int(math.Ceil(float64(bLen)/32)), int(math.Ceil(float64(eLen)/32)), diff --git a/core/vm/zk_transaction_counters.go b/core/vm/zk_transaction_counters.go index d81b0255065..5ac7d228e9a 100644 --- a/core/vm/zk_transaction_counters.go +++ b/core/vm/zk_transaction_counters.go @@ -4,11 +4,11 @@ import ( "fmt" "math" + "github.com/ledgerwatch/erigon-lib/chain" "github.com/ledgerwatch/erigon-lib/common/hexutil" "github.com/ledgerwatch/erigon/core/state" "github.com/ledgerwatch/erigon/core/types" "github.com/ledgerwatch/erigon/zk/tx" - "github.com/ledgerwatch/erigon-lib/chain" ) type TransactionCounter struct { @@ -183,7 +183,7 @@ func (tc *TransactionCounter) ProcessTx(ibs *state.IntraBlockState, returnData [ cc := NewCounterCollector(tc.smtLevels, tc.forkId) cc.Deduct(S, 300) - if tc.forkId >= chain.ForkId13Durian { + if tc.forkId >= uint16(chain.ForkId13Durian) { cc.Deduct(B, 12+7) } else { cc.Deduct(B, 11+7) diff --git a/erigon-lib/chain/chain_config.go b/erigon-lib/chain/chain_config.go index e9cccd49da7..b3445ca3462 100644 --- a/erigon-lib/chain/chain_config.go +++ b/erigon-lib/chain/chain_config.go @@ -29,6 +29,7 @@ import ( // this needs to always be in descending order // add new forkIds at the beginning of the array var ForkIdsOrdered = []ForkId{ + ForkId13Durian, ForkID12Banana, ForkID11, ForkID10, @@ -180,6 +181,8 @@ func (c *Config) SetForkIdBlock(forkIdNumber ForkId, blockNum uint64) error { c.ForkID11 = new(big.Int).SetUint64(blockNum) case ForkID12Banana: c.ForkID12BananaBlock = new(big.Int).SetUint64(blockNum) + case ForkId13Durian: + c.ForkId13Durian = new(big.Int).SetUint64(blockNum) default: return fmt.Errorf("unknown fork id number %d", forkIdNumber) } @@ -665,6 +668,7 @@ func (c *Config) Rules(num uint64, time uint64) *Rules { IsForkId10: c.IsForkID10(num), IsForkId11: c.IsForkID11(num), IsForkID12Banana: c.IsForkID12Banana(num), + IsForkID13Durian: c.IsForkID13Durian(num), } } diff --git a/erigon-lib/chain/zk_constants.go b/erigon-lib/chain/zk_constants.go index 31f189a1145..fad539ffb86 100644 --- a/erigon-lib/chain/zk_constants.go +++ b/erigon-lib/chain/zk_constants.go @@ -13,5 +13,9 @@ const ( ForkID10 ForkID11 ForkID12Banana - ForkId13Durian = 13 + ForkId13Durian + + // ImpossibleForkId is a fork ID that is greater than any possible fork ID + // Nothing should be added after this line + ImpossibleForkId ) diff --git a/zk/stages/stage_batches.go b/zk/stages/stage_batches.go index fd465623021..2c0644e44c2 100644 --- a/zk/stages/stage_batches.go +++ b/zk/stages/stage_batches.go @@ -31,7 +31,6 @@ import ( ) const ( - HIGHEST_KNOWN_FORK = 12 STAGE_PROGRESS_SAVE = 3000000 NEW_BLOCKS_ON_DS_LIMIT = 10000 ) diff --git a/zk/stages/stage_batches_processor.go b/zk/stages/stage_batches_processor.go index 35da1b5658c..404812f9320 100644 --- a/zk/stages/stage_batches_processor.go +++ b/zk/stages/stage_batches_processor.go @@ -199,7 +199,7 @@ func (p *BatchesProcessor) processFullBlock(blockEntry *types.FullL2Block) (rest // channels can be read in random orders which then creates problems in detecting fork changes during // execution if blockEntry.BatchNumber > p.highestSeenBatchNo && p.lastForkId < blockEntry.ForkId { - if blockEntry.ForkId > HIGHEST_KNOWN_FORK { + if blockEntry.ForkId >= uint64(chain.ImpossibleForkId) { message := fmt.Sprintf("unsupported fork id %v received from the data stream", blockEntry.ForkId) panic(message) }