| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648 |
- use assert_matches::assert_matches;
- use bincode::deserialize;
- use log::*;
- use solana_runtime::{
- bank::Bank,
- bank_client::BankClient,
- genesis_utils::{create_genesis_block, GenesisBlockInfo},
- };
- use solana_sdk::{
- account::{create_keyed_accounts, Account, KeyedAccount},
- account_utils::State,
- client::SyncClient,
- clock::{get_segment_from_slot, DEFAULT_SLOTS_PER_SEGMENT, DEFAULT_TICKS_PER_SLOT},
- hash::{hash, Hash},
- instruction::{Instruction, InstructionError},
- message::Message,
- pubkey::Pubkey,
- signature::{Keypair, KeypairUtil, Signature},
- system_instruction,
- sysvar::clock::{self, Clock},
- sysvar::rewards::{self, Rewards},
- };
- use solana_storage_api::{
- id,
- storage_contract::StorageAccount,
- storage_contract::{ProofStatus, StorageContract, STORAGE_ACCOUNT_SPACE},
- storage_instruction::{self, StorageAccountType},
- storage_processor::process_instruction,
- };
- use std::collections::HashMap;
- use std::sync::Arc;
- const TICKS_IN_SEGMENT: u64 = DEFAULT_SLOTS_PER_SEGMENT * DEFAULT_TICKS_PER_SLOT;
- fn test_instruction(
- ix: &Instruction,
- program_accounts: &mut [Account],
- ) -> Result<(), InstructionError> {
- let mut keyed_accounts: Vec<_> = ix
- .accounts
- .iter()
- .zip(program_accounts.iter_mut())
- .map(|(account_meta, account)| {
- KeyedAccount::new(&account_meta.pubkey, account_meta.is_signer, account)
- })
- .collect();
- let ret = process_instruction(&id(), &mut keyed_accounts, &ix.data);
- info!("ret: {:?}", ret);
- ret
- }
- #[test]
- fn test_account_owner() {
- let account_owner = Pubkey::new_rand();
- let validator_storage_pubkey = Pubkey::new_rand();
- let replicator_storage_pubkey = Pubkey::new_rand();
- let GenesisBlockInfo {
- genesis_block,
- mint_keypair,
- ..
- } = create_genesis_block(1000);
- let mut bank = Bank::new(&genesis_block);
- let mint_pubkey = mint_keypair.pubkey();
- bank.add_instruction_processor(id(), process_instruction);
- let bank = Arc::new(bank);
- let bank_client = BankClient::new_shared(&bank);
- let message = Message::new(storage_instruction::create_storage_account(
- &mint_pubkey,
- &account_owner,
- &validator_storage_pubkey,
- 1,
- StorageAccountType::Validator,
- ));
- bank_client
- .send_message(&[&mint_keypair], message)
- .expect("failed to create account");
- let account = bank
- .get_account(&validator_storage_pubkey)
- .expect("account not found");
- let storage_contract = account.state().expect("couldn't unpack account data");
- if let StorageContract::ValidatorStorage { owner, .. } = storage_contract {
- assert_eq!(owner, account_owner);
- } else {
- assert!(false, "wrong account type found")
- }
- let message = Message::new(storage_instruction::create_storage_account(
- &mint_pubkey,
- &account_owner,
- &replicator_storage_pubkey,
- 1,
- StorageAccountType::Replicator,
- ));
- bank_client
- .send_message(&[&mint_keypair], message)
- .expect("failed to create account");
- let account = bank
- .get_account(&replicator_storage_pubkey)
- .expect("account not found");
- let storage_contract = account.state().expect("couldn't unpack account data");
- if let StorageContract::ReplicatorStorage { owner, .. } = storage_contract {
- assert_eq!(owner, account_owner);
- } else {
- assert!(false, "wrong account type found")
- }
- }
- #[test]
- fn test_proof_bounds() {
- let account_owner = Pubkey::new_rand();
- let pubkey = Pubkey::new_rand();
- let mut account = Account {
- data: vec![0; STORAGE_ACCOUNT_SPACE as usize],
- ..Account::default()
- };
- {
- let mut storage_account = StorageAccount::new(pubkey, &mut account);
- storage_account
- .initialize_storage(account_owner, StorageAccountType::Replicator)
- .unwrap();
- }
- let ix = storage_instruction::mining_proof(
- &pubkey,
- Hash::default(),
- 0,
- Signature::default(),
- Hash::default(),
- );
- // the proof is for segment 0, need to move the slot into segment 2
- let mut clock_account = clock::new_account(1, 0, 0, 0, 0);
- Clock::to_account(
- &Clock {
- slot: DEFAULT_SLOTS_PER_SEGMENT * 2,
- segment: 2,
- epoch: 0,
- stakers_epoch: 0,
- },
- &mut clock_account,
- );
- assert_eq!(test_instruction(&ix, &mut [account, clock_account]), Ok(()));
- }
- #[test]
- fn test_storage_tx() {
- let pubkey = Pubkey::new_rand();
- let mut accounts = [(pubkey, Account::default())];
- let mut keyed_accounts = create_keyed_accounts(&mut accounts);
- assert!(process_instruction(&id(), &mut keyed_accounts, &[]).is_err());
- }
- #[test]
- fn test_serialize_overflow() {
- let pubkey = Pubkey::new_rand();
- let clock_id = clock::id();
- let mut keyed_accounts = Vec::new();
- let mut user_account = Account::default();
- let mut clock_account = clock::new_account(1, 0, 0, 0, 0);
- keyed_accounts.push(KeyedAccount::new(&pubkey, true, &mut user_account));
- keyed_accounts.push(KeyedAccount::new(&clock_id, false, &mut clock_account));
- let ix = storage_instruction::advertise_recent_blockhash(&pubkey, Hash::default(), 1);
- assert_eq!(
- process_instruction(&id(), &mut keyed_accounts, &ix.data),
- Err(InstructionError::InvalidAccountData)
- );
- }
- #[test]
- fn test_invalid_accounts_len() {
- let pubkey = Pubkey::new_rand();
- let mut accounts = [Account::default()];
- let ix = storage_instruction::mining_proof(
- &pubkey,
- Hash::default(),
- 0,
- Signature::default(),
- Hash::default(),
- );
- // move tick height into segment 1
- let mut clock_account = clock::new_account(1, 0, 0, 0, 0);
- Clock::to_account(
- &Clock {
- slot: 16,
- segment: 1,
- epoch: 0,
- stakers_epoch: 0,
- },
- &mut clock_account,
- );
- assert!(test_instruction(&ix, &mut accounts).is_err());
- let mut accounts = [Account::default(), clock_account, Account::default()];
- assert!(test_instruction(&ix, &mut accounts).is_err());
- }
- #[test]
- fn test_submit_mining_invalid_slot() {
- solana_logger::setup();
- let pubkey = Pubkey::new_rand();
- let mut accounts = [Account::default(), Account::default()];
- accounts[0].data.resize(STORAGE_ACCOUNT_SPACE as usize, 0);
- accounts[1].data.resize(STORAGE_ACCOUNT_SPACE as usize, 0);
- let ix = storage_instruction::mining_proof(
- &pubkey,
- Hash::default(),
- 0,
- Signature::default(),
- Hash::default(),
- );
- // submitting a proof for a slot in the past, so this should fail
- assert!(test_instruction(&ix, &mut accounts).is_err());
- }
- #[test]
- fn test_submit_mining_ok() {
- solana_logger::setup();
- let account_owner = Pubkey::new_rand();
- let pubkey = Pubkey::new_rand();
- let mut account = Account::default();
- account.data.resize(STORAGE_ACCOUNT_SPACE as usize, 0);
- {
- let mut storage_account = StorageAccount::new(pubkey, &mut account);
- storage_account
- .initialize_storage(account_owner, StorageAccountType::Replicator)
- .unwrap();
- }
- let ix = storage_instruction::mining_proof(
- &pubkey,
- Hash::default(),
- 0,
- Signature::default(),
- Hash::default(),
- );
- // move slot into segment 1
- let mut clock_account = clock::new_account(1, 0, 0, 0, 0);
- Clock::to_account(
- &Clock {
- slot: DEFAULT_SLOTS_PER_SEGMENT,
- segment: 1,
- epoch: 0,
- stakers_epoch: 0,
- },
- &mut clock_account,
- );
- assert_matches!(test_instruction(&ix, &mut [account, clock_account]), Ok(_));
- }
- #[test]
- fn test_validate_mining() {
- solana_logger::setup();
- let GenesisBlockInfo {
- mut genesis_block,
- mint_keypair,
- ..
- } = create_genesis_block(100_000_000_000);
- genesis_block
- .native_instruction_processors
- .push(solana_storage_program::solana_storage_program!());
- let mint_pubkey = mint_keypair.pubkey();
- // 1 owner for all replicator and validator accounts for the test
- let owner_pubkey = Pubkey::new_rand();
- let replicator_1_storage_keypair = Keypair::new();
- let replicator_1_storage_id = replicator_1_storage_keypair.pubkey();
- let replicator_2_storage_keypair = Keypair::new();
- let replicator_2_storage_id = replicator_2_storage_keypair.pubkey();
- let validator_storage_keypair = Keypair::new();
- let validator_storage_id = validator_storage_keypair.pubkey();
- let bank = Bank::new(&genesis_block);
- let bank = Arc::new(bank);
- let bank_client = BankClient::new_shared(&bank);
- init_storage_accounts(
- &owner_pubkey,
- &bank_client,
- &mint_keypair,
- &[&validator_storage_id],
- &[&replicator_1_storage_id, &replicator_2_storage_id],
- 10,
- );
- // create a new bank in segment 2
- let bank = Arc::new(Bank::new_from_parent(
- &bank,
- &Pubkey::default(),
- DEFAULT_SLOTS_PER_SEGMENT * 2,
- ));
- let bank_client = BankClient::new_shared(&bank);
- // advertise for storage segment 1
- let message = Message::new_with_payer(
- vec![storage_instruction::advertise_recent_blockhash(
- &validator_storage_id,
- Hash::default(),
- 1,
- )],
- Some(&mint_pubkey),
- );
- assert_matches!(
- bank_client.send_message(&[&mint_keypair, &validator_storage_keypair], message),
- Ok(_)
- );
- // submit proofs 5 proofs for each replicator for segment 0
- let mut checked_proofs: HashMap<_, Vec<_>> = HashMap::new();
- for _ in 0..5 {
- checked_proofs
- .entry(replicator_1_storage_id)
- .or_default()
- .push(submit_proof(
- &mint_keypair,
- &replicator_1_storage_keypair,
- &bank_client,
- 0,
- ));
- checked_proofs
- .entry(replicator_2_storage_id)
- .or_default()
- .push(submit_proof(
- &mint_keypair,
- &replicator_2_storage_keypair,
- &bank_client,
- 0,
- ));
- }
- let message = Message::new_with_payer(
- vec![storage_instruction::advertise_recent_blockhash(
- &validator_storage_id,
- Hash::default(),
- 2,
- )],
- Some(&mint_pubkey),
- );
- // move banks into the next segment
- let proof_segment = get_segment_from_slot(bank.slot(), bank.slots_per_segment());
- let bank = Arc::new(Bank::new_from_parent(
- &bank,
- &Pubkey::default(),
- DEFAULT_SLOTS_PER_SEGMENT + bank.slot(),
- ));
- let bank_client = BankClient::new_shared(&bank);
- assert_matches!(
- bank_client.send_message(&[&mint_keypair, &validator_storage_keypair], message),
- Ok(_)
- );
- let message = Message::new_with_payer(
- vec![storage_instruction::proof_validation(
- &validator_storage_id,
- proof_segment as u64,
- checked_proofs.into_iter().map(|entry| entry).collect(),
- )],
- Some(&mint_pubkey),
- );
- assert_matches!(
- bank_client.send_message(&[&mint_keypair, &validator_storage_keypair], message),
- Ok(_)
- );
- let message = Message::new_with_payer(
- vec![storage_instruction::advertise_recent_blockhash(
- &validator_storage_id,
- Hash::default(),
- 3,
- )],
- Some(&mint_pubkey),
- );
- // move banks into the next segment
- let bank = Arc::new(Bank::new_from_parent(
- &bank,
- &Pubkey::default(),
- DEFAULT_SLOTS_PER_SEGMENT + bank.slot(),
- ));
- let bank_client = BankClient::new_shared(&bank);
- assert_matches!(
- bank_client.send_message(&[&mint_keypair, &validator_storage_keypair], message),
- Ok(_)
- );
- assert_eq!(bank_client.get_balance(&validator_storage_id).unwrap(), 10);
- let bank = Arc::new(Bank::new_from_parent(
- &bank,
- &Pubkey::default(),
- bank.slot() + bank.epoch_schedule().slots_per_epoch,
- ));
- let bank_client = BankClient::new_shared(&bank);
- let rewards = bank
- .get_account(&rewards::id())
- .map(|account| Rewards::from_account(&account).unwrap())
- .unwrap();
- let message = Message::new_with_payer(
- vec![storage_instruction::claim_reward(
- &owner_pubkey,
- &validator_storage_id,
- )],
- Some(&mint_pubkey),
- );
- assert_matches!(bank_client.send_message(&[&mint_keypair], message), Ok(_));
- assert_eq!(
- bank_client.get_balance(&owner_pubkey).unwrap(),
- 1 + ((rewards.storage_point_value * 10_f64) as u64)
- );
- // tick the bank into the next storage epoch so that rewards can be claimed
- for _ in 0..=TICKS_IN_SEGMENT {
- bank.register_tick(&bank.last_blockhash());
- }
- assert_eq!(
- bank_client.get_balance(&replicator_1_storage_id).unwrap(),
- 10
- );
- let message = Message::new_with_payer(
- vec![storage_instruction::claim_reward(
- &owner_pubkey,
- &replicator_1_storage_id,
- )],
- Some(&mint_pubkey),
- );
- assert_matches!(bank_client.send_message(&[&mint_keypair], message), Ok(_));
- assert_eq!(
- bank_client.get_balance(&owner_pubkey).unwrap(),
- 1 + ((rewards.storage_point_value * 10_f64) as u64)
- + (rewards.storage_point_value * 5_f64) as u64
- );
- let message = Message::new_with_payer(
- vec![storage_instruction::claim_reward(
- &owner_pubkey,
- &replicator_2_storage_id,
- )],
- Some(&mint_pubkey),
- );
- assert_matches!(bank_client.send_message(&[&mint_keypair], message), Ok(_));
- assert_eq!(
- bank_client.get_balance(&owner_pubkey).unwrap(),
- 1 + (rewards.storage_point_value * 10_f64) as u64
- + (rewards.storage_point_value * 5_f64) as u64
- + (rewards.storage_point_value * 5_f64) as u64
- );
- }
- fn init_storage_accounts(
- owner: &Pubkey,
- client: &BankClient,
- mint: &Keypair,
- validator_accounts_to_create: &[&Pubkey],
- replicator_accounts_to_create: &[&Pubkey],
- lamports: u64,
- ) {
- let mut ixs: Vec<_> = vec![system_instruction::transfer_now(&mint.pubkey(), owner, 1)];
- ixs.append(
- &mut validator_accounts_to_create
- .into_iter()
- .flat_map(|account| {
- storage_instruction::create_storage_account(
- &mint.pubkey(),
- owner,
- account,
- lamports,
- StorageAccountType::Validator,
- )
- })
- .collect(),
- );
- replicator_accounts_to_create
- .into_iter()
- .for_each(|account| {
- ixs.append(&mut storage_instruction::create_storage_account(
- &mint.pubkey(),
- owner,
- account,
- lamports,
- StorageAccountType::Replicator,
- ))
- });
- let message = Message::new(ixs);
- client.send_message(&[mint], message).unwrap();
- }
- fn get_storage_segment<C: SyncClient>(client: &C, account: &Pubkey) -> u64 {
- match client.get_account_data(&account).unwrap() {
- Some(storage_system_account_data) => {
- let contract = deserialize(&storage_system_account_data);
- if let Ok(contract) = contract {
- match contract {
- StorageContract::ValidatorStorage { segment, .. } => {
- return segment;
- }
- _ => info!("error in reading segment"),
- }
- }
- }
- None => {
- info!("error in reading segment");
- }
- }
- 0
- }
- fn submit_proof(
- mint_keypair: &Keypair,
- storage_keypair: &Keypair,
- bank_client: &BankClient,
- segment_index: u64,
- ) -> ProofStatus {
- let sha_state = Hash::new(Pubkey::new_rand().as_ref());
- let message = Message::new_with_payer(
- vec![storage_instruction::mining_proof(
- &storage_keypair.pubkey(),
- sha_state,
- segment_index,
- Signature::default(),
- bank_client.get_recent_blockhash().unwrap().0,
- )],
- Some(&mint_keypair.pubkey()),
- );
- assert_matches!(
- bank_client.send_message(&[&mint_keypair, &storage_keypair], message),
- Ok(_)
- );
- ProofStatus::Valid
- }
- fn get_storage_blockhash<C: SyncClient>(client: &C, account: &Pubkey) -> Hash {
- if let Some(storage_system_account_data) = client.get_account_data(&account).unwrap() {
- let contract = deserialize(&storage_system_account_data);
- if let Ok(contract) = contract {
- match contract {
- StorageContract::ValidatorStorage { hash, .. } => {
- return hash;
- }
- _ => (),
- }
- }
- }
- Hash::default()
- }
- #[test]
- fn test_bank_storage() {
- let GenesisBlockInfo {
- mut genesis_block,
- mint_keypair,
- ..
- } = create_genesis_block(1000);
- genesis_block
- .native_instruction_processors
- .push(solana_storage_program::solana_storage_program!());
- let mint_pubkey = mint_keypair.pubkey();
- let replicator_keypair = Keypair::new();
- let replicator_pubkey = replicator_keypair.pubkey();
- let validator_keypair = Keypair::new();
- let validator_pubkey = validator_keypair.pubkey();
- let bank = Bank::new(&genesis_block);
- // tick the bank up until it's moved into storage segment 2
- // create a new bank in storage segment 2
- let bank = Bank::new_from_parent(
- &Arc::new(bank),
- &Pubkey::new_rand(),
- DEFAULT_SLOTS_PER_SEGMENT * 2,
- );
- let bank_client = BankClient::new(bank);
- let x = 42;
- let x2 = x * 2;
- let storage_blockhash = hash(&[x2]);
- let message = Message::new(storage_instruction::create_storage_account(
- &mint_pubkey,
- &Pubkey::default(),
- &replicator_pubkey,
- 11,
- StorageAccountType::Replicator,
- ));
- bank_client.send_message(&[&mint_keypair], message).unwrap();
- let message = Message::new(storage_instruction::create_storage_account(
- &mint_pubkey,
- &Pubkey::default(),
- &validator_pubkey,
- 1,
- StorageAccountType::Validator,
- ));
- bank_client.send_message(&[&mint_keypair], message).unwrap();
- let message = Message::new_with_payer(
- vec![storage_instruction::advertise_recent_blockhash(
- &validator_pubkey,
- storage_blockhash,
- 1,
- )],
- Some(&mint_pubkey),
- );
- assert_matches!(
- bank_client.send_message(&[&mint_keypair, &validator_keypair], message),
- Ok(_)
- );
- let slot = 0;
- let message = Message::new_with_payer(
- vec![storage_instruction::mining_proof(
- &replicator_pubkey,
- Hash::default(),
- slot,
- Signature::default(),
- bank_client.get_recent_blockhash().unwrap().0,
- )],
- Some(&mint_pubkey),
- );
- assert_matches!(
- bank_client.send_message(&[&mint_keypair, &replicator_keypair], message),
- Ok(_)
- );
- assert_eq!(get_storage_segment(&bank_client, &validator_pubkey), 1);
- assert_eq!(
- get_storage_blockhash(&bank_client, &validator_pubkey),
- storage_blockhash
- );
- }
|