canopy

transaction.go - Transaction Processing in the Canopy Blockchain

This file contains the core transaction handling logic for the Canopy blockchain’s state machine. It defines how transactions are processed, validated, and applied to the blockchain state.

Overview

The transaction handling system is designed to:

Core Components

Transaction Processing

The transaction processing system handles the lifecycle of a transaction from validation to execution. It ensures that transactions are properly formed, authorized, and can be safely applied to the blockchain state. This includes checking signatures, validating message payloads, and ensuring sufficient fees are provided.

When a transaction is submitted to the blockchain, it goes through several validation steps before being applied to the state. This ensures that only valid transactions are processed, maintaining the integrity of the blockchain.

Transaction Validation

Transaction validation is a multi-step process that ensures transactions meet all requirements before being accepted. This includes basic validation of the transaction structure, replay protection, signature verification, and fee validation.

The validation process is crucial for maintaining blockchain security. It prevents malicious actors from submitting invalid transactions or replaying previously executed transactions, which could potentially disrupt the blockchain’s operation or lead to double-spending.

Transaction Creation

The file provides numerous helper functions for creating different types of transactions. These functions make it easier to construct properly formatted transactions for various operations like sending tokens, staking, changing parameters, and more.

Each transaction type serves a specific purpose in the blockchain ecosystem, allowing users to perform different actions like transferring tokens, participating in consensus through staking, or modifying blockchain parameters through governance.

Technical Details

Transaction Lifecycle

When a transaction is submitted to the blockchain, it goes through the following process:

  1. Validation (CheckTx): The transaction is validated to ensure it meets all requirements:
    • Basic structure validation
    • Replay protection checks
    • Message payload validation
    • Signature verification
    • Fee validation
  2. Fee Deduction: If validation passes, fees are deducted from the sender’s account to pay for transaction processing.

  3. Message Handling: The specific message payload is processed, which may update various aspects of the blockchain state.

  4. Result Creation: A transaction result is created, containing information about the processed transaction.

This process ensures that only valid transactions are applied to the blockchain state, maintaining the integrity and security of the system.

Replay Protection

Canopy uses a unique approach to prevent replay attacks and hash collisions:

This approach is “prune-friendly,” meaning that nodes don’t need to store the entire transaction history to prevent replay attacks, which helps keep the blockchain more efficient.

Signature Verification

Transaction signatures are verified using public key cryptography:

  1. The transaction is serialized into a canonical byte representation.
  2. The signature is verified against these bytes using the sender’s public key.
  3. The system checks if the signer is authorized to perform the requested action.

For certain message types (like stake operations or parameter changes), additional information is populated during signature verification, such as the signer field or proposal hash.

Transaction and State Machine

Transactions interact with the state machine to update the blockchain state:

This interaction ensures that transactions are processed in a consistent and deterministic manner, maintaining the integrity of the blockchain state.

Transaction and Fee System

Transactions must include sufficient fees to be processed:

This fee system helps prevent spam and ensures that transaction processors (validators) are compensated for their work.

Transaction and Message Types

Transactions are containers for different types of messages:

The file provides helper functions for creating various types of transactions, each with a specific message type for operations like sending tokens, staking, changing parameters, and more.

Security & Integrity Mechansisms