Skip to content

Refactor state PDA accessors - #118

Merged
fedgiac merged 7 commits into
mainfrom
refactor-state-account-decoder
Aug 27, 2026
Merged

Refactor state PDA accessors#118
fedgiac merged 7 commits into
mainfrom
refactor-state-account-decoder

Conversation

@fedgiac

@fedgiac fedgiac commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

First step for introducing the management of solver accounts. Nothing change in the code behavior yet, it's just a refactor in preparation for new code.

Right now, the handling of the state PDA is awkward: we have an EncodedStateAccount that takes ownership of the data and a lot of accessors like EncodedStateAccount::authority(bytes, role) that take a bytes array to edit in place.

With these changes, we drop EncodedStateAccount and replace it with StateAccount. Notably, StateAccount doesn't own its data but it takes a reference. This allows us to access the data with things like state.authority(role), which reads very naturally and allows us to drop the bytes accessor and gives us a type-safe way to store the state account bytes.

The nicest trick in these changes is making the implementation generic in the Deref'ed type:

impl<T: Deref<Target = [u8]>> StateAccount<T> {...}
impl<T: DerefMut<Target = [u8]>> StateAccount<T> {...}

This means that if we use StateAccount(&bytes) then we only access read-only methods, while StateAccount(&mut bytes) also gives access to mutable functions on the ref. The code duplication between & and &mut is something we struggled with in the past and is imho nicely resolved by this code. All write and read accesses are, naturally, zero copy. This will work nicely for the code that updates solvers.

Note also that the state account now has a header. In this PR, the header is the state account. Once we add solvers, they will not be part of the header, and we can keep most of the remaining code unchanged. The layout doesn't change, yet, though it theoretically now supports extra padding bytes (ignored).

Role::ALL

I added Role::ALL because it was helpful in a test. It isn't used outside of tests but I still decided to make it part of the interface since it's properly tested and can be helpful. I also used it in other parts of the code where it would be helpful.

Tests

Updated tests work.

@fedgiac
fedgiac requested a review from a team as a code owner August 25, 2026 21:31

@kaze-cow kaze-cow left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love this change. tbh this is kind of the direction I was hoping for before!

based on what I have seen here, I would be in favor of later refactoring the order PDA similarly, as we can still benefit from zero copy reads there.

Comment thread client/src/pda/state.rs
Comment thread client/src/pda/state.rs
Comment thread interface/src/data/state.rs
Comment thread interface/src/data/state.rs
Comment thread interface/src/data/state.rs
Comment thread interface/src/data/state.rs
Comment thread interface/src/lib.rs Outdated
Comment thread interface/src/lib.rs Outdated
Comment thread programs/settlement/src/reclaim_buffer.rs Outdated
Comment thread programs/settlement/src/reclaim_buffer.rs Outdated
@fedgiac
fedgiac requested a review from kaze-cow August 26, 2026 14:13

@kaze-cow kaze-cow left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

only one followup thing to review, otherwise all addressed.

@fedgiac
fedgiac merged commit 8b87c48 into main Aug 27, 2026
14 checks passed
@fedgiac
fedgiac deleted the refactor-state-account-decoder branch August 27, 2026 10:43
@fedgiac fedgiac mentioned this pull request Aug 27, 2026
fedgiac added a commit that referenced this pull request Aug 27, 2026
Unfortunately, I merged #118 too quickly and I left some fields not
properly renamed.

Interesting that one of the missing renames is in a dev docs. The reason
our dev docs CI didn't catch it is that the bad docs is in the test.
According to Claude (I don't think it's worth investigating further)
there's no stable way to also check the test docs, so I'm going to keep
CI as is and accept that the test docs may be broken.

## How to test

Eyeball the changes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants