block_hash => blockhash

This commit is contained in:
Michael Vines
2019-03-02 10:25:16 -08:00
committed by Greg Fitzgerald
parent 0f1582c196
commit a94880574b
55 changed files with 591 additions and 597 deletions

View File

@@ -401,7 +401,7 @@ fn process_deploy(
}
}
let block_hash = get_recent_block_hash(&rpc_client)?;
let blockhash = get_recent_blockhash(&rpc_client)?;
let program_id = Keypair::new();
let mut file = File::open(program_location).map_err(|err| {
WalletError::DynamicProgramError(
@@ -418,7 +418,7 @@ fn process_deploy(
let mut tx = SystemTransaction::new_program_account(
&config.id,
program_id.pubkey(),
block_hash,
blockhash,
1,
program_userdata.len() as u64,
bpf_loader::id(),
@@ -439,7 +439,7 @@ fn process_deploy(
bpf_loader::id(),
(i * USERDATA_CHUNK_SIZE) as u32,
chunk.to_vec(),
block_hash,
blockhash,
0,
)
})
@@ -447,7 +447,7 @@ fn process_deploy(
send_and_confirm_transactions(&rpc_client, write_transactions, &program_id)?;
trace!("Finalizing program account");
let mut tx = LoaderTransaction::new_finalize(&program_id, bpf_loader::id(), block_hash, 0);
let mut tx = LoaderTransaction::new_finalize(&program_id, bpf_loader::id(), blockhash, 0);
send_and_confirm_transaction(&rpc_client, &mut tx, &program_id).map_err(|_| {
WalletError::DynamicProgramError("Program finalize transaction failed".to_string())
})?;
@@ -468,10 +468,10 @@ fn process_pay(
witnesses: &Option<Vec<Pubkey>>,
cancelable: Option<Pubkey>,
) -> ProcessResult {
let block_hash = get_recent_block_hash(&rpc_client)?;
let blockhash = get_recent_blockhash(&rpc_client)?;
if timestamp == None && *witnesses == None {
let mut tx = SystemTransaction::new_account(&config.id, to, tokens, block_hash, 0);
let mut tx = SystemTransaction::new_account(&config.id, to, tokens, blockhash, 0);
let signature_str = send_and_confirm_transaction(&rpc_client, &mut tx, &config.id)?;
Ok(signature_str.to_string())
} else if *witnesses == None {
@@ -489,7 +489,7 @@ fn process_pay(
let mut tx = SystemTransaction::new_program_account(
&config.id,
contract_funds.pubkey(),
block_hash,
blockhash,
tokens,
0,
budget_program_id,
@@ -501,7 +501,7 @@ fn process_pay(
let mut tx = SystemTransaction::new_program_account(
&config.id,
contract_state.pubkey(),
block_hash,
blockhash,
1,
196,
budget_program_id,
@@ -518,7 +518,7 @@ fn process_pay(
dt_pubkey,
cancelable,
tokens,
block_hash,
blockhash,
);
let signature_str = send_and_confirm_transaction(&rpc_client, &mut tx, &config.id)?;
@@ -528,7 +528,7 @@ fn process_pay(
})
.to_string())
} else if timestamp == None {
let block_hash = get_recent_block_hash(&rpc_client)?;
let blockhash = get_recent_blockhash(&rpc_client)?;
let witness = if let Some(ref witness_vec) = *witnesses {
witness_vec[0]
@@ -546,7 +546,7 @@ fn process_pay(
let mut tx = SystemTransaction::new_program_account(
&config.id,
contract_funds.pubkey(),
block_hash,
blockhash,
tokens,
0,
budget_program_id,
@@ -558,7 +558,7 @@ fn process_pay(
let mut tx = SystemTransaction::new_program_account(
&config.id,
contract_state.pubkey(),
block_hash,
blockhash,
1,
196,
budget_program_id,
@@ -574,7 +574,7 @@ fn process_pay(
witness,
cancelable,
tokens,
block_hash,
blockhash,
);
let signature_str = send_and_confirm_transaction(&rpc_client, &mut tx, &config.id)?;
@@ -589,9 +589,9 @@ fn process_pay(
}
fn process_cancel(rpc_client: &RpcClient, config: &WalletConfig, pubkey: Pubkey) -> ProcessResult {
let block_hash = get_recent_block_hash(&rpc_client)?;
let blockhash = get_recent_blockhash(&rpc_client)?;
let mut tx =
BudgetTransaction::new_signature(&config.id, pubkey, config.id.pubkey(), block_hash);
BudgetTransaction::new_signature(&config.id, pubkey, config.id.pubkey(), blockhash);
let signature_str = send_and_confirm_transaction(&rpc_client, &mut tx, &config.id)?;
Ok(signature_str.to_string())
}
@@ -622,9 +622,9 @@ fn process_time_elapsed(
request_and_confirm_airdrop(&rpc_client, &drone_addr, &config.id, 1)?;
}
let block_hash = get_recent_block_hash(&rpc_client)?;
let blockhash = get_recent_blockhash(&rpc_client)?;
let mut tx = BudgetTransaction::new_timestamp(&config.id, pubkey, to, dt, block_hash);
let mut tx = BudgetTransaction::new_timestamp(&config.id, pubkey, to, dt, blockhash);
let signature_str = send_and_confirm_transaction(&rpc_client, &mut tx, &config.id)?;
Ok(signature_str.to_string())
@@ -643,8 +643,8 @@ fn process_witness(
request_and_confirm_airdrop(&rpc_client, &drone_addr, &config.id, 1)?;
}
let block_hash = get_recent_block_hash(&rpc_client)?;
let mut tx = BudgetTransaction::new_signature(&config.id, pubkey, to, block_hash);
let blockhash = get_recent_blockhash(&rpc_client)?;
let mut tx = BudgetTransaction::new_signature(&config.id, pubkey, to, blockhash);
let signature_str = send_and_confirm_transaction(&rpc_client, &mut tx, &config.id)?;
Ok(signature_str.to_string())
@@ -714,45 +714,45 @@ pub fn process_command(config: &WalletConfig) -> ProcessResult {
}
}
fn get_recent_block_hash(rpc_client: &RpcClient) -> Result<Hash, Box<dyn error::Error>> {
let result = rpc_client.retry_make_rpc_request(1, &RpcRequest::GetRecentBlockHash, None, 5)?;
fn get_recent_blockhash(rpc_client: &RpcClient) -> Result<Hash, Box<dyn error::Error>> {
let result = rpc_client.retry_make_rpc_request(1, &RpcRequest::GetRecentBlockhash, None, 5)?;
if result.as_str().is_none() {
Err(WalletError::RpcRequestError(
"Received bad block_hash".to_string(),
"Received bad blockhash".to_string(),
))?
}
let block_hash_str = result.as_str().unwrap();
let block_hash_vec = bs58::decode(block_hash_str)
let blockhash_str = result.as_str().unwrap();
let blockhash_vec = bs58::decode(blockhash_str)
.into_vec()
.map_err(|_| WalletError::RpcRequestError("Received bad block_hash".to_string()))?;
Ok(Hash::new(&block_hash_vec))
.map_err(|_| WalletError::RpcRequestError("Received bad blockhash".to_string()))?;
Ok(Hash::new(&blockhash_vec))
}
fn get_next_block_hash(
fn get_next_blockhash(
rpc_client: &RpcClient,
previous_block_hash: &Hash,
previous_blockhash: &Hash,
) -> Result<Hash, Box<dyn error::Error>> {
let mut next_block_hash_retries = 3;
let mut next_blockhash_retries = 3;
loop {
let next_block_hash = get_recent_block_hash(rpc_client)?;
let next_blockhash = get_recent_blockhash(rpc_client)?;
if cfg!(not(test)) {
if next_block_hash != *previous_block_hash {
return Ok(next_block_hash);
if next_blockhash != *previous_blockhash {
return Ok(next_blockhash);
}
} else {
// When using MockRpcClient, get_recent_block_hash() returns a constant value
return Ok(next_block_hash);
// When using MockRpcClient, get_recent_blockhash() returns a constant value
return Ok(next_blockhash);
}
if next_block_hash_retries == 0 {
if next_blockhash_retries == 0 {
Err(WalletError::RpcRequestError(
format!(
"Unable to fetch new block_hash, block_hash stuck at {:?}",
next_block_hash
"Unable to fetch new blockhash, blockhash stuck at {:?}",
next_blockhash
)
.to_string(),
))?;
}
next_block_hash_retries -= 1;
next_blockhash_retries -= 1;
// Retry ~twice during a slot
sleep(Duration::from_millis(
500 * DEFAULT_TICKS_PER_SLOT / NUM_TICKS_PER_SECOND,
@@ -823,7 +823,7 @@ fn send_and_confirm_transaction(
};
match status {
RpcSignatureStatus::AccountInUse | RpcSignatureStatus::SignatureNotFound => {
// Fetch a new block_hash and re-sign the transaction before sending it again
// Fetch a new blockhash and re-sign the transaction before sending it again
resign_transaction(rpc_client, transaction, signer)?;
send_retries -= 1;
}
@@ -900,13 +900,13 @@ fn send_and_confirm_transactions(
}
send_retries -= 1;
// Re-sign any failed transactions with a new block_hash and retry
let block_hash =
get_next_block_hash(rpc_client, &transactions_signatures[0].0.recent_block_hash)?;
// Re-sign any failed transactions with a new blockhash and retry
let blockhash =
get_next_blockhash(rpc_client, &transactions_signatures[0].0.recent_blockhash)?;
transactions = transactions_signatures
.into_iter()
.map(|(mut transaction, _)| {
transaction.sign(&[signer], block_hash);
transaction.sign(&[signer], blockhash);
transaction
})
.collect();
@@ -918,8 +918,8 @@ fn resign_transaction(
tx: &mut Transaction,
signer_key: &Keypair,
) -> Result<(), Box<dyn error::Error>> {
let block_hash = get_next_block_hash(rpc_client, &tx.recent_block_hash)?;
tx.sign(&[signer_key], block_hash);
let blockhash = get_next_blockhash(rpc_client, &tx.recent_blockhash)?;
tx.sign(&[signer_key], blockhash);
Ok(())
}
@@ -929,8 +929,8 @@ pub fn request_and_confirm_airdrop(
signer: &Keypair,
tokens: u64,
) -> Result<(), Box<dyn error::Error>> {
let block_hash = get_recent_block_hash(rpc_client)?;
let mut tx = request_airdrop_transaction(drone_addr, &signer.pubkey(), tokens, block_hash)?;
let blockhash = get_recent_blockhash(rpc_client)?;
let mut tx = request_airdrop_transaction(drone_addr, &signer.pubkey(), tokens, blockhash)?;
send_and_confirm_transaction(rpc_client, &mut tx, signer)?;
Ok(())
}
@@ -1507,19 +1507,19 @@ mod tests {
}
#[test]
fn test_wallet_get_recent_block_hash() {
fn test_wallet_get_recent_blockhash() {
let rpc_client = RpcClient::new("succeeds".to_string());
let vec = bs58::decode(PUBKEY).into_vec().unwrap();
let expected_block_hash = Hash::new(&vec);
let expected_blockhash = Hash::new(&vec);
let block_hash = get_recent_block_hash(&rpc_client);
assert_eq!(block_hash.unwrap(), expected_block_hash);
let blockhash = get_recent_blockhash(&rpc_client);
assert_eq!(blockhash.unwrap(), expected_blockhash);
let rpc_client = RpcClient::new("fails".to_string());
let block_hash = get_recent_block_hash(&rpc_client);
assert!(block_hash.is_err());
let blockhash = get_recent_blockhash(&rpc_client);
assert!(blockhash.is_err());
}
#[test]
@@ -1528,8 +1528,8 @@ mod tests {
let key = Keypair::new();
let to = Keypair::new().pubkey();
let block_hash = Hash::default();
let tx = SystemTransaction::new_account(&key, to, 50, block_hash, 0);
let blockhash = Hash::default();
let tx = SystemTransaction::new_account(&key, to, 50, blockhash, 0);
let signature = send_transaction(&rpc_client, &tx);
assert_eq!(signature.unwrap(), SIGNATURE.to_string());
@@ -1564,8 +1564,8 @@ mod tests {
let key = Keypair::new();
let to = Keypair::new().pubkey();
let block_hash = Hash::default();
let mut tx = SystemTransaction::new_account(&key, to, 50, block_hash, 0);
let blockhash = Hash::default();
let mut tx = SystemTransaction::new_account(&key, to, 50, blockhash, 0);
let signer = Keypair::new();
@@ -1590,15 +1590,15 @@ mod tests {
let vec = bs58::decode("HUu3LwEzGRsUkuJS121jzkPJW39Kq62pXCTmTa1F9jDL")
.into_vec()
.unwrap();
let block_hash = Hash::new(&vec);
let prev_tx = SystemTransaction::new_account(&key, to, 50, block_hash, 0);
let mut tx = SystemTransaction::new_account(&key, to, 50, block_hash, 0);
let blockhash = Hash::new(&vec);
let prev_tx = SystemTransaction::new_account(&key, to, 50, blockhash, 0);
let mut tx = SystemTransaction::new_account(&key, to, 50, blockhash, 0);
resign_transaction(&rpc_client, &mut tx, &key).unwrap();
assert_ne!(prev_tx, tx);
assert_ne!(prev_tx.signatures, tx.signatures);
assert_ne!(prev_tx.recent_block_hash, tx.recent_block_hash);
assert_ne!(prev_tx.recent_blockhash, tx.recent_blockhash);
assert_eq!(prev_tx.fee, tx.fee);
assert_eq!(prev_tx.account_keys, tx.account_keys);
assert_eq!(prev_tx.instructions, tx.instructions);