-
Notifications
You must be signed in to change notification settings - Fork 172
/
register.rs
66 lines (60 loc) · 1.94 KB
/
register.rs
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
use std::mem::size_of;
use solana_program::{
account_info::AccountInfo, entrypoint::ProgramResult, keccak::hashv,
program_error::ProgramError, pubkey::Pubkey, system_program,
};
use crate::{
instruction::RegisterArgs,
loaders::*,
state::Proof,
utils::AccountDeserialize,
utils::{create_pda, Discriminator},
PROOF,
};
/// Register generates a new hash chain for a prospective miner. Its responsibilities include:
/// 1. Initialize a new proof account.
/// 2. Generate an initial hash from the signer's key.
///
/// Safety requirements:
/// - Register is a permissionless instruction and can be invoked by any singer.
/// - Can only succeed if the provided proof acount PDA is valid (associated with the signer).
/// - Can only succeed once per signer.
/// - The provided system program must be valid.
pub fn process_register<'a, 'info>(
_program_id: &Pubkey,
accounts: &'a [AccountInfo<'info>],
data: &[u8],
) -> ProgramResult {
// Parse args
let args = RegisterArgs::try_from_bytes(data)?;
// Load accounts
let [signer, proof_info, system_program] = accounts else {
return Err(ProgramError::NotEnoughAccountKeys);
};
load_signer(signer)?;
load_uninitialized_pda(
proof_info,
&[PROOF, signer.key.as_ref()],
args.bump,
&crate::id(),
)?;
load_program(system_program, system_program::id())?;
// Initialize proof
create_pda(
proof_info,
&crate::id(),
8 + size_of::<Proof>(),
&[PROOF, signer.key.as_ref(), &[args.bump]],
system_program,
signer,
)?;
let mut proof_data = proof_info.data.borrow_mut();
proof_data[0] = Proof::discriminator() as u8;
let proof = Proof::try_from_bytes_mut(&mut proof_data)?;
proof.authority = *signer.key;
proof.claimable_rewards = 0;
proof.hash = hashv(&[signer.key.as_ref()]).into();
proof.total_hashes = 0;
proof.total_rewards = 0;
Ok(())
}