-
Notifications
You must be signed in to change notification settings - Fork 6
/
LedgerManager.java
452 lines (439 loc) · 17.5 KB
/
LedgerManager.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
/*
* Curecoin 2.0.0a Source Code
* Copyright (c) 2015 Curecoin Developers
* Distributed under MIT License
* Requires Apache Commons Library
* Supports Java 1.7+
*/
import java.io.*;
import java.util.*;
import java.security.*;
import java.util.concurrent.*;
import javax.xml.bind.DatatypeConverter;
public class LedgerManager
{
private File addressDatabase;
public String addressDatabaseName;
private ConcurrentHashMap<String, Long> addressBalances;
private ConcurrentHashMap<String, Integer> addressSignatureCounts;
private ArrayList<String> addresses;
private MerkleAddressUtility merkleAddressUtility = new MerkleAddressUtility();
public int lastBlockNum = -1;
/**
* Constructor for LedgerManager. All that is needed is the path to the address database file.
*
* All peers on the network should have an identical copy of the LedgerManager object at any given time.
* Due to latency and whatnot, that doesn't happen, but a fully synchronized network would have the same ledger on every node at any time.
*
* @param addressDatabase The String representation of the address database file
*/
public LedgerManager(String addressDatabase)
{
this.addressDatabaseName = addressDatabase;
this.addressDatabase = new File(addressDatabase);
this.addresses = new ArrayList<String>();
addressBalances = new ConcurrentHashMap<String, Long>(16384);
addressSignatureCounts = new ConcurrentHashMap<String, Integer>(16384);
if (this.addressDatabase.exists())
{
try
{
Scanner readAddressDatabase = new Scanner(this.addressDatabase);
this.lastBlockNum = Integer.parseInt(readAddressDatabase.nextLine());
while (readAddressDatabase.hasNextLine())
{
String input = readAddressDatabase.nextLine();
if (input.contains(":"))
{
String[] parts = input.split(":");
String address = parts[0];
if (merkleAddressUtility.isAddressFormattedCorrectly(address))
{
try
{
long addressBalance = Long.parseLong(parts[1]);
int currentSignatureCount = Integer.parseInt(parts[2]);
addressBalances.put(address, addressBalance);
addressSignatureCounts.put(address, currentSignatureCount);
addresses.add(address);
} catch (Exception e)
{
System.out.println("[CRITICAL ERROR] parsing line \"" + input + "\"!");
e.printStackTrace();
}
}
}
}
readAddressDatabase.close();
} catch (Exception e)
{
System.out.println("[CRITICAL ERROR] Unable to read addressDatabase file!");
e.printStackTrace();
System.exit(-1);
}
}
else
{
File f = new File(addressDatabase);
try
{
PrintWriter out = new PrintWriter(f);
out.println("-1");
out.close();
this.lastBlockNum = -1; //Just in case...? Shouldn't be required.
} catch (Exception e)
{
System.out.println("[CRITICAL ERROR] UNABLE TO WRITE LEDGER RECORD FILE!");
e.printStackTrace();
System.exit(-1);
}
System.out.println("Address Database \"" + addressDatabase + "\" does not exist! Creating...");
}
}
/**
* Hashes the entire ledger, to compare against blocks.
*
* @return HEX SHA256 hash of the ledger
*/
public String getLedgerHash()
{
String ledger = "";
for (int i = 0; i < addresses.size(); i++)
{
ledger += addresses.get(i) + ":" + addressBalances.get(addresses.get(i)) + ":" + addressSignatureCounts.get(addresses.get(i)) + "\n";
}
try
{
MessageDigest md = MessageDigest.getInstance("SHA-256");
return DatatypeConverter.printHexBinary(md.digest(ledger.getBytes("UTF-8")));
} catch (Exception e)
{
e.printStackTrace();
System.out.println("[CRITICAL ERROR] Unable to generate hash of ledger! Exiting...");
System.exit(-1);
}
return null;
}
/**
* Sets the last block num.
*
* @param lastBlockNum The latest block applied to this tree
*/
public void setLastBlockNum(int lastBlockNum)
{
this.lastBlockNum = lastBlockNum;
}
/**
* This method executes a given transaction String of the format InputAddress;InputAmount;OutputAddress1;OutputAmount1;OutputAddress2;OutputAmount2...;SignatureData;SignatureIndex
*
* @param transaction String-formatted transaction to execute
*
* @return boolean Whether execution of the transaction was successful
*/
public boolean executeTransaction(String transaction)
{
try
{
String[] transactionParts = transaction.split(";");
String transactionMessage = "";
for (int i = 0; i < transactionParts.length - 2; i++)
{
transactionMessage += transactionParts[i] + ";";
}
transactionMessage = transactionMessage.substring(0, transactionMessage.length() - 1);
String sourceAddress = transactionParts[0];
String signatureData = transactionParts[transactionParts.length - 2];
long signatureIndex = Long.parseLong(transactionParts[transactionParts.length - 1]);
if (!merkleAddressUtility.verifyMerkleSignature(transactionMessage, signatureData, sourceAddress, signatureIndex))
{
return false; //Signature does not sign transaction message!
}
if (getAddressSignatureCount(sourceAddress) + 1 != signatureIndex)
{
return false; //The signature is valid, however it isn't using the expected signatureIndex. Blocked to ensure a compromised Lamport key from a previous transaction can't be used.
}
if (!merkleAddressUtility.isAddressFormattedCorrectly(sourceAddress))
{
return false; //Incorrect sending address
}
long sourceAmount = Long.parseLong(transactionParts[1]);
if (getAddressBalance(sourceAddress) < sourceAmount) //sourceAddress has an insufficient balance
{
return false; //Insufficient balance
}
ArrayList<String> destinationAddresses = new ArrayList<String>();
ArrayList<Long> destinationAmounts = new ArrayList<Long>();
for (int i = 2; i < transactionParts.length - 2; i+=2) //-2 because last two parts of transaction are the signature and signature index
{
destinationAddresses.add(transactionParts[i]);
destinationAmounts.add(Long.parseLong(transactionParts[i + 1]));
}
if (destinationAddresses.size() != destinationAmounts.size())
{
return false; //This should never happen. But if it does...
}
for (int i = 0; i < destinationAddresses.size(); i++)
{
if (!merkleAddressUtility.isAddressFormattedCorrectly(destinationAddresses.get(i)))
{
return false; //A destination address is not a valid address
}
}
long outputTotal = 0L;
for (int i = 0; i < destinationAmounts.size(); i++)
{
outputTotal += destinationAmounts.get(i);
}
if (sourceAmount < outputTotal)
{
return false;
}
//Looks like everything is correct--transaction should be executed correctly
addressBalances.put(sourceAddress, getAddressBalance(sourceAddress) - sourceAmount);
for (int i = 0; i < destinationAddresses.size(); i++)
{
addressBalances.put(destinationAddresses.get(i), getAddressBalance(destinationAddresses.get(i)) + destinationAmounts.get(i));
}
adjustAddressSignatureCount(sourceAddress, 1);
return true;
} catch (Exception e)
{
return false;
}
}
/**
* This method reverse-executes a given transaction String of the format InputAddress;InputAmount;OutputAddress1;OutputAmount1;OutputAddress2;OutputAmount2...;SignatureData;SignatureIndex
* Used primarily when a blockchain fork is resolved, and transactions have to be reversed that existed in the now-forked block(s).
*
* @param transaction String-formatted transaction to execute
*
* @return boolean Whether execution of the transaction was successful
*/
public boolean reverseTransaction(String transaction)
{
try
{
String[] transactionParts = transaction.split(";");
String transactionMessage = "";
for (int i = 0; i < transactionParts.length - 2; i++)
{
transactionMessage += transactionParts[i] + ";";
}
transactionMessage = transactionMessage.substring(0, transactionMessage.length() - 1);
String sourceAddress = transactionParts[0];
String signatureData = transactionParts[transactionParts.length - 2];
long signatureIndex = Long.parseLong(transactionParts[transactionParts.length - 1]);
if (!merkleAddressUtility.verifyMerkleSignature(transactionMessage, signatureData, sourceAddress, signatureIndex))
{
return false; //Signature does not sign transaction message!
}
if (getAddressSignatureCount(sourceAddress) + 1 != signatureIndex)
{
//We're not concerned with this when reversing!
//return false; //The signature is valid, however it isn't using the expected signatureIndex. Blocked to ensure a compromised Lamport key from a previous transaction can't be used.
}
if (!merkleAddressUtility.isAddressFormattedCorrectly(sourceAddress))
{
return false; //Incorrect sending address
}
long sourceAmount = Long.parseLong(transactionParts[1]);
ArrayList<String> destinationAddresses = new ArrayList<String>();
ArrayList<Long> destinationAmounts = new ArrayList<Long>();
for (int i = 2; i < transactionParts.length - 2; i+=2) //-2 because last two parts of transaction are the signature and signature index
{
destinationAddresses.add(transactionParts[i]);
destinationAmounts.add(Long.parseLong(transactionParts[i + 1]));
}
if (destinationAddresses.size() != destinationAmounts.size())
{
return false; //This should never happen. But if it does...
}
for (int i = 0; i < destinationAddresses.size(); i++)
{
if (!merkleAddressUtility.isAddressFormattedCorrectly(destinationAddresses.get(i)))
{
return false; //A destination address is not a valid address
}
}
long outputTotal = 0L;
for (int i = 0; i < destinationAmounts.size(); i++)
{
outputTotal += destinationAmounts.get(i);
}
if (sourceAmount < outputTotal)
{
return false;
}
for (int i = 0; i < destinationAmounts.size(); i++)
{
if (getAddressBalance(destinationAddresses.get(i)) < destinationAmounts.get(i))
{
System.out.println("[CRITICAL ERROR] ADDRESS " + destinationAddresses.get(i) + " needs to return " + destinationAmounts.get(i) + " but only has " + getAddressBalance(destinationAddresses.get(i))); //BIG PROBLEM THIS SHOULD NEVER HAPPEN
return false; //One of the addresses has an insufficient balance to reverse!
}
}
//Looks like everything is correct--transaction should be reversed correctly
addressBalances.put(sourceAddress, getAddressBalance(sourceAddress) + sourceAmount);
for (int i = 0; i < destinationAddresses.size(); i++)
{
addressBalances.put(destinationAddresses.get(i), getAddressBalance(destinationAddresses.get(i)) - destinationAmounts.get(i));
}
adjustAddressSignatureCount(sourceAddress, -1);
return true;
} catch (Exception e)
{
return false;
}
}
/**
* Writes ledger to file.
*
* @return boolean Whether writing the ledger to the disk was successful.
*/
public boolean writeToFile()
{
try
{
PrintWriter out = new PrintWriter(addressDatabase);
for (int i = 0; i < addresses.size(); i++)
{
out.println(addresses.get(i) + ":" + addressBalances.get(addresses.get(i)) + ":" + addressSignatureCounts.get(addresses.get(i)));
}
out.close();
} catch (Exception e)
{
System.out.println("[CRITICAL ERROR] UNABLE TO WRITE DB FILE!");
e.printStackTrace();
return false;
}
return true;
}
/**
* Returns the last-used signature index of an address.
*
* @param address Address to retrieve the latest index for
*
* @return int Last signature index used by address
*/
public int getAddressSignatureCount(String address)
{
if (addressSignatureCounts.containsKey(address))
{
return addressSignatureCounts.get(address);
}
else
{
return -1;
}
}
/**
* Adjusts an address's signature count.
*
* @param address Address to adjust
* @param adjustment Amount to adjust address's signature count by. This can be negative.
*
* @return boolean Whether the adjustment was successful
*/
public boolean adjustAddressSignatureCount(String address, int adjustment)
{
int oldCount = getAddressSignatureCount(address);
if (oldCount + adjustment < 0) //Adjustment is negative with an absolute value larger than oldBalance
{
return false;
}
return updateAddressSignatureCount(address, oldCount + adjustment);
}
/**
* Updates an address's signature count.
*
* @param address Address to update
* @param newCount New signature index to use
*
* @return boolean Whether the adjustment was successful
*/
public boolean updateAddressSignatureCount(String address, int newCount)
{
try
{
if (addressSignatureCounts.containsKey(address))
{
addressSignatureCounts.put(address, newCount);
}
else
{
addressBalances.put(address, 0L);
addressSignatureCounts.put(address, newCount);
addresses.add(address);
}
} catch (Exception e)
{
e.printStackTrace();
return false;
}
return true;
}
/**
* Returns the address balance for a given address.
*
* @param address Address to check balance of
*
* @return long Balance of address
*/
public long getAddressBalance(String address)
{
if (addressBalances.containsKey(address))
{
return addressBalances.get(address);
}
else
{
return 0L;
}
}
/**
* Adjusts the balance of an address by a given adjustment, which can be positive or negative.
*
* @param address Address to adjust the balance of
* @param adjustment Amount to adjust account balance by
*
* @return boolean Whether the adjustment was successful
*/
public boolean adjustAddressBalance(String address, long adjustment)
{
long oldBalance = getAddressBalance(address);
if (oldBalance + adjustment < 0) //Adjustment is negative with an absolute value larger than oldBalance
{
return false;
}
return updateAddressBalance(address, oldBalance + adjustment);
}
/**
* Updates the balance of an address to a new amount
*
* @param address Address to set the balance of
* @param newAmount New amount to set as the balance of address
*
* @return boolean Whether setting the new balance was successful
*/
public boolean updateAddressBalance(String address, long newAmount)
{
try
{
if (addressBalances.containsKey(address))
{
addressBalances.put(address, newAmount);
}
else
{
addressBalances.put(address, newAmount);
addressSignatureCounts.put(address, 0);
addresses.add(address);
}
} catch (Exception e)
{
e.printStackTrace();
return false;
}
return true;
}
}