Skip to content

Commit

Permalink
Fix zk counter for all HFs (#1357)
Browse files Browse the repository at this point in the history
  • Loading branch information
cffls committed Oct 24, 2024
1 parent d930066 commit f12783d
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 8 deletions.
16 changes: 8 additions & 8 deletions core/vm/zk_counters_limits.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ const stepDeduction = 200
const baseSafetyPercentage float64 = 0.05

var (
defaultTotalSteps = 1 << 23
forkId10TotalSteps = 1 << 24
forkId11TotalSteps = 1 << 25
preForkId10TotalSteps = 1 << 23
forkId10TotalSteps = 1 << 24
forkId11TotalSteps = 1 << 25

unlimitedCounters = counterLimits{
totalSteps: math.MaxInt32,
Expand Down Expand Up @@ -102,13 +102,13 @@ func getCounterLimits(forkId uint16) *Counters {
func getTotalSteps(forkId uint16) int {
var totalSteps int

switch forkId {
case uint16(zk_consts.ForkID10):
if forkId < uint16(zk_consts.ForkID10) {
return preForkId10TotalSteps
} else if forkId == uint16(zk_consts.ForkID10) {
totalSteps = forkId10TotalSteps
case uint16(zk_consts.ForkID11):
} else if forkId >= uint16(zk_consts.ForkID11) {
// Use the new total steps for ForkID11 and above, unless a new limit is added in the future
totalSteps = forkId11TotalSteps
default:
totalSteps = defaultTotalSteps
}

// we need to remove some steps as these will always be used during batch execution
Expand Down
67 changes: 67 additions & 0 deletions core/vm/zk_counters_limits_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package vm

import (
"testing"

zk_consts "github.com/ledgerwatch/erigon-lib/chain"

Check failure on line 6 in core/vm/zk_counters_limits_test.go

View workflow job for this annotation

GitHub Actions / tests (ubuntu-20.04)

no required module provides package github.com/ledgerwatch/erigon-lib/chain; to add it:

Check failure on line 6 in core/vm/zk_counters_limits_test.go

View workflow job for this annotation

GitHub Actions / tests (macos-13-xlarge)

no required module provides package github.com/ledgerwatch/erigon-lib/chain; to add it:
)

func TestGetTotalSteps(t *testing.T) {
tests := []struct {
name string
forkID uint16
expected int
}{
{
name: "Pre ForkID10",
forkID: uint16(zk_consts.ForkID9Elderberry2),
expected: preForkId10TotalSteps, // 1 << 23
},
{
name: "ForkID10",
forkID: uint16(zk_consts.ForkID10),
expected: forkId10TotalSteps - stepDeduction,
},
{
name: "ForkID11",
forkID: uint16(zk_consts.ForkID11),
expected: forkId11TotalSteps - stepDeduction,
},
{
name: "ForkID12",
forkID: uint16(zk_consts.ForkID12Banana),
expected: forkId11TotalSteps - stepDeduction,
},
{
name: "ForkID13",
forkID: uint16(zk_consts.ForkId13Durian),
expected: forkId11TotalSteps - stepDeduction,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := getTotalSteps(tt.forkID)
if got != tt.expected {
t.Errorf("getTotalSteps(%d) = %d, want %d", tt.forkID, got, tt.expected)
}
})
}
}

func TestCounterLimitsProgression(t *testing.T) {
// Test that forkid progression is correct from for 9 to 100
startForkID := uint16(zk_consts.ForkID9Elderberry2)
endForkID := uint16(100)

var prevLimit int
for forkID := startForkID; forkID <= endForkID; forkID++ {
currentLimit := getTotalSteps(forkID)

if prevLimit > currentLimit {
t.Errorf("ForkID %d has fewer steps (%d) than previous forkID %d (%d)!", forkID, currentLimit, forkID-1, prevLimit)
}

prevLimit = currentLimit
}
}

0 comments on commit f12783d

Please sign in to comment.