Skip to content

Commit

Permalink
Clean up and refactor the code
Browse files Browse the repository at this point in the history
  • Loading branch information
Aursen committed Jan 8, 2024
1 parent 0debfbb commit 50fcf17
Show file tree
Hide file tree
Showing 91 changed files with 527 additions and 2,101 deletions.
533 changes: 92 additions & 441 deletions Cargo.lock

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,10 @@ members = ["cli", "cron", "plugin", "programs/*", "relayer", "sdk", "utils"]

[profile.release]
overflow-checks = true
lto = "fat"
codegen-units = 1

[profile.release.build-override]
opt-level = 3
incremental = false
codegen-units = 1
4 changes: 0 additions & 4 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,10 @@ indicatif = "0.16"
reqwest = "0.11.14"
serde = { version = "1.0.136", features = ["derive"] }
serde_json = "1.0.79"
serde_yaml = "0.9.4"
solana-account-decoder = "=1.17.14"
solana-client = "=1.17.14"
solana-clap-utils = "=1.17.14"
solana-cli-config = "=1.17.14"
solana-sdk = "=1.17.14"
spl-associated-token-account = "1.1.1"
spl-memo = "3.0.1"
spl-token = "~3.5.0"
tar = "0.4"
thiserror = "1.0.30"
Expand Down
2 changes: 1 addition & 1 deletion cli/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::process::Command;

fn main() {
let output = Command::new("git")
.args(&["rev-parse", "HEAD"])
.args(["rev-parse", "HEAD"])
.output()
.expect("unable to get git commit hash");
let commit_hash = String::from_utf8(output.stdout).unwrap();
Expand Down
20 changes: 10 additions & 10 deletions cli/src/processor/delegation.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use anchor_lang::{
solana_program::{
instruction::Instruction, system_program, sysvar
},
InstructionData, AccountDeserialize, ToAccountMetas
solana_program::{instruction::Instruction, system_program, sysvar},
AccountDeserialize, InstructionData, ToAccountMetas,
};
use clockwork_network_program::state::{Config, Delegation, Worker};
use spl_associated_token_account::get_associated_token_address;
Expand Down Expand Up @@ -37,11 +35,11 @@ pub fn create(client: &Client, worker_id: u64) -> Result<(), CliError> {
delegation: delegation_pubkey,
delegation_tokens: get_associated_token_address(&delegation_pubkey, &config.mint),
mint: config.mint,
rent: sysvar::rent::ID,
system_program: system_program::ID,
token_program: anchor_spl::token::ID,
worker: worker_pubkey,
}.to_account_metas(Some(false)),
}
.to_account_metas(Some(false)),
data: clockwork_network_program::instruction::DelegationCreate {}.data(),
};
client.send_and_confirm(&[ix], &[client.payer()]).unwrap();
Expand Down Expand Up @@ -76,8 +74,9 @@ pub fn deposit(
config: Config::pubkey(),
delegation: delegation_pubkey,
delegation_tokens: get_associated_token_address(&delegation_pubkey, &config.mint),
token_program: anchor_spl::token::ID,
}.to_account_metas(Some(false)),
token_program: anchor_spl::token::ID,
}
.to_account_metas(Some(false)),
data: clockwork_network_program::instruction::DelegationDeposit { amount }.data(),
};
client.send_and_confirm(&[ix], &[client.payer()]).unwrap();
Expand Down Expand Up @@ -112,8 +111,9 @@ pub fn withdraw(
config: Config::pubkey(),
delegation: delegation_pubkey,
delegation_tokens: get_associated_token_address(&delegation_pubkey, &config.mint),
token_program: anchor_spl::token::ID,
}.to_account_metas(Some(false)),
token_program: anchor_spl::token::ID,
}
.to_account_metas(Some(false)),
data: clockwork_network_program::instruction::DelegationWithdraw { amount }.data(),
};
client.send_and_confirm(&[ix], &[client.payer()]).unwrap();
Expand Down
14 changes: 6 additions & 8 deletions cli/src/processor/worker.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use anchor_lang::{
solana_program::{
instruction::Instruction,
system_program, sysvar,
},
AccountDeserialize, InstructionData, ToAccountMetas
solana_program::{instruction::Instruction, system_program, sysvar},
AccountDeserialize, InstructionData, ToAccountMetas,
};
use anchor_spl::{associated_token, associated_token::get_associated_token_address, token};
use clockwork_network_program::state::{
Expand Down Expand Up @@ -101,13 +98,13 @@ pub fn create(client: &Client, signatory: Keypair, silent: bool) -> Result<(), C
penalty: Penalty::pubkey(worker_pubkey),
mint: config.mint,
registry: Registry::pubkey(),
rent: sysvar::rent::ID,
signatory: signatory.pubkey(),
system_program: system_program::ID,
token_program: token::ID,
worker: worker_pubkey,
worker_tokens: get_associated_token_address(&worker_pubkey, &config.mint),
}.to_account_metas(Some(false)),
}
.to_account_metas(Some(false)),
data: clockwork_network_program::instruction::WorkerCreate {}.data(),
};
client
Expand Down Expand Up @@ -137,7 +134,8 @@ pub fn update(client: &Client, id: u64, signatory: Option<Keypair>) -> Result<()
authority: client.payer_pubkey(),
system_program: system_program::ID,
worker: worker_pubkey,
}.to_account_metas(Some(false)),
}
.to_account_metas(Some(false)),
data: clockwork_network_program::instruction::WorkerUpdate { settings }.data(),
};
client.send_and_confirm(&[ix], &[client.payer()]).unwrap();
Expand Down
2 changes: 0 additions & 2 deletions cron/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,3 @@ name = "clockwork_cron"
[dependencies]
chrono = { version = "0.4.19", default-features = false, features = ["alloc"] }
nom = "~7"
once_cell = "1.5.2"

11 changes: 5 additions & 6 deletions cron/src/time_unit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ where
//println!("ordinals_from_specifier for {} => {:?}", Self::name(), specifier);
match *specifier {
All => Ok(Self::supported_ordinals().clone()),
Point(ordinal) => Ok((&[ordinal]).iter().cloned().collect()),
Point(ordinal) => Ok(OrdinalSet::from([ordinal])),
Range(start, end) => {
match (Self::validate_ordinal(start), Self::validate_ordinal(end)) {
(Ok(start), Ok(end)) if start <= end => Ok((start..end + 1).collect()),
Expand Down Expand Up @@ -207,14 +207,13 @@ where
specifier => Self::ordinals_from_specifier(specifier)?,
};
if *step == 0 {
return Err(ErrorKind::Expression(format!("step cannot be 0")).into());
return Err(ErrorKind::Expression("step cannot be 0".to_string()).into());
}
base_set.into_iter().step_by(*step as usize).collect()
}
RootSpecifier::NamedPoint(ref name) => (&[Self::ordinal_from_name(name)?])
.iter()
.cloned()
.collect::<OrdinalSet>(),
RootSpecifier::NamedPoint(ref name) => {
OrdinalSet::from([Self::ordinal_from_name(name)?])
}
};
Ok(ordinals)
}
Expand Down
10 changes: 0 additions & 10 deletions plugin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,36 +20,26 @@ name = "clockwork_plugin"

[dependencies]
anchor-lang = "0.29.0"
async_once = "0.2.6"
async-trait = "0.1.64"
bincode = "1.3.3"
bs58 = "0.4.0"
bugsnag = "0.2.1"
chrono = { version = "0.4.19", default-features = false, features = ["alloc"] }
clockwork-cron = { path = "../cron", version = "=2.0.18" }
clockwork-network-program = { path = "../programs/network", version = "=2.0.18" }
clockwork-plugin-utils = { path = "utils", version = "=2.0.18" }
clockwork-relayer-api = { path = "../relayer/api", version = "=2.0.18" }
clockwork-thread-program = { package = "clockwork-thread-program", path = "../programs/thread", version = "=2.0.18" }
clockwork-thread-program-v1 = { package = "clockwork-thread-program-v1", path = "../programs/thread/v1", version = "=1.4.4" }
clockwork-webhook-program = { path = "../programs/webhook", version = "=2.0.18" }
clockwork-utils = { path = "../utils", version = "=2.0.18" }
lazy_static = "1.4.0"
log = "0.4"
prost = "0.10.0"
pyth-sdk-solana = "0.9.0"
reqwest = "0.11.11"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
solana-account-decoder = "=1.17.14"
solana-client = "=1.17.14"
solana-quic-client = "=1.17.14"
solana-geyser-plugin-interface = "=1.17.14"
solana-logger = "=1.17.14"
solana-program = "=1.17.14"
solana-sdk = "=1.17.14"
solana-transaction-status = "=1.17.14"
thiserror = "1.0.30"
tokio = "1.18.4"
futures = "0.3.26"
static-pubkey = "1.0.3"
Expand Down
2 changes: 1 addition & 1 deletion plugin/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn main() {
println!("cargo:rustc-env=RUSTC_VERSION={:#?}", rustc_v,);

let output = Command::new("git")
.args(&["rev-parse", "HEAD"])
.args(["rev-parse", "HEAD"])
.output()
.expect("unable to get git commit hash");
let commit_hash = String::from_utf8(output.stdout).unwrap();
Expand Down
25 changes: 1 addition & 24 deletions plugin/src/builders/thread_exec.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::sync::Arc;

use anchor_lang::{InstructionData, ToAccountMetas};
use clockwork_thread_program::state::{VersionedThread, Trigger};
use clockwork_network_program::state::Worker;
use clockwork_thread_program::state::{Trigger, VersionedThread};
use clockwork_utils::thread::PAYER_PUBKEY;
use log::info;
use solana_account_decoder::UiAccountEncoding;
Expand Down Expand Up @@ -222,16 +222,6 @@ fn build_kickoff_ix(
// Build the instruction.
let mut kickoff_ix = match thread {
VersionedThread::V1(_) => Instruction {
program_id: clockwork_thread_program_v1::ID,
accounts: clockwork_thread_program_v1::accounts::ThreadKickoff {
signatory: signatory_pubkey,
thread: thread_pubkey,
worker: worker_pubkey,
}
.to_account_metas(Some(false)),
data: clockwork_thread_program_v1::instruction::ThreadKickoff {}.data(),
},
VersionedThread::V2(_) => Instruction {
program_id: clockwork_thread_program::ID,
accounts: clockwork_thread_program::accounts::ThreadKickoff {
signatory: signatory_pubkey,
Expand Down Expand Up @@ -278,19 +268,6 @@ fn build_exec_ix(
// Build the instruction.
let mut exec_ix = match thread {
VersionedThread::V1(_) => Instruction {
program_id: clockwork_thread_program_v1::ID,
accounts: clockwork_thread_program_v1::accounts::ThreadExec {
fee: clockwork_network_program::state::Fee::pubkey(worker_pubkey),
penalty: clockwork_network_program::state::Penalty::pubkey(worker_pubkey),
pool: clockwork_network_program::state::Pool::pubkey(0),
signatory: signatory_pubkey,
thread: thread_pubkey,
worker: worker_pubkey,
}
.to_account_metas(Some(true)),
data: clockwork_thread_program_v1::instruction::ThreadExec {}.data(),
},
VersionedThread::V2(_) => Instruction {
program_id: clockwork_thread_program::ID,
accounts: clockwork_thread_program::accounts::ThreadExec {
fee: clockwork_network_program::state::Fee::pubkey(worker_pubkey),
Expand Down
27 changes: 5 additions & 22 deletions plugin/src/events.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use anchor_lang::{prelude::AccountInfo, AccountDeserialize, Discriminator};
use bincode::deserialize;
use clockwork_thread_program::state::{Thread as ThreadV2, VersionedThread};
use clockwork_thread_program_v1::state::Thread as ThreadV1;
use clockwork_thread_program::state::{Thread, VersionedThread};
use clockwork_webhook_program::state::Webhook;
use pyth_sdk_solana::{load_price_feed_from_account_info, PriceFeed};
use solana_geyser_plugin_interface::geyser_plugin_interface::{
Expand All @@ -10,7 +9,7 @@ use solana_geyser_plugin_interface::geyser_plugin_interface::{
use solana_program::{clock::Clock, pubkey::Pubkey, sysvar};
use static_pubkey::static_pubkey;

static PYTH_ORACLE_PROGRAM_ID_MAINNET: Pubkey =
static PYTH_ORACLE_PROGRAM_ID_MAINNET: Pubkey =
static_pubkey!("FsJ3A3u2vn5cTVofAjvy6y5kwABJAqYWpe4975bi2epH");
static PYTH_ORACLE_PROGRAM_ID_DEVNET: Pubkey =
static_pubkey!("gSbePebfvPy7tRqimPoVecS2UsBvYv46ynrzWocc92s");
Expand Down Expand Up @@ -41,29 +40,13 @@ impl TryFrom<&mut ReplicaAccountInfo<'_>> for AccountUpdateEvent {
});
}

// If the account belongs to the thread v1 program, parse it.
if owner_pubkey.eq(&clockwork_thread_program_v1::ID) && account_info.data.len() > 8 {
let d = &account_info.data[..8];
if d.eq(&ThreadV1::discriminator()) {
return Ok(AccountUpdateEvent::Thread {
thread: VersionedThread::V1(
ThreadV1::try_deserialize(&mut account_info.data).map_err(|_| {
GeyserPluginError::AccountsUpdateError {
msg: "Failed to parse Clockwork thread v1 account".into(),
}
})?,
),
});
}
}

// If the account belongs to the thread v2 program, parse it.
if owner_pubkey.eq(&clockwork_thread_program::ID) && account_info.data.len() > 8 {
let d = &account_info.data[..8];
if d.eq(&ThreadV2::discriminator()) {
if d.eq(&Thread::discriminator()) {
return Ok(AccountUpdateEvent::Thread {
thread: VersionedThread::V2(
ThreadV2::try_deserialize(&mut account_info.data).map_err(|_| {
thread: VersionedThread::V1(
Thread::try_deserialize(&mut account_info.data).map_err(|_| {
GeyserPluginError::AccountsUpdateError {
msg: "Failed to parse Clockwork thread v2 account".into(),
}
Expand Down
2 changes: 0 additions & 2 deletions programs/network/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,3 @@ default = []
anchor-lang = "0.29.0"
anchor-spl = { features = ["mint", "token"], version = "0.29.0" }
clockwork-utils = { path = "../../utils", version = "=2.0.18" }
winnow = "=0.4.1"
toml_datetime = "=0.6.1"
19 changes: 19 additions & 0 deletions programs/network/src/constants.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use anchor_lang::prelude::*;

#[constant]
pub const SEED_CONFIG: &[u8] = b"config";

#[constant]
pub const SEED_DELEGATION: &[u8] = b"delegation";

#[constant]
pub const SEED_FEE: &[u8] = b"fee";

#[constant]
pub const SEED_EPOCH: &[u8] = b"epoch";

#[constant]
pub const SEED_PENALTY: &[u8] = b"penalty";

#[constant]
pub const SEED_SNAPSHOT_ENTRY: &[u8] = b"snapshot_entry";
3 changes: 1 addition & 2 deletions programs/network/src/instructions/config_update.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use {crate::state::*, anchor_lang::prelude::*};
use {crate::{constants::*, state::*}, anchor_lang::prelude::*};

#[derive(Accounts)]
#[instruction(settings: ConfigSettings)]
pub struct ConfigUpdate<'info> {
#[account(mut)]
pub admin: Signer<'info>,

#[account(
Expand Down
8 changes: 5 additions & 3 deletions programs/network/src/instructions/delegation_claim.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use {crate::state::*, anchor_lang::prelude::*};
use {
crate::{constants::*, state::*},
anchor_lang::prelude::*,
};

#[derive(Accounts)]
#[instruction(amount: u64)]
pub struct DelegationClaim<'info> {
#[account()]
pub authority: Signer<'info>,

#[account(mut)]
Expand All @@ -28,7 +30,7 @@ pub fn handler(ctx: Context<DelegationClaim>, amount: u64) -> Result<()> {
let delegation = &mut ctx.accounts.delegation;

// Decrement the delegation's claimable balance.
delegation.yield_balance = delegation.yield_balance.checked_sub(amount).unwrap();
delegation.yield_balance -= amount;

// Transfer commission to the worker.
delegation.sub_lamports(amount)?;
Expand Down
Loading

0 comments on commit 50fcf17

Please sign in to comment.