diff --git a/README.md b/README.md index 0669c37..ed1ea98 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,12 @@ You can find the architecture of the project in the [docs/architecture.md](./docs/architecture.md) file. +## Run + +```sh +zig build run +``` + ## Test ```sh diff --git a/docs/architecture.md b/docs/architecture.md index e69de29..ee8ba53 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -0,0 +1,27 @@ +# Architecture + +## Components + +### P2P + +P2P is responsible for handling the peer-to-peer network. + +The main logic is implemented in `src/p2p.zig`. + +### RPC + +RPC is responsible for handling the RPC requests from the clients. + +The main logic is implemented in `src/rpc.zig`. + +### Storage + +Storage is responsible for handling the blockchain data. + +The main logic is implemented in `src/storage.zig`. + +### Mempool + +Mempool is responsible for handling the mempool of pending transactions. + +The main logic is implemented in `src/mempool.zig`. diff --git a/docs/roadmap.md b/docs/roadmap.md index e69de29..620ab23 100644 --- a/docs/roadmap.md +++ b/docs/roadmap.md @@ -0,0 +1,9 @@ +# Roadmap + +## 0.1.0 + +- [ ] Implement the P2P network +- [ ] Implement the RPC server +- [ ] Implement the storage +- [ ] Implement the mempool +- [ ] Implement the CLI \ No newline at end of file diff --git a/src/cli.zig b/src/cli.zig index 0e9af32..299f83c 100644 --- a/src/cli.zig +++ b/src/cli.zig @@ -2,6 +2,7 @@ const std = @import("std"); const Config = @import("config.zig").Config; const RPC = @import("rpc.zig").RPC; +/// CLI for the node pub const CLI = struct { allocator: std.mem.Allocator, diff --git a/src/config.zig b/src/config.zig index 547f00d..27f27e9 100644 --- a/src/config.zig +++ b/src/config.zig @@ -1,12 +1,36 @@ const std = @import("std"); +/// Global configuration for the node +/// +/// This is loaded from the `bitcoin.conf` file +/// Must be loaded before any other modules are used. +/// Must be compatible with Bitcoin Core's `bitcoin.conf` format. pub const Config = struct { allocator: std.mem.Allocator, + + /// RPC port rpc_port: u16, + + /// P2P port p2p_port: u16, + + /// Testnet flag testnet: bool, + + /// Data directory datadir: []const u8, + /// Load the configuration from a file + /// + /// # Arguments + /// - `allocator`: Memory allocator + /// - `filename`: Path to the configuration file + /// + /// # Returns + /// - `Config`: Configuration + /// # Errors + /// - Failed to read the file + /// - Failed to parse the file pub fn load(allocator: std.mem.Allocator, filename: []const u8) !Config { const file = try std.fs.cwd().openFile(filename, .{}); defer file.close(); diff --git a/src/lib.zig b/src/lib.zig index 56374ae..7142e2f 100644 --- a/src/lib.zig +++ b/src/lib.zig @@ -1,3 +1,9 @@ +pub usingnamespace @import("config.zig"); +pub usingnamespace @import("mempool.zig"); +pub usingnamespace @import("p2p.zig"); +pub usingnamespace @import("rpc.zig"); +pub usingnamespace @import("storage.zig"); + test { @import("std").testing.refAllDeclsRecursive(@This()); } diff --git a/src/mempool.zig b/src/mempool.zig index 7281489..bfb575f 100644 --- a/src/mempool.zig +++ b/src/mempool.zig @@ -1,10 +1,21 @@ const std = @import("std"); const Config = @import("config.zig").Config; +/// Transaction mempool. +/// The mempool is a collection of transactions that are pending for confirmation. +/// The node can implement different mempool strategies. pub const Mempool = struct { allocator: std.mem.Allocator, config: *const Config, + /// Initialize the mempool + /// + /// # Arguments + /// - `allocator`: Memory allocator + /// - `config`: Configuration + /// + /// # Returns + /// - `Mempool`: Mempool pub fn init(allocator: std.mem.Allocator, config: *const Config) !Mempool { return Mempool{ .allocator = allocator, @@ -12,6 +23,10 @@ pub const Mempool = struct { }; } + /// Deinitialize the mempool + /// + /// # Arguments + /// - `self`: Mempool pub fn deinit(self: *Mempool) void { // Clean up resources if needed _ = self; diff --git a/src/p2p.zig b/src/p2p.zig index 558fea4..31af2e1 100644 --- a/src/p2p.zig +++ b/src/p2p.zig @@ -1,10 +1,22 @@ const std = @import("std"); const Config = @import("config.zig").Config; +/// P2P network handler. +/// +/// The P2P network is responsible for handling the peer-to-peer network. +/// It is responsible for handling the network protocol, the block relay, and the node sync. pub const P2P = struct { allocator: std.mem.Allocator, config: *const Config, + /// Initialize the P2P network + /// + /// # Arguments + /// - `allocator`: Memory allocator + /// - `config`: Configuration + /// + /// # Returns + /// - `P2P`: P2P network handler pub fn init(allocator: std.mem.Allocator, config: *const Config) !P2P { return P2P{ .allocator = allocator, @@ -12,11 +24,19 @@ pub const P2P = struct { }; } + /// Deinitialize the P2P network + /// + /// # Arguments + /// - `self`: P2P network handler pub fn deinit(self: *P2P) void { // Clean up resources if needed _ = self; } + /// Start the P2P network + /// + /// # Arguments + /// - `self`: P2P network handler pub fn start(self: *P2P) !void { std.log.info("Starting P2P network on port {}", .{self.config.p2p_port}); // Implement P2P network initialization diff --git a/src/rpc.zig b/src/rpc.zig index dd9f918..25147e6 100644 --- a/src/rpc.zig +++ b/src/rpc.zig @@ -4,6 +4,11 @@ const Mempool = @import("mempool.zig").Mempool; const Storage = @import("storage.zig").Storage; const P2P = @import("p2p.zig").P2P; +/// RPC Server handler. +/// +/// 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: std.mem.Allocator, config: *const Config, diff --git a/src/storage.zig b/src/storage.zig index b6f91dd..205b427 100644 --- a/src/storage.zig +++ b/src/storage.zig @@ -1,10 +1,21 @@ const std = @import("std"); const Config = @import("config.zig").Config; +/// Storage handler. +/// +/// The storage is responsible for handling the blockchain data. pub const Storage = struct { allocator: std.mem.Allocator, config: *const Config, + /// Initialize the storage + /// + /// # Arguments + /// - `allocator`: Memory allocator + /// - `config`: Configuration + /// + /// # Returns + /// - `Storage`: Storage pub fn init(allocator: std.mem.Allocator, config: *const Config) !Storage { return Storage{ .allocator = allocator, @@ -12,6 +23,10 @@ pub const Storage = struct { }; } + /// Deinitialize the storage + /// + /// # Arguments + /// - `self`: Storage pub fn deinit(self: *Storage) void { // Clean up resources if needed _ = self;