diff --git a/cmd/evm/internal/t8ntool/execution.go b/cmd/evm/internal/t8ntool/execution.go index dfdde4217396..28af534ec0bb 100644 --- a/cmd/evm/internal/t8ntool/execution.go +++ b/cmd/evm/internal/t8ntool/execution.go @@ -67,6 +67,7 @@ type ommer struct { type stEnv struct { Coinbase common.Address `json:"currentCoinbase" gencodec:"required"` Difficulty *big.Int `json:"currentDifficulty"` + Random *big.Int `json:"currentRandom"` ParentDifficulty *big.Int `json:"parentDifficulty"` GasLimit uint64 `json:"currentGasLimit" gencodec:"required"` Number uint64 `json:"currentNumber" gencodec:"required"` @@ -81,6 +82,7 @@ type stEnv struct { type stEnvMarshaling struct { Coinbase common.UnprefixedAddress Difficulty *math.HexOrDecimal256 + Random *math.HexOrDecimal256 ParentDifficulty *math.HexOrDecimal256 GasLimit math.HexOrDecimal64 Number math.HexOrDecimal64 diff --git a/cmd/evm/internal/t8ntool/gen_stenv.go b/cmd/evm/internal/t8ntool/gen_stenv.go index 1bb3c6a46b0c..a6d774cdabcf 100644 --- a/cmd/evm/internal/t8ntool/gen_stenv.go +++ b/cmd/evm/internal/t8ntool/gen_stenv.go @@ -18,6 +18,7 @@ func (s stEnv) MarshalJSON() ([]byte, error) { type stEnv struct { Coinbase common.UnprefixedAddress `json:"currentCoinbase" gencodec:"required"` Difficulty *math.HexOrDecimal256 `json:"currentDifficulty"` + Random *math.HexOrDecimal256 `json:"currentRandom"` ParentDifficulty *math.HexOrDecimal256 `json:"parentDifficulty"` GasLimit math.HexOrDecimal64 `json:"currentGasLimit" gencodec:"required"` Number math.HexOrDecimal64 `json:"currentNumber" gencodec:"required"` @@ -31,6 +32,7 @@ func (s stEnv) MarshalJSON() ([]byte, error) { var enc stEnv enc.Coinbase = common.UnprefixedAddress(s.Coinbase) enc.Difficulty = (*math.HexOrDecimal256)(s.Difficulty) + enc.Random = (*math.HexOrDecimal256)(s.Random) enc.ParentDifficulty = (*math.HexOrDecimal256)(s.ParentDifficulty) enc.GasLimit = math.HexOrDecimal64(s.GasLimit) enc.Number = math.HexOrDecimal64(s.Number) @@ -48,6 +50,7 @@ func (s *stEnv) UnmarshalJSON(input []byte) error { type stEnv struct { Coinbase *common.UnprefixedAddress `json:"currentCoinbase" gencodec:"required"` Difficulty *math.HexOrDecimal256 `json:"currentDifficulty"` + Random *math.HexOrDecimal256 `json:"currentRandom"` ParentDifficulty *math.HexOrDecimal256 `json:"parentDifficulty"` GasLimit *math.HexOrDecimal64 `json:"currentGasLimit" gencodec:"required"` Number *math.HexOrDecimal64 `json:"currentNumber" gencodec:"required"` @@ -69,6 +72,9 @@ func (s *stEnv) UnmarshalJSON(input []byte) error { if dec.Difficulty != nil { s.Difficulty = (*big.Int)(dec.Difficulty) } + if dec.Random != nil { + s.Random = (*big.Int)(dec.Random) + } if dec.ParentDifficulty != nil { s.ParentDifficulty = (*big.Int)(dec.ParentDifficulty) } diff --git a/cmd/geth/config.go b/cmd/geth/config.go index 18829bade932..7a642edd0e41 100644 --- a/cmd/geth/config.go +++ b/cmd/geth/config.go @@ -159,7 +159,7 @@ func makeFullNode(ctx *cli.Context) (*node.Node, ethapi.Backend) { cfg.Eth.OverrideArrowGlacier = new(big.Int).SetUint64(ctx.GlobalUint64(utils.OverrideArrowGlacierFlag.Name)) } if ctx.GlobalIsSet(utils.OverrideTerminalTotalDifficulty.Name) { - cfg.Eth.Genesis.Config.TerminalTotalDifficulty = new(big.Int).SetUint64(ctx.GlobalUint64(utils.OverrideTerminalTotalDifficulty.Name)) + cfg.Eth.OverrideTerminalTotalDifficulty = new(big.Int).SetUint64(ctx.GlobalUint64(utils.OverrideTerminalTotalDifficulty.Name)) } backend, _ := utils.RegisterEthService(stack, &cfg.Eth, ctx.GlobalBool(utils.CatalystFlag.Name)) diff --git a/consensus/beacon/consensus.go b/consensus/beacon/consensus.go index d90406438558..507a5fe6b3eb 100644 --- a/consensus/beacon/consensus.go +++ b/consensus/beacon/consensus.go @@ -181,10 +181,7 @@ func (beacon *Beacon) verifyHeader(chain consensus.ChainHeaderReader, header, pa if len(header.Extra) > 32 { return fmt.Errorf("extra-data longer than 32 bytes (%d)", len(header.Extra)) } - // Verify the seal parts. Ensure the mixhash, nonce and uncle hash are the expected value. - if header.MixDigest != (common.Hash{}) { - return errInvalidMixDigest - } + // Verify the seal parts. Ensure the nonce and uncle hash are the expected value. if header.Nonce != beaconNonce { return errInvalidNonce } diff --git a/core/blockchain_reader.go b/core/blockchain_reader.go index beaa57b0c11b..d43087e5778d 100644 --- a/core/blockchain_reader.go +++ b/core/blockchain_reader.go @@ -343,6 +343,12 @@ func (bc *BlockChain) GetVMConfig() *vm.Config { return &bc.vmConfig } +// SetVMConfig sets the vm config. +// Warning: might not be threadsafe with other components +func (bc *BlockChain) SetVMConfig(config vm.Config) { + bc.vmConfig = config +} + // SetTxLookupLimit is responsible for updating the txlookup limit to the // original one stored in db if the new mismatches with the old one. func (bc *BlockChain) SetTxLookupLimit(limit uint64) { diff --git a/core/evm.go b/core/evm.go index 6c67fc43762c..edbe7b7a4b48 100644 --- a/core/evm.go +++ b/core/evm.go @@ -61,6 +61,7 @@ func NewEVMBlockContext(header *types.Header, chain ChainContext, author *common Difficulty: new(big.Int).Set(header.Difficulty), BaseFee: baseFee, GasLimit: header.GasLimit, + Random: header.MixDigest, } } diff --git a/core/genesis.go b/core/genesis.go index 557440d08aa1..fda36b48991f 100644 --- a/core/genesis.go +++ b/core/genesis.go @@ -294,7 +294,8 @@ func (g *Genesis) ToBlock(db ethdb.Database) *types.Block { if g.GasLimit == 0 { head.GasLimit = params.GenesisGasLimit } - if g.Difficulty == nil { + emptyHash := common.Hash{} + if g.Difficulty == nil && bytes.Equal(g.Mixhash[:], emptyHash[:]) { head.Difficulty = params.GenesisDifficulty } if g.Config != nil && g.Config.IsLondon(common.Big0) { diff --git a/core/state_transition.go b/core/state_transition.go index 135a9c6dbe85..701919d774fb 100644 --- a/core/state_transition.go +++ b/core/state_transition.go @@ -310,7 +310,7 @@ func (st *StateTransition) TransitionDb() (*ExecutionResult, error) { } // Set up the initial access list. - if rules := st.evm.ChainConfig().Rules(st.evm.Context.BlockNumber); rules.IsBerlin { + if rules := st.evm.ChainConfig().Rules(st.evm.Context.BlockNumber, st.evm.Config.RandomOpcode); rules.IsBerlin { st.state.PrepareAccessList(msg.From(), msg.To(), vm.ActivePrecompiles(rules), msg.AccessList()) } var ( diff --git a/core/vm/evm.go b/core/vm/evm.go index 618bbcf176cb..1e8d89f60ed2 100644 --- a/core/vm/evm.go +++ b/core/vm/evm.go @@ -75,6 +75,7 @@ type BlockContext struct { Time *big.Int // Provides information for TIME Difficulty *big.Int // Provides information for DIFFICULTY BaseFee *big.Int // Provides information for BASEFEE + Random common.Hash // Provides information for RANDOM } // TxContext provides the EVM with information about a transaction. @@ -131,7 +132,7 @@ func NewEVM(blockCtx BlockContext, txCtx TxContext, statedb StateDB, chainConfig StateDB: statedb, Config: config, chainConfig: chainConfig, - chainRules: chainConfig.Rules(blockCtx.BlockNumber), + chainRules: chainConfig.Rules(blockCtx.BlockNumber, config.RandomOpcode), } evm.interpreter = NewEVMInterpreter(evm, config) return evm diff --git a/core/vm/instructions.go b/core/vm/instructions.go index bda480f083d4..3601d0a24292 100644 --- a/core/vm/instructions.go +++ b/core/vm/instructions.go @@ -475,6 +475,12 @@ func opDifficulty(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) return nil, nil } +func opRandom(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { + v := new(uint256.Int).SetBytes((interpreter.evm.Context.Random.Bytes())) + scope.Stack.push(v) + return nil, nil +} + func opGasLimit(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { scope.Stack.push(new(uint256.Int).SetUint64(interpreter.evm.Context.GasLimit)) return nil, nil diff --git a/core/vm/instructions_test.go b/core/vm/instructions_test.go index 560d26a0b8d4..2033a9ad1b7c 100644 --- a/core/vm/instructions_test.go +++ b/core/vm/instructions_test.go @@ -21,6 +21,7 @@ import ( "encoding/json" "fmt" "io/ioutil" + "math/big" "testing" "github.com/ethereum/go-ethereum/common" @@ -650,3 +651,36 @@ func TestCreate2Addreses(t *testing.T) { } } } + +func TestRandom(t *testing.T) { + type testcase struct { + name string + random common.Hash + } + + for _, tt := range []testcase{ + {name: "empty hash", random: common.Hash{}}, + {name: "1", random: common.Hash{0}}, + {name: "emptyCodeHash", random: emptyCodeHash}, + {name: "hash(0x010203)", random: crypto.Keccak256Hash([]byte{0x01, 0x02, 0x03})}, + } { + var ( + env = NewEVM(BlockContext{Random: tt.random}, TxContext{}, nil, params.TestChainConfig, Config{RandomOpcode: true}) + stack = newstack() + pc = uint64(0) + evmInterpreter = env.interpreter + ) + opRandom(&pc, evmInterpreter, &ScopeContext{nil, stack, nil}) + if len(stack.data) != 1 { + t.Errorf("Expected one item on stack after %v, got %d: ", tt.name, len(stack.data)) + } + actual := stack.pop() + expected, overflow := uint256.FromBig(new(big.Int).SetBytes(tt.random.Bytes())) + if overflow { + t.Errorf("Testcase %v: invalid overflow", tt.name) + } + if actual.Cmp(expected) != 0 { + t.Errorf("Testcase %v: expected %x, got %x", tt.name, expected, actual) + } + } +} diff --git a/core/vm/interpreter.go b/core/vm/interpreter.go index afa3c0f12118..51fb2ff7abfb 100644 --- a/core/vm/interpreter.go +++ b/core/vm/interpreter.go @@ -31,6 +31,7 @@ type Config struct { Tracer EVMLogger // Opcode logger NoRecursion bool // Disables call, callcode, delegate call and create NoBaseFee bool // Forces the EIP-1559 baseFee to 0 (needed for 0 price calls) + RandomOpcode bool // Enables the random opcode EnablePreimageRecording bool // Enables recording of SHA3/keccak preimages JumpTable JumpTable // EVM instruction table, automatically populated if unset @@ -74,6 +75,8 @@ func NewEVMInterpreter(evm *EVM, cfg Config) *EVMInterpreter { if cfg.JumpTable[STOP] == nil { var jt JumpTable switch { + case evm.chainRules.IsMerge: + jt = newMergeInstructionSet() case evm.chainRules.IsLondon: jt = londonInstructionSet case evm.chainRules.IsBerlin: diff --git a/core/vm/jump_table.go b/core/vm/jump_table.go index 329ad77cbf83..282e4541b7c4 100644 --- a/core/vm/jump_table.go +++ b/core/vm/jump_table.go @@ -58,11 +58,23 @@ var ( istanbulInstructionSet = newIstanbulInstructionSet() berlinInstructionSet = newBerlinInstructionSet() londonInstructionSet = newLondonInstructionSet() + mergeInstructionSet = newMergeInstructionSet() ) // JumpTable contains the EVM opcodes supported at a given fork. type JumpTable [256]*operation +func newMergeInstructionSet() JumpTable { + instructionSet := newLondonInstructionSet() + instructionSet[RANDOM] = &operation{ + execute: opRandom, + constantGas: GasQuickStep, + minStack: minStack(0, 1), + maxStack: maxStack(0, 1), + } + return instructionSet +} + // newLondonInstructionSet returns the frontier, homestead, byzantium, // contantinople, istanbul, petersburg, berlin and london instructions. func newLondonInstructionSet() JumpTable { diff --git a/core/vm/opcodes.go b/core/vm/opcodes.go index 6659ec401ff1..993157e07e8e 100644 --- a/core/vm/opcodes.go +++ b/core/vm/opcodes.go @@ -104,6 +104,7 @@ const ( CHAINID OpCode = 0x46 SELFBALANCE OpCode = 0x47 BASEFEE OpCode = 0x48 + RANDOM OpCode = 0x44 // Same as DIFFICULTY ) // 0x50 range - 'storage' and execution. @@ -279,7 +280,7 @@ var opCodeToString = map[OpCode]string{ COINBASE: "COINBASE", TIMESTAMP: "TIMESTAMP", NUMBER: "NUMBER", - DIFFICULTY: "DIFFICULTY", + DIFFICULTY: "DIFFICULTY", // TODO (MariusVanDerWijden) rename to RANDOM post merge GASLIMIT: "GASLIMIT", CHAINID: "CHAINID", SELFBALANCE: "SELFBALANCE", diff --git a/core/vm/runtime/runtime.go b/core/vm/runtime/runtime.go index 103ce3e175ff..c197d0db1000 100644 --- a/core/vm/runtime/runtime.go +++ b/core/vm/runtime/runtime.go @@ -118,7 +118,7 @@ func Execute(code, input []byte, cfg *Config) ([]byte, *state.StateDB, error) { vmenv = NewEnv(cfg) sender = vm.AccountRef(cfg.Origin) ) - if rules := cfg.ChainConfig.Rules(vmenv.Context.BlockNumber); rules.IsBerlin { + if rules := cfg.ChainConfig.Rules(vmenv.Context.BlockNumber, false); rules.IsBerlin { cfg.State.PrepareAccessList(cfg.Origin, &address, vm.ActivePrecompiles(rules), nil) } cfg.State.CreateAccount(address) @@ -150,7 +150,7 @@ func Create(input []byte, cfg *Config) ([]byte, common.Address, uint64, error) { vmenv = NewEnv(cfg) sender = vm.AccountRef(cfg.Origin) ) - if rules := cfg.ChainConfig.Rules(vmenv.Context.BlockNumber); rules.IsBerlin { + if rules := cfg.ChainConfig.Rules(vmenv.Context.BlockNumber, false); rules.IsBerlin { cfg.State.PrepareAccessList(cfg.Origin, nil, vm.ActivePrecompiles(rules), nil) } // Call the code with the given configuration. @@ -176,7 +176,7 @@ func Call(address common.Address, input []byte, cfg *Config) ([]byte, uint64, er sender := cfg.State.GetOrNewStateObject(cfg.Origin) statedb := cfg.State - if rules := cfg.ChainConfig.Rules(vmenv.Context.BlockNumber); rules.IsBerlin { + if rules := cfg.ChainConfig.Rules(vmenv.Context.BlockNumber, false); rules.IsBerlin { statedb.PrepareAccessList(cfg.Origin, &address, vm.ActivePrecompiles(rules), nil) } // Call the code with the given configuration. diff --git a/eth/catalyst/api.go b/eth/catalyst/api.go index 059df3670286..4b5671f089e2 100644 --- a/eth/catalyst/api.go +++ b/eth/catalyst/api.go @@ -18,7 +18,8 @@ package catalyst import ( - "bytes" + "crypto/sha256" + "encoding/binary" "errors" "fmt" "math/big" @@ -42,11 +43,13 @@ import ( ) var ( - VALID = GenericStringResponse{"VALID"} - INVALID = GenericStringResponse{"INVALID"} - SYNCING = GenericStringResponse{"SYNCING"} - UnknownHeader = rpc.CustomError{Code: -32000, Message: "unknown header"} - UnknownPayload = rpc.CustomError{Code: -32001, Message: "unknown payload"} + VALID = GenericStringResponse{"VALID"} + SUCCESS = GenericStringResponse{"SUCCESS"} + INVALID = ForkChoiceResponse{Status: "INVALID", PayloadID: nil} + SYNCING = ForkChoiceResponse{Status: "INVALID", PayloadID: nil} + UnknownHeader = rpc.CustomError{Code: -32000, Message: "unknown header"} + UnknownPayload = rpc.CustomError{Code: -32001, Message: "unknown payload"} + InvalidPayloadID = rpc.CustomError{Code: 1, Message: "invalid payload id"} ) // Register adds catalyst APIs to the full node. @@ -82,7 +85,7 @@ type ConsensusAPI struct { eth *eth.Ethereum les *les.LightEthereum engine consensus.Engine // engine is the post-merge consensus engine, only for block creation - preparedBlocks map[int]*ExecutableData + preparedBlocks map[uint64]*ExecutableDataV1 } func NewConsensusAPI(eth *eth.Ethereum, les *les.LightEthereum) *ConsensusAPI { @@ -111,7 +114,7 @@ func NewConsensusAPI(eth *eth.Ethereum, les *les.LightEthereum) *ConsensusAPI { eth: eth, les: les, engine: engine, - preparedBlocks: make(map[int]*ExecutableData), + preparedBlocks: make(map[uint64]*ExecutableDataV1), } } @@ -130,6 +133,7 @@ type blockExecutionEnv struct { func (env *blockExecutionEnv) commitTransaction(tx *types.Transaction, coinbase common.Address) error { vmconfig := *env.chain.GetVMConfig() + vmconfig.RandomOpcode = true snap := env.state.Snapshot() receipt, err := core.ApplyTransaction(env.chain.Config(), env.chain, &coinbase, env.gasPool, env.state, env.header, tx, &env.header.GasUsed, vmconfig) if err != nil { @@ -171,64 +175,92 @@ func (api *ConsensusAPI) makeEnv(parent *types.Block, header *types.Header) (*bl return env, nil } -func (api *ConsensusAPI) PreparePayload(params AssembleBlockParams) (*PayloadResponse, error) { - data, err := api.assembleBlock(params) - if err != nil { - return nil, err +func (api *ConsensusAPI) GetPayloadV1(payloadID hexutil.Bytes) (*ExecutableDataV1, error) { + hash := []byte(payloadID) + if len(hash) < 8 { + return nil, &InvalidPayloadID } - id := len(api.preparedBlocks) - api.preparedBlocks[id] = data - return &PayloadResponse{PayloadID: uint64(id)}, nil -} - -func (api *ConsensusAPI) GetPayload(PayloadID hexutil.Uint64) (*ExecutableData, error) { - data, ok := api.preparedBlocks[int(PayloadID)] + id := binary.BigEndian.Uint64(hash[:8]) + data, ok := api.preparedBlocks[id] if !ok { return nil, &UnknownPayload } return data, nil } -// ConsensusValidated is called to mark a block as valid, so -// that data that is no longer needed can be removed. -func (api *ConsensusAPI) ConsensusValidated(params ConsensusValidatedParams) error { - switch params.Status { - case VALID.Status: - return nil - case INVALID.Status: - // TODO (MariusVanDerWijden) delete the block from the bc - return nil - default: - return errors.New("invalid params.status") +func (api *ConsensusAPI) ForkchoiceUpdatedV1(heads ForkchoiceStateV1, PayloadAttributes *PayloadAttributesV1) (ForkChoiceResponse, error) { + if heads.HeadBlockHash == (common.Hash{}) { + return ForkChoiceResponse{Status: SUCCESS.Status, PayloadID: nil}, nil } + if err := api.checkTerminalTotalDifficulty(heads.HeadBlockHash); err != nil { + if block := api.eth.BlockChain().GetBlockByHash(heads.HeadBlockHash); block == nil { + // TODO (MariusVanDerWijden) trigger sync + return SYNCING, nil + } + return INVALID, err + } + // If the finalized block is set, check if it is in our blockchain + if heads.FinalizedBlockHash != (common.Hash{}) { + if block := api.eth.BlockChain().GetBlockByHash(heads.FinalizedBlockHash); block == nil { + // TODO (MariusVanDerWijden) trigger sync + return SYNCING, nil + } + } + // SetHead + if err := api.setHead(heads.HeadBlockHash); err != nil { + return INVALID, err + } + // Assemble block (if needed) + if PayloadAttributes != nil { + data, err := api.assembleBlock(heads.HeadBlockHash, PayloadAttributes) + if err != nil { + return INVALID, err + } + hash := computePayloadId(heads.HeadBlockHash, PayloadAttributes) + id := binary.BigEndian.Uint64(hash) + api.preparedBlocks[id] = data + log.Info("Created payload", "payloadid", id) + // TODO (MariusVanDerWijden) do something with the payloadID? + hex := hexutil.Bytes(hash) + return ForkChoiceResponse{Status: SUCCESS.Status, PayloadID: &hex}, nil + } + return ForkChoiceResponse{Status: SUCCESS.Status, PayloadID: nil}, nil } -func (api *ConsensusAPI) ForkchoiceUpdated(params ForkChoiceParams) error { - var emptyHash = common.Hash{} - if !bytes.Equal(params.HeadBlockHash[:], emptyHash[:]) { - if err := api.checkTerminalTotalDifficulty(params.HeadBlockHash); err != nil { - return err - } - return api.setHead(params.HeadBlockHash) +func computePayloadId(headBlockHash common.Hash, params *PayloadAttributesV1) []byte { + time := make([]byte, 8) + binary.BigEndian.PutUint64(time, params.Timestamp) + // Hash + hasher := sha256.New() + hasher.Write(headBlockHash[:]) + hasher.Write(time) + hasher.Write(params.Random[:]) + hasher.Write(params.FeeRecipient[:]) + return hasher.Sum([]byte{})[:8] +} + +func (api *ConsensusAPI) invalid() ExecutePayloadResponse { + if api.light { + return ExecutePayloadResponse{Status: INVALID.Status, LatestValidHash: api.les.BlockChain().CurrentHeader().Hash()} } - return nil + return ExecutePayloadResponse{Status: INVALID.Status, LatestValidHash: api.eth.BlockChain().CurrentHeader().Hash()} } // ExecutePayload creates an Eth1 block, inserts it in the chain, and returns the status of the chain. -func (api *ConsensusAPI) ExecutePayload(params ExecutableData) (GenericStringResponse, error) { +func (api *ConsensusAPI) ExecutePayloadV1(params ExecutableDataV1) (ExecutePayloadResponse, error) { block, err := ExecutableDataToBlock(params) if err != nil { - return INVALID, err + return api.invalid(), err } if api.light { parent := api.les.BlockChain().GetHeaderByHash(params.ParentHash) if parent == nil { - return INVALID, fmt.Errorf("could not find parent %x", params.ParentHash) + return api.invalid(), fmt.Errorf("could not find parent %x", params.ParentHash) } if err = api.les.BlockChain().InsertHeader(block.Header()); err != nil { - return INVALID, err + return api.invalid(), err } - return VALID, nil + return ExecutePayloadResponse{Status: VALID.Status, LatestValidHash: block.Hash()}, nil } if !api.eth.BlockChain().HasBlock(block.ParentHash(), block.NumberU64()-1) { /* @@ -237,37 +269,41 @@ func (api *ConsensusAPI) ExecutePayload(params ExecutableData) (GenericStringRes return SYNCING, err } */ - return SYNCING, nil + // TODO (MariusVanDerWijden) we should return nil here not empty hash + return ExecutePayloadResponse{Status: SYNCING.Status, LatestValidHash: common.Hash{}}, nil } parent := api.eth.BlockChain().GetBlockByHash(params.ParentHash) td := api.eth.BlockChain().GetTd(parent.Hash(), block.NumberU64()-1) ttd := api.eth.BlockChain().Config().TerminalTotalDifficulty if td.Cmp(ttd) < 0 { - return INVALID, fmt.Errorf("can not execute payload on top of block with low td got: %v threshold %v", td, ttd) + return api.invalid(), fmt.Errorf("can not execute payload on top of block with low td got: %v threshold %v", td, ttd) } + conf := api.eth.BlockChain().GetVMConfig() + conf.RandomOpcode = true + api.eth.BlockChain().SetVMConfig(*conf) if err := api.eth.BlockChain().InsertBlockWithoutSetHead(block); err != nil { - return INVALID, err + return api.invalid(), err } merger := api.merger() if !merger.TDDReached() { merger.ReachTTD() } - return VALID, nil + return ExecutePayloadResponse{Status: VALID.Status, LatestValidHash: block.Hash()}, nil } // AssembleBlock creates a new block, inserts it into the chain, and returns the "execution // data" required for eth2 clients to process the new block. -func (api *ConsensusAPI) assembleBlock(params AssembleBlockParams) (*ExecutableData, error) { +func (api *ConsensusAPI) assembleBlock(parentHash common.Hash, params *PayloadAttributesV1) (*ExecutableDataV1, error) { if api.light { return nil, errors.New("not supported") } - log.Info("Producing block", "parentHash", params.ParentHash) + log.Info("Producing block", "parentHash", parentHash) bc := api.eth.BlockChain() - parent := bc.GetBlockByHash(params.ParentHash) + parent := bc.GetBlockByHash(parentHash) if parent == nil { - log.Warn("Cannot assemble block with parent hash to unknown block", "parentHash", params.ParentHash) - return nil, fmt.Errorf("cannot assemble block with unknown parent %s", params.ParentHash) + log.Warn("Cannot assemble block with parent hash to unknown block", "parentHash", parentHash) + return nil, fmt.Errorf("cannot assemble block with unknown parent %s", parentHash) } if params.Timestamp < parent.Time() { @@ -287,7 +323,11 @@ func (api *ConsensusAPI) assembleBlock(params AssembleBlockParams) (*ExecutableD GasLimit: parent.GasLimit(), // Keep the gas limit constant in this prototype Extra: []byte{}, // TODO (MariusVanDerWijden) properly set extra data Time: params.Timestamp, + MixDigest: params.Random, } + conf := api.eth.BlockChain().GetVMConfig() + conf.RandomOpcode = true + api.eth.BlockChain().SetVMConfig(*conf) if config := api.eth.BlockChain().Config(); config.IsLondon(header.Number) { header.BaseFee = misc.CalcBaseFee(config, parent.Header()) } @@ -376,7 +416,7 @@ func decodeTransactions(enc [][]byte) ([]*types.Transaction, error) { return txs, nil } -func ExecutableDataToBlock(params ExecutableData) (*types.Block, error) { +func ExecutableDataToBlock(params ExecutableDataV1) (*types.Block, error) { txs, err := decodeTransactions(params.Transactions) if err != nil { return nil, err @@ -401,6 +441,7 @@ func ExecutableDataToBlock(params ExecutableData) (*types.Block, error) { Time: params.Timestamp, BaseFee: params.BaseFeePerGas, Extra: params.ExtraData, + MixDigest: params.Random, // TODO (MariusVanDerWijden) add params.Random to header once required } block := types.NewBlockWithHeader(header).WithBody(txs, nil /* uncles */) @@ -410,8 +451,8 @@ func ExecutableDataToBlock(params ExecutableData) (*types.Block, error) { return block, nil } -func BlockToExecutableData(block *types.Block, random common.Hash) *ExecutableData { - return &ExecutableData{ +func BlockToExecutableData(block *types.Block, random common.Hash) *ExecutableDataV1 { + return &ExecutableDataV1{ BlockHash: block.Hash(), ParentHash: block.ParentHash(), Coinbase: block.Coinbase(), @@ -447,11 +488,7 @@ func (api *ConsensusAPI) checkTerminalTotalDifficulty(head common.Hash) error { if newHeadBlock == nil { return &UnknownHeader } - parent := api.eth.BlockChain().GetBlockByHash(newHeadBlock.ParentHash()) - if parent == nil { - return fmt.Errorf("parent unavailable: %v", newHeadBlock.ParentHash()) - } - td := api.eth.BlockChain().GetTd(parent.Hash(), parent.NumberU64()) + td := api.eth.BlockChain().GetTd(newHeadBlock.Hash(), newHeadBlock.NumberU64()) if td != nil && td.Cmp(api.eth.BlockChain().Config().TerminalTotalDifficulty) < 0 { return errors.New("total difficulty not reached yet") } @@ -460,11 +497,6 @@ func (api *ConsensusAPI) checkTerminalTotalDifficulty(head common.Hash) error { // setHead is called to perform a force choice. func (api *ConsensusAPI) setHead(newHead common.Hash) error { - // Trigger the transition if it's the first `NewHead` event. - merger := api.merger() - if !merger.PoSFinalized() { - merger.FinalizePoS() - } log.Info("Setting head", "head", newHead) if api.light { headHeader := api.les.BlockChain().CurrentHeader() @@ -478,10 +510,20 @@ func (api *ConsensusAPI) setHead(newHead common.Hash) error { if err := api.les.BlockChain().SetChainHead(newHeadHeader); err != nil { return err } + // Trigger the transition if it's the first `NewHead` event. + merger := api.merger() + if !merger.PoSFinalized() { + merger.FinalizePoS() + } return nil } headBlock := api.eth.BlockChain().CurrentBlock() if headBlock.Hash() == newHead { + // Trigger the transition if it's the first `NewHead` event. + merger := api.merger() + if !merger.PoSFinalized() { + merger.FinalizePoS() + } return nil } newHeadBlock := api.eth.BlockChain().GetBlockByHash(newHead) @@ -491,6 +533,12 @@ func (api *ConsensusAPI) setHead(newHead common.Hash) error { if err := api.eth.BlockChain().SetChainHead(newHeadBlock); err != nil { return err } + // Trigger the transition if it's the first `NewHead` event. + merger := api.merger() + if !merger.PoSFinalized() { + merger.FinalizePoS() + } + // TODO (MariusVanDerWijden) are we really synced now? api.eth.SetSynced() return nil } diff --git a/eth/catalyst/api_test.go b/eth/catalyst/api_test.go index 98130673eb74..012b83f9c912 100644 --- a/eth/catalyst/api_test.go +++ b/eth/catalyst/api_test.go @@ -85,11 +85,10 @@ func TestEth2AssembleBlock(t *testing.T) { t.Fatalf("error signing transaction, err=%v", err) } ethservice.TxPool().AddLocal(tx) - blockParams := AssembleBlockParams{ - ParentHash: blocks[9].Hash(), - Timestamp: blocks[9].Time() + 5, + blockParams := PayloadAttributesV1{ + Timestamp: blocks[9].Time() + 5, } - execData, err := api.assembleBlock(blockParams) + execData, err := api.assembleBlock(blocks[9].Hash(), &blockParams) if err != nil { t.Fatalf("error producing block, err=%v", err) } @@ -107,11 +106,10 @@ func TestEth2AssembleBlockWithAnotherBlocksTxs(t *testing.T) { // Put the 10th block's tx in the pool and produce a new block api.insertTransactions(blocks[9].Transactions()) - blockParams := AssembleBlockParams{ - ParentHash: blocks[8].Hash(), - Timestamp: blocks[8].Time() + 5, + blockParams := PayloadAttributesV1{ + Timestamp: blocks[8].Time() + 5, } - execData, err := api.assembleBlock(blockParams) + execData, err := api.assembleBlock(blocks[8].Hash(), &blockParams) if err != nil { t.Fatalf("error producing block, err=%v", err) } @@ -126,14 +124,20 @@ func TestSetHeadBeforeTotalDifficulty(t *testing.T) { defer n.Close() api := NewConsensusAPI(ethservice, nil) - - if err := api.ForkchoiceUpdated(ForkChoiceParams{HeadBlockHash: blocks[5].Hash()}); err == nil { + fcState := ForkchoiceStateV1{ + HeadBlockHash: blocks[5].Hash(), + SafeBlockHash: common.Hash{}, + FinalizedBlockHash: common.Hash{}, + } + if _, err := api.ForkchoiceUpdatedV1(fcState, nil); err == nil { t.Errorf("fork choice updated before total terminal difficulty should fail") } } func TestEth2PrepareAndGetPayload(t *testing.T) { genesis, blocks := generatePreMergeChain(10) + // We need to properly set the terminal total difficulty + genesis.Config.TerminalTotalDifficulty.Sub(genesis.Config.TerminalTotalDifficulty, blocks[9].Difficulty()) n, ethservice := startEthService(t, genesis, blocks[:9]) defer n.Close() @@ -141,15 +145,20 @@ func TestEth2PrepareAndGetPayload(t *testing.T) { // Put the 10th block's tx in the pool and produce a new block api.insertTransactions(blocks[9].Transactions()) - blockParams := AssembleBlockParams{ - ParentHash: blocks[8].Hash(), - Timestamp: blocks[8].Time() + 5, + blockParams := PayloadAttributesV1{ + Timestamp: blocks[8].Time() + 5, + } + fcState := ForkchoiceStateV1{ + HeadBlockHash: blocks[8].Hash(), + SafeBlockHash: common.Hash{}, + FinalizedBlockHash: common.Hash{}, } - respID, err := api.PreparePayload(blockParams) + _, err := api.ForkchoiceUpdatedV1(fcState, &blockParams) if err != nil { t.Fatalf("error preparing payload, err=%v", err) } - execData, err := api.GetPayload(hexutil.Uint64(respID.PayloadID)) + payloadID := computePayloadId(fcState.HeadBlockHash, &blockParams) + execData, err := api.GetPayloadV1(hexutil.Bytes(payloadID)) if err != nil { t.Fatalf("error getting payload, err=%v", err) } @@ -201,9 +210,8 @@ func TestEth2NewBlock(t *testing.T) { tx, _ := types.SignTx(types.NewContractCreation(nonce, new(big.Int), 1000000, big.NewInt(2*params.InitialBaseFee), logCode), types.LatestSigner(ethservice.BlockChain().Config()), testKey) ethservice.TxPool().AddLocal(tx) - execData, err := api.assembleBlock(AssembleBlockParams{ - ParentHash: parent.Hash(), - Timestamp: parent.Time() + 5, + execData, err := api.assembleBlock(parent.Hash(), &PayloadAttributesV1{ + Timestamp: parent.Time() + 5, }) if err != nil { t.Fatalf("Failed to create the executable data %v", err) @@ -212,7 +220,7 @@ func TestEth2NewBlock(t *testing.T) { if err != nil { t.Fatalf("Failed to convert executable data to block %v", err) } - newResp, err := api.ExecutePayload(*execData) + newResp, err := api.ExecutePayloadV1(*execData) if err != nil || newResp.Status != "VALID" { t.Fatalf("Failed to insert block: %v", err) } @@ -220,8 +228,12 @@ func TestEth2NewBlock(t *testing.T) { t.Fatalf("Chain head shouldn't be updated") } checkLogEvents(t, newLogCh, rmLogsCh, 0, 0) - - if err := api.ForkchoiceUpdated(ForkChoiceParams{HeadBlockHash: block.Hash(), FinalizedBlockHash: block.Hash()}); err != nil { + fcState := ForkchoiceStateV1{ + HeadBlockHash: block.Hash(), + SafeBlockHash: block.Hash(), + FinalizedBlockHash: block.Hash(), + } + if _, err := api.ForkchoiceUpdatedV1(fcState, nil); err != nil { t.Fatalf("Failed to insert block: %v", err) } if ethservice.BlockChain().CurrentBlock().NumberU64() != block.NumberU64() { @@ -238,9 +250,8 @@ func TestEth2NewBlock(t *testing.T) { ) parent = preMergeBlocks[len(preMergeBlocks)-1] for i := 0; i < 10; i++ { - execData, err := api.assembleBlock(AssembleBlockParams{ - ParentHash: parent.Hash(), - Timestamp: parent.Time() + 6, + execData, err := api.assembleBlock(parent.Hash(), &PayloadAttributesV1{ + Timestamp: parent.Time() + 6, }) if err != nil { t.Fatalf("Failed to create the executable data %v", err) @@ -249,7 +260,7 @@ func TestEth2NewBlock(t *testing.T) { if err != nil { t.Fatalf("Failed to convert executable data to block %v", err) } - newResp, err := api.ExecutePayload(*execData) + newResp, err := api.ExecutePayloadV1(*execData) if err != nil || newResp.Status != "VALID" { t.Fatalf("Failed to insert block: %v", err) } @@ -257,10 +268,12 @@ func TestEth2NewBlock(t *testing.T) { t.Fatalf("Chain head shouldn't be updated") } - if err := api.ConsensusValidated(ConsensusValidatedParams{BlockHash: block.Hash(), Status: "VALID"}); err != nil { - t.Fatalf("Failed to insert block: %v", err) + fcState := ForkchoiceStateV1{ + HeadBlockHash: block.Hash(), + SafeBlockHash: block.Hash(), + FinalizedBlockHash: block.Hash(), } - if err := api.ForkchoiceUpdated(ForkChoiceParams{FinalizedBlockHash: block.Hash(), HeadBlockHash: block.Hash()}); err != nil { + if _, err := api.ForkchoiceUpdatedV1(fcState, nil); err != nil { t.Fatalf("Failed to insert block: %v", err) } if ethservice.BlockChain().CurrentBlock().NumberU64() != block.NumberU64() { @@ -360,33 +373,41 @@ func TestFullAPI(t *testing.T) { tx, _ := types.SignTx(types.NewContractCreation(nonce, new(big.Int), 1000000, big.NewInt(2*params.InitialBaseFee), logCode), types.LatestSigner(ethservice.BlockChain().Config()), testKey) ethservice.TxPool().AddLocal(tx) - params := AssembleBlockParams{ - ParentHash: parent.Hash(), + params := PayloadAttributesV1{ Timestamp: parent.Time() + 1, Random: crypto.Keccak256Hash([]byte{byte(i)}), FeeRecipient: parent.Coinbase(), } - resp, err := api.PreparePayload(params) + fcState := ForkchoiceStateV1{ + HeadBlockHash: parent.Hash(), + SafeBlockHash: common.Hash{}, + FinalizedBlockHash: common.Hash{}, + } + resp, err := api.ForkchoiceUpdatedV1(fcState, ¶ms) if err != nil { - t.Fatalf("can't prepare payload: %v", err) + t.Fatalf("error preparing payload, err=%v", err) + } + if resp.Status != SUCCESS.Status { + t.Fatalf("error preparing payload, invalid status: %v", resp.Status) } - payload, err := api.GetPayload(hexutil.Uint64(resp.PayloadID)) + payloadID := computePayloadId(parent.Hash(), ¶ms) + payload, err := api.GetPayloadV1(hexutil.Bytes(payloadID)) if err != nil { t.Fatalf("can't get payload: %v", err) } - execResp, err := api.ExecutePayload(*payload) + execResp, err := api.ExecutePayloadV1(*payload) if err != nil { t.Fatalf("can't execute payload: %v", err) } if execResp.Status != VALID.Status { t.Fatalf("invalid status: %v", execResp.Status) } - - if err := api.ConsensusValidated(ConsensusValidatedParams{BlockHash: payload.BlockHash, Status: VALID.Status}); err != nil { - t.Fatalf("failed to validate consensus: %v", err) + fcState = ForkchoiceStateV1{ + HeadBlockHash: payload.BlockHash, + SafeBlockHash: payload.ParentHash, + FinalizedBlockHash: payload.ParentHash, } - - if err := api.ForkchoiceUpdated(ForkChoiceParams{HeadBlockHash: payload.BlockHash, FinalizedBlockHash: payload.BlockHash}); err != nil { + if _, err := api.ForkchoiceUpdatedV1(fcState, nil); err != nil { t.Fatalf("Failed to insert block: %v", err) } if ethservice.BlockChain().CurrentBlock().NumberU64() != payload.Number { diff --git a/eth/catalyst/api_types.go b/eth/catalyst/api_types.go index ff0aea39bf24..2b684e564f4d 100644 --- a/eth/catalyst/api_types.go +++ b/eth/catalyst/api_types.go @@ -23,26 +23,24 @@ import ( "github.com/ethereum/go-ethereum/common/hexutil" ) -//go:generate go run github.com/fjl/gencodec -type AssembleBlockParams -field-override assembleBlockParamsMarshaling -out gen_blockparams.go +//go:generate go run github.com/fjl/gencodec -type PayloadAttributesV1 -field-override payloadAttributesMarshaling -out gen_blockparams.go // Structure described at https://github.com/ethereum/execution-apis/pull/74 -type AssembleBlockParams struct { - ParentHash common.Hash `json:"parentHash" gencodec:"required"` +type PayloadAttributesV1 struct { Timestamp uint64 `json:"timestamp" gencodec:"required"` Random common.Hash `json:"random" gencodec:"required"` FeeRecipient common.Address `json:"feeRecipient" gencodec:"required"` } -// JSON type overrides for assembleBlockParams. -type assembleBlockParamsMarshaling struct { +// JSON type overrides for PayloadAttributesV1. +type payloadAttributesMarshaling struct { Timestamp hexutil.Uint64 } -//go:generate go run github.com/fjl/gencodec -type ExecutableData -field-override executableDataMarshaling -out gen_ed.go +//go:generate go run github.com/fjl/gencodec -type ExecutableDataV1 -field-override executableDataMarshaling -out gen_ed.go -// Structure described at https://github.com/ethereum/execution-apis/pull/74/files -type ExecutableData struct { - BlockHash common.Hash `json:"blockHash" gencodec:"required"` +// Structure described at https://github.com/ethereum/execution-apis/src/engine/specification.md +type ExecutableDataV1 struct { ParentHash common.Hash `json:"parentHash" gencodec:"required"` Coinbase common.Address `json:"coinbase" gencodec:"required"` StateRoot common.Hash `json:"stateRoot" gencodec:"required"` @@ -55,6 +53,7 @@ type ExecutableData struct { Timestamp uint64 `json:"timestamp" gencodec:"required"` ExtraData []byte `json:"extraData" gencodec:"required"` BaseFeePerGas *big.Int `json:"baseFeePerGas" gencodec:"required"` + BlockHash common.Hash `json:"blockHash" gencodec:"required"` Transactions [][]byte `json:"transactions" gencodec:"required"` } @@ -93,12 +92,23 @@ type GenericStringResponse struct { Status string `json:"status"` } +type ExecutePayloadResponse struct { + Status string `json:"status"` + LatestValidHash common.Hash `json:"latestValidHash"` +} + type ConsensusValidatedParams struct { BlockHash common.Hash `json:"blockHash"` Status string `json:"status"` } -type ForkChoiceParams struct { +type ForkChoiceResponse struct { + Status string `json:"status"` + PayloadID *hexutil.Bytes `json:"payloadId"` +} + +type ForkchoiceStateV1 struct { HeadBlockHash common.Hash `json:"headBlockHash"` + SafeBlockHash common.Hash `json:"safeBlockHash"` FinalizedBlockHash common.Hash `json:"finalizedBlockHash"` } diff --git a/eth/catalyst/gen_blockparams.go b/eth/catalyst/gen_blockparams.go index 9928c12908a9..04ea6faf0bac 100644 --- a/eth/catalyst/gen_blockparams.go +++ b/eth/catalyst/gen_blockparams.go @@ -10,51 +10,44 @@ import ( "github.com/ethereum/go-ethereum/common/hexutil" ) -var _ = (*assembleBlockParamsMarshaling)(nil) +var _ = (*payloadAttributesMarshaling)(nil) // MarshalJSON marshals as JSON. -func (a AssembleBlockParams) MarshalJSON() ([]byte, error) { - type AssembleBlockParams struct { - ParentHash common.Hash `json:"parentHash" gencodec:"required"` +func (p PayloadAttributesV1) MarshalJSON() ([]byte, error) { + type PayloadAttributesV1 struct { Timestamp hexutil.Uint64 `json:"timestamp" gencodec:"required"` Random common.Hash `json:"random" gencodec:"required"` FeeRecipient common.Address `json:"feeRecipient" gencodec:"required"` } - var enc AssembleBlockParams - enc.ParentHash = a.ParentHash - enc.Timestamp = hexutil.Uint64(a.Timestamp) - enc.Random = a.Random - enc.FeeRecipient = a.FeeRecipient + var enc PayloadAttributesV1 + enc.Timestamp = hexutil.Uint64(p.Timestamp) + enc.Random = p.Random + enc.FeeRecipient = p.FeeRecipient return json.Marshal(&enc) } // UnmarshalJSON unmarshals from JSON. -func (a *AssembleBlockParams) UnmarshalJSON(input []byte) error { - type AssembleBlockParams struct { - ParentHash *common.Hash `json:"parentHash" gencodec:"required"` +func (p *PayloadAttributesV1) UnmarshalJSON(input []byte) error { + type PayloadAttributesV1 struct { Timestamp *hexutil.Uint64 `json:"timestamp" gencodec:"required"` Random *common.Hash `json:"random" gencodec:"required"` FeeRecipient *common.Address `json:"feeRecipient" gencodec:"required"` } - var dec AssembleBlockParams + var dec PayloadAttributesV1 if err := json.Unmarshal(input, &dec); err != nil { return err } - if dec.ParentHash == nil { - return errors.New("missing required field 'parentHash' for AssembleBlockParams") - } - a.ParentHash = *dec.ParentHash if dec.Timestamp == nil { - return errors.New("missing required field 'timestamp' for AssembleBlockParams") + return errors.New("missing required field 'timestamp' for PayloadAttributesV1") } - a.Timestamp = uint64(*dec.Timestamp) + p.Timestamp = uint64(*dec.Timestamp) if dec.Random == nil { - return errors.New("missing required field 'random' for AssembleBlockParams") + return errors.New("missing required field 'random' for PayloadAttributesV1") } - a.Random = *dec.Random + p.Random = *dec.Random if dec.FeeRecipient == nil { - return errors.New("missing required field 'feeRecipient' for AssembleBlockParams") + return errors.New("missing required field 'feeRecipient' for PayloadAttributesV1") } - a.FeeRecipient = *dec.FeeRecipient + p.FeeRecipient = *dec.FeeRecipient return nil } diff --git a/eth/catalyst/gen_ed.go b/eth/catalyst/gen_ed.go index 2953ab820fc2..babfb44df0bd 100644 --- a/eth/catalyst/gen_ed.go +++ b/eth/catalyst/gen_ed.go @@ -14,9 +14,8 @@ import ( var _ = (*executableDataMarshaling)(nil) // MarshalJSON marshals as JSON. -func (e ExecutableData) MarshalJSON() ([]byte, error) { - type ExecutableData struct { - BlockHash common.Hash `json:"blockHash" gencodec:"required"` +func (e ExecutableDataV1) MarshalJSON() ([]byte, error) { + type ExecutableDataV1 struct { ParentHash common.Hash `json:"parentHash" gencodec:"required"` Coinbase common.Address `json:"coinbase" gencodec:"required"` StateRoot common.Hash `json:"stateRoot" gencodec:"required"` @@ -29,10 +28,10 @@ func (e ExecutableData) MarshalJSON() ([]byte, error) { Timestamp hexutil.Uint64 `json:"timestamp" gencodec:"required"` ExtraData hexutil.Bytes `json:"extraData" gencodec:"required"` BaseFeePerGas *hexutil.Big `json:"baseFeePerGas" gencodec:"required"` + BlockHash common.Hash `json:"blockHash" gencodec:"required"` Transactions []hexutil.Bytes `json:"transactions" gencodec:"required"` } - var enc ExecutableData - enc.BlockHash = e.BlockHash + var enc ExecutableDataV1 enc.ParentHash = e.ParentHash enc.Coinbase = e.Coinbase enc.StateRoot = e.StateRoot @@ -45,6 +44,7 @@ func (e ExecutableData) MarshalJSON() ([]byte, error) { enc.Timestamp = hexutil.Uint64(e.Timestamp) enc.ExtraData = e.ExtraData enc.BaseFeePerGas = (*hexutil.Big)(e.BaseFeePerGas) + enc.BlockHash = e.BlockHash if e.Transactions != nil { enc.Transactions = make([]hexutil.Bytes, len(e.Transactions)) for k, v := range e.Transactions { @@ -55,9 +55,8 @@ func (e ExecutableData) MarshalJSON() ([]byte, error) { } // UnmarshalJSON unmarshals from JSON. -func (e *ExecutableData) UnmarshalJSON(input []byte) error { - type ExecutableData struct { - BlockHash *common.Hash `json:"blockHash" gencodec:"required"` +func (e *ExecutableDataV1) UnmarshalJSON(input []byte) error { + type ExecutableDataV1 struct { ParentHash *common.Hash `json:"parentHash" gencodec:"required"` Coinbase *common.Address `json:"coinbase" gencodec:"required"` StateRoot *common.Hash `json:"stateRoot" gencodec:"required"` @@ -70,66 +69,67 @@ func (e *ExecutableData) UnmarshalJSON(input []byte) error { Timestamp *hexutil.Uint64 `json:"timestamp" gencodec:"required"` ExtraData *hexutil.Bytes `json:"extraData" gencodec:"required"` BaseFeePerGas *hexutil.Big `json:"baseFeePerGas" gencodec:"required"` + BlockHash *common.Hash `json:"blockHash" gencodec:"required"` Transactions []hexutil.Bytes `json:"transactions" gencodec:"required"` } - var dec ExecutableData + var dec ExecutableDataV1 if err := json.Unmarshal(input, &dec); err != nil { return err } - if dec.BlockHash == nil { - return errors.New("missing required field 'blockHash' for ExecutableData") - } - e.BlockHash = *dec.BlockHash if dec.ParentHash == nil { - return errors.New("missing required field 'parentHash' for ExecutableData") + return errors.New("missing required field 'parentHash' for ExecutableDataV1") } e.ParentHash = *dec.ParentHash if dec.Coinbase == nil { - return errors.New("missing required field 'coinbase' for ExecutableData") + return errors.New("missing required field 'coinbase' for ExecutableDataV1") } e.Coinbase = *dec.Coinbase if dec.StateRoot == nil { - return errors.New("missing required field 'stateRoot' for ExecutableData") + return errors.New("missing required field 'stateRoot' for ExecutableDataV1") } e.StateRoot = *dec.StateRoot if dec.ReceiptRoot == nil { - return errors.New("missing required field 'receiptRoot' for ExecutableData") + return errors.New("missing required field 'receiptRoot' for ExecutableDataV1") } e.ReceiptRoot = *dec.ReceiptRoot if dec.LogsBloom == nil { - return errors.New("missing required field 'logsBloom' for ExecutableData") + return errors.New("missing required field 'logsBloom' for ExecutableDataV1") } e.LogsBloom = *dec.LogsBloom if dec.Random == nil { - return errors.New("missing required field 'random' for ExecutableData") + return errors.New("missing required field 'random' for ExecutableDataV1") } e.Random = *dec.Random if dec.Number == nil { - return errors.New("missing required field 'blockNumber' for ExecutableData") + return errors.New("missing required field 'blockNumber' for ExecutableDataV1") } e.Number = uint64(*dec.Number) if dec.GasLimit == nil { - return errors.New("missing required field 'gasLimit' for ExecutableData") + return errors.New("missing required field 'gasLimit' for ExecutableDataV1") } e.GasLimit = uint64(*dec.GasLimit) if dec.GasUsed == nil { - return errors.New("missing required field 'gasUsed' for ExecutableData") + return errors.New("missing required field 'gasUsed' for ExecutableDataV1") } e.GasUsed = uint64(*dec.GasUsed) if dec.Timestamp == nil { - return errors.New("missing required field 'timestamp' for ExecutableData") + return errors.New("missing required field 'timestamp' for ExecutableDataV1") } e.Timestamp = uint64(*dec.Timestamp) if dec.ExtraData == nil { - return errors.New("missing required field 'extraData' for ExecutableData") + return errors.New("missing required field 'extraData' for ExecutableDataV1") } e.ExtraData = *dec.ExtraData if dec.BaseFeePerGas == nil { - return errors.New("missing required field 'baseFeePerGas' for ExecutableData") + return errors.New("missing required field 'baseFeePerGas' for ExecutableDataV1") } e.BaseFeePerGas = (*big.Int)(dec.BaseFeePerGas) + if dec.BlockHash == nil { + return errors.New("missing required field 'blockHash' for ExecutableDataV1") + } + e.BlockHash = *dec.BlockHash if dec.Transactions == nil { - return errors.New("missing required field 'transactions' for ExecutableData") + return errors.New("missing required field 'transactions' for ExecutableDataV1") } e.Transactions = make([][]byte, len(dec.Transactions)) for k, v := range dec.Transactions { diff --git a/eth/tracers/js/tracer.go b/eth/tracers/js/tracer.go index b8e035e6f3f4..e77fc01e868e 100644 --- a/eth/tracers/js/tracer.go +++ b/eth/tracers/js/tracer.go @@ -697,7 +697,7 @@ func (jst *jsTracer) CaptureStart(env *vm.EVM, from common.Address, to common.Ad jst.ctx["block"] = env.Context.BlockNumber.Uint64() jst.dbWrapper.db = env.StateDB // Update list of precompiles based on current block - rules := env.ChainConfig().Rules(env.Context.BlockNumber) + rules := env.ChainConfig().Rules(env.Context.BlockNumber, false) jst.activePrecompiles = vm.ActivePrecompiles(rules) // Compute intrinsic gas diff --git a/eth/tracers/native/4byte.go b/eth/tracers/native/4byte.go index e60e82de479a..2624ec403ee2 100644 --- a/eth/tracers/native/4byte.go +++ b/eth/tracers/native/4byte.go @@ -83,7 +83,7 @@ func (t *fourByteTracer) CaptureStart(env *vm.EVM, from common.Address, to commo t.env = env // Update list of precompiles based on current block - rules := env.ChainConfig().Rules(env.Context.BlockNumber) + rules := env.ChainConfig().Rules(env.Context.BlockNumber, false) t.activePrecompiles = vm.ActivePrecompiles(rules) // Save the outer calldata also diff --git a/internal/ethapi/api.go b/internal/ethapi/api.go index 65e34752bf41..acb3f8b2a0dd 100644 --- a/internal/ethapi/api.go +++ b/internal/ethapi/api.go @@ -1433,7 +1433,7 @@ func AccessList(ctx context.Context, b Backend, blockNrOrHash rpc.BlockNumberOrH to = crypto.CreateAddress(args.from(), uint64(*args.Nonce)) } // Retrieve the precompiles since they don't need to be added to the access list - precompiles := vm.ActivePrecompiles(b.ChainConfig().Rules(header.Number)) + precompiles := vm.ActivePrecompiles(b.ChainConfig().Rules(header.Number, header.Difficulty == nil)) // Create an initial tracer prevTracer := logger.NewAccessListTracer(nil, args.from(), to, precompiles) diff --git a/miner/stress/beacon/main.go b/miner/stress/beacon/main.go index dc5c229410cf..70005e20dbe9 100644 --- a/miner/stress/beacon/main.go +++ b/miner/stress/beacon/main.go @@ -30,7 +30,6 @@ import ( "github.com/ethereum/go-ethereum/accounts/keystore" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/fdlimit" - "github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/consensus/ethash" "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core/types" @@ -138,25 +137,30 @@ func newNode(typ nodetype, genesis *core.Genesis, enodes []*enode.Node) *ethNode } } -func (n *ethNode) assembleBlock(parentHash common.Hash, parentTimestamp uint64) (*catalyst.ExecutableData, error) { +func (n *ethNode) assembleBlock(parentHash common.Hash, parentTimestamp uint64) (*catalyst.ExecutableDataV1, error) { if n.typ != eth2MiningNode { return nil, errors.New("invalid node type") } - payload, err := n.api.PreparePayload(catalyst.AssembleBlockParams{ - ParentHash: parentHash, - Timestamp: uint64(time.Now().Unix()), - }) + payloadAttribute := catalyst.PayloadAttributesV1{ + Timestamp: uint64(time.Now().Unix()), + } + fcState := catalyst.ForkchoiceStateV1{ + HeadBlockHash: parentHash, + SafeBlockHash: common.Hash{}, + FinalizedBlockHash: common.Hash{}, + } + payload, err := n.api.ForkchoiceUpdatedV1(fcState, &payloadAttribute) if err != nil { return nil, err } - return n.api.GetPayload(hexutil.Uint64(payload.PayloadID)) + return n.api.GetPayloadV1(*payload.PayloadID) } -func (n *ethNode) insertBlock(eb catalyst.ExecutableData) error { +func (n *ethNode) insertBlock(eb catalyst.ExecutableDataV1) error { if !eth2types(n.typ) { return errors.New("invalid node type") } - newResp, err := n.api.ExecutePayload(eb) + newResp, err := n.api.ExecutePayloadV1(eb) if err != nil { return err } else if newResp.Status != "VALID" { @@ -165,7 +169,7 @@ func (n *ethNode) insertBlock(eb catalyst.ExecutableData) error { return nil } -func (n *ethNode) insertBlockAndSetHead(parent *types.Header, ed catalyst.ExecutableData) error { +func (n *ethNode) insertBlockAndSetHead(parent *types.Header, ed catalyst.ExecutableDataV1) error { if !eth2types(n.typ) { return errors.New("invalid node type") } @@ -176,7 +180,12 @@ func (n *ethNode) insertBlockAndSetHead(parent *types.Header, ed catalyst.Execut if err != nil { return err } - if err := n.api.ConsensusValidated(catalyst.ConsensusValidatedParams{BlockHash: block.Hash(), Status: "VALID"}); err != nil { + fcState := catalyst.ForkchoiceStateV1{ + HeadBlockHash: block.ParentHash(), + SafeBlockHash: common.Hash{}, + FinalizedBlockHash: common.Hash{}, + } + if _, err := n.api.ForkchoiceUpdatedV1(fcState, nil); err != nil { return err } return nil @@ -275,7 +284,12 @@ func (mgr *nodeManager) run() { nodes = append(nodes, mgr.getNodes(eth2NormalNode)...) nodes = append(nodes, mgr.getNodes(eth2LightClient)...) for _, node := range append(nodes) { - node.api.ConsensusValidated(catalyst.ConsensusValidatedParams{BlockHash: oldest.Hash(), Status: catalyst.VALID.Status}) + fcState := catalyst.ForkchoiceStateV1{ + HeadBlockHash: oldest.Hash(), + SafeBlockHash: common.Hash{}, + FinalizedBlockHash: common.Hash{}, + } + node.api.ForkchoiceUpdatedV1(fcState, nil) } log.Info("Finalised eth2 block", "number", oldest.NumberU64(), "hash", oldest.Hash()) waitFinalise = waitFinalise[1:] diff --git a/miner/worker.go b/miner/worker.go index 54932a474deb..041c03af8029 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -1040,7 +1040,13 @@ func (w *worker) commit(uncles []*types.Header, interval func(), update bool, st return err } - if w.isRunning() && !w.merger.TDDReached() { + // If we're post merge, just ignore + td, ttd := w.chain.GetTd(block.ParentHash(), block.NumberU64()-1), w.chain.Config().TerminalTotalDifficulty + if td != nil && ttd != nil && td.Cmp(ttd) >= 0 { + return nil + } + + if w.isRunning() { if interval != nil { interval() } diff --git a/params/config.go b/params/config.go index f767c1c4b92b..eaff7d0af29a 100644 --- a/params/config.go +++ b/params/config.go @@ -267,7 +267,7 @@ var ( AllCliqueProtocolChanges = &ChainConfig{big.NewInt(1337), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, nil, nil, &CliqueConfig{Period: 0, Epoch: 30000}} TestChainConfig = &ChainConfig{big.NewInt(1), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, new(EthashConfig), nil} - TestRules = TestChainConfig.Rules(new(big.Int)) + TestRules = TestChainConfig.Rules(new(big.Int), false) ) // TrustedCheckpoint represents a set of post-processed trie roots (CHT and @@ -661,11 +661,12 @@ type Rules struct { ChainID *big.Int IsHomestead, IsEIP150, IsEIP155, IsEIP158 bool IsByzantium, IsConstantinople, IsPetersburg, IsIstanbul bool - IsBerlin, IsLondon bool + IsBerlin, IsLondon, IsMerge bool } // Rules ensures c's ChainID is not nil. -func (c *ChainConfig) Rules(num *big.Int) Rules { +func (c *ChainConfig) Rules(num *big.Int, isMerge bool) Rules { + // TODO (MariusVanDerWijden) replace isMerge after the merge once we know the merge block chainID := c.ChainID if chainID == nil { chainID = new(big.Int) @@ -682,5 +683,6 @@ func (c *ChainConfig) Rules(num *big.Int) Rules { IsIstanbul: c.IsIstanbul(num), IsBerlin: c.IsBerlin(num), IsLondon: c.IsLondon(num), + IsMerge: isMerge, } } diff --git a/tests/gen_stenv.go b/tests/gen_stenv.go index ecf7af850382..29fbce121385 100644 --- a/tests/gen_stenv.go +++ b/tests/gen_stenv.go @@ -17,7 +17,8 @@ var _ = (*stEnvMarshaling)(nil) func (s stEnv) MarshalJSON() ([]byte, error) { type stEnv struct { Coinbase common.UnprefixedAddress `json:"currentCoinbase" gencodec:"required"` - Difficulty *math.HexOrDecimal256 `json:"currentDifficulty" gencodec:"required"` + Difficulty *math.HexOrDecimal256 `json:"currentDifficulty" gencodec:"optional"` + Random *math.HexOrDecimal256 `json:"currentRandom" gencodec:"optional"` GasLimit math.HexOrDecimal64 `json:"currentGasLimit" gencodec:"required"` Number math.HexOrDecimal64 `json:"currentNumber" gencodec:"required"` Timestamp math.HexOrDecimal64 `json:"currentTimestamp" gencodec:"required"` @@ -26,6 +27,7 @@ func (s stEnv) MarshalJSON() ([]byte, error) { var enc stEnv enc.Coinbase = common.UnprefixedAddress(s.Coinbase) enc.Difficulty = (*math.HexOrDecimal256)(s.Difficulty) + enc.Random = (*math.HexOrDecimal256)(s.Random) enc.GasLimit = math.HexOrDecimal64(s.GasLimit) enc.Number = math.HexOrDecimal64(s.Number) enc.Timestamp = math.HexOrDecimal64(s.Timestamp) @@ -37,7 +39,8 @@ func (s stEnv) MarshalJSON() ([]byte, error) { func (s *stEnv) UnmarshalJSON(input []byte) error { type stEnv struct { Coinbase *common.UnprefixedAddress `json:"currentCoinbase" gencodec:"required"` - Difficulty *math.HexOrDecimal256 `json:"currentDifficulty" gencodec:"required"` + Difficulty *math.HexOrDecimal256 `json:"currentDifficulty" gencodec:"optional"` + Random *math.HexOrDecimal256 `json:"currentRandom" gencodec:"optional"` GasLimit *math.HexOrDecimal64 `json:"currentGasLimit" gencodec:"required"` Number *math.HexOrDecimal64 `json:"currentNumber" gencodec:"required"` Timestamp *math.HexOrDecimal64 `json:"currentTimestamp" gencodec:"required"` @@ -51,10 +54,12 @@ func (s *stEnv) UnmarshalJSON(input []byte) error { return errors.New("missing required field 'currentCoinbase' for stEnv") } s.Coinbase = common.Address(*dec.Coinbase) - if dec.Difficulty == nil { - return errors.New("missing required field 'currentDifficulty' for stEnv") + if dec.Difficulty != nil { + s.Difficulty = (*big.Int)(dec.Difficulty) + } + if dec.Random != nil { + s.Random = (*big.Int)(dec.Random) } - s.Difficulty = (*big.Int)(dec.Difficulty) if dec.GasLimit == nil { return errors.New("missing required field 'currentGasLimit' for stEnv") } diff --git a/tests/state_test_util.go b/tests/state_test_util.go index f7fb08bfbc8d..8a5920f29588 100644 --- a/tests/state_test_util.go +++ b/tests/state_test_util.go @@ -80,7 +80,8 @@ type stPostState struct { type stEnv struct { Coinbase common.Address `json:"currentCoinbase" gencodec:"required"` - Difficulty *big.Int `json:"currentDifficulty" gencodec:"required"` + Difficulty *big.Int `json:"currentDifficulty" gencodec:"optional"` + Random *big.Int `json:"currentRandom" gencodec:"optional"` GasLimit uint64 `json:"currentGasLimit" gencodec:"required"` Number uint64 `json:"currentNumber" gencodec:"required"` Timestamp uint64 `json:"currentTimestamp" gencodec:"required"` @@ -90,6 +91,7 @@ type stEnv struct { type stEnvMarshaling struct { Coinbase common.UnprefixedAddress Difficulty *math.HexOrDecimal256 + Random *math.HexOrDecimal256 GasLimit math.HexOrDecimal64 Number math.HexOrDecimal64 Timestamp math.HexOrDecimal64 @@ -218,8 +220,12 @@ func (t *StateTest) RunNoVerify(subtest StateSubtest, vmconfig vm.Config, snapsh context := core.NewEVMBlockContext(block.Header(), nil, &t.json.Env.Coinbase) context.GetHash = vmTestBlockHash context.BaseFee = baseFee + if t.json.Env.Random != nil { + vmconfig.RandomOpcode = true + context.Random = common.BigToHash(t.json.Env.Random) + context.Difficulty = nil + } evm := vm.NewEVM(context, txContext, statedb, config, vmconfig) - // Execute the message. snapshot := statedb.Snapshot() gaspool := new(core.GasPool) @@ -268,7 +274,7 @@ func MakePreState(db ethdb.Database, accounts core.GenesisAlloc, snapshotter boo } func (t *StateTest) genesis(config *params.ChainConfig) *core.Genesis { - return &core.Genesis{ + genesis := &core.Genesis{ Config: config, Coinbase: t.json.Env.Coinbase, Difficulty: t.json.Env.Difficulty, @@ -277,6 +283,12 @@ func (t *StateTest) genesis(config *params.ChainConfig) *core.Genesis { Timestamp: t.json.Env.Timestamp, Alloc: t.json.Pre, } + if t.json.Env.Random != nil { + // Post-Merge + genesis.Mixhash = common.BigToHash(t.json.Env.Random) + genesis.Difficulty = nil + } + return genesis } func (tx *stTransaction) toMessage(ps stPostState, baseFee *big.Int) (core.Message, error) { diff --git a/tests/testdata b/tests/testdata index 092a8834dc44..5d534e37b80e 160000 --- a/tests/testdata +++ b/tests/testdata @@ -1 +1 @@ -Subproject commit 092a8834dc445e683103689d6f0e75a5d380a190 +Subproject commit 5d534e37b80e9310e8c7751f805ca481a451123e