Skip to content

Commit 8c6e022

Browse files
committed
complete metadata read-in / error handling
1 parent 835bcad commit 8c6e022

File tree

1 file changed

+26
-11
lines changed

1 file changed

+26
-11
lines changed

contrib/utxo-tools/utxo_to_sqlite.zig

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ const UTXO_DUMP_VERSION: u16 = 2;
1111

1212
const NetworkEntry = struct {
1313
net_magic: [4]u8,
14-
description: []u8,
14+
description: []const u8,
1515
};
1616
const NETWORKS = [_]NetworkEntry {
17-
.{ .{0xf9,0xbe,0xb4,0xd9}, "Mainnet" },
18-
.{ .{0x0a,0x03,0xcf,0x40}, "Signet" },
19-
.{ .{0x0b,0x11,0x09,0x07}, "Testnet3" },
20-
.{ .{0x1c,0x16,0x3f,0x28}, "Testnet4" },
21-
.{ .{0xfa,0xbf,0xb5,0xda}, "Regtest" },
17+
.{ .net_magic = .{0xf9,0xbe,0xb4,0xd9}, .description = "Mainnet" },
18+
.{ .net_magic = .{0x0a,0x03,0xcf,0x40}, .description = "Signet" },
19+
.{ .net_magic = .{0x0b,0x11,0x09,0x07}, .description = "Testnet3" },
20+
.{ .net_magic = .{0x1c,0x16,0x3f,0x28}, .description = "Testnet4" },
21+
.{ .net_magic = .{0xfa,0xbf,0xb5,0xda}, .description = "Regtest" },
2222
};
2323

2424
// Equivalent of `ReadVarInt()` (see serialization module).
@@ -187,11 +187,26 @@ pub fn main() !void {
187187
const network_magic = try reader.takeArray(4);
188188
const block_hash = try reader.takeArray(32);
189189
const num_utxos = mem.readInt(u64, try reader.takeArray(8), .little);
190-
std.debug.print("magic bytes: {x}\n", .{magic_bytes});
191-
std.debug.print("version: {d}\n", .{version});
192-
std.debug.print("network magic: {x}\n", .{network_magic});
193-
std.debug.print("block hash: {x}\n", .{block_hash});
194-
std.debug.print("number of UTXOs: {d}\n", .{num_utxos});
190+
if (!mem.eql(u8, magic_bytes, &UTXO_DUMP_MAGIC)) {
191+
std.debug.print("Error: provided input file '{s}' is not an UTXO dump.\n", .{infile_path});
192+
std.process.exit(5);
193+
}
194+
if (version != UTXO_DUMP_VERSION) {
195+
std.debug.print("Error: provided input file '{s}' has unknown UTXO dump version {d} " ++
196+
"(only version {d} supported)\n" , .{infile_path, version, UTXO_DUMP_VERSION});
197+
std.process.exit(5);
198+
}
199+
var network_name: []const u8 = "unknown network"; // TODO: include magic bytes in string if unknown
200+
for (0..NETWORKS.len) |i| {
201+
if (std.mem.eql(u8, network_magic, &NETWORKS[i].net_magic)) {
202+
network_name = NETWORKS[i].description;
203+
break;
204+
}
205+
}
206+
var block_hash_reverse = block_hash;
207+
std.mem.reverse(u8, block_hash_reverse);
208+
std.debug.print("UTXO Snapshot for {s} at block hash {x}..., contains {d} coins\n",
209+
.{network_name, block_hash_reverse[0..16], num_utxos});
195210

196211
// TODO: implement coins conversion loop
197212
// TODO: write summary at the end

0 commit comments

Comments
 (0)