Skip to content

Commit

Permalink
🚧 init boilerplate
Browse files Browse the repository at this point in the history
  • Loading branch information
AbdelStark committed Sep 3, 2024
1 parent 71c03a0 commit 3a5fdbd
Show file tree
Hide file tree
Showing 13 changed files with 227 additions and 61 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ You can find the roadmap of the project in the [docs/roadmap.md](./docs/roadmap.
- [Bitcoin Core](https://github.com/bitcoin/bitcoin)
- [Learn me a bitcoin](https://learnmeabitcoin.com/)
- [Mastering Bitcoin](https://github.com/bitcoinbook/bitcoinbook)
- [Onboarding to Bitcoin Core](https://github.com/chaincodelabs/onboarding-to-bitcoin-core)
- [Zig](https://github.com/ziglang/zig)
- [Zig Standard Library](https://github.com/ziglang/zig/tree/master/lib/std)
- [Ziglings](https://codeberg.org/ziglings/exercises/)
9 changes: 9 additions & 0 deletions bitcoin.conf.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Bitcoin configuration file

# Network-related settings
testnet=0
port=8333
rpcport=8332

# Data directory
datadir=.bitcoin
24 changes: 24 additions & 0 deletions src/cli.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const std = @import("std");
const Config = @import("config.zig").Config;
const RPC = @import("rpc.zig").RPC;

pub const CLI = struct {
allocator: std.mem.Allocator,
config: *const Config,
rpc: *RPC,

pub fn init(allocator: std.mem.Allocator, config: *const Config, rpc: *RPC) !CLI {
return CLI{
.allocator = allocator,
.config = config,
.rpc = rpc,
};
}

pub fn deinit(self: *CLI) void {
// Clean up resources if needed
_ = self;
}

pub fn start(_: *CLI) !void {}
};
49 changes: 49 additions & 0 deletions src/config.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const std = @import("std");

pub const Config = struct {
allocator: std.mem.Allocator,
rpc_port: u16,
p2p_port: u16,
testnet: bool,
datadir: []const u8,

pub fn load(allocator: std.mem.Allocator, filename: []const u8) !Config {
const file = try std.fs.cwd().openFile(filename, .{});
defer file.close();

var buf_reader = std.io.bufferedReader(file.reader());
var in_stream = buf_reader.reader();

var config = Config{
.allocator = allocator,
.rpc_port = 8332,
.p2p_port = 8333,
.testnet = false,
.datadir = try allocator.dupe(u8, ".bitcoin"),
};

var buf: [1024]u8 = undefined;
while (try in_stream.readUntilDelimiterOrEof(&buf, '\n')) |line| {
var it = std.mem.splitSequence(u8, line, "=");
const key = it.next() orelse continue;
const value = it.next() orelse continue;

if (std.mem.eql(u8, key, "rpcport")) {
config.rpc_port = try std.fmt.parseInt(u16, value, 10);
} else if (std.mem.eql(u8, key, "port")) {
config.p2p_port = try std.fmt.parseInt(u16, value, 10);
} else if (std.mem.eql(u8, key, "testnet")) {
config.testnet = std.mem.eql(u8, value, "1");
} else if (std.mem.eql(u8, key, "datadir")) {
allocator.free(config.datadir);
config.datadir = try allocator.dupe(u8, value);
}
}

return config;
}

pub fn deinit(self: *Config) void {
self.allocator.free(self.datadir);
}
};
1 change: 0 additions & 1 deletion src/core/lib.zig

This file was deleted.

16 changes: 0 additions & 16 deletions src/core/mempool.zig

This file was deleted.

3 changes: 0 additions & 3 deletions src/lib.zig
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
pub usingnamespace @import("core/lib.zig");
pub usingnamespace @import("primitives/lib.zig");

test {
@import("std").testing.refAllDeclsRecursive(@This());
}
90 changes: 49 additions & 41 deletions src/main.zig
Original file line number Diff line number Diff line change
@@ -1,49 +1,57 @@
const std = @import("std");
const cli = @import("zig-cli");
const Config = @import("config.zig").Config;
const Mempool = @import("mempool.zig").Mempool;
const Storage = @import("storage.zig").Storage;
const P2P = @import("p2p.zig").P2P;
const RPC = @import("rpc.zig").RPC;
const CLI = @import("cli.zig").CLI;

// Configuration settings for the CLI
const Args = struct {
mint: bool = false,
mnemonic: bool = false,
};
pub fn main() !void {
// Initialize the allocator
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();

var cfg: Args = .{};
// Load configuration
var config = try Config.load(allocator, "bitcoin.conf.example");
defer config.deinit();

pub fn main() !void {
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena.deinit();

const allocator = arena.allocator();

var r = try cli.AppRunner.init(allocator);
defer r.deinit();

// Define the CLI app
const app = cli.App{
.version = "0.0.1",
.author = "@AbdelStark",
.command = .{
.name = "btczee",
.target = .{
.subcommands = &.{
.{
.name = "info",
.description = .{
.one_line = "Display information about btczee",
},
.options = &.{},
.target = .{ .action = .{ .exec = displayInfo } },
},
},
},
},
};

return r.run(&app);
// Initialize components
var mempool = try Mempool.init(allocator, &config);
defer mempool.deinit();

var storage = try Storage.init(allocator, &config);
defer storage.deinit();

var p2p = try P2P.init(allocator, &config);
defer p2p.deinit();

var rpc = try RPC.init(allocator, &config, &mempool, &storage, &p2p);
defer rpc.deinit();

var cli = try CLI.init(allocator, &config, &rpc);
defer cli.deinit();

// Start the node
try startNode(&mempool, &storage, &p2p, &rpc, &cli);
}

fn displayInfo() !void {
const stdout = std.io.getStdOut().writer();
fn startNode(_: *Mempool, _: *Storage, p2p: *P2P, rpc: *RPC, cli: *CLI) !void {
std.log.info("Starting btczee node...", .{});

// Start P2P network
try p2p.start();

// Start RPC server
try rpc.start();

// Start CLI
try cli.start();

try stdout.print("Version: 0.1.0\n", .{});
// Main event loop
while (true) {
// Handle events, process blocks, etc.
std.log.debug("Waiting for blocks...", .{});
std.time.sleep(std.time.ns_per_s);
}
}
19 changes: 19 additions & 0 deletions src/mempool.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const std = @import("std");
const Config = @import("config.zig").Config;

pub const Mempool = struct {
allocator: std.mem.Allocator,
config: *const Config,

pub fn init(allocator: std.mem.Allocator, config: *const Config) !Mempool {
return Mempool{
.allocator = allocator,
.config = config,
};
}

pub fn deinit(self: *Mempool) void {
// Clean up resources if needed
_ = self;
}
};
24 changes: 24 additions & 0 deletions src/p2p.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const std = @import("std");
const Config = @import("config.zig").Config;

pub const P2P = struct {
allocator: std.mem.Allocator,
config: *const Config,

pub fn init(allocator: std.mem.Allocator, config: *const Config) !P2P {
return P2P{
.allocator = allocator,
.config = config,
};
}

pub fn deinit(self: *P2P) void {
// Clean up resources if needed
_ = self;
}

pub fn start(self: *P2P) !void {
std.log.info("Starting P2P network on port {}", .{self.config.p2p_port});
// Implement P2P network initialization
}
};
Empty file removed src/primitives/lib.zig
Empty file.
33 changes: 33 additions & 0 deletions src/rpc.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const std = @import("std");
const Config = @import("config.zig").Config;
const Mempool = @import("mempool.zig").Mempool;
const Storage = @import("storage.zig").Storage;
const P2P = @import("p2p.zig").P2P;

pub const RPC = struct {
allocator: std.mem.Allocator,
config: *const Config,
mempool: *Mempool,
storage: *Storage,
p2p: *P2P,

pub fn init(allocator: std.mem.Allocator, config: *const Config, mempool: *Mempool, storage: *Storage, p2p: *P2P) !RPC {
return RPC{
.allocator = allocator,
.config = config,
.mempool = mempool,
.storage = storage,
.p2p = p2p,
};
}

pub fn deinit(self: *RPC) void {
// Clean up resources if needed
_ = self;
}

pub fn start(self: *RPC) !void {
std.log.info("Starting RPC server on port {}", .{self.config.rpc_port});
// Implement RPC server initialization
}
};
19 changes: 19 additions & 0 deletions src/storage.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const std = @import("std");
const Config = @import("config.zig").Config;

pub const Storage = struct {
allocator: std.mem.Allocator,
config: *const Config,

pub fn init(allocator: std.mem.Allocator, config: *const Config) !Storage {
return Storage{
.allocator = allocator,
.config = config,
};
}

pub fn deinit(self: *Storage) void {
// Clean up resources if needed
_ = self;
}
};

0 comments on commit 3a5fdbd

Please sign in to comment.