mirror of
https://github.com/d0zingcat/ore.git
synced 2026-05-13 23:16:52 +00:00
safe open
This commit is contained in:
@@ -39,7 +39,7 @@ pub fn process_close(accounts: &[AccountInfo<'_>], _data: &[u8]) -> ProgramResul
|
||||
miner_rewards_info.as_associated_token_account(&miner_info.key, &mint_info.key)?;
|
||||
}
|
||||
|
||||
// Payout block reward.
|
||||
// Payout block reward to winning miner.
|
||||
if block.best_hash_miner != Pubkey::default() {
|
||||
// Load recipient.
|
||||
recipient_info.as_associated_token_account(&block.best_hash_miner, &mint_info.key)?;
|
||||
|
||||
@@ -12,34 +12,42 @@ pub fn process_open(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult
|
||||
return Err(ProgramError::NotEnoughAccountKeys);
|
||||
};
|
||||
signer_info.is_signer()?;
|
||||
block_info
|
||||
.is_empty()? // Account has not been initialized
|
||||
.is_writable()? // Account is writable
|
||||
.has_seeds(&[BLOCK, &id.to_le_bytes()], &ore_api::ID)?; // Account has correct seeds
|
||||
market_info
|
||||
.as_account::<Market>(&ore_api::ID)?
|
||||
.assert(|m| m.block_id < id)?; // Only allow opening blocks in forward bias
|
||||
system_program.is_program(&system_program::ID)?;
|
||||
ore_program.is_program(&ore_api::ID)?;
|
||||
|
||||
// Create block account.
|
||||
create_program_account::<Block>(
|
||||
block_info,
|
||||
system_program,
|
||||
signer_info,
|
||||
&ore_api::ID,
|
||||
&[BLOCK, &id.to_le_bytes()],
|
||||
)?;
|
||||
let block = block_info.as_account_mut::<Block>(&ore_api::ID)?;
|
||||
block.id = id;
|
||||
block.opener = *signer_info.key;
|
||||
block.best_hash = [0; 32];
|
||||
block.best_hash_miner = Pubkey::default();
|
||||
block.reward = 0; // Set by reset instruction
|
||||
block.start_slot = u64::MAX; // Set by reset instruction
|
||||
block.end_slot = u64::MAX; // Set by reset instruction
|
||||
block.slot_hash = [0; 32]; // Set by reset instruction
|
||||
block.total_hashpower = 0;
|
||||
// Create block, if it doesn't exist.
|
||||
if block_info.data_is_empty() {
|
||||
block_info
|
||||
.is_empty()? // Account has not been initialized
|
||||
.is_writable()? // Account is writable
|
||||
.has_seeds(&[BLOCK, &id.to_le_bytes()], &ore_api::ID)?; // Account has correct seeds
|
||||
|
||||
// Create block account.
|
||||
create_program_account::<Block>(
|
||||
block_info,
|
||||
system_program,
|
||||
signer_info,
|
||||
&ore_api::ID,
|
||||
&[BLOCK, &id.to_le_bytes()],
|
||||
)?;
|
||||
let block = block_info.as_account_mut::<Block>(&ore_api::ID)?;
|
||||
block.id = id;
|
||||
block.opener = *signer_info.key;
|
||||
block.best_hash = [0; 32];
|
||||
block.best_hash_miner = Pubkey::default();
|
||||
block.reward = 0; // Set by reset instruction
|
||||
block.start_slot = u64::MAX; // Set by reset instruction
|
||||
block.end_slot = u64::MAX; // Set by reset instruction
|
||||
block.slot_hash = [0; 32]; // Set by reset instruction
|
||||
block.total_hashpower = 0;
|
||||
} else {
|
||||
block_info
|
||||
.as_account_mut::<Block>(&ore_api::ID)?
|
||||
.assert_mut(|b| b.id == id)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user