diff --git a/nimbus/merge/eth_common_overload.nim b/nimbus/merge/eth_common_overload.nim new file mode 100644 index 000000000..01c897b4b --- /dev/null +++ b/nimbus/merge/eth_common_overload.nim @@ -0,0 +1,7 @@ +import eth/common + +template prevRandao*(h: BlockHeader | BlockHeaderRef): Hash256 = + h.mixDigest + +template `prevRandao=`*(h: BlockHeader | BlockHeaderRef, hash: Hash256) = + h.mixDigest = hash diff --git a/nimbus/rpc/engine_api.nim b/nimbus/rpc/engine_api.nim index 8601274c6..5417ceea5 100644 --- a/nimbus/rpc/engine_api.nim +++ b/nimbus/rpc/engine_api.nim @@ -41,7 +41,7 @@ proc calcRootHashRlp*(items: openArray[seq[byte]]): Hash256 = return tr.rootHash() proc toBlockHeader(payload: ExecutionPayloadV1): eth_types.BlockHeader = - discard payload.random # TODO: What should this be used for? + discard payload.prevRandao # TODO: What should this be used for? let transactions = seq[seq[byte]](payload.transactions) let txRoot = calcRootHashRlp(transactions) @@ -84,7 +84,7 @@ proc setupEngineAPI*( return payloads[int payloadId] # https://github.com/ethereum/execution-apis/blob/v1.0.0-alpha.5/src/engine/specification.md#engine_executepayloadv1 - server.rpc("engine_executePayloadV1") do(payload: ExecutionPayloadV1) -> ExecutePayloadResponse: + #[server.rpc("engine_executePayloadV1") do(payload: ExecutionPayloadV1) -> ExecutePayloadResponse: # TODO if payload.transactions.len > 0: # Give us a break, a block with transcations? instructions to execute? @@ -140,4 +140,4 @@ proc setupEngineAPI*( return ForkchoiceUpdatedResponse(status: ForkchoiceUpdatedStatus.success, payloadId: some payloadId.toBytesBE.PayloadID) else: - return ForkchoiceUpdatedResponse(status: ForkchoiceUpdatedStatus.success) + return ForkchoiceUpdatedResponse(status: ForkchoiceUpdatedStatus.success)]# diff --git a/nimbus/sealer.nim b/nimbus/sealer.nim index b18bad330..19a0106a0 100644 --- a/nimbus/sealer.nim +++ b/nimbus/sealer.nim @@ -250,7 +250,7 @@ proc generateExecutionPayload*(engine: SealingEngineRef, payloadRes.logsBloom = Web3Bloom blk.header.bloom # TODO Check the extra data length here # payloadres.extraData = web3types.DynamicBytes[256] blk.header.extraData - payloadRes.random = web3types.FixedBytes[32](payloadAttrs.random) + payloadRes.prevRandao = web3types.FixedBytes[32](payloadAttrs.prevRandao) payloadRes.blockNumber = Web3Quantity blk.header.blockNumber.truncate(uint64) payloadRes.gasLimit = Web3Quantity blk.header.gasLimit payloadRes.gasUsed = Web3Quantity blk.header.gasUsed diff --git a/nimbus/utils/tx_pool/tx_chain.nim b/nimbus/utils/tx_pool/tx_chain.nim index a8e5c517a..b7fdfed71 100644 --- a/nimbus/utils/tx_pool/tx_chain.nim +++ b/nimbus/utils/tx_pool/tx_chain.nim @@ -68,7 +68,7 @@ type # EIP-4399 and EIP-3675 ttdReached: bool ## Total Terminal Difficulty reached - random: Hash256 ## POS block randomness + prevRandao: Hash256 ## POS block randomness # ------------------------------------------------------------------------------ # Private functions @@ -83,7 +83,7 @@ proc resetTxEnv(dh: TxChainRef; parent: BlockHeader; fee: Option[UInt256]) timestamp = getTime().utc.toTime, gasLimit = (if dh.maxMode: dh.limits.maxLimit else: dh.limits.trgLimit), fee = fee, - random = dh.random, + prevRandao= dh.prevRandao, miner = dh.miner, chainDB = dh.db, ttdReached= dh.ttdReached) @@ -313,9 +313,9 @@ proc `ttdReached=`*(dh: TxChainRef; val: bool) = ## Setter dh.ttdReached = val -proc `random=`*(dh: TxChainRef; val: Hash256) = +proc `prevRandao=`*(dh: TxChainRef; val: Hash256) = ## Setter - dh.random = val + dh.prevRandao = val # ------------------------------------------------------------------------------ # End diff --git a/nimbus/vm/interpreter.nim b/nimbus/vm/interpreter.nim index 6aa450905..99a8cb082 100644 --- a/nimbus/vm/interpreter.nim +++ b/nimbus/vm/interpreter.nim @@ -12,7 +12,7 @@ import ./interpreter/opcode_values as vmo export - vmo.Op, vmo.Random + vmo.Op, vmo.PrevRandao # see vm_message diff --git a/nimbus/vm/interpreter/opcode_values.nim b/nimbus/vm/interpreter/opcode_values.nim index 8df40cc19..5f3636a6c 100644 --- a/nimbus/vm/interpreter/opcode_values.nim +++ b/nimbus/vm/interpreter/opcode_values.nim @@ -191,4 +191,4 @@ fill_enum_holes: const # EIP-4399 new opcode - Random* = Difficulty + PrevRandao* = Difficulty diff --git a/nimbus/vm/state.nim b/nimbus/vm/state.nim index 2154f2e7d..eb20c7e87 100644 --- a/nimbus/vm/state.nim +++ b/nimbus/vm/state.nim @@ -22,6 +22,10 @@ import ./types, eth/[common, keys] +# a temporary solution until nim-eth bumped +when not compiles(common.prevRandao): + import ../merge/eth_common_overload + {.push raises: [Defect].} const @@ -59,7 +63,7 @@ proc init( timestamp: EthTime; gasLimit: GasInt; fee: Option[Uint256]; - random: Hash256; + prevRandao: Hash256; miner: EthAddress; chainDB: BaseChainDB; ttdReached: bool; @@ -72,7 +76,7 @@ proc init( self.timestamp = timestamp self.gasLimit = gasLimit self.fee = fee - self.random = random + self.prevRandao = prevRandao self.chaindb = chainDB self.ttdReached = ttdReached self.tracer = tracer @@ -88,7 +92,7 @@ proc init( timestamp: EthTime; gasLimit: GasInt; fee: Option[Uint256]; - random: Hash256; + prevRandao: Hash256; miner: EthAddress; chainDB: BaseChainDB; ttdReached: bool; @@ -102,7 +106,7 @@ proc init( timestamp = timestamp, gasLimit = gasLimit, fee = fee, - random = random, + prevRandao= prevRandao, miner = miner, chainDB = chainDB, ttdReached= ttdReached, @@ -125,7 +129,7 @@ proc new*( timestamp: EthTime; ## tx env: time stamp gasLimit: GasInt; ## tx env: gas limit fee: Option[Uint256]; ## tx env: optional base fee - random: Hash256; ## tx env: POS block randomness + prevRandao: Hash256; ## tx env: POS block randomness miner: EthAddress; ## tx env: coinbase(PoW) or signer(PoA) chainDB: BaseChainDB; ## block chain database ttdReached: bool; ## total terminal difficulty reached @@ -146,7 +150,7 @@ proc new*( timestamp = timestamp, gasLimit = gasLimit, fee = fee, - random = random, + prevRandao = prevRandao, miner = miner, chainDB = chainDB, ttdReached = ttdReached, @@ -157,7 +161,7 @@ proc reinit*(self: BaseVMState; ## Object descriptor timestamp: EthTime; ## tx env: time stamp gasLimit: GasInt; ## tx env: gas limit fee: Option[Uint256]; ## tx env: optional base fee - random: Hash256; ## tx env: POS block randomness + prevRandao:Hash256; ## tx env: POS block randomness miner: EthAddress; ## tx env: coinbase(PoW) or signer(PoA) ttdReached:bool; ## total terminal difficulty reached pruneTrie: bool = true): bool @@ -183,7 +187,7 @@ proc reinit*(self: BaseVMState; ## Object descriptor timestamp = timestamp, gasLimit = gasLimit, fee = fee, - random = random, + prevRandao = prevRandao, miner = miner, chainDB = db, ttdReached = ttdReached, @@ -214,7 +218,7 @@ proc reinit*(self: BaseVMState; ## Object descriptor timestamp = header.timestamp, gasLimit = header.gasLimit, fee = header.fee, - random = header.random, + prevRandao= header.prevRandao, miner = self.chainDB.getMinerAddress(header), ttdReached= ttdReached, pruneTrie = pruneTrie) @@ -253,7 +257,7 @@ proc init*( header.timestamp, header.gasLimit, header.fee, - header.random, + header.prevRandao, chainDB.getMinerAddress(header), chainDB, ttdReached, @@ -328,7 +332,7 @@ method blockNumber*(vmState: BaseVMState): BlockNumber {.base, gcsafe.} = method difficulty*(vmState: BaseVMState): UInt256 {.base, gcsafe.} = if vmState.ttdReached: # EIP-4399/EIP-3675 - UInt256.fromBytesBE(vmState.random.data, allowPadding = false) + UInt256.fromBytesBE(vmState.prevRandao.data, allowPadding = false) else: vmState.chainDB.config.calcDifficulty(vmState.timestamp, vmState.parent) diff --git a/nimbus/vm/types.nim b/nimbus/vm/types.nim index 2cd112861..33e2af0f5 100644 --- a/nimbus/vm/types.nim +++ b/nimbus/vm/types.nim @@ -38,7 +38,7 @@ type timestamp* : EthTime gasLimit* : GasInt fee* : Option[Uint256] - random* : Hash256 + prevRandao* : Hash256 ttdReached* : bool name* : string flags* : set[VMFlag] diff --git a/nimbus/vm2/interpreter/op_codes.nim b/nimbus/vm2/interpreter/op_codes.nim index 7e112868c..fdb119848 100644 --- a/nimbus/vm2/interpreter/op_codes.nim +++ b/nimbus/vm2/interpreter/op_codes.nim @@ -211,7 +211,7 @@ type const # EIP-4399 new opcode - Random* = Difficulty + PrevRandao* = Difficulty # ------------------------------------------------------------------------------ # Verify that Op is contiguous and sym names follow some standards diff --git a/nimbus/vm2/state.nim b/nimbus/vm2/state.nim index 33010f20d..9263a3214 100644 --- a/nimbus/vm2/state.nim +++ b/nimbus/vm2/state.nim @@ -21,6 +21,10 @@ import ./types, eth/[common, keys] +# a temporary solution until nim-eth bumped +when not compiles(common.prevRandao): + import ../merge/eth_common_overload + {.push raises: [Defect].} const @@ -58,7 +62,7 @@ proc init( timestamp: EthTime; gasLimit: GasInt; fee: Option[Uint256]; - random: Hash256; + prevRandao: Hash256; miner: EthAddress; chainDB: BaseChainDB; ttdReached: bool; @@ -71,7 +75,7 @@ proc init( self.timestamp = timestamp self.gasLimit = gasLimit self.fee = fee - self.random = random + self.prevRandao = prevRandao self.chaindb = chainDB self.ttdReached = ttdReached self.tracer = tracer @@ -87,7 +91,7 @@ proc init( timestamp: EthTime; gasLimit: GasInt; fee: Option[Uint256]; - random: Hash256; + prevRandao: Hash256; miner: EthAddress; chainDB: BaseChainDB; ttdReached: bool; @@ -101,7 +105,7 @@ proc init( timestamp = timestamp, gasLimit = gasLimit, fee = fee, - random = random, + prevRandao= prevRandao, miner = miner, chainDB = chainDB, ttdReached= ttdReached, @@ -124,7 +128,7 @@ proc new*( timestamp: EthTime; ## tx env: time stamp gasLimit: GasInt; ## tx env: gas limit fee: Option[Uint256]; ## tx env: optional base fee - random: Hash256; ## tx env: POS block randomness + prevRandao: Hash256; ## tx env: POS block randomness miner: EthAddress; ## tx env: coinbase(PoW) or signer(PoA) chainDB: BaseChainDB; ## block chain database ttdReached: bool; ## total terminal difficulty reached @@ -145,7 +149,7 @@ proc new*( timestamp = timestamp, gasLimit = gasLimit, fee = fee, - random = random, + prevRandao = prevRandao, miner = miner, chainDB = chainDB, ttdReached = ttdReached, @@ -156,7 +160,7 @@ proc reinit*(self: BaseVMState; ## Object descriptor timestamp: EthTime; ## tx env: time stamp gasLimit: GasInt; ## tx env: gas limit fee: Option[Uint256]; ## tx env: optional base fee - random: Hash256; ## tx env: POS block randomness + prevRandao:Hash256; ## tx env: POS block randomness miner: EthAddress; ## tx env: coinbase(PoW) or signer(PoA) ttdReached:bool; ## total terminal difficulty reached pruneTrie: bool = true): bool @@ -182,7 +186,7 @@ proc reinit*(self: BaseVMState; ## Object descriptor timestamp = timestamp, gasLimit = gasLimit, fee = fee, - random = random, + prevRandao = prevRandao, miner = miner, chainDB = db, ttdReached = ttdReached, @@ -213,7 +217,7 @@ proc reinit*(self: BaseVMState; ## Object descriptor timestamp = header.timestamp, gasLimit = header.gasLimit, fee = header.fee, - random = header.random, + prevRandao= header.prevRandao, miner = self.chainDB.getMinerAddress(header), ttdReached= ttdReached, pruneTrie = pruneTrie) @@ -252,7 +256,7 @@ proc init*( header.timestamp, header.gasLimit, header.fee, - header.random, + header.prevRandao, chainDB.getMinerAddress(header), chainDB, ttdReached, @@ -315,7 +319,7 @@ method blockNumber*(vmState: BaseVMState): BlockNumber {.base, gcsafe.} = method difficulty*(vmState: BaseVMState): UInt256 {.base, gcsafe.} = if vmState.ttdReached: # EIP-4399/EIP-3675 - UInt256.fromBytesBE(vmState.random.data, allowPadding = false) + UInt256.fromBytesBE(vmState.prevRandao.data, allowPadding = false) else: vmState.chainDB.config.calcDifficulty(vmState.timestamp, vmState.parent) diff --git a/nimbus/vm2/types.nim b/nimbus/vm2/types.nim index 76a698fbe..45aad29d5 100644 --- a/nimbus/vm2/types.nim +++ b/nimbus/vm2/types.nim @@ -29,7 +29,7 @@ type timestamp* : EthTime gasLimit* : GasInt fee* : Option[Uint256] - random* : Hash256 + prevRandao* : Hash256 ttdReached* : bool name* : string flags* : set[VMFlag] diff --git a/nimbus/vm_internals.nim b/nimbus/vm_internals.nim index 90ac22227..351dd959f 100644 --- a/nimbus/vm_internals.nim +++ b/nimbus/vm_internals.nim @@ -133,7 +133,7 @@ else: eGmt.refundGas, eGmt.returnGas, fVmo.Op, - fVmo.Random, + fVmo.PrevRandao, gVmg.isCreate, hStk.Stack, hStk.`$`, diff --git a/tests/macro_assembler.nim b/tests/macro_assembler.nim index 7bbf219d6..033a0d797 100644 --- a/tests/macro_assembler.nim +++ b/tests/macro_assembler.nim @@ -46,10 +46,10 @@ const static: for n in Op: idToOpcode[$n] = newLit(ord(n)) - + # EIP-4399 new opcode - idToOpcode["Random"] = newLit(ord(Difficulty)) - + idToOpcode["PrevRandao"] = newLit(ord(Difficulty)) + proc validateVMWord(val: string, n: NimNode): VMWord = if val.len <= 2 or val.len > 66: error("invalid hex string", n) if not (val[0] == '0' and val[1] == 'x'): error("invalid hex string", n) diff --git a/tests/test_op_env.nim b/tests/test_op_env.nim index ea8a9f3eb..0612141ea 100644 --- a/tests/test_op_env.nim +++ b/tests/test_op_env.nim @@ -326,19 +326,19 @@ proc opEnvMain*() = vmState.ttdReached = true assembler: - title: "EIP-4399 RANDOM 0" + title: "EIP-4399 PrevRandao 0" code: - Random + PrevRandao STOP stack: "0x0000000000000000000000000000000000000000000000000000000000000000" fork: london - vmState.random = EMPTY_UNCLE_HASH + vmState.prevRandao = EMPTY_UNCLE_HASH assembler: - title: "EIP-4399 RANDOM: EMPTY_UNCLE_HASH" + title: "EIP-4399 PrevRandao: EMPTY_UNCLE_HASH" code: - Random + PrevRandao STOP stack: "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347"