2021-03-26 06:52:01 +00:00
|
|
|
# beacon_chain
|
2023-01-20 14:14:37 +00:00
|
|
|
# Copyright (c) 2018-2023 Status Research & Development GmbH
|
2021-03-26 06:52:01 +00:00
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
2023-01-20 14:14:37 +00:00
|
|
|
{.push raises: [].}
|
2021-03-26 06:52:01 +00:00
|
|
|
|
2019-10-03 09:21:28 +00:00
|
|
|
import
|
2021-03-23 06:57:10 +00:00
|
|
|
os, sequtils, strutils, options, json, terminal,
|
2020-07-27 19:26:30 +00:00
|
|
|
chronos, chronicles, confutils, stint, json_serialization,
|
2021-03-05 13:12:00 +00:00
|
|
|
../filepath,
|
|
|
|
../networking/network_metadata,
|
2021-03-23 06:57:10 +00:00
|
|
|
web3, web3/confutils_defs, eth/keys, eth/p2p/discoveryv5/random2,
|
|
|
|
stew/io2,
|
2021-08-18 18:57:58 +00:00
|
|
|
../spec/eth2_merkleization,
|
|
|
|
../spec/datatypes/base,
|
2021-03-03 06:23:05 +00:00
|
|
|
../validators/keystore_management
|
2019-10-03 09:21:28 +00:00
|
|
|
|
2019-11-05 18:16:10 +00:00
|
|
|
# Compiled version of /scripts/depositContract.v.py in this repo
|
|
|
|
# The contract was compiled in Remix (https://remix.ethereum.org/) with vyper (remote) compiler.
|
2020-07-17 20:59:50 +00:00
|
|
|
const contractCode = staticRead "deposit_contract_code.txt"
|
2019-10-03 09:21:28 +00:00
|
|
|
|
|
|
|
type
|
2020-07-17 20:59:50 +00:00
|
|
|
Eth1Address = web3.Address
|
|
|
|
|
2019-10-03 09:21:28 +00:00
|
|
|
StartUpCommand {.pure.} = enum
|
|
|
|
deploy
|
|
|
|
sendEth
|
2020-07-27 19:26:30 +00:00
|
|
|
generateSimulationDeposits
|
|
|
|
sendDeposits
|
2019-10-03 09:21:28 +00:00
|
|
|
|
|
|
|
CliConfig = object
|
2020-03-24 11:13:07 +00:00
|
|
|
web3Url* {.
|
2020-07-27 19:26:30 +00:00
|
|
|
defaultValue: "",
|
2019-10-29 00:20:47 +00:00
|
|
|
desc: "URL of the Web3 server to observe Eth1"
|
2019-11-11 14:43:12 +00:00
|
|
|
name: "web3-url" }: string
|
2019-10-29 00:20:47 +00:00
|
|
|
|
2019-10-03 09:21:28 +00:00
|
|
|
privateKey* {.
|
2019-10-29 00:20:47 +00:00
|
|
|
defaultValue: ""
|
2019-11-11 14:43:12 +00:00
|
|
|
desc: "Private key of the controlling account"
|
|
|
|
name: "private-key" }: string
|
2019-10-03 09:21:28 +00:00
|
|
|
|
2020-07-17 20:59:50 +00:00
|
|
|
askForKey {.
|
|
|
|
defaultValue: false
|
|
|
|
desc: "Ask for an Eth1 private key interactively"
|
|
|
|
name: "ask-for-key" }: bool
|
|
|
|
|
2020-09-01 09:01:57 +00:00
|
|
|
eth2Network* {.
|
|
|
|
desc: "The Eth2 network preset to use"
|
|
|
|
name: "network" }: Option[string]
|
|
|
|
|
2019-10-03 09:21:28 +00:00
|
|
|
case cmd* {.command.}: StartUpCommand
|
|
|
|
of deploy:
|
|
|
|
discard
|
|
|
|
|
|
|
|
of sendEth:
|
2020-07-17 20:59:50 +00:00
|
|
|
toAddress {.name: "to".}: Eth1Address
|
2019-11-11 14:43:12 +00:00
|
|
|
valueEth {.name: "eth".}: string
|
2019-10-03 09:21:28 +00:00
|
|
|
|
2020-07-27 19:26:30 +00:00
|
|
|
of generateSimulationDeposits:
|
|
|
|
simulationDepositsCount {.
|
|
|
|
desc: "The number of validator keystores to generate"
|
|
|
|
name: "count" }: Natural
|
|
|
|
|
|
|
|
outValidatorsDir {.
|
|
|
|
desc: "A directory to store the generated validator keystores"
|
|
|
|
name: "out-validators-dir" }: OutDir
|
|
|
|
|
|
|
|
outSecretsDir {.
|
|
|
|
desc: "A directory to store the generated keystore password files"
|
|
|
|
name: "out-secrets-dir" }: OutDir
|
|
|
|
|
|
|
|
outDepositsFile {.
|
|
|
|
desc: "A LaunchPad deposits file to write"
|
|
|
|
name: "out-deposits-file" }: OutFile
|
|
|
|
|
2022-05-10 00:32:12 +00:00
|
|
|
threshold {.
|
|
|
|
defaultValue: 1
|
|
|
|
desc: "Used to generate distributed keys"
|
|
|
|
name: "threshold" }: uint32
|
|
|
|
|
|
|
|
remoteValidatorsCount {.
|
|
|
|
defaultValue: 0
|
|
|
|
desc: "The number of distributed validators validator"
|
|
|
|
name: "remote-validators-count" }: uint32
|
|
|
|
|
|
|
|
remoteSignersUrls {.
|
|
|
|
desc: "URLs of the remote signers"
|
|
|
|
name: "remote-signer" }: seq[string]
|
|
|
|
|
2020-07-27 19:26:30 +00:00
|
|
|
of sendDeposits:
|
2020-07-17 20:59:50 +00:00
|
|
|
depositsFile {.
|
|
|
|
desc: "A LaunchPad deposits file"
|
|
|
|
name: "deposits-file" }: InputFile
|
|
|
|
|
|
|
|
depositContractAddress {.
|
|
|
|
desc: "Address of the deposit contract"
|
|
|
|
name: "deposit-contract" }: Eth1Address
|
|
|
|
|
|
|
|
minDelay {.
|
|
|
|
defaultValue: 0.0
|
|
|
|
desc: "Minimum possible delay between making two deposits (in seconds)"
|
|
|
|
name: "min-delay" }: float
|
|
|
|
|
|
|
|
maxDelay {.
|
|
|
|
defaultValue: 0.0
|
|
|
|
desc: "Maximum possible delay between making two deposits (in seconds)"
|
|
|
|
name: "max-delay" }: float
|
|
|
|
|
2022-05-10 00:32:12 +00:00
|
|
|
type
|
|
|
|
PubKeyBytes = DynamicBytes[48, 48]
|
|
|
|
WithdrawalCredentialsBytes = DynamicBytes[32, 32]
|
|
|
|
SignatureBytes = DynamicBytes[96, 96]
|
|
|
|
|
2020-07-17 20:59:50 +00:00
|
|
|
contract(DepositContract):
|
2022-05-10 00:32:12 +00:00
|
|
|
proc deposit(pubkey: PubKeyBytes,
|
|
|
|
withdrawalCredentials: WithdrawalCredentialsBytes,
|
|
|
|
signature: SignatureBytes,
|
2020-07-17 20:59:50 +00:00
|
|
|
deposit_data_root: FixedBytes[32])
|
|
|
|
|
2020-06-28 21:06:43 +00:00
|
|
|
proc deployContract*(web3: Web3, code: string): Future[ReceiptObject] {.async.} =
|
2019-10-03 09:21:28 +00:00
|
|
|
var code = code
|
|
|
|
if code[1] notin {'x', 'X'}:
|
|
|
|
code = "0x" & code
|
|
|
|
let tr = EthSend(
|
|
|
|
source: web3.defaultAccount,
|
|
|
|
data: code,
|
|
|
|
gas: Quantity(3000000).some,
|
|
|
|
gasPrice: 1.some)
|
|
|
|
|
|
|
|
let r = await web3.send(tr)
|
2020-06-28 21:06:43 +00:00
|
|
|
result = await web3.getMinedTransactionReceipt(r)
|
2019-10-03 09:21:28 +00:00
|
|
|
|
2020-07-17 20:59:50 +00:00
|
|
|
proc sendEth(web3: Web3, to: Eth1Address, valueEth: int): Future[TxHash] =
|
2019-10-03 09:21:28 +00:00
|
|
|
let tr = EthSend(
|
|
|
|
source: web3.defaultAccount,
|
|
|
|
gas: Quantity(3000000).some,
|
|
|
|
gasPrice: 1.some,
|
|
|
|
value: some(valueEth.u256 * 1000000000000000000.u256),
|
2020-07-17 20:59:50 +00:00
|
|
|
to: some(to))
|
2019-10-03 09:21:28 +00:00
|
|
|
web3.send(tr)
|
|
|
|
|
2020-07-17 20:59:50 +00:00
|
|
|
type
|
2023-08-25 09:29:07 +00:00
|
|
|
DelayGenerator* = proc(): chronos.Duration {.gcsafe, raises: [].}
|
2020-07-17 20:59:50 +00:00
|
|
|
|
|
|
|
proc ethToWei(eth: UInt256): UInt256 =
|
|
|
|
eth * 1000000000000000000.u256
|
|
|
|
|
2020-07-28 20:37:12 +00:00
|
|
|
proc initWeb3(web3Url, privateKey: string): Future[Web3] {.async.} =
|
|
|
|
result = await newWeb3(web3Url)
|
|
|
|
if privateKey.len != 0:
|
|
|
|
result.privateKey = some(PrivateKey.fromHex(privateKey)[])
|
|
|
|
else:
|
|
|
|
let accounts = await result.provider.eth_accounts()
|
|
|
|
doAssert(accounts.len > 0)
|
|
|
|
result.defaultAccount = accounts[0]
|
|
|
|
|
2020-07-17 20:59:50 +00:00
|
|
|
# TODO: async functions should note take `seq` inputs because
|
|
|
|
# this leads to full copies.
|
|
|
|
proc sendDeposits*(deposits: seq[LaunchPadDeposit],
|
|
|
|
web3Url, privateKey: string,
|
|
|
|
depositContractAddress: Eth1Address,
|
|
|
|
delayGenerator: DelayGenerator = nil) {.async.} =
|
2020-10-01 18:56:42 +00:00
|
|
|
notice "Sending deposits",
|
2020-07-17 20:59:50 +00:00
|
|
|
web3 = web3Url,
|
|
|
|
depositContract = depositContractAddress
|
|
|
|
|
2020-07-28 20:37:12 +00:00
|
|
|
var web3 = await initWeb3(web3Url, privateKey)
|
2022-05-10 00:32:12 +00:00
|
|
|
let gasPrice = int(await web3.provider.eth_gasPrice()) * 2
|
2020-07-17 20:59:50 +00:00
|
|
|
let depositContract = web3.contractSender(DepositContract,
|
|
|
|
Address depositContractAddress)
|
2022-05-10 00:32:12 +00:00
|
|
|
for i in 4200 ..< deposits.len:
|
|
|
|
let dp = deposits[i] as DepositData
|
2020-07-28 20:37:12 +00:00
|
|
|
|
|
|
|
while true:
|
|
|
|
try:
|
|
|
|
let tx = depositContract.deposit(
|
2022-05-10 00:32:12 +00:00
|
|
|
PubKeyBytes(@(dp.pubkey.toRaw())),
|
|
|
|
WithdrawalCredentialsBytes(@(dp.withdrawal_credentials.data)),
|
|
|
|
SignatureBytes(@(dp.signature.toRaw())),
|
2020-07-28 20:37:12 +00:00
|
|
|
FixedBytes[32](hash_tree_root(dp).data))
|
|
|
|
|
2022-05-10 00:32:12 +00:00
|
|
|
let status = await tx.send(value = 32.u256.ethToWei, gasPrice = gasPrice)
|
2020-07-17 20:59:50 +00:00
|
|
|
|
2022-05-10 00:32:12 +00:00
|
|
|
info "Deposit sent", tx = $status
|
2020-07-17 20:59:50 +00:00
|
|
|
|
2020-07-28 20:37:12 +00:00
|
|
|
if delayGenerator != nil:
|
|
|
|
await sleepAsync(delayGenerator())
|
|
|
|
|
|
|
|
break
|
2020-11-17 10:14:53 +00:00
|
|
|
except CatchableError:
|
2020-07-28 20:37:12 +00:00
|
|
|
await sleepAsync(60.seconds)
|
|
|
|
web3 = await initWeb3(web3Url, privateKey)
|
2020-07-17 20:59:50 +00:00
|
|
|
|
2021-03-26 06:52:01 +00:00
|
|
|
{.pop.} # TODO confutils.nim(775, 17) Error: can raise an unlisted exception: ref IOError
|
2019-10-03 09:21:28 +00:00
|
|
|
proc main() {.async.} =
|
2022-03-22 08:42:28 +00:00
|
|
|
var conf = try: CliConfig.load()
|
|
|
|
except CatchableError as exc:
|
|
|
|
raise exc
|
|
|
|
except Exception as exc: # TODO fix confutils
|
|
|
|
raiseAssert exc.msg
|
|
|
|
|
2023-06-19 22:43:50 +00:00
|
|
|
let rng = HmacDrbgContext.new()
|
2020-07-27 19:26:30 +00:00
|
|
|
|
2021-07-13 14:27:10 +00:00
|
|
|
if conf.cmd == StartUpCommand.generateSimulationDeposits:
|
2020-07-27 19:26:30 +00:00
|
|
|
let
|
2020-08-21 19:36:42 +00:00
|
|
|
mnemonic = generateMnemonic(rng[])
|
2022-04-15 12:46:56 +00:00
|
|
|
seed = getSeed(mnemonic, KeystorePass.init "")
|
2021-07-13 14:27:10 +00:00
|
|
|
cfg = getRuntimeConfig(conf.eth2Network)
|
2023-03-05 01:40:21 +00:00
|
|
|
threshold = if conf.remoteSignersUrls.len > 0: conf.threshold
|
|
|
|
else: 0
|
|
|
|
|
|
|
|
if conf.remoteValidatorsCount > 0 and
|
|
|
|
conf.remoteSignersUrls.len == 0:
|
|
|
|
fatal "Please specify at least one remote signer URL"
|
|
|
|
quit 1
|
2020-07-27 19:26:30 +00:00
|
|
|
|
2022-03-22 17:06:21 +00:00
|
|
|
if (let res = secureCreatePath(string conf.outValidatorsDir); res.isErr):
|
2020-08-27 18:23:41 +00:00
|
|
|
warn "Could not create validators folder",
|
2022-03-22 17:06:21 +00:00
|
|
|
path = string conf.outValidatorsDir, err = ioErrorMsg(res.error)
|
2020-08-27 18:23:41 +00:00
|
|
|
|
2022-03-22 17:06:21 +00:00
|
|
|
if (let res = secureCreatePath(string conf.outSecretsDir); res.isErr):
|
2020-08-27 18:23:41 +00:00
|
|
|
warn "Could not create secrets folder",
|
2022-03-22 17:06:21 +00:00
|
|
|
path = string conf.outSecretsDir, err = ioErrorMsg(res.error)
|
2020-07-27 19:26:30 +00:00
|
|
|
|
|
|
|
let deposits = generateDeposits(
|
2021-07-13 14:27:10 +00:00
|
|
|
cfg,
|
2020-07-27 19:26:30 +00:00
|
|
|
rng[],
|
2020-10-19 19:02:48 +00:00
|
|
|
seed,
|
2021-07-13 14:27:10 +00:00
|
|
|
0, conf.simulationDepositsCount,
|
|
|
|
string conf.outValidatorsDir,
|
2022-05-10 00:32:12 +00:00
|
|
|
string conf.outSecretsDir,
|
|
|
|
conf.remoteSignersUrls,
|
2023-03-05 01:40:21 +00:00
|
|
|
threshold,
|
2022-05-10 00:32:12 +00:00
|
|
|
conf.remoteValidatorsCount,
|
|
|
|
KeystoreMode.Fast)
|
2020-07-27 19:26:30 +00:00
|
|
|
|
|
|
|
if deposits.isErr:
|
|
|
|
fatal "Failed to generate deposits", err = deposits.error
|
|
|
|
quit 1
|
|
|
|
|
|
|
|
let launchPadDeposits =
|
2021-07-13 14:27:10 +00:00
|
|
|
mapIt(deposits.value, LaunchPadDeposit.init(cfg, it))
|
2020-07-27 19:26:30 +00:00
|
|
|
|
2021-07-13 14:27:10 +00:00
|
|
|
Json.saveFile(string conf.outDepositsFile, launchPadDeposits)
|
|
|
|
notice "Deposit data written", filename = conf.outDepositsFile
|
2020-07-27 19:26:30 +00:00
|
|
|
quit 0
|
2020-07-17 20:59:50 +00:00
|
|
|
|
|
|
|
var deposits: seq[LaunchPadDeposit]
|
2021-07-13 14:27:10 +00:00
|
|
|
if conf.cmd == StartUpCommand.sendDeposits:
|
|
|
|
deposits = Json.loadFile(string conf.depositsFile, seq[LaunchPadDeposit])
|
2020-07-17 20:59:50 +00:00
|
|
|
|
2021-07-13 14:27:10 +00:00
|
|
|
if conf.askForKey:
|
2020-07-17 20:59:50 +00:00
|
|
|
var
|
2022-03-24 21:44:34 +00:00
|
|
|
privateKey: string # TODO consider using a SecretString type
|
2020-07-17 20:59:50 +00:00
|
|
|
reasonForKey = ""
|
|
|
|
|
2021-07-13 14:27:10 +00:00
|
|
|
if conf.cmd == StartUpCommand.sendDeposits:
|
2020-07-17 20:59:50 +00:00
|
|
|
let
|
|
|
|
depositsWord = if deposits.len > 1: "deposits" else: "deposit"
|
|
|
|
totalEthNeeded = 32 * deposits.len
|
|
|
|
reasonForKey = " in order to make your $1 (you'll need access to $2 ETH)" %
|
|
|
|
[depositsWord, $totalEthNeeded]
|
|
|
|
|
|
|
|
echo "Please enter your Goerli Eth1 private key in hex form (e.g. 0x1a2...f3c)" &
|
|
|
|
reasonForKey
|
|
|
|
|
|
|
|
if not readPasswordFromStdin("> ", privateKey):
|
|
|
|
error "Failed to read an Eth1 private key from standard input"
|
|
|
|
|
|
|
|
if privateKey.len > 0:
|
2021-07-13 14:27:10 +00:00
|
|
|
conf.privateKey = privateKey.string
|
2020-07-17 20:59:50 +00:00
|
|
|
|
2021-07-13 14:27:10 +00:00
|
|
|
let web3 = await initWeb3(conf.web3Url, conf.privateKey)
|
2019-10-03 09:21:28 +00:00
|
|
|
|
2021-07-13 14:27:10 +00:00
|
|
|
case conf.cmd
|
2019-10-03 09:21:28 +00:00
|
|
|
of StartUpCommand.deploy:
|
2020-06-28 21:06:43 +00:00
|
|
|
let receipt = await web3.deployContract(contractCode)
|
2020-07-02 15:14:11 +00:00
|
|
|
echo receipt.contractAddress.get, ";", receipt.blockHash
|
2020-07-17 20:59:50 +00:00
|
|
|
|
2019-10-03 09:21:28 +00:00
|
|
|
of StartUpCommand.sendEth:
|
2021-07-13 14:27:10 +00:00
|
|
|
echo await sendEth(web3, conf.toAddress, conf.valueEth.parseInt)
|
2019-10-03 09:21:28 +00:00
|
|
|
|
2020-07-27 19:26:30 +00:00
|
|
|
of StartUpCommand.sendDeposits:
|
2020-07-17 20:59:50 +00:00
|
|
|
var delayGenerator: DelayGenerator
|
2021-07-13 14:27:10 +00:00
|
|
|
if not (conf.maxDelay > 0.0):
|
|
|
|
conf.maxDelay = conf.minDelay
|
|
|
|
elif conf.minDelay > conf.maxDelay:
|
2020-07-28 20:37:12 +00:00
|
|
|
echo "The minimum delay should not be larger than the maximum delay"
|
|
|
|
quit 1
|
|
|
|
|
2021-07-13 14:27:10 +00:00
|
|
|
if conf.maxDelay > 0.0:
|
2020-09-14 14:50:03 +00:00
|
|
|
delayGenerator = proc (): chronos.Duration =
|
2021-03-23 06:57:10 +00:00
|
|
|
let
|
2021-07-13 14:27:10 +00:00
|
|
|
minDelay = (conf.minDelay*1000).int64
|
|
|
|
maxDelay = (conf.maxDelay*1000).int64
|
2021-03-23 06:57:10 +00:00
|
|
|
chronos.milliseconds (rng[].rand(maxDelay - minDelay) + minDelay)
|
2020-07-17 20:59:50 +00:00
|
|
|
|
2021-07-13 14:27:10 +00:00
|
|
|
await sendDeposits(deposits, conf.web3Url, conf.privateKey,
|
|
|
|
conf.depositContractAddress, delayGenerator)
|
2020-07-17 20:59:50 +00:00
|
|
|
|
2020-07-27 19:26:30 +00:00
|
|
|
of StartUpCommand.generateSimulationDeposits:
|
|
|
|
# This is handled above before the case statement
|
|
|
|
discard
|
|
|
|
|
2019-10-03 09:21:28 +00:00
|
|
|
when isMainModule: waitFor main()
|