canopy

account.go - State Management for Accounts, Pools, and Supply

This file defines the core state management functions for the Canopy blockchain, handling accounts, pools, and the overall token supply. It provides the essential operations needed to maintain the financial state of the blockchain.

Overview

The account.go file implements functionality for:

Core Components

Accounts

Accounts are the fundamental entities in the blockchain that can hold tokens. Each account has:

The file provides functions to:

An account in Canopy is similar to a bank account in the traditional financial system. It has an address (like an account number) and a balance. When you want to send tokens to someone, your account balance decreases and the recipient’s account balance increases.

Pools

Pools are special-purpose accounts without individual owners. They are used to hold tokens for specific blockchain functions. Unlike regular accounts that are controlled by users with private keys, pools are managed by the blockchain protocol itself according to predefined rules.

For example, a pool might hold tokens that are:

Pools are identified by numeric IDs rather than addresses, making it clear that they are not controlled by any individual user.

Supply Tracker

The Supply Tracker maintains information about the total token supply and its distribution across the blockchain. It tracks:

This component provides a comprehensive view of the blockchain’s financial state, similar to how a central bank might track the money supply in a traditional economy.

State Management

The file implements a state machine pattern to manage blockchain state. All state changes follow these principles:

  1. Atomicity: Operations either complete fully or not at all
  2. Consistency: The state remains valid after each operation
  3. Isolation: Concurrent operations don’t interfere with each other
  4. Durability: Once committed, changes persist even in case of system failure

For example, when transferring tokens between accounts:

  1. The system checks if the sender has sufficient funds
  2. It deducts tokens from the sender
  3. It adds tokens to the recipient
  4. If any step fails, the entire transaction is reverted

This ensures that the blockchain’s financial state remains consistent and accurate at all times.

Token Economics

The file implements several economic mechanisms:

Fee Collection

When users submit transactions, they pay fees that are:

  1. Deducted from the sender’s account
  2. Added to a designated fee pool

These fees serve two purposes:

Staking and Delegation

The system tracks tokens that are:

This staking mechanism is crucial for the security of proof-of-stake blockchains like Canopy.

Supply Management

The system can:

This allows for flexible monetary policy similar to how central banks manage fiat currencies.