Huione Docs
White Paper
Developers
Developers
  • Learn
    • Introduction to Huione Chain
    • Getting started with Huione Chain
  • Architecture
    • What is a Huione Chain Cluster?
    • Clusters
      • Huione Chain Clusters
      • RPC Endpoints
      • Benchmark a Cluster
      • Performance Metrics
    • Consensus
      • Synchronization
      • Leader Rotation
      • Fork Generation
      • Managing Forks
      • Turbine Block Propagation
      • Commitment Status
      • Secure Vote Signing
      • Stake Delegation and Rewards
    • Validators
      • Overview
      • TPU
      • TVU
      • Blockstore
      • Gossip Service
      • The Runtime
    • Dex & Swap
      • Trade on dex
      • Trade on swap
  • CLI
    • Command-line Guide
    • Install the Huione Tool Suite
    • Command-line Wallets
      • Command Line Wallets
      • Paper Wallet
      • File System Wallet
      • Support / Troubleshooting
    • Using Huione Chain CLI
    • Connecting to a Cluster
    • Send and Receive Tokens
    • Staking
    • Deploy a Program
    • Offline Transaction Signing
    • Durable Transaction Nonces
    • CLI Usage Reference
  • Developers
    • Get Started
      • Hello World
      • Local development
      • Rust program
    • Core Concepts
      • Accounts
      • Transactions
        • Overview
        • Versioned Transactions
        • Address Lookup Tables
      • Programs
      • Rent
      • Calling between programs
      • Runtime
    • Clients
      • JSON RPC API -1
      • JSON RPC API -2
      • JSON RPC API -3
      • Web3 JavaScript API
      • Web3 API Reference
      • Rust API
    • Writing Programs
      • Overview
      • Developing with Rust
      • Deploying
      • Debugging
      • Program Examples
      • FAQ
    • Native Programs
      • Overview
      • Sysvar Cluster Data
    • Local Development
      • Huione Test Validator
    • Backward Compatibility Policy
    • Anchor Book
      • Introduction
        • What is Anchor
        • Anchor Documentation
        • Prerequisites
      • Getting Started
        • Installation
        • Hello, Anchor!
      • Anchor Programs In-Depth
        • Essentials
          • High-level Overview
          • The Accounts Struct
          • The Program Module
          • Errors
          • Milestone Project - Tic-Tac-Toe
        • Intermediate
          • Cross-Program Invocations
          • PDAs
          • Events
      • Anchor BTS
        • The Discriminator
      • Anchor References
        • Space Reference
        • Javascript Anchor Types Reference
        • CLI Reference
        • Code References
  • Validators
    • Running a Validator
    • Getting Started
      • Validator Requirements
    • Voting Setup
      • Starting a Validator
      • Vote Account Management
      • Staking
      • Monitoring a Validator
      • Publishing Validator Info
      • Failover Setup
      • Troubleshooting
    • Geyser
      • Geyser Plugins
  • Staking
    • Staking on Huione
    • Stake Account Structure
  • Integrations
    • Add Huione to Your Exchange
    • Retrying Transactions
  • Library
    • Introduction
    • Token Program
    • Associated Token Account Program
    • Memo Program
    • Name Service
    • Feature Proposal Program
    • NFT Program
      • Overview
      • Interface
      • Usage Guidelines
        • Create a new NFT-Mint
        • Cast NFT
        • Transfer an NFT
        • Change account status
        • Permission settings
        • Query Interface
        • Continuous casting
        • Change the Mint attribute
      • Operation Overview
        • Create a new NFT-Mint
        • Transfer NFT
        • Destroy
        • Freeze NFT accounts
        • Update
    • Huione multi-sign program
      • Overview
      • Interface
      • Usage Guidelines
        • Create a multi-signature account
        • Create a proposal account
        • Vote proposal
        • Verify Proposal
        • Add-singer
        • Remove-signer
      • Operation Overview
        • Create a multi-signature account
        • Create a proposal account
        • Vote
        • Verify
        • Add-singer
        • Remove-signer
Powered by GitBook
On this page
  • Space Reference
  • Example

Was this helpful?

  1. Developers
  2. Anchor Book
  3. Anchor References

Space Reference

Space Reference

This reference tells you how much space you should allocate for an account. This only applies to accounts that don't use zero-copy. zero-copy uses repr(C) with a pointer cast, so there the C layout applies.

In addition to the space for the account data, you have to add 8 to the space constraint for Anchor's internal discriminator (see the example).

Types
Space in bytes
Details/Example

bool

1

would only require 1 bit but still uses 1 byte

u8/i8

1

u16/i16

2

u32/i32

4

u64/i64

8

u128/i128

16

[T;amount]

space(T) * amount

e.g. space([u16;32]) = 2 * 32 = 64

Pubkey

32

Vec<T>

4 + (space(T) * amount)

Account size is fixed so account should be initialized with sufficient space from the beginning

String

4 + length of string in bytes

Account size is fixed so account should be initialized with sufficient space from the beginning

Option<T>

1 + (space(T))

Enum

1 + Largest Variant Size

e.g. Enum { A, B { val: u8 }, C { val: u16 } } -> 1 + space(u16) = 3

f32

4

serialization will fail for NaN

f64

8

serialization will fail for NaN

Example

#[account]
pub struct MyData {
    pub val: u16,
    pub state: GameState,
    pub players: Vec<Pubkey> // we want to support up to 10 players
}

impl MyData {
    pub const MAX_SIZE: usize = 2 + (1 + 32) + (4 + 10 * 32);
}

#[derive(AnchorSerialize, AnchorDeserialize, Clone, PartialEq, Eq)]
pub enum GameState {
    Active,
    Tie,
    Won { winner: Pubkey },
}

#[derive(Accounts)]
pub struct InitializeMyData<'info> {
    // Note that we have to add 8 to the space for the internal huione-anchor
    #[account(init, payer = signer, space = 8 + MyData::MAX_SIZE)]
    pub acc: Account<'info, MyData>,
    pub signer: Signer<'info>,
    pub system_program: Program<'info, System>
}
PreviousAnchor ReferencesNextJavascript Anchor Types Reference

Last updated 7 months ago

Was this helpful?