Account: a Memory region

The solana term for a memory region is “account”. Some programs own thousands of independent accounts.

Programs own accounts, aka the owner of accounts.

Transactions and Accounts

You can make a program read and write data by sending transactions. Programs provide endpoints that can be called via transactions (In reality it’s a bit more complex than that but frameworks like Anchor abstract away this complexity). A function signature usually takes the following arguments:

  • the accounts that the program may read from and write to during this transaction.
  • additional data specific to the function

The first point means that even if in theory the program may read and write to a large part of the global heap, in the context of a transaction, it may only read from and write to the specific regions specified in the arguments of the transaction.

Accounts Only Store Bytes

Unlike other blockchain system, Solana didn’t provide data (de)serialization. So data in accounts are just raw bytes without any structure information, check the code in break:

entrypoint!(process_instruction);
fn process_instruction<'a>(
    _program_id: &Pubkey,
    accounts: &'a [AccountInfo<'a>],
    instruction_data: &[u8],
) -> ProgramResult {
    // Assume a writable account is at index 0
    let mut account_data = accounts[0].try_borrow_mut_data()?;

    // xor with the account data using byte and bit from ix data
    let index = u16::from_be_bytes([instruction_data[0], instruction_data[1]]);
    let byte = index >> 3;
    let bit = (index & 0x7) as u8;
    account_data[byte as usize] ^= 1 << (7 - bit);

    Ok(())
}

Let’s check the signature of AccountInfo:

pub struct AccountInfo<'a> {
    pub key: &'a Pubkey,
    pub is_signer: bool,
    pub is_writable: bool,
    pub lamports: Rc<RefCell<&'a mut u64>>,
    pub data: Rc<RefCell<&'a mut [u8]>>,
    pub owner: &'a Pubkey,
    pub executable: bool,
    pub rent_epoch: Epoch,
}

Framework like anchor provided (de)serialization of accounts and instruction data.

This also means accounts in Solana is just data, your private key just prove you own that data.