| 
									
										
										
										
											2019-06-06 17:40:01 -07:00
										 |  |  | use assert_matches::assert_matches;
 | 
					
						
							|  |  |  | use bincode::deserialize;
 | 
					
						
							|  |  |  | use log::*;
 | 
					
						
							|  |  |  | use solana_runtime::bank::Bank;
 | 
					
						
							|  |  |  | use solana_runtime::bank_client::BankClient;
 | 
					
						
							| 
									
										
										
										
											2019-06-22 17:18:35 -07:00
										 |  |  | use solana_runtime::genesis_utils::{create_genesis_block, GenesisBlockInfo};
 | 
					
						
							| 
									
										
										
										
											2019-06-06 17:40:01 -07:00
										 |  |  | use solana_sdk::account::{create_keyed_accounts, Account, KeyedAccount};
 | 
					
						
							|  |  |  | use solana_sdk::account_utils::State;
 | 
					
						
							|  |  |  | use solana_sdk::client::SyncClient;
 | 
					
						
							| 
									
										
										
										
											2019-09-06 14:30:56 -07:00
										 |  |  | use solana_sdk::clock::{get_segment_from_slot, DEFAULT_SLOTS_PER_SEGMENT, DEFAULT_TICKS_PER_SLOT};
 | 
					
						
							| 
									
										
										
										
											2019-06-06 17:40:01 -07:00
										 |  |  | use solana_sdk::hash::{hash, Hash};
 | 
					
						
							|  |  |  | use solana_sdk::instruction::{Instruction, InstructionError};
 | 
					
						
							|  |  |  | use solana_sdk::message::Message;
 | 
					
						
							|  |  |  | use solana_sdk::pubkey::Pubkey;
 | 
					
						
							|  |  |  | use solana_sdk::signature::{Keypair, KeypairUtil, Signature};
 | 
					
						
							| 
									
										
										
										
											2019-06-13 17:53:54 -07:00
										 |  |  | use solana_sdk::system_instruction;
 | 
					
						
							| 
									
										
										
										
											2019-09-09 10:55:35 -07:00
										 |  |  | use solana_sdk::sysvar::clock::{self, Clock};
 | 
					
						
							| 
									
										
										
										
											2019-09-09 16:38:52 -07:00
										 |  |  | use solana_sdk::sysvar::rewards::{self, Rewards};
 | 
					
						
							| 
									
										
										
										
											2019-07-09 16:48:40 -07:00
										 |  |  | use solana_storage_api::id;
 | 
					
						
							| 
									
										
										
										
											2019-06-06 17:40:01 -07:00
										 |  |  | use solana_storage_api::storage_contract::StorageAccount;
 | 
					
						
							| 
									
										
										
										
											2019-06-26 10:40:03 -07:00
										 |  |  | use solana_storage_api::storage_contract::{ProofStatus, StorageContract, STORAGE_ACCOUNT_SPACE};
 | 
					
						
							| 
									
										
										
										
											2019-06-06 17:40:01 -07:00
										 |  |  | use solana_storage_api::storage_instruction;
 | 
					
						
							|  |  |  | use solana_storage_api::storage_processor::process_instruction;
 | 
					
						
							|  |  |  | use std::collections::HashMap;
 | 
					
						
							|  |  |  | use std::sync::Arc;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-09 16:48:40 -07:00
										 |  |  | const TICKS_IN_SEGMENT: u64 = DEFAULT_SLOTS_PER_SEGMENT * DEFAULT_TICKS_PER_SLOT;
 | 
					
						
							| 
									
										
										
										
											2019-06-06 17:40:01 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 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();
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-22 17:18:35 -07:00
										 |  |  |     let GenesisBlockInfo {
 | 
					
						
							|  |  |  |         genesis_block,
 | 
					
						
							|  |  |  |         mint_keypair,
 | 
					
						
							|  |  |  |         ..
 | 
					
						
							|  |  |  |     } = create_genesis_block(1000);
 | 
					
						
							| 
									
										
										
										
											2019-06-06 17:40:01 -07:00
										 |  |  |     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_validator_storage_account(
 | 
					
						
							|  |  |  |         &mint_pubkey,
 | 
					
						
							|  |  |  |         &account_owner,
 | 
					
						
							|  |  |  |         &validator_storage_pubkey,
 | 
					
						
							|  |  |  |         1,
 | 
					
						
							|  |  |  |     ));
 | 
					
						
							|  |  |  |     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_replicator_storage_account(
 | 
					
						
							|  |  |  |         &mint_pubkey,
 | 
					
						
							|  |  |  |         &account_owner,
 | 
					
						
							|  |  |  |         &replicator_storage_pubkey,
 | 
					
						
							|  |  |  |         1,
 | 
					
						
							|  |  |  |     ));
 | 
					
						
							|  |  |  |     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()
 | 
					
						
							|  |  |  |     };
 | 
					
						
							|  |  |  |     {
 | 
					
						
							| 
									
										
										
										
											2019-06-13 17:53:54 -07:00
										 |  |  |         let mut storage_account = StorageAccount::new(pubkey, &mut account);
 | 
					
						
							| 
									
										
										
										
											2019-06-06 17:40:01 -07:00
										 |  |  |         storage_account
 | 
					
						
							|  |  |  |             .initialize_replicator_storage(account_owner)
 | 
					
						
							|  |  |  |             .unwrap();
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-13 17:53:54 -07:00
										 |  |  |     let ix = storage_instruction::mining_proof(
 | 
					
						
							|  |  |  |         &pubkey,
 | 
					
						
							|  |  |  |         Hash::default(),
 | 
					
						
							|  |  |  |         0,
 | 
					
						
							|  |  |  |         Signature::default(),
 | 
					
						
							|  |  |  |         Hash::default(),
 | 
					
						
							|  |  |  |     );
 | 
					
						
							| 
									
										
										
										
											2019-06-11 18:27:47 -07:00
										 |  |  |     // the proof is for segment 0, need to move the slot into segment 2
 | 
					
						
							| 
									
										
										
										
											2019-09-09 16:38:52 -07:00
										 |  |  |     let mut clock_account = clock::new_account(1, 0, 0, 0, 0);
 | 
					
						
							|  |  |  |     Clock::to_account(
 | 
					
						
							| 
									
										
										
										
											2019-07-12 16:38:15 -07:00
										 |  |  |         &Clock {
 | 
					
						
							| 
									
										
										
										
											2019-07-09 16:48:40 -07:00
										 |  |  |             slot: DEFAULT_SLOTS_PER_SEGMENT * 2,
 | 
					
						
							|  |  |  |             segment: 2,
 | 
					
						
							| 
									
										
										
										
											2019-06-11 18:27:47 -07:00
										 |  |  |             epoch: 0,
 | 
					
						
							|  |  |  |             stakers_epoch: 0,
 | 
					
						
							|  |  |  |         },
 | 
					
						
							| 
									
										
										
										
											2019-07-12 16:38:15 -07:00
										 |  |  |         &mut clock_account,
 | 
					
						
							| 
									
										
										
										
											2019-06-06 17:40:01 -07:00
										 |  |  |     );
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-12 16:38:15 -07:00
										 |  |  |     assert_eq!(test_instruction(&ix, &mut [account, clock_account]), Ok(()));
 | 
					
						
							| 
									
										
										
										
											2019-06-06 17:40:01 -07:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #[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();
 | 
					
						
							| 
									
										
										
										
											2019-07-12 16:38:15 -07:00
										 |  |  |     let clock_id = clock::id();
 | 
					
						
							| 
									
										
										
										
											2019-06-06 17:40:01 -07:00
										 |  |  |     let mut keyed_accounts = Vec::new();
 | 
					
						
							|  |  |  |     let mut user_account = Account::default();
 | 
					
						
							| 
									
										
										
										
											2019-09-09 16:38:52 -07:00
										 |  |  |     let mut clock_account = clock::new_account(1, 0, 0, 0, 0);
 | 
					
						
							| 
									
										
										
										
											2019-06-06 17:40:01 -07:00
										 |  |  |     keyed_accounts.push(KeyedAccount::new(&pubkey, true, &mut user_account));
 | 
					
						
							| 
									
										
										
										
											2019-07-12 16:38:15 -07:00
										 |  |  |     keyed_accounts.push(KeyedAccount::new(&clock_id, false, &mut clock_account));
 | 
					
						
							| 
									
										
										
										
											2019-06-06 17:40:01 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-09 16:48:40 -07:00
										 |  |  |     let ix = storage_instruction::advertise_recent_blockhash(&pubkey, Hash::default(), 1);
 | 
					
						
							| 
									
										
										
										
											2019-06-06 17:40:01 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     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()];
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-13 17:53:54 -07:00
										 |  |  |     let ix = storage_instruction::mining_proof(
 | 
					
						
							|  |  |  |         &pubkey,
 | 
					
						
							|  |  |  |         Hash::default(),
 | 
					
						
							|  |  |  |         0,
 | 
					
						
							|  |  |  |         Signature::default(),
 | 
					
						
							|  |  |  |         Hash::default(),
 | 
					
						
							|  |  |  |     );
 | 
					
						
							| 
									
										
										
										
											2019-06-06 17:40:01 -07:00
										 |  |  |     // move tick height into segment 1
 | 
					
						
							| 
									
										
										
										
											2019-09-09 16:38:52 -07:00
										 |  |  |     let mut clock_account = clock::new_account(1, 0, 0, 0, 0);
 | 
					
						
							|  |  |  |     Clock::to_account(
 | 
					
						
							| 
									
										
										
										
											2019-07-12 16:38:15 -07:00
										 |  |  |         &Clock {
 | 
					
						
							| 
									
										
										
										
											2019-06-11 18:27:47 -07:00
										 |  |  |             slot: 16,
 | 
					
						
							| 
									
										
										
										
											2019-07-09 16:48:40 -07:00
										 |  |  |             segment: 1,
 | 
					
						
							| 
									
										
										
										
											2019-06-11 18:27:47 -07:00
										 |  |  |             epoch: 0,
 | 
					
						
							|  |  |  |             stakers_epoch: 0,
 | 
					
						
							|  |  |  |         },
 | 
					
						
							| 
									
										
										
										
											2019-07-12 16:38:15 -07:00
										 |  |  |         &mut clock_account,
 | 
					
						
							| 
									
										
										
										
											2019-06-11 18:27:47 -07:00
										 |  |  |     );
 | 
					
						
							| 
									
										
										
										
											2019-06-06 17:40:01 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     assert!(test_instruction(&ix, &mut accounts).is_err());
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-12 16:38:15 -07:00
										 |  |  |     let mut accounts = [Account::default(), clock_account, Account::default()];
 | 
					
						
							| 
									
										
										
										
											2019-06-06 17:40:01 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     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);
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-13 17:53:54 -07:00
										 |  |  |     let ix = storage_instruction::mining_proof(
 | 
					
						
							|  |  |  |         &pubkey,
 | 
					
						
							|  |  |  |         Hash::default(),
 | 
					
						
							|  |  |  |         0,
 | 
					
						
							|  |  |  |         Signature::default(),
 | 
					
						
							|  |  |  |         Hash::default(),
 | 
					
						
							|  |  |  |     );
 | 
					
						
							| 
									
										
										
										
											2019-06-06 17:40:01 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 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);
 | 
					
						
							|  |  |  |     {
 | 
					
						
							| 
									
										
										
										
											2019-06-13 17:53:54 -07:00
										 |  |  |         let mut storage_account = StorageAccount::new(pubkey, &mut account);
 | 
					
						
							| 
									
										
										
										
											2019-06-06 17:40:01 -07:00
										 |  |  |         storage_account
 | 
					
						
							|  |  |  |             .initialize_replicator_storage(account_owner)
 | 
					
						
							|  |  |  |             .unwrap();
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-13 17:53:54 -07:00
										 |  |  |     let ix = storage_instruction::mining_proof(
 | 
					
						
							|  |  |  |         &pubkey,
 | 
					
						
							|  |  |  |         Hash::default(),
 | 
					
						
							|  |  |  |         0,
 | 
					
						
							|  |  |  |         Signature::default(),
 | 
					
						
							|  |  |  |         Hash::default(),
 | 
					
						
							|  |  |  |     );
 | 
					
						
							| 
									
										
										
										
											2019-06-11 18:27:47 -07:00
										 |  |  |     // move slot into segment 1
 | 
					
						
							| 
									
										
										
										
											2019-09-09 16:38:52 -07:00
										 |  |  |     let mut clock_account = clock::new_account(1, 0, 0, 0, 0);
 | 
					
						
							|  |  |  |     Clock::to_account(
 | 
					
						
							| 
									
										
										
										
											2019-07-12 16:38:15 -07:00
										 |  |  |         &Clock {
 | 
					
						
							| 
									
										
										
										
											2019-07-09 16:48:40 -07:00
										 |  |  |             slot: DEFAULT_SLOTS_PER_SEGMENT,
 | 
					
						
							|  |  |  |             segment: 1,
 | 
					
						
							| 
									
										
										
										
											2019-06-11 18:27:47 -07:00
										 |  |  |             epoch: 0,
 | 
					
						
							|  |  |  |             stakers_epoch: 0,
 | 
					
						
							|  |  |  |         },
 | 
					
						
							| 
									
										
										
										
											2019-07-12 16:38:15 -07:00
										 |  |  |         &mut clock_account,
 | 
					
						
							| 
									
										
										
										
											2019-06-11 18:27:47 -07:00
										 |  |  |     );
 | 
					
						
							| 
									
										
										
										
											2019-06-06 17:40:01 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-12 16:38:15 -07:00
										 |  |  |     assert_matches!(test_instruction(&ix, &mut [account, clock_account]), Ok(_));
 | 
					
						
							| 
									
										
										
										
											2019-06-06 17:40:01 -07:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #[test]
 | 
					
						
							|  |  |  | fn test_validate_mining() {
 | 
					
						
							|  |  |  |     solana_logger::setup();
 | 
					
						
							| 
									
										
										
										
											2019-06-22 17:18:35 -07:00
										 |  |  |     let GenesisBlockInfo {
 | 
					
						
							|  |  |  |         mut genesis_block,
 | 
					
						
							|  |  |  |         mint_keypair,
 | 
					
						
							|  |  |  |         ..
 | 
					
						
							| 
									
										
										
										
											2019-06-26 10:40:03 -07:00
										 |  |  |     } = create_genesis_block(100_000_000_000);
 | 
					
						
							| 
									
										
										
										
											2019-06-11 18:27:47 -07:00
										 |  |  |     genesis_block
 | 
					
						
							|  |  |  |         .native_instruction_processors
 | 
					
						
							|  |  |  |         .push(solana_storage_program::solana_storage_program!());
 | 
					
						
							| 
									
										
										
										
											2019-06-06 17:40:01 -07:00
										 |  |  |     let mint_pubkey = mint_keypair.pubkey();
 | 
					
						
							| 
									
										
										
										
											2019-06-13 17:53:54 -07:00
										 |  |  |     // 1 owner for all replicator and validator accounts for the test
 | 
					
						
							|  |  |  |     let owner_pubkey = Pubkey::new_rand();
 | 
					
						
							| 
									
										
										
										
											2019-06-06 17:40:01 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     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();
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-11 18:27:47 -07:00
										 |  |  |     let bank = Bank::new(&genesis_block);
 | 
					
						
							| 
									
										
										
										
											2019-06-06 17:40:01 -07:00
										 |  |  |     let bank = Arc::new(bank);
 | 
					
						
							|  |  |  |     let bank_client = BankClient::new_shared(&bank);
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     init_storage_accounts(
 | 
					
						
							| 
									
										
										
										
											2019-06-13 17:53:54 -07:00
										 |  |  |         &owner_pubkey,
 | 
					
						
							| 
									
										
										
										
											2019-06-06 17:40:01 -07:00
										 |  |  |         &bank_client,
 | 
					
						
							|  |  |  |         &mint_keypair,
 | 
					
						
							|  |  |  |         &[&validator_storage_id],
 | 
					
						
							|  |  |  |         &[&replicator_1_storage_id, &replicator_2_storage_id],
 | 
					
						
							|  |  |  |         10,
 | 
					
						
							|  |  |  |     );
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-11 18:27:47 -07:00
										 |  |  |     // create a new bank in segment 2
 | 
					
						
							|  |  |  |     let bank = Arc::new(Bank::new_from_parent(
 | 
					
						
							|  |  |  |         &bank,
 | 
					
						
							|  |  |  |         &Pubkey::default(),
 | 
					
						
							| 
									
										
										
										
											2019-07-09 16:48:40 -07:00
										 |  |  |         DEFAULT_SLOTS_PER_SEGMENT * 2,
 | 
					
						
							| 
									
										
										
										
											2019-06-11 18:27:47 -07:00
										 |  |  |     ));
 | 
					
						
							|  |  |  |     let bank_client = BankClient::new_shared(&bank);
 | 
					
						
							| 
									
										
										
										
											2019-06-06 17:40:01 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // advertise for storage segment 1
 | 
					
						
							|  |  |  |     let message = Message::new_with_payer(
 | 
					
						
							|  |  |  |         vec![storage_instruction::advertise_recent_blockhash(
 | 
					
						
							|  |  |  |             &validator_storage_id,
 | 
					
						
							|  |  |  |             Hash::default(),
 | 
					
						
							| 
									
										
										
										
											2019-07-09 16:48:40 -07:00
										 |  |  |             1,
 | 
					
						
							| 
									
										
										
										
											2019-06-06 17:40:01 -07:00
										 |  |  |         )],
 | 
					
						
							|  |  |  |         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();
 | 
					
						
							| 
									
										
										
										
											2019-06-11 18:27:47 -07:00
										 |  |  |     for _ in 0..5 {
 | 
					
						
							| 
									
										
										
										
											2019-06-06 17:40:01 -07:00
										 |  |  |         checked_proofs
 | 
					
						
							|  |  |  |             .entry(replicator_1_storage_id)
 | 
					
						
							|  |  |  |             .or_default()
 | 
					
						
							|  |  |  |             .push(submit_proof(
 | 
					
						
							|  |  |  |                 &mint_keypair,
 | 
					
						
							|  |  |  |                 &replicator_1_storage_keypair,
 | 
					
						
							|  |  |  |                 &bank_client,
 | 
					
						
							| 
									
										
										
										
											2019-06-11 18:27:47 -07:00
										 |  |  |                 0,
 | 
					
						
							| 
									
										
										
										
											2019-06-06 17:40:01 -07:00
										 |  |  |             ));
 | 
					
						
							|  |  |  |         checked_proofs
 | 
					
						
							|  |  |  |             .entry(replicator_2_storage_id)
 | 
					
						
							|  |  |  |             .or_default()
 | 
					
						
							|  |  |  |             .push(submit_proof(
 | 
					
						
							|  |  |  |                 &mint_keypair,
 | 
					
						
							|  |  |  |                 &replicator_2_storage_keypair,
 | 
					
						
							|  |  |  |                 &bank_client,
 | 
					
						
							| 
									
										
										
										
											2019-06-11 18:27:47 -07:00
										 |  |  |                 0,
 | 
					
						
							| 
									
										
										
										
											2019-06-06 17:40:01 -07:00
										 |  |  |             ));
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  |     let message = Message::new_with_payer(
 | 
					
						
							|  |  |  |         vec![storage_instruction::advertise_recent_blockhash(
 | 
					
						
							|  |  |  |             &validator_storage_id,
 | 
					
						
							|  |  |  |             Hash::default(),
 | 
					
						
							| 
									
										
										
										
											2019-07-09 16:48:40 -07:00
										 |  |  |             2,
 | 
					
						
							| 
									
										
										
										
											2019-06-06 17:40:01 -07:00
										 |  |  |         )],
 | 
					
						
							|  |  |  |         Some(&mint_pubkey),
 | 
					
						
							|  |  |  |     );
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-11 18:27:47 -07:00
										 |  |  |     // move banks into the next segment
 | 
					
						
							| 
									
										
										
										
											2019-07-09 16:48:40 -07:00
										 |  |  |     let proof_segment = get_segment_from_slot(bank.slot(), bank.slots_per_segment());
 | 
					
						
							| 
									
										
										
										
											2019-06-11 18:27:47 -07:00
										 |  |  |     let bank = Arc::new(Bank::new_from_parent(
 | 
					
						
							|  |  |  |         &bank,
 | 
					
						
							|  |  |  |         &Pubkey::default(),
 | 
					
						
							| 
									
										
										
										
											2019-07-09 16:48:40 -07:00
										 |  |  |         DEFAULT_SLOTS_PER_SEGMENT + bank.slot(),
 | 
					
						
							| 
									
										
										
										
											2019-06-11 18:27:47 -07:00
										 |  |  |     ));
 | 
					
						
							|  |  |  |     let bank_client = BankClient::new_shared(&bank);
 | 
					
						
							| 
									
										
										
										
											2019-06-06 17:40:01 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     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,
 | 
					
						
							| 
									
										
										
										
											2019-06-11 18:27:47 -07:00
										 |  |  |             proof_segment as u64,
 | 
					
						
							| 
									
										
										
										
											2019-06-13 17:53:54 -07:00
										 |  |  |             checked_proofs.into_iter().map(|entry| entry).collect(),
 | 
					
						
							| 
									
										
										
										
											2019-06-06 17:40:01 -07:00
										 |  |  |         )],
 | 
					
						
							|  |  |  |         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(),
 | 
					
						
							| 
									
										
										
										
											2019-07-09 16:48:40 -07:00
										 |  |  |             3,
 | 
					
						
							| 
									
										
										
										
											2019-06-06 17:40:01 -07:00
										 |  |  |         )],
 | 
					
						
							|  |  |  |         Some(&mint_pubkey),
 | 
					
						
							|  |  |  |     );
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-11 18:27:47 -07:00
										 |  |  |     // move banks into the next segment
 | 
					
						
							|  |  |  |     let bank = Arc::new(Bank::new_from_parent(
 | 
					
						
							|  |  |  |         &bank,
 | 
					
						
							|  |  |  |         &Pubkey::default(),
 | 
					
						
							| 
									
										
										
										
											2019-07-09 16:48:40 -07:00
										 |  |  |         DEFAULT_SLOTS_PER_SEGMENT + bank.slot(),
 | 
					
						
							| 
									
										
										
										
											2019-06-11 18:27:47 -07:00
										 |  |  |     ));
 | 
					
						
							|  |  |  |     let bank_client = BankClient::new_shared(&bank);
 | 
					
						
							| 
									
										
										
										
											2019-06-06 17:40:01 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     assert_matches!(
 | 
					
						
							|  |  |  |         bank_client.send_message(&[&mint_keypair, &validator_storage_keypair], message),
 | 
					
						
							|  |  |  |         Ok(_)
 | 
					
						
							|  |  |  |     );
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     assert_eq!(bank_client.get_balance(&validator_storage_id).unwrap(), 10);
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-26 10:40:03 -07:00
										 |  |  |     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())
 | 
					
						
							| 
									
										
										
										
											2019-09-10 18:53:02 -07:00
										 |  |  |         .map(|account| Rewards::from_account(&account).unwrap())
 | 
					
						
							| 
									
										
										
										
											2019-06-26 10:40:03 -07:00
										 |  |  |         .unwrap();
 | 
					
						
							| 
									
										
										
										
											2019-06-06 17:40:01 -07:00
										 |  |  |     let message = Message::new_with_payer(
 | 
					
						
							|  |  |  |         vec![storage_instruction::claim_reward(
 | 
					
						
							| 
									
										
										
										
											2019-06-13 17:53:54 -07:00
										 |  |  |             &owner_pubkey,
 | 
					
						
							| 
									
										
										
										
											2019-06-06 17:40:01 -07:00
										 |  |  |             &validator_storage_id,
 | 
					
						
							|  |  |  |         )],
 | 
					
						
							|  |  |  |         Some(&mint_pubkey),
 | 
					
						
							|  |  |  |     );
 | 
					
						
							|  |  |  |     assert_matches!(bank_client.send_message(&[&mint_keypair], message), Ok(_));
 | 
					
						
							|  |  |  |     assert_eq!(
 | 
					
						
							| 
									
										
										
										
											2019-06-13 17:53:54 -07:00
										 |  |  |         bank_client.get_balance(&owner_pubkey).unwrap(),
 | 
					
						
							| 
									
										
										
										
											2019-06-26 10:40:03 -07:00
										 |  |  |         1 + ((rewards.storage_point_value * 10_f64) as u64)
 | 
					
						
							| 
									
										
										
										
											2019-06-06 17:40:01 -07:00
										 |  |  |     );
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 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(
 | 
					
						
							| 
									
										
										
										
											2019-06-13 17:53:54 -07:00
										 |  |  |             &owner_pubkey,
 | 
					
						
							| 
									
										
										
										
											2019-06-06 17:40:01 -07:00
										 |  |  |             &replicator_1_storage_id,
 | 
					
						
							|  |  |  |         )],
 | 
					
						
							|  |  |  |         Some(&mint_pubkey),
 | 
					
						
							|  |  |  |     );
 | 
					
						
							|  |  |  |     assert_matches!(bank_client.send_message(&[&mint_keypair], message), Ok(_));
 | 
					
						
							| 
									
										
										
										
											2019-06-13 17:53:54 -07:00
										 |  |  |     assert_eq!(
 | 
					
						
							|  |  |  |         bank_client.get_balance(&owner_pubkey).unwrap(),
 | 
					
						
							| 
									
										
										
										
											2019-06-26 10:40:03 -07:00
										 |  |  |         1 + ((rewards.storage_point_value * 10_f64) as u64)
 | 
					
						
							|  |  |  |             + (rewards.storage_point_value * 5_f64) as u64
 | 
					
						
							| 
									
										
										
										
											2019-06-13 17:53:54 -07:00
										 |  |  |     );
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-06 17:40:01 -07:00
										 |  |  |     let message = Message::new_with_payer(
 | 
					
						
							|  |  |  |         vec![storage_instruction::claim_reward(
 | 
					
						
							| 
									
										
										
										
											2019-06-13 17:53:54 -07:00
										 |  |  |             &owner_pubkey,
 | 
					
						
							| 
									
										
										
										
											2019-06-06 17:40:01 -07:00
										 |  |  |             &replicator_2_storage_id,
 | 
					
						
							|  |  |  |         )],
 | 
					
						
							|  |  |  |         Some(&mint_pubkey),
 | 
					
						
							|  |  |  |     );
 | 
					
						
							|  |  |  |     assert_matches!(bank_client.send_message(&[&mint_keypair], message), Ok(_));
 | 
					
						
							|  |  |  |     assert_eq!(
 | 
					
						
							| 
									
										
										
										
											2019-06-13 17:53:54 -07:00
										 |  |  |         bank_client.get_balance(&owner_pubkey).unwrap(),
 | 
					
						
							| 
									
										
										
										
											2019-06-26 10:40:03 -07:00
										 |  |  |         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
 | 
					
						
							| 
									
										
										
										
											2019-06-06 17:40:01 -07:00
										 |  |  |     );
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | fn init_storage_accounts(
 | 
					
						
							| 
									
										
										
										
											2019-06-13 17:53:54 -07:00
										 |  |  |     owner: &Pubkey,
 | 
					
						
							| 
									
										
										
										
											2019-06-06 17:40:01 -07:00
										 |  |  |     client: &BankClient,
 | 
					
						
							|  |  |  |     mint: &Keypair,
 | 
					
						
							|  |  |  |     validator_accounts_to_create: &[&Pubkey],
 | 
					
						
							|  |  |  |     replicator_accounts_to_create: &[&Pubkey],
 | 
					
						
							|  |  |  |     lamports: u64,
 | 
					
						
							|  |  |  | ) {
 | 
					
						
							| 
									
										
										
										
											2019-06-13 17:53:54 -07:00
										 |  |  |     let mut ixs: Vec<_> = vec![system_instruction::create_user_account(
 | 
					
						
							|  |  |  |         &mint.pubkey(),
 | 
					
						
							|  |  |  |         owner,
 | 
					
						
							|  |  |  |         1,
 | 
					
						
							|  |  |  |     )];
 | 
					
						
							|  |  |  |     ixs.append(
 | 
					
						
							|  |  |  |         &mut validator_accounts_to_create
 | 
					
						
							|  |  |  |             .into_iter()
 | 
					
						
							|  |  |  |             .flat_map(|account| {
 | 
					
						
							|  |  |  |                 storage_instruction::create_validator_storage_account(
 | 
					
						
							|  |  |  |                     &mint.pubkey(),
 | 
					
						
							|  |  |  |                     owner,
 | 
					
						
							|  |  |  |                     account,
 | 
					
						
							|  |  |  |                     lamports,
 | 
					
						
							|  |  |  |                 )
 | 
					
						
							|  |  |  |             })
 | 
					
						
							|  |  |  |             .collect(),
 | 
					
						
							|  |  |  |     );
 | 
					
						
							| 
									
										
										
										
											2019-06-06 17:40:01 -07:00
										 |  |  |     replicator_accounts_to_create
 | 
					
						
							|  |  |  |         .into_iter()
 | 
					
						
							|  |  |  |         .for_each(|account| {
 | 
					
						
							|  |  |  |             ixs.append(&mut storage_instruction::create_replicator_storage_account(
 | 
					
						
							|  |  |  |                 &mint.pubkey(),
 | 
					
						
							| 
									
										
										
										
											2019-06-13 17:53:54 -07:00
										 |  |  |                 owner,
 | 
					
						
							| 
									
										
										
										
											2019-06-06 17:40:01 -07:00
										 |  |  |                 account,
 | 
					
						
							|  |  |  |                 lamports,
 | 
					
						
							|  |  |  |             ))
 | 
					
						
							|  |  |  |         });
 | 
					
						
							|  |  |  |     let message = Message::new(ixs);
 | 
					
						
							|  |  |  |     client.send_message(&[mint], message).unwrap();
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-09 16:48:40 -07:00
										 |  |  | fn get_storage_segment<C: SyncClient>(client: &C, account: &Pubkey) -> u64 {
 | 
					
						
							| 
									
										
										
										
											2019-06-06 17:40:01 -07:00
										 |  |  |     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 {
 | 
					
						
							| 
									
										
										
										
											2019-07-09 16:48:40 -07:00
										 |  |  |                     StorageContract::ValidatorStorage { segment, .. } => {
 | 
					
						
							|  |  |  |                         return segment;
 | 
					
						
							| 
									
										
										
										
											2019-06-06 17:40:01 -07:00
										 |  |  |                     }
 | 
					
						
							| 
									
										
										
										
											2019-07-09 16:48:40 -07:00
										 |  |  |                     _ => info!("error in reading segment"),
 | 
					
						
							| 
									
										
										
										
											2019-06-06 17:40:01 -07:00
										 |  |  |                 }
 | 
					
						
							|  |  |  |             }
 | 
					
						
							|  |  |  |         }
 | 
					
						
							|  |  |  |         None => {
 | 
					
						
							| 
									
										
										
										
											2019-07-09 16:48:40 -07:00
										 |  |  |             info!("error in reading segment");
 | 
					
						
							| 
									
										
										
										
											2019-06-06 17:40:01 -07:00
										 |  |  |         }
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  |     0
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | fn submit_proof(
 | 
					
						
							|  |  |  |     mint_keypair: &Keypair,
 | 
					
						
							|  |  |  |     storage_keypair: &Keypair,
 | 
					
						
							|  |  |  |     bank_client: &BankClient,
 | 
					
						
							| 
									
										
										
										
											2019-06-11 18:27:47 -07:00
										 |  |  |     segment_index: u64,
 | 
					
						
							| 
									
										
										
										
											2019-06-13 17:53:54 -07:00
										 |  |  | ) -> ProofStatus {
 | 
					
						
							| 
									
										
										
										
											2019-06-06 17:40:01 -07:00
										 |  |  |     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,
 | 
					
						
							| 
									
										
										
										
											2019-07-09 16:48:40 -07:00
										 |  |  |             segment_index,
 | 
					
						
							| 
									
										
										
										
											2019-06-06 17:40:01 -07:00
										 |  |  |             Signature::default(),
 | 
					
						
							| 
									
										
										
										
											2019-06-13 17:53:54 -07:00
										 |  |  |             bank_client.get_recent_blockhash().unwrap().0,
 | 
					
						
							| 
									
										
										
										
											2019-06-06 17:40:01 -07:00
										 |  |  |         )],
 | 
					
						
							|  |  |  |         Some(&mint_keypair.pubkey()),
 | 
					
						
							|  |  |  |     );
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     assert_matches!(
 | 
					
						
							|  |  |  |         bank_client.send_message(&[&mint_keypair, &storage_keypair], message),
 | 
					
						
							|  |  |  |         Ok(_)
 | 
					
						
							|  |  |  |     );
 | 
					
						
							| 
									
										
										
										
											2019-06-13 17:53:54 -07:00
										 |  |  |     ProofStatus::Valid
 | 
					
						
							| 
									
										
										
										
											2019-06-06 17:40:01 -07:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 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() {
 | 
					
						
							| 
									
										
										
										
											2019-06-22 17:18:35 -07:00
										 |  |  |     let GenesisBlockInfo {
 | 
					
						
							|  |  |  |         mut genesis_block,
 | 
					
						
							|  |  |  |         mint_keypair,
 | 
					
						
							|  |  |  |         ..
 | 
					
						
							|  |  |  |     } = create_genesis_block(1000);
 | 
					
						
							| 
									
										
										
										
											2019-06-11 18:27:47 -07:00
										 |  |  |     genesis_block
 | 
					
						
							|  |  |  |         .native_instruction_processors
 | 
					
						
							|  |  |  |         .push(solana_storage_program::solana_storage_program!());
 | 
					
						
							| 
									
										
										
										
											2019-06-06 17:40:01 -07:00
										 |  |  |     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();
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-11 18:27:47 -07:00
										 |  |  |     let bank = Bank::new(&genesis_block);
 | 
					
						
							| 
									
										
										
										
											2019-06-06 17:40:01 -07:00
										 |  |  |     // tick the bank up until it's moved into storage segment 2
 | 
					
						
							| 
									
										
										
										
											2019-06-11 18:27:47 -07:00
										 |  |  |     // create a new bank in storage segment 2
 | 
					
						
							| 
									
										
										
										
											2019-07-09 16:48:40 -07:00
										 |  |  |     let bank = Bank::new_from_parent(
 | 
					
						
							|  |  |  |         &Arc::new(bank),
 | 
					
						
							|  |  |  |         &Pubkey::new_rand(),
 | 
					
						
							|  |  |  |         DEFAULT_SLOTS_PER_SEGMENT * 2,
 | 
					
						
							|  |  |  |     );
 | 
					
						
							| 
									
										
										
										
											2019-06-06 17:40:01 -07:00
										 |  |  |     let bank_client = BankClient::new(bank);
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     let x = 42;
 | 
					
						
							|  |  |  |     let x2 = x * 2;
 | 
					
						
							|  |  |  |     let storage_blockhash = hash(&[x2]);
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     bank_client
 | 
					
						
							|  |  |  |         .transfer(10, &mint_keypair, &replicator_pubkey)
 | 
					
						
							|  |  |  |         .unwrap();
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     let message = Message::new(storage_instruction::create_replicator_storage_account(
 | 
					
						
							|  |  |  |         &mint_pubkey,
 | 
					
						
							|  |  |  |         &Pubkey::default(),
 | 
					
						
							|  |  |  |         &replicator_pubkey,
 | 
					
						
							|  |  |  |         1,
 | 
					
						
							|  |  |  |     ));
 | 
					
						
							|  |  |  |     bank_client.send_message(&[&mint_keypair], message).unwrap();
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     let message = Message::new(storage_instruction::create_validator_storage_account(
 | 
					
						
							|  |  |  |         &mint_pubkey,
 | 
					
						
							|  |  |  |         &Pubkey::default(),
 | 
					
						
							|  |  |  |         &validator_pubkey,
 | 
					
						
							|  |  |  |         1,
 | 
					
						
							|  |  |  |     ));
 | 
					
						
							|  |  |  |     bank_client.send_message(&[&mint_keypair], message).unwrap();
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     let message = Message::new_with_payer(
 | 
					
						
							|  |  |  |         vec![storage_instruction::advertise_recent_blockhash(
 | 
					
						
							|  |  |  |             &validator_pubkey,
 | 
					
						
							|  |  |  |             storage_blockhash,
 | 
					
						
							| 
									
										
										
										
											2019-07-09 16:48:40 -07:00
										 |  |  |             1,
 | 
					
						
							| 
									
										
										
										
											2019-06-06 17:40:01 -07:00
										 |  |  |         )],
 | 
					
						
							|  |  |  |         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(),
 | 
					
						
							| 
									
										
										
										
											2019-06-13 17:53:54 -07:00
										 |  |  |             bank_client.get_recent_blockhash().unwrap().0,
 | 
					
						
							| 
									
										
										
										
											2019-06-06 17:40:01 -07:00
										 |  |  |         )],
 | 
					
						
							|  |  |  |         Some(&mint_pubkey),
 | 
					
						
							|  |  |  |     );
 | 
					
						
							|  |  |  |     assert_matches!(
 | 
					
						
							|  |  |  |         bank_client.send_message(&[&mint_keypair, &replicator_keypair], message),
 | 
					
						
							|  |  |  |         Ok(_)
 | 
					
						
							|  |  |  |     );
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-09 16:48:40 -07:00
										 |  |  |     assert_eq!(get_storage_segment(&bank_client, &validator_pubkey), 1);
 | 
					
						
							| 
									
										
										
										
											2019-06-06 17:40:01 -07:00
										 |  |  |     assert_eq!(
 | 
					
						
							|  |  |  |         get_storage_blockhash(&bank_client, &validator_pubkey),
 | 
					
						
							|  |  |  |         storage_blockhash
 | 
					
						
							|  |  |  |     );
 | 
					
						
							|  |  |  | }
 |