Add get_balance() and get_account_data() to SyncClient
Migrate tests to use them.
This commit is contained in:
parent
5cd7bccdf3
commit
167f5bdc58
@ -172,7 +172,7 @@ mod tests {
|
|||||||
bank_client
|
bank_client
|
||||||
.send_message(&[&alice_keypair], message)
|
.send_message(&[&alice_keypair], message)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!(bank.get_balance(&bob_pubkey), 100);
|
assert_eq!(bank_client.get_balance(&bob_pubkey), 100);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -292,11 +292,11 @@ mod tests {
|
|||||||
bank_client
|
bank_client
|
||||||
.send_message(&[&alice_keypair], message)
|
.send_message(&[&alice_keypair], message)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!(bank.get_balance(&alice_pubkey), 1);
|
assert_eq!(bank_client.get_balance(&alice_pubkey), 1);
|
||||||
assert_eq!(bank.get_balance(&budget_pubkey), 1);
|
assert_eq!(bank_client.get_balance(&budget_pubkey), 1);
|
||||||
|
|
||||||
let contract_account = bank.get_account(&budget_pubkey).unwrap();
|
let contract_account = bank_client.get_account_data(&budget_pubkey).unwrap();
|
||||||
let budget_state = BudgetState::deserialize(&contract_account.data).unwrap();
|
let budget_state = BudgetState::deserialize(&contract_account).unwrap();
|
||||||
assert!(budget_state.is_pending());
|
assert!(budget_state.is_pending());
|
||||||
|
|
||||||
// Attack! Try to payout to mallory_pubkey
|
// Attack! Try to payout to mallory_pubkey
|
||||||
@ -311,12 +311,12 @@ mod tests {
|
|||||||
InstructionError::CustomError(serialize(&BudgetError::DestinationMissing).unwrap())
|
InstructionError::CustomError(serialize(&BudgetError::DestinationMissing).unwrap())
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
assert_eq!(bank.get_balance(&alice_pubkey), 1);
|
assert_eq!(bank_client.get_balance(&alice_pubkey), 1);
|
||||||
assert_eq!(bank.get_balance(&budget_pubkey), 1);
|
assert_eq!(bank_client.get_balance(&budget_pubkey), 1);
|
||||||
assert_eq!(bank.get_balance(&bob_pubkey), 0);
|
assert_eq!(bank_client.get_balance(&bob_pubkey), 0);
|
||||||
|
|
||||||
let contract_account = bank.get_account(&budget_pubkey).unwrap();
|
let contract_account = bank_client.get_account_data(&budget_pubkey).unwrap();
|
||||||
let budget_state = BudgetState::deserialize(&contract_account.data).unwrap();
|
let budget_state = BudgetState::deserialize(&contract_account).unwrap();
|
||||||
assert!(budget_state.is_pending());
|
assert!(budget_state.is_pending());
|
||||||
|
|
||||||
// Now, acknowledge the time in the condition occurred and
|
// Now, acknowledge the time in the condition occurred and
|
||||||
@ -326,10 +326,10 @@ mod tests {
|
|||||||
bank_client
|
bank_client
|
||||||
.send_instruction(&alice_keypair, instruction)
|
.send_instruction(&alice_keypair, instruction)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!(bank.get_balance(&alice_pubkey), 1);
|
assert_eq!(bank_client.get_balance(&alice_pubkey), 1);
|
||||||
assert_eq!(bank.get_balance(&budget_pubkey), 0);
|
assert_eq!(bank_client.get_balance(&budget_pubkey), 0);
|
||||||
assert_eq!(bank.get_balance(&bob_pubkey), 1);
|
assert_eq!(bank_client.get_balance(&bob_pubkey), 1);
|
||||||
assert_eq!(bank.get_account(&budget_pubkey), None);
|
assert_eq!(bank_client.get_account_data(&budget_pubkey), None);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -354,11 +354,11 @@ mod tests {
|
|||||||
bank_client
|
bank_client
|
||||||
.send_message(&[&alice_keypair], message)
|
.send_message(&[&alice_keypair], message)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!(bank.get_balance(&alice_pubkey), 2);
|
assert_eq!(bank_client.get_balance(&alice_pubkey), 2);
|
||||||
assert_eq!(bank.get_balance(&budget_pubkey), 1);
|
assert_eq!(bank_client.get_balance(&budget_pubkey), 1);
|
||||||
|
|
||||||
let contract_account = bank.get_account(&budget_pubkey).unwrap();
|
let contract_account = bank_client.get_account_data(&budget_pubkey).unwrap();
|
||||||
let budget_state = BudgetState::deserialize(&contract_account.data).unwrap();
|
let budget_state = BudgetState::deserialize(&contract_account).unwrap();
|
||||||
assert!(budget_state.is_pending());
|
assert!(budget_state.is_pending());
|
||||||
|
|
||||||
// Attack! try to put the lamports into the wrong account with cancel
|
// Attack! try to put the lamports into the wrong account with cancel
|
||||||
@ -367,7 +367,7 @@ mod tests {
|
|||||||
bank_client
|
bank_client
|
||||||
.transfer(1, &alice_keypair, &mallory_pubkey)
|
.transfer(1, &alice_keypair, &mallory_pubkey)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!(bank.get_balance(&alice_pubkey), 1);
|
assert_eq!(bank_client.get_balance(&alice_pubkey), 1);
|
||||||
|
|
||||||
let instruction =
|
let instruction =
|
||||||
budget_instruction::apply_signature(&mallory_pubkey, &budget_pubkey, &bob_pubkey);
|
budget_instruction::apply_signature(&mallory_pubkey, &budget_pubkey, &bob_pubkey);
|
||||||
@ -375,9 +375,9 @@ mod tests {
|
|||||||
.send_instruction(&mallory_keypair, instruction)
|
.send_instruction(&mallory_keypair, instruction)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
// nothing should be changed because apply witness didn't finalize a payment
|
// nothing should be changed because apply witness didn't finalize a payment
|
||||||
assert_eq!(bank.get_balance(&alice_pubkey), 1);
|
assert_eq!(bank_client.get_balance(&alice_pubkey), 1);
|
||||||
assert_eq!(bank.get_balance(&budget_pubkey), 1);
|
assert_eq!(bank_client.get_balance(&budget_pubkey), 1);
|
||||||
assert_eq!(bank.get_account(&bob_pubkey), None);
|
assert_eq!(bank_client.get_account_data(&bob_pubkey), None);
|
||||||
|
|
||||||
// Now, cancel the transaction. mint gets her funds back
|
// Now, cancel the transaction. mint gets her funds back
|
||||||
let instruction =
|
let instruction =
|
||||||
@ -385,8 +385,8 @@ mod tests {
|
|||||||
bank_client
|
bank_client
|
||||||
.send_instruction(&alice_keypair, instruction)
|
.send_instruction(&alice_keypair, instruction)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!(bank.get_balance(&alice_pubkey), 2);
|
assert_eq!(bank_client.get_balance(&alice_pubkey), 2);
|
||||||
assert_eq!(bank.get_account(&budget_pubkey), None);
|
assert_eq!(bank_client.get_account_data(&budget_pubkey), None);
|
||||||
assert_eq!(bank.get_account(&bob_pubkey), None);
|
assert_eq!(bank_client.get_account_data(&bob_pubkey), None);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -118,10 +118,10 @@ mod tests {
|
|||||||
.send_message(&[&from_keypair, &config_keypair], message)
|
.send_message(&[&from_keypair, &config_keypair], message)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let config_account = bank.get_account(&config_pubkey).unwrap();
|
let config_account_data = bank_client.get_account_data(&config_pubkey).unwrap();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
my_config,
|
my_config,
|
||||||
MyConfig::deserialize(&config_account.data).unwrap()
|
MyConfig::deserialize(&config_account_data).unwrap()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -652,7 +652,7 @@ mod test {
|
|||||||
let (client, owner) = create_client(&bank, mint_keypair);
|
let (client, owner) = create_client(&bank, mint_keypair);
|
||||||
|
|
||||||
let new = create_token_account(&client, &owner);
|
let new = create_token_account(&client, &owner);
|
||||||
let new_account = bank.get_account(&new).unwrap();
|
let new_account_data = client.get_account_data(&new).unwrap();
|
||||||
|
|
||||||
// Check results
|
// Check results
|
||||||
|
|
||||||
@ -660,7 +660,7 @@ mod test {
|
|||||||
TokenAccountInfo::default()
|
TokenAccountInfo::default()
|
||||||
.owner(&owner.pubkey())
|
.owner(&owner.pubkey())
|
||||||
.tokens(100_000, 100_000, 100_000, 100_000),
|
.tokens(100_000, 100_000, 100_000, 100_000),
|
||||||
ExchangeProcessor::deserialize_account(&new_account.data).unwrap()
|
ExchangeProcessor::deserialize_account(&new_account_data).unwrap()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -691,7 +691,7 @@ mod test {
|
|||||||
.send_instruction(&owner, instruction)
|
.send_instruction(&owner, instruction)
|
||||||
.expect(&format!("{}:{}", line!(), file!()));
|
.expect(&format!("{}:{}", line!(), file!()));
|
||||||
|
|
||||||
let new_account = bank.get_account(&new).unwrap();
|
let new_account_data = client.get_account_data(&new).unwrap();
|
||||||
|
|
||||||
// Check results
|
// Check results
|
||||||
|
|
||||||
@ -699,7 +699,7 @@ mod test {
|
|||||||
TokenAccountInfo::default()
|
TokenAccountInfo::default()
|
||||||
.owner(&owner.pubkey())
|
.owner(&owner.pubkey())
|
||||||
.tokens(100_042, 100_000, 100_000, 100_000),
|
.tokens(100_042, 100_000, 100_000, 100_000),
|
||||||
ExchangeProcessor::deserialize_account(&new_account.data).unwrap()
|
ExchangeProcessor::deserialize_account(&new_account_data).unwrap()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -720,9 +720,9 @@ mod test {
|
|||||||
1000,
|
1000,
|
||||||
);
|
);
|
||||||
|
|
||||||
let trade_account = bank.get_account(&trade).unwrap();
|
let trade_account_data = client.get_account_data(&trade).unwrap();
|
||||||
let src_account = bank.get_account(&src).unwrap();
|
let src_account_data = client.get_account_data(&src).unwrap();
|
||||||
let dst_account = bank.get_account(&dst).unwrap();
|
let dst_account_data = client.get_account_data(&dst).unwrap();
|
||||||
|
|
||||||
// check results
|
// check results
|
||||||
|
|
||||||
@ -736,19 +736,19 @@ mod test {
|
|||||||
src_account: src,
|
src_account: src,
|
||||||
dst_account: dst
|
dst_account: dst
|
||||||
},
|
},
|
||||||
ExchangeProcessor::deserialize_trade(&trade_account.data).unwrap()
|
ExchangeProcessor::deserialize_trade(&trade_account_data).unwrap()
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
TokenAccountInfo::default()
|
TokenAccountInfo::default()
|
||||||
.owner(&owner.pubkey())
|
.owner(&owner.pubkey())
|
||||||
.tokens(100_040, 100_000, 100_000, 100_000),
|
.tokens(100_040, 100_000, 100_000, 100_000),
|
||||||
ExchangeProcessor::deserialize_account(&src_account.data).unwrap()
|
ExchangeProcessor::deserialize_account(&src_account_data).unwrap()
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
TokenAccountInfo::default()
|
TokenAccountInfo::default()
|
||||||
.owner(&owner.pubkey())
|
.owner(&owner.pubkey())
|
||||||
.tokens(100_000, 100_000, 100_000, 100_000),
|
.tokens(100_000, 100_000, 100_000, 100_000),
|
||||||
ExchangeProcessor::deserialize_account(&dst_account.data).unwrap()
|
ExchangeProcessor::deserialize_account(&dst_account_data).unwrap()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -794,14 +794,14 @@ mod test {
|
|||||||
.send_instruction(&owner, instruction)
|
.send_instruction(&owner, instruction)
|
||||||
.expect(&format!("{}:{}", line!(), file!()));
|
.expect(&format!("{}:{}", line!(), file!()));
|
||||||
|
|
||||||
let to_trade_account = bank.get_account(&to_trade).unwrap();
|
let to_trade_account_data = client.get_account_data(&to_trade).unwrap();
|
||||||
let to_src_account = bank.get_account(&to_src).unwrap();
|
let to_src_account_data = client.get_account_data(&to_src).unwrap();
|
||||||
let to_dst_account = bank.get_account(&to_dst).unwrap();
|
let to_dst_account_data = client.get_account_data(&to_dst).unwrap();
|
||||||
let from_trade_account = bank.get_account(&from_trade).unwrap();
|
let from_trade_account_data = client.get_account_data(&from_trade).unwrap();
|
||||||
let from_src_account = bank.get_account(&from_src).unwrap();
|
let from_src_account_data = client.get_account_data(&from_src).unwrap();
|
||||||
let from_dst_account = bank.get_account(&from_dst).unwrap();
|
let from_dst_account_data = client.get_account_data(&from_dst).unwrap();
|
||||||
let profit_account = bank.get_account(&profit).unwrap();
|
let profit_account_data = client.get_account_data(&profit).unwrap();
|
||||||
let swap_account = bank.get_account(&swap).unwrap();
|
let swap_account_data = client.get_account_data(&swap).unwrap();
|
||||||
|
|
||||||
// check results
|
// check results
|
||||||
|
|
||||||
@ -815,19 +815,19 @@ mod test {
|
|||||||
src_account: to_src,
|
src_account: to_src,
|
||||||
dst_account: to_dst
|
dst_account: to_dst
|
||||||
},
|
},
|
||||||
ExchangeProcessor::deserialize_trade(&to_trade_account.data).unwrap()
|
ExchangeProcessor::deserialize_trade(&to_trade_account_data).unwrap()
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
TokenAccountInfo::default()
|
TokenAccountInfo::default()
|
||||||
.owner(&owner.pubkey())
|
.owner(&owner.pubkey())
|
||||||
.tokens(100_000, 100_000, 100_000, 100_000),
|
.tokens(100_000, 100_000, 100_000, 100_000),
|
||||||
ExchangeProcessor::deserialize_account(&to_src_account.data).unwrap()
|
ExchangeProcessor::deserialize_account(&to_src_account_data).unwrap()
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
TokenAccountInfo::default()
|
TokenAccountInfo::default()
|
||||||
.owner(&owner.pubkey())
|
.owner(&owner.pubkey())
|
||||||
.tokens(100_000, 100_002, 100_000, 100_000),
|
.tokens(100_000, 100_002, 100_000, 100_000),
|
||||||
ExchangeProcessor::deserialize_account(&to_dst_account.data).unwrap()
|
ExchangeProcessor::deserialize_account(&to_dst_account_data).unwrap()
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
TradeOrderInfo {
|
TradeOrderInfo {
|
||||||
@ -839,25 +839,25 @@ mod test {
|
|||||||
src_account: from_src,
|
src_account: from_src,
|
||||||
dst_account: from_dst
|
dst_account: from_dst
|
||||||
},
|
},
|
||||||
ExchangeProcessor::deserialize_trade(&from_trade_account.data).unwrap()
|
ExchangeProcessor::deserialize_trade(&from_trade_account_data).unwrap()
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
TokenAccountInfo::default()
|
TokenAccountInfo::default()
|
||||||
.owner(&owner.pubkey())
|
.owner(&owner.pubkey())
|
||||||
.tokens(100_000, 100_000, 100_000, 100_000),
|
.tokens(100_000, 100_000, 100_000, 100_000),
|
||||||
ExchangeProcessor::deserialize_account(&from_src_account.data).unwrap()
|
ExchangeProcessor::deserialize_account(&from_src_account_data).unwrap()
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
TokenAccountInfo::default()
|
TokenAccountInfo::default()
|
||||||
.owner(&owner.pubkey())
|
.owner(&owner.pubkey())
|
||||||
.tokens(100_001, 100_000, 100_000, 100_000),
|
.tokens(100_001, 100_000, 100_000, 100_000),
|
||||||
ExchangeProcessor::deserialize_account(&from_dst_account.data).unwrap()
|
ExchangeProcessor::deserialize_account(&from_dst_account_data).unwrap()
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
TokenAccountInfo::default()
|
TokenAccountInfo::default()
|
||||||
.owner(&owner.pubkey())
|
.owner(&owner.pubkey())
|
||||||
.tokens(100_000, 100_001, 100_000, 100_000),
|
.tokens(100_000, 100_001, 100_000, 100_000),
|
||||||
ExchangeProcessor::deserialize_account(&profit_account.data).unwrap()
|
ExchangeProcessor::deserialize_account(&profit_account_data).unwrap()
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
TradeSwapInfo {
|
TradeSwapInfo {
|
||||||
@ -869,7 +869,7 @@ mod test {
|
|||||||
secondary_tokens: 3,
|
secondary_tokens: 3,
|
||||||
secondary_price: 3000,
|
secondary_price: 3000,
|
||||||
},
|
},
|
||||||
deserialize_swap(&swap_account.data)
|
deserialize_swap(&swap_account_data)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -556,10 +556,10 @@ mod tests {
|
|||||||
// assert_eq!(bank.get_balance(&replicator), TOTAL_REPLICATOR_REWARDS);
|
// assert_eq!(bank.get_balance(&replicator), TOTAL_REPLICATOR_REWARDS);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_storage_entry_height(bank: &Bank, account: &Pubkey) -> u64 {
|
fn get_storage_entry_height<C: SyncClient>(client: &C, account: &Pubkey) -> u64 {
|
||||||
match bank.get_account(&account) {
|
match client.get_account_data(&account) {
|
||||||
Some(storage_system_account) => {
|
Some(storage_system_account_data) => {
|
||||||
let contract = deserialize(&storage_system_account.data);
|
let contract = deserialize(&storage_system_account_data);
|
||||||
if let Ok(contract) = contract {
|
if let Ok(contract) = contract {
|
||||||
match contract {
|
match contract {
|
||||||
StorageContract::ValidatorStorage { entry_height, .. } => {
|
StorageContract::ValidatorStorage { entry_height, .. } => {
|
||||||
@ -576,9 +576,9 @@ mod tests {
|
|||||||
0
|
0
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_storage_blockhash(bank: &Bank, account: &Pubkey) -> Hash {
|
fn get_storage_blockhash<C: SyncClient>(client: &C, account: &Pubkey) -> Hash {
|
||||||
if let Some(storage_system_account) = bank.get_account(&account) {
|
if let Some(storage_system_account_data) = client.get_account_data(&account) {
|
||||||
let contract = deserialize(&storage_system_account.data);
|
let contract = deserialize(&storage_system_account_data);
|
||||||
if let Ok(contract) = contract {
|
if let Ok(contract) = contract {
|
||||||
match contract {
|
match contract {
|
||||||
StorageContract::ValidatorStorage { hash, .. } => {
|
StorageContract::ValidatorStorage { hash, .. } => {
|
||||||
@ -652,11 +652,11 @@ mod tests {
|
|||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
get_storage_entry_height(&bank, &validator_pubkey),
|
get_storage_entry_height(&bank_client, &validator_pubkey),
|
||||||
ENTRIES_PER_SEGMENT
|
ENTRIES_PER_SEGMENT
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
get_storage_blockhash(&bank, &validator_pubkey),
|
get_storage_blockhash(&bank_client, &validator_pubkey),
|
||||||
storage_blockhash
|
storage_blockhash
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -73,7 +73,8 @@ mod tests {
|
|||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let ixs = vote_instruction::create_account(&from_keypair.pubkey(), vote_id, lamports);
|
let ixs = vote_instruction::create_account(&from_keypair.pubkey(), vote_id, lamports);
|
||||||
let message = Message::new(ixs);
|
let message = Message::new(ixs);
|
||||||
bank_client.send_message(&[from_keypair], message)
|
bank_client.send_message(&[from_keypair], message)?;
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create_vote_account_with_delegate(
|
fn create_vote_account_with_delegate(
|
||||||
@ -88,7 +89,8 @@ mod tests {
|
|||||||
let delegate_ix = vote_instruction::delegate_stake(&vote_id, delegate_id);
|
let delegate_ix = vote_instruction::delegate_stake(&vote_id, delegate_id);
|
||||||
ixs.push(delegate_ix);
|
ixs.push(delegate_ix);
|
||||||
let message = Message::new(ixs);
|
let message = Message::new(ixs);
|
||||||
bank_client.send_message(&[&from_keypair, vote_keypair], message)
|
bank_client.send_message(&[&from_keypair, vote_keypair], message)?;
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn submit_vote(
|
fn submit_vote(
|
||||||
@ -97,7 +99,8 @@ mod tests {
|
|||||||
tick_height: u64,
|
tick_height: u64,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let vote_ix = vote_instruction::vote(&vote_keypair.pubkey(), Vote::new(tick_height));
|
let vote_ix = vote_instruction::vote(&vote_keypair.pubkey(), Vote::new(tick_height));
|
||||||
bank_client.send_instruction(&vote_keypair, vote_ix)
|
bank_client.send_instruction(&vote_keypair, vote_ix)?;
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -111,8 +114,8 @@ mod tests {
|
|||||||
create_vote_account(&bank_client, &from_keypair, &vote_id, 100).unwrap();
|
create_vote_account(&bank_client, &from_keypair, &vote_id, 100).unwrap();
|
||||||
submit_vote(&bank_client, &vote_keypair, 0).unwrap();
|
submit_vote(&bank_client, &vote_keypair, 0).unwrap();
|
||||||
|
|
||||||
let vote_account = bank.get_account(&vote_id).unwrap();
|
let vote_account_data = bank_client.get_account_data(&vote_id).unwrap();
|
||||||
let vote_state = VoteState::deserialize(&vote_account.data).unwrap();
|
let vote_state = VoteState::deserialize(&vote_account_data).unwrap();
|
||||||
assert_eq!(vote_state.votes.len(), 1);
|
assert_eq!(vote_state.votes.len(), 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -154,8 +157,8 @@ mod tests {
|
|||||||
let result = bank_client.send_message(&[&mallory_keypair], message);
|
let result = bank_client.send_message(&[&mallory_keypair], message);
|
||||||
|
|
||||||
// And ensure there's no vote.
|
// And ensure there's no vote.
|
||||||
let vote_account = bank.get_account(&vote_id).unwrap();
|
let vote_account_data = bank_client.get_account_data(&vote_id).unwrap();
|
||||||
let vote_state = VoteState::deserialize(&vote_account.data).unwrap();
|
let vote_state = VoteState::deserialize(&vote_account_data).unwrap();
|
||||||
assert_eq!(vote_state.votes.len(), 0);
|
assert_eq!(vote_state.votes.len(), 0);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
|
@ -2,6 +2,7 @@ use crate::bank::Bank;
|
|||||||
use solana_sdk::instruction::Instruction;
|
use solana_sdk::instruction::Instruction;
|
||||||
use solana_sdk::message::Message;
|
use solana_sdk::message::Message;
|
||||||
use solana_sdk::pubkey::Pubkey;
|
use solana_sdk::pubkey::Pubkey;
|
||||||
|
use solana_sdk::signature::Signature;
|
||||||
use solana_sdk::signature::{Keypair, KeypairUtil};
|
use solana_sdk::signature::{Keypair, KeypairUtil};
|
||||||
use solana_sdk::sync_client::SyncClient;
|
use solana_sdk::sync_client::SyncClient;
|
||||||
use solana_sdk::system_instruction;
|
use solana_sdk::system_instruction;
|
||||||
@ -16,10 +17,11 @@ impl<'a> SyncClient for BankClient<'a> {
|
|||||||
&self,
|
&self,
|
||||||
keypairs: &[&Keypair],
|
keypairs: &[&Keypair],
|
||||||
message: Message,
|
message: Message,
|
||||||
) -> Result<(), TransactionError> {
|
) -> Result<Signature, TransactionError> {
|
||||||
let blockhash = self.bank.last_blockhash();
|
let blockhash = self.bank.last_blockhash();
|
||||||
let transaction = Transaction::new(&keypairs, message, blockhash);
|
let transaction = Transaction::new(&keypairs, message, blockhash);
|
||||||
self.bank.process_transaction(&transaction)
|
self.bank.process_transaction(&transaction)?;
|
||||||
|
Ok(transaction.signatures.get(0).cloned().unwrap_or_default())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create and process a transaction from a single instruction.
|
/// Create and process a transaction from a single instruction.
|
||||||
@ -27,7 +29,7 @@ impl<'a> SyncClient for BankClient<'a> {
|
|||||||
&self,
|
&self,
|
||||||
keypair: &Keypair,
|
keypair: &Keypair,
|
||||||
instruction: Instruction,
|
instruction: Instruction,
|
||||||
) -> Result<(), TransactionError> {
|
) -> Result<Signature, TransactionError> {
|
||||||
let message = Message::new(vec![instruction]);
|
let message = Message::new(vec![instruction]);
|
||||||
self.send_message(&[keypair], message)
|
self.send_message(&[keypair], message)
|
||||||
}
|
}
|
||||||
@ -38,10 +40,19 @@ impl<'a> SyncClient for BankClient<'a> {
|
|||||||
lamports: u64,
|
lamports: u64,
|
||||||
keypair: &Keypair,
|
keypair: &Keypair,
|
||||||
pubkey: &Pubkey,
|
pubkey: &Pubkey,
|
||||||
) -> Result<(), TransactionError> {
|
) -> Result<Signature, TransactionError> {
|
||||||
let move_instruction = system_instruction::transfer(&keypair.pubkey(), pubkey, lamports);
|
let move_instruction = system_instruction::transfer(&keypair.pubkey(), pubkey, lamports);
|
||||||
self.send_instruction(keypair, move_instruction)
|
self.send_instruction(keypair, move_instruction)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn get_account_data(&self, pubkey: &Pubkey) -> Option<Vec<u8>> {
|
||||||
|
let account = self.bank.get_account(pubkey)?;
|
||||||
|
Some(account.data)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_balance(&self, pubkey: &Pubkey) -> u64 {
|
||||||
|
self.bank.get_balance(pubkey)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> BankClient<'a> {
|
impl<'a> BankClient<'a> {
|
||||||
@ -75,6 +86,6 @@ mod tests {
|
|||||||
|
|
||||||
let message = Message::new(vec![move_instruction]);
|
let message = Message::new(vec![move_instruction]);
|
||||||
bank_client.send_message(&doe_keypairs, message).unwrap();
|
bank_client.send_message(&doe_keypairs, message).unwrap();
|
||||||
assert_eq!(bank.get_balance(&bob_pubkey), 42);
|
assert_eq!(bank_client.get_balance(&bob_pubkey), 42);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -307,7 +307,7 @@ mod tests {
|
|||||||
InstructionError::MissingRequiredSignature
|
InstructionError::MissingRequiredSignature
|
||||||
))
|
))
|
||||||
);
|
);
|
||||||
assert_eq!(bank.get_balance(&alice_pubkey), 50);
|
assert_eq!(bank_client.get_balance(&alice_pubkey), 50);
|
||||||
assert_eq!(bank.get_balance(&mallory_pubkey), 50);
|
assert_eq!(bank_client.get_balance(&mallory_pubkey), 50);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,14 +5,17 @@
|
|||||||
use crate::instruction::Instruction;
|
use crate::instruction::Instruction;
|
||||||
use crate::message::Message;
|
use crate::message::Message;
|
||||||
use crate::pubkey::Pubkey;
|
use crate::pubkey::Pubkey;
|
||||||
use crate::signature::Keypair;
|
use crate::signature::{Keypair, Signature};
|
||||||
use crate::transaction::TransactionError;
|
use crate::transaction::TransactionError;
|
||||||
|
|
||||||
pub trait SyncClient {
|
pub trait SyncClient {
|
||||||
/// Create a transaction from the given message, and send it to the
|
/// Create a transaction from the given message, and send it to the
|
||||||
/// server, retrying as-needed.
|
/// server, retrying as-needed.
|
||||||
fn send_message(&self, keypairs: &[&Keypair], message: Message)
|
fn send_message(
|
||||||
-> Result<(), TransactionError>;
|
&self,
|
||||||
|
keypairs: &[&Keypair],
|
||||||
|
message: Message,
|
||||||
|
) -> Result<Signature, TransactionError>;
|
||||||
|
|
||||||
/// Create a transaction from a single instruction that only requires
|
/// Create a transaction from a single instruction that only requires
|
||||||
/// a single signer. Then send it to the server, retrying as-needed.
|
/// a single signer. Then send it to the server, retrying as-needed.
|
||||||
@ -20,7 +23,7 @@ pub trait SyncClient {
|
|||||||
&self,
|
&self,
|
||||||
keypair: &Keypair,
|
keypair: &Keypair,
|
||||||
instruction: Instruction,
|
instruction: Instruction,
|
||||||
) -> Result<(), TransactionError>;
|
) -> Result<Signature, TransactionError>;
|
||||||
|
|
||||||
/// Transfer lamports from `keypair` to `pubkey`, retrying until the
|
/// Transfer lamports from `keypair` to `pubkey`, retrying until the
|
||||||
/// transfer completes or produces and error.
|
/// transfer completes or produces and error.
|
||||||
@ -29,5 +32,11 @@ pub trait SyncClient {
|
|||||||
lamports: u64,
|
lamports: u64,
|
||||||
keypair: &Keypair,
|
keypair: &Keypair,
|
||||||
pubkey: &Pubkey,
|
pubkey: &Pubkey,
|
||||||
) -> Result<(), TransactionError>;
|
) -> Result<Signature, TransactionError>;
|
||||||
|
|
||||||
|
/// Get an account or None if not found.
|
||||||
|
fn get_account_data(&self, pubkey: &Pubkey) -> Option<Vec<u8>>;
|
||||||
|
|
||||||
|
/// Get account balance or 0 if not found.
|
||||||
|
fn get_balance(&self, pubkey: &Pubkey) -> u64;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user