forked from leishman/yam
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.zig
More file actions
226 lines (195 loc) · 7.53 KB
/
main.zig
File metadata and controls
226 lines (195 loc) · 7.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
const std = @import("std");
const yam = @import("root.zig");
const scout = @import("scout.zig");
const relay = @import("relay.zig");
const Relay = relay.Relay;
const Explorer = @import("explorer.zig").Explorer;
const Command = enum {
broadcast,
explore,
help,
};
const BroadcastArgs = struct {
tx_hex: []const u8,
peer_count: u32 = 8,
timing: relay.TimingStrategy = .staggered_random,
discover: bool = false,
};
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer {
const result = gpa.deinit();
if (result == .leak) {
std.debug.print("Memory leak detected\n", .{});
}
}
const allocator = gpa.allocator();
var args_iter = try std.process.argsWithAllocator(allocator);
defer args_iter.deinit();
// Skip program name
_ = args_iter.next();
// Get subcommand
const cmd_str = args_iter.next() orelse {
// No args = explore mode
var explorer = try Explorer.init(allocator);
defer explorer.deinit();
try explorer.run();
return;
};
const cmd: Command = if (std.mem.eql(u8, cmd_str, "broadcast"))
.broadcast
else if (std.mem.eql(u8, cmd_str, "explore"))
.explore
else if (std.mem.eql(u8, cmd_str, "--help") or std.mem.eql(u8, cmd_str, "-h") or std.mem.eql(u8, cmd_str, "help"))
.help
else {
std.debug.print("Unknown command: {s}\n\n", .{cmd_str});
printUsage();
return;
};
switch (cmd) {
.broadcast => {
const broadcast_args = parseBroadcastArgs(&args_iter) orelse {
printBroadcastUsage();
return;
};
try broadcastTransaction(allocator, broadcast_args);
},
.explore => {
var explorer = try Explorer.init(allocator);
defer explorer.deinit();
try explorer.run();
},
.help => printUsage(),
}
}
fn parseBroadcastArgs(args_iter: anytype) ?BroadcastArgs {
// First positional arg is tx_hex
const tx_hex = args_iter.next() orelse return null;
// Check it's not a flag (user forgot tx hex)
if (tx_hex.len > 0 and tx_hex[0] == '-') {
std.debug.print("Error: Transaction hex is required\n\n", .{});
return null;
}
var result = BroadcastArgs{ .tx_hex = tx_hex };
while (args_iter.next()) |arg| {
if (std.mem.eql(u8, arg, "--peers") or std.mem.eql(u8, arg, "-p")) {
if (args_iter.next()) |count_str| {
result.peer_count = std.fmt.parseInt(u32, count_str, 10) catch 5;
}
} else if (std.mem.eql(u8, arg, "--simultaneous") or std.mem.eql(u8, arg, "-s")) {
result.timing = .simultaneous;
} else if (std.mem.eql(u8, arg, "--discover") or std.mem.eql(u8, arg, "-d")) {
result.discover = true;
}
}
return result;
}
fn printUsage() void {
const usage =
\\Yam - Bitcoin P2P Network Tool
\\
\\USAGE:
\\ yam broadcast <tx_hex> [options] Broadcast a transaction
\\ yam explore Interactive network explorer (default)
\\ yam help Show this help
\\
\\Run 'yam broadcast --help' for broadcast options.
\\
;
std.debug.print("{s}", .{usage});
}
fn printBroadcastUsage() void {
const usage =
\\USAGE:
\\ yam broadcast <tx_hex> [options]
\\
\\ARGUMENTS:
\\ <tx_hex> Raw transaction hex to broadcast
\\
\\OPTIONS:
\\ --peers, -p <count> Number of peers to broadcast to (default: 8)
\\ --simultaneous, -s Send to all peers at once (default: staggered)
\\ --discover, -d Enable recursive peer discovery via getaddr
\\
\\EXAMPLES:
\\ yam broadcast 0100000001...
\\ yam broadcast 0100000001... --peers 10 --simultaneous
\\
;
std.debug.print("{s}", .{usage});
}
fn broadcastTransaction(allocator: std.mem.Allocator, args: BroadcastArgs) !void {
// Parse transaction hex to validate it
std.debug.print("=== Validating Transaction ===\n", .{});
const tx_bytes = yam.hexToBytes(allocator, args.tx_hex) catch |err| {
std.debug.print("Error: Invalid transaction hex - {}\n", .{err});
return err;
};
defer allocator.free(tx_bytes);
// Parse to verify structure and get txid
var fbs = std.io.fixedBufferStream(tx_bytes);
const tx = yam.Transaction.deserialize(fbs.reader(), allocator) catch |err| {
std.debug.print("Error: Failed to parse transaction - {}\n", .{err});
return err;
};
defer tx.deinit(allocator);
const txid_hex = try tx.txidHex(allocator);
std.debug.print("Transaction ID: {s}\n", .{txid_hex});
std.debug.print("Inputs: {d}, Outputs: {d}\n", .{ tx.inputs.len, tx.outputs.len });
var total_output_value: f64 = 0;
for (tx.outputs) |output| {
total_output_value += output.valueBtc();
}
std.debug.print("Total output: {d:.8} BTC\n", .{total_output_value});
// Discover peers
std.debug.print("\n=== Scouting for Peers ===\n", .{});
var peer_list = try scout.discoverPeers(allocator);
if (peer_list.items.len == 0) {
std.debug.print("Error: No peers discovered\n", .{});
return error.NoPeersDiscovered;
}
// If --discover flag, connect to some peers and request more via getaddr
if (args.discover) {
std.debug.print("\n=== Recursive Discovery via getaddr ===\n", .{});
const expanded_list = try scout.discoverPeersViaGetaddr(allocator, peer_list.items, 3);
peer_list.deinit(allocator);
peer_list = expanded_list;
}
defer peer_list.deinit(allocator);
// Select more peers than needed since some connections will fail
// Try to connect to 2x the requested count
const connect_count = args.peer_count * 2;
const selected_peers = try scout.selectRandomPeers(allocator, peer_list.items, connect_count);
defer allocator.free(selected_peers);
std.debug.print("\nSelected {d} peers to attempt connection (targeting {d} for broadcast)\n", .{ selected_peers.len, args.peer_count });
// Initialize relay
var r = try Relay.init(selected_peers, allocator);
defer r.deinit();
// Connect to peers (stops once we have enough)
std.debug.print("\n=== Connecting to Peers ===\n", .{});
const connected = r.connectAll(args.peer_count);
std.debug.print("\nSuccessfully connected to {d} peers (target: {d})\n", .{ connected, args.peer_count });
if (connected == 0) {
std.debug.print("Error: No peers connected, aborting broadcast\n", .{});
return error.NoPeersConnected;
}
// Broadcast info
std.debug.print("\n=== Broadcasting ===\n", .{});
std.debug.print("Transaction: {s}\n", .{txid_hex});
std.debug.print("Peers: {d}\n", .{connected});
std.debug.print("Timing: {s}\n", .{if (args.timing == .staggered_random) "staggered (privacy mode)" else "simultaneous"});
// Broadcast transaction (stop after peer_count successful broadcasts)
var result = try r.broadcastTx(tx_bytes, .{
.strategy = args.timing,
.max_peers = args.peer_count,
});
defer result.deinit(allocator);
// Print results
relay.printBroadcastReport(result.reports, allocator);
if (result.success_count > 0) {
std.debug.print("\nTransaction broadcast to {d} peer(s)\n", .{result.success_count});
} else {
std.debug.print("\nError: Broadcast failed to all peers\n", .{});
}
}