2023-11-01 03:32:09 +00:00
|
|
|
# Nimbus
|
2024-01-13 01:41:57 +00:00
|
|
|
# Copyright (c) 2023-2024 Status Research & Development GmbH
|
2023-11-01 03:32:09 +00:00
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0)
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or
|
|
|
|
# http://opensource.org/licenses/MIT)
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except
|
|
|
|
# according to those terms.
|
|
|
|
|
2023-08-21 02:08:54 +00:00
|
|
|
import
|
2023-10-18 02:16:11 +00:00
|
|
|
std/[options],
|
2023-08-21 02:08:54 +00:00
|
|
|
stint,
|
|
|
|
chronicles,
|
|
|
|
chronos,
|
|
|
|
stew/byteutils,
|
2023-12-08 09:35:50 +00:00
|
|
|
web3/eth_api_types,
|
2023-08-21 02:08:54 +00:00
|
|
|
./wd_history,
|
|
|
|
../test_env,
|
|
|
|
../engine_client,
|
|
|
|
../types,
|
2023-10-19 03:28:52 +00:00
|
|
|
../base_spec,
|
2023-10-31 03:18:37 +00:00
|
|
|
../cancun/customizer,
|
2024-02-15 02:57:05 +00:00
|
|
|
../../../../nimbus/common/common,
|
|
|
|
../../../../nimbus/utils/utils,
|
|
|
|
../../../../nimbus/common/chain_config,
|
2023-12-08 09:35:50 +00:00
|
|
|
web3/execution_types,
|
2024-02-15 02:57:05 +00:00
|
|
|
../../../../nimbus/beacon/web3_eth_conv
|
2023-08-21 02:08:54 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
WDBaseSpec* = ref object of BaseSpec
|
|
|
|
wdBlockCount*: int # Number of blocks on and after withdrawals fork activation
|
|
|
|
wdPerBlock*: int # Number of withdrawals per block
|
|
|
|
wdAbleAccountCount*: int # Number of accounts to withdraw to (round-robin)
|
|
|
|
wdHistory*: WDHistory # Internal withdrawals history that keeps track of all withdrawals
|
|
|
|
wdAmounts*: seq[uint64] # Amounts of withdrawn wei on each withdrawal (round-robin)
|
|
|
|
txPerBlock*: Option[int] # Amount of test transactions to include in withdrawal blocks
|
|
|
|
testCorrupedHashPayloads*: bool # Send a valid payload with corrupted hash
|
|
|
|
skipBaseVerifications*: bool # For code reuse of the base spec procedure
|
|
|
|
|
|
|
|
WithdrawalsForBlock = object
|
2023-09-30 12:20:29 +00:00
|
|
|
wds*: seq[Withdrawal]
|
|
|
|
nextIndex*: int
|
2023-08-21 02:08:54 +00:00
|
|
|
|
|
|
|
const
|
|
|
|
WARM_COINBASE_ADDRESS = hexToByteArray[20]("0x0101010101010101010101010101010101010101")
|
|
|
|
PUSH0_ADDRESS = hexToByteArray[20]("0x0202020202020202020202020202020202020202")
|
|
|
|
MAINNET_MAX_WITHDRAWAL_COUNT_PER_BLOCK* = 16
|
|
|
|
TX_CONTRACT_ADDRESSES = [
|
|
|
|
WARM_COINBASE_ADDRESS,
|
|
|
|
PUSH0_ADDRESS,
|
|
|
|
]
|
|
|
|
|
|
|
|
# Timestamp delta between genesis and the withdrawals fork
|
2023-09-30 12:20:29 +00:00
|
|
|
func getWithdrawalsGenesisTimeDelta*(ws: WDBaseSpec): int =
|
2023-10-19 03:28:52 +00:00
|
|
|
ws.forkHeight * ws.getBlockTimeIncrements()
|
2023-08-21 02:08:54 +00:00
|
|
|
|
|
|
|
# Get the start account for all withdrawals.
|
|
|
|
func getWithdrawalsStartAccount*(ws: WDBaseSpec): UInt256 =
|
|
|
|
0x1000.u256
|
|
|
|
|
|
|
|
# Adds bytecode that unconditionally sets an storage key to specified account range
|
|
|
|
func addUnconditionalBytecode(g: Genesis, start, stop: UInt256) =
|
|
|
|
var acc = start
|
|
|
|
while acc<stop:
|
|
|
|
let accountAddress = toAddress(acc)
|
|
|
|
# Bytecode to unconditionally set a storage key
|
|
|
|
g.alloc[accountAddress] = GenesisAccount(
|
|
|
|
code: @[
|
|
|
|
0x60.byte, # PUSH1(0x01)
|
|
|
|
0x01.byte,
|
|
|
|
0x60.byte, # PUSH1(0x00)
|
|
|
|
0x00.byte,
|
|
|
|
0x55.byte, # SSTORE
|
|
|
|
0x00.byte, # STOP
|
|
|
|
], # sstore(0, 1)
|
|
|
|
nonce: 0.AccountNonce,
|
|
|
|
balance: 0.u256,
|
|
|
|
)
|
|
|
|
acc = acc + 1
|
|
|
|
|
2023-09-30 12:20:29 +00:00
|
|
|
func getWithdrawableAccountCount*(ws: WDBaseSpec):int =
|
2023-08-21 02:08:54 +00:00
|
|
|
if ws.wdAbleAccountCount == 0:
|
|
|
|
# Withdraw to MAINNET_MAX_WITHDRAWAL_COUNT_PER_BLOCK accounts by default
|
|
|
|
return MAINNET_MAX_WITHDRAWAL_COUNT_PER_BLOCK
|
|
|
|
return ws.wdAbleAccountCount
|
|
|
|
|
|
|
|
# Append the accounts we are going to withdraw to, which should also include
|
|
|
|
# bytecode for testing purposes.
|
2023-10-31 03:18:37 +00:00
|
|
|
func getGenesis*(ws: WDBaseSpec, param: NetworkParams) =
|
2023-08-21 02:08:54 +00:00
|
|
|
# Remove PoW altogether
|
|
|
|
param.genesis.difficulty = 0.u256
|
|
|
|
param.config.terminalTotalDifficulty = some(0.u256)
|
|
|
|
param.config.clique = CliqueOptions()
|
|
|
|
param.genesis.extraData = @[]
|
|
|
|
|
|
|
|
# Add some accounts to withdraw to with unconditional SSTOREs
|
|
|
|
let
|
|
|
|
startAccount = 0x1000.u256
|
|
|
|
endAccount = (0x1000 + ws.getWithdrawableAccountCount()).u256
|
|
|
|
addUnconditionalBytecode(param.genesis, startAccount, endAccount)
|
|
|
|
|
|
|
|
# Add accounts that use the coinbase (EIP-3651)
|
|
|
|
let warmCoinbaseCode = [
|
|
|
|
0x5A.byte, # GAS
|
|
|
|
0x60.byte, # PUSH1(0x00)
|
|
|
|
0x00.byte,
|
|
|
|
0x60.byte, # PUSH1(0x00)
|
|
|
|
0x00.byte,
|
|
|
|
0x60.byte, # PUSH1(0x00)
|
|
|
|
0x00.byte,
|
|
|
|
0x60.byte, # PUSH1(0x00)
|
|
|
|
0x00.byte,
|
|
|
|
0x60.byte, # PUSH1(0x00)
|
|
|
|
0x00.byte,
|
|
|
|
0x41.byte, # COINBASE
|
|
|
|
0x60.byte, # PUSH1(0xFF)
|
|
|
|
0xFF.byte,
|
|
|
|
0xF1.byte, # CALL
|
|
|
|
0x5A.byte, # GAS
|
|
|
|
0x90.byte, # SWAP1
|
|
|
|
0x50.byte, # POP - Call result
|
|
|
|
0x90.byte, # SWAP1
|
|
|
|
0x03.byte, # SUB
|
|
|
|
0x60.byte, # PUSH1(0x16) - GAS + PUSH * 6 + COINBASE
|
|
|
|
0x16.byte,
|
|
|
|
0x90.byte, # SWAP1
|
|
|
|
0x03.byte, # SUB
|
|
|
|
0x43.byte, # NUMBER
|
|
|
|
0x55.byte, # SSTORE
|
|
|
|
]
|
|
|
|
|
|
|
|
param.genesis.alloc[WARM_COINBASE_ADDRESS] = GenesisAccount(
|
|
|
|
code: @warmCoinbaseCode,
|
|
|
|
balance: 0.u256,
|
|
|
|
)
|
|
|
|
|
|
|
|
# Add accounts that use the PUSH0 (EIP-3855)
|
|
|
|
let push0Code = [
|
|
|
|
0x43.byte, # NUMBER
|
|
|
|
0x5F.byte, # PUSH0
|
|
|
|
0x55.byte, # SSTORE
|
|
|
|
]
|
|
|
|
|
|
|
|
param.genesis.alloc[PUSH0_ADDRESS] = GenesisAccount(
|
|
|
|
code: @push0Code,
|
|
|
|
balance: 0.u256,
|
|
|
|
)
|
|
|
|
|
2023-09-30 12:20:29 +00:00
|
|
|
func getTransactionCountPerPayload*(ws: WDBaseSpec): int =
|
2023-08-21 02:08:54 +00:00
|
|
|
ws.txPerBlock.get(16)
|
|
|
|
|
2023-09-06 09:18:26 +00:00
|
|
|
proc verifyContractsStorage(ws: WDBaseSpec, env: TestEnv): Result[void, string] =
|
2023-08-21 02:08:54 +00:00
|
|
|
if ws.getTransactionCountPerPayload() < TX_CONTRACT_ADDRESSES.len:
|
|
|
|
return
|
|
|
|
|
|
|
|
# Assume that forkchoice updated has been already sent
|
|
|
|
let
|
2023-09-06 09:18:26 +00:00
|
|
|
latestPayloadNumber = env.clMock.latestExecutedPayload.blockNumber.uint64.u256
|
|
|
|
r = env.client.storageAt(WARM_COINBASE_ADDRESS, latestPayloadNumber, latestPayloadNumber)
|
|
|
|
p = env.client.storageAt(PUSH0_ADDRESS, 0.u256, latestPayloadNumber)
|
2023-08-21 02:08:54 +00:00
|
|
|
|
2023-10-19 03:28:52 +00:00
|
|
|
if latestPayloadNumber.truncate(int) >= ws.forkHeight:
|
2023-08-21 02:08:54 +00:00
|
|
|
# Shanghai
|
2024-01-13 01:41:57 +00:00
|
|
|
r.expectStorageEqual(WARM_COINBASE_ADDRESS, 100.u256.w3FixedBytes) # WARM_STORAGE_READ_COST
|
|
|
|
p.expectStorageEqual(PUSH0_ADDRESS, latestPayloadNumber.w3FixedBytes) # tx succeeded
|
2023-08-21 02:08:54 +00:00
|
|
|
else:
|
|
|
|
# Pre-Shanghai
|
2024-01-13 01:41:57 +00:00
|
|
|
r.expectStorageEqual(WARM_COINBASE_ADDRESS, 2600.u256.w3FixedBytes) # COLD_ACCOUNT_ACCESS_COST
|
|
|
|
p.expectStorageEqual(PUSH0_ADDRESS, 0.u256.w3FixedBytes) # tx must've failed
|
2023-08-21 02:08:54 +00:00
|
|
|
|
|
|
|
ok()
|
|
|
|
|
|
|
|
# Number of blocks to be produced (not counting genesis) before withdrawals
|
|
|
|
# fork.
|
|
|
|
func getPreWithdrawalsBlockCount*(ws: WDBaseSpec): int =
|
2023-10-19 03:28:52 +00:00
|
|
|
if ws.forkHeight == 0:
|
2023-08-21 02:08:54 +00:00
|
|
|
0
|
|
|
|
else:
|
2023-10-19 03:28:52 +00:00
|
|
|
ws.forkHeight - 1
|
2023-08-21 02:08:54 +00:00
|
|
|
|
|
|
|
# Number of payloads to be produced (pre and post withdrawals) during the entire test
|
2023-09-30 12:20:29 +00:00
|
|
|
func getTotalPayloadCount*(ws: WDBaseSpec): int =
|
2023-08-21 02:08:54 +00:00
|
|
|
ws.getPreWithdrawalsBlockCount() + ws.wdBlockCount
|
|
|
|
|
|
|
|
# Generates a list of withdrawals based on current configuration
|
2023-09-30 12:20:29 +00:00
|
|
|
func generateWithdrawalsForBlock*(ws: WDBaseSpec, nextIndex: int, startAccount: UInt256): WithdrawalsForBlock =
|
2023-08-21 02:08:54 +00:00
|
|
|
let
|
|
|
|
differentAccounts = ws.getWithdrawableAccountCount()
|
|
|
|
|
|
|
|
var wdAmounts = ws.wdAmounts
|
|
|
|
if wdAmounts.len == 0:
|
|
|
|
wdAmounts.add(1)
|
|
|
|
|
|
|
|
for i in 0 ..< ws.wdPerBlock:
|
|
|
|
let
|
|
|
|
nextAccount = startAccount + (nextIndex mod differentAccounts).u256
|
|
|
|
nextWithdrawal = Withdrawal(
|
|
|
|
index: nextIndex.uint64,
|
|
|
|
validatorIndex: nextIndex.uint64,
|
|
|
|
address: nextAccount.toAddress,
|
|
|
|
amount: wdAmounts[nextIndex mod wdAmounts.len]
|
|
|
|
)
|
|
|
|
|
|
|
|
result.wds.add nextWithdrawal
|
|
|
|
inc result.nextIndex
|
|
|
|
|
|
|
|
# Base test case execution procedure for withdrawals
|
2023-09-06 09:18:26 +00:00
|
|
|
proc execute*(ws: WDBaseSpec, env: TestEnv): bool =
|
2023-08-21 02:08:54 +00:00
|
|
|
result = true
|
|
|
|
|
2023-09-06 09:18:26 +00:00
|
|
|
let ok = waitFor env.clMock.waitForTTD()
|
2023-08-21 02:08:54 +00:00
|
|
|
testCond ok
|
|
|
|
|
|
|
|
# Check if we have pre-Shanghai blocks
|
2023-10-19 03:28:52 +00:00
|
|
|
if ws.getForkTime() > GenesisTimestamp:
|
2023-08-21 02:08:54 +00:00
|
|
|
# Check `latest` during all pre-shanghai blocks, none should
|
|
|
|
# contain `withdrawalsRoot`, including genesis.
|
|
|
|
|
|
|
|
# Genesis should not contain `withdrawalsRoot` either
|
2023-10-31 03:18:37 +00:00
|
|
|
let r = env.client.latestHeader()
|
|
|
|
r.expectWithdrawalsRoot(none(common.Hash256))
|
2023-08-21 02:08:54 +00:00
|
|
|
else:
|
|
|
|
# Genesis is post shanghai, it should contain EmptyWithdrawalsRoot
|
2023-10-31 03:18:37 +00:00
|
|
|
let r = env.client.latestHeader()
|
|
|
|
r.expectWithdrawalsRoot(some(EMPTY_ROOT_HASH))
|
2023-08-21 02:08:54 +00:00
|
|
|
|
|
|
|
# Produce any blocks necessary to reach withdrawals fork
|
2023-09-06 09:18:26 +00:00
|
|
|
var pbRes = env.clMock.produceBlocks(ws.getPreWithdrawalsBlockCount, BlockProcessCallbacks(
|
2023-08-21 02:08:54 +00:00
|
|
|
onPayloadProducerSelected: proc(): bool =
|
|
|
|
# Send some transactions
|
|
|
|
let numTx = ws.getTransactionCountPerPayload()
|
|
|
|
for i in 0..<numTx:
|
|
|
|
let destAddr = TX_CONTRACT_ADDRESSES[i mod TX_CONTRACT_ADDRESSES.len]
|
2023-09-06 09:18:26 +00:00
|
|
|
let ok = env.sendNextTx(
|
|
|
|
env.clMock.nextBlockProducer,
|
|
|
|
BaseTx(
|
2023-08-21 02:08:54 +00:00
|
|
|
recipient: some(destAddr),
|
|
|
|
amount: 1.u256,
|
|
|
|
txType: ws.txType,
|
|
|
|
gasLimit: 75000.GasInt,
|
2023-09-06 09:18:26 +00:00
|
|
|
))
|
2023-08-21 02:08:54 +00:00
|
|
|
|
|
|
|
testCond ok:
|
|
|
|
error "Error trying to send transaction"
|
|
|
|
|
|
|
|
if not ws.skipBaseVerifications:
|
|
|
|
# Try to send a ForkchoiceUpdatedV2 with non-null
|
|
|
|
# withdrawals before Shanghai
|
2023-09-06 09:18:26 +00:00
|
|
|
var r = env.client.forkchoiceUpdatedV2(
|
2023-08-21 02:08:54 +00:00
|
|
|
ForkchoiceStateV1(
|
2023-09-06 09:18:26 +00:00
|
|
|
headBlockHash: w3Hash env.clMock.latestHeader,
|
2023-08-21 02:08:54 +00:00
|
|
|
),
|
|
|
|
some(PayloadAttributes(
|
2023-09-06 09:18:26 +00:00
|
|
|
timestamp: w3Qty(env.clMock.latestHeader.timestamp, ws.getBlockTimeIncrements()),
|
2023-08-21 02:08:54 +00:00
|
|
|
prevRandao: w3PrevRandao(),
|
|
|
|
suggestedFeeRecipient: w3Address(),
|
|
|
|
withdrawals: some(newSeq[WithdrawalV1]()),
|
|
|
|
))
|
|
|
|
)
|
2023-10-23 13:59:57 +00:00
|
|
|
let expectationDescription = "Sent pre-shanghai Forkchoice using ForkchoiceUpdatedV2 + Withdrawals, error is expected"
|
|
|
|
r.expectErrorCode(engineApiInvalidParams, expectationDescription)
|
2023-08-21 02:08:54 +00:00
|
|
|
|
|
|
|
# Send a valid Pre-Shanghai request using ForkchoiceUpdatedV2
|
|
|
|
# (clMock uses V1 by default)
|
2023-09-06 09:18:26 +00:00
|
|
|
r = env.client.forkchoiceUpdatedV2(
|
2023-08-21 02:08:54 +00:00
|
|
|
ForkchoiceStateV1(
|
2023-09-06 09:18:26 +00:00
|
|
|
headBlockHash: w3Hash env.clMock.latestHeader,
|
2023-08-21 02:08:54 +00:00
|
|
|
),
|
|
|
|
some(PayloadAttributes(
|
2023-09-06 09:18:26 +00:00
|
|
|
timestamp: w3Qty(env.clMock.latestHeader.timestamp, ws.getBlockTimeIncrements()),
|
2023-08-21 02:08:54 +00:00
|
|
|
prevRandao: w3PrevRandao(),
|
|
|
|
suggestedFeeRecipient: w3Address(),
|
|
|
|
withdrawals: none(seq[WithdrawalV1]),
|
|
|
|
))
|
|
|
|
)
|
2023-10-23 13:59:57 +00:00
|
|
|
let expectationDescription2 = "Sent pre-shanghai Forkchoice ForkchoiceUpdatedV2 + null withdrawals, no error is expected"
|
|
|
|
r.expectNoError(expectationDescription2)
|
2023-08-21 02:08:54 +00:00
|
|
|
|
|
|
|
return true
|
|
|
|
,
|
|
|
|
onGetPayload: proc(): bool =
|
|
|
|
if not ws.skipBaseVerifications:
|
|
|
|
# Try to get the same payload but use `engine_getPayloadV2`
|
|
|
|
|
2023-09-06 09:18:26 +00:00
|
|
|
let g = env.client.getPayloadV2(env.clMock.nextPayloadID)
|
|
|
|
g.expectPayload(env.clMock.latestPayloadBuilt)
|
2023-08-21 02:08:54 +00:00
|
|
|
|
|
|
|
# Send produced payload but try to include non-nil
|
|
|
|
# `withdrawals`, it should fail.
|
|
|
|
let emptyWithdrawalsList = newSeq[Withdrawal]()
|
2023-10-31 03:18:37 +00:00
|
|
|
let customizer = CustomPayloadData(
|
2023-08-21 02:08:54 +00:00
|
|
|
withdrawals: some(emptyWithdrawalsList),
|
2023-10-31 03:18:37 +00:00
|
|
|
parentBeaconRoot: ethHash env.clMock.latestPayloadAttributes.parentBeaconBlockRoot
|
2023-08-21 02:08:54 +00:00
|
|
|
)
|
2023-10-31 03:18:37 +00:00
|
|
|
let payloadPlusWithdrawals = customizer.customizePayload(env.clMock.latestExecutableData).basePayload
|
2023-09-06 09:18:26 +00:00
|
|
|
var r = env.client.newPayloadV2(payloadPlusWithdrawals.V1V2)
|
2023-08-21 02:08:54 +00:00
|
|
|
#r.ExpectationDescription = "Sent pre-shanghai payload using NewPayloadV2+Withdrawals, error is expected"
|
|
|
|
r.expectErrorCode(engineApiInvalidParams)
|
|
|
|
|
|
|
|
# Send valid ExecutionPayloadV1 using engine_newPayloadV2
|
2023-09-06 09:18:26 +00:00
|
|
|
r = env.client.newPayloadV2(env.clMock.latestPayloadBuilt.V1V2)
|
2023-08-21 02:08:54 +00:00
|
|
|
#r.ExpectationDescription = "Sent pre-shanghai payload using NewPayloadV2, no error is expected"
|
2023-10-31 03:18:37 +00:00
|
|
|
r.expectStatus(PayloadExecutionStatus.valid)
|
2023-08-21 02:08:54 +00:00
|
|
|
return true
|
|
|
|
,
|
|
|
|
onNewPayloadBroadcast: proc(): bool =
|
|
|
|
if not ws.skipBaseVerifications:
|
|
|
|
# We sent a pre-shanghai FCU.
|
|
|
|
# Keep expecting `nil` until Shanghai.
|
2023-10-31 03:18:37 +00:00
|
|
|
let r = env.client.latestHeader()
|
2023-08-21 02:08:54 +00:00
|
|
|
#r.ExpectationDescription = "Requested "latest" block expecting block to contain
|
|
|
|
#" withdrawalRoot=nil, because (block %d).timestamp < shanghaiTime
|
2023-10-31 03:18:37 +00:00
|
|
|
r.expectWithdrawalsRoot(none(common.Hash256))
|
2023-08-21 02:08:54 +00:00
|
|
|
return true
|
|
|
|
,
|
|
|
|
onForkchoiceBroadcast: proc(): bool =
|
|
|
|
if not ws.skipBaseVerifications:
|
2023-09-06 09:18:26 +00:00
|
|
|
let r = ws.verifyContractsStorage(env)
|
2023-08-21 02:08:54 +00:00
|
|
|
testCond r.isOk:
|
|
|
|
error "verifyContractsStorage error", msg=r.error
|
|
|
|
return true
|
|
|
|
))
|
|
|
|
|
|
|
|
testCond pbRes
|
|
|
|
|
|
|
|
# Produce requested post-shanghai blocks
|
|
|
|
# (At least 1 block will be produced after this procedure ends).
|
|
|
|
var
|
|
|
|
startAccount = ws.getWithdrawalsStartAccount()
|
|
|
|
nextIndex = 0
|
|
|
|
|
2023-09-06 09:18:26 +00:00
|
|
|
pbRes = env.clMock.produceBlocks(ws.wdBlockCount, BlockProcessCallbacks(
|
2023-08-21 02:08:54 +00:00
|
|
|
onPayloadProducerSelected: proc(): bool =
|
|
|
|
if not ws.skipBaseVerifications:
|
|
|
|
# Try to send a PayloadAttributesV1 with null withdrawals after
|
|
|
|
# Shanghai
|
2023-09-06 09:18:26 +00:00
|
|
|
let r = env.client.forkchoiceUpdatedV2(
|
2023-08-21 02:08:54 +00:00
|
|
|
ForkchoiceStateV1(
|
2023-09-06 09:18:26 +00:00
|
|
|
headBlockHash: w3Hash env.clMock.latestHeader,
|
2023-08-21 02:08:54 +00:00
|
|
|
),
|
|
|
|
some(PayloadAttributes(
|
2023-09-06 09:18:26 +00:00
|
|
|
timestamp: w3Qty(env.clMock.latestHeader.timestamp, ws.getBlockTimeIncrements()),
|
2023-08-21 02:08:54 +00:00
|
|
|
prevRandao: w3PrevRandao(),
|
|
|
|
suggestedFeeRecipient: w3Address(),
|
|
|
|
withdrawals: none(seq[WithdrawalV1]),
|
|
|
|
))
|
|
|
|
)
|
2023-10-23 13:59:57 +00:00
|
|
|
let expectationDescription = "Sent shanghai fcu using PayloadAttributesV1, error is expected"
|
|
|
|
r.expectErrorCode(engineApiInvalidParams, expectationDescription)
|
2023-08-21 02:08:54 +00:00
|
|
|
|
|
|
|
# Send some withdrawals
|
|
|
|
let wfb = ws.generateWithdrawalsForBlock(nextIndex, startAccount)
|
2023-09-06 09:18:26 +00:00
|
|
|
env.clMock.nextWithdrawals = some(w3Withdrawals wfb.wds)
|
|
|
|
ws.wdHistory.put(env.clMock.currentPayloadNumber, wfb.wds)
|
2023-08-21 02:08:54 +00:00
|
|
|
|
|
|
|
# Send some transactions
|
|
|
|
let numTx = ws.getTransactionCountPerPayload()
|
|
|
|
for i in 0..<numTx:
|
|
|
|
let destAddr = TX_CONTRACT_ADDRESSES[i mod TX_CONTRACT_ADDRESSES.len]
|
|
|
|
|
2023-09-06 09:18:26 +00:00
|
|
|
let ok = env.sendNextTx(
|
|
|
|
env.clMock.nextBlockProducer,
|
|
|
|
BaseTx(
|
2023-08-21 02:08:54 +00:00
|
|
|
recipient: some(destAddr),
|
|
|
|
amount: 1.u256,
|
|
|
|
txType: ws.txType,
|
|
|
|
gasLimit: 75000.GasInt,
|
2023-09-06 09:18:26 +00:00
|
|
|
))
|
2023-08-21 02:08:54 +00:00
|
|
|
|
|
|
|
testCond ok:
|
|
|
|
error "Error trying to send transaction"
|
|
|
|
|
|
|
|
return true
|
|
|
|
,
|
|
|
|
onGetPayload: proc(): bool =
|
|
|
|
if not ws.skipBaseVerifications:
|
|
|
|
# Send invalid `ExecutionPayloadV1` by replacing withdrawals list
|
|
|
|
# with null, and client must respond with `InvalidParamsError`.
|
|
|
|
# Note that StateRoot is also incorrect but null withdrawals should
|
|
|
|
# be checked first instead of responding `INVALID`
|
2023-10-31 03:18:37 +00:00
|
|
|
let customizer = CustomPayloadData(
|
2023-08-21 02:08:54 +00:00
|
|
|
removeWithdrawals: true,
|
2023-10-31 03:18:37 +00:00
|
|
|
parentBeaconRoot: ethHash env.clMock.latestPayloadAttributes.parentBeaconBlockRoot
|
2023-08-21 02:08:54 +00:00
|
|
|
)
|
2023-10-31 03:18:37 +00:00
|
|
|
let nilWithdrawalsPayload = customizer.customizePayload(env.clMock.latestExecutableData).basePayload
|
2023-09-06 09:18:26 +00:00
|
|
|
let r = env.client.newPayloadV2(nilWithdrawalsPayload.V1V2)
|
2023-08-21 02:08:54 +00:00
|
|
|
#r.ExpectationDescription = "Sent shanghai payload using ExecutionPayloadV1, error is expected"
|
|
|
|
r.expectErrorCode(engineApiInvalidParams)
|
|
|
|
|
|
|
|
# Verify the list of withdrawals returned on the payload built
|
|
|
|
# completely matches the list provided in the
|
|
|
|
# engine_forkchoiceUpdatedV2 method call
|
2023-09-06 09:18:26 +00:00
|
|
|
let res = ws.wdHistory.get(env.clMock.currentPayloadNumber)
|
2023-08-21 02:08:54 +00:00
|
|
|
doAssert(res.isOk, "withdrawals sent list was not saved")
|
|
|
|
|
|
|
|
let sentList = res.get
|
2023-09-06 09:18:26 +00:00
|
|
|
let wdList = env.clMock.latestPayloadBuilt.withdrawals.get
|
2023-08-21 02:08:54 +00:00
|
|
|
testCond sentList.len == wdList.len:
|
|
|
|
error "Incorrect list of withdrawals on built payload",
|
|
|
|
want=sentList.len,
|
|
|
|
get=wdList.len
|
|
|
|
|
|
|
|
for i, x in sentList:
|
2023-08-27 01:23:45 +00:00
|
|
|
let z = ethWithdrawal wdList[i]
|
2023-08-21 02:08:54 +00:00
|
|
|
testCond z == x:
|
|
|
|
error "Incorrect withdrawal", index=i
|
|
|
|
return true
|
|
|
|
,
|
|
|
|
onNewPayloadBroadcast: proc(): bool =
|
|
|
|
# Check withdrawal addresses and verify withdrawal balances
|
|
|
|
# have not yet been applied
|
|
|
|
if not ws.skipBaseVerifications:
|
2023-09-06 09:18:26 +00:00
|
|
|
let addrList = ws.wdHistory.getAddressesWithdrawnOnBlock(env.clMock.latestExecutedPayload.blockNumber.uint64)
|
2023-08-21 02:08:54 +00:00
|
|
|
for address in addrList:
|
|
|
|
# Test balance at `latest`, which should not yet have the
|
|
|
|
# withdrawal applied.
|
|
|
|
let expectedAccountBalance = ws.wdHistory.getExpectedAccountBalance(
|
|
|
|
address,
|
2023-09-06 09:18:26 +00:00
|
|
|
env.clMock.latestExecutedPayload.blockNumber.uint64-1)
|
2023-08-21 02:08:54 +00:00
|
|
|
|
2023-09-06 09:18:26 +00:00
|
|
|
let r = env.client.balanceAt(address)
|
2023-08-21 02:08:54 +00:00
|
|
|
#r.ExpectationDescription = fmt.Sprintf(`
|
|
|
|
# Requested balance for account %s on "latest" block
|
|
|
|
# after engine_newPayloadV2, expecting balance to be equal
|
|
|
|
# to value on previous block (%d), since the new payload
|
|
|
|
# has not yet been applied.
|
|
|
|
# `,
|
|
|
|
# addr,
|
2023-09-06 09:18:26 +00:00
|
|
|
# env.clMock.LatestExecutedPayload.Number-1,
|
2023-08-21 02:08:54 +00:00
|
|
|
#)
|
|
|
|
r.expectBalanceEqual(expectedAccountBalance)
|
|
|
|
|
|
|
|
if ws.testCorrupedHashPayloads:
|
2023-09-06 09:18:26 +00:00
|
|
|
var payload = env.clMock.latestExecutedPayload
|
2023-08-21 02:08:54 +00:00
|
|
|
|
|
|
|
# Corrupt the hash
|
2023-10-31 03:18:37 +00:00
|
|
|
let randomHash = common.Hash256.randomBytes()
|
2023-08-21 02:08:54 +00:00
|
|
|
payload.blockHash = w3Hash randomHash
|
|
|
|
|
|
|
|
# On engine_newPayloadV2 `INVALID_BLOCK_HASH` is deprecated
|
|
|
|
# in favor of reusing `INVALID`
|
2023-09-06 09:18:26 +00:00
|
|
|
let n = env.client.newPayloadV2(payload.V1V2)
|
2023-10-31 03:18:37 +00:00
|
|
|
n.expectStatus(PayloadExecutionStatus.invalid)
|
2023-08-21 02:08:54 +00:00
|
|
|
return true
|
|
|
|
,
|
|
|
|
onForkchoiceBroadcast: proc(): bool =
|
|
|
|
# Check withdrawal addresses and verify withdrawal balances
|
|
|
|
# have been applied
|
|
|
|
if not ws.skipBaseVerifications:
|
2023-09-06 09:18:26 +00:00
|
|
|
let addrList = ws.wdHistory.getAddressesWithdrawnOnBlock(env.clMock.latestExecutedPayload.blockNumber.uint64)
|
2023-08-21 02:08:54 +00:00
|
|
|
for address in addrList:
|
|
|
|
# Test balance at `latest`, which should have the
|
|
|
|
# withdrawal applied.
|
2023-09-06 09:18:26 +00:00
|
|
|
let r = env.client.balanceAt(address)
|
2023-08-21 02:08:54 +00:00
|
|
|
#r.ExpectationDescription = fmt.Sprintf(`
|
|
|
|
# Requested balance for account %s on "latest" block
|
|
|
|
# after engine_forkchoiceUpdatedV2, expecting balance to
|
|
|
|
# be equal to value on latest payload (%d), since the new payload
|
|
|
|
# has not yet been applied.
|
|
|
|
# `,
|
|
|
|
# addr,
|
2023-09-06 09:18:26 +00:00
|
|
|
# env.clMock.LatestExecutedPayload.Number,
|
2023-08-21 02:08:54 +00:00
|
|
|
#)
|
|
|
|
let expectedAccountBalance = ws.wdHistory.getExpectedAccountBalance(
|
|
|
|
address,
|
2023-09-06 09:18:26 +00:00
|
|
|
env.clMock.latestExecutedPayload.blockNumber.uint64)
|
2023-08-21 02:08:54 +00:00
|
|
|
|
|
|
|
r.expectBalanceEqual(expectedAccountBalance)
|
|
|
|
|
2023-09-06 09:18:26 +00:00
|
|
|
let wds = ws.wdHistory.getWithdrawals(env.clMock.latestExecutedPayload.blockNumber.uint64)
|
2023-08-21 02:08:54 +00:00
|
|
|
let expectedWithdrawalsRoot = some(calcWithdrawalsRoot(wds.list))
|
|
|
|
|
|
|
|
# Check the correct withdrawal root on `latest` block
|
2023-10-31 03:18:37 +00:00
|
|
|
let r = env.client.latestHeader()
|
2023-08-21 02:08:54 +00:00
|
|
|
#r.ExpectationDescription = fmt.Sprintf(`
|
|
|
|
# Requested "latest" block after engine_forkchoiceUpdatedV2,
|
|
|
|
# to verify withdrawalsRoot with the following withdrawals:
|
|
|
|
# %s`, jsWithdrawals)
|
2023-10-31 03:18:37 +00:00
|
|
|
r.expectWithdrawalsRoot(expectedWithdrawalsRoot)
|
2023-08-21 02:08:54 +00:00
|
|
|
|
2023-09-06 09:18:26 +00:00
|
|
|
let res = ws.verifyContractsStorage(env)
|
2023-08-21 02:08:54 +00:00
|
|
|
testCond res.isOk:
|
|
|
|
error "verifyContractsStorage error", msg=res.error
|
|
|
|
return true
|
|
|
|
))
|
|
|
|
testCond pbRes
|
|
|
|
|
|
|
|
# Iterate over balance history of withdrawn accounts using RPC and
|
|
|
|
# check that the balances match expected values.
|
|
|
|
# Also check one block before the withdrawal took place, verify that
|
|
|
|
# withdrawal has not been updated.
|
|
|
|
if not ws.skipBaseVerifications:
|
2023-09-06 09:18:26 +00:00
|
|
|
let maxBlock = env.clMock.latestExecutedPayload.blockNumber.uint64
|
2023-08-21 02:08:54 +00:00
|
|
|
for bn in 0..maxBlock:
|
2023-09-06 09:18:26 +00:00
|
|
|
let res = ws.wdHistory.verifyWithdrawals(bn, some(bn.u256), env.client)
|
2023-08-21 02:08:54 +00:00
|
|
|
testCond res.isOk:
|
|
|
|
error "verify wd error", msg=res.error
|
|
|
|
|
|
|
|
# Check the correct withdrawal root on past blocks
|
2023-10-31 03:18:37 +00:00
|
|
|
let r = env.client.headerByNumber(bn)
|
2023-08-21 02:08:54 +00:00
|
|
|
var expectedWithdrawalsRoot: Option[common.Hash256]
|
2023-10-19 03:28:52 +00:00
|
|
|
if bn >= ws.forkHeight.uint64:
|
2023-08-21 02:08:54 +00:00
|
|
|
let wds = ws.wdHistory.getWithdrawals(bn)
|
|
|
|
expectedWithdrawalsRoot = some(calcWithdrawalsRoot(wds.list))
|
|
|
|
|
|
|
|
#r.ExpectationDescription = fmt.Sprintf(`
|
|
|
|
# Requested block %d to verify withdrawalsRoot with the
|
|
|
|
# following withdrawals:
|
|
|
|
# %s`, block, jsWithdrawals)
|
2023-10-31 03:18:37 +00:00
|
|
|
r.expectWithdrawalsRoot(expectedWithdrawalsRoot)
|
2023-08-21 02:08:54 +00:00
|
|
|
|
|
|
|
# Verify on `latest`
|
2023-09-06 09:18:26 +00:00
|
|
|
let bnu = env.clMock.latestExecutedPayload.blockNumber.uint64
|
|
|
|
let res = ws.wdHistory.verifyWithdrawals(bnu, none(UInt256), env.client)
|
2023-08-21 02:08:54 +00:00
|
|
|
testCond res.isOk:
|
|
|
|
error "verify wd error", msg=res.error
|