mirror of
https://github.com/sigp/lighthouse.git
synced 2026-05-08 09:16:00 +00:00
Add password option to lcli deploy command
This commit is contained in:
@@ -9,7 +9,7 @@ use rayon::prelude::*;
|
|||||||
use slog::{crit, error, info, Logger};
|
use slog::{crit, error, info, Logger};
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::prelude::*;
|
use std::io::Read;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use types::{ChainSpec, EthSpec};
|
use types::{ChainSpec, EthSpec};
|
||||||
use validator_client::validator_directory::{ValidatorDirectory, ValidatorDirectoryBuilder};
|
use validator_client::validator_directory::{ValidatorDirectory, ValidatorDirectoryBuilder};
|
||||||
|
|||||||
@@ -2,6 +2,8 @@ use clap::ArgMatches;
|
|||||||
use environment::Environment;
|
use environment::Environment;
|
||||||
use eth1_test_rig::DepositContract;
|
use eth1_test_rig::DepositContract;
|
||||||
use eth2_testnet::Eth2TestnetDir;
|
use eth2_testnet::Eth2TestnetDir;
|
||||||
|
use std::fs::File;
|
||||||
|
use std::io::Read;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use types::EthSpec;
|
use types::EthSpec;
|
||||||
use web3::{transports::Http, Web3};
|
use web3::{transports::Http, Web3};
|
||||||
@@ -34,6 +36,29 @@ pub fn run<T: EthSpec>(mut env: Environment<T>, matches: &ArgMatches) -> Result<
|
|||||||
.expect("should locate home directory")
|
.expect("should locate home directory")
|
||||||
});
|
});
|
||||||
|
|
||||||
|
let password = if let Some(password_path) = matches.value_of("password") {
|
||||||
|
Some(
|
||||||
|
File::open(password_path)
|
||||||
|
.map_err(|e| format!("Unable to open password file: {:?}", e))
|
||||||
|
.and_then(|mut file| {
|
||||||
|
let mut password = String::new();
|
||||||
|
file.read_to_string(&mut password)
|
||||||
|
.map_err(|e| format!("Unable to read password file to string: {:?}", e))
|
||||||
|
.map(|_| password)
|
||||||
|
})
|
||||||
|
.map(|password| {
|
||||||
|
// Trim the linefeed from the end.
|
||||||
|
if password.ends_with("\n") {
|
||||||
|
password[0..password.len() - 1].to_string()
|
||||||
|
} else {
|
||||||
|
password
|
||||||
|
}
|
||||||
|
})?,
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
|
||||||
let endpoint = matches
|
let endpoint = matches
|
||||||
.value_of("endpoint")
|
.value_of("endpoint")
|
||||||
.ok_or_else(|| "Endpoint not specified")?;
|
.ok_or_else(|| "Endpoint not specified")?;
|
||||||
@@ -71,7 +96,11 @@ pub fn run<T: EthSpec>(mut env: Environment<T>, matches: &ArgMatches) -> Result<
|
|||||||
|
|
||||||
let deposit_contract = env
|
let deposit_contract = env
|
||||||
.runtime()
|
.runtime()
|
||||||
.block_on(DepositContract::deploy_testnet(web3, confirmations))
|
.block_on(DepositContract::deploy_testnet(
|
||||||
|
web3,
|
||||||
|
confirmations,
|
||||||
|
password,
|
||||||
|
))
|
||||||
.map_err(|e| format!("Failed to deploy contract: {}", e))?;
|
.map_err(|e| format!("Failed to deploy contract: {}", e))?;
|
||||||
|
|
||||||
info!(
|
info!(
|
||||||
|
|||||||
@@ -150,6 +150,13 @@ fn main() {
|
|||||||
.default_value("3")
|
.default_value("3")
|
||||||
.help("The number of block confirmations before declaring the contract deployed."),
|
.help("The number of block confirmations before declaring the contract deployed."),
|
||||||
)
|
)
|
||||||
|
.arg(
|
||||||
|
Arg::with_name("password")
|
||||||
|
.long("password")
|
||||||
|
.value_name("FILE")
|
||||||
|
.takes_value(true)
|
||||||
|
.help("The password file to unlock the eth1 account (see --index)"),
|
||||||
|
)
|
||||||
)
|
)
|
||||||
.subcommand(
|
.subcommand(
|
||||||
SubCommand::with_name("pycli")
|
SubCommand::with_name("pycli")
|
||||||
|
|||||||
@@ -8,7 +8,7 @@
|
|||||||
mod ganache;
|
mod ganache;
|
||||||
|
|
||||||
use deposit_contract::{eth1_tx_data, testnet, ABI, BYTECODE, CONTRACT_DEPLOY_GAS, DEPOSIT_GAS};
|
use deposit_contract::{eth1_tx_data, testnet, ABI, BYTECODE, CONTRACT_DEPLOY_GAS, DEPOSIT_GAS};
|
||||||
use futures::{stream, Future, IntoFuture, Stream};
|
use futures::{future, stream, Future, IntoFuture, Stream};
|
||||||
use ganache::GanacheInstance;
|
use ganache::GanacheInstance;
|
||||||
use std::time::{Duration, Instant};
|
use std::time::{Duration, Instant};
|
||||||
use tokio::{runtime::Runtime, timer::Delay};
|
use tokio::{runtime::Runtime, timer::Delay};
|
||||||
@@ -17,7 +17,7 @@ use types::{EthSpec, Hash256, Keypair, Signature};
|
|||||||
use web3::contract::{Contract, Options};
|
use web3::contract::{Contract, Options};
|
||||||
use web3::transports::Http;
|
use web3::transports::Http;
|
||||||
use web3::types::{Address, TransactionRequest, U256};
|
use web3::types::{Address, TransactionRequest, U256};
|
||||||
use web3::{Transport, Web3};
|
use web3::Web3;
|
||||||
|
|
||||||
pub const DEPLOYER_ACCOUNTS_INDEX: usize = 0;
|
pub const DEPLOYER_ACCOUNTS_INDEX: usize = 0;
|
||||||
pub const DEPOSIT_ACCOUNTS_INDEX: usize = 0;
|
pub const DEPOSIT_ACCOUNTS_INDEX: usize = 0;
|
||||||
@@ -31,7 +31,7 @@ pub struct GanacheEth1Instance {
|
|||||||
impl GanacheEth1Instance {
|
impl GanacheEth1Instance {
|
||||||
pub fn new() -> impl Future<Item = Self, Error = String> {
|
pub fn new() -> impl Future<Item = Self, Error = String> {
|
||||||
GanacheInstance::new().into_future().and_then(|ganache| {
|
GanacheInstance::new().into_future().and_then(|ganache| {
|
||||||
DepositContract::deploy(ganache.web3.clone(), 0).map(|deposit_contract| Self {
|
DepositContract::deploy(ganache.web3.clone(), 0, None).map(|deposit_contract| Self {
|
||||||
ganache,
|
ganache,
|
||||||
deposit_contract,
|
deposit_contract,
|
||||||
})
|
})
|
||||||
@@ -58,15 +58,23 @@ impl DepositContract {
|
|||||||
pub fn deploy(
|
pub fn deploy(
|
||||||
web3: Web3<Http>,
|
web3: Web3<Http>,
|
||||||
confirmations: usize,
|
confirmations: usize,
|
||||||
|
password: Option<String>,
|
||||||
) -> impl Future<Item = Self, Error = String> {
|
) -> impl Future<Item = Self, Error = String> {
|
||||||
Self::deploy_bytecode(web3, confirmations, BYTECODE, ABI)
|
Self::deploy_bytecode(web3, confirmations, BYTECODE, ABI, password)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn deploy_testnet(
|
pub fn deploy_testnet(
|
||||||
web3: Web3<Http>,
|
web3: Web3<Http>,
|
||||||
confirmations: usize,
|
confirmations: usize,
|
||||||
|
password: Option<String>,
|
||||||
) -> impl Future<Item = Self, Error = String> {
|
) -> impl Future<Item = Self, Error = String> {
|
||||||
Self::deploy_bytecode(web3, confirmations, testnet::BYTECODE, testnet::ABI)
|
Self::deploy_bytecode(
|
||||||
|
web3,
|
||||||
|
confirmations,
|
||||||
|
testnet::BYTECODE,
|
||||||
|
testnet::ABI,
|
||||||
|
password,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deploy_bytecode(
|
fn deploy_bytecode(
|
||||||
@@ -74,21 +82,28 @@ impl DepositContract {
|
|||||||
confirmations: usize,
|
confirmations: usize,
|
||||||
bytecode: &[u8],
|
bytecode: &[u8],
|
||||||
abi: &[u8],
|
abi: &[u8],
|
||||||
|
password: Option<String>,
|
||||||
) -> impl Future<Item = Self, Error = String> {
|
) -> impl Future<Item = Self, Error = String> {
|
||||||
let web3_1 = web3.clone();
|
let web3_1 = web3.clone();
|
||||||
|
|
||||||
deploy_deposit_contract(web3.clone(), confirmations, bytecode.to_vec(), abi.to_vec())
|
deploy_deposit_contract(
|
||||||
.map_err(|e| {
|
web3.clone(),
|
||||||
format!(
|
confirmations,
|
||||||
"Failed to deploy contract: {}. Is scripts/ganache_tests_node.sh running?.",
|
bytecode.to_vec(),
|
||||||
e
|
abi.to_vec(),
|
||||||
)
|
password,
|
||||||
})
|
)
|
||||||
.and_then(move |address| {
|
.map_err(|e| {
|
||||||
Contract::from_json(web3_1.eth(), address, ABI)
|
format!(
|
||||||
.map_err(|e| format!("Failed to init contract: {:?}", e))
|
"Failed to deploy contract: {}. Is scripts/ganache_tests_node.sh running?.",
|
||||||
})
|
e
|
||||||
.map(|contract| Self { contract, web3 })
|
)
|
||||||
|
})
|
||||||
|
.and_then(move |address| {
|
||||||
|
Contract::from_json(web3_1.eth(), address, ABI)
|
||||||
|
.map_err(|e| format!("Failed to init contract: {:?}", e))
|
||||||
|
})
|
||||||
|
.map(|contract| Self { contract, web3 })
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The deposit contract's address in `0x00ab...` format.
|
/// The deposit contract's address in `0x00ab...` format.
|
||||||
@@ -224,13 +239,15 @@ fn from_gwei(gwei: u64) -> U256 {
|
|||||||
|
|
||||||
/// Deploys the deposit contract to the given web3 instance using the account with index
|
/// Deploys the deposit contract to the given web3 instance using the account with index
|
||||||
/// `DEPLOYER_ACCOUNTS_INDEX`.
|
/// `DEPLOYER_ACCOUNTS_INDEX`.
|
||||||
fn deploy_deposit_contract<T: Transport>(
|
fn deploy_deposit_contract(
|
||||||
web3: Web3<T>,
|
web3: Web3<Http>,
|
||||||
confirmations: usize,
|
confirmations: usize,
|
||||||
bytecode: Vec<u8>,
|
bytecode: Vec<u8>,
|
||||||
abi: Vec<u8>,
|
abi: Vec<u8>,
|
||||||
|
password_opt: Option<String>,
|
||||||
) -> impl Future<Item = Address, Error = String> {
|
) -> impl Future<Item = Address, Error = String> {
|
||||||
let bytecode = String::from_utf8(bytecode).expect("bytecode must be valid utf8");
|
let bytecode = String::from_utf8(bytecode).expect("bytecode must be valid utf8");
|
||||||
|
let web3_1 = web3.clone();
|
||||||
|
|
||||||
web3.eth()
|
web3.eth()
|
||||||
.accounts()
|
.accounts()
|
||||||
@@ -241,6 +258,28 @@ fn deploy_deposit_contract<T: Transport>(
|
|||||||
.cloned()
|
.cloned()
|
||||||
.ok_or_else(|| "Insufficient accounts for deployer".to_string())
|
.ok_or_else(|| "Insufficient accounts for deployer".to_string())
|
||||||
})
|
})
|
||||||
|
.and_then(move |from_address| {
|
||||||
|
let future: Box<dyn Future<Item = Address, Error = String> + Send> =
|
||||||
|
if let Some(password) = password_opt {
|
||||||
|
// Unlock for only a single transaction.
|
||||||
|
let duration = None;
|
||||||
|
|
||||||
|
let future = web3_1
|
||||||
|
.personal()
|
||||||
|
.unlock_account(from_address, &password, duration)
|
||||||
|
.then(move |result| match result {
|
||||||
|
Ok(true) => Ok(from_address),
|
||||||
|
Ok(false) => Err("Eth1 node refused to unlock account".to_string()),
|
||||||
|
Err(e) => Err(format!("Eth1 unlock request failed: {:?}", e)),
|
||||||
|
});
|
||||||
|
|
||||||
|
Box::new(future)
|
||||||
|
} else {
|
||||||
|
Box::new(future::ok(from_address))
|
||||||
|
};
|
||||||
|
|
||||||
|
future
|
||||||
|
})
|
||||||
.and_then(move |deploy_address| {
|
.and_then(move |deploy_address| {
|
||||||
Contract::deploy(web3.eth(), &abi)
|
Contract::deploy(web3.eth(), &abi)
|
||||||
.map_err(|e| format!("Unable to build contract deployer: {:?}", e))?
|
.map_err(|e| format!("Unable to build contract deployer: {:?}", e))?
|
||||||
|
|||||||
Reference in New Issue
Block a user