From 3a5fdbda3e440b4f9e4fc1523df64d6df6c6b3bf Mon Sep 17 00:00:00 2001 From: "Abdel @ StarkWare" Date: Tue, 3 Sep 2024 09:51:12 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=A7=20init=20boilerplate?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + bitcoin.conf.example | 9 +++++ src/cli.zig | 24 +++++++++++ src/config.zig | 49 +++++++++++++++++++++++ src/core/lib.zig | 1 - src/core/mempool.zig | 16 -------- src/lib.zig | 3 -- src/main.zig | 90 +++++++++++++++++++++++------------------- src/mempool.zig | 19 +++++++++ src/p2p.zig | 24 +++++++++++ src/primitives/lib.zig | 0 src/rpc.zig | 33 ++++++++++++++++ src/storage.zig | 19 +++++++++ 13 files changed, 227 insertions(+), 61 deletions(-) create mode 100644 bitcoin.conf.example create mode 100644 src/cli.zig create mode 100644 src/config.zig delete mode 100644 src/core/lib.zig delete mode 100644 src/core/mempool.zig create mode 100644 src/mempool.zig create mode 100644 src/p2p.zig delete mode 100644 src/primitives/lib.zig create mode 100644 src/rpc.zig create mode 100644 src/storage.zig diff --git a/README.md b/README.md index 7e98a42..0669c37 100644 --- a/README.md +++ b/README.md @@ -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/) diff --git a/bitcoin.conf.example b/bitcoin.conf.example new file mode 100644 index 0000000..d6ee530 --- /dev/null +++ b/bitcoin.conf.example @@ -0,0 +1,9 @@ +# Bitcoin configuration file + +# Network-related settings +testnet=0 +port=8333 +rpcport=8332 + +# Data directory +datadir=.bitcoin \ No newline at end of file diff --git a/src/cli.zig b/src/cli.zig new file mode 100644 index 0000000..3fd0171 --- /dev/null +++ b/src/cli.zig @@ -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 {} +}; diff --git a/src/config.zig b/src/config.zig new file mode 100644 index 0000000..547f00d --- /dev/null +++ b/src/config.zig @@ -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); + } +}; diff --git a/src/core/lib.zig b/src/core/lib.zig deleted file mode 100644 index e19f3d5..0000000 --- a/src/core/lib.zig +++ /dev/null @@ -1 +0,0 @@ -pub usingnamespace @import("mempool.zig"); diff --git a/src/core/mempool.zig b/src/core/mempool.zig deleted file mode 100644 index 0520de7..0000000 --- a/src/core/mempool.zig +++ /dev/null @@ -1,16 +0,0 @@ -const std = @import("std"); - -const crypto = std.crypto; - -pub const Mempool = struct { - const Self = @This(); - allocator: std.mem.Allocator, - - pub fn init(allocator: std.mem.Allocator) !Self { - return .{ - .allocator = allocator, - }; - } - - pub fn deinit(_: Self) void {} -}; diff --git a/src/lib.zig b/src/lib.zig index 962b342..56374ae 100644 --- a/src/lib.zig +++ b/src/lib.zig @@ -1,6 +1,3 @@ -pub usingnamespace @import("core/lib.zig"); -pub usingnamespace @import("primitives/lib.zig"); - test { @import("std").testing.refAllDeclsRecursive(@This()); } diff --git a/src/main.zig b/src/main.zig index 8f7e50b..b6bd0ae 100644 --- a/src/main.zig +++ b/src/main.zig @@ -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); + } } diff --git a/src/mempool.zig b/src/mempool.zig new file mode 100644 index 0000000..7281489 --- /dev/null +++ b/src/mempool.zig @@ -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; + } +}; diff --git a/src/p2p.zig b/src/p2p.zig new file mode 100644 index 0000000..558fea4 --- /dev/null +++ b/src/p2p.zig @@ -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 + } +}; diff --git a/src/primitives/lib.zig b/src/primitives/lib.zig deleted file mode 100644 index e69de29..0000000 diff --git a/src/rpc.zig b/src/rpc.zig new file mode 100644 index 0000000..dfb27d0 --- /dev/null +++ b/src/rpc.zig @@ -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 + } +}; diff --git a/src/storage.zig b/src/storage.zig new file mode 100644 index 0000000..b6f91dd --- /dev/null +++ b/src/storage.zig @@ -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; + } +};