-
Notifications
You must be signed in to change notification settings - Fork 5
Vulkan Currency ‐ Source code overview (AI generated)
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.
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.
-
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.
-
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
- A node sends a
PKT_TYPE_CONNECT_ESTABLISH_REQpacket to establish a connection. - The receiving node replies with a
PKT_TYPE_CONNECT_ESTABLISH_RESP. - Nodes then exchange block and transaction data to synchronize.
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.
-
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.
-
graph LR
A[RESYNC_CHAIN_TASK_DELAY] -->|Delay Start| B[Chain Resync]
C[RESYNC_BLOCK_MAX_TRIES] -->|Retry Limit| D[Block Request Fails]
-
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.
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.
-
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.
-
graph TD
A[Compression Enabled?] -->|Yes| B[Snappy, LZ4, Zstd]
A -->|No| C[Uncompressed Storage]
This header file defines constants, macros, and function prototypes for blockchain management. It ensures compatibility with blockchain.c and other related modules.
-
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.
-
graph TD
A[Blockchain Initialized] --> B[Load Genesis Block]
B --> C[Validate Blocks]
C --> D[Insert Blocks]
D --> E[Backup Blockchain]
The block.c file provides functionality for creating, validating, and serializing blocks within the Vulkan blockchain.
-
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.
-
graph TD
A[Create Block] --> B[Add Transactions]
B --> C[Compute Merkle Root]
C --> D[Validate Block]
D --> E[Insert Block into Blockchain]
This header file defines the structure and prototypes for managing blocks in Vulkan.
-
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.
-
The transaction.c file implements Vulkan's transaction handling, including creation, validation, and serialization of transactions.
-
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.
-
graph TD
A[Create Transaction] --> B[Add Inputs]
B --> C[Add Outputs]
C --> D[Sign Transaction]
D --> E[Validate and Serialize]
This header file defines transaction structures and prototypes for transaction.c.
-
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.
-
graph TD
A[Unspent Outputs] --> B[Create Inputs]
B --> C[Define Outputs]
C --> D[Sign and Broadcast Transaction]
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.
graph TD
A[Dynamic Scaling] -->|Supports| B[Higher Throughput]
C[Improved Compression] -->|Optimizes| D[Storage Efficiency]
E[Advanced Backup Mechanisms] -->|Enhances| F[Disaster Recovery]
- 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.
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]
- Define the functionality in
blockchain.h. - Implement the logic in
blockchain.c. - Write unit tests to ensure proper functionality.
Vulkan is released under the MIT License. Refer to the LICENSE file for detailed information.