Skip to content

Commit cb26d21

Browse files
committed
Some fixes and build indexer and wbx
1 parent 0065869 commit cb26d21

File tree

5 files changed

+44
-44
lines changed

5 files changed

+44
-44
lines changed

build.zig

+37-36
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,22 @@ pub fn build(b: *std.Build) void {
1616
// set a preferred release mode, allowing the user to decide how to optimize.
1717
const optimize = b.standardOptimizeOption(.{});
1818

19-
const exe = b.addExecutable(.{
20-
.name = "walle",
19+
// indexer
20+
const indexer = b.addExecutable(.{
21+
.name = "indexer",
2122
// In this case the main source file is merely a path, however, in more
2223
// complicated build scripts, this could be a generated file.
23-
.root_source_file = b.path("src/main.zig"),
24+
.root_source_file = b.path("src/indexer.zig"),
25+
.target = target,
26+
.optimize = optimize,
27+
});
28+
29+
// walle bitcoin explorer
30+
const wbx = b.addExecutable(.{
31+
.name = "wbx",
32+
// In this case the main source file is merely a path, however, in more
33+
// complicated build scripts, this could be a generated file.
34+
.root_source_file = b.path("src/wbx.zig"),
2435
.target = target,
2536
.optimize = optimize,
2637
});
@@ -35,10 +46,6 @@ pub fn build(b: *std.Build) void {
3546
.root_source_file = b.path("src/crypto/crypto.zig"),
3647
});
3748

38-
exe.root_module.addImport("base58", base58);
39-
exe.root_module.addImport("clap", clap);
40-
exe.root_module.addImport("crypto", crypto);
41-
4249
const sqlite = b.addModule("sqlite", .{
4350
.root_source_file = b.path("lib/zig-sqlite/sqlite.zig"),
4451
});
@@ -49,57 +56,51 @@ pub fn build(b: *std.Build) void {
4956
.flags = &[_][]const u8{"-std=c99"},
5057
});
5158
sqlite.addIncludePath(b.path("lib/zig-sqlite/c"));
52-
exe.linkLibC();
53-
exe.linkSystemLibrary("sqlite3");
54-
exe.root_module.addImport("sqlite", sqlite);
59+
60+
indexer.root_module.addImport("base58", base58);
61+
indexer.root_module.addImport("clap", clap);
62+
indexer.root_module.addImport("crypto", crypto);
63+
indexer.linkLibC();
64+
indexer.linkSystemLibrary("sqlite3");
65+
indexer.root_module.addImport("sqlite", sqlite);
66+
67+
wbx.root_module.addImport("base58", base58);
68+
wbx.root_module.addImport("crypto", crypto);
5569

5670
// This declares intent for the executable to be installed into the
5771
// standard location when the user invokes the "install" step (the default
5872
// step when running `zig build`).
59-
b.installArtifact(exe);
60-
61-
// Check step, used for zls
62-
const exe_check = b.addExecutable(.{
63-
.name = "walle",
64-
// In this case the main source file is merely a path, however, in more
65-
// complicated build scripts, this could be a generated file.
66-
.root_source_file = b.path("src/main.zig"),
67-
.target = target,
68-
.optimize = optimize,
69-
});
70-
71-
exe_check.root_module.addImport("base58", base58);
72-
exe_check.root_module.addImport("clap", clap);
73-
exe_check.root_module.addImport("crypto", crypto);
74-
exe_check.linkLibC();
75-
exe_check.linkSystemLibrary("sqlite3");
76-
exe_check.root_module.addImport("sqlite", sqlite);
77-
78-
const check = b.step("check", "Check if main compiles");
79-
check.dependOn(&exe_check.step);
73+
b.installArtifact(indexer);
74+
b.installArtifact(wbx);
8075

8176
// This *creates* a Run step in the build graph, to be executed when another
8277
// step is evaluated that depends on it. The next line below will establish
8378
// such a dependency.
84-
const run_cmd = b.addRunArtifact(exe);
79+
const run_cmd_indexer = b.addRunArtifact(indexer);
80+
const run_cmd_wbx = b.addRunArtifact(wbx);
8581

8682
// By making the run step depend on the install step, it will be run from the
8783
// installation directory rather than directly from within the cache directory.
8884
// This is not necessary, however, if the application depends on other installed
8985
// files, this ensures they will be present and in the expected location.
90-
run_cmd.step.dependOn(b.getInstallStep());
86+
run_cmd_indexer.step.dependOn(b.getInstallStep());
87+
run_cmd_wbx.step.dependOn(b.getInstallStep());
9188

9289
// This allows the user to pass arguments to the application in the build
9390
// command itself, like this: `zig build run -- arg1 arg2 etc`
9491
if (b.args) |args| {
95-
run_cmd.addArgs(args);
92+
run_cmd_indexer.addArgs(args);
93+
run_cmd_wbx.addArgs(args);
9694
}
9795

9896
// This creates a build step. It will be visible in the `zig build --help` menu,
9997
// and can be selected like this: `zig build run`
10098
// This will evaluate the `run` step rather than the default, which is "install".
101-
const run_step = b.step("run", "Run the app");
102-
run_step.dependOn(&run_cmd.step);
99+
const run_step_indexer = b.step("run_indexer", "Run the indexer");
100+
run_step_indexer.dependOn(&run_cmd_indexer.step);
101+
102+
const run_step_wbx = b.step("run_wbx", "Run the walle bitcoin explorer");
103+
run_step_wbx.dependOn(&run_cmd_wbx.step);
103104

104105
// Similar to creating the run step earlier, this exposes a `test` step to
105106
// the `zig build --help` menu, providing a way for the user to request

src/db/db.zig

+1-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ pub fn saveTransaction(db: *sqlite.Db, txid: [64]u8, transaction_raw: []u8, is_c
148148
const sql_transaction = "INSERT OR IGNORE INTO transactions(txid, raw, block_heigth, is_coinbase) VALUES(?, ?, ?, ?)";
149149
var stmt_transaction = try db.prepare(sql_transaction);
150150
defer stmt_transaction.deinit();
151-
try stmt_transaction.exec(.{}, .{ .txid = txid, .raw = transaction_raw.?, .block_heigth = block_heigth, .is_coinbase = is_coinbase });
151+
try stmt_transaction.exec(.{}, .{ .txid = txid, .raw = transaction_raw, .block_heigth = block_heigth, .is_coinbase = is_coinbase });
152152
}
153153

154154
pub fn getCurrentBlockHeigth(db: *sqlite.Db) !?usize {

src/indexer.zig

+1-1
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ pub fn main() !void {
235235
while (it.next()) |txid| {
236236
const raw = raw_transactions_map.get(txid.*).?;
237237
const is_coinbase = relevant_transactions.get(txid.*).?;
238-
try db.saveTransaction(&database, txid, raw, is_coinbase, i);
238+
try db.saveTransaction(&database, txid.*, raw, is_coinbase, i);
239239
}
240240
}
241241
// Since db writes are not in a single transaction we commit block as lastest so that if we restart we dont't risk loosing informations, once block is persisted we are sure outputs, inputs and relevant transactions in that block are persisted too. We can recover from partial commit simply reindexing the block.

src/tx.zig

+1-1
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ pub const Transaction = struct {
197197
const input = self.inputs.items[i];
198198
try writer.print(" txid: {s}\n", .{input.prevout.?.txid});
199199
try writer.print(" reverse txid: {s}\n", .{try utils.reverseByteOrderFromHex(64, input.prevout.?.txid)});
200-
try writer.print(" n: {d}\n", .{input.prevout.?.n});
200+
try writer.print(" n: {d}\n", .{input.prevout.?.vout});
201201
try writer.print(" sequence: {d}\n\n", .{input.sequence});
202202
}
203203
try writer.print("Outputs: \n", .{});

src/main.zig renamed to src/wbx.zig

+4-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ const tx = @import("tx.zig");
88
const deriveP2WPKHAddress = @import("address.zig").deriveP2WPKHAddress;
99

1010
pub fn main() !void {
11-
std.debug.print("WALL-E. Bitcoin Wallet written in Zig\n", .{});
1211
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
1312
const allocator = gpa.allocator();
1413

@@ -61,7 +60,7 @@ pub fn main() !void {
6160
defer allocator.free(bytes);
6261
_ = try std.fmt.hexToBytes(bytes, seed);
6362
const epk = bip32.generateExtendedMasterPrivateKey(bytes);
64-
const addr = epk.address(.SEGWIT_MAINNET, 0, [4]u8{ 0, 0, 0, 0 }, 0) catch {
63+
const addr = epk.address(.segwit_mainnet, 0, [4]u8{ 0, 0, 0, 0 }, 0) catch {
6564
std.debug.print("Error while generating address", .{});
6665
return;
6766
};
@@ -85,7 +84,7 @@ pub fn main() !void {
8584
try bip39.mnemonicToSeed(allocator, &mnemonic, "", &seed);
8685

8786
const epk = bip32.generateExtendedMasterPrivateKey(&seed);
88-
const addr = epk.address(.SEGWIT_MAINNET, 0, [4]u8{ 0, 0, 0, 0 }, 0) catch {
87+
const addr = epk.address(.segwit_mainnet, 0, [4]u8{ 0, 0, 0, 0 }, 0) catch {
8988
std.debug.print("Error while generating address", .{});
9089
return;
9190
};
@@ -155,7 +154,7 @@ pub fn main() !void {
155154
var bytes: [33]u8 = undefined;
156155
_ = try std.fmt.hexToBytes(&bytes, &compressedpublic);
157156
const fingerprint = utils.hash160(&bytes)[0..4].*;
158-
const addr = current.address(.SEGWIT_MAINNET, depth, fingerprint, lastindex) catch {
157+
const addr = current.address(.segwit_mainnet, depth, fingerprint, lastindex) catch {
159158
std.debug.print("Error while converting to address\n", .{});
160159
return;
161160
};
@@ -179,7 +178,7 @@ pub fn main() !void {
179178
};
180179
defer s.deinit();
181180

182-
const addr = deriveP2WPKHAddress(allocator, s, .MAINNET) catch {
181+
const addr = deriveP2WPKHAddress(allocator, s, .mainnet) catch {
183182
std.debug.print("Error while generating address\n", .{});
184183
return;
185184
};

0 commit comments

Comments
 (0)