mirror of
https://github.com/sigp/lighthouse.git
synced 2026-05-07 00:42:42 +00:00
Tidy account manager
This commit is contained in:
@@ -38,22 +38,19 @@ fn run_account_manager<T: EthSpec>(
|
|||||||
let context = env.core_context();
|
let context = env.core_context();
|
||||||
let log = context.log.clone();
|
let log = context.log.clone();
|
||||||
|
|
||||||
|
// If the `datadir` was not provided, default to the home directory. If the home directory is
|
||||||
|
// not known, use the current directory.
|
||||||
let datadir = matches
|
let datadir = matches
|
||||||
.value_of("datadir")
|
.value_of("datadir")
|
||||||
.map(PathBuf::from)
|
.map(PathBuf::from)
|
||||||
.unwrap_or_else(|| {
|
.unwrap_or_else(|| {
|
||||||
let mut default_dir = match dirs::home_dir() {
|
dirs::home_dir()
|
||||||
Some(v) => v,
|
.unwrap_or_else(|| PathBuf::from("."))
|
||||||
None => {
|
.join(".lighthouse")
|
||||||
panic!("Failed to find a home directory");
|
.join("validators")
|
||||||
}
|
|
||||||
};
|
|
||||||
default_dir.push(".lighthouse");
|
|
||||||
default_dir.push("validators");
|
|
||||||
default_dir
|
|
||||||
});
|
});
|
||||||
|
|
||||||
fs::create_dir_all(&datadir).map_err(|e| format!("Failed to initialize datadir: {}", e))?;
|
fs::create_dir_all(&datadir).map_err(|e| format!("Failed to create datadir: {}", e))?;
|
||||||
|
|
||||||
info!(
|
info!(
|
||||||
log,
|
log,
|
||||||
@@ -157,7 +154,7 @@ fn run_new_validator_subcommand<T: EthSpec>(
|
|||||||
.map(|_| password)
|
.map(|_| password)
|
||||||
})
|
})
|
||||||
.map(|password| {
|
.map(|password| {
|
||||||
// Trim the linefeed from the end.
|
// Trim the line feed from the end of the password file, if present.
|
||||||
if password.ends_with("\n") {
|
if password.ends_with("\n") {
|
||||||
password[0..password.len() - 1].to_string()
|
password[0..password.len() - 1].to_string()
|
||||||
} else {
|
} else {
|
||||||
@@ -269,6 +266,10 @@ fn make_validators(
|
|||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// For each `ValidatorDirectory`, submit a deposit transaction to the `eth1_endpoint`.
|
||||||
|
///
|
||||||
|
/// Returns success as soon as the eth1 endpoint accepts the transaction (i.e., does not wait for
|
||||||
|
/// transaction success/revert).
|
||||||
fn deposit_validators<E: EthSpec>(
|
fn deposit_validators<E: EthSpec>(
|
||||||
context: RuntimeContext<E>,
|
context: RuntimeContext<E>,
|
||||||
eth1_endpoint: String,
|
eth1_endpoint: String,
|
||||||
@@ -290,6 +291,9 @@ fn deposit_validators<E: EthSpec>(
|
|||||||
)
|
)
|
||||||
})
|
})
|
||||||
.into_future()
|
.into_future()
|
||||||
|
/*
|
||||||
|
* Loop through the validator directories and submit the deposits.
|
||||||
|
*/
|
||||||
.and_then(move |(event_loop, transport)| {
|
.and_then(move |(event_loop, transport)| {
|
||||||
let web3 = Web3::new(transport);
|
let web3 = Web3::new(transport);
|
||||||
|
|
||||||
@@ -318,6 +322,10 @@ fn deposit_validators<E: EthSpec>(
|
|||||||
.map(|event_loop| drop(event_loop))
|
.map(|event_loop| drop(event_loop))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// For the given `ValidatorDirectory`, submit a deposit transaction to the `web3` node.
|
||||||
|
///
|
||||||
|
/// Returns success as soon as the eth1 endpoint accepts the transaction (i.e., does not wait for
|
||||||
|
/// transaction success/revert).
|
||||||
fn deposit_validator(
|
fn deposit_validator(
|
||||||
web3: Web3<Http>,
|
web3: Web3<Http>,
|
||||||
deposit_contract: Address,
|
deposit_contract: Address,
|
||||||
@@ -327,90 +335,103 @@ fn deposit_validator(
|
|||||||
password_opt: Option<String>,
|
password_opt: Option<String>,
|
||||||
log: Logger,
|
log: Logger,
|
||||||
) -> impl Future<Item = (), Error = ()> {
|
) -> impl Future<Item = (), Error = ()> {
|
||||||
let web3_1 = web3.clone();
|
validator
|
||||||
let web3_2 = web3.clone();
|
|
||||||
|
|
||||||
let log_1 = log.clone();
|
|
||||||
let log_2 = log.clone();
|
|
||||||
|
|
||||||
let validator_voting_pubkey_1 = validator
|
|
||||||
.voting_keypair
|
.voting_keypair
|
||||||
.clone()
|
.clone()
|
||||||
.expect("Validators must have a voting public key")
|
.ok_or_else(|| error!(log, "Validator does not have voting keypair"))
|
||||||
.pk;
|
.and_then(|voting_keypair| {
|
||||||
let validator_voting_pubkey_2 = validator_voting_pubkey_1.clone();
|
validator
|
||||||
|
.deposit_data
|
||||||
let deposit_data = validator
|
.clone()
|
||||||
.deposit_data
|
.ok_or_else(|| error!(log, "Validator does not have deposit data"))
|
||||||
.clone()
|
.map(|deposit_data| (voting_keypair, deposit_data))
|
||||||
.expect("Validators must have a deposit data");
|
|
||||||
|
|
||||||
web3.eth()
|
|
||||||
.accounts()
|
|
||||||
.map_err(|e| format!("Failed to get accounts: {:?}", e))
|
|
||||||
.and_then(move |accounts| {
|
|
||||||
accounts
|
|
||||||
.get(account_index)
|
|
||||||
.cloned()
|
|
||||||
.ok_or_else(|| "Insufficient accounts for deposit".to_string())
|
|
||||||
})
|
})
|
||||||
.and_then(move |from_address| {
|
.into_future()
|
||||||
let future: Box<dyn Future<Item = Address, Error = String> + Send> =
|
.and_then(move |(voting_keypair, deposit_data)| {
|
||||||
if let Some(password) = password_opt {
|
let pubkey_1 = voting_keypair.pk.clone();
|
||||||
// Unlock for only a single transaction.
|
let pubkey_2 = voting_keypair.pk.clone();
|
||||||
let duration = None;
|
|
||||||
|
|
||||||
let future = web3_1
|
let web3_1 = web3.clone();
|
||||||
.personal()
|
let web3_2 = web3.clone();
|
||||||
.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)
|
let log_1 = log.clone();
|
||||||
} else {
|
let log_2 = log.clone();
|
||||||
Box::new(future::ok(from_address))
|
web3.eth()
|
||||||
};
|
.accounts()
|
||||||
|
.map_err(|e| format!("Failed to get accounts: {:?}", e))
|
||||||
|
.and_then(move |accounts| {
|
||||||
|
accounts
|
||||||
|
.get(account_index)
|
||||||
|
.cloned()
|
||||||
|
.ok_or_else(|| "Insufficient accounts for deposit".to_string())
|
||||||
|
})
|
||||||
|
/*
|
||||||
|
* If a password was supplied, unlock the account.
|
||||||
|
*/
|
||||||
|
.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;
|
||||||
|
|
||||||
future
|
let future = web3_1
|
||||||
})
|
.personal()
|
||||||
.and_then(move |from| {
|
.unlock_account(from_address, &password, duration)
|
||||||
let tx_request = TransactionRequest {
|
.then(move |result| match result {
|
||||||
from,
|
Ok(true) => Ok(from_address),
|
||||||
to: Some(deposit_contract),
|
Ok(false) => {
|
||||||
gas: Some(U256::from(DEPOSIT_GAS)),
|
Err("Eth1 node refused to unlock account".to_string())
|
||||||
gas_price: None,
|
}
|
||||||
value: Some(U256::from(from_gwei(deposit_amount))),
|
Err(e) => Err(format!("Eth1 unlock request failed: {:?}", e)),
|
||||||
data: Some(deposit_data.into()),
|
});
|
||||||
nonce: None,
|
|
||||||
condition: None,
|
|
||||||
};
|
|
||||||
|
|
||||||
web3_2
|
Box::new(future)
|
||||||
.eth()
|
} else {
|
||||||
.send_transaction(tx_request)
|
Box::new(future::ok(from_address))
|
||||||
.map_err(|e| format!("Failed to call deposit fn: {:?}", e))
|
};
|
||||||
})
|
|
||||||
.map(move |tx| {
|
future
|
||||||
info!(
|
})
|
||||||
log_1,
|
/*
|
||||||
"Validator deposit successful";
|
* Submit the deposit transaction.
|
||||||
"eth1_tx_hash" => format!("{:?}", tx),
|
*/
|
||||||
"validator_voting_pubkey" => format!("{:?}", validator_voting_pubkey_1)
|
.and_then(move |from| {
|
||||||
)
|
let tx_request = TransactionRequest {
|
||||||
})
|
from,
|
||||||
.map_err(move |e| {
|
to: Some(deposit_contract),
|
||||||
error!(
|
gas: Some(U256::from(DEPOSIT_GAS)),
|
||||||
log_2,
|
gas_price: None,
|
||||||
"Validator deposit_failed";
|
value: Some(U256::from(from_gwei(deposit_amount))),
|
||||||
"error" => e,
|
data: Some(deposit_data.into()),
|
||||||
"validator_voting_pubkey" => format!("{:?}", validator_voting_pubkey_2)
|
nonce: None,
|
||||||
)
|
condition: None,
|
||||||
|
};
|
||||||
|
|
||||||
|
web3_2
|
||||||
|
.eth()
|
||||||
|
.send_transaction(tx_request)
|
||||||
|
.map_err(|e| format!("Failed to call deposit fn: {:?}", e))
|
||||||
|
})
|
||||||
|
.map(move |tx| {
|
||||||
|
info!(
|
||||||
|
log_1,
|
||||||
|
"Validator deposit successful";
|
||||||
|
"eth1_tx_hash" => format!("{:?}", tx),
|
||||||
|
"validator_voting_pubkey" => format!("{:?}", pubkey_1)
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.map_err(move |e| {
|
||||||
|
error!(
|
||||||
|
log_2,
|
||||||
|
"Validator deposit_failed";
|
||||||
|
"error" => e,
|
||||||
|
"validator_voting_pubkey" => format!("{:?}", pubkey_2)
|
||||||
|
)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Converts gwei to wei.
|
||||||
fn from_gwei(gwei: u64) -> U256 {
|
fn from_gwei(gwei: u64) -> U256 {
|
||||||
U256::from(gwei) * U256::exp10(9)
|
U256::from(gwei) * U256::exp10(9)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user