-
Notifications
You must be signed in to change notification settings - Fork 1
Refactor state PDA accessors #118
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
cab3e70
Refactor state PDA accessors
fedgiac 8e5d0dc
Merge branch 'main' into refactor-state-account-decoder
fedgiac 7813e83
state_header -> state_account_bytes
fedgiac 515547f
Simplify tests for `Role::ALL`
fedgiac 3e79073
Implement StateAccount::from_account
fedgiac 85c2fd5
StateAccount::new -> StateAccount::attach
fedgiac 5286c3a
`Header` -> `StateInitArgs`
fedgiac 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 |
|---|---|---|
|
|
@@ -4,3 +4,4 @@ pub use cow_settlement_interface; | |
|
|
||
| pub mod instructions; | ||
| pub mod parse; | ||
| pub mod pda; | ||
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,3 @@ | ||
| //! Off-chain decoders for the settlement program's PDAs. | ||
|
|
||
| pub mod state; |
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,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, | ||
| } | ||
|
|
||
| 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() { | ||
|
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()); | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.