nimbus-eth1/hive_integration/nodocker/engine/withdrawals/wd_reorg_spec.nim

340 lines
13 KiB
Nim
Raw Normal View History

2023-11-01 03:32:09 +00:00
# Nimbus
# 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
std/tables,
2023-08-21 02:08:54 +00:00
stint,
chronos,
chronicles,
eth/common,
2023-08-21 02:08:54 +00:00
./wd_base_spec,
./wd_history,
2023-08-21 02:08:54 +00:00
../test_env,
../engine_client,
../types,
../base_spec,
../../../../nimbus/beacon/web3_eth_conv
2023-08-21 02:08:54 +00:00
# Withdrawals re-org spec:
# Specifies a withdrawals test where the withdrawals re-org can happen
# even to a point before withdrawals were enabled, or simply to a previous
# withdrawals block.
type
ReorgSpec* = ref object of WDBaseSpec
# How many blocks the re-org will replace, including the head
reOrgBlockCount* : int
# Whether the client should fetch the sidechain by syncing from the secondary client
reOrgViaSync* : bool
sidechainTimeIncrements*: int
2023-08-21 02:08:54 +00:00
Sidechain = ref object
startAccount: UInt256
nextIndex : int
wdHistory : WDHistory
sidechain : Table[uint64, ExecutionPayload]
payloadId : PayloadID
height : uint64
attr : Option[PayloadAttributes]
2023-08-21 02:08:54 +00:00
Canonical = ref object
startAccount: UInt256
nextIndex : int
proc getSidechainSplitHeight(ws: ReorgSpec): int =
doAssert(ws.reOrgBlockCount <= ws.getTotalPayloadCount())
return ws.getTotalPayloadCount() + 1 - ws.reOrgBlockCount
2023-08-21 02:08:54 +00:00
proc getSidechainBlockTimeIncrements(ws: ReorgSpec): int=
if ws.sidechainTimeIncrements == 0:
return ws.getBlockTimeIncrements()
ws.sidechainTimeIncrements
2023-08-21 02:08:54 +00:00
proc getSidechainforkHeight(ws: ReorgSpec): int =
if ws.getSidechainBlockTimeIncrements() != ws.getBlockTimeIncrements():
# Block timestamp increments in both chains are different so need to
# calculate different heights, only if split happens before fork.
# We cannot split by having two different genesis blocks.
doAssert(ws.getSidechainSplitHeight() != 0, "invalid sidechain split height")
2023-08-21 02:08:54 +00:00
if ws.getSidechainSplitHeight() <= ws.forkHeight:
2023-08-21 02:08:54 +00:00
# We need to calculate the height of the fork on the sidechain
let sidechainSplitBlocktimestamp = (ws.getSidechainSplitHeight() - 1) * ws.getBlockTimeIncrements()
let remainingTime = ws.getWithdrawalsGenesisTimeDelta() - sidechainSplitBlocktimestamp
if remainingTime == 0 :
2023-08-21 02:08:54 +00:00
return ws.getSidechainSplitHeight()
return ((remainingTime - 1) div ws.sidechainTimeIncrements) + ws.getSidechainSplitHeight()
2023-08-21 02:08:54 +00:00
return ws.forkHeight
2023-08-21 02:08:54 +00:00
proc execute*(ws: ReorgSpec, env: TestEnv): bool =
result = true
testCond waitFor env.clMock.waitForTTD()
2023-08-21 02:08:54 +00:00
# Spawn a secondary client which will produce the sidechain
let sec = env.addEngine(addToCL = false)
var
canonical = Canonical(
startAccount: u256(0x1000),
nextIndex : 0,
)
sidechain = Sidechain(
startAccount: 1.u256 shl 160,
nextIndex : 0,
wdHistory : WDHistory(),
sidechain : initTable[uint64, ExecutionPayload]()
)
2023-08-21 02:08:54 +00:00
# Sidechain withdraws on the max account value range 0xffffffffffffffffffffffffffffffffffffffff
sidechain.startAccount -= u256(ws.getWithdrawableAccountCount()+1)
2023-08-21 02:08:54 +00:00
let numBlocks = ws.getPreWithdrawalsBlockCount()+ws.wdBlockCount
let pbRes = env.clMock.produceBlocks(numBlocks, BlockProcessCallbacks(
onPayloadProducerSelected: proc(): bool =
env.clMock.nextWithdrawals = none(seq[WithdrawalV1])
2023-08-21 02:08:54 +00:00
if env.clMock.currentPayloadNumber >= ws.forkHeight.uint64:
2023-08-21 02:08:54 +00:00
# Prepare some withdrawals
let wfb = ws.generateWithdrawalsForBlock(canonical.nextIndex, canonical.startAccount)
env.clMock.nextWithdrawals = some(w3Withdrawals wfb.wds)
canonical.nextIndex = wfb.nextIndex
ws.wdHistory.put(env.clMock.currentPayloadNumber, wfb.wds)
2023-08-21 02:08:54 +00:00
if env.clMock.currentPayloadNumber >= ws.getSidechainSplitHeight().uint64:
2023-08-21 02:08:54 +00:00
# We have split
if env.clMock.currentPayloadNumber >= ws.getSidechainforkHeight().uint64:
2023-08-21 02:08:54 +00:00
# And we are past the withdrawals fork on the sidechain
let wfb = ws.generateWithdrawalsForBlock(sidechain.nextIndex, sidechain.startAccount)
sidechain.wdHistory.put(env.clMock.currentPayloadNumber, wfb.wds)
sidechain.nextIndex = wfb.nextIndex
else:
if env.clMock.nextWithdrawals.isSome:
let wds = ethWithdrawals env.clMock.nextWithdrawals.get()
sidechain.wdHistory.put(env.clMock.currentPayloadNumber, wds)
sidechain.nextIndex = canonical.nextIndex
return true
,
onRequestNextPayload: proc(): bool =
2023-08-21 02:08:54 +00:00
# Send transactions to be included in the payload
let txs = env.makeTxs(
BaseTx(
recipient: some(prevRandaoContractAddr),
amount: 1.u256,
txType: ws.txType,
gasLimit: 75000.GasInt,
),
ws.getTransactionCountPerPayload()
2023-08-21 02:08:54 +00:00
)
testCond env.sendTxs(env.clMock.nextBlockProducer, txs):
error "Error trying to send transaction"
2023-08-21 02:08:54 +00:00
# Error will be ignored here since the tx could have been already relayed
discard env.sendTxs(sec, txs)
2023-08-21 02:08:54 +00:00
if env.clMock.currentPayloadNumber >= ws.getSidechainSplitHeight().uint64:
2023-08-21 02:08:54 +00:00
# Also request a payload from the sidechain
var fcState = ForkchoiceStateV1(
headBlockHash: env.clMock.latestForkchoice.headBlockHash,
)
if env.clMock.currentPayloadNumber > ws.getSidechainSplitHeight().uint64:
let lastSidePayload = sidechain.sidechain[env.clMock.currentPayloadNumber-1]
fcState.headBlockHash = lastSidePayload.blockHash
var attr = PayloadAttributes(
prevRandao: env.clMock.latestPayloadAttributes.prevRandao,
suggestedFeeRecipient: env.clMock.latestPayloadAttributes.suggestedFeeRecipient,
)
if env.clMock.currentPayloadNumber > ws.getSidechainSplitHeight().uint64:
attr.timestamp = w3Qty(sidechain.sidechain[env.clMock.currentPayloadNumber-1].timestamp, ws.getSidechainBlockTimeIncrements())
elif env.clMock.currentPayloadNumber == ws.getSidechainSplitHeight().uint64:
attr.timestamp = w3Qty(env.clMock.latestHeader.timestamp, ws.getSidechainBlockTimeIncrements())
else:
attr.timestamp = env.clMock.latestPayloadAttributes.timestamp
if env.clMock.currentPayloadNumber >= ws.getSidechainforkHeight().uint64:
2023-08-21 02:08:54 +00:00
# Withdrawals
let rr = sidechain.wdHistory.get(env.clMock.currentPayloadNumber)
testCond rr.isOk:
error "sidechain wd", msg=rr.error
attr.withdrawals = some(w3Withdrawals rr.get)
2023-08-21 02:08:54 +00:00
info "Requesting sidechain payload",
number=env.clMock.currentPayloadNumber
2023-08-21 02:08:54 +00:00
sidechain.attr = some(attr)
let r = sec.client.forkchoiceUpdated(fcState, attr)
2023-08-21 02:08:54 +00:00
r.expectNoError()
2023-10-31 03:18:37 +00:00
r.expectPayloadStatus(PayloadExecutionStatus.valid)
testCond r.get().payloadID.isSome:
error "Unable to get a payload ID on the sidechain"
sidechain.payloadId = r.get().payloadID.get()
return true
,
onGetPayload: proc(): bool =
var
payload: ExecutionPayload
if env.clMock.latestPayloadBuilt.blockNumber.uint64 >= ws.getSidechainSplitHeight().uint64:
2023-08-21 02:08:54 +00:00
# This payload is built by the secondary client, hence need to manually fetch it here
doAssert(sidechain.attr.isSome)
let version = sidechain.attr.get().version
let r = sec.client.getPayload(sidechain.payloadId, version)
2023-08-21 02:08:54 +00:00
r.expectNoError()
payload = r.get().executionPayload
sidechain.sidechain[payload.blockNumber.uint64] = payload
else:
2023-08-21 02:08:54 +00:00
# This block is part of both chains, simply forward it to the secondary client
payload = env.clMock.latestPayloadBuilt
2023-09-30 14:31:57 +00:00
let r = sec.client.newPayload(payload)
2023-10-31 03:18:37 +00:00
r.expectStatus(PayloadExecutionStatus.valid)
let fcState = ForkchoiceStateV1(
headBlockHash: payload.blockHash,
2023-08-21 02:08:54 +00:00
)
let p = sec.client.forkchoiceUpdated(payload.version, fcState)
2023-10-31 03:18:37 +00:00
p.expectPayloadStatus(PayloadExecutionStatus.valid)
return true
))
testCond pbRes
2023-08-21 02:08:54 +00:00
sidechain.height = env.clMock.latestExecutedPayload.blockNumber.uint64
2023-08-21 02:08:54 +00:00
if ws.forkHeight < ws.getSidechainforkHeight():
2023-08-21 02:08:54 +00:00
# This means the canonical chain forked before the sidechain.
# Therefore we need to produce more sidechain payloads to reach
# at least`ws.WithdrawalsBlockCount` withdrawals payloads produced on
# the sidechain.
let height = ws.getSidechainforkHeight()-ws.forkHeight
for i in 0..<height:
let
wfb = ws.generateWithdrawalsForBlock(sidechain.nextIndex, sidechain.startAccount)
sidechain.wdHistory.put(sidechain.height+1, wfb.wds)
sidechain.nextIndex = wfb.nextIndex
let wds = sidechain.wdHistory.get(sidechain.height+1).valueOr:
echo "get wd history error ", error
return false
let
attr = PayloadAttributes(
timestamp: w3Qty(sidechain.sidechain[sidechain.height].timestamp, ws.getSidechainBlockTimeIncrements()),
prevRandao: env.clMock.latestPayloadAttributes.prevRandao,
suggestedFeeRecipient: env.clMock.latestPayloadAttributes.suggestedFeeRecipient,
withdrawals: some(w3Withdrawals wds),
)
fcState = ForkchoiceStateV1(
headBlockHash: sidechain.sidechain[sidechain.height].blockHash,
)
let r = sec.client.forkchoiceUpdatedV2(fcState, some(attr))
2023-10-31 03:18:37 +00:00
r.expectPayloadStatus(PayloadExecutionStatus.valid)
let p = sec.client.getPayloadV2(r.get().payloadID.get)
2023-08-21 02:08:54 +00:00
p.expectNoError()
let z = p.get()
let s = sec.client.newPayloadV2(z.executionPayload)
2023-10-31 03:18:37 +00:00
s.expectStatus(PayloadExecutionStatus.valid)
let fs = ForkchoiceStateV1(headBlockHash: z.executionPayload.blockHash)
let q = sec.client.forkchoiceUpdatedV2(fs)
2023-10-31 03:18:37 +00:00
q.expectPayloadStatus(PayloadExecutionStatus.valid)
inc sidechain.height
sidechain.sidechain[sidechain.height] = executionPayload(z.executionPayload)
2023-08-21 02:08:54 +00:00
# Check the withdrawals on the latest
let res = ws.wdHistory.verifyWithdrawals(sidechain.height, none(UInt256), env.client)
testCond res.isOk
2023-08-21 02:08:54 +00:00
if ws.reOrgViaSync:
2023-08-21 02:08:54 +00:00
# Send latest sidechain payload as NewPayload + FCU and wait for sync
let
payload = sidechain.sidechain[sidechain.height]
sideHash = sidechain.sidechain[sidechain.height].blockHash
sleep = DefaultSleep
period = chronos.seconds(sleep)
var loop = 0
if ws.timeoutSeconds == 0:
ws.timeoutSeconds = DefaultTimeout
while loop < ws.timeoutSeconds:
let r = env.client.newPayloadV2(payload.V2)
2023-08-21 02:08:54 +00:00
r.expectNoError()
let fcState = ForkchoiceStateV1(headBlockHash: sideHash)
let p = env.client.forkchoiceUpdatedV2(fcState)
2023-08-21 02:08:54 +00:00
p.expectNoError()
let status = p.get().payloadStatus.status
if status == PayloadExecutionStatus.invalid:
error "Primary client invalidated side chain"
return false
2023-10-31 03:18:37 +00:00
let b = env.client.latestHeader()
testCond b.isOk
2023-10-31 03:18:37 +00:00
let header = b.get
if header.blockHash == ethHash(sideHash):
# sync successful
break
waitFor sleepAsync(period)
loop += sleep
else:
2023-08-21 02:08:54 +00:00
# Send all payloads one by one to the primary client
var payloadNumber = ws.getSidechainSplitHeight()
while payloadNumber.uint64 <= sidechain.height:
let payload = sidechain.sidechain[payloadNumber.uint64]
var version = Version.V1
if payloadNumber >= ws.getSidechainforkHeight():
version = Version.V2
info "Sending sidechain",
payloadNumber,
hash=payload.blockHash.short,
parentHash=payload.parentHash.short
2023-09-30 14:31:57 +00:00
let r = env.client.newPayload(payload)
2023-10-31 03:18:37 +00:00
r.expectStatusEither([PayloadExecutionStatus.valid, PayloadExecutionStatus.accepted])
let fcState = ForkchoiceStateV1(headBlockHash: payload.blockHash)
let p = env.client.forkchoiceUpdated(version, fcState)
2023-10-31 03:18:37 +00:00
p.expectPayloadStatus(PayloadExecutionStatus.valid)
inc payloadNumber
2023-08-21 02:08:54 +00:00
# Verify withdrawals changed
let r2 = sidechain.wdHistory.verifyWithdrawals(sidechain.height, none(UInt256), env.client)
testCond r2.isOk
2023-08-21 02:08:54 +00:00
# Verify all balances of accounts in the original chain didn't increase
# after the fork.
# We are using different accounts credited between the canonical chain
# and the fork.
# We check on `latest`.
let r3 = ws.wdHistory.verifyWithdrawals(uint64(ws.forkHeight-1), none(UInt256), env.client)
testCond r3.isOk
2023-08-21 02:08:54 +00:00
# Re-Org back to the canonical chain
let fcState = ForkchoiceStateV1(headBlockHash: env.clMock.latestPayloadBuilt.blockHash)
let r = env.client.forkchoiceUpdatedV2(fcState)
2023-10-31 03:18:37 +00:00
r.expectPayloadStatus(PayloadExecutionStatus.valid)