/
SnowBlossomClient.java
539 lines (454 loc) · 15.2 KB
/
SnowBlossomClient.java
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
package snowblossom.client;
import com.google.protobuf.ByteString;
import duckutil.Config;
import duckutil.ConfigFile;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import snowblossom.lib.*;
import org.junit.Assert;
import snowblossom.proto.*;
import snowblossom.proto.UserServiceGrpc.UserServiceBlockingStub;
import java.util.concurrent.atomic.AtomicLong;
import snowblossom.proto.UserServiceGrpc.UserServiceStub;
import snowblossom.trie.proto.TrieNode;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.Callable;
import duckutil.TaskMaster;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import duckutil.AtomicFileOutputStream;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.text.DecimalFormat;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public class SnowBlossomClient
{
private static final Logger logger = Logger.getLogger("snowblossom.client");
public static void main(String args[]) throws Exception
{
Globals.addCryptoProvider();
if (args.length < 1)
{
logger.log(Level.SEVERE, "Incorrect syntax. Syntax: SnowBlossomClient <config_file> [commands]");
System.exit(-1);
}
ConfigFile config = new ConfigFile(args[0]);
LogSetup.setup(config);
SnowBlossomClient client = new SnowBlossomClient(config);
if (args.length == 1)
{
client.showBalances(false);
client.printBasicStats();
System.out.println("Here is an unused address:");
AddressSpecHash hash = client.getPurse().getUnusedAddress(false, false);
String addr = AddressUtil.getAddressString(client.getParams().getAddressPrefix(), hash);
System.out.println(addr);
}
if (args.length > 1)
{
String command = args[1];
if (command.equals("send"))
{
if (args.length < 4)
{
logger.log(Level.SEVERE, "Incorrect syntax. Syntax: SnowBlossomClient <config_file> send <amount> <dest_address>");
System.exit(-1);
}
double val_snow = Double.parseDouble(args[2]);
long value = (long) (val_snow * Globals.SNOW_VALUE);
String to = args[3];
DecimalFormat df = new DecimalFormat("0.000000");
logger.info(String.format("Building send of %s to %s", df.format(val_snow), to));
client.send(value, to);
}
else if (command.equals("balance"))
{
client.showBalances(true);
}
else if (command.equals("getfresh"))
{
boolean mark_used = false;
boolean generate_now = false;
if (args.length > 2)
{
mark_used = Boolean.parseBoolean(args[2]);
}
if (args.length > 3)
{
generate_now = Boolean.parseBoolean(args[3]);
}
AddressSpecHash hash = client.getPurse().getUnusedAddress(mark_used, generate_now);
String addr = AddressUtil.getAddressString(client.getParams().getAddressPrefix(), hash);
System.out.println(addr);
}
else if (command.equals("monitor"))
{
while(true)
{
try
{
if (client == null)
{
client = new SnowBlossomClient(config);
}
client.showBalances(false);
}
catch(Throwable t)
{
t.printStackTrace();
client = null;
}
Thread.sleep(60000);
}
}
else if (command.equals("rpcserver"))
{
new JsonRpcServer(config);
while(true)
{
Thread.sleep(1000);
}
}
else if (command.equals("loadtest"))
{
client.runLoadTest();
}
else
{
logger.log(Level.SEVERE, String.format("Unknown command %s.", command));
System.out.println("Commands:");
System.out.println("(no command) - show total balance, show one fresh address");
System.out.println(" balance - show balance of all addresses");
System.out.println(" monitor - show balance and repeat");
System.out.println(" getfresh [mark_used] [generate_now] - get a fresh address");
System.out.println(" if mark_used is true, mark the address as used");
System.out.println(" if generate_now is true, generate a new address rather than using the key pool");
System.out.println(" send <amount> <destination> - send snow to address");
System.exit(-1);
}
}
}
private final UserServiceStub asyncStub;
private final UserServiceBlockingStub blockingStub;
private final NetworkParams params;
private File wallet_path;
private Purse purse;
private Config config;
private ThreadPoolExecutor exec;
public SnowBlossomClient(Config config) throws Exception
{
this.config = config;
logger.info(String.format("Starting SnowBlossomClient version %s", Globals.VERSION));
config.require("node_host");
String host = config.get("node_host");
params = NetworkParams.loadFromConfig(config);
int port = config.getIntWithDefault("node_port", params.getDefaultPort());
exec = TaskMaster.getBasicExecutor(64,"client_lookup");
ManagedChannel channel = ManagedChannelBuilder.forAddress(host, port).usePlaintext(true).build();
asyncStub = UserServiceGrpc.newStub(channel);
blockingStub = UserServiceGrpc.newBlockingStub(channel);
if (config.isSet("wallet_path"))
{
wallet_path = new File(config.get("wallet_path"));
loadWallet();
}
}
public Purse getPurse(){return purse;}
public NetworkParams getParams(){return params;}
public void send(long value, String to)
throws Exception
{
AddressSpecHash to_hash = AddressUtil.getHashForAddress(params.getAddressPrefix(), to);
Transaction tx = TransactionUtil.makeTransaction(purse.getDB(), getAllSpendable(),
to_hash, value, 0L,
purse.getUnusedAddress(true,false));
logger.info("Transaction: " + new ChainHash(tx.getTxHash()) + " - " + tx.toByteString().size());
TransactionUtil.prettyDisplayTx(tx, System.out, params);
//logger.info(tx.toString());
System.out.println(blockingStub.submitTransaction(tx));
}
public void loadWallet()
throws Exception
{
purse = new Purse(wallet_path, config, params);
}
public void printBasicStats()
throws ValidationException
{
int total_keys = purse.getDB().getKeysCount();
int total_addresses = purse.getDB().getAddressesCount();
int used_addresses = purse.getDB().getUsedAddressesCount();
int unused_addresses = total_addresses - used_addresses;
System.out.println(String.format("Wallet Keys: %d, Addresses: %d, Fresh pool: %d", total_keys, total_addresses, unused_addresses));
TreeMap<String, Integer> address_type_map = new TreeMap<>();
for(AddressSpec spec : purse.getDB().getAddressesList())
{
String type = AddressUtil.getAddressSpecTypeSummary(spec);
if (address_type_map.containsKey(type))
{
address_type_map.put(type, 1 + address_type_map.get(type));
}
else
{
address_type_map.put(type, 1);
}
}
for(Map.Entry<String, Integer> me : address_type_map.entrySet())
{
System.out.println(" " + me.getKey() + ": " + me.getValue());
}
}
public void showBalances(boolean print_each_address)
{
final AtomicLong total_confirmed = new AtomicLong(0);
final AtomicLong total_unconfirmed = new AtomicLong(0L);
final AtomicLong total_spendable = new AtomicLong(0L);
final DecimalFormat df = new DecimalFormat("0.000000");
Throwable logException = null;
TaskMaster tm = new TaskMaster(exec);
for(AddressSpec claim : purse.getDB().getAddressesList())
{
tm.addTask(new Callable(){
public String call()
throws Exception
{
AddressSpecHash hash = AddressUtil.getHashForSpec(claim);
String address = AddressUtil.getAddressString(params.getAddressPrefix(), hash);
StringBuilder sb = new StringBuilder();
sb.append("Address: " + address + " - ");
long value_confirmed = 0;
long value_unconfirmed = 0;
boolean used=false;
List<TransactionBridge> bridges = getSpendable(hash);
if (bridges.size() > 0)
{
used=true;
purse.markUsed(hash);
}
for(TransactionBridge b : bridges)
{
if (b.unconfirmed)
{
if (!b.spent)
{
value_unconfirmed += b.value;
}
}
else //confirmed
{
value_confirmed += b.value;
if (b.spent)
{
value_unconfirmed -= b.value;
}
}
if (!b.spent)
{
total_spendable.addAndGet(b.value);
}
}
if (purse.getDB().getUsedAddressesMap().containsKey(address))
{
used=true;
}
double val_conf_d = (double) value_confirmed / (double) Globals.SNOW_VALUE;
double val_unconf_d = (double) value_unconfirmed / (double) Globals.SNOW_VALUE;
sb.append(String.format(" %s (%s pending) in %d outputs",
df.format(val_conf_d), df.format(val_unconf_d), bridges.size()));
total_confirmed.addAndGet(value_confirmed);
total_unconfirmed.addAndGet(value_unconfirmed);
if (used)
{
return sb.toString();
}
return "";
}
});
}
List<String> addr_balances = tm.getResults();
if (print_each_address)
{
Set<String> lines = new TreeSet<String>();
lines.addAll(addr_balances);
for(String s : lines)
{
if (s.length() > 0)
{
System.out.println(s);
}
}
}
double total_conf_d = (double) total_confirmed.get() / (double) Globals.SNOW_VALUE;
double total_unconf_d = (double) total_unconfirmed.get() / (double) Globals.SNOW_VALUE;
double total_spend_d = (double) total_spendable.get() / Globals.SNOW_VALUE_D;
System.out.println(String.format("Total: %s (%s pending) (%s spendable)", df.format(total_conf_d), df.format(total_unconf_d),
df.format(total_spend_d)));
}
public List<TransactionBridge> getAllSpendable()
throws Exception
{
LinkedList<TransactionBridge> all = new LinkedList<>();
for(AddressSpec claim : purse.getDB().getAddressesList())
{
AddressSpecHash hash = AddressUtil.getHashForSpec(claim);
List<TransactionBridge> br_lst = getSpendable(hash);
if (br_lst.size() > 0)
{
purse.markUsed(hash);
}
all.addAll(br_lst);
}
return all;
}
public List<TransactionBridge> getSpendable(AddressSpecHash addr)
{
GetUTXONodeReply reply = blockingStub.getUTXONode( GetUTXONodeRequest.newBuilder()
.setPrefix(addr.getBytes())
.setIncludeProof(true)
.build());
HashMap<String, TransactionBridge> bridge_map=new HashMap<>();
for(TrieNode node : reply.getAnswerList())
{
if (node.getIsLeaf())
{
TransactionBridge b = new TransactionBridge(node);
bridge_map.put(b.getKeyString(), b);
}
}
for(ByteString tx_hash : blockingStub.getMempoolTransactionList(
RequestAddress.newBuilder().setAddressSpecHash(addr.getBytes()).build()).getTxHashesList())
{
Transaction tx = blockingStub.getTransaction(RequestTransaction.newBuilder().setTxHash(tx_hash).build());
TransactionInner inner = TransactionUtil.getInner(tx);
for(TransactionInput in : inner.getInputsList())
{
if (addr.equals(in.getSpecHash()))
{
TransactionBridge b_in = new TransactionBridge(in);
String key = b_in.getKeyString();
if (bridge_map.containsKey(key))
{
bridge_map.get(key).spent=true;
}
else
{
bridge_map.put(key, b_in);
}
}
}
for(int o=0; o<inner.getOutputsCount(); o++)
{
TransactionOutput out = inner.getOutputs(o);
if (addr.equals(out.getRecipientSpecHash()))
{
TransactionBridge b_out = new TransactionBridge(out, o, new ChainHash(tx_hash));
String key = b_out.getKeyString();
b_out.unconfirmed=true;
if (bridge_map.containsKey(key))
{
if (bridge_map.get(key).spent)
{
b_out.spent=true;
}
}
bridge_map.put(key, b_out);
}
}
}
LinkedList<TransactionBridge> lst = new LinkedList<>();
lst.addAll(bridge_map.values());
return lst;
}
public boolean submitTransaction(Transaction tx)
{
SubmitReply reply = blockingStub.submitTransaction(tx);
return reply.getSuccess();
}
public void runLoadTest()
throws Exception
{
LinkedList<TransactionBridge> spendable = new LinkedList<>();
for(TransactionBridge br : getAllSpendable())
{
if (!br.spent) spendable.add(br);
}
Collections.shuffle(spendable);
long min_send = 50000L;
long max_send = 500000L;
long send_delta = max_send - min_send;
SplittableRandom rnd = new SplittableRandom();
int output_count = 2;
while(true)
{
//Collections.shuffle(spendable);
LinkedList<TransactionOutput> out_list = new LinkedList<>();
long fee = rnd.nextLong(500);
fee = 0;
long needed_value = fee;
for(int i=0; i< output_count; i++)
{
long value = min_send + rnd.nextLong(send_delta);
out_list.add( TransactionOutput.newBuilder()
.setRecipientSpecHash(TransactionUtil.getRandomChangeAddress(purse.getDB()).getBytes() )
.setValue(value)
.build());
needed_value+=value;
}
LinkedList<TransactionBridge> input_list = new LinkedList<>();
while(needed_value > 0)
{
TransactionBridge b = spendable.pop();
needed_value -= b.value;
input_list.add(b);
}
Transaction tx = TransactionUtil.makeTransaction(purse.getDB(), input_list, out_list, fee,
purse.getUnusedAddress(true,false));
if (tx == null)
{
logger.warning("Unable to make transaction");
return;
}
TransactionInner inner = TransactionUtil.getInner(tx);
ChainHash tx_hash = new ChainHash(tx.getTxHash());
for(int i=0; i<inner.getOutputsCount(); i++)
{
TransactionBridge b = new TransactionBridge(inner.getOutputs(i), i, tx_hash);
spendable.add(b);
}
logger.info("Transaction: " + new ChainHash(tx.getTxHash()) + " - " + tx.toByteString().size());
//logger.info(tx.toString());
boolean sent=false;
while(!sent)
{
SubmitReply reply = blockingStub.submitTransaction(tx);
if (reply.getSuccess())
{
sent=true;
}
else
{
logger.info("Error: " + reply.getErrorMessage());
if (reply.getErrorMessage().contains("full"))
{
Thread.sleep(60000);
}
else
{
return;
}
}
}
boolean success = submitTransaction(tx);
System.out.println("Submit: " + success);
if (!success)
{
return;
}
}
}
}