resolve diffs

This commit is contained in:
alex
2024-07-12 15:21:30 -07:00
12 changed files with 323 additions and 3787 deletions

4005
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -3,7 +3,7 @@ resolver = "2"
members = ["api", "program", "utils"]
[workspace.package]
version = "2.0.0"
version = "2.0.0-beta.1"
edition = "2021"
license = "Apache-2.0"
homepage = "https://ore.supply"
@@ -16,18 +16,14 @@ keywords = ["solana", "crypto", "mining"]
array-const-fn-init = "0.1.1"
bytemuck = "1.14.3"
const-crypto = "0.1.0"
drillx = { git = "https://github.com/regolith-labs/drillx", branch = "relayer-integration", features = [
"solana",
] }
drillx = { version = "2.0.0-beta", features = ["solana"] }
mpl-token-metadata = "4.1.2"
num_enum = "0.7.2"
ore-api = { path = "api" }
ore-utils = { path = "utils", features = ["spl"] }
ore-api = { path = "api", version = "2.0.0-beta.1" }
ore-utils = { path = "utils", version = "2.0.0-beta.1", features = ["spl"] }
shank = "0.3.0"
solana-program = "1.18"
solana-program = "^1.18"
spl-token = { version = "^4", features = ["no-entrypoint"] }
spl-associated-token-account = { version = "^2.2", features = [
"no-entrypoint",
] }
spl-associated-token-account = { version = "^2.3", features = [ "no-entrypoint" ] }
static_assertions = "1.1.0"
thiserror = "1.0.57"

View File

@@ -79,7 +79,7 @@ pub const TREASURY: &[u8] = b"treasury";
/// Noise for deriving the mint pda
pub const MINT_NOISE: [u8; 16] = [
166, 199, 85, 221, 225, 119, 21, 185, 160, 82, 242, 237, 194, 84, 250, 252,
89, 157, 88, 232, 243, 249, 197, 132, 199, 49, 19, 234, 91, 94, 150, 41,
];
/// The name for token metadata.

View File

@@ -50,8 +50,11 @@ pub enum OreInstruction {
#[account(0, name = "ore_program", desc = "Ore program")]
#[account(1, name = "signer", desc = "Signer", signer)]
#[account(2, name = "proof", desc = "Ore proof account", writable)]
#[account(3, name = "system_program", desc = "Solana system program")]
#[account(2, name = "miner", desc = "Address to be initialized as the miner")]
#[account(3, name = "payer", desc = "Account to pay for account creation", writable, signer)]
#[account(4, name = "proof", desc = "Ore proof account", writable)]
#[account(5, name = "system_program", desc = "Solana system program")]
#[account(6, name = "slot_hashes", desc = "Solana slot hashes sysvar")]
Open = 4,
#[account(0, name = "ore_program", desc = "Ore program")]
@@ -228,6 +231,21 @@ pub fn close(signer: Pubkey) -> Instruction {
}
}
/// Builds a crown instruction.
pub fn crown(signer: Pubkey, current_top_staker: 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(CONFIG_ADDRESS, false),
AccountMeta::new_readonly(current_top_staker, false),
AccountMeta::new_readonly(proof_pda.0, false),
],
data: OreInstruction::Crown.to_vec(),
}
}
/// Builds a mine instruction.
pub fn mine(
miner: Pubkey,

View File

@@ -9,5 +9,4 @@ pub(crate) use ore_utils as utils;
use solana_program::declare_id;
// declare_id!("mineRHF5r6S7HyD9SppBfVMXMavDkJsxwGesEvxZr2A");
declare_id!("Fb9XVEimYj5KzVL6mLdbah3CGYmGP881uddBahui5RHt");
declare_id!("oreV2ZymfyeXgNgBdqMkumTqqAprVqgBWQfoYkrtKWQ");

View File

@@ -10,7 +10,7 @@ use super::AccountDiscriminator;
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq, Pod, ShankAccount, Zeroable)]
pub struct Config {
/// The admin authority with permission to update the difficulty.
// TODO Remove this
pub admin: Pubkey,
/// The base reward rate paid out for a hash of minimum difficulty.

View File

@@ -28,8 +28,4 @@ spl-token.workspace = true
spl-associated-token-account.workspace = true
[dev-dependencies]
bs64 = "0.1.2"
rand = "0.8.5"
solana-program-test = "^1.18"
solana-sdk = "^1.18"
tokio = { version = "1.35", features = ["full"] }

View File

@@ -40,11 +40,11 @@ pub fn process_crown<'a, 'info>(
// If top staker is the default null address, skip this.
let mut config_data = config_info.data.borrow_mut();
let config = Config::try_from_bytes_mut(&mut config_data)?;
let proof_data = proof_info.data.borrow();
let proof = Proof::try_from_bytes(&proof_data)?;
if config.top_staker.ne(&Pubkey::new_from_array([0; 32])) {
// Load current top staker
load_any_proof(proof_info, false)?;
let proof_data = proof_info.data.borrow();
let proof = Proof::try_from_bytes(&proof_data)?;
if proof_info.key.ne(&config.top_staker) {
return Ok(());
}

View File

@@ -138,7 +138,6 @@ pub fn process_initialize<'a, 'info>(
let mut config_data = config_info.data.borrow_mut();
config_data[0] = Config::discriminator() as u8;
let config = Config::try_from_bytes_mut(&mut config_data)?;
config.admin = *signer.key;
config.base_reward_rate = INITIAL_BASE_REWARD_RATE;
config.last_reset_at = 0;
config.max_stake = 0;

View File

@@ -97,7 +97,6 @@ pub fn process_mine<'a, 'info>(
.base_reward_rate
.checked_mul(2u64.checked_pow(difficulty).unwrap())
.unwrap();
sol_log(&format!("reward: {}", reward));
// Apply staking multiplier.
// If user has greater than or equal to the max stake on the network, they receive 2x multiplier.
@@ -105,13 +104,11 @@ pub fn process_mine<'a, 'info>(
// if the miner's last stake deposit was more than one minute ago.
let t = clock.unix_timestamp;
if config.max_stake.gt(&0) && proof.last_stake_at.saturating_add(ONE_MINUTE).le(&t) {
let staking_reward = proof
.balance
.min(config.max_stake)
.checked_mul(reward)
let staking_reward = (reward as u128)
.checked_mul(proof.balance.min(config.max_stake) as u128)
.unwrap()
.checked_div(config.max_stake)
.unwrap();
.checked_div(config.max_stake as u128)
.unwrap() as u64;
reward = reward.checked_add(staking_reward).unwrap();
}
@@ -124,16 +121,14 @@ pub fn process_mine<'a, 'info>(
// Apply liveness penalty.
let t_liveness = t_target.saturating_add(TOLERANCE);
let ratio = reward
.checked_mul(t.checked_sub(t_liveness).unwrap() as u64)
.unwrap()
.checked_div(ONE_MINUTE as u64)
.unwrap();
sol_log(&format!("ratio: {}", ratio));
if t.gt(&t_liveness) {
let reward_diff = reward.saturating_sub(ratio);
sol_log(&format!("reward_diff: {}", reward_diff));
reward = reward_diff;
reward = reward.saturating_sub(
reward
.checked_mul(t.checked_sub(t_liveness).unwrap() as u64)
.unwrap()
.checked_div(ONE_MINUTE as u64)
.unwrap(),
);
}
// Limit payout amount to whatever is left in the bus
@@ -200,6 +195,7 @@ fn introspect_transaction(msg: &[u8]) -> Result<bool, SanitizeError> {
if program_id.ne(&ore_api::ID) {
return Ok(false);
}
c += 2;
if let Ok(ix) = OreInstruction::try_from(read_u8(&mut c, msg)?) {
if ix.ne(&OreInstruction::Mine) {
return Ok(false);

View File

@@ -33,11 +33,13 @@ pub fn process_open<'a, 'info>(
let args = OpenArgs::try_from_bytes(data)?;
// Load accounts
let [signer, miner_info, proof_info, system_program, slot_hashes_info] = accounts else {
let [signer, miner_info, payer_info, proof_info, system_program, slot_hashes_info] = accounts
else {
return Err(ProgramError::NotEnoughAccountKeys);
};
load_signer(signer)?;
load_signer(miner_info)?;
load_system_account(miner_info, false)?;
load_signer(payer_info)?;
load_uninitialized_pda(
proof_info,
&[PROOF, signer.key.as_ref()],
@@ -54,7 +56,7 @@ pub fn process_open<'a, 'info>(
8 + size_of::<Proof>(),
&[PROOF, signer.key.as_ref(), &[args.bump]],
system_program,
miner_info,
payer_info,
)?;
let clock = Clock::get().or(Err(ProgramError::InvalidAccountData))?;
let mut proof_data = proof_info.data.borrow_mut();

View File

@@ -148,7 +148,7 @@ pub(crate) fn calculate_new_reward_rate(current_rate: u64, epoch_rewards: u64) -
// Smooth reward rate so it cannot change by more than a constant factor from one epoch to the next.
let new_rate_min = current_rate.saturating_div(SMOOTHING_FACTOR);
let new_rate_max = current_rate.saturating_mul(SMOOTHING_FACTOR);
let new_rate_smoothed = new_rate_min.max(new_rate_max.min(new_rate));
let new_rate_smoothed = new_rate.min(new_rate_max).max(new_rate_min);
// Prevent reward rate from dropping below 1 or exceeding BUS_EPOCH_REWARDS and return.
new_rate_smoothed.max(1).min(BUS_EPOCH_REWARDS)
@@ -182,8 +182,10 @@ mod tests {
#[test]
fn test_calculate_new_reward_rate_lower() {
let current_rate = 1000;
let new_rate =
calculate_new_reward_rate(current_rate, TARGET_EPOCH_REWARDS.saturating_add(1_000_000));
let new_rate = calculate_new_reward_rate(
current_rate,
TARGET_EPOCH_REWARDS.saturating_add(1_000_000_000),
);
assert!(new_rate.lt(&current_rate));
}
@@ -202,9 +204,10 @@ mod tests {
#[test]
fn test_calculate_new_reward_rate_higher() {
let current_rate = 1000;
let new_rate =
calculate_new_reward_rate(current_rate, TARGET_EPOCH_REWARDS.saturating_sub(1_000_000));
println!("{:?} {:?}", new_rate, current_rate);
let new_rate = calculate_new_reward_rate(
current_rate,
TARGET_EPOCH_REWARDS.saturating_sub(1_000_000_000),
);
assert!(new_rate.gt(&current_rate));
}