2021-11-14 07:52:22 +00:00
|
|
|
# Nimbus
|
2024-02-21 09:14:20 +00:00
|
|
|
# Copyright (c) 2021-2024 Status Research & Development GmbH
|
2021-11-14 07:52:22 +00:00
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
|
|
|
# at your option.
|
|
|
|
# This file may not be copied, modified, or distributed except according to
|
|
|
|
# those terms.
|
|
|
|
|
|
|
|
import
|
|
|
|
std/[tables, strutils],
|
2024-10-16 01:34:12 +00:00
|
|
|
eth/common/[eth_types_rlp, keys],
|
2021-11-14 07:52:22 +00:00
|
|
|
stew/byteutils,
|
2024-02-21 09:14:20 +00:00
|
|
|
chronos, stint,
|
2021-11-14 07:52:22 +00:00
|
|
|
json_rpc/[rpcclient],
|
2022-12-02 04:39:12 +00:00
|
|
|
../../../nimbus/utils/utils,
|
|
|
|
../../../nimbus/transaction,
|
|
|
|
./client
|
2021-11-14 07:52:22 +00:00
|
|
|
|
2024-03-20 07:35:38 +00:00
|
|
|
when false:
|
|
|
|
const
|
|
|
|
# This is the account that sends vault funding transactions.
|
2024-09-29 12:37:09 +00:00
|
|
|
vaultAccountAddr = address"0xcf49fda3be353c69b41ed96333cd24302da4556f"
|
2021-11-14 07:52:22 +00:00
|
|
|
|
|
|
|
const
|
|
|
|
# Address of the vault in genesis.
|
2024-09-29 12:37:09 +00:00
|
|
|
predeployedVaultAddr = address"0000000000000000000000000000000000000315"
|
2021-11-14 07:52:22 +00:00
|
|
|
# Number of blocks to wait before funding tx is considered valid.
|
|
|
|
vaultTxConfirmationCount = 5
|
|
|
|
|
|
|
|
# vault creates accounts for testing and funds them. An instance of the vault contract is
|
|
|
|
# deployed in the genesis block. When creating a new account using createAccount, the
|
|
|
|
# account is funded by sending a transaction to this contract.
|
|
|
|
#
|
|
|
|
# The purpose of the vault is allowing tests to run concurrently without worrying about
|
|
|
|
# nonce assignment and unexpected balance changes.
|
|
|
|
type
|
|
|
|
Vault* = ref object
|
|
|
|
# This tracks the account nonce of the vault account.
|
|
|
|
nonce: AccountNonce
|
|
|
|
# Created accounts are tracked in this map.
|
2024-10-16 01:34:12 +00:00
|
|
|
accounts: Table[Address, PrivateKey]
|
2021-11-14 07:52:22 +00:00
|
|
|
|
2022-07-04 07:38:02 +00:00
|
|
|
rng: ref HmacDrbgContext
|
2024-11-01 19:06:26 +00:00
|
|
|
chainId: ChainId
|
2021-11-14 07:52:22 +00:00
|
|
|
gasPrice: GasInt
|
|
|
|
vaultKey: PrivateKey
|
|
|
|
client: RpcClient
|
|
|
|
|
2024-11-01 19:06:26 +00:00
|
|
|
proc newVault*(chainID: ChainId, gasPrice: GasInt, client: RpcClient): Vault =
|
2021-11-14 07:52:22 +00:00
|
|
|
new(result)
|
|
|
|
result.rng = newRng()
|
2024-02-21 09:14:20 +00:00
|
|
|
result.chainId = chainID
|
2021-11-14 07:52:22 +00:00
|
|
|
result.gasPrice = gasPrice
|
|
|
|
result.vaultKey = PrivateKey.fromHex("63b508a03c3b5937ceb903af8b1b0c191012ef6eb7e9c3fb7afa94e5d214d376").get()
|
|
|
|
result.client = client
|
|
|
|
|
|
|
|
# generateKey creates a new account key and stores it.
|
2024-10-16 01:34:12 +00:00
|
|
|
proc generateKey*(v: Vault): Address =
|
2021-11-14 07:52:22 +00:00
|
|
|
let key = PrivateKey.random(v.rng[])
|
|
|
|
let address = toCanonicalAddress(key.toPublicKey)
|
|
|
|
v.accounts[address] = key
|
|
|
|
address
|
|
|
|
|
|
|
|
# nextNonce generates the nonce of a funding transaction.
|
|
|
|
proc nextNonce*(v: Vault): AccountNonce =
|
|
|
|
let nonce = v.nonce
|
|
|
|
inc(v.nonce)
|
|
|
|
nonce
|
|
|
|
|
2024-10-16 01:34:12 +00:00
|
|
|
proc sendSome(address: Address, amount: UInt256): seq[byte] =
|
2021-11-14 07:52:22 +00:00
|
|
|
const padding = repeat('\0', 12).toBytes
|
|
|
|
# makeshift contract ABI construction
|
|
|
|
# https://docs.soliditylang.org/en/develop/abi-spec.html
|
2024-10-16 01:34:12 +00:00
|
|
|
let h = keccak256("sendSome(address,uint256)".toBytes)
|
2021-11-14 07:52:22 +00:00
|
|
|
result.add h.data[0..3] # first 4 bytes of hash
|
|
|
|
result.add padding # left pad address
|
2024-09-29 12:37:09 +00:00
|
|
|
result.add address.data
|
2021-11-14 07:52:22 +00:00
|
|
|
result.add amount.toBytesBE
|
|
|
|
doAssert(result.len == 68) # 4 + 32 + 32
|
|
|
|
|
2024-05-15 03:07:59 +00:00
|
|
|
proc makeFundingTx*(
|
2024-10-16 01:34:12 +00:00
|
|
|
v: Vault, recipient: Address, amount: UInt256): PooledTransaction =
|
2021-11-14 07:52:22 +00:00
|
|
|
let
|
|
|
|
unsignedTx = Transaction(
|
|
|
|
txType : TxLegacy,
|
|
|
|
chainId : v.chainId,
|
|
|
|
nonce : v.nextNonce(),
|
|
|
|
gasPrice: v.gasPrice,
|
|
|
|
gasLimit: GasInt(75000),
|
2024-06-14 07:31:08 +00:00
|
|
|
to : Opt.some(predeployedVaultAddr),
|
2021-11-14 07:52:22 +00:00
|
|
|
value : 0.u256,
|
|
|
|
payload : sendSome(recipient, amount)
|
|
|
|
)
|
|
|
|
|
2024-05-15 03:07:59 +00:00
|
|
|
PooledTransaction(
|
2024-10-04 14:34:31 +00:00
|
|
|
tx: signTransaction(unsignedTx, v.vaultKey))
|
2021-11-14 07:52:22 +00:00
|
|
|
|
2022-05-31 08:42:01 +00:00
|
|
|
proc signTx*(v: Vault,
|
2024-10-16 01:34:12 +00:00
|
|
|
sender: Address,
|
2022-05-31 08:42:01 +00:00
|
|
|
nonce: AccountNonce,
|
2024-10-16 01:34:12 +00:00
|
|
|
recipient: Address,
|
2022-05-31 08:42:01 +00:00
|
|
|
amount: UInt256,
|
|
|
|
gasLimit, gasPrice: GasInt,
|
2024-05-15 03:07:59 +00:00
|
|
|
payload: seq[byte] = @[]): PooledTransaction =
|
2022-05-31 08:42:01 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
unsignedTx = Transaction(
|
|
|
|
txType : TxLegacy,
|
|
|
|
chainId : v.chainId,
|
|
|
|
nonce : nonce,
|
|
|
|
gasPrice: gasPrice,
|
|
|
|
gasLimit: gasLimit,
|
2024-06-14 07:31:08 +00:00
|
|
|
to : Opt.some(recipient),
|
2022-05-31 08:42:01 +00:00
|
|
|
value : amount,
|
|
|
|
payload : payload
|
|
|
|
)
|
|
|
|
|
|
|
|
let key = v.accounts[sender]
|
2024-05-15 03:07:59 +00:00
|
|
|
PooledTransaction(
|
2024-10-04 14:34:31 +00:00
|
|
|
tx: signTransaction(unsignedTx, key))
|
2022-05-31 08:42:01 +00:00
|
|
|
|
2021-11-14 07:52:22 +00:00
|
|
|
# createAccount creates a new account that is funded from the vault contract.
|
|
|
|
# It will panic when the account could not be created and funded.
|
2024-10-16 01:34:12 +00:00
|
|
|
proc createAccount*(v: Vault, amount: UInt256): Future[Address] {.async.} =
|
2021-11-14 07:52:22 +00:00
|
|
|
let address = v.generateKey()
|
|
|
|
|
|
|
|
# order the vault to send some ether
|
|
|
|
let tx = v.makeFundingTx(address, amount)
|
|
|
|
let res = await v.client.sendTransaction(tx)
|
|
|
|
if not res:
|
|
|
|
raise newException(ValueError, "unable to send funding transaction")
|
|
|
|
|
|
|
|
let txBlock = await v.client.blockNumber()
|
|
|
|
|
|
|
|
# wait for vaultTxConfirmationCount confirmation by checking the balance vaultTxConfirmationCount blocks back.
|
|
|
|
# createAndFundAccountWithSubscription for a better solution using logs
|
|
|
|
let count = vaultTxConfirmationCount*4
|
|
|
|
for i in 0..<count:
|
|
|
|
let number = await v.client.blockNumber()
|
|
|
|
if number > txBlock + vaultTxConfirmationCount:
|
|
|
|
let checkBlock = number - vaultTxConfirmationCount
|
|
|
|
let balance = await v.client.balanceAt(address, checkBlock)
|
|
|
|
if balance >= amount:
|
|
|
|
return address
|
|
|
|
|
|
|
|
let period = chronos.seconds(1)
|
|
|
|
await sleepAsync(period)
|
|
|
|
|
|
|
|
let txHash = tx.rlpHash().data.toHex
|
|
|
|
raise newException(ValueError, "could not fund account $2 in transaction $2" % [address.toHex, txHash])
|