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

@@ -14,4 +14,4 @@ serde_json = "1.0"
types = { path = "../../types"}
eth2_ssz = { path = "../ssz"}
tree_hash = { path = "../tree_hash"}
ethabi = "9.0"
ethabi = "11.0"

View File

@@ -1,14 +1,31 @@
use ethabi::{Contract, Token};
use ssz::Encode;
use ssz::{Decode, DecodeError as SszDecodeError, Encode};
use tree_hash::TreeHash;
use types::DepositData;
use types::{DepositData, Hash256, PublicKeyBytes, SignatureBytes};
pub use ethabi::Error;
#[derive(Debug)]
pub enum DecodeError {
EthabiError(ethabi::Error),
SszDecodeError(SszDecodeError),
MissingField,
UnableToGetBytes,
MissingToken,
InadequateBytes,
}
impl From<ethabi::Error> for DecodeError {
fn from(e: ethabi::Error) -> DecodeError {
DecodeError::EthabiError(e)
}
}
pub const CONTRACT_DEPLOY_GAS: usize = 4_000_000;
pub const DEPOSIT_GAS: usize = 4_000_000;
pub const ABI: &[u8] = include_bytes!("../contracts/v0.10.1_validator_registration.json");
pub const BYTECODE: &[u8] = include_bytes!("../contracts/v0.10.1_validator_registration.bytecode");
pub const DEPOSIT_DATA_LEN: usize = 420; // lol
pub mod testnet {
pub const ABI: &[u8] =
@@ -17,7 +34,7 @@ pub mod testnet {
include_bytes!("../contracts/v0.10.1_testnet_validator_registration.bytecode");
}
pub fn eth1_tx_data(deposit_data: &DepositData) -> Result<Vec<u8>, Error> {
pub fn encode_eth1_tx_data(deposit_data: &DepositData) -> Result<Vec<u8>, Error> {
let params = vec![
Token::Bytes(deposit_data.pubkey.as_ssz_bytes()),
Token::Bytes(deposit_data.withdrawal_credentials.as_ssz_bytes()),
@@ -32,6 +49,40 @@ pub fn eth1_tx_data(deposit_data: &DepositData) -> Result<Vec<u8>, Error> {
function.encode_input(&params)
}
pub fn decode_eth1_tx_data(
bytes: &[u8],
amount: u64,
) -> Result<(DepositData, Hash256), DecodeError> {
let abi = Contract::load(ABI)?;
let function = abi.function("deposit")?;
let mut tokens =
function.decode_input(bytes.get(4..).ok_or_else(|| DecodeError::InadequateBytes)?)?;
macro_rules! decode_token {
($type: ty, $to_fn: ident) => {
<$type>::from_ssz_bytes(
&tokens
.pop()
.ok_or_else(|| DecodeError::MissingToken)?
.$to_fn()
.ok_or_else(|| DecodeError::UnableToGetBytes)?,
)
.map_err(DecodeError::SszDecodeError)?
};
};
let root = decode_token!(Hash256, to_fixed_bytes);
let deposit_data = DepositData {
amount,
signature: decode_token!(SignatureBytes, to_bytes),
withdrawal_credentials: decode_token!(Hash256, to_bytes),
pubkey: decode_token!(PublicKeyBytes, to_bytes),
};
Ok((deposit_data, root))
}
#[cfg(test)]
mod tests {
use super::*;
@@ -54,14 +105,27 @@ mod tests {
}
#[test]
fn basic() {
fn round_trip() {
let spec = &E::default_spec();
let keypair = generate_deterministic_keypair(42);
let deposit = get_deposit(keypair, spec);
let original = get_deposit(keypair, spec);
let data = eth1_tx_data(&deposit).expect("should produce tx data");
let data = encode_eth1_tx_data(&original).expect("should produce tx data");
assert_eq!(data.len(), 420, "bytes should be correct length");
assert_eq!(
data.len(),
DEPOSIT_DATA_LEN,
"bytes should be correct length"
);
let (decoded, root) = decode_eth1_tx_data(&data, original.amount).expect("should decode");
assert_eq!(decoded, original, "decoded should match original");
assert_eq!(
root,
original.tree_hash_root(),
"decode root should match original root"
);
}
}