Skip to content

account_compress

View as Markdown

The account_compress syscall compresses an account by setting the COMPRESSED flag and providing a state proof. This allows the account data to be stored more efficiently while maintaining verifiability.

Code: 0x08 (TN_SYSCALL_CODE_ACCOUNT_COMPRESS)

ulong tsys_account_compress(ulong account_idx, void const* proof, ulong proof_sz);

account_idx · ulong · required

Index of the account to compress. Must be writable in the transaction.

proof · void const · required*

Pointer to the state proof data.

proof_sz · ulong · required

Size of the state proof data in bytes.

Returns a syscall result code:

Success · ulong

  • TN_VM_SYSCALL_SUCCESS (0) - Account compressed successfully
  • 0 (special case) - Ephemeral account deleted instead of compressed

Error Codes · ulong

  • TN_VM_ERR_SYSCALL_INVALID_ACCOUNT_INDEX (-8) - Account index out of bounds
  • TN_VM_ERR_SYSCALL_ACCOUNT_DOES_NOT_EXIST (-9) - Account does not exist
  • TN_VM_ERR_SYSCALL_ACCOUNT_NOT_WRITABLE (-10) - Account not writable in transaction
  • TN_VM_ERR_SYSCALL_TXN_HAS_COMPRESSED_ACCOUNT (-37) - Transaction already has a compressed account
  • TN_VM_ERR_SYSCALL_ACCOUNT_COMPRESSION_NOT_ALLOWED (-43) - Account compression is not allowed
  • TN_VM_ERR_SYSCALL_ACCOUNT_UNCOMPRESSABLE (-45) - Account cannot be compressed
  • TN_VM_ERR_SYSCALL_INVALID_PROOF_LEN (-32) - Proof size mismatch
  • TN_VM_ERR_SYSCALL_INVALID_PROOF_SLOT (-33) - Proof references invalid block slot
  • TN_VM_ERR_SYSCALL_INVALID_STATE_PROOF (-23) - State proof verification failed
  • TN_VM_ERR_SYSCALL_STATE_BYTES_REMOVED_OVERFLOW (-48) - State bytes counter overflow during deactivation
  • TN_VM_ERR_SYSCALL_PROGRAM_IN_CALL_STACK (-49) - Cannot compress a program that is currently executing
  • Base cost: TN_VM_SYSCALL_BASE_COST (512 units)
  • Proof cost: Additional units equal to proof size in bytes
  • Data cost: Additional units equal to account data size in bytes
  • Metadata cost: Additional units equal to sizeof(tsdk_account_meta_t)
  • Total formula: base_cost + proof_sz + account_data_sz + sizeof(tsdk_account_meta_t)
  • Page usage: No immediate page deallocation (compression is logical)
  • Proof storage: Temporary memory for proof verification
  • Metadata impact: Account metadata becomes writable if not already
  • GDSC (Global Deactivated State Counter): Incremented by TSDK_ACCOUNT_META_FOOTPRINT + account_data_sz bytes
  • Purpose: Tracks deactivated state for the account compression in the global state counter
  • Overflow protection: Returns TN_VM_ERR_SYSCALL_STATE_BYTES_REMOVED_OVERFLOW if counter would overflow
  • GASC/GDSC check: Compression may be denied if state counter conditions are not met (unless owner/fee payer is compressing)
  • Account state: Sets COMPRESSED flag and updates state counter
  • Transaction state: Records compression proof for transaction
  • Special handling: Ephemeral and deleted accounts are deleted instead

Ephemeral accounts are simply deleted when “compressed” - they cannot be truly compressed since they don’t persist beyond the transaction.

  • New accounts: Must use TN_STATE_PROOF_TYPE_CREATION proof type
  • Existing accounts: Must use TN_STATE_PROOF_TYPE_EXISTING proof type
  • Proof verification: Must reference valid block slot and verify against state root
  • Account hash: Proof must match the computed hash of the account’s current state
  • Only one account can be compressed per transaction
  • The transaction records the compression proof and account details
  • Compression state is tracked to prevent multiple compressions
  • Account must be writable in the transaction (signer approval)
  • State proof ensures the account state is correctly recorded
  • Compressed accounts can later be decompressed with valid proofs
  • The account’s state counter is updated to the proof block’s counter
  • Program accounts cannot be compressed while they are in the call stack (current program or any caller)
#include <thru-sdk/c/tn_sdk_syscall.h>
// Compress an account with state proof
ulong account_idx = 2;
uchar proof_data[2048];
ulong proof_size = prepare_compression_proof(account_idx, proof_data);
ulong result = tsys_account_compress(account_idx, proof_data, proof_size);
if (result == TN_VM_SYSCALL_SUCCESS) {
// Account compressed successfully
// COMPRESSED flag set
// State counter updated
// Compression proof recorded in transaction
}