mirror of
https://github.com/d0zingcat/ore.git
synced 2026-05-24 15:10:38 +00:00
groundwork for pick your difficulty
This commit is contained in:
@@ -48,6 +48,9 @@ static_assertions::const_assert!(
|
||||
/// The seed of the bus account PDA.
|
||||
pub const BUS: &[u8] = b"bus";
|
||||
|
||||
/// The seed of the config account PDA.
|
||||
pub const CONFIG: &[u8] = b"config";
|
||||
|
||||
/// The seed of the metadata account PDA.
|
||||
pub const METADATA: &[u8] = b"metadata";
|
||||
|
||||
@@ -86,6 +89,10 @@ pub const BUS_ADDRESSES: [Pubkey; BUS_COUNT] = [
|
||||
pubkey!("Ay5N9vKS2Tyo2M9u9TFt59N1XbxdW93C7UrFZW3h8sMC"),
|
||||
];
|
||||
|
||||
// TODO
|
||||
/// The address of the config account.
|
||||
pub const CONFIG_ADDRESS: Pubkey = pubkey!("FTap9fv2GPpWGqrLj3o4c9nHH7p36ih7NbSWHnrkQYqa");
|
||||
|
||||
/// The address of the mint metadata account.
|
||||
pub const METADATA_ADDRESS: Pubkey = pubkey!("2nXZSxfjELuRatcoY64yHdFLZFi3mtesxobHmsoU3Dag");
|
||||
|
||||
|
||||
@@ -8,10 +8,19 @@ use solana_program::{
|
||||
};
|
||||
|
||||
use crate::{
|
||||
impl_instruction_from_bytes, impl_to_bytes, state::Hash, BUS, METADATA, MINT, MINT_ADDRESS,
|
||||
MINT_NOISE, PROOF, TREASURY, TREASURY_ADDRESS,
|
||||
impl_instruction_from_bytes, impl_to_bytes, state::Hash, BUS, CONFIG, CONFIG_ADDRESS, METADATA,
|
||||
MINT, MINT_ADDRESS, MINT_NOISE, PROOF, TREASURY, TREASURY_ADDRESS,
|
||||
};
|
||||
|
||||
// TODO Stake
|
||||
// TODO Unstake
|
||||
|
||||
// TODO Upgrade (v1 to v2 token)
|
||||
// TODO Downgrade (v2 to v1 token)
|
||||
|
||||
// TODO Personalized difficulty
|
||||
// TODO Payout rewards based on multiplier and difficulty setting
|
||||
|
||||
#[repr(u8)]
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq, ShankInstruction, TryFromPrimitive)]
|
||||
#[rustfmt::skip]
|
||||
@@ -80,11 +89,6 @@ pub enum OreInstruction {
|
||||
#[account(1, name = "signer", desc = "Admin signer", signer)]
|
||||
#[account(2, name = "treasury", desc = "Ore treasury account")]
|
||||
UpdateAdmin = 101,
|
||||
|
||||
#[account(0, name = "ore_program", desc = "Ore program")]
|
||||
#[account(1, name = "signer", desc = "Admin signer", signer)]
|
||||
#[account(2, name = "treasury", desc = "Ore treasury account")]
|
||||
UpdateDifficulty = 102,
|
||||
}
|
||||
|
||||
impl OreInstruction {
|
||||
@@ -104,6 +108,7 @@ pub struct InitializeArgs {
|
||||
pub bus_5_bump: u8,
|
||||
pub bus_6_bump: u8,
|
||||
pub bus_7_bump: u8,
|
||||
pub config_bump: u8,
|
||||
pub metadata_bump: u8,
|
||||
pub mint_bump: u8,
|
||||
pub treasury_bump: u8,
|
||||
@@ -144,14 +149,12 @@ impl_to_bytes!(RegisterArgs);
|
||||
impl_to_bytes!(MineArgs);
|
||||
impl_to_bytes!(ClaimArgs);
|
||||
impl_to_bytes!(UpdateAdminArgs);
|
||||
impl_to_bytes!(UpdateDifficultyArgs);
|
||||
|
||||
impl_instruction_from_bytes!(InitializeArgs);
|
||||
impl_instruction_from_bytes!(RegisterArgs);
|
||||
impl_instruction_from_bytes!(MineArgs);
|
||||
impl_instruction_from_bytes!(ClaimArgs);
|
||||
impl_instruction_from_bytes!(UpdateAdminArgs);
|
||||
impl_instruction_from_bytes!(UpdateDifficultyArgs);
|
||||
|
||||
/// Builds a reset instruction.
|
||||
pub fn reset(signer: Pubkey) -> Instruction {
|
||||
@@ -215,8 +218,8 @@ pub fn mine(signer: Pubkey, bus: Pubkey, nonce: u64) -> Instruction {
|
||||
accounts: vec![
|
||||
AccountMeta::new(signer, true),
|
||||
AccountMeta::new(bus, false),
|
||||
AccountMeta::new_readonly(CONFIG_ADDRESS, false),
|
||||
AccountMeta::new(proof, false),
|
||||
AccountMeta::new_readonly(TREASURY_ADDRESS, false),
|
||||
AccountMeta::new_readonly(sysvar::slot_hashes::id(), false),
|
||||
],
|
||||
data: [
|
||||
@@ -272,6 +275,7 @@ pub fn initialize(signer: Pubkey) -> Instruction {
|
||||
Pubkey::find_program_address(&[BUS, &[6]], &crate::id()),
|
||||
Pubkey::find_program_address(&[BUS, &[7]], &crate::id()),
|
||||
];
|
||||
let config_pda = Pubkey::find_program_address(&[CONFIG], &crate::id());
|
||||
let mint_pda = Pubkey::find_program_address(&[MINT, MINT_NOISE.as_slice()], &crate::id());
|
||||
let treasury_pda = Pubkey::find_program_address(&[TREASURY], &crate::id());
|
||||
let treasury_tokens =
|
||||
@@ -296,6 +300,7 @@ pub fn initialize(signer: Pubkey) -> Instruction {
|
||||
AccountMeta::new(bus_pdas[5].0, false),
|
||||
AccountMeta::new(bus_pdas[6].0, false),
|
||||
AccountMeta::new(bus_pdas[7].0, false),
|
||||
AccountMeta::new(config_pda.0, false),
|
||||
AccountMeta::new(metadata_pda.0, false),
|
||||
AccountMeta::new(mint_pda.0, false),
|
||||
AccountMeta::new(treasury_pda.0, false),
|
||||
@@ -317,6 +322,7 @@ pub fn initialize(signer: Pubkey) -> Instruction {
|
||||
bus_5_bump: bus_pdas[5].1,
|
||||
bus_6_bump: bus_pdas[6].1,
|
||||
bus_7_bump: bus_pdas[7].1,
|
||||
config_bump: config_pda.1,
|
||||
metadata_bump: metadata_pda.1,
|
||||
mint_bump: mint_pda.1,
|
||||
treasury_bump: treasury_pda.1,
|
||||
@@ -343,19 +349,3 @@ pub fn update_admin(signer: Pubkey, new_admin: Pubkey) -> Instruction {
|
||||
.concat(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Builds an update_difficulty instruction.
|
||||
pub fn update_difficulty(signer: Pubkey, new_difficulty: Hash) -> Instruction {
|
||||
Instruction {
|
||||
program_id: crate::id(),
|
||||
accounts: vec![
|
||||
AccountMeta::new(signer, true),
|
||||
AccountMeta::new(TREASURY_ADDRESS, false),
|
||||
],
|
||||
data: [
|
||||
OreInstruction::UpdateDifficulty.to_vec(),
|
||||
UpdateDifficultyArgs { new_difficulty }.to_bytes().to_vec(),
|
||||
]
|
||||
.concat(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,7 +39,6 @@ pub fn process_instruction(
|
||||
OreInstruction::Claim => process_claim(program_id, accounts, data)?,
|
||||
OreInstruction::Initialize => process_initialize(program_id, accounts, data)?,
|
||||
OreInstruction::UpdateAdmin => process_update_admin(program_id, accounts, data)?,
|
||||
OreInstruction::UpdateDifficulty => process_update_difficulty(program_id, accounts, data)?,
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
||||
1826
src/loaders.rs
1826
src/loaders.rs
File diff suppressed because it is too large
Load Diff
@@ -48,11 +48,11 @@ pub fn process_claim<'a, 'info>(
|
||||
)?;
|
||||
load_program(token_program, spl_token::id())?;
|
||||
|
||||
// Update claimable amount
|
||||
// Update miner balance
|
||||
let mut proof_data = proof_info.data.borrow_mut();
|
||||
let proof = Proof::try_from_bytes_mut(&mut proof_data)?;
|
||||
proof.claimable_rewards = proof
|
||||
.claimable_rewards
|
||||
proof.balance = proof
|
||||
.balance
|
||||
.checked_sub(amount)
|
||||
.ok_or(OreError::ClaimTooLarge)?;
|
||||
|
||||
|
||||
@@ -13,11 +13,11 @@ use spl_token::state::Mint;
|
||||
use crate::{
|
||||
instruction::*,
|
||||
loaders::*,
|
||||
state::{Bus, Treasury},
|
||||
state::{Bus, Config, Treasury},
|
||||
utils::create_pda,
|
||||
utils::AccountDeserialize,
|
||||
utils::Discriminator,
|
||||
BUS, BUS_COUNT, INITIAL_DIFFICULTY, INITIAL_REWARD_RATE, METADATA, METADATA_NAME,
|
||||
BUS, BUS_COUNT, CONFIG, INITIAL_DIFFICULTY, INITIAL_REWARD_RATE, METADATA, METADATA_NAME,
|
||||
METADATA_SYMBOL, METADATA_URI, MINT, MINT_ADDRESS, MINT_NOISE, TOKEN_DECIMALS, TREASURY,
|
||||
};
|
||||
|
||||
@@ -48,7 +48,7 @@ pub fn process_initialize<'a, 'info>(
|
||||
let args = InitializeArgs::try_from_bytes(data)?;
|
||||
|
||||
// Load accounts
|
||||
let [signer, bus_0_info, bus_1_info, bus_2_info, bus_3_info, bus_4_info, bus_5_info, bus_6_info, bus_7_info, metadata_info, mint_info, treasury_info, treasury_tokens_info, system_program, token_program, associated_token_program, metadata_program, rent_sysvar] =
|
||||
let [signer, bus_0_info, bus_1_info, bus_2_info, bus_3_info, bus_4_info, bus_5_info, bus_6_info, bus_7_info, config_info, metadata_info, mint_info, treasury_info, treasury_tokens_info, system_program, token_program, associated_token_program, metadata_program, rent_sysvar] =
|
||||
accounts
|
||||
else {
|
||||
return Err(ProgramError::NotEnoughAccountKeys);
|
||||
@@ -62,6 +62,7 @@ pub fn process_initialize<'a, 'info>(
|
||||
load_uninitialized_pda(bus_5_info, &[BUS, &[5]], args.bus_5_bump, &crate::id())?;
|
||||
load_uninitialized_pda(bus_6_info, &[BUS, &[6]], args.bus_6_bump, &crate::id())?;
|
||||
load_uninitialized_pda(bus_7_info, &[BUS, &[7]], args.bus_7_bump, &crate::id())?;
|
||||
load_uninitialized_pda(config_info, &[CONFIG], args.config_bump, &crate::id())?;
|
||||
load_uninitialized_pda(
|
||||
metadata_info,
|
||||
&[
|
||||
@@ -117,6 +118,21 @@ pub fn process_initialize<'a, 'info>(
|
||||
bus.rewards = 0;
|
||||
}
|
||||
|
||||
// Initialize config
|
||||
create_pda(
|
||||
config_info,
|
||||
&crate::id(),
|
||||
8 + size_of::<Config>(),
|
||||
&[CONFIG, &[args.config_bump]],
|
||||
system_program,
|
||||
signer,
|
||||
)?;
|
||||
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.difficulty = INITIAL_DIFFICULTY.into();
|
||||
|
||||
// Initialize treasury
|
||||
create_pda(
|
||||
treasury_info,
|
||||
@@ -129,9 +145,7 @@ pub fn process_initialize<'a, 'info>(
|
||||
let mut treasury_data = treasury_info.data.borrow_mut();
|
||||
treasury_data[0] = Treasury::discriminator() as u8;
|
||||
let treasury = Treasury::try_from_bytes_mut(&mut treasury_data)?;
|
||||
treasury.admin = *signer.key;
|
||||
treasury.bump = args.treasury_bump as u64;
|
||||
treasury.difficulty = INITIAL_DIFFICULTY.into();
|
||||
treasury.last_reset_at = 0;
|
||||
treasury.reward_rate = INITIAL_REWARD_RATE;
|
||||
treasury.total_claimed_rewards = 0;
|
||||
|
||||
@@ -69,12 +69,16 @@ pub fn process_mine<'a, 'info>(
|
||||
// Validate provided hash
|
||||
let mut proof_data = proof_info.data.borrow_mut();
|
||||
let proof = Proof::try_from_bytes_mut(&mut proof_data)?;
|
||||
let hash = validate_hash(
|
||||
proof.hash.into(),
|
||||
*signer.key,
|
||||
u64::from_le_bytes(args.nonce),
|
||||
treasury.difficulty.into(),
|
||||
)?;
|
||||
// let hash = validate_hash(
|
||||
// proof.hash.into(),
|
||||
// *signer.key,
|
||||
// u64::from_le_bytes(args.nonce),
|
||||
// proof.difficulty.into(),
|
||||
// )?;
|
||||
|
||||
// TODO Calculate reward based on difficulty
|
||||
// TODO Calculate rewards multiplier
|
||||
// TODO Calculate reward payout amount
|
||||
|
||||
// Update claimable rewards
|
||||
let mut bus_data = bus_info.data.borrow_mut();
|
||||
@@ -83,11 +87,12 @@ pub fn process_mine<'a, 'info>(
|
||||
.rewards
|
||||
.checked_sub(treasury.reward_rate)
|
||||
.ok_or(OreError::BusRewardsInsufficient)?;
|
||||
proof.claimable_rewards = proof.claimable_rewards.saturating_add(treasury.reward_rate);
|
||||
proof.balance = proof.balance.saturating_add(treasury.reward_rate);
|
||||
|
||||
// Hash recent slot hash into the next challenge to prevent pre-mining attacks
|
||||
proof.hash = hashv(&[
|
||||
hash.as_ref(),
|
||||
// TODO
|
||||
// hash.as_ref(),
|
||||
&slot_hashes_info.data.borrow()[0..size_of::<SlotHash>()],
|
||||
])
|
||||
.into();
|
||||
|
||||
@@ -4,7 +4,6 @@ mod mine;
|
||||
mod register;
|
||||
mod reset;
|
||||
mod update_admin;
|
||||
mod update_difficulty;
|
||||
|
||||
pub use claim::*;
|
||||
pub use initialize::*;
|
||||
@@ -12,4 +11,3 @@ pub use mine::*;
|
||||
pub use register::*;
|
||||
pub use reset::*;
|
||||
pub use update_admin::*;
|
||||
pub use update_difficulty::*;
|
||||
|
||||
@@ -14,6 +14,8 @@ use crate::{
|
||||
PROOF,
|
||||
};
|
||||
|
||||
// TODO Create a stake account
|
||||
|
||||
/// 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.
|
||||
@@ -58,12 +60,14 @@ pub fn process_register<'a, 'info>(
|
||||
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.balance = 0;
|
||||
proof.hash = hashv(&[
|
||||
signer.key.as_ref(),
|
||||
&slot_hashes_info.data.borrow()[0..size_of::<SlotHash>()],
|
||||
])
|
||||
.into();
|
||||
proof.last_hash_at = 0;
|
||||
proof.multiplier = 1; // TODO
|
||||
proof.total_hashes = 0;
|
||||
proof.total_rewards = 0;
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ use solana_program::{
|
||||
pubkey::Pubkey,
|
||||
};
|
||||
|
||||
use crate::{instruction::UpdateAdminArgs, loaders::*, state::Treasury, utils::AccountDeserialize};
|
||||
use crate::{instruction::UpdateAdminArgs, loaders::*, state::Config, utils::AccountDeserialize};
|
||||
|
||||
/// UpdateAdmin updates the program's admin account. Its responsibilities include:
|
||||
/// 1. Update the treasury admin address.
|
||||
@@ -34,21 +34,21 @@ pub fn process_update_admin<'a, 'info>(
|
||||
let args = UpdateAdminArgs::try_from_bytes(data)?;
|
||||
|
||||
// Load accounts
|
||||
let [signer, treasury_info] = accounts else {
|
||||
let [signer, config_info] = accounts else {
|
||||
return Err(ProgramError::NotEnoughAccountKeys);
|
||||
};
|
||||
load_signer(signer)?;
|
||||
load_treasury(treasury_info, true)?;
|
||||
load_config(config_info, true)?;
|
||||
|
||||
// Validate signer is admin
|
||||
let mut treasury_data = treasury_info.data.borrow_mut();
|
||||
let treasury = Treasury::try_from_bytes_mut(&mut treasury_data)?;
|
||||
if treasury.admin.ne(&signer.key) {
|
||||
let mut config_data = config_info.data.borrow_mut();
|
||||
let config = Config::try_from_bytes_mut(&mut config_data)?;
|
||||
if config.admin.ne(&signer.key) {
|
||||
return Err(ProgramError::MissingRequiredSignature);
|
||||
}
|
||||
|
||||
// Update admin
|
||||
treasury.admin = args.new_admin;
|
||||
config.admin = args.new_admin;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -1,55 +0,0 @@
|
||||
use solana_program::{
|
||||
account_info::AccountInfo, entrypoint::ProgramResult, program_error::ProgramError,
|
||||
pubkey::Pubkey,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
instruction::UpdateDifficultyArgs, loaders::*, state::Treasury, utils::AccountDeserialize,
|
||||
};
|
||||
|
||||
/// UpdateDifficulty updates the program's global difficulty value. Its responsibilities include:
|
||||
/// 1. Update the mining difficulty.
|
||||
///
|
||||
/// Safety requirements:
|
||||
/// - Can only succeed if the signer is the program admin.
|
||||
/// - Can only succeed if the provided treasury is valid.
|
||||
///
|
||||
/// Discussion:
|
||||
/// - Ore subdivides into 1 billion indivisible atomic units. Therefore if global hashpower
|
||||
/// were to increase to the point where >1B valid hashes were submitted to the protocol for
|
||||
/// validation per epoch, the Ore inflation rate could be pushed above the 1 ORE / min target.
|
||||
/// - The strict limits on bus reward counters guarantee inflation can never exceed 2 ORE / min,
|
||||
/// but it is the responsibility of the admin to adjust mining difficulty if needed to maintain
|
||||
/// the 1 ORE / min target average.
|
||||
/// - It is worth noting that Solana today processes well below 1 million real TPS or
|
||||
/// (60 * 1,000,000) = 60,000,000 transactions per minute. Even if every transaction on Solana
|
||||
/// were a mine operation, this would still be two orders of magnitude below the boundary
|
||||
/// condition where Ore inflation targets would be challenged. So in practice, Solana is likely
|
||||
/// to reach its network saturation point long before Ore ever hits its theoretical limits.
|
||||
pub fn process_update_difficulty<'a, 'info>(
|
||||
_program_id: &Pubkey,
|
||||
accounts: &'a [AccountInfo<'info>],
|
||||
data: &[u8],
|
||||
) -> ProgramResult {
|
||||
// Parse args
|
||||
let args = UpdateDifficultyArgs::try_from_bytes(data)?;
|
||||
|
||||
// Load accounts
|
||||
let [signer, treasury_info] = accounts else {
|
||||
return Err(ProgramError::NotEnoughAccountKeys);
|
||||
};
|
||||
load_signer(signer)?;
|
||||
load_treasury(treasury_info, true)?;
|
||||
|
||||
// Validate signer is admin
|
||||
let mut treasury_data = treasury_info.data.borrow_mut();
|
||||
let treasury = Treasury::try_from_bytes_mut(&mut treasury_data)?;
|
||||
if treasury.admin.ne(&signer.key) {
|
||||
return Err(ProgramError::MissingRequiredSignature);
|
||||
}
|
||||
|
||||
// Update admin
|
||||
treasury.difficulty = args.new_difficulty;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -17,6 +17,12 @@ pub struct Bus {
|
||||
|
||||
/// The quantity of rewards this bus can issue in the current epoch epoch.
|
||||
pub rewards: u64,
|
||||
|
||||
/// Histogram of hash count per difficulty
|
||||
pub hash_hist: [u64; 32],
|
||||
|
||||
/// Cumulative sum of applied multipliers per difficulty
|
||||
pub multiplier_hist: [u64; 32],
|
||||
}
|
||||
|
||||
impl Discriminator for Bus {
|
||||
|
||||
29
src/state/config.rs
Normal file
29
src/state/config.rs
Normal file
@@ -0,0 +1,29 @@
|
||||
use bytemuck::{Pod, Zeroable};
|
||||
use shank::ShankAccount;
|
||||
use solana_program::pubkey::Pubkey;
|
||||
|
||||
use crate::{
|
||||
impl_account_from_bytes, impl_to_bytes,
|
||||
state::Hash,
|
||||
utils::{AccountDiscriminator, Discriminator},
|
||||
};
|
||||
|
||||
/// Config is a singleton account which manages admin configurable variables.
|
||||
#[repr(C)]
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Pod, ShankAccount, Zeroable)]
|
||||
pub struct Config {
|
||||
/// The admin authority with permission to update the difficulty.
|
||||
pub admin: Pubkey,
|
||||
|
||||
/// The hash difficulty.
|
||||
pub difficulty: Hash,
|
||||
}
|
||||
|
||||
impl Discriminator for Config {
|
||||
fn discriminator() -> AccountDiscriminator {
|
||||
AccountDiscriminator::Config
|
||||
}
|
||||
}
|
||||
|
||||
impl_to_bytes!(Config);
|
||||
impl_account_from_bytes!(Config);
|
||||
@@ -1,9 +1,11 @@
|
||||
mod bus;
|
||||
mod config;
|
||||
mod hash;
|
||||
mod proof;
|
||||
mod treasury;
|
||||
|
||||
pub use bus::*;
|
||||
pub use config::*;
|
||||
pub use hash::*;
|
||||
pub use proof::*;
|
||||
pub use treasury::*;
|
||||
|
||||
@@ -13,15 +13,22 @@ use crate::{
|
||||
#[repr(C)]
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Pod, ShankAccount, Zeroable)]
|
||||
pub struct Proof {
|
||||
/// The account (i.e. miner) authorized to use this proof.
|
||||
/// The signer authorized to use this proof.
|
||||
pub authority: Pubkey,
|
||||
|
||||
/// The quantity of tokens this miner may claim from the treasury.
|
||||
pub claimable_rewards: u64,
|
||||
pub balance: u64,
|
||||
|
||||
/// The proof's current hash.
|
||||
pub hash: Hash,
|
||||
|
||||
/// The last time this account provided a hash.
|
||||
pub last_hash_at: u64,
|
||||
|
||||
// TODO Figure out multiplier representation
|
||||
/// The rewards multiplier for this account.
|
||||
pub multiplier: u64,
|
||||
|
||||
/// The total lifetime hashes provided by this miner.
|
||||
pub total_hashes: u64,
|
||||
|
||||
|
||||
@@ -1,10 +1,8 @@
|
||||
use bytemuck::{Pod, Zeroable};
|
||||
use shank::ShankAccount;
|
||||
use solana_program::pubkey::Pubkey;
|
||||
|
||||
use crate::{
|
||||
impl_account_from_bytes, impl_to_bytes,
|
||||
state::Hash,
|
||||
utils::{AccountDiscriminator, Discriminator},
|
||||
};
|
||||
|
||||
@@ -13,22 +11,16 @@ use crate::{
|
||||
#[repr(C)]
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Pod, ShankAccount, Zeroable)]
|
||||
pub struct Treasury {
|
||||
/// The admin authority with permission to update the difficulty.
|
||||
pub admin: Pubkey,
|
||||
|
||||
/// The bump of the treasury account PDA, for signing CPIs.
|
||||
pub bump: u64,
|
||||
|
||||
/// The hash difficulty.
|
||||
pub difficulty: Hash,
|
||||
|
||||
/// The timestamp of the reset invocation.
|
||||
pub last_reset_at: i64,
|
||||
|
||||
/// The reward rate to payout to miners for submiting valid hashes.
|
||||
pub reward_rate: u64,
|
||||
|
||||
/// The total lifetime claimed rewards.
|
||||
/// The total lifetime claimed rewards of the program.
|
||||
pub total_claimed_rewards: u64,
|
||||
}
|
||||
|
||||
|
||||
@@ -82,8 +82,9 @@ pub(crate) fn create_pda<'a, 'info>(
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq, IntoPrimitive, TryFromPrimitive)]
|
||||
pub enum AccountDiscriminator {
|
||||
Bus = 100,
|
||||
Proof = 101,
|
||||
Treasury = 102,
|
||||
Config = 101,
|
||||
Proof = 102,
|
||||
Treasury = 103,
|
||||
}
|
||||
|
||||
pub trait Discriminator {
|
||||
|
||||
@@ -1,170 +1,170 @@
|
||||
use mpl_token_metadata::{
|
||||
accounts::Metadata,
|
||||
types::{Key, TokenStandard},
|
||||
};
|
||||
use ore::{
|
||||
state::{Bus, Treasury},
|
||||
utils::AccountDeserialize,
|
||||
BUS_ADDRESSES, BUS_COUNT, INITIAL_DIFFICULTY, INITIAL_REWARD_RATE, METADATA_ADDRESS,
|
||||
METADATA_NAME, METADATA_SYMBOL, METADATA_URI, MINT_ADDRESS, TREASURY,
|
||||
};
|
||||
use solana_program::{
|
||||
hash::Hash, program_option::COption, program_pack::Pack, pubkey::Pubkey, rent::Rent,
|
||||
};
|
||||
use solana_program_test::{processor, read_file, BanksClient, ProgramTest};
|
||||
use solana_sdk::{
|
||||
account::Account,
|
||||
signature::{Keypair, Signer},
|
||||
transaction::Transaction,
|
||||
};
|
||||
use spl_token::state::{AccountState, Mint};
|
||||
// use mpl_token_metadata::{
|
||||
// accounts::Metadata,
|
||||
// types::{Key, TokenStandard},
|
||||
// };
|
||||
// use ore::{
|
||||
// state::{Bus, Treasury},
|
||||
// utils::AccountDeserialize,
|
||||
// BUS_ADDRESSES, BUS_COUNT, INITIAL_DIFFICULTY, INITIAL_REWARD_RATE, METADATA_ADDRESS,
|
||||
// METADATA_NAME, METADATA_SYMBOL, METADATA_URI, MINT_ADDRESS, TREASURY,
|
||||
// };
|
||||
// use solana_program::{
|
||||
// hash::Hash, program_option::COption, program_pack::Pack, pubkey::Pubkey, rent::Rent,
|
||||
// };
|
||||
// use solana_program_test::{processor, read_file, BanksClient, ProgramTest};
|
||||
// use solana_sdk::{
|
||||
// account::Account,
|
||||
// signature::{Keypair, Signer},
|
||||
// transaction::Transaction,
|
||||
// };
|
||||
// use spl_token::state::{AccountState, Mint};
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_initialize() {
|
||||
// Setup
|
||||
let (mut banks, payer, blockhash) = setup_program_test_env().await;
|
||||
// #[tokio::test]
|
||||
// async fn test_initialize() {
|
||||
// // Setup
|
||||
// let (mut banks, payer, blockhash) = setup_program_test_env().await;
|
||||
|
||||
// Pdas
|
||||
let treasury_pda = Pubkey::find_program_address(&[TREASURY], &ore::id());
|
||||
let treasury_tokens_address =
|
||||
spl_associated_token_account::get_associated_token_address(&treasury_pda.0, &MINT_ADDRESS);
|
||||
// // Pdas
|
||||
// let treasury_pda = Pubkey::find_program_address(&[TREASURY], &ore::id());
|
||||
// let treasury_tokens_address =
|
||||
// spl_associated_token_account::get_associated_token_address(&treasury_pda.0, &MINT_ADDRESS);
|
||||
|
||||
// Submit tx
|
||||
let ix = ore::instruction::initialize(payer.pubkey());
|
||||
let tx = Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], blockhash);
|
||||
let res = banks.process_transaction(tx).await;
|
||||
assert!(res.is_ok());
|
||||
// // Submit tx
|
||||
// let ix = ore::instruction::initialize(payer.pubkey());
|
||||
// let tx = Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], blockhash);
|
||||
// let res = banks.process_transaction(tx).await;
|
||||
// assert!(res.is_ok());
|
||||
|
||||
// Test bus state
|
||||
for i in 0..BUS_COUNT {
|
||||
let bus_account = banks.get_account(BUS_ADDRESSES[i]).await.unwrap().unwrap();
|
||||
assert_eq!(bus_account.owner, ore::id());
|
||||
let bus = Bus::try_from_bytes(&bus_account.data).unwrap();
|
||||
assert_eq!(bus.id as u8, i as u8);
|
||||
assert_eq!(bus.rewards, 0);
|
||||
}
|
||||
// // Test bus state
|
||||
// for i in 0..BUS_COUNT {
|
||||
// let bus_account = banks.get_account(BUS_ADDRESSES[i]).await.unwrap().unwrap();
|
||||
// assert_eq!(bus_account.owner, ore::id());
|
||||
// let bus = Bus::try_from_bytes(&bus_account.data).unwrap();
|
||||
// assert_eq!(bus.id as u8, i as u8);
|
||||
// assert_eq!(bus.rewards, 0);
|
||||
// }
|
||||
|
||||
// Test treasury state
|
||||
let treasury_account = banks.get_account(treasury_pda.0).await.unwrap().unwrap();
|
||||
assert_eq!(treasury_account.owner, ore::id());
|
||||
let treasury = Treasury::try_from_bytes(&treasury_account.data).unwrap();
|
||||
assert_eq!(treasury.bump as u8, treasury_pda.1);
|
||||
assert_eq!(treasury.admin, payer.pubkey());
|
||||
assert_eq!(treasury.difficulty, INITIAL_DIFFICULTY.into());
|
||||
assert_eq!(treasury.last_reset_at as u8, 0);
|
||||
assert_eq!(treasury.reward_rate, INITIAL_REWARD_RATE);
|
||||
assert_eq!(treasury.total_claimed_rewards as u8, 0);
|
||||
// // Test treasury state
|
||||
// let treasury_account = banks.get_account(treasury_pda.0).await.unwrap().unwrap();
|
||||
// assert_eq!(treasury_account.owner, ore::id());
|
||||
// let treasury = Treasury::try_from_bytes(&treasury_account.data).unwrap();
|
||||
// assert_eq!(treasury.bump as u8, treasury_pda.1);
|
||||
// // assert_eq!(treasury.admin, payer.pubkey());
|
||||
// // assert_eq!(treasury.difficulty, INITIAL_DIFFICULTY.into());
|
||||
// assert_eq!(treasury.last_reset_at as u8, 0);
|
||||
// assert_eq!(treasury.reward_rate, INITIAL_REWARD_RATE);
|
||||
// assert_eq!(treasury.total_claimed_rewards as u8, 0);
|
||||
|
||||
// Test mint state
|
||||
let mint_account = banks.get_account(MINT_ADDRESS).await.unwrap().unwrap();
|
||||
assert_eq!(mint_account.owner, spl_token::id());
|
||||
let mint = Mint::unpack(&mint_account.data).unwrap();
|
||||
assert_eq!(mint.mint_authority, COption::Some(treasury_pda.0));
|
||||
assert_eq!(mint.supply, 0);
|
||||
assert_eq!(mint.decimals, ore::TOKEN_DECIMALS);
|
||||
assert_eq!(mint.is_initialized, true);
|
||||
assert_eq!(mint.freeze_authority, COption::None);
|
||||
// // Test mint state
|
||||
// let mint_account = banks.get_account(MINT_ADDRESS).await.unwrap().unwrap();
|
||||
// assert_eq!(mint_account.owner, spl_token::id());
|
||||
// let mint = Mint::unpack(&mint_account.data).unwrap();
|
||||
// assert_eq!(mint.mint_authority, COption::Some(treasury_pda.0));
|
||||
// assert_eq!(mint.supply, 0);
|
||||
// assert_eq!(mint.decimals, ore::TOKEN_DECIMALS);
|
||||
// assert_eq!(mint.is_initialized, true);
|
||||
// assert_eq!(mint.freeze_authority, COption::None);
|
||||
|
||||
// Test metadata state
|
||||
let metadata_account = banks.get_account(METADATA_ADDRESS).await.unwrap().unwrap();
|
||||
assert_eq!(metadata_account.owner, mpl_token_metadata::ID);
|
||||
let metadata = Metadata::from_bytes(&metadata_account.data).unwrap();
|
||||
assert_eq!(metadata.key, Key::MetadataV1);
|
||||
assert_eq!(metadata.update_authority, payer.pubkey());
|
||||
assert_eq!(metadata.mint, MINT_ADDRESS);
|
||||
assert_eq!(metadata.name.trim_end_matches('\0'), METADATA_NAME);
|
||||
assert_eq!(metadata.symbol.trim_end_matches('\0'), METADATA_SYMBOL);
|
||||
assert_eq!(metadata.uri.trim_end_matches('\0'), METADATA_URI);
|
||||
assert_eq!(metadata.seller_fee_basis_points, 0);
|
||||
assert_eq!(metadata.creators, None);
|
||||
assert_eq!(metadata.primary_sale_happened, false);
|
||||
assert_eq!(metadata.is_mutable, true);
|
||||
assert_eq!(metadata.token_standard, Some(TokenStandard::Fungible));
|
||||
assert_eq!(metadata.collection, None);
|
||||
assert_eq!(metadata.uses, None);
|
||||
assert_eq!(metadata.collection_details, None);
|
||||
assert_eq!(metadata.programmable_config, None);
|
||||
// // Test metadata state
|
||||
// let metadata_account = banks.get_account(METADATA_ADDRESS).await.unwrap().unwrap();
|
||||
// assert_eq!(metadata_account.owner, mpl_token_metadata::ID);
|
||||
// let metadata = Metadata::from_bytes(&metadata_account.data).unwrap();
|
||||
// assert_eq!(metadata.key, Key::MetadataV1);
|
||||
// assert_eq!(metadata.update_authority, payer.pubkey());
|
||||
// assert_eq!(metadata.mint, MINT_ADDRESS);
|
||||
// assert_eq!(metadata.name.trim_end_matches('\0'), METADATA_NAME);
|
||||
// assert_eq!(metadata.symbol.trim_end_matches('\0'), METADATA_SYMBOL);
|
||||
// assert_eq!(metadata.uri.trim_end_matches('\0'), METADATA_URI);
|
||||
// assert_eq!(metadata.seller_fee_basis_points, 0);
|
||||
// assert_eq!(metadata.creators, None);
|
||||
// assert_eq!(metadata.primary_sale_happened, false);
|
||||
// assert_eq!(metadata.is_mutable, true);
|
||||
// assert_eq!(metadata.token_standard, Some(TokenStandard::Fungible));
|
||||
// assert_eq!(metadata.collection, None);
|
||||
// assert_eq!(metadata.uses, None);
|
||||
// assert_eq!(metadata.collection_details, None);
|
||||
// assert_eq!(metadata.programmable_config, None);
|
||||
|
||||
// Test treasury token state
|
||||
let treasury_tokens_account = banks
|
||||
.get_account(treasury_tokens_address)
|
||||
.await
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
assert_eq!(treasury_tokens_account.owner, spl_token::id());
|
||||
let treasury_tokens = spl_token::state::Account::unpack(&treasury_tokens_account.data).unwrap();
|
||||
assert_eq!(treasury_tokens.mint, MINT_ADDRESS);
|
||||
assert_eq!(treasury_tokens.owner, treasury_pda.0);
|
||||
assert_eq!(treasury_tokens.amount, 0);
|
||||
assert_eq!(treasury_tokens.delegate, COption::None);
|
||||
assert_eq!(treasury_tokens.state, AccountState::Initialized);
|
||||
assert_eq!(treasury_tokens.is_native, COption::None);
|
||||
assert_eq!(treasury_tokens.delegated_amount, 0);
|
||||
assert_eq!(treasury_tokens.close_authority, COption::None);
|
||||
}
|
||||
// // Test treasury token state
|
||||
// let treasury_tokens_account = banks
|
||||
// .get_account(treasury_tokens_address)
|
||||
// .await
|
||||
// .unwrap()
|
||||
// .unwrap();
|
||||
// assert_eq!(treasury_tokens_account.owner, spl_token::id());
|
||||
// let treasury_tokens = spl_token::state::Account::unpack(&treasury_tokens_account.data).unwrap();
|
||||
// assert_eq!(treasury_tokens.mint, MINT_ADDRESS);
|
||||
// assert_eq!(treasury_tokens.owner, treasury_pda.0);
|
||||
// assert_eq!(treasury_tokens.amount, 0);
|
||||
// assert_eq!(treasury_tokens.delegate, COption::None);
|
||||
// assert_eq!(treasury_tokens.state, AccountState::Initialized);
|
||||
// assert_eq!(treasury_tokens.is_native, COption::None);
|
||||
// assert_eq!(treasury_tokens.delegated_amount, 0);
|
||||
// assert_eq!(treasury_tokens.close_authority, COption::None);
|
||||
// }
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_initialize_not_enough_accounts() {
|
||||
// Setup
|
||||
let (mut banks, payer, blockhash) = setup_program_test_env().await;
|
||||
// #[tokio::test]
|
||||
// async fn test_initialize_not_enough_accounts() {
|
||||
// // Setup
|
||||
// let (mut banks, payer, blockhash) = setup_program_test_env().await;
|
||||
|
||||
// Submit tx
|
||||
let mut ix = ore::instruction::initialize(payer.pubkey());
|
||||
ix.accounts.remove(1);
|
||||
let tx = Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], blockhash);
|
||||
let res = banks.process_transaction(tx).await;
|
||||
assert!(res.is_err());
|
||||
}
|
||||
// // Submit tx
|
||||
// let mut ix = ore::instruction::initialize(payer.pubkey());
|
||||
// ix.accounts.remove(1);
|
||||
// let tx = Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], blockhash);
|
||||
// let res = banks.process_transaction(tx).await;
|
||||
// assert!(res.is_err());
|
||||
// }
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_initialize_bad_key() {
|
||||
// Setup
|
||||
let (mut banks, payer, blockhash) = setup_program_test_env().await;
|
||||
// #[tokio::test]
|
||||
// async fn test_initialize_bad_key() {
|
||||
// // Setup
|
||||
// let (mut banks, payer, blockhash) = setup_program_test_env().await;
|
||||
|
||||
// Bad addresses
|
||||
let bad_pda = Pubkey::find_program_address(&[b"t"], &ore::id());
|
||||
for i in 1..12 {
|
||||
let mut ix = ore::instruction::initialize(payer.pubkey());
|
||||
ix.accounts[i].pubkey = bad_pda.0;
|
||||
let tx =
|
||||
Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], blockhash);
|
||||
let res = banks.process_transaction(tx).await;
|
||||
assert!(res.is_err());
|
||||
}
|
||||
}
|
||||
// // Bad addresses
|
||||
// let bad_pda = Pubkey::find_program_address(&[b"t"], &ore::id());
|
||||
// for i in 1..12 {
|
||||
// let mut ix = ore::instruction::initialize(payer.pubkey());
|
||||
// ix.accounts[i].pubkey = bad_pda.0;
|
||||
// let tx =
|
||||
// Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], blockhash);
|
||||
// let res = banks.process_transaction(tx).await;
|
||||
// assert!(res.is_err());
|
||||
// }
|
||||
// }
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_initialize_bad_programs() {
|
||||
// Setup
|
||||
let (mut banks, payer, blockhash) = setup_program_test_env().await;
|
||||
// #[tokio::test]
|
||||
// async fn test_initialize_bad_programs() {
|
||||
// // Setup
|
||||
// let (mut banks, payer, blockhash) = setup_program_test_env().await;
|
||||
|
||||
// Bad addresses
|
||||
for i in 13..18 {
|
||||
let mut ix = ore::instruction::initialize(payer.pubkey());
|
||||
ix.accounts[i].pubkey = Pubkey::new_unique();
|
||||
let tx =
|
||||
Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], blockhash);
|
||||
let res = banks.process_transaction(tx).await;
|
||||
assert!(res.is_err());
|
||||
}
|
||||
}
|
||||
// // Bad addresses
|
||||
// for i in 13..18 {
|
||||
// let mut ix = ore::instruction::initialize(payer.pubkey());
|
||||
// ix.accounts[i].pubkey = Pubkey::new_unique();
|
||||
// let tx =
|
||||
// Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], blockhash);
|
||||
// let res = banks.process_transaction(tx).await;
|
||||
// assert!(res.is_err());
|
||||
// }
|
||||
// }
|
||||
|
||||
async fn setup_program_test_env() -> (BanksClient, Keypair, Hash) {
|
||||
let mut program_test = ProgramTest::new("ore", ore::ID, processor!(ore::process_instruction));
|
||||
program_test.prefer_bpf(true);
|
||||
// async fn setup_program_test_env() -> (BanksClient, Keypair, Hash) {
|
||||
// let mut program_test = ProgramTest::new("ore", ore::ID, processor!(ore::process_instruction));
|
||||
// program_test.prefer_bpf(true);
|
||||
|
||||
// Setup metadata program
|
||||
let data = read_file(&"tests/buffers/metadata_program.bpf");
|
||||
program_test.add_account(
|
||||
mpl_token_metadata::ID,
|
||||
Account {
|
||||
lamports: Rent::default().minimum_balance(data.len()).max(1),
|
||||
data,
|
||||
owner: solana_sdk::bpf_loader::id(),
|
||||
executable: true,
|
||||
rent_epoch: 0,
|
||||
},
|
||||
);
|
||||
// // Setup metadata program
|
||||
// let data = read_file(&"tests/buffers/metadata_program.bpf");
|
||||
// program_test.add_account(
|
||||
// mpl_token_metadata::ID,
|
||||
// Account {
|
||||
// lamports: Rent::default().minimum_balance(data.len()).max(1),
|
||||
// data,
|
||||
// owner: solana_sdk::bpf_loader::id(),
|
||||
// executable: true,
|
||||
// rent_epoch: 0,
|
||||
// },
|
||||
// );
|
||||
|
||||
program_test.start().await
|
||||
}
|
||||
// program_test.start().await
|
||||
// }
|
||||
|
||||
1368
tests/test_mine.rs
1368
tests/test_mine.rs
File diff suppressed because it is too large
Load Diff
@@ -1,188 +1,188 @@
|
||||
use std::str::FromStr;
|
||||
// use std::str::FromStr;
|
||||
|
||||
use ore::{
|
||||
instruction::{register, OreInstruction, RegisterArgs},
|
||||
state::{Bus, Treasury},
|
||||
utils::Discriminator,
|
||||
BUS_ADDRESSES, BUS_COUNT, INITIAL_REWARD_RATE, MINT_ADDRESS, PROOF, TOKEN_DECIMALS, TREASURY,
|
||||
TREASURY_ADDRESS,
|
||||
};
|
||||
use solana_program::{
|
||||
clock::Clock,
|
||||
epoch_schedule::DEFAULT_SLOTS_PER_EPOCH,
|
||||
instruction::{AccountMeta, Instruction},
|
||||
keccak::Hash as KeccakHash,
|
||||
program_option::COption,
|
||||
program_pack::Pack,
|
||||
pubkey::Pubkey,
|
||||
rent::Rent,
|
||||
sysvar,
|
||||
};
|
||||
use solana_program_test::{processor, BanksClient, ProgramTest};
|
||||
use solana_sdk::{
|
||||
signature::{Keypair, Signer},
|
||||
system_transaction::transfer,
|
||||
transaction::Transaction,
|
||||
};
|
||||
use spl_token::state::{AccountState, Mint};
|
||||
// use ore::{
|
||||
// instruction::{register, OreInstruction, RegisterArgs},
|
||||
// state::{Bus, Treasury},
|
||||
// utils::Discriminator,
|
||||
// BUS_ADDRESSES, BUS_COUNT, INITIAL_REWARD_RATE, MINT_ADDRESS, PROOF, TOKEN_DECIMALS, TREASURY,
|
||||
// TREASURY_ADDRESS,
|
||||
// };
|
||||
// use solana_program::{
|
||||
// clock::Clock,
|
||||
// epoch_schedule::DEFAULT_SLOTS_PER_EPOCH,
|
||||
// instruction::{AccountMeta, Instruction},
|
||||
// keccak::Hash as KeccakHash,
|
||||
// program_option::COption,
|
||||
// program_pack::Pack,
|
||||
// pubkey::Pubkey,
|
||||
// rent::Rent,
|
||||
// sysvar,
|
||||
// };
|
||||
// use solana_program_test::{processor, BanksClient, ProgramTest};
|
||||
// use solana_sdk::{
|
||||
// signature::{Keypair, Signer},
|
||||
// system_transaction::transfer,
|
||||
// transaction::Transaction,
|
||||
// };
|
||||
// use spl_token::state::{AccountState, Mint};
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_register_account_with_lamports() {
|
||||
let (mut banks, payer, blockhash) = setup_program_test_env().await;
|
||||
// #[tokio::test]
|
||||
// async fn test_register_account_with_lamports() {
|
||||
// let (mut banks, payer, blockhash) = setup_program_test_env().await;
|
||||
|
||||
// Send lamports to the proof pda
|
||||
let proof_pda = Pubkey::find_program_address(&[PROOF, payer.pubkey().as_ref()], &ore::id());
|
||||
let lamports = Rent::default().minimum_balance(0);
|
||||
let tx = transfer(&payer, &proof_pda.0, lamports, blockhash);
|
||||
let res = banks.process_transaction(tx).await;
|
||||
assert!(res.is_ok());
|
||||
// // Send lamports to the proof pda
|
||||
// let proof_pda = Pubkey::find_program_address(&[PROOF, payer.pubkey().as_ref()], &ore::id());
|
||||
// let lamports = Rent::default().minimum_balance(0);
|
||||
// let tx = transfer(&payer, &proof_pda.0, lamports, blockhash);
|
||||
// let res = banks.process_transaction(tx).await;
|
||||
// assert!(res.is_ok());
|
||||
|
||||
// Assert register succeeds
|
||||
let ix = register(payer.pubkey());
|
||||
let tx = Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], blockhash);
|
||||
let res = banks.process_transaction(tx).await;
|
||||
assert!(res.is_ok());
|
||||
}
|
||||
// // Assert register succeeds
|
||||
// let ix = register(payer.pubkey());
|
||||
// let tx = Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], blockhash);
|
||||
// let res = banks.process_transaction(tx).await;
|
||||
// assert!(res.is_ok());
|
||||
// }
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_register_not_enough_accounts() {
|
||||
let (mut banks, payer, blockhash) = setup_program_test_env().await;
|
||||
// #[tokio::test]
|
||||
// async fn test_register_not_enough_accounts() {
|
||||
// let (mut banks, payer, blockhash) = setup_program_test_env().await;
|
||||
|
||||
// Assert register fails
|
||||
let mut ix = register(payer.pubkey());
|
||||
ix.accounts.remove(1);
|
||||
let tx = Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], blockhash);
|
||||
let res = banks.process_transaction(tx).await;
|
||||
assert!(res.is_err());
|
||||
}
|
||||
// // Assert register fails
|
||||
// let mut ix = register(payer.pubkey());
|
||||
// ix.accounts.remove(1);
|
||||
// let tx = Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], blockhash);
|
||||
// let res = banks.process_transaction(tx).await;
|
||||
// assert!(res.is_err());
|
||||
// }
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_register_fail_other() {
|
||||
let (mut banks, payer, blockhash) = setup_program_test_env().await;
|
||||
// #[tokio::test]
|
||||
// async fn test_register_fail_other() {
|
||||
// let (mut banks, payer, blockhash) = setup_program_test_env().await;
|
||||
|
||||
// Try register for another keypair
|
||||
let other = Keypair::new();
|
||||
let proof_pda = Pubkey::find_program_address(&[PROOF, other.pubkey().as_ref()], &ore::id());
|
||||
let ix = Instruction {
|
||||
program_id: ore::id(),
|
||||
accounts: vec![
|
||||
AccountMeta::new(payer.pubkey(), true),
|
||||
AccountMeta::new(proof_pda.0, false),
|
||||
AccountMeta::new_readonly(solana_program::system_program::id(), false),
|
||||
],
|
||||
data: [
|
||||
OreInstruction::Register.to_vec(),
|
||||
RegisterArgs { bump: proof_pda.1 }.to_bytes().to_vec(),
|
||||
]
|
||||
.concat(),
|
||||
};
|
||||
let tx = Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], blockhash);
|
||||
let res = banks.process_transaction(tx).await;
|
||||
assert!(res.is_err());
|
||||
}
|
||||
// // Try register for another keypair
|
||||
// let other = Keypair::new();
|
||||
// let proof_pda = Pubkey::find_program_address(&[PROOF, other.pubkey().as_ref()], &ore::id());
|
||||
// let ix = Instruction {
|
||||
// program_id: ore::id(),
|
||||
// accounts: vec![
|
||||
// AccountMeta::new(payer.pubkey(), true),
|
||||
// AccountMeta::new(proof_pda.0, false),
|
||||
// AccountMeta::new_readonly(solana_program::system_program::id(), false),
|
||||
// ],
|
||||
// data: [
|
||||
// OreInstruction::Register.to_vec(),
|
||||
// RegisterArgs { bump: proof_pda.1 }.to_bytes().to_vec(),
|
||||
// ]
|
||||
// .concat(),
|
||||
// };
|
||||
// let tx = Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], blockhash);
|
||||
// let res = banks.process_transaction(tx).await;
|
||||
// assert!(res.is_err());
|
||||
// }
|
||||
|
||||
async fn setup_program_test_env() -> (BanksClient, Keypair, solana_program::hash::Hash) {
|
||||
let mut program_test = ProgramTest::new("ore", ore::ID, processor!(ore::process_instruction));
|
||||
program_test.prefer_bpf(true);
|
||||
// async fn setup_program_test_env() -> (BanksClient, Keypair, solana_program::hash::Hash) {
|
||||
// let mut program_test = ProgramTest::new("ore", ore::ID, processor!(ore::process_instruction));
|
||||
// program_test.prefer_bpf(true);
|
||||
|
||||
// Busses
|
||||
for i in 0..BUS_COUNT {
|
||||
program_test.add_account_with_base64_data(
|
||||
BUS_ADDRESSES[i],
|
||||
1057920,
|
||||
ore::id(),
|
||||
bs64::encode(
|
||||
&[
|
||||
&(Bus::discriminator() as u64).to_le_bytes(),
|
||||
Bus {
|
||||
id: i as u64,
|
||||
rewards: 250_000_000,
|
||||
}
|
||||
.to_bytes(),
|
||||
]
|
||||
.concat(),
|
||||
)
|
||||
.as_str(),
|
||||
);
|
||||
}
|
||||
// // Busses
|
||||
// for i in 0..BUS_COUNT {
|
||||
// program_test.add_account_with_base64_data(
|
||||
// BUS_ADDRESSES[i],
|
||||
// 1057920,
|
||||
// ore::id(),
|
||||
// bs64::encode(
|
||||
// &[
|
||||
// &(Bus::discriminator() as u64).to_le_bytes(),
|
||||
// Bus {
|
||||
// id: i as u64,
|
||||
// rewards: 250_000_000,
|
||||
// }
|
||||
// .to_bytes(),
|
||||
// ]
|
||||
// .concat(),
|
||||
// )
|
||||
// .as_str(),
|
||||
// );
|
||||
// }
|
||||
|
||||
// Treasury
|
||||
let admin_address = Pubkey::from_str("AeNqnoLwFanMd3ig9WoMxQZVwQHtCtqKMMBsT1sTrvz6").unwrap();
|
||||
let treasury_pda = Pubkey::find_program_address(&[TREASURY], &ore::id());
|
||||
program_test.add_account_with_base64_data(
|
||||
treasury_pda.0,
|
||||
1614720,
|
||||
ore::id(),
|
||||
bs64::encode(
|
||||
&[
|
||||
&(Treasury::discriminator() as u64).to_le_bytes(),
|
||||
Treasury {
|
||||
bump: treasury_pda.1 as u64,
|
||||
admin: admin_address,
|
||||
difficulty: KeccakHash::new_from_array([u8::MAX; 32]).into(),
|
||||
last_reset_at: 100,
|
||||
reward_rate: INITIAL_REWARD_RATE,
|
||||
total_claimed_rewards: 0,
|
||||
}
|
||||
.to_bytes(),
|
||||
]
|
||||
.concat(),
|
||||
)
|
||||
.as_str(),
|
||||
);
|
||||
// // Treasury
|
||||
// let admin_address = Pubkey::from_str("AeNqnoLwFanMd3ig9WoMxQZVwQHtCtqKMMBsT1sTrvz6").unwrap();
|
||||
// let treasury_pda = Pubkey::find_program_address(&[TREASURY], &ore::id());
|
||||
// program_test.add_account_with_base64_data(
|
||||
// treasury_pda.0,
|
||||
// 1614720,
|
||||
// ore::id(),
|
||||
// bs64::encode(
|
||||
// &[
|
||||
// &(Treasury::discriminator() as u64).to_le_bytes(),
|
||||
// Treasury {
|
||||
// bump: treasury_pda.1 as u64,
|
||||
// admin: admin_address,
|
||||
// difficulty: KeccakHash::new_from_array([u8::MAX; 32]).into(),
|
||||
// last_reset_at: 100,
|
||||
// reward_rate: INITIAL_REWARD_RATE,
|
||||
// total_claimed_rewards: 0,
|
||||
// }
|
||||
// .to_bytes(),
|
||||
// ]
|
||||
// .concat(),
|
||||
// )
|
||||
// .as_str(),
|
||||
// );
|
||||
|
||||
// Mint
|
||||
let mut mint_src: [u8; Mint::LEN] = [0; Mint::LEN];
|
||||
Mint {
|
||||
mint_authority: COption::Some(TREASURY_ADDRESS),
|
||||
supply: 2_000_000_000,
|
||||
decimals: TOKEN_DECIMALS,
|
||||
is_initialized: true,
|
||||
freeze_authority: COption::None,
|
||||
}
|
||||
.pack_into_slice(&mut mint_src);
|
||||
program_test.add_account_with_base64_data(
|
||||
MINT_ADDRESS,
|
||||
1461600,
|
||||
spl_token::id(),
|
||||
bs64::encode(&mint_src).as_str(),
|
||||
);
|
||||
// // Mint
|
||||
// let mut mint_src: [u8; Mint::LEN] = [0; Mint::LEN];
|
||||
// Mint {
|
||||
// mint_authority: COption::Some(TREASURY_ADDRESS),
|
||||
// supply: 2_000_000_000,
|
||||
// decimals: TOKEN_DECIMALS,
|
||||
// is_initialized: true,
|
||||
// freeze_authority: COption::None,
|
||||
// }
|
||||
// .pack_into_slice(&mut mint_src);
|
||||
// program_test.add_account_with_base64_data(
|
||||
// MINT_ADDRESS,
|
||||
// 1461600,
|
||||
// spl_token::id(),
|
||||
// bs64::encode(&mint_src).as_str(),
|
||||
// );
|
||||
|
||||
// Treasury tokens
|
||||
let tokens_address = spl_associated_token_account::get_associated_token_address(
|
||||
&TREASURY_ADDRESS,
|
||||
&MINT_ADDRESS,
|
||||
);
|
||||
let mut tokens_src: [u8; spl_token::state::Account::LEN] = [0; spl_token::state::Account::LEN];
|
||||
spl_token::state::Account {
|
||||
mint: MINT_ADDRESS,
|
||||
owner: TREASURY_ADDRESS,
|
||||
amount: 2_000_000_000,
|
||||
delegate: COption::None,
|
||||
state: AccountState::Initialized,
|
||||
is_native: COption::None,
|
||||
delegated_amount: 0,
|
||||
close_authority: COption::None,
|
||||
}
|
||||
.pack_into_slice(&mut tokens_src);
|
||||
program_test.add_account_with_base64_data(
|
||||
tokens_address,
|
||||
2039280,
|
||||
spl_token::id(),
|
||||
bs64::encode(&tokens_src).as_str(),
|
||||
);
|
||||
// // Treasury tokens
|
||||
// let tokens_address = spl_associated_token_account::get_associated_token_address(
|
||||
// &TREASURY_ADDRESS,
|
||||
// &MINT_ADDRESS,
|
||||
// );
|
||||
// let mut tokens_src: [u8; spl_token::state::Account::LEN] = [0; spl_token::state::Account::LEN];
|
||||
// spl_token::state::Account {
|
||||
// mint: MINT_ADDRESS,
|
||||
// owner: TREASURY_ADDRESS,
|
||||
// amount: 2_000_000_000,
|
||||
// delegate: COption::None,
|
||||
// state: AccountState::Initialized,
|
||||
// is_native: COption::None,
|
||||
// delegated_amount: 0,
|
||||
// close_authority: COption::None,
|
||||
// }
|
||||
// .pack_into_slice(&mut tokens_src);
|
||||
// program_test.add_account_with_base64_data(
|
||||
// tokens_address,
|
||||
// 2039280,
|
||||
// spl_token::id(),
|
||||
// bs64::encode(&tokens_src).as_str(),
|
||||
// );
|
||||
|
||||
// Set sysvar
|
||||
program_test.add_sysvar_account(
|
||||
sysvar::clock::id(),
|
||||
&Clock {
|
||||
slot: 0,
|
||||
epoch_start_timestamp: 0,
|
||||
epoch: 0,
|
||||
leader_schedule_epoch: DEFAULT_SLOTS_PER_EPOCH,
|
||||
unix_timestamp: 0,
|
||||
},
|
||||
);
|
||||
// // Set sysvar
|
||||
// program_test.add_sysvar_account(
|
||||
// sysvar::clock::id(),
|
||||
// &Clock {
|
||||
// slot: 0,
|
||||
// epoch_start_timestamp: 0,
|
||||
// epoch: 0,
|
||||
// leader_schedule_epoch: DEFAULT_SLOTS_PER_EPOCH,
|
||||
// unix_timestamp: 0,
|
||||
// },
|
||||
// );
|
||||
|
||||
program_test.start().await
|
||||
}
|
||||
// program_test.start().await
|
||||
// }
|
||||
|
||||
@@ -1,440 +1,440 @@
|
||||
use std::str::FromStr;
|
||||
// use std::str::FromStr;
|
||||
|
||||
use ore::{
|
||||
instruction::OreInstruction,
|
||||
state::{Bus, Treasury},
|
||||
utils::{AccountDeserialize, Discriminator},
|
||||
BUS, BUS_ADDRESSES, BUS_COUNT, BUS_EPOCH_REWARDS, INITIAL_DIFFICULTY, INITIAL_REWARD_RATE,
|
||||
MAX_EPOCH_REWARDS, MINT_ADDRESS, START_AT, TOKEN_DECIMALS, TREASURY, TREASURY_ADDRESS,
|
||||
};
|
||||
use rand::seq::SliceRandom;
|
||||
use solana_program::{
|
||||
clock::Clock,
|
||||
epoch_schedule::DEFAULT_SLOTS_PER_EPOCH,
|
||||
hash::Hash,
|
||||
instruction::{AccountMeta, Instruction},
|
||||
native_token::LAMPORTS_PER_SOL,
|
||||
program_option::COption,
|
||||
program_pack::Pack,
|
||||
pubkey::Pubkey,
|
||||
system_program, sysvar,
|
||||
};
|
||||
use solana_program_test::{processor, BanksClient, ProgramTest};
|
||||
use solana_sdk::{
|
||||
account::Account,
|
||||
signature::{Keypair, Signer},
|
||||
transaction::Transaction,
|
||||
};
|
||||
use spl_token::state::{AccountState, Mint};
|
||||
// use ore::{
|
||||
// instruction::OreInstruction,
|
||||
// state::{Bus, Treasury},
|
||||
// utils::{AccountDeserialize, Discriminator},
|
||||
// BUS, BUS_ADDRESSES, BUS_COUNT, BUS_EPOCH_REWARDS, INITIAL_DIFFICULTY, INITIAL_REWARD_RATE,
|
||||
// MAX_EPOCH_REWARDS, MINT_ADDRESS, START_AT, TOKEN_DECIMALS, TREASURY, TREASURY_ADDRESS,
|
||||
// };
|
||||
// use rand::seq::SliceRandom;
|
||||
// use solana_program::{
|
||||
// clock::Clock,
|
||||
// epoch_schedule::DEFAULT_SLOTS_PER_EPOCH,
|
||||
// hash::Hash,
|
||||
// instruction::{AccountMeta, Instruction},
|
||||
// native_token::LAMPORTS_PER_SOL,
|
||||
// program_option::COption,
|
||||
// program_pack::Pack,
|
||||
// pubkey::Pubkey,
|
||||
// system_program, sysvar,
|
||||
// };
|
||||
// use solana_program_test::{processor, BanksClient, ProgramTest};
|
||||
// use solana_sdk::{
|
||||
// account::Account,
|
||||
// signature::{Keypair, Signer},
|
||||
// transaction::Transaction,
|
||||
// };
|
||||
// use spl_token::state::{AccountState, Mint};
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_reset() {
|
||||
// Setup
|
||||
let (mut banks, payer, _, blockhash) = setup_program_test_env(ClockState::Normal).await;
|
||||
// #[tokio::test]
|
||||
// async fn test_reset() {
|
||||
// // Setup
|
||||
// let (mut banks, payer, _, blockhash) = setup_program_test_env(ClockState::Normal).await;
|
||||
|
||||
// Pdas
|
||||
let bus_pdas = vec![
|
||||
Pubkey::find_program_address(&[BUS, &[0]], &ore::id()),
|
||||
Pubkey::find_program_address(&[BUS, &[1]], &ore::id()),
|
||||
Pubkey::find_program_address(&[BUS, &[2]], &ore::id()),
|
||||
Pubkey::find_program_address(&[BUS, &[3]], &ore::id()),
|
||||
Pubkey::find_program_address(&[BUS, &[4]], &ore::id()),
|
||||
Pubkey::find_program_address(&[BUS, &[5]], &ore::id()),
|
||||
Pubkey::find_program_address(&[BUS, &[6]], &ore::id()),
|
||||
Pubkey::find_program_address(&[BUS, &[7]], &ore::id()),
|
||||
];
|
||||
// let mint_pda = Pubkey::find_program_address(&[MINT], &ore::id());
|
||||
let treasury_tokens_address = spl_associated_token_account::get_associated_token_address(
|
||||
&TREASURY_ADDRESS,
|
||||
&MINT_ADDRESS,
|
||||
);
|
||||
// // Pdas
|
||||
// let bus_pdas = vec![
|
||||
// Pubkey::find_program_address(&[BUS, &[0]], &ore::id()),
|
||||
// Pubkey::find_program_address(&[BUS, &[1]], &ore::id()),
|
||||
// Pubkey::find_program_address(&[BUS, &[2]], &ore::id()),
|
||||
// Pubkey::find_program_address(&[BUS, &[3]], &ore::id()),
|
||||
// Pubkey::find_program_address(&[BUS, &[4]], &ore::id()),
|
||||
// Pubkey::find_program_address(&[BUS, &[5]], &ore::id()),
|
||||
// Pubkey::find_program_address(&[BUS, &[6]], &ore::id()),
|
||||
// Pubkey::find_program_address(&[BUS, &[7]], &ore::id()),
|
||||
// ];
|
||||
// // let mint_pda = Pubkey::find_program_address(&[MINT], &ore::id());
|
||||
// let treasury_tokens_address = spl_associated_token_account::get_associated_token_address(
|
||||
// &TREASURY_ADDRESS,
|
||||
// &MINT_ADDRESS,
|
||||
// );
|
||||
|
||||
// Submit tx
|
||||
let ix = ore::instruction::reset(payer.pubkey());
|
||||
let tx = Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], blockhash);
|
||||
let res = banks.process_transaction(tx).await;
|
||||
assert!(res.is_ok());
|
||||
// // Submit tx
|
||||
// let ix = ore::instruction::reset(payer.pubkey());
|
||||
// let tx = Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], blockhash);
|
||||
// let res = banks.process_transaction(tx).await;
|
||||
// assert!(res.is_ok());
|
||||
|
||||
// Test bus state
|
||||
for i in 0..BUS_COUNT {
|
||||
let bus_account = banks.get_account(bus_pdas[i].0).await.unwrap().unwrap();
|
||||
assert_eq!(bus_account.owner, ore::id());
|
||||
let bus = Bus::try_from_bytes(&bus_account.data).unwrap();
|
||||
assert_eq!(bus.id as u8, i as u8);
|
||||
assert_eq!(bus.rewards, BUS_EPOCH_REWARDS);
|
||||
}
|
||||
// // Test bus state
|
||||
// for i in 0..BUS_COUNT {
|
||||
// let bus_account = banks.get_account(bus_pdas[i].0).await.unwrap().unwrap();
|
||||
// assert_eq!(bus_account.owner, ore::id());
|
||||
// let bus = Bus::try_from_bytes(&bus_account.data).unwrap();
|
||||
// assert_eq!(bus.id as u8, i as u8);
|
||||
// assert_eq!(bus.rewards, BUS_EPOCH_REWARDS);
|
||||
// }
|
||||
|
||||
// Test treasury state
|
||||
let treasury_account = banks.get_account(TREASURY_ADDRESS).await.unwrap().unwrap();
|
||||
assert_eq!(treasury_account.owner, ore::id());
|
||||
let treasury = Treasury::try_from_bytes(&treasury_account.data).unwrap();
|
||||
assert_eq!(
|
||||
treasury.admin,
|
||||
Pubkey::from_str("AeNqnoLwFanMd3ig9WoMxQZVwQHtCtqKMMBsT1sTrvz6").unwrap()
|
||||
);
|
||||
assert_eq!(treasury.difficulty, INITIAL_DIFFICULTY.into());
|
||||
assert_eq!(treasury.last_reset_at, START_AT + 1);
|
||||
assert_eq!(treasury.reward_rate, INITIAL_REWARD_RATE.saturating_div(2));
|
||||
assert_eq!(treasury.total_claimed_rewards as u8, 0);
|
||||
// // Test treasury state
|
||||
// let treasury_account = banks.get_account(TREASURY_ADDRESS).await.unwrap().unwrap();
|
||||
// assert_eq!(treasury_account.owner, ore::id());
|
||||
// let treasury = Treasury::try_from_bytes(&treasury_account.data).unwrap();
|
||||
// assert_eq!(
|
||||
// treasury.admin,
|
||||
// Pubkey::from_str("AeNqnoLwFanMd3ig9WoMxQZVwQHtCtqKMMBsT1sTrvz6").unwrap()
|
||||
// );
|
||||
// assert_eq!(treasury.difficulty, INITIAL_DIFFICULTY.into());
|
||||
// assert_eq!(treasury.last_reset_at, START_AT + 1);
|
||||
// assert_eq!(treasury.reward_rate, INITIAL_REWARD_RATE.saturating_div(2));
|
||||
// assert_eq!(treasury.total_claimed_rewards as u8, 0);
|
||||
|
||||
// Test mint state
|
||||
let mint_account = banks.get_account(MINT_ADDRESS).await.unwrap().unwrap();
|
||||
assert_eq!(mint_account.owner, spl_token::id());
|
||||
let mint = Mint::unpack(&mint_account.data).unwrap();
|
||||
assert_eq!(mint.mint_authority, COption::Some(TREASURY_ADDRESS));
|
||||
assert_eq!(mint.supply, MAX_EPOCH_REWARDS);
|
||||
assert_eq!(mint.decimals, ore::TOKEN_DECIMALS);
|
||||
assert_eq!(mint.is_initialized, true);
|
||||
assert_eq!(mint.freeze_authority, COption::None);
|
||||
// // Test mint state
|
||||
// let mint_account = banks.get_account(MINT_ADDRESS).await.unwrap().unwrap();
|
||||
// assert_eq!(mint_account.owner, spl_token::id());
|
||||
// let mint = Mint::unpack(&mint_account.data).unwrap();
|
||||
// assert_eq!(mint.mint_authority, COption::Some(TREASURY_ADDRESS));
|
||||
// assert_eq!(mint.supply, MAX_EPOCH_REWARDS);
|
||||
// assert_eq!(mint.decimals, ore::TOKEN_DECIMALS);
|
||||
// assert_eq!(mint.is_initialized, true);
|
||||
// assert_eq!(mint.freeze_authority, COption::None);
|
||||
|
||||
// Test treasury token state
|
||||
let treasury_tokens_account = banks
|
||||
.get_account(treasury_tokens_address)
|
||||
.await
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
assert_eq!(treasury_tokens_account.owner, spl_token::id());
|
||||
let treasury_tokens = spl_token::state::Account::unpack(&treasury_tokens_account.data).unwrap();
|
||||
assert_eq!(treasury_tokens.mint, MINT_ADDRESS);
|
||||
assert_eq!(treasury_tokens.owner, TREASURY_ADDRESS);
|
||||
assert_eq!(treasury_tokens.amount, MAX_EPOCH_REWARDS);
|
||||
assert_eq!(treasury_tokens.delegate, COption::None);
|
||||
assert_eq!(treasury_tokens.state, AccountState::Initialized);
|
||||
assert_eq!(treasury_tokens.is_native, COption::None);
|
||||
assert_eq!(treasury_tokens.delegated_amount, 0);
|
||||
assert_eq!(treasury_tokens.close_authority, COption::None);
|
||||
}
|
||||
// // Test treasury token state
|
||||
// let treasury_tokens_account = banks
|
||||
// .get_account(treasury_tokens_address)
|
||||
// .await
|
||||
// .unwrap()
|
||||
// .unwrap();
|
||||
// assert_eq!(treasury_tokens_account.owner, spl_token::id());
|
||||
// let treasury_tokens = spl_token::state::Account::unpack(&treasury_tokens_account.data).unwrap();
|
||||
// assert_eq!(treasury_tokens.mint, MINT_ADDRESS);
|
||||
// assert_eq!(treasury_tokens.owner, TREASURY_ADDRESS);
|
||||
// assert_eq!(treasury_tokens.amount, MAX_EPOCH_REWARDS);
|
||||
// assert_eq!(treasury_tokens.delegate, COption::None);
|
||||
// assert_eq!(treasury_tokens.state, AccountState::Initialized);
|
||||
// assert_eq!(treasury_tokens.is_native, COption::None);
|
||||
// assert_eq!(treasury_tokens.delegated_amount, 0);
|
||||
// assert_eq!(treasury_tokens.close_authority, COption::None);
|
||||
// }
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_reset_bad_key() {
|
||||
// Setup
|
||||
let (mut banks, payer, _, blockhash) = setup_program_test_env(ClockState::Normal).await;
|
||||
// #[tokio::test]
|
||||
// async fn test_reset_bad_key() {
|
||||
// // Setup
|
||||
// let (mut banks, payer, _, blockhash) = setup_program_test_env(ClockState::Normal).await;
|
||||
|
||||
// Bad addresses
|
||||
let bad_pda = Pubkey::find_program_address(&[b"t"], &ore::id());
|
||||
for i in 1..13 {
|
||||
let mut ix = ore::instruction::reset(payer.pubkey());
|
||||
ix.accounts[i].pubkey = bad_pda.0;
|
||||
let tx =
|
||||
Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], blockhash);
|
||||
let res = banks.process_transaction(tx).await;
|
||||
assert!(res.is_err());
|
||||
}
|
||||
}
|
||||
// // Bad addresses
|
||||
// let bad_pda = Pubkey::find_program_address(&[b"t"], &ore::id());
|
||||
// for i in 1..13 {
|
||||
// let mut ix = ore::instruction::reset(payer.pubkey());
|
||||
// ix.accounts[i].pubkey = bad_pda.0;
|
||||
// let tx =
|
||||
// Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], blockhash);
|
||||
// let res = banks.process_transaction(tx).await;
|
||||
// assert!(res.is_err());
|
||||
// }
|
||||
// }
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_reset_busses_out_of_order_fail() {
|
||||
// Setup
|
||||
let (mut banks, payer, _, blockhash) = setup_program_test_env(ClockState::Normal).await;
|
||||
// #[tokio::test]
|
||||
// async fn test_reset_busses_out_of_order_fail() {
|
||||
// // Setup
|
||||
// let (mut banks, payer, _, blockhash) = setup_program_test_env(ClockState::Normal).await;
|
||||
|
||||
// Pdas
|
||||
let signer = payer.pubkey();
|
||||
let bus_pdas = vec![
|
||||
Pubkey::find_program_address(&[BUS, &[5]], &ore::id()),
|
||||
Pubkey::find_program_address(&[BUS, &[0]], &ore::id()),
|
||||
Pubkey::find_program_address(&[BUS, &[6]], &ore::id()),
|
||||
Pubkey::find_program_address(&[BUS, &[2]], &ore::id()),
|
||||
Pubkey::find_program_address(&[BUS, &[3]], &ore::id()),
|
||||
Pubkey::find_program_address(&[BUS, &[7]], &ore::id()),
|
||||
Pubkey::find_program_address(&[BUS, &[1]], &ore::id()),
|
||||
Pubkey::find_program_address(&[BUS, &[4]], &ore::id()),
|
||||
];
|
||||
let treasury_tokens = spl_associated_token_account::get_associated_token_address(
|
||||
&TREASURY_ADDRESS,
|
||||
&MINT_ADDRESS,
|
||||
);
|
||||
// // Pdas
|
||||
// let signer = payer.pubkey();
|
||||
// let bus_pdas = vec![
|
||||
// Pubkey::find_program_address(&[BUS, &[5]], &ore::id()),
|
||||
// Pubkey::find_program_address(&[BUS, &[0]], &ore::id()),
|
||||
// Pubkey::find_program_address(&[BUS, &[6]], &ore::id()),
|
||||
// Pubkey::find_program_address(&[BUS, &[2]], &ore::id()),
|
||||
// Pubkey::find_program_address(&[BUS, &[3]], &ore::id()),
|
||||
// Pubkey::find_program_address(&[BUS, &[7]], &ore::id()),
|
||||
// Pubkey::find_program_address(&[BUS, &[1]], &ore::id()),
|
||||
// Pubkey::find_program_address(&[BUS, &[4]], &ore::id()),
|
||||
// ];
|
||||
// let treasury_tokens = spl_associated_token_account::get_associated_token_address(
|
||||
// &TREASURY_ADDRESS,
|
||||
// &MINT_ADDRESS,
|
||||
// );
|
||||
|
||||
// Submit tx
|
||||
let ix = Instruction {
|
||||
program_id: ore::id(),
|
||||
accounts: vec![
|
||||
AccountMeta::new(signer, true),
|
||||
AccountMeta::new(bus_pdas[0].0, false),
|
||||
AccountMeta::new(bus_pdas[1].0, false),
|
||||
AccountMeta::new(bus_pdas[2].0, false),
|
||||
AccountMeta::new(bus_pdas[3].0, false),
|
||||
AccountMeta::new(bus_pdas[4].0, false),
|
||||
AccountMeta::new(bus_pdas[5].0, false),
|
||||
AccountMeta::new(bus_pdas[6].0, false),
|
||||
AccountMeta::new(bus_pdas[7].0, false),
|
||||
AccountMeta::new(MINT_ADDRESS, false),
|
||||
AccountMeta::new(TREASURY_ADDRESS, false),
|
||||
AccountMeta::new(treasury_tokens, false),
|
||||
AccountMeta::new_readonly(spl_token::id(), false),
|
||||
],
|
||||
data: OreInstruction::Reset.to_vec(),
|
||||
};
|
||||
let tx = Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], blockhash);
|
||||
let res = banks.process_transaction(tx).await;
|
||||
assert!(res.is_err());
|
||||
}
|
||||
// // Submit tx
|
||||
// let ix = Instruction {
|
||||
// program_id: ore::id(),
|
||||
// accounts: vec![
|
||||
// AccountMeta::new(signer, true),
|
||||
// AccountMeta::new(bus_pdas[0].0, false),
|
||||
// AccountMeta::new(bus_pdas[1].0, false),
|
||||
// AccountMeta::new(bus_pdas[2].0, false),
|
||||
// AccountMeta::new(bus_pdas[3].0, false),
|
||||
// AccountMeta::new(bus_pdas[4].0, false),
|
||||
// AccountMeta::new(bus_pdas[5].0, false),
|
||||
// AccountMeta::new(bus_pdas[6].0, false),
|
||||
// AccountMeta::new(bus_pdas[7].0, false),
|
||||
// AccountMeta::new(MINT_ADDRESS, false),
|
||||
// AccountMeta::new(TREASURY_ADDRESS, false),
|
||||
// AccountMeta::new(treasury_tokens, false),
|
||||
// AccountMeta::new_readonly(spl_token::id(), false),
|
||||
// ],
|
||||
// data: OreInstruction::Reset.to_vec(),
|
||||
// };
|
||||
// let tx = Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], blockhash);
|
||||
// let res = banks.process_transaction(tx).await;
|
||||
// assert!(res.is_err());
|
||||
// }
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_reset_race() {
|
||||
// Setup
|
||||
let (mut banks, payer, payer_alt, blockhash) = setup_program_test_env(ClockState::Normal).await;
|
||||
// #[tokio::test]
|
||||
// async fn test_reset_race() {
|
||||
// // Setup
|
||||
// let (mut banks, payer, payer_alt, blockhash) = setup_program_test_env(ClockState::Normal).await;
|
||||
|
||||
// Reset one passes
|
||||
let ix = ore::instruction::reset(payer.pubkey());
|
||||
let tx = Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], blockhash);
|
||||
let res = banks.process_transaction(tx).await;
|
||||
assert!(res.is_ok());
|
||||
// // Reset one passes
|
||||
// let ix = ore::instruction::reset(payer.pubkey());
|
||||
// let tx = Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], blockhash);
|
||||
// let res = banks.process_transaction(tx).await;
|
||||
// assert!(res.is_ok());
|
||||
|
||||
// Reset two fails
|
||||
let ix = ore::instruction::reset(payer_alt.pubkey());
|
||||
let tx = Transaction::new_signed_with_payer(
|
||||
&[ix],
|
||||
Some(&payer_alt.pubkey()),
|
||||
&[&payer_alt],
|
||||
blockhash,
|
||||
);
|
||||
let res = banks.process_transaction(tx).await;
|
||||
assert!(res.is_err());
|
||||
}
|
||||
// // Reset two fails
|
||||
// let ix = ore::instruction::reset(payer_alt.pubkey());
|
||||
// let tx = Transaction::new_signed_with_payer(
|
||||
// &[ix],
|
||||
// Some(&payer_alt.pubkey()),
|
||||
// &[&payer_alt],
|
||||
// blockhash,
|
||||
// );
|
||||
// let res = banks.process_transaction(tx).await;
|
||||
// assert!(res.is_err());
|
||||
// }
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_reset_too_early() {
|
||||
// Setup
|
||||
let (mut banks, payer, _, blockhash) = setup_program_test_env(ClockState::TooEarly).await;
|
||||
// #[tokio::test]
|
||||
// async fn test_reset_too_early() {
|
||||
// // Setup
|
||||
// let (mut banks, payer, _, blockhash) = setup_program_test_env(ClockState::TooEarly).await;
|
||||
|
||||
// Reset one passes
|
||||
let ix = ore::instruction::reset(payer.pubkey());
|
||||
let tx = Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], blockhash);
|
||||
let res = banks.process_transaction(tx).await;
|
||||
assert!(res.is_err());
|
||||
}
|
||||
// // Reset one passes
|
||||
// let ix = ore::instruction::reset(payer.pubkey());
|
||||
// let tx = Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], blockhash);
|
||||
// let res = banks.process_transaction(tx).await;
|
||||
// assert!(res.is_err());
|
||||
// }
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_reset_not_enough_keys() {
|
||||
// Setup
|
||||
let (mut banks, payer, _, blockhash) = setup_program_test_env(ClockState::Normal).await;
|
||||
// #[tokio::test]
|
||||
// async fn test_reset_not_enough_keys() {
|
||||
// // Setup
|
||||
// let (mut banks, payer, _, blockhash) = setup_program_test_env(ClockState::Normal).await;
|
||||
|
||||
// Reset with missing account
|
||||
let mut ix = ore::instruction::reset(payer.pubkey());
|
||||
ix.accounts.remove(1);
|
||||
let tx = Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], blockhash);
|
||||
let res = banks.process_transaction(tx).await;
|
||||
assert!(res.is_err());
|
||||
}
|
||||
// // Reset with missing account
|
||||
// let mut ix = ore::instruction::reset(payer.pubkey());
|
||||
// ix.accounts.remove(1);
|
||||
// let tx = Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], blockhash);
|
||||
// let res = banks.process_transaction(tx).await;
|
||||
// assert!(res.is_err());
|
||||
// }
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_reset_busses_duplicate_fail() {
|
||||
// Setup
|
||||
let (mut banks, payer, _, blockhash) = setup_program_test_env(ClockState::Normal).await;
|
||||
// #[tokio::test]
|
||||
// async fn test_reset_busses_duplicate_fail() {
|
||||
// // Setup
|
||||
// let (mut banks, payer, _, blockhash) = setup_program_test_env(ClockState::Normal).await;
|
||||
|
||||
// Pdas
|
||||
let signer = payer.pubkey();
|
||||
let bus_pda = Pubkey::find_program_address(&[BUS, &[0]], &ore::id());
|
||||
let treasury_tokens = spl_associated_token_account::get_associated_token_address(
|
||||
&TREASURY_ADDRESS,
|
||||
&MINT_ADDRESS,
|
||||
);
|
||||
// // Pdas
|
||||
// let signer = payer.pubkey();
|
||||
// let bus_pda = Pubkey::find_program_address(&[BUS, &[0]], &ore::id());
|
||||
// let treasury_tokens = spl_associated_token_account::get_associated_token_address(
|
||||
// &TREASURY_ADDRESS,
|
||||
// &MINT_ADDRESS,
|
||||
// );
|
||||
|
||||
// Submit tx
|
||||
let ix = Instruction {
|
||||
program_id: ore::id(),
|
||||
accounts: vec![
|
||||
AccountMeta::new(signer, true),
|
||||
AccountMeta::new(bus_pda.0, false),
|
||||
AccountMeta::new(bus_pda.0, false),
|
||||
AccountMeta::new(bus_pda.0, false),
|
||||
AccountMeta::new(bus_pda.0, false),
|
||||
AccountMeta::new(bus_pda.0, false),
|
||||
AccountMeta::new(bus_pda.0, false),
|
||||
AccountMeta::new(bus_pda.0, false),
|
||||
AccountMeta::new(bus_pda.0, false),
|
||||
AccountMeta::new(MINT_ADDRESS, false),
|
||||
AccountMeta::new(TREASURY_ADDRESS, false),
|
||||
AccountMeta::new(treasury_tokens, false),
|
||||
AccountMeta::new_readonly(spl_token::id(), false),
|
||||
],
|
||||
data: OreInstruction::Reset.to_vec(),
|
||||
};
|
||||
let tx = Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], blockhash);
|
||||
let res = banks.process_transaction(tx).await;
|
||||
assert!(res.is_err());
|
||||
}
|
||||
// // Submit tx
|
||||
// let ix = Instruction {
|
||||
// program_id: ore::id(),
|
||||
// accounts: vec![
|
||||
// AccountMeta::new(signer, true),
|
||||
// AccountMeta::new(bus_pda.0, false),
|
||||
// AccountMeta::new(bus_pda.0, false),
|
||||
// AccountMeta::new(bus_pda.0, false),
|
||||
// AccountMeta::new(bus_pda.0, false),
|
||||
// AccountMeta::new(bus_pda.0, false),
|
||||
// AccountMeta::new(bus_pda.0, false),
|
||||
// AccountMeta::new(bus_pda.0, false),
|
||||
// AccountMeta::new(bus_pda.0, false),
|
||||
// AccountMeta::new(MINT_ADDRESS, false),
|
||||
// AccountMeta::new(TREASURY_ADDRESS, false),
|
||||
// AccountMeta::new(treasury_tokens, false),
|
||||
// AccountMeta::new_readonly(spl_token::id(), false),
|
||||
// ],
|
||||
// data: OreInstruction::Reset.to_vec(),
|
||||
// };
|
||||
// let tx = Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], blockhash);
|
||||
// let res = banks.process_transaction(tx).await;
|
||||
// assert!(res.is_err());
|
||||
// }
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_reset_shuffle_error() {
|
||||
// Setup
|
||||
const FUZZ: u64 = 100;
|
||||
let (mut banks, payer, _, blockhash) = setup_program_test_env(ClockState::Normal).await;
|
||||
// #[tokio::test]
|
||||
// async fn test_reset_shuffle_error() {
|
||||
// // Setup
|
||||
// const FUZZ: u64 = 100;
|
||||
// let (mut banks, payer, _, blockhash) = setup_program_test_env(ClockState::Normal).await;
|
||||
|
||||
// Pdas
|
||||
let signer = payer.pubkey();
|
||||
let bus_pdas = vec![
|
||||
Pubkey::find_program_address(&[BUS, &[5]], &ore::id()),
|
||||
Pubkey::find_program_address(&[BUS, &[0]], &ore::id()),
|
||||
Pubkey::find_program_address(&[BUS, &[6]], &ore::id()),
|
||||
Pubkey::find_program_address(&[BUS, &[2]], &ore::id()),
|
||||
Pubkey::find_program_address(&[BUS, &[3]], &ore::id()),
|
||||
Pubkey::find_program_address(&[BUS, &[7]], &ore::id()),
|
||||
Pubkey::find_program_address(&[BUS, &[1]], &ore::id()),
|
||||
Pubkey::find_program_address(&[BUS, &[4]], &ore::id()),
|
||||
];
|
||||
let treasury_tokens = spl_associated_token_account::get_associated_token_address(
|
||||
&TREASURY_ADDRESS,
|
||||
&MINT_ADDRESS,
|
||||
);
|
||||
// // Pdas
|
||||
// let signer = payer.pubkey();
|
||||
// let bus_pdas = vec![
|
||||
// Pubkey::find_program_address(&[BUS, &[5]], &ore::id()),
|
||||
// Pubkey::find_program_address(&[BUS, &[0]], &ore::id()),
|
||||
// Pubkey::find_program_address(&[BUS, &[6]], &ore::id()),
|
||||
// Pubkey::find_program_address(&[BUS, &[2]], &ore::id()),
|
||||
// Pubkey::find_program_address(&[BUS, &[3]], &ore::id()),
|
||||
// Pubkey::find_program_address(&[BUS, &[7]], &ore::id()),
|
||||
// Pubkey::find_program_address(&[BUS, &[1]], &ore::id()),
|
||||
// Pubkey::find_program_address(&[BUS, &[4]], &ore::id()),
|
||||
// ];
|
||||
// let treasury_tokens = spl_associated_token_account::get_associated_token_address(
|
||||
// &TREASURY_ADDRESS,
|
||||
// &MINT_ADDRESS,
|
||||
// );
|
||||
|
||||
// Fuzz test shuffled accounts.
|
||||
// Note some shuffles may still be valid if signer and non-bus accounts are all in correct positions.
|
||||
let mut rng = rand::thread_rng();
|
||||
for _ in 0..FUZZ {
|
||||
let mut accounts = vec![
|
||||
AccountMeta::new(signer, true),
|
||||
AccountMeta::new(bus_pdas[0].0, false),
|
||||
AccountMeta::new(bus_pdas[1].0, false),
|
||||
AccountMeta::new(bus_pdas[2].0, false),
|
||||
AccountMeta::new(bus_pdas[3].0, false),
|
||||
AccountMeta::new(bus_pdas[4].0, false),
|
||||
AccountMeta::new(bus_pdas[5].0, false),
|
||||
AccountMeta::new(bus_pdas[6].0, false),
|
||||
AccountMeta::new(bus_pdas[7].0, false),
|
||||
AccountMeta::new(MINT_ADDRESS, false),
|
||||
AccountMeta::new(TREASURY_ADDRESS, false),
|
||||
AccountMeta::new(treasury_tokens, false),
|
||||
AccountMeta::new_readonly(spl_token::id(), false),
|
||||
];
|
||||
accounts.shuffle(&mut rng);
|
||||
let ix = Instruction {
|
||||
program_id: ore::id(),
|
||||
accounts,
|
||||
data: OreInstruction::Reset.to_vec(),
|
||||
};
|
||||
let tx =
|
||||
Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], blockhash);
|
||||
let res = banks.process_transaction(tx).await;
|
||||
assert!(res.is_err());
|
||||
}
|
||||
}
|
||||
// // Fuzz test shuffled accounts.
|
||||
// // Note some shuffles may still be valid if signer and non-bus accounts are all in correct positions.
|
||||
// let mut rng = rand::thread_rng();
|
||||
// for _ in 0..FUZZ {
|
||||
// let mut accounts = vec![
|
||||
// AccountMeta::new(signer, true),
|
||||
// AccountMeta::new(bus_pdas[0].0, false),
|
||||
// AccountMeta::new(bus_pdas[1].0, false),
|
||||
// AccountMeta::new(bus_pdas[2].0, false),
|
||||
// AccountMeta::new(bus_pdas[3].0, false),
|
||||
// AccountMeta::new(bus_pdas[4].0, false),
|
||||
// AccountMeta::new(bus_pdas[5].0, false),
|
||||
// AccountMeta::new(bus_pdas[6].0, false),
|
||||
// AccountMeta::new(bus_pdas[7].0, false),
|
||||
// AccountMeta::new(MINT_ADDRESS, false),
|
||||
// AccountMeta::new(TREASURY_ADDRESS, false),
|
||||
// AccountMeta::new(treasury_tokens, false),
|
||||
// AccountMeta::new_readonly(spl_token::id(), false),
|
||||
// ];
|
||||
// accounts.shuffle(&mut rng);
|
||||
// let ix = Instruction {
|
||||
// program_id: ore::id(),
|
||||
// accounts,
|
||||
// data: OreInstruction::Reset.to_vec(),
|
||||
// };
|
||||
// let tx =
|
||||
// Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], blockhash);
|
||||
// let res = banks.process_transaction(tx).await;
|
||||
// assert!(res.is_err());
|
||||
// }
|
||||
// }
|
||||
|
||||
enum ClockState {
|
||||
Normal,
|
||||
TooEarly,
|
||||
}
|
||||
// enum ClockState {
|
||||
// Normal,
|
||||
// TooEarly,
|
||||
// }
|
||||
|
||||
async fn setup_program_test_env(clock_state: ClockState) -> (BanksClient, Keypair, Keypair, Hash) {
|
||||
let mut program_test = ProgramTest::new("ore", ore::ID, processor!(ore::process_instruction));
|
||||
program_test.prefer_bpf(true);
|
||||
// async fn setup_program_test_env(clock_state: ClockState) -> (BanksClient, Keypair, Keypair, Hash) {
|
||||
// let mut program_test = ProgramTest::new("ore", ore::ID, processor!(ore::process_instruction));
|
||||
// program_test.prefer_bpf(true);
|
||||
|
||||
// Busses
|
||||
for i in 0..BUS_COUNT {
|
||||
program_test.add_account_with_base64_data(
|
||||
BUS_ADDRESSES[i],
|
||||
1057920,
|
||||
ore::id(),
|
||||
bs64::encode(
|
||||
&[
|
||||
&(Bus::discriminator() as u64).to_le_bytes(),
|
||||
Bus {
|
||||
id: i as u64,
|
||||
rewards: 0,
|
||||
}
|
||||
.to_bytes(),
|
||||
]
|
||||
.concat(),
|
||||
)
|
||||
.as_str(),
|
||||
);
|
||||
}
|
||||
// // Busses
|
||||
// for i in 0..BUS_COUNT {
|
||||
// program_test.add_account_with_base64_data(
|
||||
// BUS_ADDRESSES[i],
|
||||
// 1057920,
|
||||
// ore::id(),
|
||||
// bs64::encode(
|
||||
// &[
|
||||
// &(Bus::discriminator() as u64).to_le_bytes(),
|
||||
// Bus {
|
||||
// id: i as u64,
|
||||
// rewards: 0,
|
||||
// }
|
||||
// .to_bytes(),
|
||||
// ]
|
||||
// .concat(),
|
||||
// )
|
||||
// .as_str(),
|
||||
// );
|
||||
// }
|
||||
|
||||
// Treasury
|
||||
let admin_address = Pubkey::from_str("AeNqnoLwFanMd3ig9WoMxQZVwQHtCtqKMMBsT1sTrvz6").unwrap();
|
||||
let treasury_pda = Pubkey::find_program_address(&[TREASURY], &ore::id());
|
||||
program_test.add_account_with_base64_data(
|
||||
treasury_pda.0,
|
||||
1614720,
|
||||
ore::id(),
|
||||
bs64::encode(
|
||||
&[
|
||||
&(Treasury::discriminator() as u64).to_le_bytes(),
|
||||
Treasury {
|
||||
bump: treasury_pda.1 as u64,
|
||||
admin: admin_address,
|
||||
difficulty: INITIAL_DIFFICULTY.into(),
|
||||
last_reset_at: 0,
|
||||
reward_rate: INITIAL_REWARD_RATE,
|
||||
total_claimed_rewards: 0,
|
||||
}
|
||||
.to_bytes(),
|
||||
]
|
||||
.concat(),
|
||||
)
|
||||
.as_str(),
|
||||
);
|
||||
// // Treasury
|
||||
// let admin_address = Pubkey::from_str("AeNqnoLwFanMd3ig9WoMxQZVwQHtCtqKMMBsT1sTrvz6").unwrap();
|
||||
// let treasury_pda = Pubkey::find_program_address(&[TREASURY], &ore::id());
|
||||
// program_test.add_account_with_base64_data(
|
||||
// treasury_pda.0,
|
||||
// 1614720,
|
||||
// ore::id(),
|
||||
// bs64::encode(
|
||||
// &[
|
||||
// &(Treasury::discriminator() as u64).to_le_bytes(),
|
||||
// Treasury {
|
||||
// bump: treasury_pda.1 as u64,
|
||||
// admin: admin_address,
|
||||
// difficulty: INITIAL_DIFFICULTY.into(),
|
||||
// last_reset_at: 0,
|
||||
// reward_rate: INITIAL_REWARD_RATE,
|
||||
// total_claimed_rewards: 0,
|
||||
// }
|
||||
// .to_bytes(),
|
||||
// ]
|
||||
// .concat(),
|
||||
// )
|
||||
// .as_str(),
|
||||
// );
|
||||
|
||||
// Mint
|
||||
let mut mint_src: [u8; Mint::LEN] = [0; Mint::LEN];
|
||||
Mint {
|
||||
mint_authority: COption::Some(TREASURY_ADDRESS),
|
||||
supply: 0,
|
||||
decimals: TOKEN_DECIMALS,
|
||||
is_initialized: true,
|
||||
freeze_authority: COption::None,
|
||||
}
|
||||
.pack_into_slice(&mut mint_src);
|
||||
program_test.add_account_with_base64_data(
|
||||
MINT_ADDRESS,
|
||||
1461600,
|
||||
spl_token::id(),
|
||||
bs64::encode(&mint_src).as_str(),
|
||||
);
|
||||
// // Mint
|
||||
// let mut mint_src: [u8; Mint::LEN] = [0; Mint::LEN];
|
||||
// Mint {
|
||||
// mint_authority: COption::Some(TREASURY_ADDRESS),
|
||||
// supply: 0,
|
||||
// decimals: TOKEN_DECIMALS,
|
||||
// is_initialized: true,
|
||||
// freeze_authority: COption::None,
|
||||
// }
|
||||
// .pack_into_slice(&mut mint_src);
|
||||
// program_test.add_account_with_base64_data(
|
||||
// MINT_ADDRESS,
|
||||
// 1461600,
|
||||
// spl_token::id(),
|
||||
// bs64::encode(&mint_src).as_str(),
|
||||
// );
|
||||
|
||||
// Treasury tokens
|
||||
let tokens_address = spl_associated_token_account::get_associated_token_address(
|
||||
&TREASURY_ADDRESS,
|
||||
&MINT_ADDRESS,
|
||||
);
|
||||
let mut tokens_src: [u8; spl_token::state::Account::LEN] = [0; spl_token::state::Account::LEN];
|
||||
spl_token::state::Account {
|
||||
mint: MINT_ADDRESS,
|
||||
owner: TREASURY_ADDRESS,
|
||||
amount: 0,
|
||||
delegate: COption::None,
|
||||
state: AccountState::Initialized,
|
||||
is_native: COption::None,
|
||||
delegated_amount: 0,
|
||||
close_authority: COption::None,
|
||||
}
|
||||
.pack_into_slice(&mut tokens_src);
|
||||
program_test.add_account_with_base64_data(
|
||||
tokens_address,
|
||||
2039280,
|
||||
spl_token::id(),
|
||||
bs64::encode(&tokens_src).as_str(),
|
||||
);
|
||||
// // Treasury tokens
|
||||
// let tokens_address = spl_associated_token_account::get_associated_token_address(
|
||||
// &TREASURY_ADDRESS,
|
||||
// &MINT_ADDRESS,
|
||||
// );
|
||||
// let mut tokens_src: [u8; spl_token::state::Account::LEN] = [0; spl_token::state::Account::LEN];
|
||||
// spl_token::state::Account {
|
||||
// mint: MINT_ADDRESS,
|
||||
// owner: TREASURY_ADDRESS,
|
||||
// amount: 0,
|
||||
// delegate: COption::None,
|
||||
// state: AccountState::Initialized,
|
||||
// is_native: COption::None,
|
||||
// delegated_amount: 0,
|
||||
// close_authority: COption::None,
|
||||
// }
|
||||
// .pack_into_slice(&mut tokens_src);
|
||||
// program_test.add_account_with_base64_data(
|
||||
// tokens_address,
|
||||
// 2039280,
|
||||
// spl_token::id(),
|
||||
// bs64::encode(&tokens_src).as_str(),
|
||||
// );
|
||||
|
||||
// Set sysvar
|
||||
let ts = match clock_state {
|
||||
ClockState::Normal => START_AT + 1,
|
||||
ClockState::TooEarly => START_AT - 1,
|
||||
};
|
||||
program_test.add_sysvar_account(
|
||||
sysvar::clock::id(),
|
||||
&Clock {
|
||||
slot: 0,
|
||||
epoch_start_timestamp: 0,
|
||||
epoch: 0,
|
||||
leader_schedule_epoch: DEFAULT_SLOTS_PER_EPOCH,
|
||||
unix_timestamp: ts,
|
||||
},
|
||||
);
|
||||
// // Set sysvar
|
||||
// let ts = match clock_state {
|
||||
// ClockState::Normal => START_AT + 1,
|
||||
// ClockState::TooEarly => START_AT - 1,
|
||||
// };
|
||||
// program_test.add_sysvar_account(
|
||||
// sysvar::clock::id(),
|
||||
// &Clock {
|
||||
// slot: 0,
|
||||
// epoch_start_timestamp: 0,
|
||||
// epoch: 0,
|
||||
// leader_schedule_epoch: DEFAULT_SLOTS_PER_EPOCH,
|
||||
// unix_timestamp: ts,
|
||||
// },
|
||||
// );
|
||||
|
||||
// Setup alt payer
|
||||
let payer_alt = Keypair::new();
|
||||
program_test.add_account(
|
||||
payer_alt.pubkey(),
|
||||
Account {
|
||||
lamports: LAMPORTS_PER_SOL,
|
||||
data: vec![],
|
||||
owner: system_program::id(),
|
||||
executable: false,
|
||||
rent_epoch: 0,
|
||||
},
|
||||
);
|
||||
// // Setup alt payer
|
||||
// let payer_alt = Keypair::new();
|
||||
// program_test.add_account(
|
||||
// payer_alt.pubkey(),
|
||||
// Account {
|
||||
// lamports: LAMPORTS_PER_SOL,
|
||||
// data: vec![],
|
||||
// owner: system_program::id(),
|
||||
// executable: false,
|
||||
// rent_epoch: 0,
|
||||
// },
|
||||
// );
|
||||
|
||||
let (banks, payer, blockhash) = program_test.start().await;
|
||||
(banks, payer, payer_alt, blockhash)
|
||||
}
|
||||
// let (banks, payer, blockhash) = program_test.start().await;
|
||||
// (banks, payer, payer_alt, blockhash)
|
||||
// }
|
||||
|
||||
@@ -1,128 +1,128 @@
|
||||
use ore::{state::Treasury, utils::AccountDeserialize, TREASURY_ADDRESS};
|
||||
use solana_program::{
|
||||
hash::Hash, native_token::LAMPORTS_PER_SOL, pubkey::Pubkey, rent::Rent, system_program,
|
||||
};
|
||||
use solana_program_test::{processor, read_file, BanksClient, ProgramTest};
|
||||
use solana_sdk::{
|
||||
account::Account,
|
||||
signature::{Keypair, Signer},
|
||||
transaction::Transaction,
|
||||
};
|
||||
// use ore::{state::Treasury, utils::AccountDeserialize, TREASURY_ADDRESS};
|
||||
// use solana_program::{
|
||||
// hash::Hash, native_token::LAMPORTS_PER_SOL, pubkey::Pubkey, rent::Rent, system_program,
|
||||
// };
|
||||
// use solana_program_test::{processor, read_file, BanksClient, ProgramTest};
|
||||
// use solana_sdk::{
|
||||
// account::Account,
|
||||
// signature::{Keypair, Signer},
|
||||
// transaction::Transaction,
|
||||
// };
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_update_admin() {
|
||||
// Setup
|
||||
let (mut banks, payer, _, blockhash) = setup_program_test_env().await;
|
||||
// #[tokio::test]
|
||||
// async fn test_update_admin() {
|
||||
// // Setup
|
||||
// let (mut banks, payer, _, blockhash) = setup_program_test_env().await;
|
||||
|
||||
// Submit tx
|
||||
let ix = ore::instruction::initialize(payer.pubkey());
|
||||
let tx = Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], blockhash);
|
||||
let res = banks.process_transaction(tx).await;
|
||||
assert!(res.is_ok());
|
||||
// // Submit tx
|
||||
// let ix = ore::instruction::initialize(payer.pubkey());
|
||||
// let tx = Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], blockhash);
|
||||
// let res = banks.process_transaction(tx).await;
|
||||
// assert!(res.is_ok());
|
||||
|
||||
// Get treasury account
|
||||
let treasury_account = banks.get_account(TREASURY_ADDRESS).await.unwrap().unwrap();
|
||||
let treasury = Treasury::try_from_bytes(&treasury_account.data).unwrap();
|
||||
// // Get treasury account
|
||||
// let treasury_account = banks.get_account(TREASURY_ADDRESS).await.unwrap().unwrap();
|
||||
// let treasury = Treasury::try_from_bytes(&treasury_account.data).unwrap();
|
||||
|
||||
// Submit update admin ix
|
||||
let new_admin = Pubkey::new_unique();
|
||||
let ix = ore::instruction::update_admin(payer.pubkey(), new_admin);
|
||||
let tx = Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], blockhash);
|
||||
let res = banks.process_transaction(tx).await;
|
||||
assert!(res.is_ok());
|
||||
// // Submit update admin ix
|
||||
// let new_admin = Pubkey::new_unique();
|
||||
// let ix = ore::instruction::update_admin(payer.pubkey(), new_admin);
|
||||
// let tx = Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], blockhash);
|
||||
// let res = banks.process_transaction(tx).await;
|
||||
// assert!(res.is_ok());
|
||||
|
||||
// Assert treasury state
|
||||
let treasury_account = banks.get_account(TREASURY_ADDRESS).await.unwrap().unwrap();
|
||||
let treasury_ = Treasury::try_from_bytes(&treasury_account.data).unwrap();
|
||||
assert_eq!(treasury_.bump, treasury.bump);
|
||||
assert_eq!(treasury_.admin, new_admin);
|
||||
assert_eq!(treasury_.difficulty, treasury.difficulty);
|
||||
assert_eq!(treasury_.last_reset_at, treasury.last_reset_at);
|
||||
assert_eq!(treasury_.reward_rate, treasury.reward_rate);
|
||||
assert_eq!(
|
||||
treasury_.total_claimed_rewards,
|
||||
treasury.total_claimed_rewards
|
||||
);
|
||||
// // Assert treasury state
|
||||
// let treasury_account = banks.get_account(TREASURY_ADDRESS).await.unwrap().unwrap();
|
||||
// let treasury_ = Treasury::try_from_bytes(&treasury_account.data).unwrap();
|
||||
// assert_eq!(treasury_.bump, treasury.bump);
|
||||
// assert_eq!(treasury_.admin, new_admin);
|
||||
// assert_eq!(treasury_.difficulty, treasury.difficulty);
|
||||
// assert_eq!(treasury_.last_reset_at, treasury.last_reset_at);
|
||||
// assert_eq!(treasury_.reward_rate, treasury.reward_rate);
|
||||
// assert_eq!(
|
||||
// treasury_.total_claimed_rewards,
|
||||
// treasury.total_claimed_rewards
|
||||
// );
|
||||
|
||||
// Submit another update admin ix
|
||||
let ix = ore::instruction::update_admin(payer.pubkey(), payer.pubkey());
|
||||
let tx = Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], blockhash);
|
||||
let res = banks.process_transaction(tx).await;
|
||||
assert!(res.is_err());
|
||||
}
|
||||
// // Submit another update admin ix
|
||||
// let ix = ore::instruction::update_admin(payer.pubkey(), payer.pubkey());
|
||||
// let tx = Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], blockhash);
|
||||
// let res = banks.process_transaction(tx).await;
|
||||
// assert!(res.is_err());
|
||||
// }
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_update_admin_bad_signer() {
|
||||
// Setup
|
||||
let (mut banks, payer, alt_payer, blockhash) = setup_program_test_env().await;
|
||||
// #[tokio::test]
|
||||
// async fn test_update_admin_bad_signer() {
|
||||
// // Setup
|
||||
// let (mut banks, payer, alt_payer, blockhash) = setup_program_test_env().await;
|
||||
|
||||
// Submit tx
|
||||
let ix = ore::instruction::initialize(payer.pubkey());
|
||||
let tx = Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], blockhash);
|
||||
let res = banks.process_transaction(tx).await;
|
||||
assert!(res.is_ok());
|
||||
// // Submit tx
|
||||
// let ix = ore::instruction::initialize(payer.pubkey());
|
||||
// let tx = Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], blockhash);
|
||||
// let res = banks.process_transaction(tx).await;
|
||||
// assert!(res.is_ok());
|
||||
|
||||
// Submit ix
|
||||
let ix = ore::instruction::update_admin(alt_payer.pubkey(), Pubkey::new_unique());
|
||||
let tx = Transaction::new_signed_with_payer(
|
||||
&[ix],
|
||||
Some(&alt_payer.pubkey()),
|
||||
&[&alt_payer],
|
||||
blockhash,
|
||||
);
|
||||
let res = banks.process_transaction(tx).await;
|
||||
assert!(res.is_err());
|
||||
}
|
||||
// // Submit ix
|
||||
// let ix = ore::instruction::update_admin(alt_payer.pubkey(), Pubkey::new_unique());
|
||||
// let tx = Transaction::new_signed_with_payer(
|
||||
// &[ix],
|
||||
// Some(&alt_payer.pubkey()),
|
||||
// &[&alt_payer],
|
||||
// blockhash,
|
||||
// );
|
||||
// let res = banks.process_transaction(tx).await;
|
||||
// assert!(res.is_err());
|
||||
// }
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_update_admin_not_enough_accounts() {
|
||||
// Setup
|
||||
let (mut banks, payer, _, blockhash) = setup_program_test_env().await;
|
||||
// #[tokio::test]
|
||||
// async fn test_update_admin_not_enough_accounts() {
|
||||
// // Setup
|
||||
// let (mut banks, payer, _, blockhash) = setup_program_test_env().await;
|
||||
|
||||
// Submit tx
|
||||
let ix = ore::instruction::initialize(payer.pubkey());
|
||||
let tx = Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], blockhash);
|
||||
let res = banks.process_transaction(tx).await;
|
||||
assert!(res.is_ok());
|
||||
// // Submit tx
|
||||
// let ix = ore::instruction::initialize(payer.pubkey());
|
||||
// let tx = Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], blockhash);
|
||||
// let res = banks.process_transaction(tx).await;
|
||||
// assert!(res.is_ok());
|
||||
|
||||
// Submit ix without enough accounts
|
||||
let mut ix = ore::instruction::update_admin(payer.pubkey(), Pubkey::new_unique());
|
||||
ix.accounts.remove(1);
|
||||
let tx = Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], blockhash);
|
||||
let res = banks.process_transaction(tx).await;
|
||||
assert!(res.is_err());
|
||||
}
|
||||
// // Submit ix without enough accounts
|
||||
// let mut ix = ore::instruction::update_admin(payer.pubkey(), Pubkey::new_unique());
|
||||
// ix.accounts.remove(1);
|
||||
// let tx = Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], blockhash);
|
||||
// let res = banks.process_transaction(tx).await;
|
||||
// assert!(res.is_err());
|
||||
// }
|
||||
|
||||
async fn setup_program_test_env() -> (BanksClient, Keypair, Keypair, Hash) {
|
||||
let mut program_test = ProgramTest::new("ore", ore::ID, processor!(ore::process_instruction));
|
||||
program_test.prefer_bpf(true);
|
||||
// async fn setup_program_test_env() -> (BanksClient, Keypair, Keypair, Hash) {
|
||||
// let mut program_test = ProgramTest::new("ore", ore::ID, processor!(ore::process_instruction));
|
||||
// program_test.prefer_bpf(true);
|
||||
|
||||
// Setup metadata program
|
||||
let data = read_file(&"tests/buffers/metadata_program.bpf");
|
||||
program_test.add_account(
|
||||
mpl_token_metadata::ID,
|
||||
Account {
|
||||
lamports: Rent::default().minimum_balance(data.len()).max(1),
|
||||
data,
|
||||
owner: solana_sdk::bpf_loader::id(),
|
||||
executable: true,
|
||||
rent_epoch: 0,
|
||||
},
|
||||
);
|
||||
// // Setup metadata program
|
||||
// let data = read_file(&"tests/buffers/metadata_program.bpf");
|
||||
// program_test.add_account(
|
||||
// mpl_token_metadata::ID,
|
||||
// Account {
|
||||
// lamports: Rent::default().minimum_balance(data.len()).max(1),
|
||||
// data,
|
||||
// owner: solana_sdk::bpf_loader::id(),
|
||||
// executable: true,
|
||||
// rent_epoch: 0,
|
||||
// },
|
||||
// );
|
||||
|
||||
// Setup alt payer
|
||||
let payer_alt = Keypair::new();
|
||||
program_test.add_account(
|
||||
payer_alt.pubkey(),
|
||||
Account {
|
||||
lamports: LAMPORTS_PER_SOL,
|
||||
data: vec![],
|
||||
owner: system_program::id(),
|
||||
executable: false,
|
||||
rent_epoch: 0,
|
||||
},
|
||||
);
|
||||
// // Setup alt payer
|
||||
// let payer_alt = Keypair::new();
|
||||
// program_test.add_account(
|
||||
// payer_alt.pubkey(),
|
||||
// Account {
|
||||
// lamports: LAMPORTS_PER_SOL,
|
||||
// data: vec![],
|
||||
// owner: system_program::id(),
|
||||
// executable: false,
|
||||
// rent_epoch: 0,
|
||||
// },
|
||||
// );
|
||||
|
||||
let (banks, payer, blockhash) = program_test.start().await;
|
||||
(banks, payer, payer_alt, blockhash)
|
||||
}
|
||||
// let (banks, payer, blockhash) = program_test.start().await;
|
||||
// (banks, payer, payer_alt, blockhash)
|
||||
// }
|
||||
|
||||
@@ -1,125 +1,125 @@
|
||||
use ore::{state::Treasury, utils::AccountDeserialize, TREASURY_ADDRESS};
|
||||
use solana_program::{
|
||||
hash::Hash, keccak::Hash as KeccakHash, native_token::LAMPORTS_PER_SOL, rent::Rent,
|
||||
system_program,
|
||||
};
|
||||
use solana_program_test::{processor, read_file, BanksClient, ProgramTest};
|
||||
use solana_sdk::{
|
||||
account::Account,
|
||||
signature::{Keypair, Signer},
|
||||
transaction::Transaction,
|
||||
};
|
||||
// use ore::{state::Treasury, utils::AccountDeserialize, TREASURY_ADDRESS};
|
||||
// use solana_program::{
|
||||
// hash::Hash, keccak::Hash as KeccakHash, native_token::LAMPORTS_PER_SOL, rent::Rent,
|
||||
// system_program,
|
||||
// };
|
||||
// use solana_program_test::{processor, read_file, BanksClient, ProgramTest};
|
||||
// use solana_sdk::{
|
||||
// account::Account,
|
||||
// signature::{Keypair, Signer},
|
||||
// transaction::Transaction,
|
||||
// };
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_update_difficulty() {
|
||||
// Setup
|
||||
let (mut banks, payer, _, blockhash) = setup_program_test_env().await;
|
||||
// #[tokio::test]
|
||||
// async fn test_update_difficulty() {
|
||||
// // Setup
|
||||
// let (mut banks, payer, _, blockhash) = setup_program_test_env().await;
|
||||
|
||||
// Submit tx
|
||||
let ix = ore::instruction::initialize(payer.pubkey());
|
||||
let tx = Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], blockhash);
|
||||
let res = banks.process_transaction(tx).await;
|
||||
assert!(res.is_ok());
|
||||
// // Submit tx
|
||||
// let ix = ore::instruction::initialize(payer.pubkey());
|
||||
// let tx = Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], blockhash);
|
||||
// let res = banks.process_transaction(tx).await;
|
||||
// assert!(res.is_ok());
|
||||
|
||||
// Get treasury account
|
||||
let treasury_account = banks.get_account(TREASURY_ADDRESS).await.unwrap().unwrap();
|
||||
let treasury = Treasury::try_from_bytes(&treasury_account.data).unwrap();
|
||||
// // Get treasury account
|
||||
// let treasury_account = banks.get_account(TREASURY_ADDRESS).await.unwrap().unwrap();
|
||||
// let treasury = Treasury::try_from_bytes(&treasury_account.data).unwrap();
|
||||
|
||||
// Submit update difficulty ix
|
||||
let new_difficulty = KeccakHash::new_unique();
|
||||
let ix = ore::instruction::update_difficulty(payer.pubkey(), new_difficulty.into());
|
||||
let tx = Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], blockhash);
|
||||
let res = banks.process_transaction(tx).await;
|
||||
assert!(res.is_ok());
|
||||
// // Submit update difficulty ix
|
||||
// let new_difficulty = KeccakHash::new_unique();
|
||||
// let ix = ore::instruction::update_difficulty(payer.pubkey(), new_difficulty.into());
|
||||
// let tx = Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], blockhash);
|
||||
// let res = banks.process_transaction(tx).await;
|
||||
// assert!(res.is_ok());
|
||||
|
||||
// Assert treasury state
|
||||
let treasury_account = banks.get_account(TREASURY_ADDRESS).await.unwrap().unwrap();
|
||||
let treasury_ = Treasury::try_from_bytes(&treasury_account.data).unwrap();
|
||||
assert_eq!(treasury_.bump, treasury.bump);
|
||||
assert_eq!(treasury_.admin, treasury.admin);
|
||||
assert_eq!(treasury_.difficulty, new_difficulty.into());
|
||||
assert_eq!(treasury_.last_reset_at, treasury.last_reset_at);
|
||||
assert_eq!(treasury_.reward_rate, treasury.reward_rate);
|
||||
assert_eq!(
|
||||
treasury_.total_claimed_rewards,
|
||||
treasury.total_claimed_rewards
|
||||
);
|
||||
}
|
||||
// // Assert treasury state
|
||||
// let treasury_account = banks.get_account(TREASURY_ADDRESS).await.unwrap().unwrap();
|
||||
// let treasury_ = Treasury::try_from_bytes(&treasury_account.data).unwrap();
|
||||
// assert_eq!(treasury_.bump, treasury.bump);
|
||||
// assert_eq!(treasury_.admin, treasury.admin);
|
||||
// assert_eq!(treasury_.difficulty, new_difficulty.into());
|
||||
// assert_eq!(treasury_.last_reset_at, treasury.last_reset_at);
|
||||
// assert_eq!(treasury_.reward_rate, treasury.reward_rate);
|
||||
// assert_eq!(
|
||||
// treasury_.total_claimed_rewards,
|
||||
// treasury.total_claimed_rewards
|
||||
// );
|
||||
// }
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_update_difficulty_bad_signer() {
|
||||
// Setup
|
||||
let (mut banks, payer, alt_payer, blockhash) = setup_program_test_env().await;
|
||||
// #[tokio::test]
|
||||
// async fn test_update_difficulty_bad_signer() {
|
||||
// // Setup
|
||||
// let (mut banks, payer, alt_payer, blockhash) = setup_program_test_env().await;
|
||||
|
||||
// Submit tx
|
||||
let ix = ore::instruction::initialize(payer.pubkey());
|
||||
let tx = Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], blockhash);
|
||||
let res = banks.process_transaction(tx).await;
|
||||
assert!(res.is_ok());
|
||||
// // Submit tx
|
||||
// let ix = ore::instruction::initialize(payer.pubkey());
|
||||
// let tx = Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], blockhash);
|
||||
// let res = banks.process_transaction(tx).await;
|
||||
// assert!(res.is_ok());
|
||||
|
||||
// Submit update difficulty ix
|
||||
let new_difficulty = KeccakHash::new_unique();
|
||||
let ix = ore::instruction::update_difficulty(alt_payer.pubkey(), new_difficulty.into());
|
||||
let tx = Transaction::new_signed_with_payer(
|
||||
&[ix],
|
||||
Some(&alt_payer.pubkey()),
|
||||
&[&alt_payer],
|
||||
blockhash,
|
||||
);
|
||||
let res = banks.process_transaction(tx).await;
|
||||
assert!(res.is_err());
|
||||
}
|
||||
// // Submit update difficulty ix
|
||||
// let new_difficulty = KeccakHash::new_unique();
|
||||
// let ix = ore::instruction::update_difficulty(alt_payer.pubkey(), new_difficulty.into());
|
||||
// let tx = Transaction::new_signed_with_payer(
|
||||
// &[ix],
|
||||
// Some(&alt_payer.pubkey()),
|
||||
// &[&alt_payer],
|
||||
// blockhash,
|
||||
// );
|
||||
// let res = banks.process_transaction(tx).await;
|
||||
// assert!(res.is_err());
|
||||
// }
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_update_difficulty_not_enough_accounts() {
|
||||
// Setup
|
||||
let (mut banks, payer, _, blockhash) = setup_program_test_env().await;
|
||||
// #[tokio::test]
|
||||
// async fn test_update_difficulty_not_enough_accounts() {
|
||||
// // Setup
|
||||
// let (mut banks, payer, _, blockhash) = setup_program_test_env().await;
|
||||
|
||||
// Submit tx
|
||||
let ix = ore::instruction::initialize(payer.pubkey());
|
||||
let tx = Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], blockhash);
|
||||
let res = banks.process_transaction(tx).await;
|
||||
assert!(res.is_ok());
|
||||
// // Submit tx
|
||||
// let ix = ore::instruction::initialize(payer.pubkey());
|
||||
// let tx = Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], blockhash);
|
||||
// let res = banks.process_transaction(tx).await;
|
||||
// assert!(res.is_ok());
|
||||
|
||||
// Submit ix without enough accounts
|
||||
let new_difficulty = KeccakHash::new_unique();
|
||||
let mut ix = ore::instruction::update_difficulty(payer.pubkey(), new_difficulty.into());
|
||||
ix.accounts.remove(1);
|
||||
let tx = Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], blockhash);
|
||||
let res = banks.process_transaction(tx).await;
|
||||
assert!(res.is_err());
|
||||
}
|
||||
// // Submit ix without enough accounts
|
||||
// let new_difficulty = KeccakHash::new_unique();
|
||||
// let mut ix = ore::instruction::update_difficulty(payer.pubkey(), new_difficulty.into());
|
||||
// ix.accounts.remove(1);
|
||||
// let tx = Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], blockhash);
|
||||
// let res = banks.process_transaction(tx).await;
|
||||
// assert!(res.is_err());
|
||||
// }
|
||||
|
||||
async fn setup_program_test_env() -> (BanksClient, Keypair, Keypair, Hash) {
|
||||
let mut program_test = ProgramTest::new("ore", ore::ID, processor!(ore::process_instruction));
|
||||
program_test.prefer_bpf(true);
|
||||
// async fn setup_program_test_env() -> (BanksClient, Keypair, Keypair, Hash) {
|
||||
// let mut program_test = ProgramTest::new("ore", ore::ID, processor!(ore::process_instruction));
|
||||
// program_test.prefer_bpf(true);
|
||||
|
||||
// Setup metadata program
|
||||
let data = read_file(&"tests/buffers/metadata_program.bpf");
|
||||
program_test.add_account(
|
||||
mpl_token_metadata::ID,
|
||||
Account {
|
||||
lamports: Rent::default().minimum_balance(data.len()).max(1),
|
||||
data,
|
||||
owner: solana_sdk::bpf_loader::id(),
|
||||
executable: true,
|
||||
rent_epoch: 0,
|
||||
},
|
||||
);
|
||||
// // Setup metadata program
|
||||
// let data = read_file(&"tests/buffers/metadata_program.bpf");
|
||||
// program_test.add_account(
|
||||
// mpl_token_metadata::ID,
|
||||
// Account {
|
||||
// lamports: Rent::default().minimum_balance(data.len()).max(1),
|
||||
// data,
|
||||
// owner: solana_sdk::bpf_loader::id(),
|
||||
// executable: true,
|
||||
// rent_epoch: 0,
|
||||
// },
|
||||
// );
|
||||
|
||||
// Setup alt payer
|
||||
let payer_alt = Keypair::new();
|
||||
program_test.add_account(
|
||||
payer_alt.pubkey(),
|
||||
Account {
|
||||
lamports: LAMPORTS_PER_SOL,
|
||||
data: vec![],
|
||||
owner: system_program::id(),
|
||||
executable: false,
|
||||
rent_epoch: 0,
|
||||
},
|
||||
);
|
||||
// // Setup alt payer
|
||||
// let payer_alt = Keypair::new();
|
||||
// program_test.add_account(
|
||||
// payer_alt.pubkey(),
|
||||
// Account {
|
||||
// lamports: LAMPORTS_PER_SOL,
|
||||
// data: vec![],
|
||||
// owner: system_program::id(),
|
||||
// executable: false,
|
||||
// rent_epoch: 0,
|
||||
// },
|
||||
// );
|
||||
|
||||
let (banks, payer, blockhash) = program_test.start().await;
|
||||
(banks, payer, payer_alt, blockhash)
|
||||
}
|
||||
// let (banks, payer, blockhash) = program_test.start().await;
|
||||
// (banks, payer, payer_alt, blockhash)
|
||||
// }
|
||||
|
||||
Reference in New Issue
Block a user