Consolidate Nonce state under one struct (#8624)

automerge
This commit is contained in:
Trent Nelson
2020-03-04 09:51:48 -07:00
committed by GitHub
parent 8f60f1093a
commit 1cc7131bb7
12 changed files with 180 additions and 163 deletions

View File

@ -3261,10 +3261,11 @@ 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 data =
nonce::state::Versions::new_current(nonce::State::Initialized(nonce::state::Data {
authority: config.signers[0].pubkey(),
blockhash,
}));
let nonce_response = json!(Response {
context: RpcResponseContext { slot: 1 },
value: json!(RpcAccount::encode(
@ -3288,10 +3289,11 @@ 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 data =
nonce::state::Versions::new_current(nonce::State::Initialized(nonce::state::Data {
authority: bob_pubkey,
blockhash,
}));
let nonce_authority_response = json!(Response {
context: RpcResponseContext { slot: 1 },
value: json!(RpcAccount::encode(

View File

@ -367,10 +367,10 @@ pub fn check_nonce_account(
.map(|v| v.convert_to_current())
.map_err(|_| Box::new(CliError::InvalidNonce(CliNonceError::InvalidAccountData)))?;
match nonce_state {
State::Initialized(meta, hash) => {
if &hash != nonce_hash {
State::Initialized(ref data) => {
if &data.blockhash != nonce_hash {
Err(CliError::InvalidNonce(CliNonceError::InvalidHash).into())
} else if nonce_authority != &meta.nonce_authority {
} else if nonce_authority != &data.authority {
Err(CliError::InvalidNonce(CliNonceError::InvalidAuthority).into())
} else {
Ok(())
@ -496,7 +496,7 @@ pub fn process_get_nonce(rpc_client: &RpcClient, nonce_account_pubkey: &Pubkey)
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)),
Ok(State::Initialized(ref data)) => Ok(format!("{:?}", data.blockhash)),
Err(err) => Err(CliError::RpcRequestError(format!(
"Account data could not be deserialized to nonce state: {:?}",
err
@ -553,7 +553,7 @@ pub fn process_show_nonce_account(
))
.into());
}
let print_account = |data: Option<(nonce::state::Meta, Hash)>| {
let print_account = |data: Option<&nonce::state::Data>| {
println!(
"Balance: {}",
build_balance_message(nonce_account.lamports, use_lamports_unit, true)
@ -567,9 +567,9 @@ pub fn process_show_nonce_account(
)
);
match data {
Some((meta, hash)) => {
println!("Nonce: {}", hash);
println!("Authority: {}", meta.nonce_authority);
Some(ref data) => {
println!("Nonce: {}", data.blockhash);
println!("Authority: {}", data.authority);
}
None => {
println!("Nonce: uninitialized");
@ -581,7 +581,7 @@ pub fn process_show_nonce_account(
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))),
Ok(State::Initialized(ref data)) => print_account(Some(data)),
Err(err) => Err(CliError::RpcRequestError(format!(
"Account data could not be deserialized to nonce state: {:?}",
err
@ -903,10 +903,10 @@ mod tests {
fn test_check_nonce_account() {
let blockhash = Hash::default();
let nonce_pubkey = Pubkey::new_rand();
let data = Versions::new_current(State::Initialized(
nonce::state::Meta::new(&nonce_pubkey),
let data = Versions::new_current(State::Initialized(nonce::state::Data {
authority: nonce_pubkey,
blockhash,
));
}));
let valid = Account::new_data(1, &data, &system_program::ID);
assert!(check_nonce_account(&valid.unwrap(), &nonce_pubkey, &blockhash).is_ok());
@ -926,20 +926,20 @@ mod tests {
))),
);
let data = Versions::new_current(State::Initialized(
nonce::state::Meta::new(&nonce_pubkey),
hash(b"invalid"),
));
let data = Versions::new_current(State::Initialized(nonce::state::Data {
authority: nonce_pubkey,
blockhash: 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 data = Versions::new_current(State::Initialized(
nonce::state::Meta::new(&Pubkey::new_rand()),
let data = Versions::new_current(State::Initialized(nonce::state::Data {
authority: 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),

View File

@ -413,7 +413,7 @@ fn test_nonced_pay_tx() {
.unwrap()
.convert_to_current();
let nonce_hash = match nonce_state {
nonce::State::Initialized(_meta, hash) => hash,
nonce::State::Initialized(ref data) => data.blockhash,
_ => panic!("Nonce is not initialized"),
};
@ -437,7 +437,7 @@ fn test_nonced_pay_tx() {
.unwrap()
.convert_to_current();
match nonce_state {
nonce::State::Initialized(_meta, hash) => assert_ne!(hash, nonce_hash),
nonce::State::Initialized(ref data) => assert_ne!(data.blockhash, nonce_hash),
_ => assert!(false, "Nonce is not initialized"),
}

View File

@ -504,7 +504,7 @@ fn test_nonced_stake_delegation_and_deactivation() {
.unwrap()
.convert_to_current();
let nonce_hash = match nonce_state {
nonce::State::Initialized(_meta, hash) => hash,
nonce::State::Initialized(ref data) => data.blockhash,
_ => panic!("Nonce is not initialized"),
};
@ -529,7 +529,7 @@ fn test_nonced_stake_delegation_and_deactivation() {
.unwrap()
.convert_to_current();
let nonce_hash = match nonce_state {
nonce::State::Initialized(_meta, hash) => hash,
nonce::State::Initialized(ref data) => data.blockhash,
_ => panic!("Nonce is not initialized"),
};
@ -722,7 +722,7 @@ fn test_stake_authorize() {
.unwrap()
.convert_to_current();
let nonce_hash = match nonce_state {
nonce::State::Initialized(_meta, hash) => hash,
nonce::State::Initialized(ref data) => data.blockhash,
_ => panic!("Nonce is not initialized"),
};
@ -773,7 +773,7 @@ fn test_stake_authorize() {
.unwrap()
.convert_to_current();
let new_nonce_hash = match nonce_state {
nonce::State::Initialized(_meta, hash) => hash,
nonce::State::Initialized(ref data) => data.blockhash,
_ => panic!("Nonce is not initialized"),
};
assert_ne!(nonce_hash, new_nonce_hash);
@ -1009,7 +1009,7 @@ fn test_stake_split() {
.unwrap()
.convert_to_current();
let nonce_hash = match nonce_state {
nonce::State::Initialized(_meta, hash) => hash,
nonce::State::Initialized(ref data) => data.blockhash,
_ => panic!("Nonce is not initialized"),
};
@ -1262,7 +1262,7 @@ fn test_stake_set_lockup() {
.unwrap()
.convert_to_current();
let nonce_hash = match nonce_state {
nonce::State::Initialized(_meta, hash) => hash,
nonce::State::Initialized(ref data) => data.blockhash,
_ => panic!("Nonce is not initialized"),
};
@ -1378,7 +1378,7 @@ fn test_offline_nonced_create_stake_account_and_withdraw() {
.unwrap()
.convert_to_current();
let nonce_hash = match nonce_state {
nonce::State::Initialized(_meta, hash) => hash,
nonce::State::Initialized(ref data) => data.blockhash,
_ => panic!("Nonce is not initialized"),
};
@ -1428,7 +1428,7 @@ fn test_offline_nonced_create_stake_account_and_withdraw() {
.unwrap()
.convert_to_current();
let nonce_hash = match nonce_state {
nonce::State::Initialized(_meta, hash) => hash,
nonce::State::Initialized(ref data) => data.blockhash,
_ => panic!("Nonce is not initialized"),
};
@ -1471,7 +1471,7 @@ fn test_offline_nonced_create_stake_account_and_withdraw() {
.unwrap()
.convert_to_current();
let nonce_hash = match nonce_state {
nonce::State::Initialized(_meta, hash) => hash,
nonce::State::Initialized(ref data) => data.blockhash,
_ => panic!("Nonce is not initialized"),
};

View File

@ -138,7 +138,7 @@ fn test_transfer() {
.unwrap()
.convert_to_current();
let nonce_hash = match nonce_state {
nonce::State::Initialized(_meta, hash) => hash,
nonce::State::Initialized(ref data) => data.blockhash,
_ => panic!("Nonce is not initialized"),
};
@ -162,7 +162,7 @@ fn test_transfer() {
.unwrap()
.convert_to_current();
let new_nonce_hash = match nonce_state {
nonce::State::Initialized(_meta, hash) => hash,
nonce::State::Initialized(ref data) => data.blockhash,
_ => panic!("Nonce is not initialized"),
};
assert_ne!(nonce_hash, new_nonce_hash);
@ -183,7 +183,7 @@ fn test_transfer() {
.unwrap()
.convert_to_current();
let nonce_hash = match nonce_state {
nonce::State::Initialized(_meta, hash) => hash,
nonce::State::Initialized(ref data) => data.blockhash,
_ => panic!("Nonce is not initialized"),
};