Skip to content

Vulkan Currency ‐ Source code overview (AI generated)

Caleb Marshall edited this page Jan 24, 2025 · 1 revision

Vulkan Blockchain Source Documentation

This document provides detailed insights into the core source files used in the Vulkan blockchain project, focusing on their purpose and functionality. This documentation is designed to help developers understand and extend the Vulkan blockchain.

protocol.c

Overview

The protocol.c file handles the core implementation of Vulkan's peer-to-peer (P2P) communication protocol. It defines how nodes communicate, synchronize, and exchange data within the network.

Key Functions

  • Packet Management:

    • serialize_packet(): Converts a packet structure into a serialized format for transmission.
    • deserialize_packet(): Converts serialized data back into a packet structure.
    • create_new_packet(): Allocates and initializes a new packet.
    • free_packet(): Frees memory allocated to a packet.
  • Message Handling:

    • serialize_message(): Serializes a message into a packet structure.
    • deserialize_message(): Parses a packet to extract a message.
    • free_message(): Frees allocated memory for a message object.
  • Synchronization:

    • init_sync_request(): Initiates a blockchain synchronization request.
    • request_sync_block(): Requests a specific block from a peer.
    • handle_sync_added_block(): Processes a block received during synchronization.
  • Error Handling:

    • handle_packet_anonymous(): Processes packets from anonymous or unverified peers.
    • can_packet_be_processed(): Checks if a packet is valid and can be processed.

Example Workflow

graph TD
    A[Node A] -->|Send PKT_TYPE_CONNECT_ESTABLISH_REQ| B[Node B]
    B -->|Reply PKT_TYPE_CONNECT_ESTABLISH_RESP| A
    A -->|Request Block Data| B
    B -->|Send Block Data| A
Loading
  1. A node sends a PKT_TYPE_CONNECT_ESTABLISH_REQ packet to establish a connection.
  2. The receiving node replies with a PKT_TYPE_CONNECT_ESTABLISH_RESP.
  3. Nodes then exchange block and transaction data to synchronize.

protocol.h

Overview

This header file defines the data structures, constants, and function prototypes used in protocol.c. It is essential for ensuring compatibility across all modules interacting with the P2P protocol.

Key Definitions

  • Packet Types:

    • PKT_TYPE_GET_BLOCK_HEIGHT_REQ: Request the blockchain height from a peer.
    • PKT_TYPE_GET_BLOCK_BY_HASH_REQ: Request a block by its hash.
    • PKT_TYPE_INCOMING_MEMPOOL_TRANSACTION: Notify peers of a new transaction in the mempool.
  • Structures:

    • packet_t: Represents a network packet, including its ID, size, and payload data.
    • sync_entry_t: Tracks synchronization status, including pending blocks and transactions.

Synchronization Constants

graph LR
    A[RESYNC_CHAIN_TASK_DELAY] -->|Delay Start| B[Chain Resync]
    C[RESYNC_BLOCK_MAX_TRIES] -->|Retry Limit| D[Block Request Fails]
Loading
  • RESYNC_CHAIN_TASK_DELAY: Delay in seconds before initiating a chain resynchronization.
  • RESYNC_BLOCK_MAX_TRIES: Maximum number of attempts to request a block before marking it as failed.

blockchain.c

Overview

The blockchain.c file is responsible for managing the core blockchain data, including blocks, transactions, and storage operations. It implements key blockchain functionality like compression, backup, restoration, and block validation.

Key Functions

  • Blockchain Management:

    • init_blockchain(): Initializes the blockchain, including its directory and backup.
    • open_blockchain(): Opens the blockchain database.
    • close_blockchain(): Closes the blockchain database.
    • reset_blockchain(): Resets the blockchain by purging all data.
  • Compression and Storage:

    • set_want_blockchain_compression(): Toggles compression for blockchain storage.
    • set_blockchain_compression_type(): Configures the compression type (e.g., Snappy, LZ4, Zstd).
    • get_compression_type_str(): Returns a string representation of the compression type.
  • Block Handling:

    • insert_block(): Inserts a block into the blockchain.
    • validate_and_insert_block(): Validates a block and inserts it if valid.
    • rollback_blockchain(): Rolls back the blockchain to a specified height.
    • get_block_from_hash(): Retrieves a block by its hash.
    • get_block_from_height(): Retrieves a block by its height.

Compression Options

graph TD
    A[Compression Enabled?] -->|Yes| B[Snappy, LZ4, Zstd]
    A -->|No| C[Uncompressed Storage]
Loading

blockchain.h

Overview

This header file defines constants, macros, and function prototypes for blockchain management. It ensures compatibility with blockchain.c and other related modules.

Key Definitions

  • Database Prefixes:

    • DB_KEY_PREFIX_TX: Prefix for transaction keys.
    • DB_KEY_PREFIX_BLOCK: Prefix for block keys.
    • DB_KEY_PREFIX_UNSPENT_TX: Prefix for unspent transaction keys.
  • Function Prototypes:

    • valid_compression_type(): Checks if a given compression type is valid.
    • repair_blockchain(): Repairs a corrupted blockchain database.
    • backup_blockchain(): Backs up the blockchain data.
    • restore_blockchain(): Restores blockchain data from a backup.

Example Workflow

graph TD
    A[Blockchain Initialized] --> B[Load Genesis Block]
    B --> C[Validate Blocks]
    C --> D[Insert Blocks]
    D --> E[Backup Blockchain]
Loading

block.c

Overview

The block.c file provides functionality for creating, validating, and serializing blocks within the Vulkan blockchain.

Key Functions

  • Block Creation:

    • create_new_block(): Allocates memory and initializes a new block structure.
  • Validation:

    • valid_block(): Ensures that the block meets all necessary criteria (e.g., valid timestamp, valid transactions, etc.).
    • valid_block_hash(): Checks the integrity of a block's hash.
    • valid_merkle_root(): Validates the Merkle root of a block.
  • Serialization:

    • serialize_block(): Converts a block structure into a buffer for storage or transmission.
    • deserialize_block(): Reconstructs a block from a serialized buffer.

Example Workflow

graph TD
    A[Create Block] --> B[Add Transactions]
    B --> C[Compute Merkle Root]
    C --> D[Validate Block]
    D --> E[Insert Block into Blockchain]
Loading

block.h

Overview

This header file defines the structure and prototypes for managing blocks in Vulkan.

Key Definitions

  • Block Structure:

    • block_t: Represents a block, including fields for version, hash, timestamp, nonce, and transactions.
  • Function Prototypes:

    • valid_block(): Validates the integrity of a block.
    • compute_merkle_root(): Calculates the Merkle root for a block.
    • serialize_block(): Converts a block structure into serialized format.
    • deserialize_block(): Reconstructs a block from serialized data.

transaction.c

Overview

The transaction.c file implements Vulkan's transaction handling, including creation, validation, and serialization of transactions.

Key Functions

  • Transaction Creation:

    • create_new_transaction(): Allocates and initializes a new transaction.
    • create_new_txin(): Creates a new input transaction.
    • create_new_txout(): Creates a new output transaction.
  • Validation:

    • valid_transaction(): Validates the integrity of a transaction.
    • validate_tx_signatures(): Verifies all input signatures of a transaction.
    • do_txins_reference_unspent_txouts(): Ensures inputs reference valid unspent outputs.
  • Serialization:

    • serialize_transaction(): Serializes a transaction for storage or transmission.
    • deserialize_transaction(): Deserializes a transaction from a serialized buffer.

Example Workflow

graph TD
    A[Create Transaction] --> B[Add Inputs]
    B --> C[Add Outputs]
    C --> D[Sign Transaction]
    D --> E[Validate and Serialize]
Loading

transaction.h

Overview

This header file defines transaction structures and prototypes for transaction.c.

Key Definitions

  • Structures:

    • input_transaction_t: Represents an input transaction referencing a previous output.
    • output_transaction_t: Represents an output transaction with a value and recipient address.
    • transaction_t: Represents a complete transaction with inputs, outputs, and metadata.
  • Function Prototypes:

    • compute_tx_id(): Computes the unique ID for a transaction.
    • add_txin_to_transaction(): Adds an input to a transaction.
    • add_txout_to_transaction(): Adds an output to a transaction.

Example Workflow

graph TD
    A[Unspent Outputs] --> B[Create Inputs]
    B --> C[Define Outputs]
    C --> D[Sign and Broadcast Transaction]
Loading

Integration

These files form the backbone of Vulkan's blockchain data management, ensuring secure storage, validation, and retrieval of blockchain data. Their modular and flexible design allows for future enhancements and scalability.

Future Enhancements

graph TD
    A[Dynamic Scaling] -->|Supports| B[Higher Throughput]
    C[Improved Compression] -->|Optimizes| D[Storage Efficiency]
    E[Advanced Backup Mechanisms] -->|Enhances| F[Disaster Recovery]
Loading
  • Dynamic Scaling: Improve blockchain throughput for larger networks.
  • Improved Compression: Experiment with more efficient algorithms for better storage.
  • Advanced Backup Mechanisms: Implement incremental backups to reduce downtime and data loss.

How to Extend

To add new blockchain functionalities:

graph TD
    A[Define New Functionality] --> B[Update Headers in blockchain.h]
    B --> C[Implement Logic in blockchain.c]
    C --> D[Write Tests for Functionality]
Loading
  1. Define the functionality in blockchain.h.
  2. Implement the logic in blockchain.c.
  3. Write unit tests to ensure proper functionality.

License

Vulkan is released under the MIT License. Refer to the LICENSE file for detailed information.