Skip to content

Latest commit

 

History

History
97 lines (66 loc) · 3.85 KB

File metadata and controls

97 lines (66 loc) · 3.85 KB

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:

  • Managing user accounts and their balances
  • Handling token transfers between accounts
  • Managing special-purpose token pools
  • Tracking the total token supply and its distribution
  • Supporting fee collection and distribution

Core Components

Accounts

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

  • A unique address (derived from a public key)
  • A token balance

The file provides functions to:

  • Retrieve account information
  • Update account balances
  • Transfer tokens between accounts
  • Collect transaction fees

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:

  • Reserved for validator rewards
  • Collected as transaction fees
  • Set aside for community development

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:

  • The total number of tokens in existence
  • Tokens that are staked by validators
  • Tokens that are delegated to validators
  • Distribution of staked tokens across different chains

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:

  • They prevent spam by making transactions cost something
  • They provide rewards for validators who process transactions

Staking and Delegation

The system tracks tokens that are:

  • Staked directly by validators (committed to securing the network)
  • Delegated by users to validators (supporting validators without running nodes)

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

Supply Management

The system can:

  • Track the total token supply
  • Add new tokens (mint)
  • Remove tokens from circulation if needed

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