Struct clockwork_network_program::state::SnapshotEntry
source · pub struct SnapshotEntry {
pub delegation: Pubkey,
pub id: u64,
pub snapshot_frame: Pubkey,
pub stake_amount: u64,
}
Expand description
- SnapshotEntry
Fields§
§delegation: Pubkey
§id: u64
§snapshot_frame: Pubkey
§stake_amount: u64
Implementations§
source§impl SnapshotEntry
impl SnapshotEntry
sourcepub fn pubkey(snapshot_frame: Pubkey, id: u64) -> Pubkey
pub fn pubkey(snapshot_frame: Pubkey, id: u64) -> Pubkey
Examples found in repository?
More examples
src/instructions/snapshot_frame_delete.rs (line 86)
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 101 102 103 104 105 106 107 108 109 110 111
pub fn handler(ctx: Context<SnapshotFrameDelete>) -> Result<ThreadResponse> {
// Get accounts
let config = &ctx.accounts.config;
let registry = &ctx.accounts.registry;
let snapshot = &mut ctx.accounts.snapshot;
let snapshot_frame = &mut ctx.accounts.snapshot_frame;
let thread = &mut ctx.accounts.thread;
// If this frame has no entries, then close the frame account.
if snapshot_frame.total_entries.eq(&0) {
let snapshot_frame_lamports = snapshot_frame.to_account_info().lamports();
**snapshot_frame.to_account_info().lamports.borrow_mut() = 0;
**thread.to_account_info().lamports.borrow_mut() = thread
.to_account_info()
.lamports()
.checked_add(snapshot_frame_lamports)
.unwrap();
// If this is also the last frame in the snapshot, then close the snapshot account.
if snapshot_frame.id.checked_add(1).unwrap().eq(&snapshot.total_frames) {
let snapshot_lamports = snapshot.to_account_info().lamports();
**snapshot.to_account_info().lamports.borrow_mut() = 0;
**thread.to_account_info().lamports.borrow_mut() = thread
.to_account_info()
.lamports()
.checked_add(snapshot_lamports)
.unwrap();
}
}
// Build the next instruction.
let next_instruction = if snapshot_frame.total_entries.gt(&0) {
// This frame has entries. Delete the entries.
Some(InstructionData {
program_id: crate::ID,
accounts: vec![
AccountMetaData::new_readonly(config.key(), false),
AccountMetaData::new_readonly(registry.key(), false),
AccountMetaData::new(snapshot.key(), false),
AccountMetaData::new(SnapshotEntry::pubkey(snapshot_frame.key(), 0), false),
AccountMetaData::new(snapshot_frame.key(), false),
AccountMetaData::new(thread.key(), true),
],
data: anchor_sighash("snapshot_entry_delete").to_vec(),
})
} else if snapshot_frame.id.checked_add(1).unwrap().lt(&snapshot.total_frames) {
// There are no more entries in this frame. Move on to the next frame.
Some(InstructionData {
program_id: crate::ID,
accounts: vec![
AccountMetaData::new_readonly(config.key(), false),
AccountMetaData::new_readonly(registry.key(), false),
AccountMetaData::new(snapshot.key(), false),
AccountMetaData::new(SnapshotFrame::pubkey(snapshot.key(), snapshot_frame.id.checked_add(1).unwrap()), false),
AccountMetaData::new(thread.key(), true),
],
data: anchor_sighash("snapshot_frame_delete").to_vec(),
})
} else {
// This frame has no entries, and it was the last frame. We are done!
None
};
Ok( ThreadResponse { next_instruction, ..ThreadResponse::default() } )
}
src/instructions/snapshot_entry_delete.rs (line 108)
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
pub fn handler(ctx: Context<SnapshotEntryDelete>) -> Result<ThreadResponse> {
// Get accounts
let config = &ctx.accounts.config;
let registry = &ctx.accounts.registry;
let snapshot = &mut ctx.accounts.snapshot;
let snapshot_entry = &mut ctx.accounts.snapshot_entry;
let snapshot_frame = &mut ctx.accounts.snapshot_frame;
let thread = &mut ctx.accounts.thread;
// Close the snapshot entry account.
let snapshot_entry_lamports = snapshot_entry.to_account_info().lamports();
**snapshot_entry.to_account_info().lamports.borrow_mut() = 0;
**thread.to_account_info().lamports.borrow_mut() = thread
.to_account_info()
.lamports()
.checked_add(snapshot_entry_lamports)
.unwrap();
// If this frame has no more entries, then close the frame account.
if snapshot_entry.id.checked_add(1).unwrap().eq(&snapshot_frame.total_entries) {
let snapshot_frame_lamports = snapshot_frame.to_account_info().lamports();
**snapshot_frame.to_account_info().lamports.borrow_mut() = 0;
**thread.to_account_info().lamports.borrow_mut() = thread
.to_account_info()
.lamports()
.checked_add(snapshot_frame_lamports)
.unwrap();
// If this is also the last frame in the snapshot, then close the snapshot account.
if snapshot_frame.id.checked_add(1).unwrap().eq(&snapshot.total_frames) {
let snapshot_lamports = snapshot.to_account_info().lamports();
**snapshot.to_account_info().lamports.borrow_mut() = 0;
**thread.to_account_info().lamports.borrow_mut() = thread
.to_account_info()
.lamports()
.checked_add(snapshot_lamports)
.unwrap();
}
}
// Build the next instruction
let next_instruction = if snapshot_entry.id.checked_add(1).unwrap().lt(&snapshot_frame.total_entries) {
// Move on to the next entry.
Some(InstructionData {
program_id: crate::ID,
accounts: vec![
AccountMetaData::new_readonly(config.key(), false),
AccountMetaData::new_readonly(registry.key(), false),
AccountMetaData::new(snapshot.key(), false),
AccountMetaData::new(SnapshotEntry::pubkey(snapshot_frame.key(), snapshot_entry.id.checked_add(1).unwrap()), false),
AccountMetaData::new(snapshot_frame.key(), false),
AccountMetaData::new(thread.key(), true),
],
data: anchor_sighash("snapshot_entry_delete").to_vec(),
})
} else if snapshot_frame.id.checked_add(1).unwrap().lt(&snapshot.total_frames) {
// This frame has no more entries. Move onto the next frame.
Some(InstructionData {
program_id: crate::ID,
accounts: vec![
AccountMetaData::new_readonly(config.key(), false),
AccountMetaData::new_readonly(registry.key(), false),
AccountMetaData::new(snapshot.key(), false),
AccountMetaData::new(SnapshotFrame::pubkey(snapshot.key(), snapshot_frame.id.checked_add(1).unwrap()), false),
AccountMetaData::new(thread.key(), true),
],
data: anchor_sighash("snapshot_frame_delete").to_vec(),
})
} else {
// This frame as no more entires and it was the last frame in the snapshot. We are done!
None
};
Ok( ThreadResponse { next_instruction, ..ThreadResponse::default() } )
}
src/instructions/snapshot_frame_create.rs (line 98)
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
pub fn handler(ctx: Context<SnapshotFrameCreate>) -> Result<ThreadResponse> {
// Get accounts.
let config = &ctx.accounts.config;
let registry = &ctx.accounts.registry;
let snapshot = &mut ctx.accounts.snapshot;
let snapshot_frame = &mut ctx.accounts.snapshot_frame;
let system_program = &ctx.accounts.system_program;
let thread = &ctx.accounts.thread;
let worker = &ctx.accounts.worker;
let worker_stake = &ctx.accounts.worker_stake;
// Initialize snapshot frame account.
snapshot_frame.init(
snapshot.total_frames,
snapshot.key(),
worker_stake.amount,
snapshot.total_stake,
worker.key(),
)?;
// Update snapshot total workers.
snapshot.total_stake = snapshot
.total_stake
.checked_add(worker_stake.amount)
.unwrap();
snapshot.total_frames = snapshot.total_frames.checked_add(1).unwrap();
// Build the next instruction for the thread.
let next_instruction = if worker.total_delegations.gt(&0) {
// This worker has delegations. Create a snapshot entry for each delegation associated with this worker.
let zeroth_delegation_pubkey = Delegation::pubkey(worker.pubkey(), 0);
let zeroth_snapshot_entry_pubkey = SnapshotEntry::pubkey(snapshot_frame.key(), 0);
Some(InstructionData {
program_id: crate::ID,
accounts: vec![
AccountMetaData::new_readonly(config.key(), false),
AccountMetaData::new_readonly(zeroth_delegation_pubkey, false),
AccountMetaData::new(clockwork_utils::PAYER_PUBKEY, true),
AccountMetaData::new_readonly(registry.key(), false),
AccountMetaData::new_readonly(snapshot.key(), false),
AccountMetaData::new(zeroth_snapshot_entry_pubkey, false),
AccountMetaData::new(snapshot_frame.key(), false),
AccountMetaData::new_readonly(system_program.key(), false),
AccountMetaData::new_readonly(thread.key(), true),
AccountMetaData::new_readonly(worker.key(), false),
],
data: anchor_sighash("snapshot_entry_create").to_vec(),
})
} else if snapshot.total_frames.lt(®istry.total_workers) {
// This worker has no delegations. Create a snapshot frame for the next worker.
let next_snapshot_frame_pubkey =
SnapshotFrame::pubkey(snapshot.key(), snapshot_frame.id.checked_add(1).unwrap());
let next_worker_pubkey = Worker::pubkey(worker.id.checked_add(1).unwrap());
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.key(), false),
AccountMetaData::new(next_snapshot_frame_pubkey, false),
AccountMetaData::new_readonly(system_program.key(), false),
AccountMetaData::new_readonly(thread.key(), true),
AccountMetaData::new_readonly(next_worker_pubkey, false),
AccountMetaData::new_readonly(
get_associated_token_address(&next_worker_pubkey, &config.mint),
false,
),
],
data: anchor_sighash("snapshot_frame_create").to_vec(),
})
} else {
// This worker has no delegations and this is the last frame, so the snapshot is done. 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 {
next_instruction,
..ThreadResponse::default()
})
}
src/instructions/snapshot_entry_create.rs (lines 103-106)
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 162 163
pub fn handler(ctx: Context<SnapshotEntryCreate>) -> Result<ThreadResponse> {
// Get accounts.
let config = &ctx.accounts.config;
let delegation = &ctx.accounts.delegation;
let registry = &ctx.accounts.registry;
let snapshot = &mut ctx.accounts.snapshot;
let snapshot_entry = &mut ctx.accounts.snapshot_entry;
let snapshot_frame = &mut ctx.accounts.snapshot_frame;
let system_program = &ctx.accounts.system_program;
let thread = &ctx.accounts.thread;
let worker = &ctx.accounts.worker;
// Initialize snapshot entry account.
snapshot_entry.init(
delegation.key(),
snapshot_frame.total_entries,
snapshot_frame.key(),
delegation.stake_amount,
)?;
// Update the snapshot frame.
snapshot_frame.total_entries = snapshot_frame.total_entries.checked_add(1).unwrap();
// Build the next instruction for the thread.
let next_instruction = if snapshot_frame.total_entries.lt(&worker.total_delegations) {
// Create a snapshot entry for the next delegation.
let next_delegation_pubkey =
Delegation::pubkey(worker.pubkey(), delegation.id.checked_add(1).unwrap());
let next_snapshot_entry_pubkey = SnapshotEntry::pubkey(
snapshot_frame.key(),
snapshot_entry.id.checked_add(1).unwrap(),
);
Some(InstructionData {
program_id: crate::ID,
accounts: vec![
AccountMetaData::new_readonly(config.key(), false),
AccountMetaData::new_readonly(next_delegation_pubkey, false),
AccountMetaData::new(clockwork_utils::PAYER_PUBKEY, true),
AccountMetaData::new_readonly(thread.key(), true),
AccountMetaData::new_readonly(registry.key(), false),
AccountMetaData::new_readonly(snapshot.key(), false),
AccountMetaData::new(next_snapshot_entry_pubkey, false),
AccountMetaData::new(snapshot_frame.key(), false),
AccountMetaData::new_readonly(system_program.key(), false),
AccountMetaData::new_readonly(worker.key(), false),
],
data: anchor_sighash("snapshot_entry_create").to_vec(),
})
} else if snapshot.total_frames.lt(®istry.total_workers) {
// This frame has captured all its entries. Create a frame for the next worker.
let next_snapshot_frame_pubkey =
SnapshotFrame::pubkey(snapshot.key(), snapshot_frame.id.checked_add(1).unwrap());
let next_worker_pubkey = Worker::pubkey(worker.id.checked_add(1).unwrap());
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.key(), false),
AccountMetaData::new(next_snapshot_frame_pubkey, false),
AccountMetaData::new_readonly(system_program.key(), false),
AccountMetaData::new_readonly(thread.key(), true),
AccountMetaData::new_readonly(next_worker_pubkey, false),
AccountMetaData::new_readonly(
get_associated_token_address(&next_worker_pubkey, &config.mint),
false,
),
],
data: anchor_sighash("snapshot_frame_create").to_vec(),
})
} else {
// All entries in this frame have been captured, and it is the last frame. The snapshot is done!
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 {
next_instruction,
..ThreadResponse::default()
})
}
src/instructions/fee_distribute.rs (lines 116-119)
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 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
pub fn handler(ctx: Context<FeeDistribute>) -> Result<ThreadResponse> {
// Get accounts
let config = &ctx.accounts.config;
let delegation = &mut ctx.accounts.delegation;
let fee = &mut ctx.accounts.fee;
let registry = &ctx.accounts.registry;
let snapshot = &ctx.accounts.snapshot;
let snapshot_entry = &ctx.accounts.snapshot_entry;
let snapshot_frame = &ctx.accounts.snapshot_frame;
let thread = &ctx.accounts.thread;
let worker = &ctx.accounts.worker;
// Calculate the balance of this particular delegation, based on the weight of its stake with this worker.
let distribution_balance = if snapshot_frame.stake_amount.gt(&0) {
fee.distributable_balance
.checked_mul(snapshot_entry.stake_amount)
.unwrap()
.checked_div(snapshot_frame.stake_amount)
.unwrap()
} else {
0
};
// Transfer yield to the worker.
**fee.to_account_info().try_borrow_mut_lamports()? = fee
.to_account_info()
.lamports()
.checked_sub(distribution_balance)
.unwrap();
**delegation.to_account_info().try_borrow_mut_lamports()? = delegation
.to_account_info()
.lamports()
.checked_add(distribution_balance)
.unwrap();
// Increment the delegation's yield balance.
delegation.yield_balance = delegation
.yield_balance
.checked_add(distribution_balance)
.unwrap();
// Build the next instruction for the thread.
let next_instruction = if snapshot_entry
.id
.checked_add(1)
.unwrap()
.lt(&snapshot_frame.total_entries)
{
// This frame has more entries. Move on to the next one.
let next_delegation_pubkey =
Delegation::pubkey(worker.key(), delegation.id.checked_add(1).unwrap());
let next_snapshot_entry_pubkey = SnapshotEntry::pubkey(
snapshot_frame.key(),
snapshot_entry.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(fee.key(), false),
AccountMetaData::new_readonly(registry.key(), false),
AccountMetaData::new_readonly(snapshot.key(), false),
AccountMetaData::new_readonly(snapshot_frame.key(), false),
AccountMetaData::new_readonly(next_snapshot_entry_pubkey, false),
AccountMetaData::new_readonly(thread.key(), true),
AccountMetaData::new_readonly(worker.key(), false),
],
data: anchor_sighash("fee_distribute").to_vec(),
})
} else if snapshot_frame
.id
.checked_add(1)
.unwrap()
.lt(&snapshot.total_frames)
{
// This frame has no more entries. Move on to the next worker.
let next_worker_pubkey = Worker::pubkey(worker.id.checked_add(1).unwrap());
let next_snapshot_frame_pubkey =
SnapshotFrame::pubkey(snapshot.key(), snapshot_frame.id.checked_add(1).unwrap());
Some(InstructionData {
program_id: crate::ID,
accounts: vec![
AccountMetaData::new_readonly(config.key(), false),
AccountMetaData::new(Fee::pubkey(next_worker_pubkey), false),
AccountMetaData::new_readonly(registry.key(), false),
AccountMetaData::new_readonly(snapshot.key(), false),
AccountMetaData::new_readonly(next_snapshot_frame_pubkey, false),
AccountMetaData::new_readonly(thread.key(), true),
AccountMetaData::new(next_worker_pubkey, false),
],
data: anchor_sighash("worker_fees_distribute").to_vec(),
})
} else {
// This frame has no more entires and it is the last frame. Move on to staking delegations.
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(),
})
};
Ok(ThreadResponse {
next_instruction,
..ThreadResponse::default()
})
}
Additional examples can be found in:
Trait Implementations§
source§impl AccountDeserialize for SnapshotEntry
impl AccountDeserialize for SnapshotEntry
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 moresource§impl AccountSerialize for SnapshotEntry
impl AccountSerialize for SnapshotEntry
source§impl BorshDeserialize for SnapshotEntrywhere
Pubkey: BorshDeserialize,
u64: BorshDeserialize,
impl BorshDeserialize for SnapshotEntrywhere
Pubkey: BorshDeserialize,
u64: BorshDeserialize,
source§impl BorshSerialize for SnapshotEntrywhere
Pubkey: BorshSerialize,
u64: BorshSerialize,
impl BorshSerialize for SnapshotEntrywhere
Pubkey: BorshSerialize,
u64: BorshSerialize,
source§impl Clone for SnapshotEntry
impl Clone for SnapshotEntry
source§fn clone(&self) -> SnapshotEntry
fn clone(&self) -> SnapshotEntry
Returns a copy of the value. Read more
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read more