-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
54 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; |