2022-04-11 10:00:39 +00:00
|
|
|
import
|
2023-08-21 02:08:54 +00:00
|
|
|
std/[typetraits, times],
|
2022-09-03 18:15:35 +00:00
|
|
|
nimcrypto/sysrand,
|
2023-07-24 14:50:39 +00:00
|
|
|
eth/[common, rlp, keys],
|
2022-08-04 05:06:31 +00:00
|
|
|
json_rpc/[rpcclient],
|
2023-08-21 02:08:54 +00:00
|
|
|
../../../nimbus/transaction,
|
|
|
|
../../../nimbus/utils/utils,
|
2023-08-27 01:23:45 +00:00
|
|
|
../../../nimbus/beacon/execution_types,
|
|
|
|
../../../nimbus/beacon/web3_eth_conv
|
2023-03-09 23:40:55 +00:00
|
|
|
|
2022-04-11 10:00:39 +00:00
|
|
|
type
|
|
|
|
ExecutableData* = object
|
2023-08-27 01:23:45 +00:00
|
|
|
parentHash* : common.Hash256
|
2022-04-11 10:00:39 +00:00
|
|
|
feeRecipient* : EthAddress
|
2023-08-27 01:23:45 +00:00
|
|
|
stateRoot* : common.Hash256
|
|
|
|
receiptsRoot* : common.Hash256
|
2022-04-11 10:00:39 +00:00
|
|
|
logsBloom* : BloomFilter
|
2023-08-27 01:23:45 +00:00
|
|
|
prevRandao* : common.Hash256
|
2022-04-11 10:00:39 +00:00
|
|
|
number* : uint64
|
|
|
|
gasLimit* : GasInt
|
|
|
|
gasUsed* : GasInt
|
|
|
|
timestamp* : EthTime
|
2023-01-27 14:57:48 +00:00
|
|
|
extraData* : common.Blob
|
2022-04-11 10:00:39 +00:00
|
|
|
baseFeePerGas*: UInt256
|
2023-08-27 01:23:45 +00:00
|
|
|
blockHash* : common.Hash256
|
2022-04-11 10:00:39 +00:00
|
|
|
transactions* : seq[Transaction]
|
2023-08-21 02:08:54 +00:00
|
|
|
withdrawals* : Option[seq[Withdrawal]]
|
|
|
|
blobGasUsed* : Option[uint64]
|
|
|
|
excessBlobGas*: Option[uint64]
|
2022-04-11 10:00:39 +00:00
|
|
|
|
|
|
|
CustomPayload* = object
|
2023-08-27 01:23:45 +00:00
|
|
|
parentHash* : Option[common.Hash256]
|
2022-04-11 10:00:39 +00:00
|
|
|
feeRecipient* : Option[EthAddress]
|
2023-08-27 01:23:45 +00:00
|
|
|
stateRoot* : Option[common.Hash256]
|
|
|
|
receiptsRoot* : Option[common.Hash256]
|
2022-04-11 10:00:39 +00:00
|
|
|
logsBloom* : Option[BloomFilter]
|
2023-08-27 01:23:45 +00:00
|
|
|
prevRandao* : Option[common.Hash256]
|
2022-04-11 10:00:39 +00:00
|
|
|
number* : Option[uint64]
|
|
|
|
gasLimit* : Option[GasInt]
|
|
|
|
gasUsed* : Option[GasInt]
|
|
|
|
timestamp* : Option[EthTime]
|
2023-01-27 14:57:48 +00:00
|
|
|
extraData* : Option[common.Blob]
|
2022-04-11 10:00:39 +00:00
|
|
|
baseFeePerGas*: Option[UInt256]
|
2023-08-27 01:23:45 +00:00
|
|
|
blockHash* : Option[common.Hash256]
|
2022-04-11 10:00:39 +00:00
|
|
|
transactions* : Option[seq[Transaction]]
|
2023-08-21 02:08:54 +00:00
|
|
|
withdrawals* : Option[seq[Withdrawal]]
|
|
|
|
blobGasUsed* : Option[uint64]
|
|
|
|
excessBlobGas*: Option[uint64]
|
2023-08-27 01:23:45 +00:00
|
|
|
beaconRoot* : Option[common.Hash256]
|
2023-08-21 02:08:54 +00:00
|
|
|
removeWithdrawals*: bool
|
2022-04-11 10:00:39 +00:00
|
|
|
|
2022-06-13 09:42:01 +00:00
|
|
|
InvalidPayloadField* = enum
|
|
|
|
InvalidParentHash
|
|
|
|
InvalidStateRoot
|
|
|
|
InvalidReceiptsRoot
|
|
|
|
InvalidNumber
|
|
|
|
InvalidGasLimit
|
|
|
|
InvalidGasUsed
|
|
|
|
InvalidTimestamp
|
|
|
|
InvalidPrevRandao
|
|
|
|
RemoveTransaction
|
|
|
|
InvalidTransactionSignature
|
|
|
|
InvalidTransactionNonce
|
|
|
|
InvalidTransactionGas
|
|
|
|
InvalidTransactionGasPrice
|
|
|
|
InvalidTransactionValue
|
|
|
|
|
|
|
|
SignatureVal = object
|
|
|
|
V: int64
|
|
|
|
R: UInt256
|
|
|
|
S: UInt256
|
|
|
|
|
|
|
|
CustomTx = object
|
|
|
|
nonce : Option[AccountNonce]
|
|
|
|
gasPrice: Option[GasInt]
|
|
|
|
gasLimit: Option[GasInt]
|
|
|
|
to : Option[EthAddress]
|
|
|
|
value : Option[UInt256]
|
|
|
|
data : Option[seq[byte]]
|
|
|
|
sig : Option[SignatureVal]
|
|
|
|
|
2023-08-21 02:08:54 +00:00
|
|
|
proc customizePayload*(basePayload: ExecutableData, customData: CustomPayload): ExecutionPayload =
|
2022-04-11 10:00:39 +00:00
|
|
|
let txs = if customData.transactions.isSome:
|
|
|
|
customData.transactions.get
|
|
|
|
else:
|
|
|
|
basePayload.transactions
|
|
|
|
let txRoot = calcTxRoot(txs)
|
|
|
|
|
2023-08-21 02:08:54 +00:00
|
|
|
let wdRoot = if customData.withdrawals.isSome:
|
|
|
|
some(calcWithdrawalsRoot(customData.withdrawals.get))
|
|
|
|
elif basePayload.withdrawals.isSome:
|
|
|
|
some(calcWithdrawalsRoot(basePayload.withdrawals.get))
|
|
|
|
else:
|
2023-08-27 01:23:45 +00:00
|
|
|
none(common.Hash256)
|
2023-08-21 02:08:54 +00:00
|
|
|
|
2023-08-27 01:23:45 +00:00
|
|
|
var customHeader = common.BlockHeader(
|
2022-04-11 10:00:39 +00:00
|
|
|
parentHash: basePayload.parentHash,
|
|
|
|
ommersHash: EMPTY_UNCLE_HASH,
|
|
|
|
coinbase: basePayload.feeRecipient,
|
|
|
|
stateRoot: basePayload.stateRoot,
|
|
|
|
txRoot: txRoot,
|
|
|
|
receiptRoot: basePayload.receiptsRoot,
|
|
|
|
bloom: basePayload.logsBloom,
|
|
|
|
difficulty: 0.u256,
|
|
|
|
blockNumber: basePayload.number.toBlockNumber,
|
|
|
|
gasLimit: basePayload.gasLimit,
|
|
|
|
gasUsed: basePayload.gasUsed,
|
|
|
|
timestamp: basePayload.timestamp,
|
|
|
|
extraData: basePayload.extraData,
|
|
|
|
mixDigest: basePayload.prevRandao,
|
|
|
|
nonce: default(BlockNonce),
|
2023-08-21 02:08:54 +00:00
|
|
|
fee: some(basePayload.baseFeePerGas),
|
|
|
|
withdrawalsRoot: wdRoot,
|
|
|
|
blobGasUsed: basePayload.blobGasUsed,
|
|
|
|
excessBlobGas: basePayload.excessBlobGas,
|
2022-04-11 10:00:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# Overwrite custom information
|
|
|
|
if customData.parentHash.isSome:
|
|
|
|
customHeader.parentHash = customData.parentHash.get
|
|
|
|
|
|
|
|
if customData.feeRecipient.isSome:
|
|
|
|
customHeader.coinbase = customData.feeRecipient.get
|
|
|
|
|
|
|
|
if customData.stateRoot.isSome:
|
|
|
|
customHeader.stateRoot = customData.stateRoot.get
|
|
|
|
|
|
|
|
if customData.receiptsRoot.isSome:
|
|
|
|
customHeader.receiptRoot = customData.receiptsRoot.get
|
|
|
|
|
|
|
|
if customData.logsBloom.isSome:
|
|
|
|
customHeader.bloom = customData.logsBloom.get
|
|
|
|
|
|
|
|
if customData.prevRandao.isSome:
|
|
|
|
customHeader.mixDigest = customData.prevRandao.get
|
|
|
|
|
|
|
|
if customData.number.isSome:
|
|
|
|
customHeader.blockNumber = toBlockNumber(customData.number.get)
|
|
|
|
|
|
|
|
if customData.gasLimit.isSome:
|
|
|
|
customHeader.gasLimit = customData.gasLimit.get
|
|
|
|
|
|
|
|
if customData.gasUsed.isSome:
|
|
|
|
customHeader.gasUsed = customData.gasUsed.get
|
|
|
|
|
|
|
|
if customData.timestamp.isSome:
|
|
|
|
customHeader.timestamp = customData.timestamp.get
|
|
|
|
|
|
|
|
if customData.extraData.isSome:
|
|
|
|
customHeader.extraData = customData.extraData.get
|
|
|
|
|
|
|
|
if customData.baseFeePerGas.isSome:
|
|
|
|
customHeader.baseFee = customData.baseFeePerGas.get
|
|
|
|
|
2023-08-21 02:08:54 +00:00
|
|
|
if customData.blobGasUsed.isSome:
|
|
|
|
customHeader.blobGasUsed = customData.blobGasUsed
|
|
|
|
|
|
|
|
if customData.excessBlobGas.isSome:
|
|
|
|
customHeader.excessBlobGas = customData.excessBlobGas
|
|
|
|
|
|
|
|
if customData.beaconRoot.isSome:
|
|
|
|
customHeader.parentBeaconBlockRoot = customData.beaconRoot
|
|
|
|
|
2022-04-11 10:00:39 +00:00
|
|
|
# Return the new payload
|
2023-08-21 02:08:54 +00:00
|
|
|
result = ExecutionPayload(
|
|
|
|
parentHash: w3Hash customHeader.parentHash,
|
2023-08-27 01:23:45 +00:00
|
|
|
feeRecipient: w3Addr customHeader.coinbase,
|
2023-08-21 02:08:54 +00:00
|
|
|
stateRoot: w3Hash customHeader.stateRoot,
|
|
|
|
receiptsRoot: w3Hash customHeader.receiptRoot,
|
2023-08-27 01:23:45 +00:00
|
|
|
logsBloom: w3Bloom customHeader.bloom,
|
|
|
|
prevRandao: w3PrevRandao customHeader.mixDigest,
|
|
|
|
blockNumber: w3Qty customHeader.blockNumber,
|
|
|
|
gasLimit: w3Qty customHeader.gasLimit,
|
|
|
|
gasUsed: w3Qty customHeader.gasUsed,
|
|
|
|
timestamp: w3Qty customHeader.timestamp,
|
|
|
|
extraData: w3ExtraData customHeader.extraData,
|
2022-04-11 10:00:39 +00:00
|
|
|
baseFeePerGas: customHeader.baseFee,
|
2023-08-21 02:08:54 +00:00
|
|
|
blockHash: w3Hash customHeader.blockHash,
|
|
|
|
blobGasUsed: w3Qty customHeader.blobGasUsed,
|
|
|
|
excessBlobGas: w3Qty customHeader.excessBlobGas,
|
2022-04-11 10:00:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
for tx in txs:
|
|
|
|
let txData = rlp.encode(tx)
|
|
|
|
result.transactions.add TypedTransaction(txData)
|
|
|
|
|
2023-08-21 02:08:54 +00:00
|
|
|
let wds = if customData.withdrawals.isSome:
|
|
|
|
customData.withdrawals
|
|
|
|
elif basePayload.withdrawals.isSome:
|
|
|
|
basePayload.withdrawals
|
|
|
|
else:
|
|
|
|
none(seq[Withdrawal])
|
|
|
|
|
|
|
|
if wds.isSome and customData.removeWithdrawals.not:
|
|
|
|
result.withdrawals = some(w3Withdrawals(wds.get))
|
2022-04-11 10:00:39 +00:00
|
|
|
|
2023-08-21 02:08:54 +00:00
|
|
|
proc toExecutableData*(payload: ExecutionPayload): ExecutableData =
|
2022-04-11 10:00:39 +00:00
|
|
|
result = ExecutableData(
|
2023-08-27 01:23:45 +00:00
|
|
|
parentHash : ethHash payload.parentHash,
|
2022-04-11 10:00:39 +00:00
|
|
|
feeRecipient : distinctBase payload.feeRecipient,
|
2023-08-27 01:23:45 +00:00
|
|
|
stateRoot : ethHash payload.stateRoot,
|
|
|
|
receiptsRoot : ethHash payload.receiptsRoot,
|
2022-04-11 10:00:39 +00:00
|
|
|
logsBloom : distinctBase payload.logsBloom,
|
2023-08-27 01:23:45 +00:00
|
|
|
prevRandao : ethHash payload.prevRandao,
|
2022-04-11 10:00:39 +00:00
|
|
|
number : uint64 payload.blockNumber,
|
|
|
|
gasLimit : GasInt payload.gasLimit,
|
|
|
|
gasUsed : GasInt payload.gasUsed,
|
2023-08-27 01:23:45 +00:00
|
|
|
timestamp : ethTime payload.timestamp,
|
2022-04-11 10:00:39 +00:00
|
|
|
extraData : distinctBase payload.extraData,
|
|
|
|
baseFeePerGas : payload.baseFeePerGas,
|
2023-08-27 01:23:45 +00:00
|
|
|
blockHash : ethHash payload.blockHash,
|
2023-08-21 02:08:54 +00:00
|
|
|
blobGasUsed : u64 payload.blobGasUsed,
|
|
|
|
excessBlobGas : u64 payload.excessBlobGas,
|
2023-08-27 01:23:45 +00:00
|
|
|
transactions : ethTxs payload.transactions,
|
|
|
|
withdrawals : ethWithdrawals payload.withdrawals,
|
2022-04-11 10:00:39 +00:00
|
|
|
)
|
|
|
|
|
2023-08-21 02:08:54 +00:00
|
|
|
proc customizePayload*(basePayload: ExecutionPayload, customData: CustomPayload): ExecutionPayload =
|
2022-06-27 13:18:54 +00:00
|
|
|
customizePayload(basePayload.toExecutableData, customData)
|
|
|
|
|
2022-06-13 09:42:01 +00:00
|
|
|
proc customizeTx(baseTx: Transaction, vaultKey: PrivateKey, customTx: CustomTx): Transaction =
|
|
|
|
# Create a modified transaction base, from the base transaction and customData mix
|
|
|
|
var modTx = Transaction(
|
|
|
|
txType : TxLegacy,
|
|
|
|
nonce : baseTx.nonce,
|
|
|
|
gasPrice: baseTx.gasPrice,
|
|
|
|
gasLimit: baseTx.gasLimit,
|
|
|
|
to : baseTx.to,
|
|
|
|
value : baseTx.value,
|
|
|
|
payload : baseTx.payload
|
|
|
|
)
|
|
|
|
|
|
|
|
if customTx.nonce.isSome:
|
|
|
|
modTx.nonce = customTx.nonce.get
|
|
|
|
|
|
|
|
if customTx.gasPrice.isSome:
|
|
|
|
modTx.gasPrice = customTx.gasPrice.get
|
|
|
|
|
|
|
|
if customTx.gasLimit.isSome:
|
|
|
|
modTx.gasLimit = customTx.gasLimit.get
|
|
|
|
|
|
|
|
if customTx.to.isSome:
|
|
|
|
modTx.to = customTx.to
|
|
|
|
|
|
|
|
if customTx.value.isSome:
|
|
|
|
modTx.value = customTx.value.get
|
|
|
|
|
|
|
|
if customTx.data.isSome:
|
|
|
|
modTx.payload = customTx.data.get
|
|
|
|
|
|
|
|
if customTx.sig.isSome:
|
|
|
|
let sig = customTx.sig.get
|
|
|
|
modTx.V = sig.V
|
|
|
|
modTx.R = sig.R
|
|
|
|
modTx.S = sig.S
|
|
|
|
modTx
|
|
|
|
else:
|
|
|
|
# If a custom signature was not specified, simply sign the transaction again
|
|
|
|
let chainId = baseTx.chainId
|
|
|
|
signTransaction(modTx, vaultKey, chainId, eip155 = true)
|
|
|
|
|
2023-08-27 01:23:45 +00:00
|
|
|
proc modifyHash(x: common.Hash256): common.Hash256 =
|
2022-06-13 09:42:01 +00:00
|
|
|
result = x
|
|
|
|
result.data[^1] = byte(255 - x.data[^1].int)
|
|
|
|
|
2022-06-17 00:53:33 +00:00
|
|
|
proc generateInvalidPayload*(basePayload: ExecutableData,
|
|
|
|
payloadField: InvalidPayloadField,
|
2023-08-21 02:08:54 +00:00
|
|
|
vaultKey: PrivateKey): ExecutionPayload =
|
2022-06-17 00:53:33 +00:00
|
|
|
|
2022-06-13 09:42:01 +00:00
|
|
|
var customPayload: CustomPayload
|
|
|
|
|
|
|
|
case payloadField
|
|
|
|
of InvalidParentHash:
|
|
|
|
customPayload.parentHash = some(modifyHash(basePayload.parentHash))
|
|
|
|
of InvalidStateRoot:
|
|
|
|
customPayload.stateRoot = some(modifyHash(basePayload.stateRoot))
|
|
|
|
of InvalidReceiptsRoot:
|
|
|
|
customPayload.receiptsRoot = some(modifyHash(basePayload.receiptsRoot))
|
|
|
|
of InvalidNumber:
|
|
|
|
customPayload.number = some(basePayload.number - 1'u64)
|
|
|
|
of InvalidGasLimit:
|
|
|
|
customPayload.gasLimit = some(basePayload.gasLimit * 2)
|
|
|
|
of InvalidGasUsed:
|
|
|
|
customPayload.gasUsed = some(basePayload.gasUsed - 1)
|
|
|
|
of InvalidTimestamp:
|
|
|
|
customPayload.timestamp = some(basePayload.timestamp - 1.seconds)
|
|
|
|
of InvalidPrevRandao:
|
|
|
|
# This option potentially requires a transaction that uses the PREVRANDAO opcode.
|
|
|
|
# Otherwise the payload will still be valid.
|
2023-08-27 01:23:45 +00:00
|
|
|
var randomHash: common.Hash256
|
2022-09-03 18:15:35 +00:00
|
|
|
doAssert randomBytes(randomHash.data) == 32
|
2022-06-13 09:42:01 +00:00
|
|
|
customPayload.prevRandao = some(randomHash)
|
|
|
|
of RemoveTransaction:
|
|
|
|
let emptyTxs: seq[Transaction] = @[]
|
|
|
|
customPayload.transactions = some(emptyTxs)
|
|
|
|
of InvalidTransactionSignature,
|
|
|
|
InvalidTransactionNonce,
|
|
|
|
InvalidTransactionGas,
|
|
|
|
InvalidTransactionGasPrice,
|
|
|
|
InvalidTransactionValue:
|
|
|
|
|
|
|
|
doAssert(basePayload.transactions.len != 0, "No transactions available for modification")
|
|
|
|
|
|
|
|
var baseTx = basePayload.transactions[0]
|
|
|
|
var customTx: CustomTx
|
|
|
|
case payloadField
|
|
|
|
of InvalidTransactionSignature:
|
|
|
|
let sig = SignatureVal(
|
|
|
|
V: baseTx.V,
|
|
|
|
R: baseTx.R - 1.u256,
|
|
|
|
S: baseTx.S
|
|
|
|
)
|
|
|
|
customTx.sig = some(sig)
|
|
|
|
of InvalidTransactionNonce:
|
|
|
|
customTx.nonce = some(baseTx.nonce - 1)
|
|
|
|
of InvalidTransactionGas:
|
|
|
|
customTx.gasLimit = some(0.GasInt)
|
|
|
|
of InvalidTransactionGasPrice:
|
|
|
|
customTx.gasPrice = some(0.GasInt)
|
|
|
|
of InvalidTransactionValue:
|
|
|
|
# Vault account initially has 0x123450000000000000000, so this value should overflow
|
|
|
|
customTx.value = some(UInt256.fromHex("0x123450000000000000001"))
|
|
|
|
else:
|
|
|
|
discard
|
|
|
|
|
|
|
|
let modTx = customizeTx(baseTx, vaultKey, customTx)
|
|
|
|
customPayload.transactions = some(@[modTx])
|
|
|
|
|
|
|
|
customizePayload(basePayload, customPayload)
|
2022-06-17 00:53:33 +00:00
|
|
|
|
2023-08-21 02:08:54 +00:00
|
|
|
proc generateInvalidPayload*(basePayload: ExecutionPayload,
|
2022-06-17 00:53:33 +00:00
|
|
|
payloadField: InvalidPayloadField,
|
2023-08-21 02:08:54 +00:00
|
|
|
vaultKey = default(PrivateKey)): ExecutionPayload =
|
2022-06-17 00:53:33 +00:00
|
|
|
generateInvalidPayload(basePayload.toExecutableData, payloadField, vaultKey)
|
2022-06-27 05:26:03 +00:00
|
|
|
|
2023-08-27 01:23:45 +00:00
|
|
|
proc txInPayload*(payload: ExecutionPayload, txHash: common.Hash256): bool =
|
2022-06-27 05:26:03 +00:00
|
|
|
for txBytes in payload.transactions:
|
2023-01-27 14:57:48 +00:00
|
|
|
let currTx = rlp.decode(common.Blob txBytes, Transaction)
|
2022-06-27 05:26:03 +00:00
|
|
|
if rlpHash(currTx) == txHash:
|
|
|
|
return true
|