Add lcli tool for checking deposit data (#940)

* Add check-deposit-data tool

* Update help text

* Update function name
This commit is contained in:
Paul Hauner
2020-04-01 17:40:32 +11:00
committed by GitHub
parent 11a238900a
commit 5b984ad394
9 changed files with 158 additions and 19 deletions

View File

@@ -0,0 +1,31 @@
use crate::helpers::{parse_hex_bytes, parse_u64};
use clap::ArgMatches;
use deposit_contract::{decode_eth1_tx_data, DEPOSIT_DATA_LEN};
use tree_hash::TreeHash;
use types::EthSpec;
pub fn run<T: EthSpec>(matches: &ArgMatches) -> Result<(), String> {
let rlp_bytes = parse_hex_bytes(matches, "deposit-data")?;
let amount = parse_u64(matches, "deposit-amount")?;
if rlp_bytes.len() != DEPOSIT_DATA_LEN {
return Err(format!(
"The given deposit-data is {} bytes, expected {}",
rlp_bytes.len(),
DEPOSIT_DATA_LEN
));
}
let (deposit_data, root) = decode_eth1_tx_data(&rlp_bytes, amount)
.map_err(|e| format!("Invalid deposit data bytes: {:?}", e))?;
let expected_root = deposit_data.tree_hash_root();
if root != expected_root {
return Err(format!(
"Deposit data root is invalid. Expected {:?}, but got {:?}. Perhaps the amount is incorrect?",
expected_root, root
));
}
Ok(())
}

View File

@@ -83,3 +83,16 @@ pub fn parse_fork_opt(matches: &ArgMatches, name: &'static str) -> Result<Option
})
.transpose()
}
pub fn parse_hex_bytes(matches: &ArgMatches, name: &'static str) -> Result<Vec<u8>, String> {
matches
.value_of(name)
.ok_or_else(|| format!("{} not specified", name))
.and_then(|val| {
if val.starts_with("0x") {
hex::decode(&val[2..]).map_err(|e| format!("Unable to parse {}: {:?}", name, e))
} else {
Err(format!("Unable to parse {}, must have 0x prefix", name))
}
})
}

View File

@@ -2,6 +2,7 @@
extern crate log;
mod change_genesis_time;
mod check_deposit_data;
mod deploy_deposit_contract;
mod eth1_genesis;
mod helpers;
@@ -359,6 +360,29 @@ fn main() {
optimization for nodes, please do it."),
)
)
.subcommand(
SubCommand::with_name("check-deposit-data")
.about(
"Checks the integrity of some deposit data.",
)
.arg(
Arg::with_name("deposit-amount")
.index(1)
.value_name("GWEI")
.takes_value(true)
.required(true)
.help("The amount (in Gwei) that was deposited"),
)
.arg(
Arg::with_name("deposit-data")
.index(2)
.value_name("HEX")
.takes_value(true)
.required(true)
.help("A 0x-prefixed hex string of the deposit data. Should include the
function signature."),
)
)
.get_matches();
macro_rules! run_with_spec {
@@ -445,6 +469,8 @@ fn run<T: EthSpec>(env_builder: EnvironmentBuilder<T>, matches: &ArgMatches) {
.unwrap_or_else(|e| error!("Failed to run change-genesis-time command: {}", e)),
("new-testnet", Some(matches)) => new_testnet::run::<T>(matches)
.unwrap_or_else(|e| error!("Failed to run new_testnet command: {}", e)),
("check-deposit-data", Some(matches)) => check_deposit_data::run::<T>(matches)
.unwrap_or_else(|e| error!("Failed to run check-deposit-data command: {}", e)),
(other, _) => error!("Unknown subcommand {}. See --help.", other),
}
}