Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

thread: Add domain field to the thread state #32

Merged
merged 2 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ pub enum CliCommand {
// Thread commands
ThreadCreate {
id: String,
domain: String,
kickoff_instruction: SerializableInstruction,
trigger: Trigger,
},
Expand Down
1 change: 1 addition & 0 deletions cli/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ fn parse_thread_command(matches: &ArgMatches) -> Result<CliCommand, CliError> {
Some(("crate-info", _)) => Ok(CliCommand::ThreadCrateInfo {}),
Some(("create", matches)) => Ok(CliCommand::ThreadCreate {
id: parse_string("id", matches)?,
domain: parse_string("domain", matches)?,
kickoff_instruction: parse_instruction_file("kickoff_instruction", matches)?,
trigger: parse_trigger(matches)?,
}),
Expand Down
6 changes: 4 additions & 2 deletions cli/src/processor/localnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ fn register_worker(client: &Client, config: &CliConfig) -> Result<()> {
fn create_threads(client: &Client, mint_pubkey: Pubkey) -> Result<()> {
// Create epoch thread.
let epoch_thread_id = "sablier.network.epoch";
let epoch_thread_pubkey = Thread::pubkey(client.payer_pubkey(), epoch_thread_id.into());
let epoch_thread_pubkey = Thread::pubkey(client.payer_pubkey(), epoch_thread_id.into(), None);
let ix_a1 = Instruction {
program_id: sablier_network_program::ID,
accounts: sablier_network_program::accounts::DistributeFeesJob {
Expand Down Expand Up @@ -237,6 +237,7 @@ fn create_threads(client: &Client, mint_pubkey: Pubkey) -> Result<()> {
data: sablier_thread_program::instruction::ThreadCreate {
amount: LAMPORTS_PER_SOL,
id: epoch_thread_id.into(),
domain: None,
instructions: vec![
ix_a1.into(),
ix_a2.into(),
Expand All @@ -255,7 +256,7 @@ fn create_threads(client: &Client, mint_pubkey: Pubkey) -> Result<()> {

// Create hasher thread.
let hasher_thread_id = "sablier.network.hasher";
let hasher_thread_pubkey = Thread::pubkey(client.payer_pubkey(), hasher_thread_id.into());
let hasher_thread_pubkey = Thread::pubkey(client.payer_pubkey(), hasher_thread_id.into(), None);
let registry_hash_ix = Instruction {
program_id: sablier_network_program::ID,
accounts: sablier_network_program::accounts::RegistryNonceHash {
Expand All @@ -278,6 +279,7 @@ fn create_threads(client: &Client, mint_pubkey: Pubkey) -> Result<()> {
data: sablier_thread_program::instruction::ThreadCreate {
amount: LAMPORTS_PER_SOL,
id: hasher_thread_id.into(),
domain: None,
instructions: vec![registry_hash_ix.into()],
trigger: Trigger::Cron {
schedule: "*/15 * * * * * *".into(),
Expand Down
3 changes: 2 additions & 1 deletion cli/src/processor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,10 @@ pub fn process(matches: &ArgMatches) -> Result<(), CliError> {
CliCommand::ThreadCrateInfo {} => thread::crate_info(&client),
CliCommand::ThreadCreate {
id,
domain,
kickoff_instruction,
trigger,
} => thread::create(&client, id, vec![kickoff_instruction], trigger),
} => thread::create(&client, id, domain, vec![kickoff_instruction], trigger),
CliCommand::ThreadDelete { id } => thread::delete(&client, id),
CliCommand::ThreadPause { id } => thread::pause(&client, id),
CliCommand::ThreadResume { id } => thread::resume(&client, id),
Expand Down
20 changes: 13 additions & 7 deletions cli/src/processor/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,15 @@ pub fn crate_info(client: &Client) -> Result<(), CliError> {
pub fn create(
client: &Client,
id: String,
domain: String,
instructions: Vec<SerializableInstruction>,
trigger: Trigger,
) -> Result<(), CliError> {
let thread_pubkey = Thread::pubkey(client.payer_pubkey(), id.clone().into_bytes());
let thread_pubkey = Thread::pubkey(
client.payer_pubkey(),
id.clone().into_bytes(),
Some(domain.into_bytes()),
);
let ix = Instruction {
program_id: sablier_thread_program::ID,
accounts: sablier_thread_program::accounts::ThreadCreate {
Expand All @@ -43,6 +48,7 @@ pub fn create(
data: sablier_thread_program::instruction::ThreadCreate {
amount: 0,
id: id.into_bytes(),
domain: None,
instructions,
trigger,
}
Expand All @@ -54,7 +60,7 @@ pub fn create(
}

pub fn delete(client: &Client, id: String) -> Result<(), CliError> {
let thread_pubkey = Thread::pubkey(client.payer_pubkey(), id.into_bytes());
let thread_pubkey = Thread::pubkey(client.payer_pubkey(), id.into_bytes(), None);
let ix = Instruction {
program_id: sablier_thread_program::ID,
accounts: sablier_thread_program::accounts::ThreadDelete {
Expand All @@ -77,7 +83,7 @@ pub fn get(client: &Client, address: Pubkey) -> Result<(), CliError> {
}

pub fn pause(client: &Client, id: String) -> Result<(), CliError> {
let thread_pubkey = Thread::pubkey(client.payer_pubkey(), id.into_bytes());
let thread_pubkey = Thread::pubkey(client.payer_pubkey(), id.into_bytes(), None);
let ix = Instruction {
program_id: sablier_thread_program::ID,
accounts: sablier_thread_program::accounts::ThreadPause {
Expand All @@ -93,7 +99,7 @@ pub fn pause(client: &Client, id: String) -> Result<(), CliError> {
}

pub fn resume(client: &Client, id: String) -> Result<(), CliError> {
let thread_pubkey = Thread::pubkey(client.payer_pubkey(), id.into_bytes());
let thread_pubkey = Thread::pubkey(client.payer_pubkey(), id.into_bytes(), None);
let ix = Instruction {
program_id: sablier_thread_program::ID,
accounts: sablier_thread_program::accounts::ThreadResume {
Expand All @@ -109,7 +115,7 @@ pub fn resume(client: &Client, id: String) -> Result<(), CliError> {
}

pub fn reset(client: &Client, id: String) -> Result<(), CliError> {
let thread_pubkey = Thread::pubkey(client.payer_pubkey(), id.into_bytes());
let thread_pubkey = Thread::pubkey(client.payer_pubkey(), id.into_bytes(), None);
let ix = Instruction {
program_id: sablier_thread_program::ID,
accounts: sablier_thread_program::accounts::ThreadReset {
Expand All @@ -130,7 +136,7 @@ pub fn update(
rate_limit: Option<u64>,
schedule: Option<String>,
) -> Result<(), CliError> {
let thread_pubkey = Thread::pubkey(client.payer_pubkey(), id.into_bytes());
let thread_pubkey = Thread::pubkey(client.payer_pubkey(), id.into_bytes(), None);
let trigger = schedule.map(|schedule| Trigger::Cron {
schedule,
skippable: true,
Expand Down Expand Up @@ -162,6 +168,6 @@ pub fn parse_pubkey_from_id_or_address(
id: Option<String>,
address: Option<Pubkey>,
) -> Result<Pubkey, CliError> {
let address_from_id = id.map(|str| Thread::pubkey(authority, str.into()));
let address_from_id = id.map(|str| Thread::pubkey(authority, str.into(), None));
address.or(address_from_id).ok_or(CliError::InvalidAddress)
}
5 changes: 4 additions & 1 deletion programs/thread/src/instructions/thread_create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{constants::*, state::*};

/// Accounts required by the `thread_create` instruction.
#[derive(Accounts)]
#[instruction(amount: u64, id: Vec<u8>, instructions: Vec<SerializableInstruction>, trigger: Trigger)]
#[instruction(amount: u64, id: Vec<u8>, domain: Option<Vec<u8>>, instructions: Vec<SerializableInstruction>, trigger: Trigger)]
pub struct ThreadCreate<'info> {
/// The authority (owner) of the thread.
pub authority: Signer<'info>,
Expand All @@ -29,6 +29,7 @@ pub struct ThreadCreate<'info> {
SEED_THREAD,
authority.key().as_ref(),
id.as_slice(),
domain.as_ref().unwrap_or(&Vec::new()).as_slice()
],
bump,
payer= payer,
Expand All @@ -48,6 +49,7 @@ pub fn handler(
ctx: Context<ThreadCreate>,
amount: u64,
id: Vec<u8>,
domain: Option<Vec<u8>>,
instructions: Vec<SerializableInstruction>,
trigger: Trigger,
) -> Result<()> {
Expand All @@ -65,6 +67,7 @@ pub fn handler(
thread.exec_context = None;
thread.fee = THREAD_MINIMUM_FEE;
thread.id = id;
thread.domain = domain;
thread.instructions = instructions;
thread.name = String::new();
thread.next_instruction = None;
Expand Down
1 change: 1 addition & 0 deletions programs/thread/src/instructions/thread_delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub struct ThreadDelete<'info> {
SEED_THREAD,
thread.authority.as_ref(),
thread.id.as_slice(),
thread.domain.as_ref().unwrap_or(&Vec::new()).as_slice()
],
bump = thread.bump,
)]
Expand Down
2 changes: 2 additions & 0 deletions programs/thread/src/instructions/thread_exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ pub struct ThreadExec<'info> {
SEED_THREAD,
thread.authority.as_ref(),
thread.id.as_slice(),
thread.domain.as_ref().unwrap_or(&Vec::new()).as_slice()
],
bump = thread.bump,
constraint = !thread.paused @ SablierError::ThreadPaused,
Expand Down Expand Up @@ -93,6 +94,7 @@ pub fn handler(ctx: Context<ThreadExec>) -> Result<()> {
SEED_THREAD,
thread.authority.as_ref(),
thread.id.as_slice(),
thread.domain.as_ref().unwrap_or(&Vec::new()).as_slice(),
&[thread.bump],
]],
)?;
Expand Down
1 change: 1 addition & 0 deletions programs/thread/src/instructions/thread_instruction_add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub struct ThreadInstructionAdd<'info> {
SEED_THREAD,
thread.authority.as_ref(),
thread.id.as_slice(),
thread.domain.as_ref().unwrap_or(&Vec::new()).as_slice()
],
bump = thread.bump,
has_one = authority
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub struct ThreadInstructionRemove<'info> {
SEED_THREAD,
thread.authority.as_ref(),
thread.id.as_slice(),
thread.domain.as_ref().unwrap_or(&Vec::new()).as_slice()
],
bump = thread.bump,
has_one = authority
Expand Down
8 changes: 7 additions & 1 deletion programs/thread/src/instructions/thread_kickoff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ pub struct ThreadKickoff<'info> {
SEED_THREAD,
thread.authority.as_ref(),
thread.id.as_slice(),
thread.domain.as_ref().unwrap_or(&Vec::new()).as_slice()
],
bump = thread.bump,
constraint = !thread.paused @ SablierError::ThreadPaused,
constraint = thread.next_instruction.is_none() @ SablierError::ThreadBusy,
)]
pub thread: Box<Account<'info, Thread>>,
pub thread: Account<'info, Thread>,

/// The worker.
#[account(address = worker.pubkey())]
Expand Down Expand Up @@ -117,6 +118,11 @@ pub fn handler(ctx: Context<ThreadKickoff>) -> Result<()> {
// Verify the current timestamp is greater than or equal to the threshold timestamp.
let threshold_timestamp = next_timestamp(reference_timestamp, schedule.clone())
.ok_or(SablierError::TriggerConditionFailed)?;
msg!(
"Threshold timestamp: {}, clock timestamp: {}",
threshold_timestamp,
clock.unix_timestamp
);
require!(
clock.unix_timestamp.ge(&threshold_timestamp),
SablierError::TriggerConditionFailed
Expand Down
1 change: 1 addition & 0 deletions programs/thread/src/instructions/thread_pause.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub struct ThreadPause<'info> {
SEED_THREAD,
thread.authority.as_ref(),
thread.id.as_slice(),
thread.domain.as_ref().unwrap_or(&Vec::new()).as_slice()
],
bump = thread.bump,
has_one = authority
Expand Down
1 change: 1 addition & 0 deletions programs/thread/src/instructions/thread_reset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub struct ThreadReset<'info> {
SEED_THREAD,
thread.authority.as_ref(),
thread.id.as_slice(),
thread.domain.as_ref().unwrap_or(&Vec::new()).as_slice()
],
bump = thread.bump,
has_one = authority
Expand Down
1 change: 1 addition & 0 deletions programs/thread/src/instructions/thread_resume.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub struct ThreadResume<'info> {
SEED_THREAD,
thread.authority.as_ref(),
thread.id.as_slice(),
thread.domain.as_ref().unwrap_or(&Vec::new()).as_slice()
],
bump = thread.bump,
has_one = authority
Expand Down
1 change: 1 addition & 0 deletions programs/thread/src/instructions/thread_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub struct ThreadUpdate<'info> {
SEED_THREAD,
thread.authority.as_ref(),
thread.id.as_slice(),
thread.domain.as_ref().unwrap_or(&Vec::new()).as_slice()
],
bump = thread.bump,
has_one = authority,
Expand Down
1 change: 1 addition & 0 deletions programs/thread/src/instructions/thread_withdraw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub struct ThreadWithdraw<'info> {
SEED_THREAD,
thread.authority.as_ref(),
thread.id.as_slice(),
thread.domain.as_ref().unwrap_or(&Vec::new()).as_slice()
],
bump = thread.bump,
has_one = authority,
Expand Down
3 changes: 2 additions & 1 deletion programs/thread/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,11 @@ pub mod thread_program {
ctx: Context<ThreadCreate>,
amount: u64,
id: Vec<u8>,
domain: Option<Vec<u8>>,
instructions: Vec<SerializableInstruction>,
trigger: Trigger,
) -> Result<()> {
thread_create::handler(ctx, amount, id, instructions, trigger)
thread_create::handler(ctx, amount, id, domain, instructions, trigger)
}

/// Closes an existing thread account and returns the lamports to the owner.
Expand Down
12 changes: 9 additions & 3 deletions programs/thread/src/state/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub struct Thread {
pub bump: u8,
/// The cluster clock at the moment the thread was created.
pub created_at: ClockData,
pub domain: Option<Vec<u8>>,
/// The context of the thread's current execution state.
pub exec_context: Option<ExecContext>,
/// The number of lamports to payout to workers per execution.
Expand All @@ -41,9 +42,14 @@ pub struct Thread {

impl Thread {
/// Derive the pubkey of a thread account.
pub fn pubkey(authority: Pubkey, id: Vec<u8>) -> Pubkey {
pub fn pubkey(authority: Pubkey, id: Vec<u8>, domain: Option<Vec<u8>>) -> Pubkey {
Pubkey::find_program_address(
&[SEED_THREAD, authority.as_ref(), id.as_slice()],
&[
SEED_THREAD,
authority.as_ref(),
id.as_slice(),
domain.unwrap_or_default().as_slice(),
],
&crate::ID,
)
.0
Expand All @@ -69,7 +75,7 @@ pub trait ThreadAccount {

impl ThreadAccount for Account<'_, Thread> {
fn pubkey(&self) -> Pubkey {
Thread::pubkey(self.authority, self.id.clone())
Thread::pubkey(self.authority, self.id.clone(), self.domain.clone())
}

fn realloc_account(&mut self) -> Result<()> {
Expand Down
8 changes: 7 additions & 1 deletion programs/thread/src/state/versioned_thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ impl VersionedThread {
}
}

pub fn domain(&self) -> Option<Vec<u8>> {
match self {
Self::V1(t) => t.domain.clone(),
}
}

pub fn next_instruction(&self) -> Option<SerializableInstruction> {
match self {
Self::V1(t) => t.next_instruction.clone(),
Expand All @@ -51,7 +57,7 @@ impl VersionedThread {

pub fn pubkey(&self) -> Pubkey {
match self {
Self::V1(_) => Thread::pubkey(self.authority(), self.id()),
Self::V1(_) => Thread::pubkey(self.authority(), self.id(), self.domain()),
}
}

Expand Down
3 changes: 2 additions & 1 deletion sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ pub mod cpi {
ctx: CpiContext<'_, '_, '_, 'info, ThreadCreate<'info>>,
amount: u64,
id: Vec<u8>,
domain: Option<Vec<u8>>,
instructions: Vec<crate::state::SerializableInstruction>,
trigger: crate::state::Trigger,
) -> Result<()> {
sablier_thread_program::cpi::thread_create(ctx, amount, id, instructions, trigger)
sablier_thread_program::cpi::thread_create(ctx, amount, id, domain, instructions, trigger)
}

pub fn thread_delete<'info>(
Expand Down