magicblock-account 4.3.1

Solana Account type
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
//! Copy-on-write account data with zero-copy access to aligned external storage.
//!
//! `borrowed` defines the raw buffer layout and `owned` holds the heap-backed form.
#![allow(unsafe_op_in_unsafe_fn)]

mod borrowed;
mod owned;

pub use borrowed::BorrowedAccount;
pub use owned::{AccountBuilder, OwnedAccount};

use crate::{Account, ReadableAccount, WritableAccount, patch::AccountPatchError};
use solana_clock::{Epoch, Slot};
use solana_pubkey::Pubkey;
use std::{
    cell::RefCell,
    ops::{Deref, DerefMut},
    rc::Rc,
    sync::Arc,
};

use CoWAccount::*;

/// Borrowed buffers must be aligned to this many bytes.
pub const ALIGNMENT: usize = 8;
/// Bytes in one storage unit.
pub const STORAGE_UNIT: usize = size_of::<StorageUnit>();
/// Minimum addressable storage unit for borrowed account images.
#[repr(C)]
#[derive(Clone, Copy, Default)]
pub struct StorageUnit(pub u64);

/// Shared account data that borrows directly from an aligned external buffer
/// until a write requires promotion to owned heap storage.
///
/// Higher layers use `mutable()` to enforce transaction write permissions.
#[cfg_attr(feature = "serde", derive(serde::Deserialize), serde(from = "Account"))]
#[derive(Clone, Default)]
pub struct AccountSharedData {
    /// Backing storage, borrowed until promotion or direct construction.
    pub(crate) cow: CoWAccount,
    /// Fields changed through the writable APIs.
    pub(crate) dirty: DirtyMarkers,
}

/// Core account state shared by the borrowed and owned representations.
#[repr(C)]
#[derive(Clone, Copy, Default, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct AccountCore {
    /// Lamport balance.
    pub(crate) lamports: u64,
    /// Account owner.
    pub(crate) owner: Pubkey,
    /// On-chain slot, at which the account was cloned.
    pub(crate) slot: Slot,
    /// Mutually exclusive mode of existence for the account.
    pub(crate) mode: AccountMode,
    /// Account state modifier flags.
    pub(crate) flags: StateFlags,
    /// Reserved bytes that make the serialized representation deterministic.
    _padding: [u8; 6],
}

impl Deref for AccountSharedData {
    type Target = AccountCore;

    fn deref(&self) -> &Self::Target {
        match &self.cow {
            Borrowed(account) => {
                // SAFETY: `BorrowedAccount` owns the invariant that `core` points at
                // a live `AccountCore` inside the borrowed buffer.
                unsafe { account.core.as_ref() }
            }
            Owned(account) => &account.core,
        }
    }
}

impl DerefMut for AccountSharedData {
    fn deref_mut(&mut self) -> &mut Self::Target {
        match &mut self.cow {
            Borrowed(account) => {
                // SAFETY: `&mut self` guarantees unique access to the borrowed image.
                unsafe { account.core.as_mut() }
            }
            Owned(account) => &mut account.core,
        }
    }
}

impl PartialEq for AccountSharedData {
    fn eq(&self, other: &Self) -> bool {
        self.deref() == other.deref() && self.cow.data() == other.cow.data()
    }
}

impl Eq for AccountSharedData {}

impl PartialEq<OwnedAccount> for AccountSharedData {
    fn eq(&self, other: &OwnedAccount) -> bool {
        self.deref() == &other.core && self.cow.data() == other.data.as_slice()
    }
}

impl AccountSharedData {
    /// Returns a reference to the inner copy-on-write representation.
    pub fn cow(&self) -> &CoWAccount {
        &self.cow
    }

    /// Returns mutable access to the inner copy-on-write representation.
    pub fn cow_mut(&mut self) -> &mut CoWAccount {
        &mut self.cow
    }

    /// Returns the account's on-chain slot.
    pub fn slot(&self) -> Slot {
        self.slot
    }

    /// Copies a clean borrowed image into the shadow buffer before mutation.
    pub fn translate(&mut self) {
        if self.dirty() {
            return;
        }
        if let Borrowed(ref mut acc) = self.cow {
            // SAFETY: this runs before the first dirty marker, so the borrowed
            // view still points at the active image selected by `init`.
            unsafe { acc.translate() };
        }
    }

    /// Returns an owned copy of the current account state.
    pub fn owned(&self) -> OwnedAccount {
        match self.cow() {
            Borrowed(a) => a.into(),
            Owned(a) => a.clone(),
        }
    }

    /// Returns whether the current transaction may leave the account modified.
    ///
    /// Mutable modes are always accepted. `Transient` and `Closed` are accepted
    /// only when this transaction performed the corresponding mode transition.
    /// This is a transaction-final writeback predicate, not permission for a new
    /// program mutation; instruction checks must use [`AccountMode::mutable`].
    pub fn mutable(&self) -> bool {
        self.mode.mutable()
            || matches!(self.mode, AccountMode::Transient | AccountMode::Closed)
                && self.dirty.contains(DirtyMarkers::MODE)
    }

    /// Returns the account's exact lifecycle mode.
    pub fn mode(&self) -> AccountMode {
        self.mode
    }

    /// Returns `true` when the account is in `mode`.
    pub fn is(&self, mode: AccountMode) -> bool {
        self.mode == mode
    }

    /// Returns the account modifier flags.
    pub fn flags(&self) -> &StateFlags {
        &self.flags
    }

    /// Returns the dirty-field markers.
    pub fn markers(&self) -> &DirtyMarkers {
        &self.dirty
    }

    /// Marks the data buffer as modified.
    pub(crate) fn mark_data_dirty(&mut self) {
        self.dirty.insert(DirtyMarkers::DATA);
    }

    /// Returns `true` when the owned buffer has more than one strong reference.
    pub fn is_shared(&self) -> bool {
        self.cow.is_shared()
    }

    /// Returns `true` if any field has been modified.
    pub fn dirty(&self) -> bool {
        self.dirty.intersects(DirtyMarkers::all())
    }

    /// Returns the current data capacity.
    pub fn capacity(&self) -> usize {
        self.cow.capacity()
    }

    /// Returns a shared owned copy of the current data bytes.
    pub fn data_clone(&self) -> Arc<Vec<u8>> {
        self.cow.data_clone()
    }

    /// Resizes the account data.
    pub fn resize(&mut self, len: usize, val: u8) {
        self.translate();
        self.mark_data_dirty();
        self.cow.resize(len, val);
    }

    /// Appends bytes to the account data.
    pub fn extend_from_slice(&mut self, data: &[u8]) {
        self.translate();
        self.mark_data_dirty();
        self.cow.extend_from_slice(data);
    }

    /// Replaces the account data with the provided bytes.
    pub fn set_data_from_slice(&mut self, data: &[u8]) {
        self.translate();
        self.mark_data_dirty();
        self.cow.set_data_from_slice(data);
    }

    /// Applies mode and slot as one validated lifecycle transition.
    ///
    /// Mode pairs determine whether slots may stay equal or must advance.
    /// Authoritative modes cannot be reapplied, even at a newer slot.
    /// Validation precedes translation, so an error leaves account state and
    /// dirty markers unchanged.
    pub fn set_lifecycle(
        &mut self,
        mode: AccountMode,
        slot: Slot,
    ) -> Result<(), AccountPatchError> {
        self.mode.validate_transition(mode, self.slot, slot)?;
        self.translate();
        if self.mode != mode {
            self.dirty.insert(DirtyMarkers::MODE);
            self.mode = mode;
        }
        self.dirty.insert(DirtyMarkers::SLOT);
        self.slot = slot;
        Ok(())
    }

    /// Writes bytes at `offset`, extending and zero-filling as needed.
    pub(crate) fn set_data_at(&mut self, offset: usize, data: &[u8]) {
        self.translate();
        self.mark_data_dirty();
        let len = self.data().len();
        if offset > len {
            // Grow to `offset`, zero-filling the gap; the write below then
            // appends `data` past it via `extend_from_slice`.
            self.resize(offset, 0);
        }

        // Write the overlap in place, then append any remaining tail. This
        // keeps borrowed buffers on the fast path when the write fits.
        let n = self.data().len().saturating_sub(offset).min(data.len());
        self.data_as_mut_slice()[offset..offset + n].copy_from_slice(&data[..n]);
        self.extend_from_slice(&data[n..]);
    }

    /// Replaces all state flags and marks them dirty when the value changes.
    pub fn set_flags(&mut self, flags: StateFlags) {
        if self.flags == flags {
            return;
        }
        self.translate();
        self.dirty.set(DirtyMarkers::FLAGS, true);
        self.flags = flags;
    }

    /// Creates a new owned shared-data account with zero-filled data.
    pub fn new(lamports: u64, space: usize, owner: &Pubkey) -> Self {
        AccountBuilder::default()
            .lamports(lamports)
            .data(vec![0; space])
            .owner(*owner)
            .build()
    }
    /// Creates a new shared-data account wrapped in a `RefCell`.
    pub fn new_ref(lamports: u64, space: usize, owner: &Pubkey) -> Rc<RefCell<Self>> {
        Rc::new(RefCell::new(Self::new(lamports, space, owner)))
    }

    /// Creates a new account with serialized data.
    #[cfg(feature = "bincode")]
    pub fn new_data<T: serde::Serialize>(
        lamports: u64,
        state: &T,
        owner: &Pubkey,
    ) -> Result<Self, bincode::Error> {
        let data = bincode::serialize(state)?;
        Ok(Self::create_from_existing_shared_data(
            lamports,
            Arc::new(data),
            *owner,
            false,
            Epoch::default(),
        ))
    }

    /// Creates a new serialized account wrapped in a `RefCell`.
    #[cfg(feature = "bincode")]
    pub fn new_ref_data<T: serde::Serialize>(
        lamports: u64,
        state: &T,
        owner: &Pubkey,
    ) -> Result<RefCell<Self>, bincode::Error> {
        Self::new_data(lamports, state, owner).map(RefCell::new)
    }

    /// Creates a new fixed-size account with serialized data.
    #[cfg(feature = "bincode")]
    pub fn new_data_with_space<T: serde::Serialize>(
        lamports: u64,
        state: &T,
        space: usize,
        owner: &Pubkey,
    ) -> Result<Self, bincode::Error> {
        let mut account = Self::new(lamports, space, owner);
        crate::codec::serialize_data(&mut account, state)?;
        Ok(account)
    }

    /// Creates a new fixed-size serialized account wrapped in a `RefCell`.
    #[cfg(feature = "bincode")]
    pub fn new_ref_data_with_space<T: serde::Serialize>(
        lamports: u64,
        state: &T,
        space: usize,
        owner: &Pubkey,
    ) -> Result<RefCell<Self>, bincode::Error> {
        Self::new_data_with_space(lamports, state, space, owner).map(RefCell::new)
    }

    /// Creates a new shared-data account.
    ///
    /// `rent_epoch` is ignored because this type does not store it.
    pub fn new_rent_epoch(lamports: u64, space: usize, owner: &Pubkey, _: Epoch) -> Self {
        Self::new(lamports, space, owner)
    }

    /// Deserializes the account data as `T`.
    #[cfg(feature = "bincode")]
    pub fn deserialize_data<T: serde::de::DeserializeOwned>(&self) -> Result<T, bincode::Error> {
        crate::codec::deserialize_data(self)
    }

    /// Serializes `state` into the existing account data buffer.
    #[cfg(feature = "bincode")]
    pub fn serialize_data<T: serde::Serialize>(&mut self, state: &T) -> Result<(), bincode::Error> {
        crate::codec::serialize_data(self, state)
    }

    /// Creates an owned shared-data account from existing shared bytes.
    ///
    /// `rent_epoch` is ignored because this type does not store it.
    pub fn create_from_existing_shared_data(
        lamports: u64,
        data: Arc<Vec<u8>>,
        owner: Pubkey,
        executable: bool,
        _: Epoch,
    ) -> Self {
        AccountBuilder::default()
            .lamports(lamports)
            .data(data)
            .owner(owner)
            .executable(executable)
            .build()
    }
}

bitflags::bitflags! {
    /// Account state modifier flags.
    #[derive(Clone, Copy, Default, PartialEq, Eq, Debug)]
    #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
    pub struct StateFlags: u8 {
        /// Executable account data.
        const EXECUTABLE = 1 << 0;
    }

    /// Bits that record which fields changed through `AccountSharedData`.
    #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
    pub struct DirtyMarkers: u8 {
        /// Owner changed.
        const OWNER    = 1 << 0;
        /// Lamports changed.
        const LAMPORTS = 1 << 1;
        /// Mode changed.
        const MODE     = 1 << 2;
        /// State flags changed.
        const FLAGS    = 1 << 3;
        /// Slot changed.
        const SLOT     = 1 << 4;
        /// Data bytes changed.
        const DATA     = 1 << 5;
    }
}

/// `wincode` codec for `StateFlags`, which is a `bitflags!` newtype and so
/// cannot use the derives. Routed through `bincode`/`serde`, which encodes the
/// single bits byte identically to a plain `u8`.
#[cfg(feature = "wincode")]
const _: () = {
    use core::mem::MaybeUninit;
    use wincode::{
        ReadError, ReadResult, SchemaRead, SchemaWrite, TypeMeta, WriteError, WriteResult,
        config::ConfigCore,
        io::{Reader, Writer},
    };

    // SAFETY: encodes exactly one byte; matches `TYPE_META` / `size_of`.
    unsafe impl<C: ConfigCore> SchemaWrite<C> for StateFlags {
        type Src = StateFlags;
        const TYPE_META: TypeMeta = TypeMeta::Static { size: 1, zero_copy: false };

        fn size_of(_: &Self::Src) -> WriteResult<usize> {
            Ok(1)
        }

        fn write(mut writer: impl Writer, src: &Self::Src) -> WriteResult<()> {
            let bytes = bincode::serialize(src).map_err(|_| WriteError::Custom("StateFlags"))?;
            writer.write(&bytes)?;
            Ok(())
        }
    }

    // SAFETY: consumes exactly one byte; matches `TYPE_META`.
    unsafe impl<'de, C: ConfigCore> SchemaRead<'de, C> for StateFlags {
        type Dst = StateFlags;
        const TYPE_META: TypeMeta = TypeMeta::Static { size: 1, zero_copy: false };

        fn read(mut reader: impl Reader<'de>, dst: &mut MaybeUninit<Self::Dst>) -> ReadResult<()> {
            let bytes = reader.take_array::<1>()?;
            dst.write(bincode::deserialize(&bytes).map_err(|_| ReadError::Custom("StateFlags"))?);
            Ok(())
        }
    }
};

/// Backing storage for `AccountSharedData`.
#[derive(PartialEq, Eq)]
pub enum CoWAccount {
    /// Borrowed image, a view into static backing buffer.
    Borrowed(BorrowedAccount),
    /// Heap-owned image.
    Owned(OwnedAccount),
}

impl Clone for CoWAccount {
    fn clone(&self) -> Self {
        match self {
            Borrowed(acc) => Self::Owned(acc.into()),
            Owned(acc) => Self::Owned(acc.clone()),
        }
    }
}

impl CoWAccount {
    /// Promotes borrowed storage to the owned form.
    pub(crate) fn promote(&mut self) {
        let Self::Borrowed(account) = self else {
            return;
        };
        *self = Self::Owned(account.deref().into());
    }

    /// Returns the current data slice.
    pub(crate) fn data(&self) -> &[u8] {
        match self {
            Self::Borrowed(account) => &account.data,
            Self::Owned(account) => &account.data,
        }
    }

    /// Returns `true` when the heap buffer has multiple owners.
    pub(crate) fn is_shared(&self) -> bool {
        match self {
            Self::Borrowed(_) => false,
            Self::Owned(account) => Arc::strong_count(&account.data) > 1,
        }
    }

    /// Returns the current data capacity.
    pub(crate) fn capacity(&self) -> usize {
        match self {
            Self::Borrowed(account) => account.data.capacity(),
            Self::Owned(account) => account.data.capacity(),
        }
    }

    /// Returns a shared owned copy of the current data bytes.
    pub(crate) fn data_clone(&self) -> Arc<Vec<u8>> {
        match self {
            Self::Borrowed(account) => Arc::new(account.data.to_vec()),
            Self::Owned(account) => Arc::clone(&account.data),
        }
    }

    /// Returns mutable data, promoting borrowed storage only when needed.
    pub(crate) fn data_mut(&mut self) -> &mut [u8] {
        match self {
            Self::Borrowed(account) => &mut account.data,
            Self::Owned(account) => Arc::<Vec<u8>>::make_mut(&mut account.data).as_mut_slice(),
        }
    }

    /// Reserves additional space for the account data.
    pub fn reserve(&mut self, additional: usize) {
        if let Self::Borrowed(a) = self
            && a.data.spare() >= additional
        {
            return;
        }
        self.promote();
        if let Self::Owned(account) = self {
            Arc::make_mut(&mut account.data).reserve(additional);
        }
    }

    /// Resizes the account data.
    pub(crate) fn resize(&mut self, len: usize, val: u8) {
        if let Self::Borrowed(a) = self
            && len <= a.data.capacity()
        {
            // SAFETY: this stays in the borrowed image only while the resized
            // range fits within the borrowed capacity.
            unsafe { a.data.resize(len, val) };
            return;
        }

        self.promote();
        if let Self::Owned(account) = self {
            Arc::make_mut(&mut account.data).resize(len, val);
        }
    }

    /// Appends bytes to the account data.
    pub(crate) fn extend_from_slice(&mut self, data: &[u8]) {
        self.reserve(data.len());

        match self {
            Self::Borrowed(account) => {
                // SAFETY: `reserve` keeps the borrowed image only when the appended
                // bytes fit in the remaining borrowed capacity.
                unsafe { account.data.extend(data) };
            }
            Self::Owned(account) => Arc::make_mut(&mut account.data).extend_from_slice(data),
        }
    }

    /// Replaces the account data with the provided bytes.
    pub(crate) fn set_data_from_slice(&mut self, data: &[u8]) {
        let additional = data.len().saturating_sub(self.data().len());
        self.reserve(additional);

        match self {
            Self::Borrowed(account) => {
                // SAFETY: `reserve` keeps the borrowed image only when the
                // replacement bytes fit in the borrowed capacity.
                unsafe { account.data.set(data) };
            }
            Self::Owned(account) => {
                let data_buf = Arc::make_mut(&mut account.data);
                data_buf.clear();
                data_buf.extend_from_slice(data);
            }
        }
    }
}

/// Mutually exclusive modes an account can occupy in the ephemeral rollup (ER).
#[derive(Clone, Copy, PartialEq, Eq, Default, Debug)]
#[repr(u8)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[cfg_attr(feature = "wincode", derive(wincode::SchemaRead, wincode::SchemaWrite))]
pub enum AccountMode {
    /// Empty account (not found on chain) used to avoid frequent chain syncs.
    #[default]
    Placeholder = 0,
    /// Not writable by users (exists on chain, but not delegated)
    ReadOnly,
    /// Internal account used for sysvars, features, and precompiles.
    System,
    /// Account delegated to the current ER node instance.
    Delegated,
    /// Account that exists only inside the ER.
    Ephemeral,
    /// Temporary state during mode transitions (e.g. delegated -> readonly).
    Transient,
    /// Closed account that should be removed from storage.
    Closed = 255,
}

impl AccountMode {
    /// Returns whether a privileged lifecycle operation may apply `to` at
    /// `to_slot`, including same-mode refreshes and slot ordering.
    ///
    /// Only placeholder, read-only, and system accounts permit same-mode
    /// refreshes, and those require a newer slot. Slots may never regress.
    pub fn allows_transition(self, to: Self, from_slot: Slot, to_slot: Slot) -> bool {
        self.validate_transition(to, from_slot, to_slot).is_ok()
    }

    fn validate_transition(
        self,
        to: Self,
        from_slot: Slot,
        to_slot: Slot,
    ) -> Result<(), AccountPatchError> {
        use AccountMode::*;
        let valid_slot = match (self, to) {
            (Placeholder, ReadOnly | System | Delegated | Ephemeral | Closed)
            | (ReadOnly, Delegated | Ephemeral | Closed)
            | (Delegated, Transient)
            | (Transient, ReadOnly | Placeholder)
            | (Ephemeral, Closed) => to_slot >= from_slot,
            // Refreshes, observed disappearance, and redelegation need newer evidence.
            (Placeholder, Placeholder)
            | (ReadOnly, ReadOnly | Placeholder)
            | (System, System)
            | (Transient, Delegated) => to_slot > from_slot,
            _ => return Err(AccountPatchError::InvalidModeTransition { from: self, to }),
        };
        if !valid_slot {
            return Err(AccountPatchError::InvalidSlotTransition { from: from_slot, to: to_slot });
        }
        Ok(())
    }

    /// Returns `true` for modes that may be mutated by user programs.
    pub fn mutable(&self) -> bool {
        use AccountMode::*;
        matches!(self, Delegated | Ephemeral)
    }

    /// Returns `true` for modes whose state is authoritative in this engine.
    pub fn authoritative(&self) -> bool {
        use AccountMode::*;
        matches!(self, Delegated | Ephemeral | Transient)
    }
}

/// Read wrapper that retries borrowed account reads when a concurrent publish
/// changes the backing image.
pub struct AccountSeqLock {
    account: AccountSharedData,
    sequence: Option<u32>,
}

impl AccountSeqLock {
    /// Creates a read lock with the sequence that matches the current account view.
    pub fn new(account: AccountSharedData) -> Self {
        let mut sequence = None;
        if let Borrowed(ref acc) = account.cow {
            sequence.replace(acc.version);
        }
        Self { account, sequence }
    }

    /// Runs `reader` against a stable account image.
    ///
    /// For borrowed accounts, the sequence is checked after the read. If a
    /// writer published a new image meanwhile, the account view is reset to that
    /// active image and the read is retried.
    pub fn read<F, R>(&mut self, reader: F) -> R
    where
        F: Fn(&AccountSharedData) -> R,
    {
        loop {
            // sequence is always present for borrowed accounts
            let pre = self.sequence.unwrap_or_default();
            let result = reader(&self.account);
            match self.account.cow_mut() {
                Borrowed(acc) => {
                    let post = acc.sequence();
                    if pre == post {
                        return result;
                    }
                    // SAFETY: a changed sequence means the active image may have
                    // moved, so the borrowed view must be repointed before retrying.
                    unsafe { acc.reset() };
                    self.sequence = Some(acc.version);
                }
                Owned(_) => return result,
            }
        }
    }
}

impl Default for CoWAccount {
    fn default() -> Self {
        Self::Owned(OwnedAccount::default())
    }
}

/// Wraps an owned account in `AccountSharedData`.
impl From<OwnedAccount> for AccountSharedData {
    fn from(value: OwnedAccount) -> Self {
        Self {
            cow: Owned(value),
            dirty: DirtyMarkers::default(),
        }
    }
}

/// Wraps a borrowed account in `AccountSharedData`.
impl From<BorrowedAccount> for AccountSharedData {
    fn from(value: BorrowedAccount) -> Self {
        Self {
            cow: Borrowed(value),
            dirty: DirtyMarkers::default(),
        }
    }
}

/// Converts a plain `Account` into shared data.
impl From<Account> for AccountSharedData {
    fn from(value: Account) -> Self {
        AccountBuilder::default()
            .lamports(value.lamports)
            .data(value.data)
            .owner(value.owner)
            .executable(value.executable)
            .build()
    }
}

/// We only access AccountSharedData via transaction lock in the
/// execution layer or with a SeqLock semantics outside of execution
unsafe impl Sync for AccountSharedData {}