Introducing a new way to review your PRs.

Read the announcement

solana-foundation / solana-go

Go SDK library and RPC client for the Solana Blockchain

View on GitHub

26 documents · Updated Nov 16, 2026

docs / account-and-sysvar-codecs

Account and sysvar codecs

Account and sysvar codecs turn validator-owned binary state into typed SDK values and back again. They let transaction and RPC workflows work with account balances, owners, rent state, cluster time, effective slot timing, epoch data, slot history, stake history, and fee-related state without treating the account data as an unstructured byte buffer. See Transaction lifecycle for how these values relate to transaction construction and legacy and versioned transactions for the transaction wire formats that must not be confused with sysvar encoding.

Account state

On-chain account representation

Account is the plain binary form of an on-chain account. It carries the lamport balance, program-owned data, owner public key, executable state, and next rent epoch. This form is for validator-style binary state and fixtures. It is not the JSON-RPC account response shape.

The account codec keeps the field order and value rules used by the Rust account type. Empty data decodes as nil, the decoded data does not share storage with the input buffer, and extra bytes after one account are allowed. This makes the type useful both for one account value and for sequential account values in a stream.

References: account.go• L193-233 account.go• L254-329

Account decoding flow

The decoding flow starts with account bytes from an RPC response or another binary source. DecodeAccount returns a typed account or an error when the input is too short, declares data beyond the available bytes, or contains an invalid executable flag.

The diagram shows the boundary between account retrieval and typed state use.

flowchart LR
    A[Account data bytes] --> B[DecodeAccount]
    B -->|valid account prefix| C[Account state]
    B -->|invalid or truncated data| D[Decode error]
    C --> E[Balance, owner, program data, rent epoch]

References: account.go• L235-329 account_test.go• L244-339

Sysvar account model

Sysvar identities and typed values

A sysvar is a validator-maintained account that exposes cluster state to programs and SDK clients. The root package defines the canonical public keys, and the sysvar package re-exports IDs beside the matching data types so a caller can fetch one account and decode it with a single package. Current sysvar IDs cover the active typed values, while the deprecated fee-related IDs remain available for compatibility.

The SDK exposes these sysvar roles:

Sysvar familyInformation providedTyped value
Clock and epochCurrent slot, timestamps, epoch boundaries, leader schedule timing, and the schedule used for slot-time changesClock, EpochSchedule
RewardsEpoch reward distribution stateEpochRewards
RentRent rate, exemption threshold, and burn shareRent
Slot stateRecent slot hashes and the presence of rooted slots in a rolling windowSlotHashes, SlotHistory
Stake stateStake activation and deactivation history by epochStakeHistory
Restart stateThe last cluster restart slotLastRestartSlot
Legacy fee statePer-signature fee information and recent blockhash fee recordsFees, RecentBlockhashes

The account address identifies the sysvar to fetch. The decoder identifies the expected binary shape, so callers must keep the address and decoder paired.

References: sysvar.go• L17-77 sysvar/ids.go• L19-40

Effective slot duration

The effective slot duration is the cluster slot time after feature gates take effect. A feature gate is a cluster setting that becomes active at a recorded slot. MsPerSlotAt combines the activation slots with EpochSchedule and applies each change at the first slot of the next epoch. MsPerSlotInEpoch provides the same result for a whole epoch because the duration does not change within that epoch.

The diagram shows how feature account state becomes the slot duration used for time-related calculations.

flowchart LR
    A[Feature gate accounts] --> B[FeatureSet activation slots]
    C[EpochSchedule] --> D[Epoch boundary]
    B --> D
    D --> E[Effective slot duration]
    F[Slot or epoch] --> E

The duration starts at the default 400 milliseconds. Supported reductions move it to 350, 300, 250, or 200 milliseconds. The result uses the shortest gate that is effective at the slot, so a later activation cannot increase the duration. An invalid zero-value epoch schedule and arithmetic overflow use the default duration rather than claiming a reduction.

References: sysvar/clock.go• L26-45 sysvar/slot_time.go• L25-85 feature_gate.go• L25-40 feature_gate.go• L101-115 sysvar/epoch_schedule.go• L76-117 sysvar/slot_time_test.go• L10-92

Sysvar decoding flow

Each supported sysvar follows the same client path: fetch the account by its known ID, obtain its binary data, and call the decoder for that sysvar type. The result is a value with domain methods for time, epoch, rent, lookup, or reward work.

The diagram shows the common path and the point at which a type-specific decoder takes over.

flowchart LR
    A[Known sysvar ID] --> B[Sysvar account data]
    B --> C[Binary data]
    C --> D[DecodeClock or another Decode helper]
    D --> E[Typed sysvar value]
    E --> F[Cluster-state calculation or transaction decision]

References: sysvar/ids.go• L19-39 sysvar/clock.go• L109-116 sysvar/rent.go• L158-169

Binary codec contract

Shared wire format

Sysvar account data uses standard bincode-style encoding. Integer fields use little-endian order. A variable-length list uses a little-endian u64 length prefix followed by its elements. This is different from the compact-u16 or short-vector encoding used by transaction wire formats.

Every typed sysvar provides binary marshal and unmarshal operations plus a Decode<Type> helper. Fixed-size sysvars publish size constants. List-backed sysvars publish entry limits and the expected fixed account size used by the on-chain record.

References: sysvar/sysvar_codec.go• L17-39 sysvar/clock.go• L23-24 sysvar/slot_hashes.go• L25-34 sysvar/stake_history.go• L24-34

Collection invariants

History values are more than decoded lists. Their order and window limits support the lookup methods that callers use after decoding.

  • SlotHashes keeps the newest slot first and retains at most 512 entries.
  • StakeHistory keeps the newest epoch first and retains at most 512 entries.
  • SlotHistory tracks one million recent slot positions in a bit vector. Its checks distinguish a found slot from a missing, too-old, or future slot.
  • Decoding normalizes the order of slot hashes and stake history before lookup. Adding entries also preserves the order and limit.

These rules let callers use Get, Position, and Check without rebuilding the validator's ordering model.

References: sysvar/slot_hashes.go• L42-93 sysvar/stake_history.go• L52-81 sysvar/slot_history.go• L64-158

Codec safety boundary

Decoders return errors for incomplete data and invalid structural values. They also avoid trusting a declared list or account-data length before reading the available bytes. The account decoder accepts trailing bytes to match the upstream account behavior, while a caller that needs a complete record must track the consumed prefix or validate the surrounding container.

The codec tests use fixed byte vectors, round trips, malformed inputs, and fuzz input to protect compatibility with validator data. These checks verify the wire contract rather than a separate application format.

References: account.go• L254-319 sysvar/sysvars_test.go• L452-477 account_test.go• L295-422

Rent and fee types

Rent rules

Rent represents the cluster's rent parameters. Its minimum-balance calculation combines the account storage overhead, data length, lamports-per-byte rate, and exemption threshold. IsExempt compares an account balance with that result. TryMinimumBalance also reports when the data length is outside the permitted range or arithmetic cannot fit in uint64.

The default configuration reflects the current SDK form, while the wire layout remains a rate, an eight-byte floating-point threshold, and a burn percentage. This keeps current rent calculations compatible with the sysvar account representation.

References: sysvar/rent.go• L25-81 sysvar/rent.go• L82-135 sysvar/rent.go• L136-169

Fee state

FeeCalculator expresses the cost in lamports for each transaction signature. Fees wraps that value as the deprecated Fees sysvar. RecentBlockhashes pairs old blockhashes with their fee calculators, but that sysvar is also deprecated.

Current clients should use getFeeForMessage to estimate a transaction fee and getLatestBlockhash to obtain a usable recent blockhash. The legacy types remain useful when decoding historical or compatibility data.

References: fee_calculator.go• L23-35 sysvar/fees.go• L22-56 sysvar/recent_blockhashes.go• L24-51

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep field order, integer width, boolean form, and list-length encoding aligned between each marshal and unmarshal path.
  • Keep each sysvar public key paired with the correct typed decoder. A valid byte sequence for one sysvar can still be the wrong data for another.
  • Recheck published size constants and fixed-record assumptions when a sysvar field or collection limit changes.
  • Preserve descending order and retention limits for slot hashes and stake history because lookup methods depend on them.
  • Treat rent default or arithmetic changes as changes to account-funding decisions, not only as changes to a helper.
  • Keep slot-time feature IDs, activation rules, epoch-boundary handling, and default durations aligned when cluster timing rules change.
  • Route new fee and blockhash consumers to the current RPC methods instead of extending use of the deprecated sysvars.

Important failure modes

TriggerConsequenceGuard
A transaction short-vector encoding is used for a sysvar listThe decoder reads the wrong length and cannot interpret the account dataUse standard bincode with a little-endian u64 list length for sysvars
A sysvar ID is paired with the wrong decoderBytes may fail to decode or produce meaningless typed stateUse the matching sysvar ID and Decode<Type> helper
Account or sysvar data is truncatedDecoding returns an error and no complete state is availableCheck the returned error before using the value
A declared account data length exceeds the inputAccount decoding fails instead of reading outside the recordTreat the length as untrusted and use DecodeAccount
Rent arithmetic exceeds uint64 or the data length is too largeA direct minimum-balance request can report saturation or failureUse TryMinimumBalance when the caller must detect invalid input
A slot-time gate is treated as active in its activation epochTime calculations use a shorter slot duration before the cluster rule takes effectApply the gate from the first slot of the next epoch through MsPerSlotAt or MsPerSlotInEpoch
A zero-value epoch schedule or overflowing activation epoch is used for slot-time calculationThe gate may appear active at the wrong slot or wrap to an earlier epochFall back to DefaultMsPerSlot for invalid schedule or saturated epoch arithmetic
A caller relies on the Fees or RecentBlockhashes sysvar for current valuesFee or blockhash data can be deprecated or unsuitable for a new transactionUse getFeeForMessage and getLatestBlockhash

Key files

FileWhy to read it
account.goDefines the binary account state model, its codec contract, and the main account decoder.
sysvar.goDefines the canonical root-package addresses for validator-maintained sysvar accounts.
sysvar/ids.goPairs convenient sysvar IDs with the typed values and current/deprecated status.
sysvar/sysvar_codec.goStates the shared bincode contract used by sysvar values.
sysvar/clock.go and sysvar/epoch_schedule.goShow how cluster time and epoch calculations become typed state.
sysvar/slot_time.go and feature_gate.goShow how feature activation and epoch boundaries determine effective slot duration.
sysvar/rent.goDefines rent parameters, exemption decisions, and rent account encoding.
sysvar/slot_hashes.go, sysvar/slot_history.go, and sysvar/stake_history.goDefine the rolling slot and epoch histories used for ordered lookup and window checks.
sysvar/epoch_rewards.goDefines epoch reward distribution state and its bounded distribution update.
sysvar/fees.go and sysvar/recent_blockhashes.goDescribe the legacy fee-related sysvar codecs and their replacement RPC paths.
sysvar/sysvars_test.go and account_test.goShow the wire-level compatibility checks, error checks, and fuzz guarantees for these codecs.