Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions bench-report.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
"create_buffers/max_buffers_in_one_instruction": 176947,
"create_order/happy_path_creates_order_pda_with_expected_body": 4976,
"initialize/happy_path_initializes_state_pda_with_expected_data": 4526,
"reclaim_buffer/funded_buffer_is_skipped": 6299,
"reclaim_buffer/happy_path_reclaims_empty_buffer_to_the_authority_itself": 7447,
"reclaim_buffer/max_buffers_in_one_instruction": 136501,
"reclaim_buffer/reclaims_multiple_buffers_skipping_funded": 18043,
"reclaim_buffer/funded_buffer_is_skipped": 6324,
"reclaim_buffer/happy_path_reclaims_empty_buffer_to_the_authority_itself": 7472,
"reclaim_buffer/max_buffers_in_one_instruction": 136526,
"reclaim_buffer/reclaims_multiple_buffers_skipping_funded": 18068,
"reclaim_order/happy_path_returns_lamports_and_closes_pda": 2183,
"settle/finalizes_with_no_pushes": 7062,
"settle/pulls_from_multiple_orders": 19921,
Expand All @@ -43,9 +43,9 @@
"settle/pushes_several_orders_from_one_buffer": 17609,
"settle/settles_a_single_order": 12384,
"settle/settles_multiple_orders": 22894,
"transfer_authority/manager_can_transfer_manager": 3170,
"transfer_authority/manager_can_transfer_reclaim_authority": 3172,
"transfer_authority/reclaim_authority_can_transfer_itself": 3175
"transfer_authority/manager_can_transfer_manager": 3174,
"transfer_authority/manager_can_transfer_reclaim_authority": 3176,
"transfer_authority/reclaim_authority_can_transfer_itself": 3180
},
"transaction_bytes": {
"create_buffers/happy_path_creates_initialized_buffer_token_account": 303,
Expand Down
1 change: 1 addition & 0 deletions client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ pub use cow_settlement_interface;

pub mod instructions;
pub mod parse;
pub mod pda;
3 changes: 3 additions & 0 deletions client/src/pda/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
//! Off-chain decoders for the settlement program's PDAs.

pub mod state;
76 changes: 76 additions & 0 deletions client/src/pda/state.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//! Off-chain decoded snapshot of a settlement state account.

use cow_settlement_interface::{data::state::StateAccount, Pubkey, Role};
use solana_program_error::ProgramError;

/// An owned, decoded snapshot of a settlement state account.
/// Similar to [`StateAccount`], but it fully owns its data.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct DecodedStateAccount {
pub manager: Pubkey,
pub reclaim_authority: Pubkey,
}
Comment thread
fedgiac marked this conversation as resolved.

impl TryFrom<&[u8]> for DecodedStateAccount {
type Error = ProgramError;

fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
let state = StateAccount::attach(bytes)?;
Ok(Self {
manager: state.authority(Role::Manager),
reclaim_authority: state.authority(Role::ReclaimAuthority),
})
}
}

#[cfg(test)]
mod tests {
use super::*;
use cow_settlement_interface::data::state::{StateInitArgs, WIDTH_HEADER};
use cow_settlement_interface::fixtures::pubkey_from_seed;

fn state_bytes(manager: &Pubkey, reclaim_authority: &Pubkey) -> [u8; WIDTH_HEADER] {
let mut bytes = [0u8; WIDTH_HEADER];
StateAccount::initialize(
&mut bytes[..],
&StateInitArgs {
manager: *manager,
reclaim_authority: *reclaim_authority,
},
)
.expect("header fits");
bytes
}

#[test]
fn decodes_the_header() {
Comment thread
kaze-cow marked this conversation as resolved.
let manager = pubkey_from_seed("manager");
let reclaim_authority = pubkey_from_seed("reclaim authority");
let bytes = state_bytes(&manager, &reclaim_authority);

let decoded = DecodedStateAccount::try_from(&bytes[..]).expect("valid state account");
assert_eq!(
decoded,
DecodedStateAccount {
manager,
reclaim_authority,
},
);
}

#[test]
fn rejects_non_state_account() {
// A zeroed buffer: right length, but its leading byte isn't the state
// discriminator.
let bytes = [0u8; WIDTH_HEADER];
assert!(DecodedStateAccount::try_from(&bytes[..]).is_err());
}

#[test]
fn rejects_too_short_account() {
let manager = pubkey_from_seed("manager");
let reclaim_authority = pubkey_from_seed("reclaim authority");
let bytes = state_bytes(&manager, &reclaim_authority);
assert!(DecodedStateAccount::try_from(&bytes[..WIDTH_HEADER - 1]).is_err());
}
}
Loading