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-04 03:47:18 +00:00
|
|
|
std/[tables, sets, strutils],
|
2024-06-14 07:31:08 +00:00
|
|
|
eth/common/eth_types as common,
|
2023-08-21 02:08:54 +00:00
|
|
|
json_rpc/[rpcclient],
|
2024-06-14 07:31:08 +00:00
|
|
|
stew/byteutils,
|
|
|
|
results,
|
2023-10-04 03:47:18 +00:00
|
|
|
../engine_client,
|
2024-02-15 02:57:05 +00:00
|
|
|
../../../../nimbus/utils/utils,
|
|
|
|
../../../../nimbus/beacon/web3_eth_conv
|
2023-08-21 02:08:54 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
Withdrawals* = ref object
|
|
|
|
list*: seq[Withdrawal]
|
|
|
|
|
|
|
|
# Helper structure used to keep history of the amounts
|
|
|
|
# withdrawn to each test account.
|
|
|
|
WDHistory* = object
|
|
|
|
map: Table[uint64, Withdrawals]
|
|
|
|
|
|
|
|
proc put*(wh: var WDHistory, blockNumber: uint64, wds: openArray[Withdrawal]) =
|
|
|
|
wh.map[blockNumber] = Withdrawals(
|
|
|
|
list: @wds
|
|
|
|
)
|
|
|
|
|
|
|
|
proc get*(wh: WDHistory, blockNumber: uint64): Result[seq[Withdrawal], string] =
|
|
|
|
let wds = wh.map.getOrDefault(blockNumber)
|
|
|
|
if wds.isNil:
|
|
|
|
return err("withdrawal not found in block " & $blockNumber)
|
|
|
|
ok(wds.list)
|
|
|
|
|
|
|
|
# Gets an account expected value for a given block, taking into account all
|
|
|
|
# withdrawals that credited the account.
|
|
|
|
func getExpectedAccountBalance*(wh: WDHistory, account: EthAddress, blockNumber: uint64): UInt256 =
|
|
|
|
for b in 0..blockNumber:
|
|
|
|
let wds = wh.map.getOrDefault(b)
|
|
|
|
if wds.isNil: continue
|
|
|
|
for wd in wds.list:
|
|
|
|
if wd.address == account:
|
|
|
|
result += wd.weiAmount
|
|
|
|
|
|
|
|
# Get a list of all addresses that were credited by withdrawals on a given block.
|
|
|
|
func getAddressesWithdrawnOnBlock*(wh: WDHistory, blockNumber: uint64): seq[EthAddress] =
|
|
|
|
var addressMap: HashSet[EthAddress]
|
|
|
|
let wds = wh.map.getOrDefault(blockNumber)
|
|
|
|
if wds.isNil.not:
|
|
|
|
for wd in wds.list:
|
|
|
|
addressMap.incl wd.address
|
|
|
|
|
|
|
|
for address in addressMap:
|
|
|
|
result.add address
|
|
|
|
|
|
|
|
# Get the withdrawals list for a given block.
|
|
|
|
func getWithdrawals*(wh: WDHistory, blockNumber: uint64): Withdrawals =
|
|
|
|
let wds = wh.map.getOrDefault(blockNumber)
|
|
|
|
if wds.isNil:
|
|
|
|
Withdrawals()
|
|
|
|
else:
|
|
|
|
wds
|
|
|
|
|
|
|
|
# Get the withdrawn accounts list until a given block height.
|
|
|
|
func getWithdrawnAccounts*(wh: WDHistory, blockHeight: uint64): Table[EthAddress, UInt256] =
|
|
|
|
for blockNumber in 0..blockHeight:
|
|
|
|
let wds = wh.map.getOrDefault(blockNumber)
|
|
|
|
if wds.isNil: continue
|
|
|
|
for wd in wds.list:
|
|
|
|
result.withValue(wd.address, value) do:
|
|
|
|
value[] += wd.weiAmount
|
|
|
|
do:
|
|
|
|
result[wd.address] = wd.weiAmount
|
|
|
|
|
|
|
|
# Verify all withdrawals on a client at a given height
|
2024-06-14 07:31:08 +00:00
|
|
|
proc verifyWithdrawals*(wh: WDHistory, blockNumber: uint64,
|
|
|
|
rpcBlock: Opt[common.BlockNumber],
|
|
|
|
client: RpcClient): Result[void, string] =
|
2023-08-21 02:08:54 +00:00
|
|
|
let accounts = wh.getWithdrawnAccounts(blockNumber)
|
|
|
|
for account, expectedBalance in accounts:
|
|
|
|
let res = if rpcBlock.isSome:
|
|
|
|
client.balanceAt(account, rpcBlock.get)
|
|
|
|
else:
|
|
|
|
client.balanceAt(account)
|
|
|
|
res.expectBalanceEqual(account, expectedBalance)
|
|
|
|
|
|
|
|
# All withdrawals account have a bytecode that unconditionally set the
|
|
|
|
# zero storage key to one on EVM execution.
|
|
|
|
# Withdrawals must not trigger EVM so we expect zero.
|
|
|
|
let s = if rpcBlock.isSome:
|
|
|
|
client.storageAt(account, 0.u256, rpcBlock.get)
|
|
|
|
else:
|
|
|
|
client.storageAt(account, 0.u256)
|
2024-01-13 01:41:57 +00:00
|
|
|
s.expectStorageEqual(account, 0.u256.w3FixedBytes)
|
2023-08-21 02:08:54 +00:00
|
|
|
ok()
|
|
|
|
|
|
|
|
# Create a new copy of the withdrawals history
|
|
|
|
func copy*(wh: WDHistory): WDHistory =
|
|
|
|
for k, v in wh.map:
|
|
|
|
result.map[k] = v
|