Skip to content

Commit

Permalink
Fix configuration on Cancun
Browse files Browse the repository at this point in the history
  • Loading branch information
marioevz committed Sep 6, 2023
1 parent 48bcadf commit 94093c1
Show file tree
Hide file tree
Showing 15 changed files with 482 additions and 476 deletions.
54 changes: 23 additions & 31 deletions simulators/ethereum/engine/clmock/clmock.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/ethereum/go-ethereum/core/forkid"
"github.com/ethereum/hive/simulators/ethereum/engine/client"
"github.com/ethereum/hive/simulators/ethereum/engine/config"
"github.com/ethereum/hive/simulators/ethereum/engine/config/cancun"
"github.com/ethereum/hive/simulators/ethereum/engine/globals"
"github.com/ethereum/hive/simulators/ethereum/engine/helper"
typ "github.com/ethereum/hive/simulators/ethereum/engine/types"
Expand All @@ -27,15 +28,14 @@ import (
)

var (
DefaultSlotsToSafe = big.NewInt(1)
DefaultSlotsToFinalized = big.NewInt(2)
DefaultSafeSlotsToImportOptimistically = big.NewInt(0)
DefaultSlotsToSafe = big.NewInt(1)
DefaultSlotsToFinalized = big.NewInt(2)
DefaultBlockTimestampIncrement = big.NewInt(1)

// Time delay between ForkchoiceUpdated and GetPayload to allow the clients
// to produce a new Payload
DefaultPayloadProductionClientDelay = time.Second

// Fork specific constants
BLOB_COMMITMENT_VERSION_KZG = byte(0x01)
)

type ExecutableDataHistory map[uint64]*typ.ExecutableData
Expand Down Expand Up @@ -127,36 +127,31 @@ type CLMocker struct {
TimeoutContext context.Context
}

func NewCLMocker(t *hivesim.T, genesis *core.Genesis, slotsToSafe, slotsToFinalized, safeSlotsToImportOptimistically *big.Int, forkConfig *config.ForkConfig) *CLMocker {
func NewCLMocker(t *hivesim.T, genesis *core.Genesis, forkConfig *config.ForkConfig) *CLMocker {
// Init random seed for different purposes
seed := time.Now().Unix()
t.Logf("Randomness seed: %v\n", seed)
rand.Seed(seed)

if slotsToSafe == nil {
// Use default
slotsToSafe = DefaultSlotsToSafe
}
if slotsToFinalized == nil {
// Use default
slotsToFinalized = DefaultSlotsToFinalized
}
// Create the new CL mocker
newCLMocker := &CLMocker{
T: t,
EngineClients: make([]client.EngineClient, 0),
PrevRandaoHistory: map[uint64]common.Hash{},
ExecutedPayloadHistory: ExecutableDataHistory{},
SlotsToSafe: slotsToSafe,
SlotsToFinalized: slotsToFinalized,
SafeSlotsToImportOptimistically: safeSlotsToImportOptimistically,
T: t,
EngineClients: make([]client.EngineClient, 0),
PrevRandaoHistory: map[uint64]common.Hash{},
ExecutedPayloadHistory: ExecutableDataHistory{},

SlotsToSafe: DefaultSlotsToSafe,
SlotsToFinalized: DefaultSlotsToFinalized,
SafeSlotsToImportOptimistically: DefaultSafeSlotsToImportOptimistically,
PayloadProductionClientDelay: DefaultPayloadProductionClientDelay,
PayloadIDHistory: make(map[api.PayloadID]interface{}),
LatestHeader: nil,
FirstPoSBlockNumber: nil,
LatestHeadNumber: nil,
TTDReached: false,
NextFeeRecipient: common.Address{},
BlockTimestampIncrement: DefaultBlockTimestampIncrement,

PayloadIDHistory: make(map[api.PayloadID]interface{}),
LatestHeader: nil,
FirstPoSBlockNumber: nil,
LatestHeadNumber: nil,
TTDReached: false,
NextFeeRecipient: common.Address{},
LatestForkchoice: api.ForkchoiceStateV1{
HeadBlockHash: common.Hash{},
SafeBlockHash: common.Hash{},
Expand Down Expand Up @@ -327,9 +322,6 @@ func (cl *CLMocker) IsBlockPoS(bn *big.Int) bool {

// Return the per-block timestamp value increment
func (cl *CLMocker) GetTimestampIncrement() uint64 {
if cl.BlockTimestampIncrement == nil {
return 1
}
return cl.BlockTimestampIncrement.Uint64()
}

Expand Down Expand Up @@ -513,7 +505,7 @@ func (cl *CLMocker) GetNextPayload() {
cl.Fatalf("CLMocker: No blob bundle on cancun")
}
// Broadcast the blob bundle to all clients
cl.LatestPayloadBuilt.VersionedHashes, err = cl.LatestBlobBundle.VersionedHashes(BLOB_COMMITMENT_VERSION_KZG)
cl.LatestPayloadBuilt.VersionedHashes, err = cl.LatestBlobBundle.VersionedHashes(cancun.BLOB_COMMITMENT_VERSION_KZG)
if err != nil {
cl.Fatalf("CLMocker: Could not get versioned hashes from blob bundle: %v", err)
}
Expand Down
31 changes: 31 additions & 0 deletions simulators/ethereum/engine/config/cancun/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package cancun

import (
"math/big"

"github.com/ethereum/go-ethereum/common"
)

var (
// EIP 4844
GAS_PER_BLOB = uint64(0x20000)

MIN_DATA_GASPRICE = uint64(1)
MAX_BLOB_GAS_PER_BLOCK = uint64(786432)
TARGET_BLOB_GAS_PER_BLOCK = uint64(393216)

TARGET_BLOBS_PER_BLOCK = uint64(TARGET_BLOB_GAS_PER_BLOCK / GAS_PER_BLOB)
MAX_BLOBS_PER_BLOCK = uint64(MAX_BLOB_GAS_PER_BLOCK / GAS_PER_BLOB)

BLOB_GASPRICE_UPDATE_FRACTION = uint64(3338477)

BLOB_COMMITMENT_VERSION_KZG = byte(0x01)

// EIP 4788
HISTORY_STORAGE_ADDRESS = common.HexToAddress("0x000000000000000000000000000000000000000b")
HISTORICAL_ROOTS_MODULUS = uint64(98304)

// Test constants
DATAHASH_START_ADDRESS = big.NewInt(0x20000)
DATAHASH_ADDRESS_COUNT = 1000
)
77 changes: 77 additions & 0 deletions simulators/ethereum/engine/config/cancun/genesis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package cancun

import (
"fmt"
"math/big"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core"
)

// ConfigGenesis configures the genesis block for the Cancun fork.
func ConfigGenesis(genesis *core.Genesis, forkTimestamp uint64) error {
if genesis.Config.ShanghaiTime == nil {
return fmt.Errorf("cancun fork requires shanghai fork")
}
genesis.Config.CancunTime = &forkTimestamp
if *genesis.Config.ShanghaiTime > forkTimestamp {
return fmt.Errorf("cancun fork must be after shanghai fork")
}
if genesis.Timestamp >= forkTimestamp {
if genesis.BlobGasUsed == nil {
genesis.BlobGasUsed = new(uint64)
}
if genesis.ExcessBlobGas == nil {
genesis.ExcessBlobGas = new(uint64)
}
}

// Add bytecode pre deploy to the EIP-4788 address.
genesis.Alloc[HISTORY_STORAGE_ADDRESS] = core.GenesisAccount{
Balance: common.Big0,
Nonce: 1,
Code: common.Hex2Bytes("3373fffffffffffffffffffffffffffffffffffffffe14604457602036146024575f5ffd5b620180005f350680545f35146037575f5ffd5b6201800001545f5260205ff35b42620180004206555f3562018000420662018000015500"),
}

return nil
}

// Configure specific test genesis accounts related to Cancun funtionality.
func ConfigTestAccounts(genesis *core.Genesis) error {
// Add accounts that use the DATAHASH opcode
datahashCode := []byte{
0x5F, // PUSH0
0x80, // DUP1
0x49, // DATAHASH
0x55, // SSTORE
0x60, // PUSH1(0x01)
0x01,
0x80, // DUP1
0x49, // DATAHASH
0x55, // SSTORE
0x60, // PUSH1(0x02)
0x02,
0x80, // DUP1
0x49, // DATAHASH
0x55, // SSTORE
0x60, // PUSH1(0x03)
0x03,
0x80, // DUP1
0x49, // DATAHASH
0x55, // SSTORE
}

for i := 0; i < DATAHASH_ADDRESS_COUNT; i++ {
address := common.BigToAddress(big.NewInt(0).Add(DATAHASH_START_ADDRESS, big.NewInt(int64(i))))
// check first if the address is already in the genesis
if _, ok := genesis.Alloc[address]; ok {
panic(fmt.Errorf("reused address %s during genesis configuration for cancun", address.Hex()))
}
genesis.Alloc[address] = core.GenesisAccount{
Code: datahashCode,
Balance: common.Big0,
}
}

return nil
}
38 changes: 0 additions & 38 deletions simulators/ethereum/engine/config/fork.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package config

import (
"fmt"
"math/big"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core"
)

type Fork string
Expand All @@ -21,40 +17,6 @@ type ForkConfig struct {
CancunTimestamp *big.Int
}

func (f *ForkConfig) ConfigGenesis(genesis *core.Genesis) error {
if f.ShanghaiTimestamp != nil {
shanghaiTime := f.ShanghaiTimestamp.Uint64()
genesis.Config.ShanghaiTime = &shanghaiTime

if genesis.Timestamp >= shanghaiTime {
// Remove PoW altogether
genesis.Difficulty = common.Big0
genesis.Config.TerminalTotalDifficulty = common.Big0
genesis.Config.Clique = nil
genesis.ExtraData = []byte{}
}
}
if f.CancunTimestamp != nil {
if genesis.Config.ShanghaiTime == nil {
return fmt.Errorf("cancun fork requires Shanghai fork")
}
cancunTime := f.CancunTimestamp.Uint64()
genesis.Config.CancunTime = &cancunTime
if *genesis.Config.ShanghaiTime > cancunTime {
return fmt.Errorf("cancun fork must be after Shanghai fork")
}
if genesis.Timestamp >= cancunTime {
if genesis.BlobGasUsed == nil {
genesis.BlobGasUsed = new(uint64)
}
if genesis.ExcessBlobGas == nil {
genesis.ExcessBlobGas = new(uint64)
}
}
}
return nil
}

func (f *ForkConfig) IsShanghai(blockTimestamp uint64) bool {
return f.ShanghaiTimestamp != nil && new(big.Int).SetUint64(blockTimestamp).Cmp(f.ShanghaiTimestamp) >= 0
}
Expand Down
30 changes: 30 additions & 0 deletions simulators/ethereum/engine/config/genesis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package config

import (
"fmt"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/hive/simulators/ethereum/engine/config/cancun"
)

func (f *ForkConfig) ConfigGenesis(genesis *core.Genesis) error {
if f.ShanghaiTimestamp != nil {
shanghaiTime := f.ShanghaiTimestamp.Uint64()
genesis.Config.ShanghaiTime = &shanghaiTime

if genesis.Timestamp >= shanghaiTime {
// Remove PoW altogether
genesis.Difficulty = common.Big0
genesis.Config.TerminalTotalDifficulty = common.Big0
genesis.Config.Clique = nil
genesis.ExtraData = []byte{}
}
}
if f.CancunTimestamp != nil {
if err := cancun.ConfigGenesis(genesis, f.CancunTimestamp.Uint64()); err != nil {
return fmt.Errorf("failed to configure cancun fork: %v", err)
}
}
return nil
}
21 changes: 21 additions & 0 deletions simulators/ethereum/engine/globals/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package globals

var (
// Engine API errors
SERVER_ERROR = pInt(-32000)
INVALID_REQUEST = pInt(-32600)
METHOD_NOT_FOUND = pInt(-32601)
INVALID_PARAMS_ERROR = pInt(-32602)
INTERNAL_ERROR = pInt(-32603)
INVALID_JSON = pInt(-32700)

UNKNOWN_PAYLOAD = pInt(-38001)
INVALID_FORKCHOICE_STATE = pInt(-38002)
INVALID_PAYLOAD_ATTRIBUTES = pInt(-38003)
TOO_LARGE_REQUEST = pInt(-38004)
UNSUPPORTED_FORK_ERROR = pInt(-38005)
)

func pInt(v int) *int {
return &v
}
2 changes: 1 addition & 1 deletion simulators/ethereum/engine/globals/globals.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ var (
GasPrice = big.NewInt(30 * params.GWei)
GasTipPrice = big.NewInt(1 * params.GWei)
NetworkID = big.NewInt(7)
GenesisTimestamp = int64(0x1234)
GenesisTimestamp = uint64(0x1234)

// RPC Timeout for every call
RPCTimeout = 10 * time.Second
Expand Down
Loading

0 comments on commit 94093c1

Please sign in to comment.