2019-12-07 12:54:10 -07:00
|
|
|
use solana_sdk::{
|
2020-01-10 16:57:31 -07:00
|
|
|
account::Account,
|
2020-01-22 17:54:06 -08:00
|
|
|
account_utils::StateMut,
|
2020-03-05 07:40:26 -07:00
|
|
|
fee_calculator::FeeCalculator,
|
2020-01-10 16:57:31 -07:00
|
|
|
hash::Hash,
|
2020-09-29 21:34:07 -06:00
|
|
|
nonce::{state::Versions, State},
|
2019-12-07 12:54:10 -07:00
|
|
|
};
|
|
|
|
|
2020-01-22 16:31:39 -07:00
|
|
|
pub fn verify_nonce_account(acc: &Account, hash: &Hash) -> bool {
|
2020-03-03 19:39:09 -07:00
|
|
|
match StateMut::<Versions>::state(acc).map(|v| v.convert_to_current()) {
|
2020-03-04 09:51:48 -07:00
|
|
|
Ok(State::Initialized(ref data)) => *hash == data.blockhash,
|
2019-12-07 12:54:10 -07:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-05 07:40:26 -07:00
|
|
|
pub fn fee_calculator_of(account: &Account) -> Option<FeeCalculator> {
|
|
|
|
let state = StateMut::<Versions>::state(account)
|
|
|
|
.ok()?
|
|
|
|
.convert_to_current();
|
|
|
|
match state {
|
|
|
|
State::Initialized(data) => Some(data.fee_calculator),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-07 12:54:10 -07:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use solana_sdk::{
|
2020-03-03 18:00:39 -07:00
|
|
|
account_utils::State as AccountUtilsState,
|
2019-12-07 12:54:10 -07:00
|
|
|
hash::Hash,
|
2020-09-29 21:34:07 -06:00
|
|
|
nonce::{account::with_test_keyed_account, Account as NonceAccount, State},
|
2019-12-07 12:54:10 -07:00
|
|
|
sysvar::{recent_blockhashes::create_test_recent_blockhashes, rent::Rent},
|
|
|
|
};
|
|
|
|
use std::collections::HashSet;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn verify_nonce_ok() {
|
|
|
|
with_test_keyed_account(42, true, |nonce_account| {
|
|
|
|
let mut signers = HashSet::new();
|
2020-06-09 01:38:14 +01:00
|
|
|
signers.insert(nonce_account.signer_key().unwrap());
|
2020-03-03 18:00:39 -07:00
|
|
|
let state: State = nonce_account.state().unwrap();
|
2019-12-07 12:54:10 -07:00
|
|
|
// New is in Uninitialzed state
|
2020-03-03 18:00:39 -07:00
|
|
|
assert_eq!(state, State::Uninitialized);
|
2019-12-07 12:54:10 -07:00
|
|
|
let recent_blockhashes = create_test_recent_blockhashes(0);
|
2020-05-15 17:35:43 +01:00
|
|
|
let authorized = nonce_account.unsigned_key();
|
2019-12-07 12:54:10 -07:00
|
|
|
nonce_account
|
2020-01-22 16:31:39 -07:00
|
|
|
.initialize_nonce_account(&authorized, &recent_blockhashes, &Rent::free())
|
2019-12-07 12:54:10 -07:00
|
|
|
.unwrap();
|
2020-01-22 16:31:39 -07:00
|
|
|
assert!(verify_nonce_account(
|
2020-01-22 09:11:56 -08:00
|
|
|
&nonce_account.account.borrow(),
|
2020-03-04 12:01:32 -07:00
|
|
|
&recent_blockhashes[0].blockhash,
|
2020-01-22 09:11:56 -08:00
|
|
|
));
|
2019-12-07 12:54:10 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn verify_nonce_bad_acc_state_fail() {
|
|
|
|
with_test_keyed_account(42, true, |nonce_account| {
|
2020-01-22 16:31:39 -07:00
|
|
|
assert!(!verify_nonce_account(
|
2020-01-22 09:11:56 -08:00
|
|
|
&nonce_account.account.borrow(),
|
|
|
|
&Hash::default()
|
|
|
|
));
|
2019-12-07 12:54:10 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn verify_nonce_bad_query_hash_fail() {
|
|
|
|
with_test_keyed_account(42, true, |nonce_account| {
|
|
|
|
let mut signers = HashSet::new();
|
2020-06-09 01:38:14 +01:00
|
|
|
signers.insert(nonce_account.signer_key().unwrap());
|
2020-03-03 18:00:39 -07:00
|
|
|
let state: State = nonce_account.state().unwrap();
|
2019-12-07 12:54:10 -07:00
|
|
|
// New is in Uninitialzed state
|
2020-03-03 18:00:39 -07:00
|
|
|
assert_eq!(state, State::Uninitialized);
|
2019-12-07 12:54:10 -07:00
|
|
|
let recent_blockhashes = create_test_recent_blockhashes(0);
|
2020-05-15 17:35:43 +01:00
|
|
|
let authorized = nonce_account.unsigned_key();
|
2019-12-07 12:54:10 -07:00
|
|
|
nonce_account
|
2020-01-22 16:31:39 -07:00
|
|
|
.initialize_nonce_account(&authorized, &recent_blockhashes, &Rent::free())
|
2019-12-07 12:54:10 -07:00
|
|
|
.unwrap();
|
2020-01-22 16:31:39 -07:00
|
|
|
assert!(!verify_nonce_account(
|
2020-01-22 09:11:56 -08:00
|
|
|
&nonce_account.account.borrow(),
|
2020-03-04 12:01:32 -07:00
|
|
|
&recent_blockhashes[1].blockhash,
|
2019-12-07 12:54:10 -07:00
|
|
|
));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|