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-09-27 07:49:26 -07:00
|
|
|
use solana_program_interface::account::Account;
|
|
|
|
|
use solana_program_interface::pubkey::Pubkey;
|
2018-09-21 15:32:15 -07:00
|
|
|
use transaction::Transaction;
|
|
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
|
|
|
|
pub enum StorageProgram {
|
|
|
|
|
SubmitMiningProof { sha_state: [u8; 32] },
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-26 13:50:30 -07:00
|
|
|
pub enum StorageError {
|
|
|
|
|
InvalidUserData,
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-21 15:32:15 -07:00
|
|
|
pub const STORAGE_PROGRAM_ID: [u8; 32] = [1u8; 32];
|
|
|
|
|
|
|
|
|
|
impl StorageProgram {
|
|
|
|
|
pub fn check_id(program_id: &Pubkey) -> bool {
|
|
|
|
|
program_id.as_ref() == STORAGE_PROGRAM_ID
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn id() -> Pubkey {
|
|
|
|
|
Pubkey::new(&STORAGE_PROGRAM_ID)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn get_balance(account: &Account) -> i64 {
|
|
|
|
|
account.tokens
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-26 13:50:30 -07:00
|
|
|
pub fn process_transaction(
|
|
|
|
|
tx: &Transaction,
|
|
|
|
|
_accounts: &mut [Account],
|
|
|
|
|
) -> Result<(), StorageError> {
|
|
|
|
|
if let Ok(syscall) = deserialize(&tx.userdata) {
|
|
|
|
|
match syscall {
|
|
|
|
|
StorageProgram::SubmitMiningProof { sha_state } => {
|
|
|
|
|
info!("Mining proof submitted with state {}", sha_state[0]);
|
|
|
|
|
return Ok(());
|
|
|
|
|
}
|
2018-09-21 15:32:15 -07:00
|
|
|
}
|
2018-09-26 13:50:30 -07:00
|
|
|
} else {
|
|
|
|
|
return Err(StorageError::InvalidUserData);
|
2018-09-21 15:32:15 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod test {}
|