SDK: Add versioning to nonce state (#8607)

This commit is contained in:
Trent Nelson
2020-03-03 19:39:09 -07:00
committed by GitHub
parent be0cc0273f
commit 1cb6101c6a
9 changed files with 188 additions and 107 deletions

View File

@ -3261,18 +3261,14 @@ mod tests {
// Nonced pay
let blockhash = Hash::default();
let data = nonce::state::Versions::new_current(nonce::State::Initialized(
nonce::state::Meta::new(&config.signers[0].pubkey()),
blockhash,
));
let nonce_response = json!(Response {
context: RpcResponseContext { slot: 1 },
value: json!(RpcAccount::encode(
Account::new_data(
1,
&nonce::State::Initialized(
nonce::state::Meta::new(&config.signers[0].pubkey()),
blockhash
),
&system_program::ID,
)
.unwrap()
Account::new_data(1, &data, &system_program::ID,).unwrap()
)),
});
let mut mocks = HashMap::new();
@ -3292,15 +3288,14 @@ mod tests {
let bob_keypair = Keypair::new();
let bob_pubkey = bob_keypair.pubkey();
let blockhash = Hash::default();
let data = nonce::state::Versions::new_current(nonce::State::Initialized(
nonce::state::Meta::new(&bob_pubkey),
blockhash,
));
let nonce_authority_response = json!(Response {
context: RpcResponseContext { slot: 1 },
value: json!(RpcAccount::encode(
Account::new_data(
1,
&nonce::State::Initialized(nonce::state::Meta::new(&bob_pubkey), blockhash),
&system_program::ID,
)
.unwrap()
Account::new_data(1, &data, &system_program::ID,).unwrap()
)),
});
let mut mocks = HashMap::new();

View File

@ -14,7 +14,7 @@ use solana_sdk::{
account_utils::StateMut,
hash::Hash,
message::Message,
nonce::{self, State},
nonce::{self, state::Versions, State},
pubkey::Pubkey,
system_instruction::{
advance_nonce_account, authorize_nonce_account, create_address_with_seed,
@ -363,8 +363,8 @@ pub fn check_nonce_account(
if nonce_account.owner != system_program::ID {
return Err(CliError::InvalidNonce(CliNonceError::InvalidAccountOwner).into());
}
let nonce_state: State = nonce_account
.state()
let nonce_state = StateMut::<Versions>::state(nonce_account)
.map(|v| v.convert_to_current())
.map_err(|_| Box::new(CliError::InvalidNonce(CliNonceError::InvalidAccountData)))?;
match nonce_state {
State::Initialized(meta, hash) => {
@ -427,7 +427,7 @@ pub fn process_create_nonce_account(
if let Ok(nonce_account) = rpc_client.get_account(&nonce_account_address) {
let err_msg = if nonce_account.owner == system_program::id()
&& StateMut::<State>::state(&nonce_account).is_ok()
&& StateMut::<Versions>::state(&nonce_account).is_ok()
{
format!("Nonce account {} already exists", nonce_account_address)
} else {
@ -493,7 +493,8 @@ pub fn process_get_nonce(rpc_client: &RpcClient, nonce_account_pubkey: &Pubkey)
))
.into());
}
match nonce_account.state() {
let nonce_state = StateMut::<Versions>::state(&nonce_account).map(|v| v.convert_to_current());
match nonce_state {
Ok(State::Uninitialized) => Ok("Nonce account is uninitialized".to_string()),
Ok(State::Initialized(_, hash)) => Ok(format!("{:?}", hash)),
Err(err) => Err(CliError::RpcRequestError(format!(
@ -577,7 +578,8 @@ pub fn process_show_nonce_account(
}
Ok("".to_string())
};
match nonce_account.state() {
let nonce_state = StateMut::<Versions>::state(&nonce_account).map(|v| v.convert_to_current());
match nonce_state {
Ok(State::Uninitialized) => print_account(None),
Ok(State::Initialized(meta, hash)) => print_account(Some((meta, hash))),
Err(err) => Err(CliError::RpcRequestError(format!(
@ -901,18 +903,14 @@ mod tests {
fn test_check_nonce_account() {
let blockhash = Hash::default();
let nonce_pubkey = Pubkey::new_rand();
let valid = Account::new_data(
1,
&State::Initialized(nonce::state::Meta::new(&nonce_pubkey), blockhash),
&system_program::ID,
);
let data = Versions::new_current(State::Initialized(
nonce::state::Meta::new(&nonce_pubkey),
blockhash,
));
let valid = Account::new_data(1, &data, &system_program::ID);
assert!(check_nonce_account(&valid.unwrap(), &nonce_pubkey, &blockhash).is_ok());
let invalid_owner = Account::new_data(
1,
&State::Initialized(nonce::state::Meta::new(&nonce_pubkey), blockhash),
&Pubkey::new(&[1u8; 32]),
);
let invalid_owner = Account::new_data(1, &data, &Pubkey::new(&[1u8; 32]));
assert_eq!(
check_nonce_account(&invalid_owner.unwrap(), &nonce_pubkey, &blockhash),
Err(Box::new(CliError::InvalidNonce(
@ -928,21 +926,21 @@ mod tests {
))),
);
let invalid_hash = Account::new_data(
1,
&State::Initialized(nonce::state::Meta::new(&nonce_pubkey), hash(b"invalid")),
&system_program::ID,
);
let data = Versions::new_current(State::Initialized(
nonce::state::Meta::new(&nonce_pubkey),
hash(b"invalid"),
));
let invalid_hash = Account::new_data(1, &data, &system_program::ID);
assert_eq!(
check_nonce_account(&invalid_hash.unwrap(), &nonce_pubkey, &blockhash),
Err(Box::new(CliError::InvalidNonce(CliNonceError::InvalidHash))),
);
let invalid_authority = Account::new_data(
1,
&State::Initialized(nonce::state::Meta::new(&Pubkey::new_rand()), blockhash),
&system_program::ID,
);
let data = Versions::new_current(State::Initialized(
nonce::state::Meta::new(&Pubkey::new_rand()),
blockhash,
));
let invalid_authority = Account::new_data(1, &data, &system_program::ID);
assert_eq!(
check_nonce_account(&invalid_authority.unwrap(), &nonce_pubkey, &blockhash),
Err(Box::new(CliError::InvalidNonce(
@ -950,7 +948,8 @@ mod tests {
))),
);
let invalid_state = Account::new_data(1, &State::Uninitialized, &system_program::ID);
let data = Versions::new_current(State::Uninitialized);
let invalid_state = Account::new_data(1, &data, &system_program::ID);
assert_eq!(
check_nonce_account(&invalid_state.unwrap(), &nonce_pubkey, &blockhash),
Err(Box::new(CliError::InvalidNonce(

View File

@ -409,7 +409,9 @@ fn test_nonced_pay_tx() {
// Fetch nonce hash
let account = rpc_client.get_account(&nonce_account.pubkey()).unwrap();
let nonce_state: nonce::State = account.state().unwrap();
let nonce_state = StateMut::<nonce::state::Versions>::state(&account)
.unwrap()
.convert_to_current();
let nonce_hash = match nonce_state {
nonce::State::Initialized(_meta, hash) => hash,
_ => panic!("Nonce is not initialized"),
@ -431,7 +433,9 @@ fn test_nonced_pay_tx() {
// Verify that nonce has been used
let account = rpc_client.get_account(&nonce_account.pubkey()).unwrap();
let nonce_state: nonce::State = account.state().unwrap();
let nonce_state = StateMut::<nonce::state::Versions>::state(&account)
.unwrap()
.convert_to_current();
match nonce_state {
nonce::State::Initialized(_meta, hash) => assert_ne!(hash, nonce_hash),
_ => assert!(false, "Nonce is not initialized"),

View File

@ -500,7 +500,9 @@ fn test_nonced_stake_delegation_and_deactivation() {
// Fetch nonce hash
let account = rpc_client.get_account(&nonce_account.pubkey()).unwrap();
let nonce_state: nonce::State = account.state().unwrap();
let nonce_state = StateMut::<nonce::state::Versions>::state(&account)
.unwrap()
.convert_to_current();
let nonce_hash = match nonce_state {
nonce::State::Initialized(_meta, hash) => hash,
_ => panic!("Nonce is not initialized"),
@ -523,7 +525,9 @@ fn test_nonced_stake_delegation_and_deactivation() {
// Fetch nonce hash
let account = rpc_client.get_account(&nonce_account.pubkey()).unwrap();
let nonce_state: nonce::State = account.state().unwrap();
let nonce_state = StateMut::<nonce::state::Versions>::state(&account)
.unwrap()
.convert_to_current();
let nonce_hash = match nonce_state {
nonce::State::Initialized(_meta, hash) => hash,
_ => panic!("Nonce is not initialized"),
@ -714,7 +718,9 @@ fn test_stake_authorize() {
// Fetch nonce hash
let account = rpc_client.get_account(&nonce_account.pubkey()).unwrap();
let nonce_state: nonce::State = account.state().unwrap();
let nonce_state = StateMut::<nonce::state::Versions>::state(&account)
.unwrap()
.convert_to_current();
let nonce_hash = match nonce_state {
nonce::State::Initialized(_meta, hash) => hash,
_ => panic!("Nonce is not initialized"),
@ -763,7 +769,9 @@ fn test_stake_authorize() {
};
assert_eq!(current_authority, online_authority_pubkey);
let account = rpc_client.get_account(&nonce_account.pubkey()).unwrap();
let nonce_state: nonce::State = account.state().unwrap();
let nonce_state = StateMut::<nonce::state::Versions>::state(&account)
.unwrap()
.convert_to_current();
let new_nonce_hash = match nonce_state {
nonce::State::Initialized(_meta, hash) => hash,
_ => panic!("Nonce is not initialized"),
@ -997,7 +1005,9 @@ fn test_stake_split() {
// Fetch nonce hash
let account = rpc_client.get_account(&nonce_account.pubkey()).unwrap();
let nonce_state: nonce::State = account.state().unwrap();
let nonce_state = StateMut::<nonce::state::Versions>::state(&account)
.unwrap()
.convert_to_current();
let nonce_hash = match nonce_state {
nonce::State::Initialized(_meta, hash) => hash,
_ => panic!("Nonce is not initialized"),
@ -1248,7 +1258,9 @@ fn test_stake_set_lockup() {
// Fetch nonce hash
let account = rpc_client.get_account(&nonce_account_pubkey).unwrap();
let nonce_state: nonce::State = account.state().unwrap();
let nonce_state = StateMut::<nonce::state::Versions>::state(&account)
.unwrap()
.convert_to_current();
let nonce_hash = match nonce_state {
nonce::State::Initialized(_meta, hash) => hash,
_ => panic!("Nonce is not initialized"),
@ -1362,7 +1374,9 @@ fn test_offline_nonced_create_stake_account_and_withdraw() {
// Fetch nonce hash
let account = rpc_client.get_account(&nonce_account.pubkey()).unwrap();
let nonce_state: nonce::State = account.state().unwrap();
let nonce_state = StateMut::<nonce::state::Versions>::state(&account)
.unwrap()
.convert_to_current();
let nonce_hash = match nonce_state {
nonce::State::Initialized(_meta, hash) => hash,
_ => panic!("Nonce is not initialized"),
@ -1410,7 +1424,9 @@ fn test_offline_nonced_create_stake_account_and_withdraw() {
// Fetch nonce hash
let account = rpc_client.get_account(&nonce_account.pubkey()).unwrap();
let nonce_state: nonce::State = account.state().unwrap();
let nonce_state = StateMut::<nonce::state::Versions>::state(&account)
.unwrap()
.convert_to_current();
let nonce_hash = match nonce_state {
nonce::State::Initialized(_meta, hash) => hash,
_ => panic!("Nonce is not initialized"),
@ -1451,7 +1467,9 @@ fn test_offline_nonced_create_stake_account_and_withdraw() {
// Fetch nonce hash
let account = rpc_client.get_account(&nonce_account.pubkey()).unwrap();
let nonce_state: nonce::State = account.state().unwrap();
let nonce_state = StateMut::<nonce::state::Versions>::state(&account)
.unwrap()
.convert_to_current();
let nonce_hash = match nonce_state {
nonce::State::Initialized(_meta, hash) => hash,
_ => panic!("Nonce is not initialized"),

View File

@ -134,7 +134,9 @@ fn test_transfer() {
// Fetch nonce hash
let account = rpc_client.get_account(&nonce_account.pubkey()).unwrap();
let nonce_state: nonce::State = account.state().unwrap();
let nonce_state = StateMut::<nonce::state::Versions>::state(&account)
.unwrap()
.convert_to_current();
let nonce_hash = match nonce_state {
nonce::State::Initialized(_meta, hash) => hash,
_ => panic!("Nonce is not initialized"),
@ -156,7 +158,9 @@ fn test_transfer() {
check_balance(49_976 - minimum_nonce_balance, &rpc_client, &sender_pubkey);
check_balance(30, &rpc_client, &recipient_pubkey);
let account = rpc_client.get_account(&nonce_account.pubkey()).unwrap();
let nonce_state: nonce::State = account.state().unwrap();
let nonce_state = StateMut::<nonce::state::Versions>::state(&account)
.unwrap()
.convert_to_current();
let new_nonce_hash = match nonce_state {
nonce::State::Initialized(_meta, hash) => hash,
_ => panic!("Nonce is not initialized"),
@ -175,7 +179,9 @@ fn test_transfer() {
// Fetch nonce hash
let account = rpc_client.get_account(&nonce_account.pubkey()).unwrap();
let nonce_state: nonce::State = account.state().unwrap();
let nonce_state = StateMut::<nonce::state::Versions>::state(&account)
.unwrap()
.convert_to_current();
let nonce_hash = match nonce_state {
nonce::State::Initialized(_meta, hash) => hash,
_ => panic!("Nonce is not initialized"),