Re-wire the sending of deposits by the 'deposits create' command

This commit is contained in:
Zahary Karadjov 2020-06-24 16:57:09 +03:00 committed by zah
parent 384e512031
commit 62e4efe34b
3 changed files with 36 additions and 24 deletions

View File

@ -7,7 +7,7 @@
import import
# Standard library # Standard library
algorithm, os, tables, random, strutils, times, math, terminal, algorithm, os, tables, strutils, times, math, terminal,
# Nimble packages # Nimble packages
stew/[objects, byteutils], stew/shims/macros, stew/[objects, byteutils], stew/shims/macros,
@ -1233,33 +1233,25 @@ programMain:
createDir(config.outValidatorsDir) createDir(config.outValidatorsDir)
createDir(config.outSecretsDir) createDir(config.outSecretsDir)
discard generateDeposits( let deposits = generateDeposits(
config.totalDeposits, config.totalDeposits,
config.outValidatorsDir, config.outValidatorsDir,
config.outSecretsDir) config.outSecretsDir)
if deposits.isErr:
fatal "Failed to generate deposits", err = deposits.error
quit 1
if not config.dontSend:
waitFor sendDeposits(config, deposits.value)
of DepositsCmd.send: of DepositsCmd.send:
if config.minDelay > config.maxDelay: if config.minDelay > config.maxDelay:
echo "The minimum delay should not be larger than the maximum delay" echo "The minimum delay should not be larger than the maximum delay"
quit 1 quit 1
let deposits = loadDeposits(config.depositsDir) let deposits = loadDeposits(config.depositsDir)
waitFor sendDeposits(config, deposits)
var delayGenerator: DelayGenerator
if config.maxDelay > 0.0:
delayGenerator = proc (): chronos.Duration {.gcsafe.} =
chronos.milliseconds (rand(config.minDelay..config.maxDelay)*1000).int
info "Sending deposits",
web3 = config.web3Url,
depositContract = config.depositContractAddress
waitFor sendDeposits(
deposits,
config.web3Url,
config.depositContractAddress,
config.depositPrivateKey,
delayGenerator)
of DepositsCmd.status: of DepositsCmd.status:
# TODO # TODO

View File

@ -282,6 +282,11 @@ type
discard discard
of deposits: of deposits:
depositPrivateKey* {.
defaultValue: ""
desc: "Private key of the controlling (sending) account",
name: "deposit-private-key" }: string
case depositsCmd* {.command.}: DepositsCmd case depositsCmd* {.command.}: DepositsCmd
of DepositsCmd.create: of DepositsCmd.create:
totalDeposits* {. totalDeposits* {.
@ -303,11 +308,6 @@ type
desc: "Output folder for randomly generated keystore passphrases" desc: "Output folder for randomly generated keystore passphrases"
name: "out-secrets-dir" }: string name: "out-secrets-dir" }: string
depositPrivateKey* {.
defaultValue: ""
desc: "Private key of the controlling (sending) account",
name: "deposit-private-key" }: string
dontSend* {. dontSend* {.
defaultValue: false, defaultValue: false,
desc: "By default, all created deposits are also immediately sent " & desc: "By default, all created deposits are also immediately sent " &

View File

@ -1,5 +1,5 @@
import import
os, strutils, terminal, os, strutils, terminal, random,
chronicles, chronos, blscurve, nimcrypto, json_serialization, serialization, chronicles, chronos, blscurve, nimcrypto, json_serialization, serialization,
web3, stint, eth/keys, confutils, web3, stint, eth/keys, confutils,
spec/[datatypes, digest, crypto, keystore], conf, ssz/merkleization, merkle_minimal spec/[datatypes, digest, crypto, keystore], conf, ssz/merkleization, merkle_minimal
@ -162,6 +162,8 @@ proc loadDeposits*(depositsDir: string): seq[Deposit] =
{.pop.} {.pop.}
# TODO: async functions should note take `seq` inputs because
# this leads to full copies.
proc sendDeposits*(deposits: seq[Deposit], proc sendDeposits*(deposits: seq[Deposit],
web3Url, depositContractAddress, privateKey: string, web3Url, depositContractAddress, privateKey: string,
delayGenerator: DelayGenerator = nil) {.async.} = delayGenerator: DelayGenerator = nil) {.async.} =
@ -190,3 +192,21 @@ proc sendDeposits*(deposits: seq[Deposit],
if delayGenerator != nil: if delayGenerator != nil:
await sleepAsync(delayGenerator()) await sleepAsync(delayGenerator())
proc sendDeposits*(config: BeaconNodeConf,
deposits: seq[Deposit]) {.async.} =
var delayGenerator: DelayGenerator
if config.maxDelay > 0.0:
delayGenerator = proc (): chronos.Duration {.gcsafe.} =
chronos.milliseconds (rand(config.minDelay..config.maxDelay)*1000).int
info "Sending deposits",
web3 = config.web3Url,
depositContract = config.depositContractAddress
await sendDeposits(
deposits,
config.web3Url,
config.depositContractAddress,
config.depositPrivateKey,
delayGenerator)