forked from vecheslav/randomness-oracle
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprocessor.rs
More file actions
107 lines (89 loc) · 3.52 KB
/
Copy pathprocessor.rs
File metadata and controls
107 lines (89 loc) · 3.52 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//! Program state processor.
use crate::{
instruction::RandomnessOracleInstruction,
state::{InitRandomnessOracleParams, RandomnessOracle},
utils::*,
};
use borsh::BorshDeserialize;
use solana_program::{
account_info::{next_account_info, AccountInfo},
entrypoint::ProgramResult,
msg,
program_error::ProgramError,
program_pack::Pack,
pubkey::Pubkey,
sysvar::Sysvar,
};
/// Program state handler.
pub struct Processor {}
impl Processor {
/// Process `InitRandomnessOracle` instruction.
pub fn init_randomness_oracle(program_id: &Pubkey, accounts: &[AccountInfo]) -> ProgramResult {
let account_info_iter = &mut accounts.iter();
let randomness_oracle_info = next_account_info(account_info_iter)?;
let authority_info = next_account_info(account_info_iter)?;
let clock_info = next_account_info(account_info_iter)?;
let clock = solana_program::clock::Clock::from_account_info(clock_info)?;
// Check signer
assert_signer(authority_info)?;
// Check random oracle owner
assert_owned_by(randomness_oracle_info, program_id)?;
// Get state
let mut randomness_oracle =
RandomnessOracle::unpack_unchecked(&randomness_oracle_info.data.borrow())?;
// Initialize
randomness_oracle.init(InitRandomnessOracleParams {
authority: *authority_info.key,
slot: clock.slot,
});
// Save state
RandomnessOracle::pack(randomness_oracle, *randomness_oracle_info.data.borrow_mut())?;
Ok(())
}
/// Process `UpdateRandomnessOracle` instruction.
pub fn update_randomness_oracle(
program_id: &Pubkey,
accounts: &[AccountInfo],
value: [u8; 32],
) -> ProgramResult {
let account_info_iter = &mut accounts.iter();
let randomness_oracle_info = next_account_info(account_info_iter)?;
let authority_info = next_account_info(account_info_iter)?;
let clock_info = next_account_info(account_info_iter)?;
let clock = solana_program::clock::Clock::from_account_info(clock_info)?;
// Check signer
assert_signer(authority_info)?;
// Check random oracle owner
assert_owned_by(randomness_oracle_info, program_id)?;
// Get state
let mut randomness_oracle =
RandomnessOracle::unpack(&randomness_oracle_info.data.borrow())?;
// Check random oracle authority
if randomness_oracle.authority != *authority_info.key {
return Err(ProgramError::InvalidArgument);
}
// Update
randomness_oracle.update(value, clock.slot);
// Save state
RandomnessOracle::pack(randomness_oracle, *randomness_oracle_info.data.borrow_mut())?;
Ok(())
}
/// Instruction processing router.
pub fn process_instruction(
program_id: &Pubkey,
accounts: &[AccountInfo],
input: &[u8],
) -> ProgramResult {
let instruction = RandomnessOracleInstruction::try_from_slice(input)?;
match instruction {
RandomnessOracleInstruction::InitRandomnessOracle => {
msg!("RandomnessOracleInstruction: InitRandomnessOracle");
Self::init_randomness_oracle(program_id, accounts)
}
RandomnessOracleInstruction::UpdateRandomnessOracle { value } => {
msg!("RandomnessOracleInstruction: UpdateRandomnessOracle");
Self::update_randomness_oracle(program_id, accounts, value)
}
}
}
}