-
Notifications
You must be signed in to change notification settings - Fork 5
/
Nxt$Account.java
336 lines (316 loc) · 12.1 KB
/
Nxt$Account.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
import java.math.BigInteger;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicReference;
import org.json.simple.JSONObject;
class Nxt$Account
{
final long id;
private long balance;
final int height;
final AtomicReference<byte[]> publicKey = new AtomicReference();
private final Map<Long, Integer> assetBalances = new HashMap();
private long unconfirmedBalance;
private final Map<Long, Integer> unconfirmedAssetBalances = new HashMap();
private Nxt$Account(long paramLong)
{
this.id = paramLong;
this.height = Nxt.Block.getLastBlock().height;
}
static Account addAccount(long paramLong)
{
Account localAccount = new Account(paramLong);
Nxt.accounts.put(Long.valueOf(paramLong), localAccount);
return localAccount;
}
boolean setOrVerify(byte[] paramArrayOfByte)
{
return (this.publicKey.compareAndSet(null, paramArrayOfByte)) || (Arrays.equals(paramArrayOfByte, (byte[])this.publicKey.get()));
}
void generateBlock(String paramString)
throws Exception
{
TreeSet localTreeSet = new TreeSet();
Object localObject1 = Nxt.unconfirmedTransactions.values().iterator();
while (((Iterator)localObject1).hasNext())
{
localObject2 = (Nxt.Transaction)((Iterator)localObject1).next();
if ((((Nxt.Transaction)localObject2).referencedTransaction == 0L) || (Nxt.transactions.get(Long.valueOf(((Nxt.Transaction)localObject2).referencedTransaction)) != null)) {
localTreeSet.add(localObject2);
}
}
localObject1 = new HashMap();
Object localObject2 = new HashSet();
HashMap localHashMap = new HashMap();
int i = 0;
Object localObject3;
while (i <= 32640)
{
int j = ((Map)localObject1).size();
localObject3 = localTreeSet.iterator();
while (((Iterator)localObject3).hasNext())
{
localObject4 = (Nxt.Transaction)((Iterator)localObject3).next();
int m = ((Nxt.Transaction)localObject4).getSize();
if ((((Map)localObject1).get(Long.valueOf(((Nxt.Transaction)localObject4).getId())) == null) && (i + m <= 32640))
{
long l1 = ((Nxt.Transaction)localObject4).getSenderAccountId();
Long localLong = (Long)localHashMap.get(Long.valueOf(l1));
if (localLong == null) {
localLong = Long.valueOf(0L);
}
long l2 = (((Nxt.Transaction)localObject4).amount + ((Nxt.Transaction)localObject4).fee) * 100L;
if ((localLong.longValue() + l2 <= ((Account)Nxt.accounts.get(Long.valueOf(l1))).getBalance()) && (((Nxt.Transaction)localObject4).validateAttachment())) {
switch (((Nxt.Transaction)localObject4).type)
{
case 1:
switch (((Nxt.Transaction)localObject4).subtype)
{
case 1:
if (!((Set)localObject2).add(((Nxt.Transaction.MessagingAliasAssignmentAttachment)((Nxt.Transaction)localObject4).attachment).alias.toLowerCase())) {}
break;
}
default:
localHashMap.put(Long.valueOf(l1), Long.valueOf(localLong.longValue() + l2));
((Map)localObject1).put(Long.valueOf(((Nxt.Transaction)localObject4).getId()), localObject4);
i += m;
}
}
}
}
if (((Map)localObject1).size() == j) {
break;
}
}
Nxt.Block localBlock;
if (Nxt.Block.getLastBlock().height < 30000)
{
localBlock = new Nxt.Block(1, Nxt.getEpochTime(System.currentTimeMillis()), Nxt.lastBlock, ((Map)localObject1).size(), 0, 0, 0, null, Nxt.Crypto.getPublicKey(paramString), null, new byte[64]);
}
else
{
localObject3 = MessageDigest.getInstance("SHA-256").digest(Nxt.Block.getLastBlock().getBytes());
localBlock = new Nxt.Block(2, Nxt.getEpochTime(System.currentTimeMillis()), Nxt.lastBlock, ((Map)localObject1).size(), 0, 0, 0, null, Nxt.Crypto.getPublicKey(paramString), null, new byte[64], (byte[])localObject3);
}
int k = 0;
Object localObject4 = ((Map)localObject1).entrySet().iterator();
while (((Iterator)localObject4).hasNext())
{
localObject5 = (Map.Entry)((Iterator)localObject4).next();
localObject6 = (Nxt.Transaction)((Map.Entry)localObject5).getValue();
localBlock.totalAmount += ((Nxt.Transaction)localObject6).amount;
localBlock.totalFee += ((Nxt.Transaction)localObject6).fee;
localBlock.payloadLength += ((Nxt.Transaction)localObject6).getSize();
localBlock.transactions[(k++)] = ((Long)((Map.Entry)localObject5).getKey()).longValue();
}
Arrays.sort(localBlock.transactions);
localObject4 = MessageDigest.getInstance("SHA-256");
for (k = 0; k < localBlock.numberOfTransactions; k++) {
((MessageDigest)localObject4).update(((Nxt.Transaction)((Map)localObject1).get(Long.valueOf(localBlock.transactions[k]))).getBytes());
}
localBlock.payloadHash = ((MessageDigest)localObject4).digest();
if (Nxt.Block.getLastBlock().height < 30000)
{
localBlock.generationSignature = Nxt.Crypto.sign(Nxt.Block.getLastBlock().generationSignature, paramString);
}
else
{
((MessageDigest)localObject4).update(Nxt.Block.getLastBlock().generationSignature);
localBlock.generationSignature = ((MessageDigest)localObject4).digest(Nxt.Crypto.getPublicKey(paramString));
}
Object localObject5 = localBlock.getBytes();
Object localObject6 = new byte[localObject5.length - 64];
System.arraycopy(localObject5, 0, localObject6, 0, localObject6.length);
localBlock.blockSignature = Nxt.Crypto.sign((byte[])localObject6, paramString);
if ((localBlock.verifyBlockSignature()) && (localBlock.verifyGenerationSignature()))
{
JSONObject localJSONObject = localBlock.getJSONObject((Map)localObject1);
localJSONObject.put("requestType", "processBlock");
Nxt.Peer.sendToAllPeers(localJSONObject);
}
else
{
Nxt.logMessage("Generated an incorrect block. Waiting for the next one...");
}
}
int getEffectiveBalance()
{
if (this.height == 0) {
return (int)(getBalance() / 100L);
}
if (Nxt.Block.getLastBlock().height - this.height < 1440) {
return 0;
}
int i = 0;
for (long l : Nxt.Block.getLastBlock().transactions)
{
Nxt.Transaction localTransaction = (Nxt.Transaction)Nxt.transactions.get(Long.valueOf(l));
if (localTransaction.recipient == this.id) {
i += localTransaction.amount;
}
}
return (int)(getBalance() / 100L) - i;
}
static long getId(byte[] paramArrayOfByte)
throws Exception
{
byte[] arrayOfByte = MessageDigest.getInstance("SHA-256").digest(paramArrayOfByte);
BigInteger localBigInteger = new BigInteger(1, new byte[] { arrayOfByte[7], arrayOfByte[6], arrayOfByte[5], arrayOfByte[4], arrayOfByte[3], arrayOfByte[2], arrayOfByte[1], arrayOfByte[0] });
return localBigInteger.longValue();
}
synchronized Integer getAssetBalance(Long paramLong)
{
return (Integer)this.assetBalances.get(paramLong);
}
synchronized Integer getUnconfirmedAssetBalance(Long paramLong)
{
return (Integer)this.unconfirmedAssetBalances.get(paramLong);
}
synchronized void addToAssetBalance(Long paramLong, int paramInt)
{
Integer localInteger = (Integer)this.assetBalances.get(paramLong);
if (localInteger == null) {
this.assetBalances.put(paramLong, Integer.valueOf(paramInt));
} else {
this.assetBalances.put(paramLong, Integer.valueOf(localInteger.intValue() + paramInt));
}
}
synchronized void addToUnconfirmedAssetBalance(Long paramLong, int paramInt)
{
Integer localInteger = (Integer)this.unconfirmedAssetBalances.get(paramLong);
if (localInteger == null) {
this.unconfirmedAssetBalances.put(paramLong, Integer.valueOf(paramInt));
} else {
this.unconfirmedAssetBalances.put(paramLong, Integer.valueOf(localInteger.intValue() + paramInt));
}
}
synchronized void addToAssetAndUnconfirmedAssetBalance(Long paramLong, int paramInt)
{
Integer localInteger = (Integer)this.assetBalances.get(paramLong);
if (localInteger == null)
{
this.assetBalances.put(paramLong, Integer.valueOf(paramInt));
this.unconfirmedAssetBalances.put(paramLong, Integer.valueOf(paramInt));
}
else
{
this.assetBalances.put(paramLong, Integer.valueOf(localInteger.intValue() + paramInt));
this.unconfirmedAssetBalances.put(paramLong, Integer.valueOf(((Integer)this.unconfirmedAssetBalances.get(paramLong)).intValue() + paramInt));
}
}
synchronized long getBalance()
{
return this.balance;
}
long getGuaranteedBalance(int paramInt)
throws Exception
{
long l1 = getBalance();
ArrayList localArrayList = Nxt.Block.getLastBlocks(paramInt - 1);
byte[] arrayOfByte = (byte[])this.publicKey.get();
Iterator localIterator = localArrayList.iterator();
while (localIterator.hasNext())
{
Nxt.Block localBlock = (Nxt.Block)localIterator.next();
if ((Arrays.equals(localBlock.generatorPublicKey, arrayOfByte)) && (l1 -= localBlock.totalFee * 100L <= 0L)) {
return 0L;
}
int i = localBlock.numberOfTransactions;
while (i-- > 0)
{
Nxt.Transaction localTransaction = (Nxt.Transaction)Nxt.transactions.get(Long.valueOf(localBlock.transactions[i]));
long l2;
if (Arrays.equals(localTransaction.senderPublicKey, arrayOfByte))
{
l2 = localTransaction.getSenderDeltaBalance();
if ((l2 > 0L) && (l1 -= l2 <= 0L)) {
return 0L;
}
if ((l2 < 0L) && (l1 += l2 <= 0L)) {
return 0L;
}
}
if (localTransaction.recipient == this.id)
{
l2 = localTransaction.getRecipientDeltaBalance();
if ((l2 > 0L) && (l1 -= l2 <= 0L)) {
return 0L;
}
if ((l2 < 0L) && (l1 += l2 <= 0L)) {
return 0L;
}
}
}
}
return l1;
}
synchronized long getUnconfirmedBalance()
{
return this.unconfirmedBalance;
}
void addToBalance(long paramLong)
throws Exception
{
synchronized (this)
{
this.balance += paramLong;
}
updatePeerWeights();
}
void addToUnconfirmedBalance(long paramLong)
throws Exception
{
synchronized (this)
{
this.unconfirmedBalance += paramLong;
}
updateUserUnconfirmedBalance();
}
void addToBalanceAndUnconfirmedBalance(long paramLong)
throws Exception
{
synchronized (this)
{
this.balance += paramLong;
this.unconfirmedBalance += paramLong;
}
updatePeerWeights();
updateUserUnconfirmedBalance();
}
private void updatePeerWeights()
{
Iterator localIterator = Nxt.peers.values().iterator();
while (localIterator.hasNext())
{
Nxt.Peer localPeer = (Nxt.Peer)localIterator.next();
if ((localPeer.accountId == this.id) && (localPeer.adjustedWeight > 0L)) {
localPeer.updateWeight();
}
}
}
private void updateUserUnconfirmedBalance()
throws Exception
{
JSONObject localJSONObject = new JSONObject();
localJSONObject.put("response", "setBalance");
localJSONObject.put("balance", Long.valueOf(getUnconfirmedBalance()));
byte[] arrayOfByte = (byte[])this.publicKey.get();
Iterator localIterator = Nxt.users.values().iterator();
while (localIterator.hasNext())
{
Nxt.User localUser = (Nxt.User)localIterator.next();
if ((localUser.secretPhrase != null) && (Arrays.equals(Nxt.Crypto.getPublicKey(localUser.secretPhrase), arrayOfByte))) {
localUser.send(localJSONObject);
}
}
}