|
| 1 | +use anchor_lang::prelude::*; |
| 2 | + |
| 3 | +declare_id!("8GM8KqKaxYb1jEbn5TiqqPqJYsChhjYvfpT2f6KTmUKb"); |
| 4 | + |
| 5 | +#[program] |
| 6 | +pub mod accountloader_realloc { |
| 7 | + use super::*; |
| 8 | + |
| 9 | + /// Create the zero-copy account with the full required footprint |
| 10 | + /// (`DISCRIMINATOR.len() + size_of::<Data>()`). |
| 11 | + pub fn initialize(ctx: Context<Initialize>) -> Result<()> { |
| 12 | + let mut data = ctx.accounts.data.load_init()?; |
| 13 | + data.value = 42; |
| 14 | + Ok(()) |
| 15 | + } |
| 16 | + |
| 17 | + /// Shrink the `AccountLoader` to `new_len` bytes |
| 18 | + pub fn shrink(_ctx: Context<Shrink>, _new_len: u16) -> Result<()> { |
| 19 | + Ok(()) |
| 20 | + } |
| 21 | + |
| 22 | + /// Verify the account stays readable after the shrink. |
| 23 | + pub fn read(ctx: Context<Read>) -> Result<u64> { |
| 24 | + let data = ctx.accounts.data.load()?; |
| 25 | + Ok(data.value) |
| 26 | + } |
| 27 | + |
| 28 | + /// Creates an account as an older program version left it: v1 footprint |
| 29 | + /// with the current discriminator. (In a real upgrade the struct name — |
| 30 | + /// and therefore the discriminator — doesn't change; the V1/V2 split |
| 31 | + /// exists only so both layouts can coexist in this test.) |
| 32 | + pub fn initialize_legacy(ctx: Context<InitializeLegacy>) -> Result<()> { |
| 33 | + let mut data = ctx.accounts.counter.try_borrow_mut_data()?; |
| 34 | + data[..8].copy_from_slice(CounterV2::DISCRIMINATOR); |
| 35 | + let v1 = CounterV1 { value: 42 }; |
| 36 | + data[8..].copy_from_slice(bytemuck::bytes_of(&v1)); |
| 37 | + Ok(()) |
| 38 | + } |
| 39 | + |
| 40 | + /// Grows the legacy account to the v2 footprint via the `realloc` |
| 41 | + /// constraint, then fills the new field from existing v1 data. |
| 42 | + pub fn migrate(ctx: Context<Migrate>) -> Result<()> { |
| 43 | + let mut counter = ctx.accounts.counter.load_mut()?; |
| 44 | + counter.extra = counter.value * 2; |
| 45 | + Ok(()) |
| 46 | + } |
| 47 | + |
| 48 | + pub fn read_extra(ctx: Context<ReadCounter>) -> Result<u64> { |
| 49 | + Ok(ctx.accounts.counter.load()?.extra) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +#[derive(Accounts)] |
| 54 | +pub struct Initialize<'info> { |
| 55 | + #[account(mut)] |
| 56 | + pub authority: Signer<'info>, |
| 57 | + |
| 58 | + #[account( |
| 59 | + init, |
| 60 | + payer = authority, |
| 61 | + seeds = [b"data"], |
| 62 | + bump, |
| 63 | + space = 8 + core::mem::size_of::<Data>(), |
| 64 | + )] |
| 65 | + pub data: AccountLoader<'info, Data>, |
| 66 | + |
| 67 | + pub system_program: Program<'info, System>, |
| 68 | +} |
| 69 | + |
| 70 | +#[derive(Accounts)] |
| 71 | +#[instruction(new_len: u16)] |
| 72 | +pub struct Shrink<'info> { |
| 73 | + #[account(mut)] |
| 74 | + pub authority: Signer<'info>, |
| 75 | + #[account( |
| 76 | + mut, |
| 77 | + seeds = [b"data"], |
| 78 | + bump, |
| 79 | + realloc = new_len as usize, |
| 80 | + realloc::payer = authority, |
| 81 | + realloc::zero = false, |
| 82 | + )] |
| 83 | + pub data: AccountLoader<'info, Data>, |
| 84 | + |
| 85 | + pub system_program: Program<'info, System>, |
| 86 | +} |
| 87 | + |
| 88 | +#[derive(Accounts)] |
| 89 | +pub struct Read<'info> { |
| 90 | + #[account(seeds = [b"data"], bump)] |
| 91 | + pub data: AccountLoader<'info, Data>, |
| 92 | +} |
| 93 | + |
| 94 | +#[derive(Accounts)] |
| 95 | +pub struct InitializeLegacy<'info> { |
| 96 | + #[account(mut)] |
| 97 | + pub authority: Signer<'info>, |
| 98 | + |
| 99 | + /// CHECK: holds the v1 layout; written manually in the handler. |
| 100 | + #[account( |
| 101 | + init, |
| 102 | + payer = authority, |
| 103 | + seeds = [b"legacy"], |
| 104 | + bump, |
| 105 | + space = 8 + core::mem::size_of::<CounterV1>(), |
| 106 | + owner = crate::ID, |
| 107 | + )] |
| 108 | + pub counter: UncheckedAccount<'info>, |
| 109 | + |
| 110 | + pub system_program: Program<'info, System>, |
| 111 | +} |
| 112 | + |
| 113 | +#[derive(Accounts)] |
| 114 | +pub struct Migrate<'info> { |
| 115 | + #[account(mut)] |
| 116 | + pub authority: Signer<'info>, |
| 117 | + |
| 118 | + #[account( |
| 119 | + mut, |
| 120 | + seeds = [b"legacy"], |
| 121 | + bump, |
| 122 | + realloc = 8 + core::mem::size_of::<CounterV2>(), |
| 123 | + realloc::payer = authority, |
| 124 | + realloc::zero = false, |
| 125 | + )] |
| 126 | + pub counter: AccountLoader<'info, CounterV2>, |
| 127 | + |
| 128 | + pub system_program: Program<'info, System>, |
| 129 | +} |
| 130 | + |
| 131 | +#[derive(Accounts)] |
| 132 | +pub struct ReadCounter<'info> { |
| 133 | + #[account(seeds = [b"legacy"], bump)] |
| 134 | + pub counter: AccountLoader<'info, CounterV2>, |
| 135 | +} |
| 136 | + |
| 137 | +#[account(zero_copy)] |
| 138 | +#[repr(C)] |
| 139 | +pub struct Data { |
| 140 | + pub value: u64, |
| 141 | + pub padding: [u8; 64], |
| 142 | +} |
| 143 | + |
| 144 | +/// The original account layout (8-bytes body + 8-bytes discriminator = 16 bytes). |
| 145 | +#[zero_copy] |
| 146 | +pub struct CounterV1 { |
| 147 | + pub value: u64, |
| 148 | +} |
| 149 | + |
| 150 | +/// The upgraded layout: `extra` was added. (16 bytes body + 8 bytes discriminator = 24 bytes) |
| 151 | +#[account(zero_copy)] |
| 152 | +#[repr(C)] |
| 153 | +pub struct CounterV2 { |
| 154 | + pub value: u64, |
| 155 | + pub extra: u64, |
| 156 | +} |
0 commit comments