Skip to content

Commit

Permalink
Some fixes and build indexer and wbx
Browse files Browse the repository at this point in the history
  • Loading branch information
iskyd committed Sep 28, 2024
1 parent 0065869 commit cb26d21
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 44 deletions.
73 changes: 37 additions & 36 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,22 @@ pub fn build(b: *std.Build) void {
// set a preferred release mode, allowing the user to decide how to optimize.
const optimize = b.standardOptimizeOption(.{});

const exe = b.addExecutable(.{
.name = "walle",
// indexer
const indexer = b.addExecutable(.{
.name = "indexer",
// In this case the main source file is merely a path, however, in more
// complicated build scripts, this could be a generated file.
.root_source_file = b.path("src/main.zig"),
.root_source_file = b.path("src/indexer.zig"),
.target = target,
.optimize = optimize,
});

// walle bitcoin explorer
const wbx = b.addExecutable(.{
.name = "wbx",
// In this case the main source file is merely a path, however, in more
// complicated build scripts, this could be a generated file.
.root_source_file = b.path("src/wbx.zig"),
.target = target,
.optimize = optimize,
});
Expand All @@ -35,10 +46,6 @@ pub fn build(b: *std.Build) void {
.root_source_file = b.path("src/crypto/crypto.zig"),
});

exe.root_module.addImport("base58", base58);
exe.root_module.addImport("clap", clap);
exe.root_module.addImport("crypto", crypto);

const sqlite = b.addModule("sqlite", .{
.root_source_file = b.path("lib/zig-sqlite/sqlite.zig"),
});
Expand All @@ -49,57 +56,51 @@ pub fn build(b: *std.Build) void {
.flags = &[_][]const u8{"-std=c99"},
});
sqlite.addIncludePath(b.path("lib/zig-sqlite/c"));
exe.linkLibC();
exe.linkSystemLibrary("sqlite3");
exe.root_module.addImport("sqlite", sqlite);

indexer.root_module.addImport("base58", base58);
indexer.root_module.addImport("clap", clap);
indexer.root_module.addImport("crypto", crypto);
indexer.linkLibC();
indexer.linkSystemLibrary("sqlite3");
indexer.root_module.addImport("sqlite", sqlite);

wbx.root_module.addImport("base58", base58);
wbx.root_module.addImport("crypto", crypto);

// This declares intent for the executable to be installed into the
// standard location when the user invokes the "install" step (the default
// step when running `zig build`).
b.installArtifact(exe);

// Check step, used for zls
const exe_check = b.addExecutable(.{
.name = "walle",
// In this case the main source file is merely a path, however, in more
// complicated build scripts, this could be a generated file.
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});

exe_check.root_module.addImport("base58", base58);
exe_check.root_module.addImport("clap", clap);
exe_check.root_module.addImport("crypto", crypto);
exe_check.linkLibC();
exe_check.linkSystemLibrary("sqlite3");
exe_check.root_module.addImport("sqlite", sqlite);

const check = b.step("check", "Check if main compiles");
check.dependOn(&exe_check.step);
b.installArtifact(indexer);
b.installArtifact(wbx);

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

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

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

// This creates a build step. It will be visible in the `zig build --help` menu,
// and can be selected like this: `zig build run`
// This will evaluate the `run` step rather than the default, which is "install".
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);
const run_step_indexer = b.step("run_indexer", "Run the indexer");
run_step_indexer.dependOn(&run_cmd_indexer.step);

const run_step_wbx = b.step("run_wbx", "Run the walle bitcoin explorer");
run_step_wbx.dependOn(&run_cmd_wbx.step);

// Similar to creating the run step earlier, this exposes a `test` step to
// the `zig build --help` menu, providing a way for the user to request
Expand Down
2 changes: 1 addition & 1 deletion src/db/db.zig
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ pub fn saveTransaction(db: *sqlite.Db, txid: [64]u8, transaction_raw: []u8, is_c
const sql_transaction = "INSERT OR IGNORE INTO transactions(txid, raw, block_heigth, is_coinbase) VALUES(?, ?, ?, ?)";
var stmt_transaction = try db.prepare(sql_transaction);
defer stmt_transaction.deinit();
try stmt_transaction.exec(.{}, .{ .txid = txid, .raw = transaction_raw.?, .block_heigth = block_heigth, .is_coinbase = is_coinbase });
try stmt_transaction.exec(.{}, .{ .txid = txid, .raw = transaction_raw, .block_heigth = block_heigth, .is_coinbase = is_coinbase });
}

pub fn getCurrentBlockHeigth(db: *sqlite.Db) !?usize {
Expand Down
2 changes: 1 addition & 1 deletion src/indexer.zig
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ pub fn main() !void {
while (it.next()) |txid| {
const raw = raw_transactions_map.get(txid.*).?;
const is_coinbase = relevant_transactions.get(txid.*).?;
try db.saveTransaction(&database, txid, raw, is_coinbase, i);
try db.saveTransaction(&database, txid.*, raw, is_coinbase, i);
}
}
// 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.
Expand Down
2 changes: 1 addition & 1 deletion src/tx.zig
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ pub const Transaction = struct {
const input = self.inputs.items[i];
try writer.print(" txid: {s}\n", .{input.prevout.?.txid});
try writer.print(" reverse txid: {s}\n", .{try utils.reverseByteOrderFromHex(64, input.prevout.?.txid)});
try writer.print(" n: {d}\n", .{input.prevout.?.n});
try writer.print(" n: {d}\n", .{input.prevout.?.vout});
try writer.print(" sequence: {d}\n\n", .{input.sequence});
}
try writer.print("Outputs: \n", .{});
Expand Down
9 changes: 4 additions & 5 deletions src/main.zig → src/wbx.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ const tx = @import("tx.zig");
const deriveP2WPKHAddress = @import("address.zig").deriveP2WPKHAddress;

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

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

const epk = bip32.generateExtendedMasterPrivateKey(&seed);
const addr = epk.address(.SEGWIT_MAINNET, 0, [4]u8{ 0, 0, 0, 0 }, 0) catch {
const addr = epk.address(.segwit_mainnet, 0, [4]u8{ 0, 0, 0, 0 }, 0) catch {
std.debug.print("Error while generating address", .{});
return;
};
Expand Down Expand Up @@ -155,7 +154,7 @@ pub fn main() !void {
var bytes: [33]u8 = undefined;
_ = try std.fmt.hexToBytes(&bytes, &compressedpublic);
const fingerprint = utils.hash160(&bytes)[0..4].*;
const addr = current.address(.SEGWIT_MAINNET, depth, fingerprint, lastindex) catch {
const addr = current.address(.segwit_mainnet, depth, fingerprint, lastindex) catch {
std.debug.print("Error while converting to address\n", .{});
return;
};
Expand All @@ -179,7 +178,7 @@ pub fn main() !void {
};
defer s.deinit();

const addr = deriveP2WPKHAddress(allocator, s, .MAINNET) catch {
const addr = deriveP2WPKHAddress(allocator, s, .mainnet) catch {
std.debug.print("Error while generating address\n", .{});
return;
};
Expand Down

0 comments on commit cb26d21

Please sign in to comment.