Skip to content

Commit

Permalink
feat: add lmdb database in Storage
Browse files Browse the repository at this point in the history
  • Loading branch information
tdelabro committed Sep 5, 2024
1 parent 8c0dc33 commit 7bfbecd
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 25 deletions.
9 changes: 4 additions & 5 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ const external_dependencies = [_]build_helpers.Dependency{
.name = "httpz",
.module_name = "httpz",
},
.{
.name = "lmdb",
.module_name = "lmdb",
},
};

pub fn build(b: *std.Build) !void {
Expand All @@ -30,16 +34,11 @@ pub fn build(b: *std.Build) !void {
// **************************************************************
// * HANDLE DEPENDENCY MODULES *
// **************************************************************
const dependencies_opts = .{
.target = target,
.optimize = optimize,
};

// This array can be passed to add the dependencies to lib, executable, tests, etc using `addModule` function.
const deps = build_helpers.generateModuleDependencies(
b,
&external_dependencies,
dependencies_opts,
) catch unreachable;

// **************************************************************
Expand Down
4 changes: 4 additions & 0 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
.url = "git+https://github.com/Hejsil/zig-clap#2d9db156ae928860a9acf2f1260750d3b44a4c98",
.hash = "122005e589ab3b6bff8e589b45f5b12cd27ce79f266bdac17e9f33ebfe2fbaff7fe3",
},
.lmdb = .{
.url = "git+https://github.com/zig-bitcoin/zig-lmdb#9d6d9db15ed957674d9702aa7745ed4cf7b7a443",
.hash = "1220d5ca02660a791ea022d60a032ae56b629002d3930117d8047ecf615f012044f7",
},
},
.paths = .{
"build.zig",
Expand Down
3 changes: 1 addition & 2 deletions build_helpers.zig
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ pub const Dependency = struct {
pub fn generateModuleDependencies(
b: *std.Build,
external_dependencies: []const Dependency,
dependencies_opts: anytype,
) ![]std.Build.Module.Import {
var dependency_modules = std.ArrayList(*std.Build.Module).init(b.allocator);
defer _ = dependency_modules.deinit();
Expand All @@ -25,7 +24,7 @@ pub fn generateModuleDependencies(
for (external_dependencies) |dep| {
const module = b.dependency(
dep.name,
dependencies_opts,
.{},
).module(dep.module_name);
_ = dependency_modules.append(module) catch unreachable;
}
Expand Down
6 changes: 3 additions & 3 deletions src/config/config.zig
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub const Config = struct {
testnet: bool,

/// Data directory
datadir: []const u8,
datadir: [:0]const u8,

seednode: []const u8,

Expand All @@ -45,7 +45,7 @@ pub const Config = struct {
.rpc_port = 8332,
.p2p_port = 8333,
.testnet = false,
.datadir = try allocator.dupe(u8, ".bitcoin"),
.datadir = ".bitcoin",
.seednode = "",
};

Expand All @@ -63,7 +63,7 @@ pub const Config = struct {
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);
config.datadir = try allocator.dupeZ(u8, value);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ fn runNodeCommand(program: *Program) !void {
var mempool = try Mempool.init(program.allocator, &config);
defer mempool.deinit();

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

var p2p = try P2P.init(program.allocator, &config);
Expand Down
49 changes: 35 additions & 14 deletions src/storage/storage.zig
Original file line number Diff line number Diff line change
@@ -1,34 +1,55 @@
const std = @import("std");
const Config = @import("../config/config.zig").Config;
const Block = @import("../types/block.zig").Block;
const lmdb = @import("lmdb");

/// Storage handler.
///
/// The storage is responsible for handling the blockchain data.
pub const Storage = struct {
allocator: std.mem.Allocator,
config: *const Config,
env: lmdb.Environment,

/// Initialize the storage
///
/// # Arguments
/// - `allocator`: Memory allocator
/// - `config`: Configuration
///
/// # Returns
/// - `Storage`: Storage
pub fn init(allocator: std.mem.Allocator, config: *const Config) !Storage {
/// Will create the full path to the directory if it doesn't already exist.
pub fn init(config: *const Config) !Storage {
const datadir = config.datadir;
try std.fs.cwd().makePath(datadir);

// Init the db env
// `max_dbs` is set to 1:
// - "blocks"
const env = try lmdb.Environment.init(datadir, .{ .max_dbs = 1 });

return Storage{
.allocator = allocator,
.config = config,
.env = env,
};
}

/// Deinitialize the storage
///
/// # Arguments
/// - `self`: Storage
pub fn deinit(self: *Storage) void {
// Clean up resources if needed
_ = self;
/// Release the lmdb environment handle.
pub fn deinit(self: Storage) void {
self.env.deinit();
}

pub fn init_transaction(self: Storage) !Transaction {
const txn = try lmdb.Transaction.init(self.env, .{ .mode = .ReadWrite });
return Transaction{ .txn = txn };
}
};

pub const Transaction = struct {
txn: lmdb.Transaction,

pub fn abort(self: Transaction) void {
self.txn.abort();
}

pub fn store_block(allocator: std.mem.Allocator, txn: Transaction, block: *Block) !void {
const blocks = try txn.txn.database("blocks", .{ .create = true });
try blocks.set(&block.hash, try block.serizalize(allocator));
}
};
12 changes: 12 additions & 0 deletions src/types/block.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const std = @import("std");

pub const Block = struct {
hash: [32]u8,
height: i32,

pub fn serizalize(self: *Block, allocator: std.mem.Allocator) ![]u8 {
_ = self;
const ret = try allocator.alloc(u8, 0);
return ret;
}
};

0 comments on commit 7bfbecd

Please sign in to comment.