Skip to content

Commit

Permalink
removing some magic numbers for block gas and some misleading comments
Browse files Browse the repository at this point in the history
  • Loading branch information
hexoscott committed Jun 25, 2024
1 parent 238943b commit d111d79
Show file tree
Hide file tree
Showing 13 changed files with 28 additions and 33 deletions.
3 changes: 2 additions & 1 deletion cl/clparams/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/ledgerwatch/erigon/cl/cltypes/ssz"
"github.com/ledgerwatch/erigon/cl/utils"
"github.com/ledgerwatch/erigon/params/networkname"
zkUtils "github.com/ledgerwatch/erigon/zk/utils"
)

type NetworkType int
Expand Down Expand Up @@ -718,7 +719,7 @@ var MainnetBeaconConfig BeaconChainConfig = BeaconChainConfig{
MinSyncCommitteeParticipants: 1,

// Bellatrix
TerminalBlockHashActivationEpoch: 18446744073709551615,
TerminalBlockHashActivationEpoch: zkUtils.ForkId7BlockGasLimit,
TerminalBlockHash: [32]byte{},
TerminalTotalDifficulty: "58750000000000000000000", // Estimated: Sept 15, 2022
EthBurnAddressHex: "0x0000000000000000000000000000000000000000",
Expand Down
5 changes: 2 additions & 3 deletions core/blockchain_zkevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/erigon/core/vm"
"github.com/ledgerwatch/erigon/core/vm/evmtypes"
"github.com/ledgerwatch/erigon/zk/utils"
)

type EphemeralExecResultZk struct {
Expand Down Expand Up @@ -66,9 +67,8 @@ func ExecuteBlockEphemerallyZk(
blockTransactions := block.Transactions()
blockGasLimit := block.GasLimit()

//[hack] - on forkid7 this gas limit was used for execution but rpc is now returning forkid8 gas limit
if !chainConfig.IsForkID8Elderberry(block.NumberU64()) {
blockGasLimit = 18446744073709551615
blockGasLimit = utils.ForkId7BlockGasLimit
}

gp := new(GasPool).AddGas(blockGasLimit)
Expand Down Expand Up @@ -184,7 +184,6 @@ func ExecuteBlockEphemerallyZk(
EffectiveGasPrice: effectiveGasPricePercentage,
Signer: &txSender,
})

}

var l2InfoRoot *common.Hash
Expand Down
5 changes: 3 additions & 2 deletions core/types/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"github.com/ledgerwatch/erigon/crypto"
"github.com/ledgerwatch/erigon/params"
"github.com/ledgerwatch/erigon/rlp"
"github.com/ledgerwatch/erigon/zk/utils"
)

// from bcValidBlockTest.json, "SimpleTx"
Expand Down Expand Up @@ -371,7 +372,7 @@ func TestAuRaHeaderEncoding(t *testing.T) {
ReceiptHash: libcommon.HexToHash("0x324f54860e214ea896ea7a05bda30f85541be3157de77a9059a04fdb1e86badd"),
Difficulty: difficulty,
Number: big.NewInt(24679923),
GasLimit: 30_000_000,
GasLimit: utils.PreForkId7BlockGasLimit,
GasUsed: 3_074_345,
Time: 1666343339,
Extra: common.FromHex("0x1234"),
Expand All @@ -396,7 +397,7 @@ func TestWithdrawalsEncoding(t *testing.T) {
Root: libcommon.HexToHash("0x351780124dae86b84998c6d4fe9a88acfb41b4856b4f2c56767b51a4e2f94dd4"),
Difficulty: libcommon.Big0,
Number: big.NewInt(20_000_000),
GasLimit: 30_000_000,
GasLimit: utils.PreForkId7BlockGasLimit,
GasUsed: 3_074_345,
Time: 1666343339,
Extra: make([]byte, 0),
Expand Down
4 changes: 0 additions & 4 deletions smt/pkg/blockinfo/block_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ import (
"github.com/ledgerwatch/log/v3"
)

const (
BlockGasLimit = 18446744073709551615
)

type ExecutedTxInfo struct {
Tx ethTypes.Transaction
EffectiveGasPrice uint8
Expand Down
5 changes: 3 additions & 2 deletions smt/pkg/blockinfo/block_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/ledgerwatch/erigon/core/types"
ethTypes "github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/erigon/smt/pkg/smt"
"github.com/ledgerwatch/erigon/zk/utils"
)

/*
Expand Down Expand Up @@ -55,7 +56,7 @@ func TestBlockInfoHeader(t *testing.T) {
BlockHash: "0x045fa48a1342813a61c1dd2d235620d621b59cdda0bd07ff3536c6cf64f5e688",
CoinbaseAddress: "0x9aeCf44E36f20DC407d1A580630c9a2419912dcB",
NewBlockNumber: 592221,
BlockGasLimit: 1125899906842624,
BlockGasLimit: utils.ForkId8BlockGasLimit,
FinalTimestamp: 1708198045,
FinalGER: "0x0000000000000000000000000000000000000000000000000000000000000000",
L1BlochHash: "0x0000000000000000000000000000000000000000000000000000000000000000",
Expand All @@ -64,7 +65,7 @@ func TestBlockInfoHeader(t *testing.T) {
BlockHash: "0x268a22af2bae40acd1cc4228896de4420c5f3bc3bbdd8515d6d01b1b99731f82",
CoinbaseAddress: "0x9aeCf44E36f20DC407d1A580630c9a2419912dcB",
NewBlockNumber: 592223,
BlockGasLimit: 18446744073709551615,
BlockGasLimit: utils.ForkId7BlockGasLimit,
FinalTimestamp: 1708198051,
FinalGER: "0x0000000000000000000000000000000000000000000000000000000000000000",
L1BlochHash: "0x0000000000000000000000000000000000000000000000000000000000000000",
Expand Down
5 changes: 3 additions & 2 deletions smt/pkg/utils/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strconv"
"strings"
"testing"
"github.com/ledgerwatch/erigon/zk/utils"
)

func TestBinaryStringToInt64(t *testing.T) {
Expand All @@ -18,8 +19,8 @@ func TestBinaryStringToInt64(t *testing.T) {
{"110011", 51},
{"0", 0},
{"11111111", 255},
{"1111111111111111111111111111111111111111111111111111111111111111", 18446744073709551615}, // max uint64 value
{"10000000000000000000000000000000000000000000000000000000000000000", 0}, // overflow scenario
{"1111111111111111111111111111111111111111111111111111111111111111", utils.ForkId7BlockGasLimit}, // max uint64 value
{"10000000000000000000000000000000000000000000000000000000000000000", 0}, // overflow scenario
}

for _, tc := range testCases {
Expand Down
2 changes: 1 addition & 1 deletion zk/stages/stage_batches.go
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,7 @@ func writeL2Block(eriDb ErigonDb, hermezDb HermezDb, l2Block *types.FullL2Block,
txCollection := ethTypes.Transactions(txs)
txHash := ethTypes.DeriveSha(txCollection)

gasLimit := utils.GetExecutionGasLimit(l2Block.ForkId)
gasLimit := utils.GetBlockGasLimitForFork(l2Block.ForkId)

h, err := eriDb.WriteHeader(bn, l2Block.StateRoot, txHash, l2Block.ParentHash, l2Block.Coinbase, uint64(l2Block.Timestamp), gasLimit)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion zk/stages/stage_sequence_execute_blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func finaliseBlock(
finalHeader := finalBlock.HeaderNoCopy()
finalHeader.Root = newRoot
finalHeader.Coinbase = cfg.zk.AddressSequencer
finalHeader.GasLimit = utils.GetExecutionGasLimit(forkId)
finalHeader.GasLimit = utils.GetBlockGasLimitForFork(forkId)
finalHeader.ReceiptHash = types.DeriveSha(receipts)
finalHeader.Bloom = types.CreateBloom(receipts)
newNum := finalBlock.Number()
Expand Down
2 changes: 1 addition & 1 deletion zk/stages/stage_sequence_execute_transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ LOOP:
}
if err := cfg.txPoolDb.View(context.Background(), func(poolTx kv.Tx) error {
slots := types2.TxsRlp{}
_, count, err = cfg.txPool.YieldBest(yieldSize, &slots, poolTx, executionAt, utils.GetExecutionGasLimit(forkId), alreadyYielded)
_, count, err = cfg.txPool.YieldBest(yieldSize, &slots, poolTx, executionAt, utils.GetBlockGasLimitForFork(forkId), alreadyYielded)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion zk/stages/stage_sequence_execute_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ func prepareHeader(tx kv.RwTx, previousBlockNumber, deltaTimestamp, forcedTimest
Coinbase: coinbase,
Difficulty: blockDifficulty,
Number: new(big.Int).SetUint64(previousBlockNumber + 1),
GasLimit: utils.GetExecutionGasLimit(forkId),
GasLimit: utils.GetBlockGasLimitForFork(forkId),
Time: newBlockTimestamp,
}, parentBlock, nil
}
Expand Down
3 changes: 2 additions & 1 deletion zk/txpool/pool_zk.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/holiman/uint256"
"github.com/ledgerwatch/erigon/common/math"
"github.com/ledgerwatch/log/v3"
"github.com/ledgerwatch/erigon/zk/utils"
)

/*
Expand All @@ -22,7 +23,7 @@ hard compilation fail when rebasing from upstream further down the line.
*/

const (
transactionGasLimit = 30_000_000
transactionGasLimit = utils.PreForkId7BlockGasLimit
)

func calcProtocolBaseFee(baseFee uint64) uint64 {
Expand Down
21 changes: 8 additions & 13 deletions zk/utils/gas_limit.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
package utils

const (
preForkId7BlockGasLimit = 30_000_000
forkId7BlockGasLimit = 18446744073709551615 // 0xffffffffffffffff
forkId8BlockGasLimit = 1125899906842624 // 0x4000000000000
PreForkId7BlockGasLimit = 30_000_000
ForkId7BlockGasLimit = 18446744073709551615 // 0xffffffffffffffff
ForkId8BlockGasLimit = 1125899906842624 // 0x4000000000000
)

func GetExecutionGasLimit(forkId uint64) uint64 {
if forkId >= 8 {
return forkId8BlockGasLimit
func GetBlockGasLimitForFork(forkId uint64) uint64 {
if forkId >= 7 {
// the gas limit for fork 8 was actually used for fork 7 and above after a re-hash
return ForkId8BlockGasLimit
}

// [hack] the rpc returns forkid8 value, but forkid7 is used in execution
if forkId == 7 {
return forkId8BlockGasLimit
// return forkId7BlockGasLimit
}

return preForkId7BlockGasLimit
return PreForkId7BlockGasLimit
}
2 changes: 1 addition & 1 deletion zk/witness/witness.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (g *Generator) GetWitnessByBatch(tx kv.Tx, ctx context.Context, batchNum ui
Coinbase: decoded.Coinbase,
Difficulty: new(big.Int).SetUint64(0),
Number: new(big.Int).SetUint64(nextNum),
GasLimit: zkUtils.GetExecutionGasLimit(fork),
GasLimit: zkUtils.GetBlockGasLimitForFork(fork),
Time: timestamp,
}

Expand Down

0 comments on commit d111d79

Please sign in to comment.