|
| 1 | +use magicblock_committor_program::{ |
| 2 | + instruction_builder::{ |
| 3 | + close_buffer::{create_close_ix, CreateCloseIxArgs}, |
| 4 | + init_buffer::{create_init_ix, CreateInitIxArgs}, |
| 5 | + realloc_buffer::{ |
| 6 | + create_realloc_buffer_ixs, CreateReallocBufferIxArgs, |
| 7 | + }, |
| 8 | + write_buffer::{create_write_ix, CreateWriteIxArgs}, |
| 9 | + }, |
| 10 | + pdas, ChangesetChunks, Chunks, |
| 11 | +}; |
| 12 | +use magicblock_program::magic_scheduled_base_intent::CommittedAccount; |
| 13 | +use solana_account::Account; |
| 14 | +use solana_instruction::Instruction; |
| 15 | +use solana_pubkey::Pubkey; |
| 16 | + |
| 17 | +use crate::consts::MAX_WRITE_CHUNK_SIZE; |
| 18 | + |
| 19 | +#[derive(Debug, Clone)] |
| 20 | +pub struct BufferLifecycle { |
| 21 | + // TODO (snawaz): rename |
| 22 | + // PreparationTask -> CreateBufferTask |
| 23 | + // CleanupTask -> DestroyBufferTask |
| 24 | + pub preparation: PreparationTask, |
| 25 | + pub cleanup: CleanupTask, |
| 26 | +} |
| 27 | + |
| 28 | +impl BufferLifecycle { |
| 29 | + pub fn new( |
| 30 | + commit_id: u64, |
| 31 | + account: &CommittedAccount, |
| 32 | + base_account: Option<&Account>, |
| 33 | + ) -> BufferLifecycle { |
| 34 | + let data = if let Some(base_account) = base_account { |
| 35 | + dlp::compute_diff(&base_account.data, &account.account.data) |
| 36 | + .to_vec() |
| 37 | + } else { |
| 38 | + account.account.data.clone() |
| 39 | + }; |
| 40 | + |
| 41 | + BufferLifecycle { |
| 42 | + preparation: PreparationTask { |
| 43 | + commit_id, |
| 44 | + pubkey: account.pubkey, |
| 45 | + chunks: Chunks::from_data_length( |
| 46 | + data.len(), |
| 47 | + MAX_WRITE_CHUNK_SIZE, |
| 48 | + ), |
| 49 | + state_or_diff: data, |
| 50 | + }, |
| 51 | + cleanup: CleanupTask { |
| 52 | + pubkey: account.pubkey, |
| 53 | + commit_id, |
| 54 | + }, |
| 55 | + } |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +#[derive(Clone, Debug)] |
| 60 | +pub struct PreparationTask { |
| 61 | + pub commit_id: u64, |
| 62 | + pub pubkey: Pubkey, |
| 63 | + pub chunks: Chunks, |
| 64 | + pub state_or_diff: Vec<u8>, |
| 65 | +} |
| 66 | + |
| 67 | +impl PreparationTask { |
| 68 | + /// Returns initialization [`Instruction`] |
| 69 | + pub fn instruction(&self, authority: &Pubkey) -> Instruction { |
| 70 | + // // SAFETY: as object_length internally uses only already allocated or static buffers, |
| 71 | + // // and we don't use any fs writers, so the only error that may occur here is of kind |
| 72 | + // // OutOfMemory or WriteZero. This is impossible due to: |
| 73 | + // // Chunks::new panics if its size exceeds MAX_ACCOUNT_ALLOC_PER_INSTRUCTION_SIZE or 10_240 |
| 74 | + // // https://github.com/near/borsh-rs/blob/f1b75a6b50740bfb6231b7d0b1bd93ea58ca5452/borsh/src/ser/helpers.rs#L59 |
| 75 | + let chunks_account_size = |
| 76 | + borsh::object_length(&self.chunks).unwrap() as u64; |
| 77 | + let buffer_account_size = self.state_or_diff.len() as u64; |
| 78 | + |
| 79 | + let (instruction, _, _) = create_init_ix(CreateInitIxArgs { |
| 80 | + authority: *authority, |
| 81 | + pubkey: self.pubkey, |
| 82 | + chunks_account_size, |
| 83 | + buffer_account_size, |
| 84 | + commit_id: self.commit_id, |
| 85 | + chunk_count: self.chunks.count(), |
| 86 | + chunk_size: self.chunks.chunk_size(), |
| 87 | + }); |
| 88 | + |
| 89 | + instruction |
| 90 | + } |
| 91 | + |
| 92 | + /// Returns compute units required for realloc instruction |
| 93 | + pub fn init_compute_units(&self) -> u32 { |
| 94 | + 12_000 |
| 95 | + } |
| 96 | + |
| 97 | + /// Returns realloc instruction required for Buffer preparation |
| 98 | + #[allow(clippy::let_and_return)] |
| 99 | + pub fn realloc_instructions(&self, authority: &Pubkey) -> Vec<Instruction> { |
| 100 | + let buffer_account_size = self.state_or_diff.len() as u64; |
| 101 | + let realloc_instructions = |
| 102 | + create_realloc_buffer_ixs(CreateReallocBufferIxArgs { |
| 103 | + authority: *authority, |
| 104 | + pubkey: self.pubkey, |
| 105 | + buffer_account_size, |
| 106 | + commit_id: self.commit_id, |
| 107 | + }); |
| 108 | + |
| 109 | + realloc_instructions |
| 110 | + } |
| 111 | + |
| 112 | + /// Returns compute units required for realloc instruction |
| 113 | + pub fn realloc_compute_units(&self) -> u32 { |
| 114 | + 6_000 |
| 115 | + } |
| 116 | + |
| 117 | + /// Returns realloc instruction required for Buffer preparation |
| 118 | + #[allow(clippy::let_and_return)] |
| 119 | + pub fn write_instructions(&self, authority: &Pubkey) -> Vec<Instruction> { |
| 120 | + let chunks_iter = |
| 121 | + ChangesetChunks::new(&self.chunks, self.chunks.chunk_size()) |
| 122 | + .iter(&self.state_or_diff); |
| 123 | + let write_instructions = chunks_iter |
| 124 | + .map(|chunk| { |
| 125 | + create_write_ix(CreateWriteIxArgs { |
| 126 | + authority: *authority, |
| 127 | + pubkey: self.pubkey, |
| 128 | + offset: chunk.offset, |
| 129 | + data_chunk: chunk.data_chunk, |
| 130 | + commit_id: self.commit_id, |
| 131 | + }) |
| 132 | + }) |
| 133 | + .collect::<Vec<_>>(); |
| 134 | + |
| 135 | + write_instructions |
| 136 | + } |
| 137 | + |
| 138 | + pub fn write_compute_units(&self, bytes_count: usize) -> u32 { |
| 139 | + const PER_BYTE: u32 = 3; |
| 140 | + |
| 141 | + u32::try_from(bytes_count) |
| 142 | + .ok() |
| 143 | + .and_then(|bytes_count| bytes_count.checked_mul(PER_BYTE)) |
| 144 | + .unwrap_or(u32::MAX) |
| 145 | + } |
| 146 | + |
| 147 | + pub fn chunks_pda(&self, authority: &Pubkey) -> Pubkey { |
| 148 | + pdas::chunks_pda( |
| 149 | + authority, |
| 150 | + &self.pubkey, |
| 151 | + self.commit_id.to_le_bytes().as_slice(), |
| 152 | + ) |
| 153 | + .0 |
| 154 | + } |
| 155 | + |
| 156 | + pub fn buffer_pda(&self, authority: &Pubkey) -> Pubkey { |
| 157 | + pdas::buffer_pda( |
| 158 | + authority, |
| 159 | + &self.pubkey, |
| 160 | + self.commit_id.to_le_bytes().as_slice(), |
| 161 | + ) |
| 162 | + .0 |
| 163 | + } |
| 164 | + |
| 165 | + pub fn cleanup_task(&self) -> CleanupTask { |
| 166 | + CleanupTask { |
| 167 | + pubkey: self.pubkey, |
| 168 | + commit_id: self.commit_id, |
| 169 | + } |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +#[derive(Clone, Debug)] |
| 174 | +pub struct CleanupTask { |
| 175 | + pub pubkey: Pubkey, |
| 176 | + pub commit_id: u64, |
| 177 | +} |
| 178 | + |
| 179 | +impl CleanupTask { |
| 180 | + pub fn instruction(&self, authority: &Pubkey) -> Instruction { |
| 181 | + create_close_ix(CreateCloseIxArgs { |
| 182 | + authority: *authority, |
| 183 | + pubkey: self.pubkey, |
| 184 | + commit_id: self.commit_id, |
| 185 | + }) |
| 186 | + } |
| 187 | + |
| 188 | + /// Returns compute units required to execute [`CleanupTask`] |
| 189 | + pub fn compute_units(&self) -> u32 { |
| 190 | + 30_000 |
| 191 | + } |
| 192 | + |
| 193 | + /// Returns a number of [`CleanupTask`]s that is possible to fit in single |
| 194 | + pub const fn max_tx_fit_count_with_budget() -> usize { |
| 195 | + 8 |
| 196 | + } |
| 197 | + |
| 198 | + pub fn chunks_pda(&self, authority: &Pubkey) -> Pubkey { |
| 199 | + pdas::chunks_pda( |
| 200 | + authority, |
| 201 | + &self.pubkey, |
| 202 | + self.commit_id.to_le_bytes().as_slice(), |
| 203 | + ) |
| 204 | + .0 |
| 205 | + } |
| 206 | + |
| 207 | + pub fn buffer_pda(&self, authority: &Pubkey) -> Pubkey { |
| 208 | + pdas::buffer_pda( |
| 209 | + authority, |
| 210 | + &self.pubkey, |
| 211 | + self.commit_id.to_le_bytes().as_slice(), |
| 212 | + ) |
| 213 | + .0 |
| 214 | + } |
| 215 | +} |
0 commit comments