2020-10-19 13:19:24 -07:00
|
|
|
use crate::{
|
2021-04-26 10:06:40 -07:00
|
|
|
account::{AccountSharedData, ReadableAccount},
|
2020-01-22 17:54:06 -08:00
|
|
|
account_utils::StateMut,
|
2020-01-10 15:57:31 -08:00
|
|
|
hash::Hash,
|
2020-09-29 20:34:07 -07:00
|
|
|
nonce::{state::Versions, State},
|
2019-12-07 11:54:10 -08:00
|
|
|
};
|
2020-10-28 22:01:07 -07:00
|
|
|
use std::cell::RefCell;
|
|
|
|
|
2021-03-09 13:06:07 -08:00
|
|
|
pub fn create_account(lamports: u64) -> RefCell<AccountSharedData> {
|
2020-10-28 22:01:07 -07:00
|
|
|
RefCell::new(
|
2021-03-09 13:06:07 -08:00
|
|
|
AccountSharedData::new_data_with_space(
|
2020-10-28 22:01:07 -07:00
|
|
|
lamports,
|
|
|
|
&Versions::new_current(State::Uninitialized),
|
|
|
|
State::size(),
|
|
|
|
&crate::system_program::id(),
|
|
|
|
)
|
|
|
|
.expect("nonce_account"),
|
|
|
|
)
|
|
|
|
}
|
2019-12-07 11:54:10 -08:00
|
|
|
|
2021-03-09 13:06:07 -08:00
|
|
|
pub fn verify_nonce_account(acc: &AccountSharedData, hash: &Hash) -> bool {
|
2021-04-26 10:06:40 -07:00
|
|
|
if acc.owner() != &crate::system_program::id() {
|
2020-11-25 16:03:43 -08:00
|
|
|
return false;
|
|
|
|
}
|
2020-03-03 18:39:09 -08:00
|
|
|
match StateMut::<Versions>::state(acc).map(|v| v.convert_to_current()) {
|
2020-03-04 08:51:48 -08:00
|
|
|
Ok(State::Initialized(ref data)) => *hash == data.blockhash,
|
2019-12-07 11:54:10 -08:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-22 14:32:40 -07:00
|
|
|
pub fn lamports_per_signature_of(account: &AccountSharedData) -> Option<u64> {
|
2020-03-05 06:40:26 -08:00
|
|
|
let state = StateMut::<Versions>::state(account)
|
|
|
|
.ok()?
|
|
|
|
.convert_to_current();
|
|
|
|
match state {
|
2021-10-22 14:32:40 -07:00
|
|
|
State::Initialized(data) => Some(data.fee_calculator.lamports_per_signature),
|
2020-03-05 06:40:26 -08:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
2020-11-25 16:03:43 -08:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use crate::pubkey::Pubkey;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_verify_bad_account_owner_fails() {
|
|
|
|
let program_id = Pubkey::new_unique();
|
|
|
|
assert_ne!(program_id, crate::system_program::id());
|
2021-03-09 13:06:07 -08:00
|
|
|
let account = AccountSharedData::new_data_with_space(
|
2020-11-25 16:03:43 -08:00
|
|
|
42,
|
|
|
|
&Versions::new_current(State::Uninitialized),
|
|
|
|
State::size(),
|
|
|
|
&program_id,
|
|
|
|
)
|
|
|
|
.expect("nonce_account");
|
|
|
|
assert!(!verify_nonce_account(&account, &Hash::default()));
|
|
|
|
}
|
|
|
|
}
|