Skip to content

Commit

Permalink
🚧 init zig doc for components
Browse files Browse the repository at this point in the history
  • Loading branch information
AbdelStark committed Sep 3, 2024
1 parent 3b9a17a commit 5a652b2
Show file tree
Hide file tree
Showing 10 changed files with 128 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 27 additions & 0 deletions docs/architecture.md
Original file line number Diff line number Diff line change
@@ -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`.
9 changes: 9 additions & 0 deletions docs/roadmap.md
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions src/cli.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand Down
24 changes: 24 additions & 0 deletions src/config.zig
Original file line number Diff line number Diff line change
@@ -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();
Expand Down
6 changes: 6 additions & 0 deletions src/lib.zig
Original file line number Diff line number Diff line change
@@ -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());
}
15 changes: 15 additions & 0 deletions src/mempool.zig
Original file line number Diff line number Diff line change
@@ -1,17 +1,32 @@
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,
.config = config,
};
}

/// Deinitialize the mempool
///
/// # Arguments
/// - `self`: Mempool
pub fn deinit(self: *Mempool) void {
// Clean up resources if needed
_ = self;
Expand Down
20 changes: 20 additions & 0 deletions src/p2p.zig
Original file line number Diff line number Diff line change
@@ -1,22 +1,42 @@
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,
.config = config,
};
}

/// 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
Expand Down
5 changes: 5 additions & 0 deletions src/rpc.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
15 changes: 15 additions & 0 deletions src/storage.zig
Original file line number Diff line number Diff line change
@@ -1,17 +1,32 @@
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,
.config = config,
};
}

/// Deinitialize the storage
///
/// # Arguments
/// - `self`: Storage
pub fn deinit(self: *Storage) void {
// Clean up resources if needed
_ = self;
Expand Down

0 comments on commit 5a652b2

Please sign in to comment.