From 6bdc8cbca22b1bd6fe47fcb9d8b1077d5bfad429 Mon Sep 17 00:00:00 2001 From: Hardhat Chad Date: Thu, 27 Jun 2024 15:49:45 +0000 Subject: [PATCH 1/2] split miner authority --- src/instruction.rs | 3 ++- src/loaders.rs | 33 +++++++++++++++++++++++++++++++++ src/processor/mine.rs | 2 +- src/processor/open.rs | 4 +++- src/state/proof.rs | 3 +++ 5 files changed, 42 insertions(+), 3 deletions(-) diff --git a/src/instruction.rs b/src/instruction.rs index f5ebd74..dade54e 100644 --- a/src/instruction.rs +++ b/src/instruction.rs @@ -214,12 +214,13 @@ pub fn reset(signer: Pubkey) -> Instruction { } /// Builds an open instruction. -pub fn open(signer: Pubkey) -> Instruction { +pub fn open(signer: Pubkey, miner: Pubkey) -> Instruction { let proof_pda = Pubkey::find_program_address(&[PROOF, signer.as_ref()], &crate::id()); Instruction { program_id: crate::id(), accounts: vec![ AccountMeta::new(signer, true), + AccountMeta::new(miner, true), AccountMeta::new(proof_pda.0, false), AccountMeta::new_readonly(solana_program::system_program::id(), false), AccountMeta::new_readonly(sysvar::slot_hashes::id(), false), diff --git a/src/loaders.rs b/src/loaders.rs index 73d7c50..8cc7e07 100644 --- a/src/loaders.rs +++ b/src/loaders.rs @@ -158,6 +158,39 @@ pub fn load_proof<'a, 'info>( Ok(()) } +/// Errors if: +/// - Owner is not Ore program. +/// - Data is empty. +/// - Data cannot deserialize into a proof account. +/// - Proof miner does not match the expected address. +/// - Expected to be writable, but is not. +pub fn load_proof_with_miner<'a, 'info>( + info: &'a AccountInfo<'info>, + miner: &Pubkey, + is_writable: bool, +) -> Result<(), ProgramError> { + if info.owner.ne(&crate::id()) { + return Err(ProgramError::InvalidAccountOwner); + } + + if info.data_is_empty() { + return Err(ProgramError::UninitializedAccount); + } + + let proof_data = info.data.borrow(); + let proof = Proof::try_from_bytes(&proof_data)?; + + if proof.miner.ne(&miner) { + return Err(ProgramError::InvalidAccountData); + } + + if is_writable && !info.is_writable { + return Err(ProgramError::InvalidAccountData); + } + + Ok(()) +} + /// Errors if: /// - Owner is not Ore program. /// - Address does not match the expected address. diff --git a/src/processor/mine.rs b/src/processor/mine.rs index 265bc61..a3df1c8 100644 --- a/src/processor/mine.rs +++ b/src/processor/mine.rs @@ -57,7 +57,7 @@ pub fn process_mine<'a, 'info>( load_signer(signer)?; load_any_bus(bus_info, true)?; load_config(config_info, false)?; - load_proof(proof_info, signer.key, true)?; + load_proof_with_miner(proof_info, signer.key, true)?; load_sysvar(instructions_sysvar, sysvar::instructions::id())?; load_sysvar(slot_hashes_sysvar, sysvar::slot_hashes::id())?; diff --git a/src/processor/open.rs b/src/processor/open.rs index 8db78c7..f9ea4a7 100644 --- a/src/processor/open.rs +++ b/src/processor/open.rs @@ -39,10 +39,11 @@ pub fn process_open<'a, 'info>( let args = OpenArgs::try_from_bytes(data)?; // Load accounts - let [signer, proof_info, system_program, slot_hashes_info] = accounts else { + let [signer, miner_info, proof_info, system_program, slot_hashes_info] = accounts else { return Err(ProgramError::NotEnoughAccountKeys); }; load_signer(signer)?; + load_signer(miner_info)?; load_uninitialized_pda( proof_info, &[PROOF, signer.key.as_ref()], @@ -75,6 +76,7 @@ pub fn process_open<'a, 'info>( proof.last_hash = [0; 32]; proof.last_hash_at = clock.unix_timestamp; proof.last_stake_at = clock.unix_timestamp; + proof.miner = *miner_info.key; proof.total_hashes = 0; proof.total_rewards = 0; diff --git a/src/state/proof.rs b/src/state/proof.rs index 5cc2f9c..6b94a8a 100644 --- a/src/state/proof.rs +++ b/src/state/proof.rs @@ -30,6 +30,9 @@ pub struct Proof { /// The last time stake was deposited into this account. pub last_stake_at: i64, + /// The keypair which can submit hashes for mining. + pub miner: Pubkey, + /// The total lifetime hashes provided by this miner. pub total_hashes: u64, From 2b0b28b8131687f1d6e919ed7f2101f4d47be795 Mon Sep 17 00:00:00 2001 From: Hardhat Chad Date: Thu, 27 Jun 2024 15:51:56 +0000 Subject: [PATCH 2/2] comment --- src/state/proof.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/state/proof.rs b/src/state/proof.rs index 6b94a8a..1d1aa76 100644 --- a/src/state/proof.rs +++ b/src/state/proof.rs @@ -30,7 +30,7 @@ pub struct Proof { /// The last time stake was deposited into this account. pub last_stake_at: i64, - /// The keypair which can submit hashes for mining. + /// The keypair which has permission to submit hashes for mining. pub miner: Pubkey, /// The total lifetime hashes provided by this miner.