EIP 4399 changes: random -> prevRandao

This commit is contained in:
jangko 2022-02-27 12:21:46 +07:00
parent f8ede9659e
commit 667cb6d750
No known key found for this signature in database
GPG Key ID: 31702AE10541E6B9
14 changed files with 59 additions and 44 deletions

View File

@ -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

View File

@ -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)]#

View File

@ -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

View File

@ -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

View File

@ -12,7 +12,7 @@
import
./interpreter/opcode_values as vmo
export
vmo.Op, vmo.Random
vmo.Op, vmo.PrevRandao
# see vm_message

View File

@ -191,4 +191,4 @@ fill_enum_holes:
const
# EIP-4399 new opcode
Random* = Difficulty
PrevRandao* = Difficulty

View File

@ -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)

View File

@ -38,7 +38,7 @@ type
timestamp* : EthTime
gasLimit* : GasInt
fee* : Option[Uint256]
random* : Hash256
prevRandao* : Hash256
ttdReached* : bool
name* : string
flags* : set[VMFlag]

View File

@ -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

View File

@ -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)

View File

@ -29,7 +29,7 @@ type
timestamp* : EthTime
gasLimit* : GasInt
fee* : Option[Uint256]
random* : Hash256
prevRandao* : Hash256
ttdReached* : bool
name* : string
flags* : set[VMFlag]

View File

@ -133,7 +133,7 @@ else:
eGmt.refundGas,
eGmt.returnGas,
fVmo.Op,
fVmo.Random,
fVmo.PrevRandao,
gVmg.isCreate,
hStk.Stack,
hStk.`$`,

View File

@ -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)

View File

@ -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"