1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//! storage program
//!  Receive mining proofs from miners, validate the answers
//!  and give reward for good proofs.

use bincode::deserialize;
use buffett_interface::account::Account;
use buffett_interface::pubkey::Pubkey;
use crate::transaction::Transaction;

#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum StorageProgram {
    SubmitMiningProof { sha_state: [u8; 32] },
}

pub enum StorageError {
    InvalidUserData,
}

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
    }

    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(());
                }
            }
        } else {
            return Err(StorageError::InvalidUserData);
        }
    }
}

#[cfg(test)]
mod test {}