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 4b25c7b
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 18 deletions.
4 changes: 4 additions & 0 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 Down
4 changes: 4 additions & 0 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
.name = "btczee",
.version = "0.0.1",
.dependencies = .{
.lmdb = .{
.url = "https://github.com/canvasxyz/zig-lmdb/archive/refs/tags/v0.1.0.tar.gz",
.hash = "1220d5ca02660a791ea022d60a032ae56b629002d3930117d8047ecf615f012044f7",
},
.zul = .{
.url = "https://github.com/karlseguin/zul/archive/ae0c27350c0db6b460f22cba30b6b0c4a02d1ffd.zip",
.hash = "1220457e2c8867f6734520d9b335f01e1d851d6fe7adaa7f6f0756158acaf6c5e87f",
Expand Down
6 changes: 6 additions & 0 deletions src/block.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const std = @import("std");

pub const Block = struct {
hash: [32]u8,
height: i32,
};
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
1 change: 1 addition & 0 deletions src/lib.zig
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub const rpc = @import("network/rpc.zig");
pub const storage = @import("storage/storage.zig");
pub const wallet = @import("wallet/wallet.zig");
pub const miner = @import("miner/miner.zig");
pub usingnamespace @import("block.zig");

test {
@import("std").testing.refAllDeclsRecursive(@This());
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 lmdb = @import("lmdb");
const Block = @import("block").Block;

/// 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(txn: Transaction, block: *Block) !void {
const blocks = try txn.database("blocks", .{ .create = true });
try blocks.set(block.hash, block);
}
};

0 comments on commit 4b25c7b

Please sign in to comment.