Skip to main content

StateAccount

Struct StateAccount 

Source
pub struct StateAccount<T>(/* private fields */);
Expand description

A zero-copy accessor over a settlement state account’s canonical byte representation: the discriminator byte followed by the current holder of each Role.

T is the borrow backing it, anything that dereferences to the account’s bytes: &[u8] grant read access; &mut [u8] grants write access.

Implementations§

Source§

impl<T: Deref<Target = [u8]>> StateAccount<T>

Source

pub fn attach(bytes: T) -> Result<Self, ProgramError>

Wrap an account’s bytes, checking they begin with the settlement-state discriminator and are at least a full header long. Every accessor relies on that guarantee not to panic.

Source

pub fn authority(&self, role: Role) -> Pubkey

Current holder of role.

Locate solver in the sorted solver list, mirroring slice::binary_search: Ok(index) if present, Err(index) with the slot it would occupy otherwise. The list is sorted by address bytes.

Source

pub fn is_solver(&self, solver: &Pubkey) -> bool

Whether solver is in the solver list.

Source

pub fn solvers(&self) -> impl Iterator<Item = Pubkey> + '_

The stored solvers, in order (sorted ascending by address).

Source

pub fn grown_len(&self) -> Result<usize, ProgramError>

The account’s data length after growing it by one solver slot: the size it must be resized to before insert_solver can fill that new slot.

Returns ProgramError::ArithmeticOverflow if that length overflows usize, which a caller handling data from a real account can treat as unreachable: the runtime caps account data at MAX_PERMITTED_DATA_LENGTH.

Source§

impl<'a> StateAccount<Ref<'a, [u8]>>

Source

pub fn from_account(account: &'a AccountView) -> Result<Self, ProgramError>

Source§

impl<T: DerefMut<Target = [u8]>> StateAccount<T>

Source

pub fn initialize(bytes: T, args: &StateInitArgs) -> Result<Self, ProgramError>

Stamp a fresh header (discriminator + role holders) into a zeroed account and return the accessor over it.

The generated data is a full header long, which is needed for the read accessors not to panic.

Source

pub fn set_authority(&mut self, role: Role, new: &Pubkey)

Set role’s holder to new in place.

Source

pub fn insert_solver(&mut self, solver: &Pubkey) -> Result<(), ProgramError>

Insert solver into the sorted solver list, or fail with SettlementError::SolverAlreadyExists if it is already stored.

The account must already be grown by one solver slot (grown_len): the trailing slot is spare capacity for the new entry, so the live list is every entry but that last one. The solver is placed in sorted order by shifting all entries and inserting the new solver in the initial slot. An error is returned if the solver is already included.

Source

pub fn remove_solver(&mut self, solver: &Pubkey) -> Result<usize, ProgramError>

Remove solver from the sorted solver list, or fail with SettlementError::SolverNotFound if it isn’t stored. Returns the length the account must be resized down to.

The entries after the removed one are shifted one slot left to close the gap; the now-stale trailing slot is left in place for the caller to drop by resizing the account down to the returned length.

Auto Trait Implementations§

§

impl<T> Freeze for StateAccount<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for StateAccount<T>
where T: RefUnwindSafe,

§

impl<T> Send for StateAccount<T>
where T: Send,

§

impl<T> Sync for StateAccount<T>
where T: Sync,

§

impl<T> Unpin for StateAccount<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for StateAccount<T>
where T: UnsafeUnpin,

§

impl<T> UnwindSafe for StateAccount<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.