-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
491 lines (406 loc) · 18.3 KB
/
Program.cs
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
/* This script gets all addresses ever used on the Veriblock blockchain
* then counts all balances of those addresses and compares is to the
* amount mentioned by the networkstats. */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace CheckVeriblockBalance
{
class Program
{
static void Main(string[] args)
{
DateTime start_time = DateTime.Now;
decimal totalBalanceCalculatedFromAddresses = 0;
int intCurrentVeriblockBlock = GetLatestVeriblock();
List<string> listAddresses = new List<string> ();
Console.WriteLine("Make sure your Nodecore is connected to the network and synced!");
Console.WriteLine("");
// Start collecting data from Veriblock blockchain
Console.WriteLine("Collecting addresses from Veriblock Blockchain:");
for (int i = 0; i < intCurrentVeriblockBlock; i++)
{
GetBlock(i, listAddresses);
listAddresses = listAddresses.Distinct().ToList();
DateTime current_time = DateTime.Now;
TimeSpan time_diff = current_time - start_time;
Console.Write("\r{0} of {1} Veriblock blocks processed. {2} unique adresses found. Script running for {3}", i + 1, intCurrentVeriblockBlock, listAddresses.Count, time_diff.ToString(@"hh\:mm\:ss"));
}
for (int i = 0; i < listAddresses.Count; i++)
{
decimal addressBalance = GetBalance(listAddresses[i]);
totalBalanceCalculatedFromAddresses = totalBalanceCalculatedFromAddresses + addressBalance;
Console.Write("\r{0} of {1} Veriblock adresses processed. {2} total balance thus far", i + 1, listAddresses.Count, totalBalanceCalculatedFromAddresses);
}
decimal totalBalanceFromStats = GetVeriblockStats();
Console.WriteLine("");
Console.WriteLine("Total balance from network stats: " + (totalBalanceFromStats / 100000000) + " VBK at block " + intCurrentVeriblockBlock);
Console.WriteLine();
Console.WriteLine("Results:");
Console.WriteLine("Total balance calculated from addresses: " + (totalBalanceCalculatedFromAddresses));
Console.WriteLine("Total balance from network stats: " + totalBalanceFromStats / 100000000);
Console.WriteLine("Difference: " + ((totalBalanceCalculatedFromAddresses) - (totalBalanceFromStats / 100000000)));
Console.WriteLine("");
Console.WriteLine("Please note, a bit of difference is expected because the balance from network stats is updated once in ~15 minutes.");
Console.ReadLine();
}
static public string TalkToNode(string json)
{
using (var webClient = new WebClient())
try
{
string response = webClient.UploadString("http://127.0.0.1:10600/api", "POST", json);
return response;
}
catch (Exception exception)
{
Console.WriteLine("Error in TalkToNode: {1} \n {0} \n {1} ", exception);
return exception.ToString();
}
}
static public int GetLatestVeriblock()
{
string jsonBlock = "{\"jsonrpc\": \"2.0\",\"method\": \"getinfo\",\"params\": {},\"id\": 1}";
int latestVeriblock = 0;
try
{
string response = TalkToNode(jsonBlock);
JToken token = JObject.Parse(response);
latestVeriblock = (int)token.SelectToken("result.lastBlock.number");
}
catch (Exception exception)
{
Console.WriteLine($"Exception: {exception}");
}
return latestVeriblock;
}
static public decimal GetVeriblockStats()
{
decimal totalSupply = 0;
try
{
var client = new WebClient();
var response = client.DownloadString("https://explore.veriblock.org/api/stats/summary");
JToken token = JObject.Parse(response);
decimal totalPowAfterBlock1 = decimal.Parse((string)token.SelectToken("supply.totalPowAfterBlock1"));
decimal totalPopAfterBlock1 = decimal.Parse((string)token.SelectToken("supply.totalPopAfterBlock1"));
decimal totalReflectedInBlock1 = decimal.Parse((string)token.SelectToken("supply.totalReflectedInBlock1"));
totalSupply = totalPowAfterBlock1 + totalPopAfterBlock1 + totalReflectedInBlock1;
}
catch (Exception exception)
{
Console.WriteLine($"Exception: {exception}");
}
return totalSupply;
}
static public decimal GetBalance(string address)
{
decimal balance = 0;
string url1 = "{\"jsonrpc\": \"2.0\",\"method\": \"getbalance\",\"params\": {\"addresses\": [";
string url3 = "]},\"id\": 1}";
string jsonBlock = String.Concat(url1, address, url3);
string response = TalkToNode(jsonBlock);
Getbalance result = JsonConvert.DeserializeObject<Getbalance>(response);
ResultGetbalance ResultGetbalance = result.ResultGetbalance;
if (result.ResultGetbalance != null)
{
Confirmed[] confirmedList = ResultGetbalance.Confirmed;
Confirmed Confirmed = confirmedList[0];
string totalConfirmed = Confirmed.TotalAmount;
if (totalConfirmed != null)
{
balance = decimal.Parse(totalConfirmed) / 100000000;
}
}
return balance;
}
static public void GetBlock(int block_number, List<string> listAddresses)
{
string url1 = "{\"jsonrpc\": \"2.0\",\"method\": \"getblocks\",\"params\": {\"searchLength\": 1,\"filters\": [{\"index\": ";
string url3 = "}]},\"id\": 1}";
string jsonBlock = String.Concat(url1, block_number, url3);
string response = TalkToNode(jsonBlock);
VbkBlock items = JsonConvert.DeserializeObject<VbkBlock>(response);
Result result = items.Result;
if (result.Blocks != null)
{
Block[] blockArr = result.Blocks;
Block block = blockArr[0];
RegularTransaction[] regularTransactions = block.RegularTransactions;
if (block.RegularTransactions != null)
{
for (int i = 0; i < regularTransactions.Length; i++)
{
RegularTransaction RegularTransaction = regularTransactions[i];
Signed signed = RegularTransaction.Signed;
if (RegularTransaction.Signed != null)
{
Transaction transaction = signed.Transaction;
// save the source address from the regular transaction
listAddresses.Add(transaction.SourceAddress);
Output[] Txoutputs = transaction.Outputs;
if (transaction.Outputs != null)
{
for (int j = 0; j < Txoutputs.Length; j++)
{
Output output = Txoutputs[j];
// save the output addresses from the regular transaction
listAddresses.Add(output.Address);
}
}
}
SignedMultisig signedMultisig = RegularTransaction.SignedMultisig;
if (RegularTransaction.SignedMultisig != null)
{
Transaction transaction1 = signedMultisig.Transaction;
// save the source address from the multisig transaction
listAddresses.Add(transaction1.SourceAddress);
Output[] Txoutputs1 = transaction1.Outputs;
if (transaction1.Outputs != null)
{
for (int j = 0; j < Txoutputs1.Length; j++)
{
Output output = Txoutputs1[j];
// save the output addresses from the multisig transaction
listAddresses.Add(output.Address);
}
}
}
}
}
BlockContentMetapackage BlockContentMetapackage = block.BlockContentMetapackage;
CoinbaseTransaction CoinbaseTransaction = BlockContentMetapackage.CoinbaseTransaction;
Output[] PowOutputs = CoinbaseTransaction.PowOutputs;
if (CoinbaseTransaction.PowOutputs != null)
{
for (int i = 0; i < PowOutputs.Length; i++)
{
Output output = PowOutputs[i];
// save the addresses from the PoW reward
listAddresses.Add(output.Address);
}
}
Output[] PopOutputs = CoinbaseTransaction.PopOutputs;
if (CoinbaseTransaction.PopOutputs != null)
{
for (int i = 0; i < PopOutputs.Length; i++)
{
Output output = PopOutputs[i];
// save the addresses from the PoP reward
listAddresses.Add(output.Address);
}
}
}
}
public partial class VbkBlock
{
[JsonProperty("jsonrpc")]
public string Jsonrpc { get; set; }
[JsonProperty("result")]
public Result Result { get; set; }
[JsonProperty("id")]
public long Id { get; set; }
}
public partial class Result
{
[JsonProperty("blocks")]
public Block[] Blocks { get; set; }
[JsonProperty("success")]
public bool Success { get; set; }
}
public partial class Block
{
[JsonProperty("regularTransactions")]
public RegularTransaction[] RegularTransactions { get; set; }
[JsonProperty("blockContentMetapackage")]
public BlockContentMetapackage BlockContentMetapackage { get; set; }
[JsonProperty("number")]
public long Number { get; set; }
[JsonProperty("timestamp")]
public long Timestamp { get; set; }
[JsonProperty("hash")]
public string Hash { get; set; }
[JsonProperty("previousHash")]
public string PreviousHash { get; set; }
[JsonProperty("secondPreviousHash")]
public string SecondPreviousHash { get; set; }
[JsonProperty("thirdPreviousHash")]
public string ThirdPreviousHash { get; set; }
[JsonProperty("encodedDifficulty")]
public long EncodedDifficulty { get; set; }
[JsonProperty("winningNonce")]
public long WinningNonce { get; set; }
[JsonProperty("totalFees")]
public long TotalFees { get; set; }
[JsonProperty("powCoinbaseReward")]
public string PowCoinbaseReward { get; set; }
[JsonProperty("popCoinbaseReward")]
public string PopCoinbaseReward { get; set; }
[JsonProperty("size")]
public long Size { get; set; }
[JsonProperty("version")]
public long Version { get; set; }
[JsonProperty("merkleRoot")]
public string MerkleRoot { get; set; }
}
public partial class BlockContentMetapackage
{
[JsonProperty("coinbaseTransaction")]
public CoinbaseTransaction CoinbaseTransaction { get; set; }
[JsonProperty("popDatastore")]
public PopDatastore PopDatastore { get; set; }
[JsonProperty("blockFeeTable")]
public BlockFeeTable BlockFeeTable { get; set; }
[JsonProperty("ledgerHash")]
public string LedgerHash { get; set; }
[JsonProperty("extraNonce")]
public string ExtraNonce { get; set; }
[JsonProperty("hash")]
public string Hash { get; set; }
}
public partial class BlockFeeTable
{
[JsonProperty("popFeeShare")]
public long PopFeeShare { get; set; }
}
public partial class CoinbaseTransaction
{
[JsonProperty("powOutputs")]
public Output[] PowOutputs { get; set; }
[JsonProperty("popOutputs")]
public Output[] PopOutputs { get; set; }
[JsonProperty("powCoinbaseAmount")]
public string PowCoinbaseAmount { get; set; }
[JsonProperty("popCoinbaseAmount")]
public string PopCoinbaseAmount { get; set; }
[JsonProperty("powFeeShare")]
public long PowFeeShare { get; set; }
[JsonProperty("popFeeShare")]
public long PopFeeShare { get; set; }
[JsonProperty("blockHeight")]
public long BlockHeight { get; set; }
[JsonProperty("txId")]
public string TxId { get; set; }
}
public partial class Output
{
[JsonProperty("address")]
public string Address { get; set; }
[JsonProperty("amount")]
public string Amount { get; set; }
}
public partial class PopDatastore
{
[JsonProperty("datastoreHash")]
public string DatastoreHash { get; set; }
[JsonProperty("endorsedVeriblockHeadersHash")]
public string EndorsedVeriblockHeadersHash { get; set; }
[JsonProperty("endorsedAltchainHeadersHash")]
public string EndorsedAltchainHeadersHash { get; set; }
}
public partial class RegularTransaction
{
[JsonProperty("signed")]
public Signed Signed { get; set; }
}
public partial class Signed
{
[JsonProperty("transaction")]
public Transaction Transaction { get; set; }
[JsonProperty("signature")]
public string Signature { get; set; }
[JsonProperty("publicKey")]
public string PublicKey { get; set; }
[JsonProperty("signatureIndex")]
public long SignatureIndex { get; set; }
}
public partial class Transaction
{
[JsonProperty("outputs")]
public Output[] Outputs { get; set; }
[JsonProperty("type")]
public string Type { get; set; }
[JsonProperty("sourceAddress")]
public string SourceAddress { get; set; }
[JsonProperty("sourceAmount")]
public string SourceAmount { get; set; }
[JsonProperty("transactionFee")]
public long TransactionFee { get; set; }
[JsonProperty("timestamp")]
public long Timestamp { get; set; }
[JsonProperty("size")]
public long Size { get; set; }
[JsonProperty("txId")]
public string TxId { get; set; }
}
public partial class Getbalance
{
[JsonProperty("jsonrpc")]
public string Jsonrpc { get; set; }
[JsonProperty("result")]
public ResultGetbalance ResultGetbalance { get; set; }
[JsonProperty("id")]
public long Id { get; set; }
}
public partial class ResultGetbalance
{
[JsonProperty("confirmed")]
public Confirmed[] Confirmed { get; set; }
[JsonProperty("unconfirmed")]
public List<Unconfirmed> Unconfirmed { get; set; }
[JsonProperty("success")]
public bool Success { get; set; }
[JsonProperty("totalConfirmed")]
public string TotalConfirmed { get; set; }
}
public partial class Confirmed
{
[JsonProperty("address")]
public string Address { get; set; }
[JsonProperty("unlockedAmount")]
public long UnlockedAmount { get; set; }
[JsonProperty("lockedAmount")]
public string LockedAmount { get; set; }
[JsonProperty("totalAmount")]
public string TotalAmount { get; set; }
}
public partial class Unconfirmed
{
[JsonProperty("address")]
public string Address { get; set; }
}
public partial class RegularTransaction
{
[JsonProperty("signedMultisig")]
public SignedMultisig SignedMultisig { get; set; }
}
public partial class SignedMultisig
{
[JsonProperty("signatureBundle")]
public SignatureBundle SignatureBundle { get; set; }
[JsonProperty("transaction")]
public Transaction Transaction { get; set; }
[JsonProperty("signatureIndex")]
public long SignatureIndex { get; set; }
}
public partial class SignatureBundle
{
[JsonProperty("slots")]
public List<Slot> Slots { get; set; }
}
public partial class Slot
{
[JsonProperty("populated", NullValueHandling = NullValueHandling.Ignore)]
public bool? Populated { get; set; }
[JsonProperty("signature", NullValueHandling = NullValueHandling.Ignore)]
public string Signature { get; set; }
[JsonProperty("publicKey", NullValueHandling = NullValueHandling.Ignore)]
public string PublicKey { get; set; }
[JsonProperty("ownerAddress", NullValueHandling = NullValueHandling.Ignore)]
public string OwnerAddress { get; set; }
}
}
}