Struct clockwork_network_program::state::Snapshot
source · Expand description
Snapshot
Fields§
§id: u64
§total_frames: u64
§total_stake: u64
Implementations§
source§impl Snapshot
impl Snapshot
sourcepub fn pubkey(id: u64) -> Pubkey
pub fn pubkey(id: u64) -> Pubkey
Examples found in repository?
More examples
src/instructions/registry_epoch_cutover.rs (line 39)
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
pub fn handler(ctx: Context<RegistryEpochCutover>) -> Result<ThreadResponse> {
// Get accounts.
let config = &ctx.accounts.config;
let registry = &mut ctx.accounts.registry;
let thread = &ctx.accounts.thread;
// Move the current epoch forward.
registry.current_epoch = registry.current_epoch.checked_add(1).unwrap();
registry.locked = false;
// Build next instruction for the thread.
// For cost-efficiency, close the prior snapshot accounts and return the lamports to the epoch thread.
let next_instruction = Some(InstructionData {
program_id: crate::ID,
accounts: vec![
AccountMetaData::new_readonly(config.key(), false),
AccountMetaData::new_readonly(registry.key(), false),
AccountMetaData::new(
Snapshot::pubkey(registry.current_epoch.checked_sub(1).unwrap()),
false,
),
AccountMetaData::new(thread.key(), true),
],
data: anchor_sighash("snapshot_delete").to_vec(),
});
Ok(ThreadResponse {
next_instruction,
..ThreadResponse::default()
})
}
src/instructions/registry_epoch_kickoff.rs (line 64)
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
pub fn handler(ctx: Context<RegistryEpochKickoff>) -> Result<ThreadResponse> {
// Get accounts.
let config = &ctx.accounts.config;
let registry = &mut ctx.accounts.registry;
let snapshot = &ctx.accounts.snapshot;
let thread = &ctx.accounts.thread;
// Lock the registry
registry.locked = true;
// Setup the next kickoff instruction to use the next snapshot.
let kickoff_instruction = Some(InstructionData {
program_id: crate::ID,
accounts: vec![
AccountMetaData::new_readonly(config.key(), false),
AccountMetaData::new(registry.key(), false),
AccountMetaData::new_readonly(
Snapshot::pubkey(snapshot.id.checked_add(1).unwrap()),
false,
),
AccountMetaData::new_readonly(thread.key(), true),
],
data: anchor_sighash("registry_epoch_kickoff").to_vec(),
});
// Build the next instruction for thread.
let next_instruction = if snapshot.total_frames.gt(&0) {
// The current snapshot has frames. Distribute fees collected by workers.
Some(InstructionData {
program_id: crate::ID,
accounts: vec![
AccountMetaData::new_readonly(config.key(), false),
AccountMetaData::new(Fee::pubkey(Worker::pubkey(0)), false),
AccountMetaData::new_readonly(registry.key(), false),
AccountMetaData::new_readonly(snapshot.key(), false),
AccountMetaData::new_readonly(SnapshotFrame::pubkey(snapshot.key(), 0), false),
AccountMetaData::new_readonly(thread.key(), true),
AccountMetaData::new(Worker::pubkey(0), false),
],
data: anchor_sighash("worker_fees_distribute").to_vec(),
})
} else if registry.total_workers.gt(&0) {
// The registry has workers. Begin delegating stakes to workers.
Some(InstructionData {
program_id: crate::ID,
accounts: vec![
AccountMetaData::new_readonly(config.key(), false),
AccountMetaData::new_readonly(registry.key(), false),
AccountMetaData::new_readonly(thread.key(), true),
AccountMetaData::new_readonly(Worker::pubkey(0), false),
],
data: anchor_sighash("worker_delegations_stake").to_vec(),
})
} else {
// Cutover to the next epoch.
Some(InstructionData {
program_id: crate::ID,
accounts: vec![
AccountMetaData::new_readonly(config.key(), false),
AccountMetaData::new(registry.key(), false),
AccountMetaData::new_readonly(thread.key(), true),
],
data: anchor_sighash("registry_epoch_cutover").to_vec(),
})
};
Ok(ThreadResponse {
kickoff_instruction,
next_instruction,
})
}
src/instructions/worker_delegations_stake.rs (line 86)
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
pub fn handler(ctx: Context<WorkerStakeDelegations>) -> Result<ThreadResponse> {
// Get accounts.
let config = &ctx.accounts.config;
let registry = &ctx.accounts.registry;
let thread = &ctx.accounts.thread;
let worker = &ctx.accounts.worker;
// Build the next instruction for the thread.
let next_instruction = if worker.total_delegations.gt(&0) {
// This worker has delegations. Stake their deposits.
let delegation_pubkey = Delegation::pubkey(worker.key(), 0);
Some(InstructionData {
program_id: crate::ID,
accounts: vec![
AccountMetaData::new_readonly(config.key(), false),
AccountMetaData::new(delegation_pubkey, false),
AccountMetaData::new(
get_associated_token_address(&delegation_pubkey, &config.mint),
false,
),
AccountMetaData::new_readonly(registry.key(), false),
AccountMetaData::new_readonly(thread.key(), true),
AccountMetaData::new_readonly(anchor_spl::token::ID, false),
AccountMetaData::new_readonly(worker.key(), false),
AccountMetaData::new(
get_associated_token_address(&worker.key(), &config.mint),
false,
),
],
data: anchor_sighash("delegation_stake").to_vec(),
})
} else if worker
.id
.checked_add(1)
.unwrap()
.lt(®istry.total_workers)
{
// This worker has no delegations. Move on to the next worker.
Some(InstructionData {
program_id: crate::ID,
accounts: vec![
AccountMetaData::new_readonly(config.key(), false),
AccountMetaData::new_readonly(registry.key(), false),
AccountMetaData::new_readonly(thread.key(), true),
AccountMetaData::new_readonly(
Worker::pubkey(worker.id.checked_add(1).unwrap()),
false,
),
],
data: anchor_sighash("worker_delegations_stake").to_vec(),
})
} else {
// This worker has no delegations and it is the last worker. Move on to the snapshot job!
Some(InstructionData {
program_id: crate::ID,
accounts: vec![
AccountMetaData::new_readonly(config.key(), false),
AccountMetaData::new(clockwork_utils::PAYER_PUBKEY, true),
AccountMetaData::new_readonly(registry.key(), false),
AccountMetaData::new(
Snapshot::pubkey(registry.current_epoch.checked_add(1).unwrap()),
false,
),
AccountMetaData::new_readonly(system_program::ID, false),
AccountMetaData::new_readonly(thread.key(), true),
],
data: anchor_sighash("snapshot_create").to_vec(),
})
};
Ok(ThreadResponse {
next_instruction,
..ThreadResponse::default()
})
}
src/instructions/delegation_stake.rs (line 147)
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
pub fn handler(ctx: Context<DelegationStake>) -> Result<ThreadResponse> {
// Get accounts.
let config = &ctx.accounts.config;
let delegation = &mut ctx.accounts.delegation;
let delegation_stake = &mut ctx.accounts.delegation_stake;
let registry = &ctx.accounts.registry;
let thread = &ctx.accounts.thread;
let token_program = &ctx.accounts.token_program;
let worker = &ctx.accounts.worker;
let worker_stake = &ctx.accounts.worker_stake;
// Transfer tokens from delegation to worker account.
let amount = delegation_stake.amount;
let bump = *ctx.bumps.get("delegation").unwrap();
transfer(
CpiContext::new_with_signer(
token_program.to_account_info(),
Transfer {
from: delegation_stake.to_account_info(),
to: worker_stake.to_account_info(),
authority: delegation.to_account_info(),
},
&[&[
SEED_DELEGATION,
delegation.worker.as_ref(),
delegation.id.to_be_bytes().as_ref(),
&[bump],
]],
),
amount,
)?;
// Update the delegation's stake amount.
delegation.stake_amount = delegation.stake_amount.checked_add(amount).unwrap();
// Build next instruction for the thread.
let next_instruction = if delegation
.id
.checked_add(1)
.unwrap()
.lt(&worker.total_delegations)
{
// This worker has more delegations, continue locking their stake.
let next_delegation_pubkey =
Delegation::pubkey(worker.key(), delegation.id.checked_add(1).unwrap());
Some(InstructionData {
program_id: crate::ID,
accounts: vec![
AccountMetaData::new_readonly(config.key(), false),
AccountMetaData::new(next_delegation_pubkey, false),
AccountMetaData::new(
get_associated_token_address(&next_delegation_pubkey, &config.mint),
false,
),
AccountMetaData::new_readonly(registry.key(), false),
AccountMetaData::new_readonly(thread.key(), true),
AccountMetaData::new_readonly(token_program.key(), false),
AccountMetaData::new_readonly(worker.key(), false),
AccountMetaData::new(worker_stake.key(), false),
],
data: anchor_sighash("delegation_stake").to_vec(),
})
} else if worker
.id
.checked_add(1)
.unwrap()
.lt(®istry.total_workers)
{
// This worker has no more delegations, move on to the next worker.
Some(InstructionData {
program_id: crate::ID,
accounts: vec![
AccountMetaData::new_readonly(config.key(), false),
AccountMetaData::new_readonly(registry.key(), false),
AccountMetaData::new_readonly(thread.key(), true),
AccountMetaData::new_readonly(
Worker::pubkey(worker.id.checked_add(1).unwrap()),
false,
),
],
data: anchor_sighash("worker_delegations_stake").to_vec(),
})
} else {
// This worker has no more delegations and it is the last worker. Start the snapshot!
Some(InstructionData {
program_id: crate::ID,
accounts: vec![
AccountMetaData::new_readonly(config.key(), false),
AccountMetaData::new(clockwork_utils::PAYER_PUBKEY, true),
AccountMetaData::new_readonly(registry.key(), false),
AccountMetaData::new(
Snapshot::pubkey(registry.current_epoch.checked_add(1).unwrap()),
false,
),
AccountMetaData::new_readonly(system_program::ID, false),
AccountMetaData::new_readonly(thread.key(), true),
],
data: anchor_sighash("snapshot_create").to_vec(),
})
};
Ok(ThreadResponse {
next_instruction,
..ThreadResponse::default()
})
}
Trait Implementations§
source§impl AccountDeserialize for Snapshot
impl AccountDeserialize for Snapshot
source§fn try_deserialize(buf: &mut &[u8]) -> Result<Self>
fn try_deserialize(buf: &mut &[u8]) -> Result<Self>
Deserializes previously initialized account data. Should fail for all
uninitialized accounts, where the bytes are zeroed. Implementations
should be unique to a particular account type so that one can never
successfully deserialize the data of one account type into another.
For example, if the SPL token program were to implement this trait,
it should be impossible to deserialize a
Mint
account into a token
Account
. Read more