forked from BurstProject/burstcoin
/
TransactionType.java
2411 lines (2011 loc) · 112 KB
/
TransactionType.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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package nxt;
import nxt.Attachment.AbstractAttachment;
import nxt.Attachment.AutomatedTransactionsCreation;
import nxt.NxtException.NotValidException;
import nxt.NxtException.ValidationException;
import nxt.at.AT_Constants;
import nxt.at.AT_Controller;
import nxt.at.AT_Exception;
import nxt.util.Convert;
import org.json.simple.JSONObject;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public abstract class TransactionType {
private static final byte TYPE_PAYMENT = 0;
private static final byte TYPE_MESSAGING = 1;
private static final byte TYPE_COLORED_COINS = 2;
private static final byte TYPE_DIGITAL_GOODS = 3;
private static final byte TYPE_ACCOUNT_CONTROL = 4;
private static final byte TYPE_BURST_MINING = 20; // jump some for easier nxt updating
private static final byte TYPE_ADVANCED_PAYMENT = 21;
private static final byte TYPE_AUTOMATED_TRANSACTIONS = 22;
private static final byte SUBTYPE_PAYMENT_ORDINARY_PAYMENT = 0;
private static final byte SUBTYPE_MESSAGING_ARBITRARY_MESSAGE = 0;
private static final byte SUBTYPE_MESSAGING_ALIAS_ASSIGNMENT = 1;
private static final byte SUBTYPE_MESSAGING_POLL_CREATION = 2;
private static final byte SUBTYPE_MESSAGING_VOTE_CASTING = 3;
private static final byte SUBTYPE_MESSAGING_HUB_ANNOUNCEMENT = 4;
private static final byte SUBTYPE_MESSAGING_ACCOUNT_INFO = 5;
private static final byte SUBTYPE_MESSAGING_ALIAS_SELL = 6;
private static final byte SUBTYPE_MESSAGING_ALIAS_BUY = 7;
private static final byte SUBTYPE_COLORED_COINS_ASSET_ISSUANCE = 0;
private static final byte SUBTYPE_COLORED_COINS_ASSET_TRANSFER = 1;
private static final byte SUBTYPE_COLORED_COINS_ASK_ORDER_PLACEMENT = 2;
private static final byte SUBTYPE_COLORED_COINS_BID_ORDER_PLACEMENT = 3;
private static final byte SUBTYPE_COLORED_COINS_ASK_ORDER_CANCELLATION = 4;
private static final byte SUBTYPE_COLORED_COINS_BID_ORDER_CANCELLATION = 5;
private static final byte SUBTYPE_DIGITAL_GOODS_LISTING = 0;
private static final byte SUBTYPE_DIGITAL_GOODS_DELISTING = 1;
private static final byte SUBTYPE_DIGITAL_GOODS_PRICE_CHANGE = 2;
private static final byte SUBTYPE_DIGITAL_GOODS_QUANTITY_CHANGE = 3;
private static final byte SUBTYPE_DIGITAL_GOODS_PURCHASE = 4;
private static final byte SUBTYPE_DIGITAL_GOODS_DELIVERY = 5;
private static final byte SUBTYPE_DIGITAL_GOODS_FEEDBACK = 6;
private static final byte SUBTYPE_DIGITAL_GOODS_REFUND = 7;
private static final byte SUBTYPE_AT_CREATION = 0;
private static final byte SUBTYPE_AT_NXT_PAYMENT = 1;
private static final byte SUBTYPE_ACCOUNT_CONTROL_EFFECTIVE_BALANCE_LEASING = 0;
private static final byte SUBTYPE_BURST_MINING_REWARD_RECIPIENT_ASSIGNMENT = 0;
private static final byte SUBTYPE_ADVANCED_PAYMENT_ESCROW_CREATION = 0;
private static final byte SUBTYPE_ADVANCED_PAYMENT_ESCROW_SIGN = 1;
private static final byte SUBTYPE_ADVANCED_PAYMENT_ESCROW_RESULT = 2;
private static final byte SUBTYPE_ADVANCED_PAYMENT_SUBSCRIPTION_SUBSCRIBE = 3;
private static final byte SUBTYPE_ADVANCED_PAYMENT_SUBSCRIPTION_CANCEL = 4;
private static final byte SUBTYPE_ADVANCED_PAYMENT_SUBSCRIPTION_PAYMENT = 5;
private static final int BASELINE_FEE_HEIGHT = 1; // At release time must be less than current block - 1440
private static final Fee BASELINE_FEE = new Fee(Constants.ONE_NXT, 0);
private static final Fee BASELINE_ASSET_ISSUANCE_FEE = new Fee(1000 * Constants.ONE_NXT, 0);
private static final int NEXT_FEE_HEIGHT = Integer.MAX_VALUE;
private static final Fee NEXT_FEE = new Fee(Constants.ONE_NXT, 0);
private static final Fee NEXT_ASSET_ISSUANCE_FEE = new Fee(1000 * Constants.ONE_NXT, 0);
public static TransactionType findTransactionType(byte type, byte subtype) {
switch (type) {
case TYPE_PAYMENT:
switch (subtype) {
case SUBTYPE_PAYMENT_ORDINARY_PAYMENT:
return Payment.ORDINARY;
default:
return null;
}
case TYPE_MESSAGING:
switch (subtype) {
case SUBTYPE_MESSAGING_ARBITRARY_MESSAGE:
return Messaging.ARBITRARY_MESSAGE;
case SUBTYPE_MESSAGING_ALIAS_ASSIGNMENT:
return Messaging.ALIAS_ASSIGNMENT;
case SUBTYPE_MESSAGING_POLL_CREATION:
return Messaging.POLL_CREATION;
case SUBTYPE_MESSAGING_VOTE_CASTING:
return Messaging.VOTE_CASTING;
case SUBTYPE_MESSAGING_HUB_ANNOUNCEMENT:
return Messaging.HUB_ANNOUNCEMENT;
case SUBTYPE_MESSAGING_ACCOUNT_INFO:
return Messaging.ACCOUNT_INFO;
case SUBTYPE_MESSAGING_ALIAS_SELL:
return Messaging.ALIAS_SELL;
case SUBTYPE_MESSAGING_ALIAS_BUY:
return Messaging.ALIAS_BUY;
default:
return null;
}
case TYPE_COLORED_COINS:
switch (subtype) {
case SUBTYPE_COLORED_COINS_ASSET_ISSUANCE:
return ColoredCoins.ASSET_ISSUANCE;
case SUBTYPE_COLORED_COINS_ASSET_TRANSFER:
return ColoredCoins.ASSET_TRANSFER;
case SUBTYPE_COLORED_COINS_ASK_ORDER_PLACEMENT:
return ColoredCoins.ASK_ORDER_PLACEMENT;
case SUBTYPE_COLORED_COINS_BID_ORDER_PLACEMENT:
return ColoredCoins.BID_ORDER_PLACEMENT;
case SUBTYPE_COLORED_COINS_ASK_ORDER_CANCELLATION:
return ColoredCoins.ASK_ORDER_CANCELLATION;
case SUBTYPE_COLORED_COINS_BID_ORDER_CANCELLATION:
return ColoredCoins.BID_ORDER_CANCELLATION;
default:
return null;
}
case TYPE_DIGITAL_GOODS:
switch (subtype) {
case SUBTYPE_DIGITAL_GOODS_LISTING:
return DigitalGoods.LISTING;
case SUBTYPE_DIGITAL_GOODS_DELISTING:
return DigitalGoods.DELISTING;
case SUBTYPE_DIGITAL_GOODS_PRICE_CHANGE:
return DigitalGoods.PRICE_CHANGE;
case SUBTYPE_DIGITAL_GOODS_QUANTITY_CHANGE:
return DigitalGoods.QUANTITY_CHANGE;
case SUBTYPE_DIGITAL_GOODS_PURCHASE:
return DigitalGoods.PURCHASE;
case SUBTYPE_DIGITAL_GOODS_DELIVERY:
return DigitalGoods.DELIVERY;
case SUBTYPE_DIGITAL_GOODS_FEEDBACK:
return DigitalGoods.FEEDBACK;
case SUBTYPE_DIGITAL_GOODS_REFUND:
return DigitalGoods.REFUND;
default:
return null;
}
case TYPE_ACCOUNT_CONTROL:
switch (subtype) {
case SUBTYPE_ACCOUNT_CONTROL_EFFECTIVE_BALANCE_LEASING:
return AccountControl.EFFECTIVE_BALANCE_LEASING;
default:
return null;
}
case TYPE_BURST_MINING:
switch (subtype) {
case SUBTYPE_BURST_MINING_REWARD_RECIPIENT_ASSIGNMENT:
return BurstMining.REWARD_RECIPIENT_ASSIGNMENT;
default:
return null;
}
case TYPE_ADVANCED_PAYMENT:
switch (subtype) {
case SUBTYPE_ADVANCED_PAYMENT_ESCROW_CREATION:
return AdvancedPayment.ESCROW_CREATION;
case SUBTYPE_ADVANCED_PAYMENT_ESCROW_SIGN:
return AdvancedPayment.ESCROW_SIGN;
case SUBTYPE_ADVANCED_PAYMENT_ESCROW_RESULT:
return AdvancedPayment.ESCROW_RESULT;
case SUBTYPE_ADVANCED_PAYMENT_SUBSCRIPTION_SUBSCRIBE:
return AdvancedPayment.SUBSCRIPTION_SUBSCRIBE;
case SUBTYPE_ADVANCED_PAYMENT_SUBSCRIPTION_CANCEL:
return AdvancedPayment.SUBSCRIPTION_CANCEL;
case SUBTYPE_ADVANCED_PAYMENT_SUBSCRIPTION_PAYMENT:
return AdvancedPayment.SUBSCRIPTION_PAYMENT;
default:
return null;
}
case TYPE_AUTOMATED_TRANSACTIONS:
switch (subtype) {
case SUBTYPE_AT_CREATION:
return AutomatedTransactions.AUTOMATED_TRANSACTION_CREATION;
case SUBTYPE_AT_NXT_PAYMENT:
return AutomatedTransactions.AT_PAYMENT;
default:
return null;
}
default:
return null;
}
}
private TransactionType() {
}
public abstract byte getType();
public abstract byte getSubtype();
abstract Attachment.AbstractAttachment parseAttachment(ByteBuffer buffer, byte transactionVersion) throws NxtException.NotValidException;
abstract Attachment.AbstractAttachment parseAttachment(JSONObject attachmentData) throws NxtException.NotValidException;
abstract void validateAttachment(Transaction transaction) throws NxtException.ValidationException;
// return false iff double spending
final boolean applyUnconfirmed(Transaction transaction, Account senderAccount) {
long totalAmountNQT = Convert.safeAdd(transaction.getAmountNQT(), transaction.getFeeNQT());
if (transaction.getReferencedTransactionFullHash() != null
&& transaction.getTimestamp() > Constants.REFERENCED_TRANSACTION_FULL_HASH_BLOCK_TIMESTAMP) {
totalAmountNQT = Convert.safeAdd(totalAmountNQT, Constants.UNCONFIRMED_POOL_DEPOSIT_NQT);
}
if (senderAccount.getUnconfirmedBalanceNQT() < totalAmountNQT) {
return false;
}
senderAccount.addToUnconfirmedBalanceNQT(-totalAmountNQT);
if (!applyAttachmentUnconfirmed(transaction, senderAccount)) {
senderAccount.addToUnconfirmedBalanceNQT(totalAmountNQT);
return false;
}
return true;
}
abstract boolean applyAttachmentUnconfirmed(Transaction transaction, Account senderAccount);
final void apply(Transaction transaction, Account senderAccount, Account recipientAccount) {
senderAccount.addToBalanceNQT(- (Convert.safeAdd(transaction.getAmountNQT(), transaction.getFeeNQT())));
if (transaction.getReferencedTransactionFullHash() != null
&& transaction.getTimestamp() > Constants.REFERENCED_TRANSACTION_FULL_HASH_BLOCK_TIMESTAMP) {
senderAccount.addToUnconfirmedBalanceNQT(Constants.UNCONFIRMED_POOL_DEPOSIT_NQT);
}
if (recipientAccount != null) {
recipientAccount.addToBalanceAndUnconfirmedBalanceNQT(transaction.getAmountNQT());
}
applyAttachment(transaction, senderAccount, recipientAccount);
}
abstract void applyAttachment(Transaction transaction, Account senderAccount, Account recipientAccount);
final void undoUnconfirmed(Transaction transaction, Account senderAccount) {
undoAttachmentUnconfirmed(transaction, senderAccount);
senderAccount.addToUnconfirmedBalanceNQT(Convert.safeAdd(transaction.getAmountNQT(), transaction.getFeeNQT()));
if (transaction.getReferencedTransactionFullHash() != null
&& transaction.getTimestamp() > Constants.REFERENCED_TRANSACTION_FULL_HASH_BLOCK_TIMESTAMP) {
senderAccount.addToUnconfirmedBalanceNQT(Constants.UNCONFIRMED_POOL_DEPOSIT_NQT);
}
}
abstract void undoAttachmentUnconfirmed(Transaction transaction, Account senderAccount);
boolean isDuplicate(Transaction transaction, Map<TransactionType, Set<String>> duplicates) {
return false;
}
static boolean isDuplicate(TransactionType uniqueType, String key, Map<TransactionType, Set<String>> duplicates) {
Set<String> typeDuplicates = duplicates.get(uniqueType);
if (typeDuplicates == null) {
typeDuplicates = new HashSet<>();
duplicates.put(uniqueType, typeDuplicates);
}
return ! typeDuplicates.add(key);
}
public abstract boolean hasRecipient();
public boolean isSigned() {
return true;
}
@Override
public final String toString() {
return "type: " + getType() + ", subtype: " + getSubtype();
}
/*
Collection<TransactionType> getPhasingTransactionTypes() {
return Collections.emptyList();
}
Collection<TransactionType> getPhasedTransactionTypes() {
return Collections.emptyList();
}
*/
public static abstract class Payment extends TransactionType {
private Payment() {
}
@Override
public final byte getType() {
return TransactionType.TYPE_PAYMENT;
}
@Override
final boolean applyAttachmentUnconfirmed(Transaction transaction, Account senderAccount) {
return true;
}
@Override
final void applyAttachment(Transaction transaction, Account senderAccount, Account recipientAccount) {
}
@Override
final void undoAttachmentUnconfirmed(Transaction transaction, Account senderAccount) {
}
@Override
final public boolean hasRecipient() {
return true;
}
public static final TransactionType ORDINARY = new Payment() {
@Override
public final byte getSubtype() {
return TransactionType.SUBTYPE_PAYMENT_ORDINARY_PAYMENT;
}
@Override
Attachment.EmptyAttachment parseAttachment(ByteBuffer buffer, byte transactionVersion) throws NxtException.NotValidException {
return Attachment.ORDINARY_PAYMENT;
}
@Override
Attachment.EmptyAttachment parseAttachment(JSONObject attachmentData) throws NxtException.NotValidException {
return Attachment.ORDINARY_PAYMENT;
}
@Override
void validateAttachment(Transaction transaction) throws NxtException.ValidationException {
if (transaction.getAmountNQT() <= 0 || transaction.getAmountNQT() >= Constants.MAX_BALANCE_NQT) {
throw new NxtException.NotValidException("Invalid ordinary payment");
}
}
};
}
public static abstract class Messaging extends TransactionType {
private Messaging() {
}
@Override
public final byte getType() {
return TransactionType.TYPE_MESSAGING;
}
@Override
final boolean applyAttachmentUnconfirmed(Transaction transaction, Account senderAccount) {
return true;
}
@Override
final void undoAttachmentUnconfirmed(Transaction transaction, Account senderAccount) {
}
public final static TransactionType ARBITRARY_MESSAGE = new Messaging() {
@Override
public final byte getSubtype() {
return TransactionType.SUBTYPE_MESSAGING_ARBITRARY_MESSAGE;
}
@Override
Attachment.EmptyAttachment parseAttachment(ByteBuffer buffer, byte transactionVersion) throws NxtException.NotValidException {
return Attachment.ARBITRARY_MESSAGE;
}
@Override
Attachment.EmptyAttachment parseAttachment(JSONObject attachmentData) throws NxtException.NotValidException {
return Attachment.ARBITRARY_MESSAGE;
}
@Override
void applyAttachment(Transaction transaction, Account senderAccount, Account recipientAccount) {
}
@Override
void validateAttachment(Transaction transaction) throws NxtException.ValidationException {
Attachment attachment = transaction.getAttachment();
if (transaction.getAmountNQT() != 0) {
throw new NxtException.NotValidException("Invalid arbitrary message: " + attachment.getJSONObject());
}
if (Nxt.getBlockchain().getHeight() < Constants.DIGITAL_GOODS_STORE_BLOCK && transaction.getMessage() == null) {
throw new NxtException.NotCurrentlyValidException("Missing message appendix not allowed before DGS block");
}
}
@Override
public boolean hasRecipient() {
return true;
}
};
public static final TransactionType ALIAS_ASSIGNMENT = new Messaging() {
@Override
public final byte getSubtype() {
return TransactionType.SUBTYPE_MESSAGING_ALIAS_ASSIGNMENT;
}
@Override
Attachment.MessagingAliasAssignment parseAttachment(ByteBuffer buffer, byte transactionVersion) throws NxtException.NotValidException {
return new Attachment.MessagingAliasAssignment(buffer, transactionVersion);
}
@Override
Attachment.MessagingAliasAssignment parseAttachment(JSONObject attachmentData) throws NxtException.NotValidException {
return new Attachment.MessagingAliasAssignment(attachmentData);
}
@Override
void applyAttachment(Transaction transaction, Account senderAccount, Account recipientAccount) {
Attachment.MessagingAliasAssignment attachment = (Attachment.MessagingAliasAssignment) transaction.getAttachment();
Alias.addOrUpdateAlias(transaction, attachment);
}
@Override
boolean isDuplicate(Transaction transaction, Map<TransactionType, Set<String>> duplicates) {
Attachment.MessagingAliasAssignment attachment = (Attachment.MessagingAliasAssignment) transaction.getAttachment();
return isDuplicate(Messaging.ALIAS_ASSIGNMENT, attachment.getAliasName().toLowerCase(), duplicates);
}
@Override
void validateAttachment(Transaction transaction) throws NxtException.ValidationException {
Attachment.MessagingAliasAssignment attachment = (Attachment.MessagingAliasAssignment) transaction.getAttachment();
if (attachment.getAliasName().length() == 0
|| Convert.toBytes(attachment.getAliasName()).length > Constants.MAX_ALIAS_LENGTH
|| attachment.getAliasURI().length() > Constants.MAX_ALIAS_URI_LENGTH) {
throw new NxtException.NotValidException("Invalid alias assignment: " + attachment.getJSONObject());
}
String normalizedAlias = attachment.getAliasName().toLowerCase();
for (int i = 0; i < normalizedAlias.length(); i++) {
if (Constants.ALPHABET.indexOf(normalizedAlias.charAt(i)) < 0) {
throw new NxtException.NotValidException("Invalid alias name: " + normalizedAlias);
}
}
Alias alias = Alias.getAlias(normalizedAlias);
if (alias != null && alias.getAccountId() != transaction.getSenderId()) {
throw new NxtException.NotCurrentlyValidException("Alias already owned by another account: " + normalizedAlias);
}
}
@Override
public boolean hasRecipient() {
return false;
}
};
public static final TransactionType ALIAS_SELL = new Messaging() {
@Override
public final byte getSubtype() {
return TransactionType.SUBTYPE_MESSAGING_ALIAS_SELL;
}
@Override
Attachment.MessagingAliasSell parseAttachment(ByteBuffer buffer, byte transactionVersion) throws NxtException.NotValidException {
return new Attachment.MessagingAliasSell(buffer, transactionVersion);
}
@Override
Attachment.MessagingAliasSell parseAttachment(JSONObject attachmentData) throws NxtException.NotValidException {
return new Attachment.MessagingAliasSell(attachmentData);
}
@Override
void applyAttachment(Transaction transaction, Account senderAccount, Account recipientAccount) {
final Attachment.MessagingAliasSell attachment =
(Attachment.MessagingAliasSell) transaction.getAttachment();
Alias.sellAlias(transaction, attachment);
}
@Override
boolean isDuplicate(Transaction transaction, Map<TransactionType, Set<String>> duplicates) {
Attachment.MessagingAliasSell attachment = (Attachment.MessagingAliasSell) transaction.getAttachment();
// not a bug, uniqueness is based on Messaging.ALIAS_ASSIGNMENT
return isDuplicate(Messaging.ALIAS_ASSIGNMENT, attachment.getAliasName().toLowerCase(), duplicates);
}
@Override
void validateAttachment(Transaction transaction) throws NxtException.ValidationException {
if (Nxt.getBlockchain().getLastBlock().getHeight() < Constants.DIGITAL_GOODS_STORE_BLOCK) {
throw new NxtException.NotYetEnabledException("Alias transfer not yet enabled at height " + Nxt.getBlockchain().getLastBlock().getHeight());
}
if (transaction.getAmountNQT() != 0) {
throw new NxtException.NotValidException("Invalid sell alias transaction: " +
transaction.getJSONObject());
}
final Attachment.MessagingAliasSell attachment =
(Attachment.MessagingAliasSell) transaction.getAttachment();
final String aliasName = attachment.getAliasName();
if (aliasName == null || aliasName.length() == 0) {
throw new NxtException.NotValidException("Missing alias name");
}
long priceNQT = attachment.getPriceNQT();
if (priceNQT < 0 || priceNQT > Constants.MAX_BALANCE_NQT) {
throw new NxtException.NotValidException("Invalid alias sell price: " + priceNQT);
}
if (priceNQT == 0) {
if (Genesis.CREATOR_ID == transaction.getRecipientId()) {
throw new NxtException.NotValidException("Transferring aliases to Genesis account not allowed");
} else if (transaction.getRecipientId() == 0) {
throw new NxtException.NotValidException("Missing alias transfer recipient");
}
}
final Alias alias = Alias.getAlias(aliasName);
if (alias == null) {
throw new NxtException.NotCurrentlyValidException("Alias hasn't been registered yet: " + aliasName);
} else if (alias.getAccountId() != transaction.getSenderId()) {
throw new NxtException.NotCurrentlyValidException("Alias doesn't belong to sender: " + aliasName);
}
}
@Override
public boolean hasRecipient() {
return true;
}
};
public static final TransactionType ALIAS_BUY = new Messaging() {
@Override
public final byte getSubtype() {
return TransactionType.SUBTYPE_MESSAGING_ALIAS_BUY;
}
@Override
Attachment.MessagingAliasBuy parseAttachment(ByteBuffer buffer, byte transactionVersion) throws NxtException.NotValidException {
return new Attachment.MessagingAliasBuy(buffer, transactionVersion);
}
@Override
Attachment.MessagingAliasBuy parseAttachment(JSONObject attachmentData) throws NxtException.NotValidException {
return new Attachment.MessagingAliasBuy(attachmentData);
}
@Override
void applyAttachment(Transaction transaction, Account senderAccount, Account recipientAccount) {
final Attachment.MessagingAliasBuy attachment =
(Attachment.MessagingAliasBuy) transaction.getAttachment();
final String aliasName = attachment.getAliasName();
Alias.changeOwner(transaction.getSenderId(), aliasName, transaction.getBlockTimestamp());
}
@Override
boolean isDuplicate(Transaction transaction, Map<TransactionType, Set<String>> duplicates) {
Attachment.MessagingAliasBuy attachment = (Attachment.MessagingAliasBuy) transaction.getAttachment();
// not a bug, uniqueness is based on Messaging.ALIAS_ASSIGNMENT
return isDuplicate(Messaging.ALIAS_ASSIGNMENT, attachment.getAliasName().toLowerCase(), duplicates);
}
@Override
void validateAttachment(Transaction transaction) throws NxtException.ValidationException {
if (Nxt.getBlockchain().getLastBlock().getHeight() < Constants.DIGITAL_GOODS_STORE_BLOCK) {
throw new NxtException.NotYetEnabledException("Alias transfer not yet enabled at height " + Nxt.getBlockchain().getLastBlock().getHeight());
}
final Attachment.MessagingAliasBuy attachment =
(Attachment.MessagingAliasBuy) transaction.getAttachment();
final String aliasName = attachment.getAliasName();
final Alias alias = Alias.getAlias(aliasName);
if (alias == null) {
throw new NxtException.NotCurrentlyValidException("Alias hasn't been registered yet: " + aliasName);
} else if (alias.getAccountId() != transaction.getRecipientId()) {
throw new NxtException.NotCurrentlyValidException("Alias is owned by account other than recipient: "
+ Convert.toUnsignedLong(alias.getAccountId()));
}
Alias.Offer offer = Alias.getOffer(alias);
if (offer == null) {
throw new NxtException.NotCurrentlyValidException("Alias is not for sale: " + aliasName);
}
if (transaction.getAmountNQT() < offer.getPriceNQT()) {
String msg = "Price is too low for: " + aliasName + " ("
+ transaction.getAmountNQT() + " < " + offer.getPriceNQT() + ")";
throw new NxtException.NotCurrentlyValidException(msg);
}
if (offer.getBuyerId() != 0 && offer.getBuyerId() != transaction.getSenderId()) {
throw new NxtException.NotCurrentlyValidException("Wrong buyer for " + aliasName + ": "
+ Convert.toUnsignedLong(transaction.getSenderId()) + " expected: "
+ Convert.toUnsignedLong(offer.getBuyerId()));
}
}
@Override
public boolean hasRecipient() {
return true;
}
};
public final static TransactionType POLL_CREATION = new Messaging() {
@Override
public final byte getSubtype() {
return TransactionType.SUBTYPE_MESSAGING_POLL_CREATION;
}
@Override
Attachment.MessagingPollCreation parseAttachment(ByteBuffer buffer, byte transactionVersion) throws NxtException.NotValidException {
return new Attachment.MessagingPollCreation(buffer, transactionVersion);
}
@Override
Attachment.MessagingPollCreation parseAttachment(JSONObject attachmentData) throws NxtException.NotValidException {
return new Attachment.MessagingPollCreation(attachmentData);
}
@Override
void applyAttachment(Transaction transaction, Account senderAccount, Account recipientAccount) {
Attachment.MessagingPollCreation attachment = (Attachment.MessagingPollCreation) transaction.getAttachment();
Poll.addPoll(transaction, attachment);
}
@Override
void validateAttachment(Transaction transaction) throws NxtException.ValidationException {
if (Nxt.getBlockchain().getLastBlock().getHeight() < Constants.VOTING_SYSTEM_BLOCK) {
throw new NxtException.NotYetEnabledException("Voting System not yet enabled at height " + Nxt.getBlockchain().getLastBlock().getHeight());
}
Attachment.MessagingPollCreation attachment = (Attachment.MessagingPollCreation) transaction.getAttachment();
for (int i = 0; i < attachment.getPollOptions().length; i++) {
if (attachment.getPollOptions()[i].length() > Constants.MAX_POLL_OPTION_LENGTH) {
throw new NxtException.NotValidException("Invalid poll options length: " + attachment.getJSONObject());
}
}
if (attachment.getPollName().length() > Constants.MAX_POLL_NAME_LENGTH
|| attachment.getPollDescription().length() > Constants.MAX_POLL_DESCRIPTION_LENGTH
|| attachment.getPollOptions().length > Constants.MAX_POLL_OPTION_COUNT) {
throw new NxtException.NotValidException("Invalid poll attachment: " + attachment.getJSONObject());
}
}
@Override
public boolean hasRecipient() {
return false;
}
};
public final static TransactionType VOTE_CASTING = new Messaging() {
@Override
public final byte getSubtype() {
return TransactionType.SUBTYPE_MESSAGING_VOTE_CASTING;
}
@Override
Attachment.MessagingVoteCasting parseAttachment(ByteBuffer buffer, byte transactionVersion) throws NxtException.NotValidException {
return new Attachment.MessagingVoteCasting(buffer, transactionVersion);
}
@Override
Attachment.MessagingVoteCasting parseAttachment(JSONObject attachmentData) throws NxtException.NotValidException {
return new Attachment.MessagingVoteCasting(attachmentData);
}
@Override
void applyAttachment(Transaction transaction, Account senderAccount, Account recipientAccount) {
Attachment.MessagingVoteCasting attachment = (Attachment.MessagingVoteCasting) transaction.getAttachment();
Poll poll = Poll.getPoll(attachment.getPollId());
if (poll != null) {
Vote.addVote(transaction, attachment);
}
}
@Override
void validateAttachment(Transaction transaction) throws NxtException.ValidationException {
if (Nxt.getBlockchain().getLastBlock().getHeight() < Constants.VOTING_SYSTEM_BLOCK) {
throw new NxtException.NotYetEnabledException("Voting System not yet enabled at height " + Nxt.getBlockchain().getLastBlock().getHeight());
}
Attachment.MessagingVoteCasting attachment = (Attachment.MessagingVoteCasting) transaction.getAttachment();
if (attachment.getPollId() == 0 || attachment.getPollVote() == null
|| attachment.getPollVote().length > Constants.MAX_POLL_OPTION_COUNT) {
throw new NxtException.NotValidException("Invalid vote casting attachment: " + attachment.getJSONObject());
}
if (Poll.getPoll(attachment.getPollId()) == null) {
throw new NxtException.NotCurrentlyValidException("Invalid poll: " + Convert.toUnsignedLong(attachment.getPollId()));
}
}
@Override
public boolean hasRecipient() {
return false;
}
};
public static final TransactionType HUB_ANNOUNCEMENT = new Messaging() {
@Override
public final byte getSubtype() {
return TransactionType.SUBTYPE_MESSAGING_HUB_ANNOUNCEMENT;
}
@Override
Attachment.MessagingHubAnnouncement parseAttachment(ByteBuffer buffer, byte transactionVersion) throws NxtException.NotValidException {
return new Attachment.MessagingHubAnnouncement(buffer, transactionVersion);
}
@Override
Attachment.MessagingHubAnnouncement parseAttachment(JSONObject attachmentData) throws NxtException.NotValidException {
return new Attachment.MessagingHubAnnouncement(attachmentData);
}
@Override
void applyAttachment(Transaction transaction, Account senderAccount, Account recipientAccount) {
Attachment.MessagingHubAnnouncement attachment = (Attachment.MessagingHubAnnouncement) transaction.getAttachment();
Hub.addOrUpdateHub(transaction, attachment);
}
@Override
void validateAttachment(Transaction transaction) throws NxtException.ValidationException {
if (Nxt.getBlockchain().getLastBlock().getHeight() < Constants.TRANSPARENT_FORGING_BLOCK_7) {
throw new NxtException.NotYetEnabledException("Hub terminal announcement not yet enabled at height " + Nxt.getBlockchain().getLastBlock().getHeight());
}
Attachment.MessagingHubAnnouncement attachment = (Attachment.MessagingHubAnnouncement) transaction.getAttachment();
if (attachment.getMinFeePerByteNQT() < 0 || attachment.getMinFeePerByteNQT() > Constants.MAX_BALANCE_NQT
|| attachment.getUris().length > Constants.MAX_HUB_ANNOUNCEMENT_URIS) {
// cfb: "0" is allowed to show that another way to determine the min fee should be used
throw new NxtException.NotValidException("Invalid hub terminal announcement: " + attachment.getJSONObject());
}
for (String uri : attachment.getUris()) {
if (uri.length() > Constants.MAX_HUB_ANNOUNCEMENT_URI_LENGTH) {
throw new NxtException.NotValidException("Invalid URI length: " + uri.length());
}
//TODO: also check URI validity here?
}
}
@Override
public boolean hasRecipient() {
return false;
}
};
public static final Messaging ACCOUNT_INFO = new Messaging() {
@Override
public byte getSubtype() {
return TransactionType.SUBTYPE_MESSAGING_ACCOUNT_INFO;
}
@Override
Attachment.MessagingAccountInfo parseAttachment(ByteBuffer buffer, byte transactionVersion) throws NxtException.NotValidException {
return new Attachment.MessagingAccountInfo(buffer, transactionVersion);
}
@Override
Attachment.MessagingAccountInfo parseAttachment(JSONObject attachmentData) throws NxtException.NotValidException {
return new Attachment.MessagingAccountInfo(attachmentData);
}
@Override
void validateAttachment(Transaction transaction) throws NxtException.ValidationException {
Attachment.MessagingAccountInfo attachment = (Attachment.MessagingAccountInfo)transaction.getAttachment();
if (Convert.toBytes(attachment.getName()).length > Constants.MAX_ACCOUNT_NAME_LENGTH
|| Convert.toBytes(attachment.getDescription()).length > Constants.MAX_ACCOUNT_DESCRIPTION_LENGTH
) {
throw new NxtException.NotValidException("Invalid account info issuance: " + attachment.getJSONObject());
}
}
@Override
void applyAttachment(Transaction transaction, Account senderAccount, Account recipientAccount) {
Attachment.MessagingAccountInfo attachment = (Attachment.MessagingAccountInfo) transaction.getAttachment();
senderAccount.setAccountInfo(attachment.getName(), attachment.getDescription());
}
@Override
public boolean hasRecipient() {
return false;
}
};
}
public static abstract class ColoredCoins extends TransactionType {
private ColoredCoins() {}
@Override
public final byte getType() {
return TransactionType.TYPE_COLORED_COINS;
}
public static final TransactionType ASSET_ISSUANCE = new ColoredCoins() {
@Override
public final byte getSubtype() {
return TransactionType.SUBTYPE_COLORED_COINS_ASSET_ISSUANCE;
}
@Override
public Fee getBaselineFee() {
return BASELINE_ASSET_ISSUANCE_FEE;
}
@Override
public Fee getNextFee() {
return NEXT_ASSET_ISSUANCE_FEE;
}
@Override
Attachment.ColoredCoinsAssetIssuance parseAttachment(ByteBuffer buffer, byte transactionVersion) throws NxtException.NotValidException {
return new Attachment.ColoredCoinsAssetIssuance(buffer, transactionVersion);
}
@Override
Attachment.ColoredCoinsAssetIssuance parseAttachment(JSONObject attachmentData) throws NxtException.NotValidException {
return new Attachment.ColoredCoinsAssetIssuance(attachmentData);
}
@Override
boolean applyAttachmentUnconfirmed(Transaction transaction, Account senderAccount) {
return true;
}
@Override
void applyAttachment(Transaction transaction, Account senderAccount, Account recipientAccount) {
Attachment.ColoredCoinsAssetIssuance attachment = (Attachment.ColoredCoinsAssetIssuance) transaction.getAttachment();
long assetId = transaction.getId();
Asset.addAsset(transaction, attachment);
senderAccount.addToAssetAndUnconfirmedAssetBalanceQNT(assetId, attachment.getQuantityQNT());
}
@Override
void undoAttachmentUnconfirmed(Transaction transaction, Account senderAccount) {
}
@Override
void validateAttachment(Transaction transaction) throws NxtException.ValidationException {
Attachment.ColoredCoinsAssetIssuance attachment = (Attachment.ColoredCoinsAssetIssuance)transaction.getAttachment();
if (attachment.getName().length() < Constants.MIN_ASSET_NAME_LENGTH
|| attachment.getName().length() > Constants.MAX_ASSET_NAME_LENGTH
|| attachment.getDescription().length() > Constants.MAX_ASSET_DESCRIPTION_LENGTH
|| attachment.getDecimals() < 0 || attachment.getDecimals() > 8
|| attachment.getQuantityQNT() <= 0
|| attachment.getQuantityQNT() > Constants.MAX_ASSET_QUANTITY_QNT
) {
throw new NxtException.NotValidException("Invalid asset issuance: " + attachment.getJSONObject());
}
String normalizedName = attachment.getName().toLowerCase();
for (int i = 0; i < normalizedName.length(); i++) {
if (Constants.ALPHABET.indexOf(normalizedName.charAt(i)) < 0) {
throw new NxtException.NotValidException("Invalid asset name: " + normalizedName);
}
}
}
@Override
public boolean hasRecipient() {
return false;
}
};
public static final TransactionType ASSET_TRANSFER = new ColoredCoins() {
@Override
public final byte getSubtype() {
return TransactionType.SUBTYPE_COLORED_COINS_ASSET_TRANSFER;
}
@Override
Attachment.ColoredCoinsAssetTransfer parseAttachment(ByteBuffer buffer, byte transactionVersion) throws NxtException.NotValidException {
return new Attachment.ColoredCoinsAssetTransfer(buffer, transactionVersion);
}
@Override
Attachment.ColoredCoinsAssetTransfer parseAttachment(JSONObject attachmentData) throws NxtException.NotValidException {
return new Attachment.ColoredCoinsAssetTransfer(attachmentData);
}
@Override
boolean applyAttachmentUnconfirmed(Transaction transaction, Account senderAccount) {
Attachment.ColoredCoinsAssetTransfer attachment = (Attachment.ColoredCoinsAssetTransfer) transaction.getAttachment();
long unconfirmedAssetBalance = senderAccount.getUnconfirmedAssetBalanceQNT(attachment.getAssetId());
if (unconfirmedAssetBalance >= 0 && unconfirmedAssetBalance >= attachment.getQuantityQNT()) {
senderAccount.addToUnconfirmedAssetBalanceQNT(attachment.getAssetId(), -attachment.getQuantityQNT());
return true;
}
return false;
}
@Override
void applyAttachment(Transaction transaction, Account senderAccount, Account recipientAccount) {
Attachment.ColoredCoinsAssetTransfer attachment = (Attachment.ColoredCoinsAssetTransfer) transaction.getAttachment();
senderAccount.addToAssetBalanceQNT(attachment.getAssetId(), -attachment.getQuantityQNT());
recipientAccount.addToAssetAndUnconfirmedAssetBalanceQNT(attachment.getAssetId(), attachment.getQuantityQNT());
AssetTransfer.addAssetTransfer(transaction, attachment);
}
@Override
void undoAttachmentUnconfirmed(Transaction transaction, Account senderAccount) {
Attachment.ColoredCoinsAssetTransfer attachment = (Attachment.ColoredCoinsAssetTransfer) transaction.getAttachment();
senderAccount.addToUnconfirmedAssetBalanceQNT(attachment.getAssetId(), attachment.getQuantityQNT());
}
@Override
void validateAttachment(Transaction transaction) throws NxtException.ValidationException {
Attachment.ColoredCoinsAssetTransfer attachment = (Attachment.ColoredCoinsAssetTransfer)transaction.getAttachment();
if (transaction.getAmountNQT() != 0
|| attachment.getComment() != null && attachment.getComment().length() > Constants.MAX_ASSET_TRANSFER_COMMENT_LENGTH
|| attachment.getAssetId() == 0) {
throw new NxtException.NotValidException("Invalid asset transfer amount or comment: " + attachment.getJSONObject());
}
if (transaction.getVersion() > 0 && attachment.getComment() != null) {
throw new NxtException.NotValidException("Asset transfer comments no longer allowed, use message " +
"or encrypted message appendix instead");
}
Asset asset = Asset.getAsset(attachment.getAssetId());
if (attachment.getQuantityQNT() <= 0 || (asset != null && attachment.getQuantityQNT() > asset.getQuantityQNT())) {
throw new NxtException.NotValidException("Invalid asset transfer asset or quantity: " + attachment.getJSONObject());
}
if (asset == null) {
throw new NxtException.NotCurrentlyValidException("Asset " + Convert.toUnsignedLong(attachment.getAssetId()) +
" does not exist yet");
}
}
@Override
public boolean hasRecipient() {
return true;
}
};
abstract static class ColoredCoinsOrderPlacement extends ColoredCoins {
@Override
final void validateAttachment(Transaction transaction) throws NxtException.ValidationException {
Attachment.ColoredCoinsOrderPlacement attachment = (Attachment.ColoredCoinsOrderPlacement)transaction.getAttachment();
if (attachment.getPriceNQT() <= 0 || attachment.getPriceNQT() > Constants.MAX_BALANCE_NQT
|| attachment.getAssetId() == 0) {
throw new NxtException.NotValidException("Invalid asset order placement: " + attachment.getJSONObject());
}
Asset asset = Asset.getAsset(attachment.getAssetId());
if (attachment.getQuantityQNT() <= 0 || (asset != null && attachment.getQuantityQNT() > asset.getQuantityQNT())) {
throw new NxtException.NotValidException("Invalid asset order placement asset or quantity: " + attachment.getJSONObject());
}
if (asset == null) {
throw new NxtException.NotCurrentlyValidException("Asset " + Convert.toUnsignedLong(attachment.getAssetId()) +
" does not exist yet");
}
}
@Override
final public boolean hasRecipient() {
return false;
}
}
public static final TransactionType ASK_ORDER_PLACEMENT = new ColoredCoins.ColoredCoinsOrderPlacement() {
@Override
public final byte getSubtype() {
return TransactionType.SUBTYPE_COLORED_COINS_ASK_ORDER_PLACEMENT;
}
@Override
Attachment.ColoredCoinsAskOrderPlacement parseAttachment(ByteBuffer buffer, byte transactionVersion) throws NxtException.NotValidException {
return new Attachment.ColoredCoinsAskOrderPlacement(buffer, transactionVersion);
}
@Override
Attachment.ColoredCoinsAskOrderPlacement parseAttachment(JSONObject attachmentData) throws NxtException.NotValidException {
return new Attachment.ColoredCoinsAskOrderPlacement(attachmentData);
}
@Override
boolean applyAttachmentUnconfirmed(Transaction transaction, Account senderAccount) {
Attachment.ColoredCoinsAskOrderPlacement attachment = (Attachment.ColoredCoinsAskOrderPlacement) transaction.getAttachment();
long unconfirmedAssetBalance = senderAccount.getUnconfirmedAssetBalanceQNT(attachment.getAssetId());
if (unconfirmedAssetBalance >= 0 && unconfirmedAssetBalance >= attachment.getQuantityQNT()) {
senderAccount.addToUnconfirmedAssetBalanceQNT(attachment.getAssetId(), -attachment.getQuantityQNT());
return true;
}
return false;
}
@Override
void applyAttachment(Transaction transaction, Account senderAccount, Account recipientAccount) {
Attachment.ColoredCoinsAskOrderPlacement attachment = (Attachment.ColoredCoinsAskOrderPlacement) transaction.getAttachment();
if (Asset.getAsset(attachment.getAssetId()) != null) {
Order.Ask.addOrder(transaction, attachment);
}
}
@Override
void undoAttachmentUnconfirmed(Transaction transaction, Account senderAccount) {
Attachment.ColoredCoinsAskOrderPlacement attachment = (Attachment.ColoredCoinsAskOrderPlacement) transaction.getAttachment();