From 2419cfe7dee84abf70eb0cb2a9a24acd6bfce18e Mon Sep 17 00:00:00 2001 From: Darioush Jalali Date: Tue, 28 Jan 2025 12:18:10 -0800 Subject: [PATCH] simulate staker preference --- core/block_validator_test.go | 38 ++++++++++++++++++++++++++++++++++++ params/protocol_params.go | 11 +++++++---- 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/core/block_validator_test.go b/core/block_validator_test.go index 8af4057693..1793f92208 100644 --- a/core/block_validator_test.go +++ b/core/block_validator_test.go @@ -18,6 +18,7 @@ package core import ( "math/big" + "math/rand" "testing" "time" @@ -265,3 +266,40 @@ func TestCalcGasLimit(t *testing.T) { } } } + +func TestSimulateGasLimit(t *testing.T) { + minLimit, maxLimit := params.MinGasLimit, params.MaxGasLimit // can change these + params.MinGasLimit, params.MaxGasLimit = minLimit, maxLimit + rand.Seed(time.Now().UnixNano()) + + type weightedPreference struct { + weight float32 + limit uint64 + } + stakerPreference := []weightedPreference{ + {0.1, 100_000}, + {0.1, 200_000}, + {0.1, 300_000}, + {0.1, 400_000}, + {0.1, 500_000}, + {0.1, 600_000}, + {0.1, 700_000}, + {0.1, 800_000}, + {0.1, 900_000}, + {0.1, 1_000_000}, + } + + start := params.MinGasLimit + currentGasLimit := start + for i := 0; i < 100_000; i++ { + r := rand.Float32() + for _, p := range stakerPreference { + r -= p.weight + if r <= 0 { + currentGasLimit = CalcGasLimit(currentGasLimit, p.limit) + break + } + } + t.Logf("Gas limit: %d", currentGasLimit) + } +} diff --git a/params/protocol_params.go b/params/protocol_params.go index 030083aa9a..a8235a5cb6 100644 --- a/params/protocol_params.go +++ b/params/protocol_params.go @@ -22,11 +22,14 @@ import ( "github.com/ethereum/go-ethereum/common" ) +var ( + MinGasLimit uint64 = 5000 // Minimum the gas limit may ever be. + MaxGasLimit uint64 = 0x7fffffffffffffff // Maximum the gas limit (2^63-1). +) + const ( - GasLimitBoundDivisor uint64 = 1024 // The bound divisor of the gas limit, used in update calculations. - MinGasLimit uint64 = 5000 // Minimum the gas limit may ever be. - MaxGasLimit uint64 = 0x7fffffffffffffff // Maximum the gas limit (2^63-1). - GenesisGasLimit uint64 = 4712388 // Gas limit of the Genesis block. + GasLimitBoundDivisor uint64 = 1024 // The bound divisor of the gas limit, used in update calculations. + GenesisGasLimit uint64 = 4712388 // Gas limit of the Genesis block. MaximumExtraDataSize uint64 = 32 // Maximum size extra data may be after Genesis. ExpByteGas uint64 = 10 // Times ceil(log256(exponent)) for the EXP instruction.