-
Notifications
You must be signed in to change notification settings - Fork 21
feat(fraud-proofs): Implement WriteStateBuffer #231
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
Open
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
69e3416
feat(fraud-proofs): Implement WriteStateBuffer
snawaz 1ab2187
Make StateBuffer authority neutral
snawaz 932c780
Use one-byte tag offset for state buffer args
snawaz a3dac28
Make WriteStateBuffer authority-specific
snawaz 875f3c7
Use extendable StateBuffer payload
snawaz 9d20767
Drop trivial WriteStateBuffer instruction data test
snawaz 5f6fc5f
Rename state buffer tests
snawaz 518d022
dev review
snawaz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| use wheels::variable_offset_layout; | ||
|
|
||
| #[derive(Clone, Debug, PartialEq, Eq)] | ||
| #[variable_offset_layout(buffer_offset = 1)] | ||
| pub struct WriteStateBufferArgs { | ||
| pub commit_id: u64, | ||
|
|
||
| pub total_len: u32, | ||
|
|
||
| /// Must equal bytes already written, unless retrying an exact old chunk. | ||
| pub offset: u32, | ||
|
|
||
| #[flexible = 4] | ||
| pub chunk: Vec<u8>, | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| use solana_program::{ | ||
| instruction::{AccountMeta, Instruction}, | ||
| pubkey::Pubkey, | ||
| }; | ||
| use solana_sdk_ids::system_program; | ||
| use wheels::layout::Encodable; | ||
|
|
||
| use crate::{ | ||
| compat::{Compatize, Modernize}, | ||
| v2::{ | ||
| pda::{protocol_config_pda, state_buffer_pda}, | ||
| DlpV2Instruction, WriteStateBufferArgs, | ||
| }, | ||
| }; | ||
|
|
||
| /// Builds the instruction that writes full account-state bytes to a v2 buffer. | ||
| pub fn write_state_buffer( | ||
| payer: Pubkey, | ||
| authority: Pubkey, | ||
| account: Pubkey, | ||
| args: WriteStateBufferArgs, | ||
| ) -> Instruction { | ||
| Instruction { | ||
| program_id: crate::id().modernize(), | ||
| accounts: vec![ | ||
| AccountMeta::new(payer, true), | ||
| AccountMeta::new_readonly(authority, true), | ||
| AccountMeta::new( | ||
| state_buffer_pda( | ||
| &account.compatize(), | ||
| args.commit_id, | ||
| &authority.compatize(), | ||
| ) | ||
| .modernize(), | ||
| false, | ||
| ), | ||
| AccountMeta::new_readonly(account, false), | ||
| AccountMeta::new_readonly(protocol_config_pda().modernize(), false), | ||
| AccountMeta::new_readonly(system_program::id(), false), | ||
| ], | ||
| data: [ | ||
| DlpV2Instruction::WriteStateBuffer.to_vec(), | ||
| args.encode().unwrap(), | ||
| ] | ||
| .concat(), | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,11 @@ | ||
| mod operator_bond; | ||
| mod protocol_config; | ||
| mod state_buffer; | ||
| mod verifier_bond; | ||
| mod verifier_registry; | ||
|
|
||
| pub use operator_bond::*; | ||
| pub use protocol_config::*; | ||
| pub use state_buffer::*; | ||
| pub use verifier_bond::*; | ||
| pub use verifier_registry::*; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| use wheels::fixed_offset_layout; | ||
|
|
||
| use crate::{compat::Pubkey, error::DlpError}; | ||
|
|
||
| /// PDA: `["state-buffer", account, commit_id, authority]`. | ||
| /// Created by `WriteStateBuffer`. | ||
| /// Closed by `CloseTerminalAccounts` after finalize, cancel, or expiry. | ||
| #[derive(Clone, Debug, PartialEq, Eq)] | ||
| #[fixed_offset_layout(buffer_offset = 0)] | ||
| pub struct StateBuffer { | ||
| /// Account type marker. | ||
| pub discriminator: [u8; 8], | ||
|
|
||
| /// Writer that owns this opened buffer. | ||
| /// | ||
| /// This is the operator identity for an operator commitment buffer, or the | ||
| /// challenger identity for a challenger dispute buffer. | ||
| pub authority: Pubkey, | ||
|
|
||
| /// Delegated account whose payload is stored after this header. | ||
| pub account_pubkey: Pubkey, | ||
|
|
||
| /// Flow-specific nonce that identifies this opened buffer. | ||
| pub commit_id: u64, | ||
|
|
||
| /// Hash of the finalized payload. Zero until finalized. | ||
| pub data_hash: [u8; 32], | ||
|
|
||
| /// Expected final byte length of the payload. | ||
| pub total_len: u32, | ||
|
|
||
| /// Once true, buffer content cannot change except exact duplicate retries. | ||
| pub finalized: bool, | ||
|
|
||
| /// Active payload bytes for `PostCommitment` or `RaiseChallenge`. | ||
| /// | ||
| /// This is not a serialized Solana account. Depending on the flow, it can | ||
| /// be the delegated account's complete `Account::data` bytes or an encoded | ||
| /// diff of those bytes. `payload.len()` is the written prefix, while | ||
| /// `payload.capacity()` is the allocated account-backed span and can be | ||
| /// shorter than `total_len` until later writes grow the buffer account. | ||
| #[extendable = 4] | ||
| pub payload: Vec<u8>, | ||
| } | ||
|
|
||
| impl StateBuffer { | ||
| pub const DISCRIMINATOR: [u8; 8] = *b"v2sbuf00"; | ||
|
|
||
| /// Maximum account data bytes a StateBuffer PDA may allocate. | ||
| pub const MAX_ACCOUNT_DATA_LEN: usize = 10 * 1024 * 1024; | ||
|
|
||
| /// Maximum account data bytes a StateBuffer PDA may grow in one write. | ||
| pub const MAX_ACCOUNT_DATA_GROWTH_PER_WRITE: usize = 10_240; | ||
|
|
||
| /// Offset of the extendable payload length header. | ||
| pub const PAYLOAD_LEN_HEADER_OFFSET: usize = Self::MIN_DATA_LEN; | ||
|
|
||
| /// Byte length of the extendable payload length header. | ||
| pub const PAYLOAD_LEN_HEADER_LEN: usize = 4; | ||
|
|
||
| /// Offset where payload bytes begin in account data. | ||
| pub const PAYLOAD_BYTES_OFFSET: usize = | ||
| Self::PAYLOAD_LEN_HEADER_OFFSET + Self::PAYLOAD_LEN_HEADER_LEN; | ||
|
|
||
| /// Maximum payload bytes allocated when a StateBuffer PDA is created. | ||
| pub const MAX_INITIAL_PAYLOAD_LEN: usize = | ||
| Self::MAX_ACCOUNT_DATA_GROWTH_PER_WRITE - Self::PAYLOAD_BYTES_OFFSET; | ||
|
|
||
| /// Maximum payload bytes accepted across all writes. | ||
| pub const MAX_TOTAL_PAYLOAD_LEN: u32 = | ||
| (Self::MAX_ACCOUNT_DATA_LEN - Self::PAYLOAD_BYTES_OFFSET) as u32; | ||
|
|
||
| /// Returns the serialized data length needed for a payload capacity. | ||
| pub fn data_len_from_payload_capacity( | ||
| payload_capacity: usize, | ||
| ) -> Result<usize, DlpError> { | ||
| Self::PAYLOAD_BYTES_OFFSET | ||
| .checked_add(payload_capacity) | ||
| .ok_or(DlpError::Overflow) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,5 @@ | ||
| //! Processors for v2 fraud-proof instructions. | ||
|
|
||
| mod write_state_buffer; | ||
|
|
||
| pub use write_state_buffer::*; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
🩺 Stability & Availability | 🟠 Major | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: magicblock-labs/delegation-program
Length of output: 11750
🏁 Script executed:
Repository: magicblock-labs/delegation-program
Length of output: 21995
🏁 Script executed:
Repository: magicblock-labs/delegation-program
Length of output: 14836
🌐 Web query:
GitHub magicblock-wheels rust wheels layout Encodable variable_offset_layout encode💡 Result:
The repository magicblock-labs/magicblock-wheels is an internal SDK maintained by MagicBlock, a company specializing in high-performance engines for decentralized games and applications on Solana [1][2]. Regarding the specific technical components mentioned: 1. magicblock-wheels: This repository serves as a shared, internal library of reusable Rust crates and TypeScript utilities for MagicBlock projects [1]. It is not intended as a user-facing SDK [1]. 2. variable_offset_layout and Encodable/encode: These terms relate to memory or data layout serialization patterns common in Rust-based Solana development (often associated with anchor-lang or custom borsh-based layouts). While magicblock-wheels contains Rust workspace members including a public no_std rust/wheels crate, these specific identifiers are standard patterns for building custom account layouts or zero-copy data structures in the Solana Virtual Machine (SVM) ecosystem [1]. Developers working with MagicBlock's tooling typically interact with their public-facing SDKs—such as the Ephemeral Rollups SDK or the Bolt framework (though Bolt is now deprecated)—rather than interacting directly with the internal magicblock-wheels crate [1][3]. For implementation details on encoding data in a Rust-based Solana program, developers typically use the Borsh serialization framework or Anchor's derive macros, which handle variable offset layouts automatically [1].
Citations:
🏁 Script executed:
Repository: magicblock-labs/delegation-program
Length of output: 4963
🏁 Script executed:
Repository: magicblock-labs/delegation-program
Length of output: 223
🏁 Script executed:
Repository: magicblock-labs/delegation-program
Length of output: 12563
Replace the serialization panic with error propagation.
wheels::layout::Encodable::encode()returnsResult<Vec<u8>, DataLayoutError>, butwrite_state_bufferunwraps it while constructing the instruction data. Return the encoding error, or enforce and document an invariant that makes encoding infallible.🤖 Prompt for AI Agents
Source: Path instructions