@@ -81,7 +81,12 @@ describe("VoteTransaction", () => {
81
81
let unvoteTransaction : Interfaces . ITransaction ;
82
82
let secondSignatureUnvoteTransaction : Interfaces . ITransaction ;
83
83
let multiSignatureUnvoteTransaction : Interfaces . ITransaction ;
84
- let delegateWallet : Wallets . Wallet ;
84
+ let voteUnvoteTransaction : Interfaces . ITransaction ;
85
+ let unvoteVoteTransaction : Interfaces . ITransaction ;
86
+ let voteVoteTransaction : Interfaces . ITransaction ;
87
+ let unvoteUnvoteTransaction : Interfaces . ITransaction ;
88
+ let delegateWallet1 : Wallets . Wallet ;
89
+ let delegateWallet2 : Wallets . Wallet ;
85
90
let handler : TransactionHandler ;
86
91
87
92
beforeEach ( async ( ) => {
@@ -93,61 +98,93 @@ describe("VoteTransaction", () => {
93
98
2 ,
94
99
) ;
95
100
96
- delegateWallet = factoryBuilder
101
+ delegateWallet1 = factoryBuilder
97
102
. get ( "Wallet" )
98
103
. withOptions ( {
99
104
passphrase : passphrases [ 8 ] ,
100
105
nonce : 0 ,
101
106
} )
102
107
. make ( ) ;
108
+ delegateWallet1 . setAttribute ( "delegate" , { username : "test1" } ) ;
109
+ walletRepository . index ( delegateWallet1 ) ;
103
110
104
- delegateWallet . setAttribute ( "delegate" , { username : "test" } ) ;
105
-
106
- walletRepository . index ( delegateWallet ) ;
111
+ delegateWallet2 = factoryBuilder
112
+ . get ( "Wallet" )
113
+ . withOptions ( {
114
+ passphrase : passphrases [ 9 ] ,
115
+ nonce : 0 ,
116
+ } )
117
+ . make ( ) ;
118
+ delegateWallet2 . setAttribute ( "delegate" , { username : "test2" } ) ;
119
+ walletRepository . index ( delegateWallet2 ) ;
107
120
108
121
voteTransaction = BuilderFactory . vote ( )
109
- . votesAsset ( [ "+" + delegateWallet . publicKey ! ] )
122
+ . votesAsset ( [ "+" + delegateWallet1 . publicKey ! ] )
110
123
. nonce ( "1" )
111
124
. sign ( passphrases [ 0 ] )
112
125
. build ( ) ;
113
126
114
127
secondSignatureVoteTransaction = BuilderFactory . vote ( )
115
- . votesAsset ( [ "+" + delegateWallet . publicKey ! ] )
128
+ . votesAsset ( [ "+" + delegateWallet1 . publicKey ! ] )
116
129
. nonce ( "1" )
117
130
. sign ( passphrases [ 1 ] )
118
131
. secondSign ( passphrases [ 2 ] )
119
132
. build ( ) ;
120
133
121
134
multiSignatureVoteTransaction = BuilderFactory . vote ( )
122
135
. senderPublicKey ( multiSignatureWallet . publicKey ! )
123
- . votesAsset ( [ "+" + delegateWallet . publicKey ! ] )
136
+ . votesAsset ( [ "+" + delegateWallet1 . publicKey ! ] )
124
137
. nonce ( "1" )
125
138
. multiSign ( passphrases [ 0 ] , 0 )
126
139
. multiSign ( passphrases [ 1 ] , 1 )
127
140
. multiSign ( passphrases [ 2 ] , 2 )
128
141
. build ( ) ;
129
142
130
143
unvoteTransaction = BuilderFactory . vote ( )
131
- . votesAsset ( [ "-" + delegateWallet . publicKey ! ] )
144
+ . votesAsset ( [ "-" + delegateWallet1 . publicKey ! ] )
132
145
. nonce ( "1" )
133
146
. sign ( passphrases [ 0 ] )
134
147
. build ( ) ;
135
148
136
149
secondSignatureUnvoteTransaction = BuilderFactory . vote ( )
137
- . votesAsset ( [ "-" + delegateWallet . publicKey ! ] )
150
+ . votesAsset ( [ "-" + delegateWallet1 . publicKey ! ] )
138
151
. nonce ( "1" )
139
152
. sign ( passphrases [ 1 ] )
140
153
. secondSign ( passphrases [ 2 ] )
141
154
. build ( ) ;
142
155
143
156
multiSignatureUnvoteTransaction = BuilderFactory . vote ( )
144
157
. senderPublicKey ( multiSignatureWallet . publicKey ! )
145
- . votesAsset ( [ "-" + delegateWallet . publicKey ! ] )
158
+ . votesAsset ( [ "-" + delegateWallet1 . publicKey ! ] )
146
159
. nonce ( "1" )
147
160
. multiSign ( passphrases [ 0 ] , 0 )
148
161
. multiSign ( passphrases [ 1 ] , 1 )
149
162
. multiSign ( passphrases [ 2 ] , 2 )
150
163
. build ( ) ;
164
+
165
+ voteUnvoteTransaction = BuilderFactory . vote ( )
166
+ . votesAsset ( [ "+" + delegateWallet1 . publicKey ! , "-" + delegateWallet1 . publicKey ! ] )
167
+ . nonce ( "1" )
168
+ . sign ( passphrases [ 0 ] )
169
+ . build ( ) ;
170
+
171
+ unvoteVoteTransaction = BuilderFactory . vote ( )
172
+ . votesAsset ( [ "-" + delegateWallet1 . publicKey ! , "+" + delegateWallet2 . publicKey ! ] )
173
+ . nonce ( "1" )
174
+ . sign ( passphrases [ 0 ] )
175
+ . build ( ) ;
176
+
177
+ voteVoteTransaction = BuilderFactory . vote ( )
178
+ . votesAsset ( [ "+" + delegateWallet1 . publicKey ! , "+" + delegateWallet2 . publicKey ! ] )
179
+ . nonce ( "1" )
180
+ . sign ( passphrases [ 0 ] )
181
+ . build ( ) ;
182
+
183
+ unvoteUnvoteTransaction = BuilderFactory . vote ( )
184
+ . votesAsset ( [ "-" + delegateWallet1 . publicKey ! , "-" + delegateWallet2 . publicKey ! ] )
185
+ . nonce ( "1" )
186
+ . sign ( passphrases [ 0 ] )
187
+ . build ( ) ;
151
188
} ) ;
152
189
153
190
describe ( "bootstrap" , ( ) => {
@@ -169,7 +206,7 @@ describe("VoteTransaction", () => {
169
206
transactionHistoryService . streamByCriteria . mockImplementationOnce ( async function * ( ) {
170
207
yield voteTransaction . data ;
171
208
} ) ;
172
- senderWallet . setAttribute ( "vote" , delegateWallet . publicKey ) ;
209
+ senderWallet . setAttribute ( "vote" , delegateWallet1 . publicKey ) ;
173
210
await expect ( handler . bootstrap ( ) ) . rejects . toThrow ( AlreadyVotedError ) ;
174
211
} ) ;
175
212
@@ -258,34 +295,34 @@ describe("VoteTransaction", () => {
258
295
} ) ;
259
296
260
297
it ( "should not throw if the unvote is valid and the wallet has voted" , async ( ) => {
261
- senderWallet . setAttribute ( "vote" , delegateWallet . publicKey ) ;
298
+ senderWallet . setAttribute ( "vote" , delegateWallet1 . publicKey ) ;
262
299
await expect ( handler . throwIfCannotBeApplied ( unvoteTransaction , senderWallet ) ) . toResolve ( ) ;
263
300
} ) ;
264
301
265
302
it ( "should not throw - second sign unvote" , async ( ) => {
266
- secondSignatureWallet . setAttribute ( "vote" , delegateWallet . publicKey ) ;
303
+ secondSignatureWallet . setAttribute ( "vote" , delegateWallet1 . publicKey ) ;
267
304
await expect (
268
305
handler . throwIfCannotBeApplied ( secondSignatureUnvoteTransaction , secondSignatureWallet ) ,
269
306
) . toResolve ( ) ;
270
307
} ) ;
271
308
272
309
it ( "should not throw - multi sign unvote" , async ( ) => {
273
- multiSignatureWallet . setAttribute ( "vote" , delegateWallet . publicKey ) ;
310
+ multiSignatureWallet . setAttribute ( "vote" , delegateWallet1 . publicKey ) ;
274
311
await expect (
275
312
handler . throwIfCannotBeApplied ( multiSignatureUnvoteTransaction , multiSignatureWallet ) ,
276
313
) . toResolve ( ) ;
277
314
} ) ;
278
315
279
316
it ( "should throw if wallet has already voted" , async ( ) => {
280
- senderWallet . setAttribute ( "vote" , delegateWallet . publicKey ) ;
317
+ senderWallet . setAttribute ( "vote" , delegateWallet1 . publicKey ) ;
281
318
await expect ( handler . throwIfCannotBeApplied ( voteTransaction , senderWallet ) ) . rejects . toThrow (
282
319
AlreadyVotedError ,
283
320
) ;
284
321
} ) ;
285
322
286
323
it ( "should throw if vote for non delegate wallet" , async ( ) => {
287
- delegateWallet . forgetAttribute ( "delegate" ) ;
288
- walletRepository . index ( delegateWallet ) ;
324
+ delegateWallet1 . forgetAttribute ( "delegate" ) ;
325
+ walletRepository . index ( delegateWallet1 ) ;
289
326
await expect ( handler . throwIfCannotBeApplied ( voteTransaction , senderWallet ) ) . rejects . toThrow (
290
327
VotedForNonDelegateError ,
291
328
) ;
@@ -311,7 +348,7 @@ describe("VoteTransaction", () => {
311
348
312
349
it ( "should throw if wallet has insufficient funds for unvote" , async ( ) => {
313
350
senderWallet . balance = Utils . BigNumber . ZERO ;
314
- senderWallet . setAttribute ( "vote" , delegateWallet . publicKey ) ;
351
+ senderWallet . setAttribute ( "vote" , delegateWallet1 . publicKey ) ;
315
352
await expect ( handler . throwIfCannotBeApplied ( unvoteTransaction , senderWallet ) ) . rejects . toThrow (
316
353
InsufficientBalanceError ,
317
354
) ;
@@ -332,6 +369,44 @@ describe("VoteTransaction", () => {
332
369
Exceptions . Runtime . AssertionException ,
333
370
) ;
334
371
} ) ;
372
+
373
+ it ( "should not throw on vote+unvote transaction when wallet has not voted" , async ( ) => {
374
+ await handler . throwIfCannotBeApplied ( voteUnvoteTransaction , senderWallet ) ;
375
+ } ) ;
376
+
377
+ it ( "should throw on vote+unvote transaction when wallet has voted" , async ( ) => {
378
+ senderWallet . setAttribute ( "vote" , delegateWallet1 . publicKey ) ;
379
+
380
+ await expect ( handler . throwIfCannotBeApplied ( voteUnvoteTransaction , senderWallet ) ) . rejects . toThrow (
381
+ AlreadyVotedError ,
382
+ ) ;
383
+ } ) ;
384
+
385
+ it ( "should not throw on unvote+vote transaction when wallet has voted" , async ( ) => {
386
+ senderWallet . setAttribute ( "vote" , delegateWallet1 . publicKey ) ;
387
+
388
+ await handler . throwIfCannotBeApplied ( unvoteVoteTransaction , senderWallet ) ;
389
+ } ) ;
390
+
391
+ it ( "should throw on unvote+vote transaction when wallet has not voted" , async ( ) => {
392
+ await expect ( handler . throwIfCannotBeApplied ( unvoteVoteTransaction , senderWallet ) ) . rejects . toThrow (
393
+ NoVoteError ,
394
+ ) ;
395
+ } ) ;
396
+
397
+ it ( "should throw on vote+vote transaction when wallet has not voted" , async ( ) => {
398
+ await expect ( handler . throwIfCannotBeApplied ( voteVoteTransaction , senderWallet ) ) . rejects . toThrow (
399
+ AlreadyVotedError ,
400
+ ) ;
401
+ } ) ;
402
+
403
+ it ( "should throw on unvote+unvote transaction when wallet has voted" , async ( ) => {
404
+ senderWallet . setAttribute ( "vote" , delegateWallet1 . publicKey ) ;
405
+
406
+ await expect ( handler . throwIfCannotBeApplied ( unvoteUnvoteTransaction , senderWallet ) ) . rejects . toThrow (
407
+ NoVoteError ,
408
+ ) ;
409
+ } ) ;
335
410
} ) ;
336
411
337
412
describe ( "throwIfCannotEnterPool" , ( ) => {
@@ -358,7 +433,7 @@ describe("VoteTransaction", () => {
358
433
} ) ;
359
434
360
435
it ( "should not be ok" , async ( ) => {
361
- senderWallet . setAttribute ( "vote" , delegateWallet . publicKey ) ;
436
+ senderWallet . setAttribute ( "vote" , delegateWallet1 . publicKey ) ;
362
437
363
438
expect ( senderWallet . getAttribute ( "vote" ) ) . not . toBeUndefined ( ) ;
364
439
@@ -370,7 +445,7 @@ describe("VoteTransaction", () => {
370
445
371
446
describe ( "unvote" , ( ) => {
372
447
it ( "should remove the vote from the wallet" , async ( ) => {
373
- senderWallet . setAttribute ( "vote" , delegateWallet . publicKey ) ;
448
+ senderWallet . setAttribute ( "vote" , delegateWallet1 . publicKey ) ;
374
449
375
450
expect ( senderWallet . getAttribute ( "vote" ) ) . not . toBeUndefined ( ) ;
376
451
@@ -379,6 +454,40 @@ describe("VoteTransaction", () => {
379
454
expect ( senderWallet . hasAttribute ( "vote" ) ) . toBeFalse ( ) ;
380
455
} ) ;
381
456
} ) ;
457
+
458
+ describe ( "vote+unvote" , ( ) => {
459
+ it ( "should apply when wallet has not voted" , async ( ) => {
460
+ await handler . apply ( voteUnvoteTransaction ) ;
461
+
462
+ expect ( senderWallet . hasAttribute ( "vote" ) ) . toBeFalse ( ) ;
463
+ } ) ;
464
+
465
+ it ( "should throw when wallet has voted" , async ( ) => {
466
+ senderWallet . setAttribute ( "vote" , delegateWallet1 . publicKey ) ;
467
+
468
+ await expect ( handler . apply ( voteUnvoteTransaction ) ) . rejects . toThrow ( AlreadyVotedError ) ;
469
+ } ) ;
470
+ } ) ;
471
+
472
+ describe ( "unvote+vote" , ( ) => {
473
+ it ( "should apply when wallet has voted" , async ( ) => {
474
+ senderWallet . setAttribute ( "vote" , delegateWallet1 . publicKey ) ;
475
+
476
+ await handler . apply ( unvoteVoteTransaction ) ;
477
+
478
+ expect ( senderWallet . getAttribute ( "vote" ) ) . toEqual ( delegateWallet2 . publicKey ) ;
479
+ } ) ;
480
+
481
+ it ( "should throw when wallet has not voted" , async ( ) => {
482
+ await expect ( handler . apply ( unvoteUnvoteTransaction ) ) . rejects . toThrow ( NoVoteError ) ;
483
+ } ) ;
484
+
485
+ it ( "should throw when wallet has voted for different delegate" , async ( ) => {
486
+ senderWallet . setAttribute ( "vote" , delegateWallet2 . publicKey ) ;
487
+
488
+ await expect ( handler . apply ( unvoteUnvoteTransaction ) ) . rejects . toThrow ( UnvoteMismatchError ) ;
489
+ } ) ;
490
+ } ) ;
382
491
} ) ;
383
492
384
493
describe ( "applyForSender" , ( ) => {
@@ -402,7 +511,7 @@ describe("VoteTransaction", () => {
402
511
describe ( "revert" , ( ) => {
403
512
describe ( "vote" , ( ) => {
404
513
it ( "should remove the vote from the wallet" , async ( ) => {
405
- senderWallet . setAttribute ( "vote" , delegateWallet . publicKey ) ;
514
+ senderWallet . setAttribute ( "vote" , delegateWallet1 . publicKey ) ;
406
515
senderWallet . nonce = Utils . BigNumber . make ( 1 ) ;
407
516
408
517
expect ( senderWallet . getAttribute ( "vote" ) ) . not . toBeUndefined ( ) ;
@@ -423,7 +532,28 @@ describe("VoteTransaction", () => {
423
532
await handler . revert ( unvoteTransaction ) ;
424
533
425
534
expect ( senderWallet . nonce . isZero ( ) ) . toBeTrue ( ) ;
426
- expect ( senderWallet . getAttribute ( "vote" ) ) . toBe ( delegateWallet . publicKey ) ;
535
+ expect ( senderWallet . getAttribute ( "vote" ) ) . toBe ( delegateWallet1 . publicKey ) ;
536
+ } ) ;
537
+ } ) ;
538
+
539
+ describe ( "vote+unvote" , ( ) => {
540
+ it ( "should revert when wallet has no vote" , async ( ) => {
541
+ senderWallet . nonce = Utils . BigNumber . make ( 1 ) ;
542
+
543
+ await handler . revert ( voteUnvoteTransaction ) ;
544
+
545
+ expect ( senderWallet . hasAttribute ( "vote" ) ) . toBeFalse ( ) ;
546
+ } ) ;
547
+ } ) ;
548
+
549
+ describe ( "unvote+vote" , ( ) => {
550
+ it ( "should revert when wallet has no vote" , async ( ) => {
551
+ senderWallet . setAttribute ( "vote" , delegateWallet2 . publicKey ) ;
552
+ senderWallet . nonce = Utils . BigNumber . make ( 1 ) ;
553
+
554
+ await handler . revert ( unvoteVoteTransaction ) ;
555
+
556
+ expect ( senderWallet . getAttribute ( "vote" ) ) . toEqual ( delegateWallet1 . publicKey ) ;
427
557
} ) ;
428
558
} ) ;
429
559
} ) ;
@@ -444,7 +574,9 @@ describe("VoteTransaction", () => {
444
574
445
575
senderWallet . nonce = Utils . BigNumber . ONE ;
446
576
447
- await expect ( handler . revertForSender ( voteTransaction ) ) . rejects . toThrow ( Exceptions . Runtime . AssertionException ) ;
577
+ await expect ( handler . revertForSender ( voteTransaction ) ) . rejects . toThrow (
578
+ Exceptions . Runtime . AssertionException ,
579
+ ) ;
448
580
} ) ;
449
581
} ) ;
450
582
} ) ;
0 commit comments