2019-03-22 21:18:51 -06:00
|
|
|
use crate::id;
|
2019-05-21 11:07:13 -07:00
|
|
|
use crate::storage_contract::{CheckedProof, STORAGE_ACCOUNT_SPACE};
|
2019-03-22 14:30:18 -06:00
|
|
|
use serde_derive::{Deserialize, Serialize};
|
|
|
|
use solana_sdk::hash::Hash;
|
2019-03-22 22:02:00 -06:00
|
|
|
use solana_sdk::instruction::{AccountMeta, Instruction};
|
2019-03-22 21:18:51 -06:00
|
|
|
use solana_sdk::pubkey::Pubkey;
|
2019-03-22 14:30:18 -06:00
|
|
|
use solana_sdk::signature::Signature;
|
2019-06-11 18:27:47 -07:00
|
|
|
use solana_sdk::syscall::current;
|
2019-05-21 11:07:13 -07:00
|
|
|
use solana_sdk::system_instruction;
|
2019-05-24 14:49:10 -07:00
|
|
|
use std::collections::HashMap;
|
2019-03-22 14:30:18 -06:00
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
|
|
|
pub enum StorageInstruction {
|
2019-05-23 14:50:23 -07:00
|
|
|
/// Initialize the account as a mining pool, validator or replicator
|
|
|
|
///
|
|
|
|
/// Expects 1 Account:
|
|
|
|
/// 0 - Account to be initialized
|
|
|
|
InitializeMiningPool,
|
2019-06-04 14:52:52 -07:00
|
|
|
InitializeValidatorStorage {
|
|
|
|
owner: Pubkey,
|
|
|
|
},
|
|
|
|
InitializeReplicatorStorage {
|
|
|
|
owner: Pubkey,
|
|
|
|
},
|
2019-05-23 14:50:23 -07:00
|
|
|
|
2019-03-22 14:30:18 -06:00
|
|
|
SubmitMiningProof {
|
|
|
|
sha_state: Hash,
|
2019-06-11 18:27:47 -07:00
|
|
|
segment_index: usize,
|
2019-03-22 14:30:18 -06:00
|
|
|
signature: Signature,
|
|
|
|
},
|
|
|
|
AdvertiseStorageRecentBlockhash {
|
|
|
|
hash: Hash,
|
2019-05-03 16:27:53 -07:00
|
|
|
slot: u64,
|
2019-03-22 14:30:18 -06:00
|
|
|
},
|
2019-05-23 14:50:23 -07:00
|
|
|
/// Redeem storage reward credits
|
|
|
|
///
|
|
|
|
/// Expects 1 Account:
|
|
|
|
/// 0 - Storage account with credits to redeem
|
|
|
|
/// 1 - MiningPool account to redeem credits from
|
2019-06-11 18:27:47 -07:00
|
|
|
ClaimStorageReward,
|
2019-03-22 14:30:18 -06:00
|
|
|
ProofValidation {
|
2019-05-24 14:49:10 -07:00
|
|
|
segment: u64,
|
|
|
|
proofs: Vec<(Pubkey, Vec<CheckedProof>)>,
|
2019-03-22 14:30:18 -06:00
|
|
|
},
|
|
|
|
}
|
2019-03-22 21:18:51 -06:00
|
|
|
|
2019-05-23 14:50:23 -07:00
|
|
|
pub fn create_validator_storage_account(
|
|
|
|
from_pubkey: &Pubkey,
|
2019-06-04 14:52:52 -07:00
|
|
|
storage_owner: &Pubkey,
|
2019-05-23 14:50:23 -07:00
|
|
|
storage_pubkey: &Pubkey,
|
|
|
|
lamports: u64,
|
|
|
|
) -> Vec<Instruction> {
|
|
|
|
vec![
|
|
|
|
system_instruction::create_account(
|
|
|
|
from_pubkey,
|
|
|
|
storage_pubkey,
|
|
|
|
lamports,
|
|
|
|
STORAGE_ACCOUNT_SPACE,
|
|
|
|
&id(),
|
|
|
|
),
|
|
|
|
Instruction::new(
|
|
|
|
id(),
|
2019-06-04 14:52:52 -07:00
|
|
|
&StorageInstruction::InitializeValidatorStorage {
|
|
|
|
owner: *storage_owner,
|
|
|
|
},
|
2019-05-23 14:50:23 -07:00
|
|
|
vec![AccountMeta::new(*storage_pubkey, false)],
|
|
|
|
),
|
|
|
|
]
|
2019-05-21 11:07:13 -07:00
|
|
|
}
|
|
|
|
|
2019-05-23 14:50:23 -07:00
|
|
|
pub fn create_replicator_storage_account(
|
2019-04-03 09:45:57 -06:00
|
|
|
from_pubkey: &Pubkey,
|
2019-06-04 14:52:52 -07:00
|
|
|
storage_owner: &Pubkey,
|
2019-05-23 14:50:23 -07:00
|
|
|
storage_pubkey: &Pubkey,
|
|
|
|
lamports: u64,
|
|
|
|
) -> Vec<Instruction> {
|
|
|
|
vec![
|
|
|
|
system_instruction::create_account(
|
|
|
|
from_pubkey,
|
|
|
|
storage_pubkey,
|
|
|
|
lamports,
|
|
|
|
STORAGE_ACCOUNT_SPACE,
|
|
|
|
&id(),
|
|
|
|
),
|
|
|
|
Instruction::new(
|
|
|
|
id(),
|
2019-06-04 14:52:52 -07:00
|
|
|
&StorageInstruction::InitializeReplicatorStorage {
|
|
|
|
owner: *storage_owner,
|
|
|
|
},
|
2019-05-23 14:50:23 -07:00
|
|
|
vec![AccountMeta::new(*storage_pubkey, false)],
|
|
|
|
),
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn create_mining_pool_account(
|
|
|
|
from_pubkey: &Pubkey,
|
|
|
|
storage_pubkey: &Pubkey,
|
|
|
|
lamports: u64,
|
|
|
|
) -> Vec<Instruction> {
|
|
|
|
vec![
|
|
|
|
system_instruction::create_account(
|
|
|
|
from_pubkey,
|
|
|
|
storage_pubkey,
|
|
|
|
lamports,
|
|
|
|
STORAGE_ACCOUNT_SPACE,
|
|
|
|
&id(),
|
|
|
|
),
|
|
|
|
Instruction::new(
|
|
|
|
id(),
|
|
|
|
&StorageInstruction::InitializeMiningPool,
|
|
|
|
vec![AccountMeta::new(*storage_pubkey, false)],
|
|
|
|
),
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn mining_proof(
|
|
|
|
storage_pubkey: &Pubkey,
|
2019-04-03 09:45:57 -06:00
|
|
|
sha_state: Hash,
|
2019-06-11 18:27:47 -07:00
|
|
|
segment_index: usize,
|
2019-04-03 09:45:57 -06:00
|
|
|
signature: Signature,
|
|
|
|
) -> Instruction {
|
|
|
|
let storage_instruction = StorageInstruction::SubmitMiningProof {
|
|
|
|
sha_state,
|
2019-06-11 18:27:47 -07:00
|
|
|
segment_index,
|
2019-04-03 09:45:57 -06:00
|
|
|
signature,
|
|
|
|
};
|
2019-05-31 16:29:21 -06:00
|
|
|
let account_metas = vec![
|
|
|
|
AccountMeta::new(*storage_pubkey, true),
|
2019-06-11 18:27:47 -07:00
|
|
|
AccountMeta::new(current::id(), false),
|
2019-05-31 16:29:21 -06:00
|
|
|
];
|
2019-04-03 09:45:57 -06:00
|
|
|
Instruction::new(id(), &storage_instruction, account_metas)
|
|
|
|
}
|
2019-03-22 21:18:51 -06:00
|
|
|
|
2019-04-03 09:45:57 -06:00
|
|
|
pub fn advertise_recent_blockhash(
|
2019-05-23 14:50:23 -07:00
|
|
|
storage_pubkey: &Pubkey,
|
2019-04-03 09:45:57 -06:00
|
|
|
storage_hash: Hash,
|
2019-05-03 16:27:53 -07:00
|
|
|
slot: u64,
|
2019-04-03 09:45:57 -06:00
|
|
|
) -> Instruction {
|
|
|
|
let storage_instruction = StorageInstruction::AdvertiseStorageRecentBlockhash {
|
|
|
|
hash: storage_hash,
|
2019-05-03 16:27:53 -07:00
|
|
|
slot,
|
2019-04-03 09:45:57 -06:00
|
|
|
};
|
2019-05-31 16:29:21 -06:00
|
|
|
let account_metas = vec![
|
|
|
|
AccountMeta::new(*storage_pubkey, true),
|
2019-06-11 18:27:47 -07:00
|
|
|
AccountMeta::new(current::id(), false),
|
2019-05-31 16:29:21 -06:00
|
|
|
];
|
2019-04-03 09:45:57 -06:00
|
|
|
Instruction::new(id(), &storage_instruction, account_metas)
|
|
|
|
}
|
2019-03-22 21:18:51 -06:00
|
|
|
|
2019-05-24 14:49:10 -07:00
|
|
|
pub fn proof_validation<S: std::hash::BuildHasher>(
|
2019-05-23 14:50:23 -07:00
|
|
|
storage_pubkey: &Pubkey,
|
2019-05-24 14:49:10 -07:00
|
|
|
segment: u64,
|
|
|
|
checked_proofs: HashMap<Pubkey, Vec<CheckedProof>, S>,
|
2019-05-23 14:50:23 -07:00
|
|
|
) -> Instruction {
|
|
|
|
let mut account_metas = vec![AccountMeta::new(*storage_pubkey, true)];
|
2019-05-24 14:49:10 -07:00
|
|
|
let mut proofs = vec![];
|
|
|
|
checked_proofs.into_iter().for_each(|(id, p)| {
|
|
|
|
proofs.push((id, p));
|
|
|
|
account_metas.push(AccountMeta::new(id, false))
|
2019-04-03 15:51:05 -07:00
|
|
|
});
|
2019-05-24 14:49:10 -07:00
|
|
|
let storage_instruction = StorageInstruction::ProofValidation { segment, proofs };
|
2019-04-03 09:45:57 -06:00
|
|
|
Instruction::new(id(), &storage_instruction, account_metas)
|
|
|
|
}
|
2019-03-22 21:18:51 -06:00
|
|
|
|
2019-06-11 18:27:47 -07:00
|
|
|
pub fn claim_reward(storage_pubkey: &Pubkey, mining_pool_pubkey: &Pubkey) -> Instruction {
|
|
|
|
let storage_instruction = StorageInstruction::ClaimStorageReward;
|
2019-05-23 14:50:23 -07:00
|
|
|
let account_metas = vec![
|
|
|
|
AccountMeta::new(*storage_pubkey, false),
|
|
|
|
AccountMeta::new(*mining_pool_pubkey, false),
|
|
|
|
];
|
2019-04-03 09:45:57 -06:00
|
|
|
Instruction::new(id(), &storage_instruction, account_metas)
|
2019-03-22 21:18:51 -06:00
|
|
|
}
|