-
Notifications
You must be signed in to change notification settings - Fork 23
/
transaction.spec.ts
1347 lines (1184 loc) · 54.6 KB
/
transaction.spec.ts
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
import * as ByteBuffer from 'bytebuffer';
import * as chai from 'chai';
import * as chaiAsPromised from 'chai-as-promised';
import * as crypto from 'crypto';
import { Container } from 'inversify';
import * as sinon from 'sinon';
import { SinonSandbox, SinonStub } from 'sinon';
import { BigNum, IKeypair, TransactionType } from '../../../src/helpers';
import { Symbols } from '../../../src/ioc/symbols';
import { SignedAndChainedBlockType, TransactionLogic } from '../../../src/logic';
import { IConfirmedTransaction, SendTransaction, VoteTransaction } from '../../../src/logic/transactions';
import {
AccountLogicStub,
EdStub,
ExceptionsManagerStub,
LoggerStub,
RoundsLogicStub,
SlotsStub,
ZSchemaStub
} from '../../stubs';
import { createContainer } from '../../utils/containerCreator';
import { TransactionsModel } from '../../../src/models';
import { VerificationType } from '../../../src/ioc/interfaces/logic';
import { DBBulkCreateOp } from '../../../src/types/genericTypes';
import { createRandomTransactions, toBufferedTransaction } from '../../utils/txCrafter';
import { z_schema } from '../../../src/helpers/z_schema';
chai.use(chaiAsPromised);
// tslint:disable-next-line no-var-requires
const expect = chai.expect;
// tslint:disable no-unused-expression
describe('logic/transaction', () => {
let instance: TransactionLogic;
let container: Container;
let edStub: EdStub;
let slotsStub: SlotsStub;
let zSchemaStub: ZSchemaStub;
let accountLogicStub: AccountLogicStub;
let roundsLogicStub: RoundsLogicStub;
let loggerStub: LoggerStub;
let excManagerStub: ExceptionsManagerStub;
let sendTransaction: SendTransaction;
let sandbox: SinonSandbox;
let sender: any;
let requester: any;
let block: any;
let genesisBlock: SignedAndChainedBlockType;
let txModel: typeof TransactionsModel;
let tx: IConfirmedTransaction<any>;
const sampleBuffer = Buffer.from('35526f8a1e2f482264e5d4982fc07e73f4ab9f4794b110ceefecd8f880d51892', 'hex');
const publicKeyHex = '6588716f9c941530c74eabdf0b27b1a2bac0a1525e9605a37e6c0b3817e58fe3';
const privateKeyHex = 'cd25f48e0bf2c9ac3c9fe67f22fea54bb108f694bb69eb10520c48b228635df0' +
'6588716f9c941530c74eabdf0b27b1a2bac0a1525e9605a37e6c0b3817e58fe3';
const keyPair: IKeypair = {
privateKey: Buffer.from(privateKeyHex, 'hex'),
publicKey : Buffer.from(publicKeyHex, 'hex'),
};
beforeEach(() => {
container = createContainer();
container.rebind(Symbols.logic.transaction).to(TransactionLogic).inSingletonScope();
instance = container.get(Symbols.logic.transaction);
edStub = container.get(Symbols.helpers.ed);
slotsStub = container.get(Symbols.helpers.slots);
zSchemaStub = container.get(Symbols.generic.zschema);
accountLogicStub = container.get(Symbols.logic.account);
roundsLogicStub = container.get(Symbols.logic.rounds);
loggerStub = container.get(Symbols.helpers.logger);
genesisBlock = container.get(Symbols.generic.genesisBlock);
excManagerStub = container.get(Symbols.helpers.exceptionsManager);
txModel = container.get(Symbols.models.transactions);
sendTransaction = new SendTransaction();
instance.attachAssetType(sendTransaction);
tx = {
amount : 108910891000000,
asset : {},
fee : 10,
id : '8139741256612355994',
recipientId : '15256762582730568272R',
senderId : '1233456789012345R',
senderPublicKey: Buffer.from('6588716f9c941530c74eabdf0b27b1a2bac0a1525e9605a37e6c0b3817e58fe3', 'hex'),
signature : Buffer.from('f8fbf9b8433bf1bbea971dc8b14c6772d33c7dd285d84c5e6c984b10c4141e9f' +
'a56ace902b910e05e98b55898d982b3d5b9bf8bd897083a7d1ca1d5028703e03', 'hex'),
timestamp : 0,
height : 10,
blockId : '11',
type : TransactionType.SEND,
};
sender = {
address : '1233456789012345R',
balance : 10000000,
publicKey: Buffer.from('6588716f9c941530c74eabdf0b27b1a2bac0a1525e9605a37e6c0b3817e58fe3', 'hex')
};
requester = {
address : '9999999999999999R',
balance : 20000000,
publicKey: Buffer.from('e73f4ab9f4794b110ceefecd8f880d5189235526f8a1e2f482264e5d4982fc07', 'hex'),
};
block = {
blockSignature : Buffer.from('a9e12f49432364c1e171dd1f9e2d34f8baffdda0f4ef0989d9439e5d46aba55ef640' +
'149ff7ed7d3e3d4e1ad4173ecfd1cc0384f5598175c15434e0d97ce4150d', 'hex'),
generatorPublicKey : Buffer.from('35526f8a1e2f482264e5d4982fc07e73f4ab9f4794b110ceefecd8f880d51892', 'hex'),
height : 1,
id : '13191140260435645922',
numberOfTransactions: 0,
payloadHash : Buffer.from('cd8171332c012514864edd8eb6f68fc3ea6cb2afbaf21c56e12751022684cea5', 'hex'),
payloadLength : 0,
previousBlock : null,
timestamp : 0,
totalAmount : 10999999991000000,
totalFee : 0,
transactions : [],
version : 0,
};
edStub.stubs.sign.returns(sampleBuffer);
sandbox = sinon.createSandbox();
});
afterEach(() => {
sandbox.restore();
});
describe('attachAssetType', () => {
it('should throw an error if invalid object is passed', () => {
expect(() => {
instance.attachAssetType({} as any);
}).to.throw('Invalid instance interface');
});
it('should add the instance to the types array and return instance', () => {
// We need a valid object to avoid the error, easy to instantiate real code.
const txType = new SendTransaction();
instance.attachAssetType(txType);
expect((instance as any).types[txType.type]).to.be.deep.equal(txType);
});
});
describe('sign', () => {
it('should call getHash', () => {
const getHashSpy = sandbox.spy(instance, 'getHash');
instance.sign(keyPair, tx);
expect(getHashSpy.calledOnce).to.be.true;
expect(getHashSpy.firstCall.args.length).to.be.equal(1);
expect(getHashSpy.firstCall.args[0]).to.be.deep.equal(tx);
});
it('should call ed.sign', () => {
const getHashSpy = sandbox.spy(instance, 'getHash');
instance.sign(keyPair, tx);
expect(edStub.stubs.sign.calledOnce).to.be.true;
expect(edStub.stubs.sign.firstCall.args.length).to.be.equal(2);
expect(edStub.stubs.sign.firstCall.args[0]).to.be.deep.equal(getHashSpy.firstCall.returnValue);
expect(edStub.stubs.sign.firstCall.args[1]).to.be.deep.equal(keyPair);
});
it('should return a hex string', () => {
const toRet = instance.sign(keyPair, tx);
expect(toRet).to.match(/^[0-9a-f]+$/);
});
});
describe('multiSign', () => {
it('should call getHash', () => {
const getHashSpy = sandbox.spy(instance, 'getHash');
instance.multiSign(keyPair, tx);
expect(getHashSpy.calledOnce).to.be.true;
expect(getHashSpy.firstCall.args.length).to.be.equal(3);
expect(getHashSpy.firstCall.args[0]).to.be.deep.equal(tx);
expect(getHashSpy.firstCall.args[1]).to.be.true;
expect(getHashSpy.firstCall.args[2]).to.be.true;
});
it('should call ed.sign', () => {
const getHashSpy = sandbox.spy(instance, 'getHash');
instance.multiSign(keyPair, tx);
expect(edStub.stubs.sign.calledOnce).to.be.true;
expect(edStub.stubs.sign.firstCall.args.length).to.be.equal(2);
expect(edStub.stubs.sign.firstCall.args[0]).to.be.deep.equal(getHashSpy.firstCall.returnValue);
expect(edStub.stubs.sign.firstCall.args[1]).to.be.deep.equal(keyPair);
});
it('should return a hex string', () => {
const toRet = instance.multiSign(keyPair, tx);
expect(toRet).to.match(/[0-9a-f]+/);
});
});
describe('getId', () => {
it('should call getHash', () => {
const getHashSpy = sandbox.spy(instance, 'getHash');
instance.getId(tx);
expect(getHashSpy.calledOnce).to.be.true;
expect(getHashSpy.firstCall.args[0]).to.be.deep.equal(tx);
});
it('should call BigNum.fromBuffer', () => {
const fromBufferSpy = sandbox.spy(BigNum, 'fromBuffer');
instance.getId(tx);
expect(fromBufferSpy.calledOnce).to.be.true;
});
it('should return a string', () => {
const retVal = instance.getId(tx);
expect(retVal).to.be.a('string');
expect(retVal).to.match(/^[0-9]+$/);
});
});
describe('getHash', () => {
it('should call crypto.createHash', () => {
const createHashSpy = sandbox.spy(crypto, 'createHash');
instance.getHash(tx);
expect(createHashSpy.calledOnce).to.be.true;
expect(createHashSpy.firstCall.args.length).to.be.equal(1);
expect(createHashSpy.firstCall.args[0]).to.be.equal('sha256');
});
it('should call this.getBytes', () => {
const getBytesSpy = sandbox.spy(instance, 'getBytes');
instance.getHash(tx, true, false);
expect(getBytesSpy.calledOnce).to.be.true;
expect(getBytesSpy.firstCall.args[0]).to.be.deep.equal(tx);
expect(getBytesSpy.firstCall.args[1]).to.be.deep.equal(true);
expect(getBytesSpy.firstCall.args[2]).to.be.deep.equal(false);
});
it('should return a Buffer', () => {
const retVal = instance.getHash(tx);
expect(retVal).to.be.instanceOf(Buffer);
});
});
describe('getBytes', () => {
let sequence: any[];
let lastBB: any;
const toRestore = {} as any;
before(() => {
toRestore.writeByte = ByteBuffer.prototype.writeByte;
toRestore.writeInt = ByteBuffer.prototype.writeInt;
toRestore.writeLong = (ByteBuffer.prototype as any).writeLong;
toRestore.flip = ByteBuffer.prototype.flip;
});
beforeEach(() => {
sequence = [];
(ByteBuffer.prototype as any).writeByte = function(b) {
sequence.push(b);
lastBB = this;
};
(ByteBuffer.prototype as any).writeInt = (b) => sequence.push(b);
(ByteBuffer.prototype as any).writeLong = (b) => sequence.push(b);
ByteBuffer.prototype.flip = sandbox.stub();
});
after(() => {
(ByteBuffer.prototype as any).writeByte = toRestore.writeByte;
(ByteBuffer.prototype as any).writeInt = toRestore.writeInt;
(ByteBuffer.prototype as any).writeLong = toRestore.writeLong;
ByteBuffer.prototype.flip = toRestore.flip;
});
it('should throw an error if wrong tx type', () => {
// Only tx type 0 is registered
tx.type = 999;
expect(() => {
instance.getBytes(tx);
}).to.throw('Unknown transaction type 999');
});
it('should call getBytes from the right txType', () => {
const getBytesSpy = sandbox.spy(sendTransaction, 'getBytes');
instance.getBytes(tx, true, false);
expect(getBytesSpy.calledOnce).to.be.true;
expect(getBytesSpy.firstCall.args[0]).to.be.deep.equal(tx);
expect(getBytesSpy.firstCall.args[1]).to.be.deep.equal(true);
expect(getBytesSpy.firstCall.args[2]).to.be.deep.equal(false);
});
it('should create a ByteBuffer of the right length', () => {
instance.getBytes(tx, true, false);
expect(lastBB.capacity()).to.be.equal(213);
});
it('should add the type as first byte of the ByteBuffer', () => {
instance.getBytes(tx, true, false);
expect(sequence[0]).to.be.equal(tx.type);
});
it('should add the timestamp to the ByteBuffer via writeInt', () => {
tx.timestamp = Date.now();
instance.getBytes(tx, true, false);
expect(sequence[1]).to.be.equal(tx.timestamp);
});
it('should add the senderPublicKey to the ByteBuffer', () => {
const senderPublicKeyBuffer = tx.senderPublicKey;
instance.getBytes(tx);
for (let i = 0; i < senderPublicKeyBuffer.length; i++) {
expect(sequence[2 + i]).to.be.equal(senderPublicKeyBuffer[i]);
}
});
it('should add the requesterPublicKey to the ByteBuffer if tx.requesterPublicKey', () => {
tx.requesterPublicKey = Buffer.from('35526f8a1e2f482264e5d4982fc07e73f4ab9f4794b110ceefecd8f880d51899', 'hex');
instance.getBytes(tx);
for (let i = 0; i < tx.requesterPublicKey.length; i++) {
// We always get here after 34 writes to the ByteBuffer
expect(sequence[34 + i]).to.be.equal(tx.requesterPublicKey[i]);
}
});
it('should add the recipientId to the ByteBuffer if tx.recipientId', () => {
tx.recipientId = '123123123123123R';
const recipient = tx.recipientId.slice(0, -1);
const recBuf = new BigNum(recipient).toBuffer({ size: 8 });
instance.getBytes(tx);
for (let i = 0; i < recBuf.length; i++) {
expect(sequence[34 + i]).to.be.equal(recBuf[i]);
}
});
it('should add 8 zeroes to the ByteBuffer if NOT tx.recipientId', () => {
tx.recipientId = undefined;
instance.getBytes(tx);
for (let i = 34; i < 42; i++) {
expect(sequence[i]).to.be.equal(0);
}
});
it('should add the amount to the ByteBuffer via writeLong', () => {
instance.getBytes(tx);
expect(sequence[42]).to.be.equal(tx.amount);
});
it('should add the asset bytes to the ByteBuffer if not empty', () => {
tx.signSignature = Buffer.from('');
const getBytesStub = sinon.stub(sendTransaction, 'getBytes');
getBytesStub.returns(sampleBuffer);
instance.getBytes(tx);
for (let i = 0; i < sampleBuffer.length; i++) {
expect(sequence[43 + i]).to.be.equal(sampleBuffer[i]);
}
getBytesStub.restore();
});
it('should add the signature to the ByteBuffer', () => {
instance.getBytes(tx);
for (let i = 0; i < tx.signature.length; i++) {
// tx.asset is empty so we start from 43
expect(sequence[43 + i]).to.be.equal(tx.signature[i]);
}
});
it('should NOT add the signature to the ByteBuffer if skipSignature', () => {
instance.getBytes(tx, true, true);
for (let i = 0; i < tx.signature.length; i++) {
// tx.asset is empty so we start from 43
expect(sequence[43 + i]).to.be.equal(undefined);
}
});
it('should add the signSignature to the ByteBuffer', () => {
tx.signSignature = Buffer.from('93e49ce591472c5c587ff419c02c80e78159a82e0143f87c51dec43a2613cbd9' +
'93e49ce591472c5c587ff419c02c80e78159a82e0143f87c51dec43a2613cbd9', 'hex');
instance.getBytes(tx);
for (let i = 0; i < tx.signSignature.length; i++) {
// tx.asset is empty so we start from 43
expect(sequence[107 + i]).to.be.equal(tx.signSignature[i]);
}
});
it('should NOT add the signSignature to the ByteBuffer if skipSecondSignature', () => {
tx.signSignature = Buffer.from('93e49ce591472c5c587ff419c02c80e78159a82e0143f87c51dec43a2613cbd9' +
'93e49ce591472c5c587ff419c02c80e78159a82e0143f87c51dec43a2613cbd9', 'hex');
instance.getBytes(tx, false, true);
for (let i = 0; i < tx.signSignature.length; i++) {
// tx.asset is empty so we start from 43
expect(sequence[107 + i]).to.be.equal(undefined);
}
});
it('should flip the ByteBuffer', () => {
instance.getBytes(tx, true, true);
expect(lastBB.flip.calledOnce).to.be.true;
});
it('should return a Buffer', () => {
const retVal = instance.getBytes(tx, true, true);
expect(retVal).to.be.instanceOf(Buffer);
});
});
describe('ready', () => {
it('should call assertKnownTransactionType', () => {
const akttStub = sandbox.stub(instance, 'assertKnownTransactionType');
instance.ready(tx, {} as any);
expect(akttStub.calledOnce).to.be.true;
});
it('should return false if !sender', () => {
const retVal = instance.ready(tx, undefined);
expect(retVal).to.be.false;
});
it('should call txType.ready and return', () => {
const txTypeReadyStub = sandbox.stub(sendTransaction, 'ready').returns('OK');
const retVal = instance.ready(tx, {} as any);
expect(txTypeReadyStub.calledOnce).to.be.true;
expect(retVal).to.be.eq('OK');
});
});
describe('assertKnownTransactionType', () => {
it('should throw if invalid txtype', () => {
tx.type = 999999;
expect(() => {
instance.assertKnownTransactionType(tx.type);
}).to.throw(/Unknown transaction type/);
});
it('should not throw if OK', () => {
expect(() => {
instance.assertKnownTransactionType(tx.type);
}).not.to.throw();
});
});
describe('checkBalance', () => {
it('should return error:null if OK', () => {
const retVal = instance.checkBalance(1000, 'balance', tx, sender as any);
expect(retVal.error).to.be.eq(null);
});
it('should return error if balance exceeded', () => {
// Pass an amount greater than sender balance.
const retVal = instance.checkBalance(sender.balance + 1, 'balance', tx, sender as any);
expect(retVal.error).to.match(/Account does not have enough currency/);
});
});
describe('verify', () => {
let verifySignatureStub: SinonStub;
let checkBalanceStub: SinonStub;
let calculateFeeStub: SinonStub;
let txTypeVerifyStub: SinonStub;
let instGetIdStub;
beforeEach(() => {
(tx as any).blockId = '12345ab';
// instance stubs
verifySignatureStub = sandbox.stub(instance, 'verifySignature').returns(true);
checkBalanceStub = sandbox.stub(instance, 'checkBalance').returns({ exceeded: false });
// txType stubs
calculateFeeStub = sandbox.stub(sendTransaction, 'calculateFee').returns(tx.fee);
txTypeVerifyStub = sandbox.stub(sendTransaction, 'verify').resolves();
sender.isMultisignature = () => false;
instGetIdStub = sandbox.stub(instance, 'getId').returns(tx.id);
});
it('should throw if the tx.id is wrong', async () => {
instGetIdStub.returns('10');
await expect(instance.verify(tx, sender, {} as any, null))
.to.be.rejectedWith('Invalid transaction id');
});
it('should call assertKnownTransactionType', async () => {
// we want it to throw immediately
tx.type = 99999;
const akttStub = sandbox.stub(instance, 'assertKnownTransactionType').throws('stop');
try {
await instance.verify(tx, sender, requester, 1);
} catch (e) {
expect(akttStub.calledOnce).to.be.true;
}
});
it('should throw if Missing sender', async () => {
await expect(instance.verify(tx, undefined, requester, 1)).to.be.rejectedWith('Missing sender');
});
it('should throw if sender second signature + multisig and no signSignature in tx', async () => {
tx.requesterPublicKey = requester.publicKey;
sender.multisignatures = [];
sender.isMultisignature = () => true;
sender.secondSignature = 'signature';
delete tx.signSignature;
await expect(instance.verify(tx, sender, requester, 1)).to.be.rejectedWith('Missing sender second signature');
});
it('should throw if second signature provided and sender has none enabled', async () => {
delete tx.requesterPublicKey;
delete sender.secondSignature;
tx.signSignature = Buffer.from('signSignature');
await expect(instance.verify(tx, sender, requester, 1))
.to.be.rejectedWith('Sender does not have a second signature');
});
it('should throw if missing requester second signature', async () => {
tx.requesterPublicKey = requester.publicKey;
sender.isMultisignature = () => true;
requester.secondSignature = Buffer.from('secondSignature');
sender.multisignatures = [];
delete tx.signSignature;
await expect(instance.verify(tx, sender, requester, 1)).to.be.rejectedWith('Missing requester second signature');
});
it('should throw if second signature provided, and requester has none enabled', async () => {
tx.requesterPublicKey = requester.publicKey;
delete requester.secondSignature;
sender.multisignatures = [];
sender.isMultisignature = () => true;
tx.signSignature = Buffer.from('signSignature');
await expect(instance.verify(tx, sender, requester, 1))
.to.be.rejectedWith('Requester does not have a second signature');
});
it('should throw if sender publicKey and tx.senderPublicKey mismatches', async () => {
tx.senderPublicKey = Buffer.from('anotherPublicKey');
await expect(instance.verify(tx, sender, requester, 1)).to.be.rejectedWith(/Invalid sender public key/);
});
it('should throw if sender is not genesis account unless block id equals genesis', async () => {
sender.publicKey = genesisBlock.generatorPublicKey;
tx.senderPublicKey = sender.publicKey;
(tx as any).blockId = 'anotherBlockId';
await expect(instance.verify(tx, sender, requester, 1))
.to.be.rejectedWith('Invalid sender. Can not send from genesis account');
});
it('should throw if senderId mismatch', async () => {
tx.senderId = sender.address + 'ABC';
await expect(instance.verify(tx, sender, requester, 1)).to.be.rejectedWith('Invalid sender address');
});
it('should throw if invalid member in keysgroup', async () => {
sender.multisignatures = [];
tx.asset.multisignature = {
keysgroup: [
0, // invalid
false, // invalid
'a', // valid
],
};
await expect(instance.verify(tx, sender, requester, 1)).to.be.rejectedWith('Invalid member in keysgroup');
});
it('should verify multisignatures', async () => {
tx.signatures = ['aa', 'bb'];
tx.asset.multisignature = {
keysgroup: [
'+cc',
'+dd',
],
};
sender.isMultisignature = () => true;
verifySignatureStub.returns(true);
verifySignatureStub.onCall(2).returns(false);
await instance.verify(tx, sender, requester, 1);
expect(verifySignatureStub.callCount).to.equal(4);
expect(verifySignatureStub.args[0][0]).to.be.deep.equal(tx);
expect(verifySignatureStub.args[0][1]).to.be.deep.equal(sender.publicKey);
expect(verifySignatureStub.args[0][2]).to.be.equal(tx.signature);
expect(verifySignatureStub.args[1][0]).to.be.deep.equal(tx);
expect(verifySignatureStub.args[1][1]).to.be.deep.equal(Buffer.from('cc', 'hex'));
expect(verifySignatureStub.args[1][2]).to.be.deep.equal(Buffer.from('aa', 'hex'));
expect(verifySignatureStub.args[1][3]).to.be.deep.equal(VerificationType.ALL);
expect(verifySignatureStub.args[3][0]).to.be.deep.equal(tx);
expect(verifySignatureStub.args[3][1]).to.be.deep.equal(Buffer.from('dd', 'hex'));
expect(verifySignatureStub.args[3][2]).to.be.deep.equal(Buffer.from('bb', 'hex'));
expect(verifySignatureStub.args[3][3]).to.be.deep.equal(VerificationType.ALL);
});
it('should throw if account does not belong to multisignature group', async () => {
tx.requesterPublicKey = Buffer.from('bb', 'hex');
// Initializing this as an empty string is the only way to test this behavior
sender.multisignatures = ['aa'];
sender.isMultisignature = () => true;
await expect(instance.verify(tx, sender, requester, 1))
.to.be.rejectedWith('Account does not belong to multisignature group');
});
it('should call verifySignature', async () => {
tx.requesterPublicKey = requester.publicKey;
sender.multisignatures = [tx.requesterPublicKey.toString('hex')];
sender.isMultisignature = () => true;
verifySignatureStub.returns(false);
await expect(instance.verify(tx, sender, requester, 1)).to.be.rejectedWith('Failed to verify signature');
expect(verifySignatureStub.calledOnce).to.be.true;
expect(verifySignatureStub.firstCall.args[0]).to.be.deep.equal(tx);
expect(verifySignatureStub.firstCall.args[1]).to.be.deep.equal(tx.requesterPublicKey);
expect(verifySignatureStub.firstCall.args[2]).to.be.equal(tx.signature);
expect(verifySignatureStub.firstCall.args[3]).to.be.equal(VerificationType.SIGNATURE);
});
it('should call verifySignature with secondPublicKey if sender.secondSignature', async () => {
sender.secondSignature = 'aaaaaaa';
sender.secondPublicKey = 'secondPublicKey';
requester.secondSignature = 'bbbbbbb';
tx.signSignature = sender.secondSignature;
// tx.requesterPublicKey = requester.publicKey;
sender.multisignatures = [];
verifySignatureStub.onCall(0).returns(true);
verifySignatureStub.onCall(1).returns(false);
await expect(instance.verify(tx, sender, requester, 1)).to.be.rejectedWith('Failed to verify second signature');
expect(verifySignatureStub.calledTwice).to.be.true;
expect(verifySignatureStub.secondCall.args[0]).to.be.deep.equal(tx);
expect(verifySignatureStub.secondCall.args[1]).to.be.equal(sender.secondPublicKey);
expect(verifySignatureStub.secondCall.args[2]).to.be.equal(tx.signSignature);
expect(verifySignatureStub.secondCall.args[3]).to.be.equal(VerificationType.SECOND_SIGNATURE);
});
it('should throw if signatures are not unique', async () => {
tx.signatures = ['a', 'a', 'b'];
await expect(instance.verify(tx, sender, requester, 1))
.to.be.rejectedWith('Encountered duplicate signature in transaction');
});
it('should throw if failed to verify multisignature', async () => {
tx.signatures = ['a', 'b'];
// First call is simple validation with requester or sender public key
verifySignatureStub.onCall(0).returns(true);
// Second call is inside tx.signatures loop
verifySignatureStub.onCall(1).returns(false);
await expect(instance.verify(tx, sender, requester, 1))
.to.be.rejectedWith('Failed to verify multisignature');
});
it('should throw Failed to verify multisignature if verifySignature() is fails', async () => {
sender.multisignatures = ['aa'];
sender.isMultisignature = () => true;
tx.signatures = ['a', 'b'];
tx.requesterPublicKey = Buffer.from('aa', 'hex');
tx.asset.multisignature = {
keysgroup: [
'+yz',
'+ef',
],
};
verifySignatureStub.returns(false);
verifySignatureStub.onCall(0).returns(true);
verifySignatureStub.onCall(2).returns(true);
await expect(instance.verify(tx, sender, requester, 1))
.to.be.rejectedWith('Failed to verify multisignature');
});
it('should call verifySignature if tx.signatures not empty', async () => {
tx.signatures = ['aa', 'bb'];
sender.multisignatures = ['cc', 'dd'];
sender.isMultisignature = () => true;
// First call is simple validation with requester or sender publio key
verifySignatureStub.onCall(0).returns(true);
// Second call is inside tx.signatures loop
verifySignatureStub.onCall(1).returns(false);
await instance.verify(tx, sender, requester, 1);
expect(verifySignatureStub.secondCall.args[0]).to.be.deep.equal(tx);
expect(verifySignatureStub.secondCall.args[1]).to.be.deep.equal(Buffer.from(sender.multisignatures[0], 'hex'));
expect(verifySignatureStub.secondCall.args[2]).to.be.deep.equal(Buffer.from(tx.signatures[0], 'hex'));
expect(verifySignatureStub.secondCall.args[3]).to.be.equal(VerificationType.ALL);
});
it('should call txType.calculateFee and throw if fee mismatch', async () => {
// Returned value different from fee in tx (tx.fee is 10)
calculateFeeStub.returns(9);
await expect(instance.verify(tx, sender, requester, 1)).to.be.rejectedWith('Invalid transaction fee');
expect(calculateFeeStub.calledOnce).to.be.true;
expect(calculateFeeStub.firstCall.args[0]).to.be.deep.equal(tx);
expect(calculateFeeStub.firstCall.args[1]).to.be.deep.equal(sender);
expect(calculateFeeStub.firstCall.args[2]).to.be.equal(1);
});
it('should throw if amount is < 0', async () => {
tx.amount = -100;
await expect(instance.verify(tx, sender, requester, 1)).to.be.rejectedWith('Invalid transaction amount');
});
it('should throw if amount is > totalAmout', async () => {
(tx as any).amount = '10999999991000001';
await expect(instance.verify(tx, sender, requester, 1)).to.be.rejectedWith('Invalid transaction amount');
});
it('should throw if amount is decimal', async () => {
tx.amount = 10.1;
await expect(instance.verify(tx, sender, requester, 1)).to.be.rejectedWith('Invalid transaction amount');
});
it('should throw if amount is written in exponential notation', async () => {
(tx as any).amount = '10e3';
await expect(instance.verify(tx, sender, requester, 1)).to.be.rejectedWith('Invalid transaction amount');
});
it('should reject tx if verifySignature throws (for whatever reason', async () => {
verifySignatureStub.throws(new Error('whatever'));
await expect(instance.verify(tx, sender, null, 1))
.to.rejectedWith('whatever');
});
it('should call checkBalance and throw if checkBalance returns an error', async () => {
checkBalanceStub.returns({ exceeded: true, error: 'checkBalance error' });
await expect(instance.verify(tx, sender, requester, 1)).to.be.rejectedWith('checkBalance error');
expect(checkBalanceStub.calledOnce).to.be.true;
expect(checkBalanceStub.firstCall.args[0].toString()).to.be.equal('108910891000010');
expect(checkBalanceStub.firstCall.args[1]).to.be.equal('balance');
expect(checkBalanceStub.firstCall.args[2]).to.be.deep.equal(tx);
expect(checkBalanceStub.firstCall.args[3]).to.be.deep.equal(sender);
});
it('should call slots.getSlotNumber', async () => {
slotsStub.stubs.getSlotNumber.returns(1);
await instance.verify(tx, sender, requester, 1);
expect(slotsStub.stubs.getSlotNumber.calledTwice).to.be.true;
expect(slotsStub.stubs.getSlotNumber.firstCall.args[0]).to.be.equal(tx.timestamp);
});
it('should throw if timestamp is in the future', async () => {
slotsStub.stubs.getSlotNumber.onCall(0).returns(1000000);
slotsStub.stubs.getSlotNumber.onCall(1).returns(10);
await expect(instance.verify(tx, sender, requester, 1))
.to.be.rejectedWith('Invalid transaction timestamp. Timestamp is in the future');
});
it('should await verify from the txType', async () => {
await instance.verify(tx, sender, requester, 1);
expect(txTypeVerifyStub.calledOnce).to.be.true;
expect(txTypeVerifyStub.firstCall.args[0]).to.be.deep.equal(tx);
expect(txTypeVerifyStub.firstCall.args[1]).to.be.deep.equal(sender);
});
it('should reject tx if requesetPublicKey and account is not multisign', async () => {
sender.multisignatures = null;
tx.signatures = ['a', 'b'];
tx.requesterPublicKey = Buffer.from('aa', 'hex');
tx.asset.multisignature = {
keysgroup: [
'+aa',
'+ef',
],
};
verifySignatureStub.returns(true);
await expect(instance.verify(tx, sender, requester, 1))
.to.rejectedWith('Account or requester account is not multisignature');
});
it('should reject tx if requesterPublicKey, account is multisign but requester is null', async () => {
sender.multisignatures = ['a'];
tx.signatures = ['a', 'b'];
tx.requesterPublicKey = Buffer.from('aa', 'hex');
tx.asset.multisignature = {
keysgroup: [
'+aa',
'+ef',
],
};
verifySignatureStub.returns(true);
await expect(instance.verify(tx, sender, null /*requester*/, 1))
.to.rejectedWith('Account or requester account is not multisignature');
});
});
describe('verifySignature', () => {
let akttStub: SinonStub;
let getHashStub: SinonStub;
const theHash = Buffer.from('123abc', 'hex');
beforeEach(() => {
edStub.enqueueResponse('verify', true);
akttStub = sandbox.stub(instance, 'assertKnownTransactionType').returns(true);
getHashStub = sandbox.stub(instance, 'getHash').returns(theHash);
});
it('should call assertKnownTransactionType', () => {
instance.verifySignature(tx, tx.senderPublicKey, tx.signature, VerificationType.ALL);
expect(akttStub.calledOnce).to.be.true;
expect(akttStub.firstCall.args[0]).to.be.deep.equal(tx.type);
});
it('should call ed.verify', () => {
instance.verifySignature(tx, tx.senderPublicKey, tx.signature, VerificationType.ALL);
expect(edStub.stubs.verify.calledOnce).to.be.true;
expect(edStub.stubs.verify.firstCall.args[0]).to.be.deep.equal(theHash);
expect(edStub.stubs.verify.firstCall.args[1]).to.be.deep.equal(tx.signature);
expect(edStub.stubs.verify.firstCall.args[2]).to.be.deep.equal(tx.senderPublicKey);
});
describe('verificationType', () => {
it ('should call getHash with false, false when VerificationType is ALL', () => {
instance.verifySignature(tx, tx.senderPublicKey, tx.signature, VerificationType.ALL);
expect(getHashStub.calledOnce).to.be.true;
expect(getHashStub.firstCall.args[0]).to.be.deep.equal(tx);
expect(getHashStub.firstCall.args[1]).to.be.equal(false);
expect(getHashStub.firstCall.args[2]).to.be.equal(false);
});
it ('should call getHash with false, true when VerificationType is SECOND_SIGNATURE', () => {
instance.verifySignature(tx, tx.senderPublicKey, tx.signature, VerificationType.SECOND_SIGNATURE);
expect(getHashStub.calledOnce).to.be.true;
expect(getHashStub.firstCall.args[0]).to.be.deep.equal(tx);
expect(getHashStub.firstCall.args[1]).to.be.equal(false);
expect(getHashStub.firstCall.args[2]).to.be.equal(true);
});
it ('should call getHash with true, true when VerificationType is SIGNATURE', () => {
instance.verifySignature(tx, tx.senderPublicKey, tx.signature, VerificationType.SIGNATURE);
expect(getHashStub.calledOnce).to.be.true;
expect(getHashStub.firstCall.args[0]).to.be.deep.equal(tx);
expect(getHashStub.firstCall.args[1]).to.be.equal(true);
expect(getHashStub.firstCall.args[2]).to.be.equal(true);
});
});
it('should call false if signature is null', () => {
expect(instance.verifySignature(tx, tx.senderPublicKey, null, VerificationType.ALL)).to.be.false;
});
});
describe('apply', () => {
let readyStub: SinonStub;
let checkBalanceStub: SinonStub;
let txTypeApplyStub: SinonStub;
beforeEach(() => {
// instance stubs
readyStub = sandbox.stub(instance, 'ready').returns(true);
checkBalanceStub = sandbox.stub(instance, 'checkBalance').returns({ exceeded: false });
// dependency stubs
roundsLogicStub.stubs.calcRound.returns(1);
accountLogicStub.stubs.merge.returns([{op1: 'op'}]);
// txType stub
txTypeApplyStub = sandbox.stub(sendTransaction, 'apply').resolves([]);
});
it('should call ready', async () => {
await instance.apply(tx as any, block, sender);
expect(readyStub.calledOnce).to.be.true;
expect(readyStub.firstCall.args[0]).to.be.deep.equal(tx);
expect(readyStub.firstCall.args[1]).to.be.deep.equal(sender);
});
it('should throw if not ready', async () => {
readyStub.returns(false);
await expect(instance.apply(tx as any, block, sender)).to.be.rejectedWith('Transaction is not ready');
});
it('should call checkBalance', async () => {
await instance.apply(tx as any, block, sender);
expect(checkBalanceStub.calledOnce).to.be.true;
const expectedAmount = new BigNum(tx.amount.toString()).plus(tx.fee.toString());
expect(checkBalanceStub.firstCall.args[0]).to.be.deep.equal(expectedAmount);
expect(checkBalanceStub.firstCall.args[1]).to.be.equal('balance');
expect(checkBalanceStub.firstCall.args[2]).to.be.deep.equal(tx);
expect(checkBalanceStub.firstCall.args[3]).to.be.deep.equal(sender);
});
it('should throw if checkBalance returns an error', async () => {
checkBalanceStub.returns({ exceeded: true, error: 'checkBalance error' });
await expect(instance.apply(tx as any, block, sender)).to.be.rejectedWith('checkBalance error');
});
it('should call logger.trace', async () => {
await instance.apply(tx as any, block, sender);
expect(loggerStub.stubs.trace.calledOnce).to.be.true;
});
it('should call accountLogic.merge', async () => {
await instance.apply(tx as any, block, sender);
expect(accountLogicStub.stubs.merge.calledOnce).to.be.true;
expect(accountLogicStub.stubs.merge.firstCall.args[0]).to.be.equal(sender.address);
expect(accountLogicStub.stubs.merge.firstCall.args[1]).to.be.deep.equal({
balance: -108910891000010,
blockId: '13191140260435645922',
round : 1,
});
});
it('should call roundsLogic.calcRound', async () => {
await instance.apply(tx as any, block, sender);
expect(roundsLogicStub.stubs.calcRound.called).to.be.true;
expect(roundsLogicStub.stubs.calcRound.firstCall.args[0]).to.be.equal(block.height);
});
it('should call apply from the txType', async () => {
await instance.apply(tx as any, block, sender);
expect(txTypeApplyStub.calledOnce).to.be.true;
expect(txTypeApplyStub.firstCall.args[0]).to.be.deep.equal(tx);
expect(txTypeApplyStub.firstCall.args[1]).to.be.deep.equal(block);
expect(txTypeApplyStub.firstCall.args[2]).to.be.deep.equal(sender);
});
});
describe('undo', () => {
let txTypeUndoStub: SinonStub;
beforeEach(() => {
// dependency stubs
roundsLogicStub.stubs.calcRound.returns(1);
accountLogicStub.stubs.merge.returns([]);
// txType stub
txTypeUndoStub = sandbox.stub(sendTransaction, 'undo').resolves([]);
});
it('should call logger.trace', async () => {
await instance.undo(tx as any, block, sender);
expect(loggerStub.stubs.trace.calledOnce).to.be.true;
});
it('should call accountLogic.merge', async () => {
await instance.undo(tx as any, block, sender);
expect(accountLogicStub.stubs.merge.calledOnce).to.be.true;
expect(accountLogicStub.stubs.merge.firstCall.args[0]).to.be.equal(sender.address);
expect(accountLogicStub.stubs.merge.firstCall.args[1]).to.be.deep.equal({
balance: 108910891000010,
blockId: '13191140260435645922',
round : 1,
});
});
it('should call roundsLogic.calcRound', async () => {
await instance.undo(tx as any, block, sender);
expect(roundsLogicStub.stubs.calcRound.called).to.be.true;
expect(roundsLogicStub.stubs.calcRound.firstCall.args[0]).to.be.equal(block.height);
});
it('should call undo from the txType', async () => {
await instance.undo(tx as any, block, sender);
expect(txTypeUndoStub.calledOnce).to.be.true;
expect(txTypeUndoStub.firstCall.args[0]).to.be.deep.equal(tx);
expect(txTypeUndoStub.firstCall.args[1]).to.be.deep.equal(block);
expect(txTypeUndoStub.firstCall.args[2]).to.be.deep.equal(sender);
});
});
describe('applyUnconfirmed', () => {
let checkBalanceStub: SinonStub;
let txTypeApplyUnconfirmedStub: SinonStub;
beforeEach(() => {
// instance stubs
checkBalanceStub = sandbox.stub(instance, 'checkBalance').returns({ exceeded: false });
// dependency stubs
roundsLogicStub.stubs.calcRound.returns(1);
accountLogicStub.stubs.merge.returns([]);
// txType stub
txTypeApplyUnconfirmedStub = sandbox.stub(sendTransaction, 'applyUnconfirmed').resolves([]);
});
it('should call checkBalance', async () => {
await instance.applyUnconfirmed(tx as any, sender, requester);
expect(checkBalanceStub.calledOnce).to.be.true;
const expectedAmount = new BigNum(tx.amount.toString()).plus(tx.fee.toString());
expect(checkBalanceStub.firstCall.args[0]).to.be.deep.equal(expectedAmount);
expect(checkBalanceStub.firstCall.args[1]).to.be.equal('u_balance');
expect(checkBalanceStub.firstCall.args[2]).to.be.deep.equal(tx);
expect(checkBalanceStub.firstCall.args[3]).to.be.deep.equal(sender);
});
it('should throw if checkBalance returns an error', async () => {
checkBalanceStub.returns({ exceeded: true, error: 'checkBalance error' });
await expect(instance.applyUnconfirmed(tx as any, block, sender)).to.be.rejectedWith('checkBalance error');
});
it('should call accountLogic.merge', async () => {
await instance.applyUnconfirmed(tx as any, sender, requester);
expect(accountLogicStub.stubs.merge.calledOnce).to.be.true;
expect(accountLogicStub.stubs.merge.firstCall.args[0]).to.be.equal(sender.address);
expect(accountLogicStub.stubs.merge.firstCall.args[1]).to.be.deep.equal({
u_balance: -108910891000010,
});
});
it('should call applyUnconfirmed from the txTypes', async () => {
await instance.applyUnconfirmed(tx as any, sender, requester);
expect(txTypeApplyUnconfirmedStub.calledOnce).to.be.true;
expect(txTypeApplyUnconfirmedStub.firstCall.args[0]).to.be.deep.equal(tx);
expect(txTypeApplyUnconfirmedStub.firstCall.args[1]).to.be.deep.equal(sender);
});
});
describe('undoUnconfirmed', () => {
let txTypeUndoUnconfirmedStub: SinonStub;
beforeEach(() => {
// dependency stubs
roundsLogicStub.stubs.calcRound.returns(1);
accountLogicStub.stubs.merge.returns([]);