-
Notifications
You must be signed in to change notification settings - Fork 26
refactor: Simplify Task types #665
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
base: snawaz/diff-stragegy
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,215 @@ | ||
| use magicblock_committor_program::{ | ||
| instruction_builder::{ | ||
| close_buffer::{create_close_ix, CreateCloseIxArgs}, | ||
| init_buffer::{create_init_ix, CreateInitIxArgs}, | ||
| realloc_buffer::{ | ||
| create_realloc_buffer_ixs, CreateReallocBufferIxArgs, | ||
| }, | ||
| write_buffer::{create_write_ix, CreateWriteIxArgs}, | ||
| }, | ||
| pdas, ChangesetChunks, Chunks, | ||
| }; | ||
| use magicblock_program::magic_scheduled_base_intent::CommittedAccount; | ||
| use solana_account::Account; | ||
| use solana_instruction::Instruction; | ||
| use solana_pubkey::Pubkey; | ||
|
|
||
| use crate::consts::MAX_WRITE_CHUNK_SIZE; | ||
|
|
||
| #[derive(Debug, Clone)] | ||
| pub struct BufferLifecycle { | ||
| // TODO (snawaz): rename | ||
| // PreparationTask -> CreateBufferTask | ||
| // CleanupTask -> DestroyBufferTask | ||
| pub preparation: PreparationTask, | ||
| pub cleanup: CleanupTask, | ||
| } | ||
|
|
||
| impl BufferLifecycle { | ||
| pub fn new( | ||
| commit_id: u64, | ||
| account: &CommittedAccount, | ||
| base_account: Option<&Account>, | ||
| ) -> BufferLifecycle { | ||
| let data = if let Some(base_account) = base_account { | ||
| dlp::compute_diff(&base_account.data, &account.account.data) | ||
| .to_vec() | ||
| } else { | ||
| account.account.data.clone() | ||
| }; | ||
|
|
||
| BufferLifecycle { | ||
| preparation: PreparationTask { | ||
| commit_id, | ||
| pubkey: account.pubkey, | ||
| chunks: Chunks::from_data_length( | ||
| data.len(), | ||
| MAX_WRITE_CHUNK_SIZE, | ||
| ), | ||
| state_or_diff: data, | ||
| }, | ||
| cleanup: CleanupTask { | ||
| pubkey: account.pubkey, | ||
| commit_id, | ||
| }, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[derive(Clone, Debug)] | ||
| pub struct PreparationTask { | ||
| pub commit_id: u64, | ||
| pub pubkey: Pubkey, | ||
| pub chunks: Chunks, | ||
| pub state_or_diff: Vec<u8>, | ||
| } | ||
|
|
||
| impl PreparationTask { | ||
| /// Returns initialization [`Instruction`] | ||
| pub fn instruction(&self, authority: &Pubkey) -> Instruction { | ||
| // // SAFETY: as object_length internally uses only already allocated or static buffers, | ||
| // // and we don't use any fs writers, so the only error that may occur here is of kind | ||
| // // OutOfMemory or WriteZero. This is impossible due to: | ||
| // // Chunks::new panics if its size exceeds MAX_ACCOUNT_ALLOC_PER_INSTRUCTION_SIZE or 10_240 | ||
| // // https://github.com/near/borsh-rs/blob/f1b75a6b50740bfb6231b7d0b1bd93ea58ca5452/borsh/src/ser/helpers.rs#L59 | ||
| let chunks_account_size = | ||
| borsh::object_length(&self.chunks).unwrap() as u64; | ||
| let buffer_account_size = self.state_or_diff.len() as u64; | ||
|
|
||
| let (instruction, _, _) = create_init_ix(CreateInitIxArgs { | ||
| authority: *authority, | ||
| pubkey: self.pubkey, | ||
| chunks_account_size, | ||
| buffer_account_size, | ||
| commit_id: self.commit_id, | ||
| chunk_count: self.chunks.count(), | ||
| chunk_size: self.chunks.chunk_size(), | ||
| }); | ||
|
|
||
| instruction | ||
| } | ||
|
|
||
| /// Returns compute units required for realloc instruction | ||
| pub fn init_compute_units(&self) -> u32 { | ||
| 12_000 | ||
| } | ||
|
|
||
| /// Returns realloc instruction required for Buffer preparation | ||
| #[allow(clippy::let_and_return)] | ||
| pub fn realloc_instructions(&self, authority: &Pubkey) -> Vec<Instruction> { | ||
| let buffer_account_size = self.state_or_diff.len() as u64; | ||
| let realloc_instructions = | ||
| create_realloc_buffer_ixs(CreateReallocBufferIxArgs { | ||
| authority: *authority, | ||
| pubkey: self.pubkey, | ||
| buffer_account_size, | ||
| commit_id: self.commit_id, | ||
| }); | ||
|
|
||
| realloc_instructions | ||
| } | ||
|
|
||
| /// Returns compute units required for realloc instruction | ||
| pub fn realloc_compute_units(&self) -> u32 { | ||
| 6_000 | ||
| } | ||
|
|
||
| /// Returns realloc instruction required for Buffer preparation | ||
| #[allow(clippy::let_and_return)] | ||
| pub fn write_instructions(&self, authority: &Pubkey) -> Vec<Instruction> { | ||
| let chunks_iter = | ||
| ChangesetChunks::new(&self.chunks, self.chunks.chunk_size()) | ||
| .iter(&self.state_or_diff); | ||
| let write_instructions = chunks_iter | ||
| .map(|chunk| { | ||
| create_write_ix(CreateWriteIxArgs { | ||
| authority: *authority, | ||
| pubkey: self.pubkey, | ||
| offset: chunk.offset, | ||
| data_chunk: chunk.data_chunk, | ||
| commit_id: self.commit_id, | ||
| }) | ||
| }) | ||
| .collect::<Vec<_>>(); | ||
|
|
||
| write_instructions | ||
| } | ||
|
|
||
| pub fn write_compute_units(&self, bytes_count: usize) -> u32 { | ||
| const PER_BYTE: u32 = 3; | ||
|
|
||
| u32::try_from(bytes_count) | ||
| .ok() | ||
| .and_then(|bytes_count| bytes_count.checked_mul(PER_BYTE)) | ||
| .unwrap_or(u32::MAX) | ||
| } | ||
|
Comment on lines
+117
to
+145
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧹 Nitpick | 🔵 Trivial
const PER_BYTE: u32 = 3;
u32::try_from(bytes_count)
.ok()
.and_then(|bytes_count| bytes_count.checked_mul(PER_BYTE))
.unwrap_or(u32::MAX)This is a sensible pattern, but the semantics (“if the length doesn’t fit in
No code change needed; just making the saturation behavior explicit will help future readers. 🤖 Prompt for AI Agents |
||
|
|
||
| pub fn chunks_pda(&self, authority: &Pubkey) -> Pubkey { | ||
| pdas::chunks_pda( | ||
| authority, | ||
| &self.pubkey, | ||
| self.commit_id.to_le_bytes().as_slice(), | ||
| ) | ||
| .0 | ||
| } | ||
|
|
||
| pub fn buffer_pda(&self, authority: &Pubkey) -> Pubkey { | ||
| pdas::buffer_pda( | ||
| authority, | ||
| &self.pubkey, | ||
| self.commit_id.to_le_bytes().as_slice(), | ||
| ) | ||
| .0 | ||
| } | ||
|
|
||
| pub fn cleanup_task(&self) -> CleanupTask { | ||
| CleanupTask { | ||
| pubkey: self.pubkey, | ||
| commit_id: self.commit_id, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[derive(Clone, Debug)] | ||
| pub struct CleanupTask { | ||
| pub pubkey: Pubkey, | ||
| pub commit_id: u64, | ||
| } | ||
|
|
||
| impl CleanupTask { | ||
| pub fn instruction(&self, authority: &Pubkey) -> Instruction { | ||
| create_close_ix(CreateCloseIxArgs { | ||
| authority: *authority, | ||
| pubkey: self.pubkey, | ||
| commit_id: self.commit_id, | ||
| }) | ||
| } | ||
|
|
||
| /// Returns compute units required to execute [`CleanupTask`] | ||
| pub fn compute_units(&self) -> u32 { | ||
| 30_000 | ||
| } | ||
|
|
||
| /// Returns a number of [`CleanupTask`]s that is possible to fit in single | ||
| pub const fn max_tx_fit_count_with_budget() -> usize { | ||
| 8 | ||
| } | ||
|
|
||
| pub fn chunks_pda(&self, authority: &Pubkey) -> Pubkey { | ||
| pdas::chunks_pda( | ||
| authority, | ||
| &self.pubkey, | ||
| self.commit_id.to_le_bytes().as_slice(), | ||
| ) | ||
| .0 | ||
| } | ||
|
|
||
| pub fn buffer_pda(&self, authority: &Pubkey) -> Pubkey { | ||
| pdas::buffer_pda( | ||
| authority, | ||
| &self.pubkey, | ||
| self.commit_id.to_le_bytes().as_slice(), | ||
| ) | ||
| .0 | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix spelling error.
"eveything" should be "everything".
Apply this diff:
📝 Committable suggestion
🧰 Tools
🪛 LanguageTool
[grammar] ~26-~26: Ensure spelling is correct
Context: ... Tasks are ready we need to prepare eveything for their successful execution. **Deliv...
(QB_NEW_EN_ORTHOGRAPHY_ERROR_IDS_1)
🤖 Prompt for AI Agents