Handle replicator errors without panicking (#4957)

* Handle replicator errors without panicking

* Typo

* Handle error with match instead of if-let

* Discard error
This commit is contained in:
Sagar Dhawan
2019-07-08 11:23:21 -07:00
committed by GitHub
parent 6e51babff9
commit 4d49820188

View File

@ -466,7 +466,15 @@ impl Replicator {
// check if the storage account exists // check if the storage account exists
let balance = client.poll_get_balance(&storage_keypair.pubkey()); let balance = client.poll_get_balance(&storage_keypair.pubkey());
if balance.is_err() || balance.unwrap() == 0 { if balance.is_err() || balance.unwrap() == 0 {
let (blockhash, _fee_calculator) = client.get_recent_blockhash().expect("blockhash"); let blockhash = match client.get_recent_blockhash() {
Ok((blockhash, _)) => blockhash,
Err(_) => {
return Err(Error::IO(<io::Error>::new(
io::ErrorKind::Other,
"unable to get recent blockhash, can't submit proof",
)))
}
};
let ix = storage_instruction::create_replicator_storage_account( let ix = storage_instruction::create_replicator_storage_account(
&keypair.pubkey(), &keypair.pubkey(),
@ -493,16 +501,25 @@ impl Replicator {
// No point if we've got no storage account... // No point if we've got no storage account...
let nodes = self.cluster_info.read().unwrap().tvu_peers(); let nodes = self.cluster_info.read().unwrap().tvu_peers();
let client = crate::gossip_service::get_client(&nodes); let client = crate::gossip_service::get_client(&nodes);
assert!( let storage_balance = client.poll_get_balance(&self.storage_keypair.pubkey());
client if storage_balance.is_err() || storage_balance.unwrap() == 0 {
.poll_get_balance(&self.storage_keypair.pubkey()) error!("Unable to submit mining proof, no storage account");
.unwrap() return;
> 0 }
);
// ...or no lamports for fees // ...or no lamports for fees
assert!(client.poll_get_balance(&self.keypair.pubkey()).unwrap() > 0); let balance = client.poll_get_balance(&self.keypair.pubkey());
if balance.is_err() || balance.unwrap() == 0 {
error!("Unable to submit mining proof, insufficient Replicator Account balance");
return;
}
let (blockhash, _) = client.get_recent_blockhash().expect("No recent blockhash"); let blockhash = match client.get_recent_blockhash() {
Ok((blockhash, _)) => blockhash,
Err(_) => {
error!("unable to get recent blockhash, can't submit proof");
return;
}
};
let instruction = storage_instruction::mining_proof( let instruction = storage_instruction::mining_proof(
&self.storage_keypair.pubkey(), &self.storage_keypair.pubkey(),
self.sha_state, self.sha_state,
@ -516,14 +533,14 @@ impl Replicator {
message, message,
blockhash, blockhash,
); );
client if let Err(err) = client.send_and_confirm_transaction(
.send_and_confirm_transaction( &[&self.keypair, &self.storage_keypair],
&[&self.keypair, &self.storage_keypair], &mut transaction,
&mut transaction, 10,
10, 0,
0, ) {
) error!("Error: {:?}; while sending mining proof", err);
.expect("transfer didn't work!"); }
} }
pub fn close(self) { pub fn close(self) {