Skip to content

Commit

Permalink
📝 add documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
AbdelStark committed Sep 4, 2024
1 parent 28c54f6 commit cec6f28
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 7 deletions.
6 changes: 6 additions & 0 deletions src/benchmarks.zig
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
//! Benchmark module handles the benchmarking of btczee.
//! Currently, it is using the zul library to run the benchmarks.
//! The benchmarks can be run with:
//! ```
//! zig build bench
//! ```
const std = @import("std");

const zul = @import("zul");
Expand Down
11 changes: 11 additions & 0 deletions src/lib.zig
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
//! btczee is a Bitcoin protocol implementation in Zig.
//! It can be used as a binary or a library.
//! Warning: This is still a work in progress and is not yet ready for production use.
//!
//! btczee can be run as:
//! - a wallet
//! - a miner
//! - a full node
//!
//! btczee is licensed under the MIT license.
pub const config = @import("config/config.zig");
pub const mempool = @import("core/mempool.zig");
pub const p2p = @import("network/p2p.zig");
pub const rpc = @import("network/rpc.zig");
pub const storage = @import("storage/storage.zig");
pub const wallet = @import("wallet/wallet.zig");
pub const miner = @import("miner/miner.zig");

test {
@import("std").testing.refAllDeclsRecursive(@This());
Expand Down
6 changes: 6 additions & 0 deletions src/miner/miner.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//! Miner module handles the miner features of btczee.
//! It is responsible for mining blocks and validating transactions.
const std = @import("std");

/// Miner service.
const Miner = struct {};
32 changes: 26 additions & 6 deletions src/network/p2p.zig
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
//! P2P module handles the peer-to-peer networking of btczee.
//! It is responsible for the connection to other nodes in the network.
//! It can receive and send messages to other nodes, based on the Bitcoin protocol.
const std = @import("std");
const net = std.net;
const Config = @import("../config/config.zig").Config;
const Peer = @import("peer.zig").Peer;

/// P2P network handler.
pub const P2P = struct {
/// Allocator.
allocator: std.mem.Allocator,
/// Configuration.
config: *const Config,
/// List of peers.
peers: std.ArrayList(*Peer),
/// Listener.
listener: ?net.Server,

/// Initialize the P2P network
/// Initialize the P2P network handler.
/// # Arguments
/// - `allocator`: Allocator.
/// - `config`: Configuration.
/// # Returns
/// - `P2P`: P2P network handler.
pub fn init(allocator: std.mem.Allocator, config: *const Config) !P2P {
return P2P{
.allocator = allocator,
Expand All @@ -20,7 +32,7 @@ pub const P2P = struct {
};
}

/// Deinitialize the P2P network
/// Deinitialize the P2P network handler.
pub fn deinit(self: *P2P) void {
if (self.listener) |*l| l.deinit();
for (self.peers.items) |peer| {
Expand All @@ -29,10 +41,11 @@ pub const P2P = struct {
self.peers.deinit();
}

/// Start the P2P network
/// Start the P2P network handler.
pub fn start(self: *P2P) !void {
std.log.info("Starting P2P network on port {}", .{self.config.p2p_port});

// TODO: Implement the P2P network handler
// Initialize the listener
// const address = try net.Address.parseIp4("0.0.0.0", self.config.p2p_port);
// const stream = try net.tcpConnectToAddress(address);
Expand All @@ -49,7 +62,8 @@ pub const P2P = struct {
// try self.connectToSeedNodes();
}

/// Accept incoming connections
/// Accept incoming connections.
/// The P2P network handler will accept incoming connections and handle them in a separate thread.
fn acceptConnections(self: *P2P) !void {
while (true) {
const connection = self.listener.?.accept() catch |err| {
Expand All @@ -63,29 +77,35 @@ pub const P2P = struct {
}
}

/// Handle a new connection
/// Handle a new connection.
/// # Arguments
/// - `self`: P2P network handler.
/// - `connection`: Connection.
fn handleConnection(self: *P2P, connection: net.Server.Connection) void {
const peer = Peer.init(self.allocator, connection) catch |err| {
std.log.err("Failed to initialize peer: {}", .{err});
connection.stream.close();
return;
};

// Add the peer to the list of peers
self.peers.append(peer) catch |err| {
std.log.err("Failed to add peer: {}", .{err});
peer.deinit();
return;
};

// Start the peer in a new thread
peer.start() catch |err| {
std.log.err("Peer encountered an error: {}", .{err});
_ = self.peers.swapRemove(self.peers.items.len - 1);
peer.deinit();
};
}

/// Connect to seed nodes
/// Connect to seed nodes.
fn connectToSeedNodes(self: *P2P) !void {
// If no seed nodes are configured, do nothing
if (self.config.seednode.len == 0) {
return;
}
Expand Down
20 changes: 19 additions & 1 deletion src/network/rpc.zig
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//! RPC module handles the RPC server of btczee.
//! It is responsible for the communication between the node and the clients.
//! See https://developer.bitcoin.org/reference/rpc/
const std = @import("std");
const Config = @import("../config/config.zig").Config;
const Mempool = @import("../core/mempool.zig").Mempool;
Expand All @@ -8,13 +11,24 @@ const httpz = @import("httpz");
///
/// The RPC server is responsible for handling the RPC requests from the clients.
///
/// See https://developer.bitcoin.org/reference/rpc/
pub const RPC = struct {
/// Allocator .
allocator: std.mem.Allocator,
/// Configuration.
config: *const Config,
/// Transaction pool.
mempool: *Mempool,
/// Blockchain storage.
storage: *Storage,

/// Initialize the RPC server.
/// # Arguments
/// - `allocator`: Allocator.
/// - `config`: Configuration.
/// - `mempool`: Transaction pool.
/// - `storage`: Blockchain storage.
/// # Returns
/// - `RPC`: RPC server.
pub fn init(
allocator: std.mem.Allocator,
config: *const Config,
Expand All @@ -31,10 +45,14 @@ pub const RPC = struct {
return rpc;
}

/// Deinitialize the RPC server.
/// Clean up the RPC server resources.
pub fn deinit(self: *RPC) void {
_ = self;
}

/// Start the RPC server.
/// The RPC server will start a HTTP server and listen on the RPC port.
pub fn start(self: *RPC) !void {
std.log.info("Starting RPC server on port {}", .{self.config.rpc_port});
var handler = Handler{};
Expand Down
9 changes: 9 additions & 0 deletions src/node/node.zig
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
//! Contains functionalities to run a Bitcoin full node on the Bitcoin network.
//! It enables the validation of transactions and blocks, the exchange of transactions and blocks with other peers.
const std = @import("std");
const Config = @import("../config/config.zig").Config;
const Mempool = @import("../core/mempool.zig").Mempool;
const Storage = @import("../storage/storage.zig").Storage;
const P2P = @import("../network/p2p.zig").P2P;
const RPC = @import("../network/rpc.zig").RPC;

/// Start the node.
///
/// # Arguments
/// - `mempool`: Transaction pool.
/// - `storage`: Blockchain storage.
/// - `p2p`: P2P network handler.
/// - `rpc`: RPC server.
pub fn startNode(_: *Mempool, _: *Storage, p2p: *P2P, rpc: *RPC) !void {
std.log.info("Starting btczee node...", .{});

Expand Down
Empty file added src/types/block.zig
Empty file.
7 changes: 7 additions & 0 deletions src/types/lib.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//! Types module contains the definitions of the data structures used in the node.

/// Block
pub const block = @import("block.zig");

/// Transaction
pub const transaction = @import("transaction.zig");
7 changes: 7 additions & 0 deletions src/wallet/wallet.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//! Wallet module handles the wallet features of btczee.
//! It is responsible for creating, managing, and storing wallets.
//! It also handles the generation of wallet addresses and the storage of private keys.
const std = @import("std");

/// Wallet struct represents a wallet with its addresses and keys.
const Wallet = struct {};

0 comments on commit cec6f28

Please sign in to comment.