2018-09-21 15:32:15 -07:00
|
|
|
//! storage program
|
|
|
|
//! Receive mining proofs from miners, validate the answers
|
|
|
|
//! and give reward for good proofs.
|
|
|
|
|
|
|
|
use bincode::deserialize;
|
2018-11-23 15:26:47 -08:00
|
|
|
use program::ProgramError;
|
2018-10-25 11:13:08 -07:00
|
|
|
use solana_sdk::account::Account;
|
2018-11-16 08:04:46 -08:00
|
|
|
use solana_sdk::hash::Hash;
|
2018-10-25 11:13:08 -07:00
|
|
|
use solana_sdk::pubkey::Pubkey;
|
2018-09-21 15:32:15 -07:00
|
|
|
use transaction::Transaction;
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
|
|
|
pub enum StorageProgram {
|
2018-10-02 16:10:56 -07:00
|
|
|
SubmitMiningProof { sha_state: Hash },
|
2018-09-21 15:32:15 -07:00
|
|
|
}
|
|
|
|
|
2018-09-26 13:50:30 -07:00
|
|
|
pub enum StorageError {
|
|
|
|
InvalidUserData,
|
|
|
|
}
|
|
|
|
|
2018-10-26 19:44:53 -07:00
|
|
|
const STORAGE_PROGRAM_ID: [u8; 32] = [
|
|
|
|
130, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0,
|
|
|
|
];
|
2018-09-21 15:32:15 -07:00
|
|
|
|
2018-11-23 12:45:34 -08:00
|
|
|
pub fn check_id(program_id: &Pubkey) -> bool {
|
|
|
|
program_id.as_ref() == STORAGE_PROGRAM_ID
|
|
|
|
}
|
2018-09-21 15:32:15 -07:00
|
|
|
|
2018-11-23 12:45:34 -08:00
|
|
|
pub fn id() -> Pubkey {
|
|
|
|
Pubkey::new(&STORAGE_PROGRAM_ID)
|
|
|
|
}
|
2018-09-21 15:32:15 -07:00
|
|
|
|
2018-11-23 12:45:34 -08:00
|
|
|
pub fn get_balance(account: &Account) -> u64 {
|
|
|
|
account.tokens
|
|
|
|
}
|
2018-09-21 15:32:15 -07:00
|
|
|
|
2018-11-23 14:14:13 -08:00
|
|
|
pub fn process_instruction(
|
2018-11-23 12:45:34 -08:00
|
|
|
tx: &Transaction,
|
|
|
|
pix: usize,
|
|
|
|
_accounts: &mut [&mut Account],
|
|
|
|
) -> Result<(), StorageError> {
|
|
|
|
if let Ok(syscall) = deserialize(tx.userdata(pix)) {
|
|
|
|
match syscall {
|
|
|
|
StorageProgram::SubmitMiningProof { sha_state } => {
|
|
|
|
info!("Mining proof submitted with state {:?}", sha_state);
|
|
|
|
return Ok(());
|
2018-09-21 15:32:15 -07:00
|
|
|
}
|
|
|
|
}
|
2018-11-23 12:45:34 -08:00
|
|
|
} else {
|
|
|
|
return Err(StorageError::InvalidUserData);
|
2018-09-21 15:32:15 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-23 15:26:47 -08:00
|
|
|
pub fn process(
|
|
|
|
tx: &Transaction,
|
|
|
|
instruction_index: usize,
|
|
|
|
accounts: &mut [&mut Account],
|
|
|
|
) -> std::result::Result<(), ProgramError> {
|
|
|
|
process_instruction(&tx, instruction_index, accounts).map_err(|_| ProgramError::RuntimeError)
|
|
|
|
}
|
|
|
|
|
2018-09-21 15:32:15 -07:00
|
|
|
#[cfg(test)]
|
2018-09-28 16:16:35 -07:00
|
|
|
mod test {
|
|
|
|
use super::*;
|
|
|
|
use signature::Keypair;
|
|
|
|
use signature::KeypairUtil;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_storage_tx() {
|
|
|
|
let keypair = Keypair::new();
|
2018-11-23 12:45:34 -08:00
|
|
|
let tx = Transaction::new(&keypair, &[], id(), &(), Default::default(), 0);
|
2018-11-23 14:14:13 -08:00
|
|
|
assert!(process_instruction(&tx, 0, &mut []).is_err());
|
2018-09-28 16:16:35 -07:00
|
|
|
}
|
|
|
|
}
|