-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathgovernance.ts
More file actions
895 lines (717 loc) · 31.6 KB
/
governance.ts
File metadata and controls
895 lines (717 loc) · 31.6 KB
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
import { BN } from 'bn.js'
import { assert, describe, test } from 'vitest'
import { type Chain, defaultAccountsSr25519 } from '@e2e-test/networks'
import { type Client, setupNetworks } from '@e2e-test/shared'
import { check, checkEvents, checkSystemEvents, objectCmp, scheduleInlineCallWithOrigin } from './helpers/index.js'
import { sendTransaction } from '@acala-network/chopsticks-testing'
import type { Option, u32 } from '@polkadot/types'
import type {
PalletConvictionVotingVoteCasting,
PalletConvictionVotingVoteVoting,
PalletReferendaDeposit,
PalletReferendaReferendumInfoConvictionVotingTally,
PalletReferendaReferendumStatusConvictionVotingTally,
} from '@polkadot/types/lookup'
import type { ITuple } from '@polkadot/types/types'
import { encodeAddress } from '@polkadot/util-crypto'
/// -------
/// Helpers
/// -------
const devAccounts = defaultAccountsSr25519
/**
* Compare the selected properties of two referenda.
*
* Fails if any of the properties to be compared is different.
*
* It can be desirable to compare a referendum in its pre- and post-block-execution states.
* For example:
* 1. from the time its decision deposited is placed until its preparation period elapses, no field
* of the referendum may change
*
* a. to know which block in the iterated comparisons caused the failure, an optional error
* message parameter is passable
* 2. after placing a vote, the referendum's tally and alarm should change, but nothing else
*
* @param ref1
* @param ref2
* @param propertiesToBeSkipped List of properties to not be included in the referenda comparison
* @param errorMsg Additional error message when using this function inside a loop, to
* identify failing iteration.
*/
function referendumCmp(
ref1: PalletReferendaReferendumStatusConvictionVotingTally,
ref2: PalletReferendaReferendumStatusConvictionVotingTally,
propertiesToBeSkipped: string[],
optErrorMsg?: string,
) {
const properties = [
'track',
'origin',
'proposal',
'enactment',
'submitted',
'submissionDeposit',
'decisionDeposit',
'deciding',
'tally',
'inQueue',
'alarm',
]
const msgFun = (p: string) =>
`Referenda differed on property \`${p}\`
Left: ${ref1[p]}
Right: ${ref2[p]}`
objectCmp(ref1, ref2, properties, propertiesToBeSkipped, msgFun, optErrorMsg)
}
/// -------
/// -------
/// -------
/**
* Test the process of
* 1. submitting a referendum for a treasury spend
* 2. placing its decision deposit
* 3. awaiting the end of the preparation period
* 4. voting on it after the decision period has commenced
*
* 4.1. using `vote`
*
* 4.2. using a split vote
*
* 4.3. using a split-abstain vote
* 5. cancelling the referendum using the scheduler to insert a `Root`-origin call
*
* 5.1 checking that locks on submission/decision deposits are released
*
* 5.2 checking that voters' class locks and voting data are not affected
*
* 6. removing the votes cast
*
* 6.1 asserting that voting locks are preserved
*
* 6.2 asserting that voting funds are returned
*
* 7. refunding the submission and decision deposits
*/
export async function referendumLifecycleTest<
TCustom extends Record<string, unknown> | undefined,
TInitStorages extends Record<string, Record<string, any>> | undefined,
>(client: Client<TCustom, TInitStorages>, addressEncoding: number) {
// Fund test accounts not already provisioned in the test chain spec.
await client.dev.setStorage({
System: {
account: [
[[devAccounts.bob.address], { providers: 1, data: { free: 10e10 } }],
[[devAccounts.charlie.address], { providers: 1, data: { free: 10e10 } }],
[[devAccounts.dave.address], { providers: 1, data: { free: 10e10 } }],
[[devAccounts.eve.address], { providers: 1, data: { free: 10e10 } }],
],
},
})
/**
* Get current referendum count i.e. the next referendum's index
*/
const referendumIndex = await client.api.query.referenda.referendumCount()
/**
* Submit a new referendum
*/
const submissionTx = client.api.tx.referenda.submit(
{
Origins: 'SmallTipper',
} as any,
{
Inline: client.api.tx.treasury.spendLocal(1e10, devAccounts.bob.address).method.toHex(),
},
{
After: 1,
},
)
const submissionEvents = await sendTransaction(submissionTx.signAsync(devAccounts.alice))
await client.dev.newBlock()
// Fields to be removed, check comment below.
let unwantedFields = /index/
await checkEvents(submissionEvents, 'referenda')
.redact({ removeKeys: unwantedFields })
.toMatchSnapshot('referendum submission events')
/**
* Check the created referendum's data
*/
let referendumDataOpt: Option<PalletReferendaReferendumInfoConvictionVotingTally> =
await client.api.query.referenda.referendumInfoFor(referendumIndex)
assert(referendumDataOpt.isSome, "submitted referendum's data cannot be `None`")
let referendumData: PalletReferendaReferendumInfoConvictionVotingTally = referendumDataOpt.unwrap()
// These fields must be excised from the queried referendum data before being put in the test
// snapshot.
// These fields contain epoch-sensitive data, which will cause spurious test failures
// periodically.
unwantedFields = /alarm|submitted/
await check(referendumData)
.redact({ removeKeys: unwantedFields })
.toMatchSnapshot('referendum info before decision deposit')
assert(referendumData.isOngoing)
// Ongoing referendum data, prior to the decision deposit.
const ongoingRefPreDecDep: PalletReferendaReferendumStatusConvictionVotingTally = referendumData.asOngoing
assert(ongoingRefPreDecDep.alarm.isSome)
const undecidingTimeoutAlarm = ongoingRefPreDecDep.alarm.unwrap()[0]
const blocksUntilAlarm = undecidingTimeoutAlarm.sub(ongoingRefPreDecDep.submitted)
// Check that the referendum's alarm is set to ring after the (globally predetermined) timeout
// of 14 days, or 201600 blocks.
assert(blocksUntilAlarm.eq(client.api.consts.referenda.undecidingTimeout))
// The referendum was above set to be enacted 1 block after its passing.
assert(ongoingRefPreDecDep.enactment.isAfter)
assert(ongoingRefPreDecDep.enactment.asAfter.eq(1))
const referendaTracks = client.api.consts.referenda.tracks
const smallTipper = referendaTracks.find((track) => track[1].name.eq('small_tipper'))!
assert(ongoingRefPreDecDep.track.eq(smallTipper[0]))
await check(ongoingRefPreDecDep.origin).toMatchObject({
origins: 'SmallTipper',
})
// Immediately after a referendum's submission, it will not have a decision deposit,
// which it will need to begin the decision period.
assert(ongoingRefPreDecDep.deciding.isNone)
assert(ongoingRefPreDecDep.decisionDeposit.isNone)
assert(ongoingRefPreDecDep.submissionDeposit.who.eq(encodeAddress(devAccounts.alice.address, addressEncoding)))
assert(ongoingRefPreDecDep.submissionDeposit.amount.eq(client.api.consts.referenda.submissionDeposit))
// Current voting state of the referendum.
const votes = {
ayes: 0,
nays: 0,
support: 0,
}
// Check that voting data is empty
await check(ongoingRefPreDecDep.tally).toMatchObject(votes)
/**
* Place decision deposit
*/
const decisionDepTx = client.api.tx.referenda.placeDecisionDeposit(referendumIndex)
const decisiondepEvents = await sendTransaction(decisionDepTx.signAsync(devAccounts.bob))
await client.dev.newBlock()
// Once more, fields containing temporally-contigent information - block numbers - must be excised
// from test data to avoid spurious failures after updating block numbers.
unwantedFields = /alarm|index|submitted/
await checkEvents(decisiondepEvents, 'referenda')
.redact({ removeKeys: unwantedFields })
.toMatchSnapshot("events for bob's decision deposit")
referendumDataOpt = await client.api.query.referenda.referendumInfoFor(referendumIndex)
assert(referendumDataOpt.isSome, "referendum's data cannot be `None`")
referendumData = referendumDataOpt.unwrap()
await check(referendumData)
.redact({ removeKeys: unwantedFields })
.toMatchSnapshot('referendum info post decision deposit')
assert(referendumData.isOngoing)
const ongoingRefPostDecDep = referendumData.asOngoing
// The referendum can only begin deciding after its track's preparation period has elapsed, even though
// the decision deposit has been placed.
assert(ongoingRefPostDecDep.deciding.isNone)
assert(ongoingRefPostDecDep.decisionDeposit.isSome)
assert(ongoingRefPostDecDep.decisionDeposit.unwrap().who.eq(encodeAddress(devAccounts.bob.address, addressEncoding)))
assert(ongoingRefPostDecDep.decisionDeposit.unwrap().amount.eq(smallTipper[1].decisionDeposit))
// The block at which the referendum's preparation period will end, and its decision period will begin.
const preparePeriodWithOffset = smallTipper[1].preparePeriod.add(ongoingRefPostDecDep.submitted)
assert(ongoingRefPostDecDep.alarm.isSome)
// The decision deposit has been placed, so the referendum's alarm should point to that block, at the
// end of the decision period.
assert(ongoingRefPostDecDep.alarm.unwrap()[0].eq(preparePeriodWithOffset))
// Placing a decision deposit for a referendum should change nothing BUT the referendum's
// 1. deposit data and
// 2. alarm.
referendumCmp(ongoingRefPreDecDep, ongoingRefPostDecDep, ['decisionDeposit', 'alarm'])
/**
* Wait for preparation period to elapse
*/
let refPre = ongoingRefPostDecDep
let refPost: PalletReferendaReferendumStatusConvictionVotingTally
for (let i = 0; i < smallTipper[1].preparePeriod.toNumber() - 2; i++) {
await client.dev.newBlock()
referendumDataOpt = await client.api.query.referenda.referendumInfoFor(referendumIndex)
assert(referendumDataOpt.isSome, "referendum's data cannot be `None`")
referendumData = referendumDataOpt.unwrap()
assert(referendumData.isOngoing)
refPost = referendumData.asOngoing
referendumCmp(refPre, refPost, [], `Failed on iteration number ${i}.`)
refPre = refPost
}
await client.dev.newBlock()
referendumDataOpt = await client.api.query.referenda.referendumInfoFor(referendumIndex)
const refNowDeciding = referendumDataOpt.unwrap().asOngoing
unwantedFields = /alarm|submitted|since/
await check(refNowDeciding)
.redact({ removeKeys: unwantedFields })
.toMatchSnapshot('referendum upon start of decision period')
const decisionPeriodStartBlock = ongoingRefPreDecDep.submitted.add(smallTipper[1].preparePeriod)
assert(refNowDeciding.alarm.unwrap()[0].eq(smallTipper[1].decisionPeriod.add(decisionPeriodStartBlock)))
assert(
refNowDeciding.deciding.eq({
since: decisionPeriodStartBlock,
confirming: null,
}),
)
referendumCmp(refPost!, refNowDeciding, ['alarm', 'deciding'])
/**
* Vote on the referendum
*/
// Charlie's vote
const ayeVote = 5e10
let voteTx = client.api.tx.convictionVoting.vote(referendumIndex, {
Standard: {
vote: {
aye: true,
conviction: 'Locked3x',
},
balance: ayeVote,
},
})
let voteEvents = await sendTransaction(voteTx.signAsync(devAccounts.charlie))
await client.dev.newBlock()
unwantedFields = /alarm|when|since|submitted/
await checkEvents(voteEvents, 'convictionVoting')
.redact({ removeKeys: unwantedFields, redactKeys: unwantedFields })
.toMatchSnapshot("events for charlie's vote")
referendumDataOpt = await client.api.query.referenda.referendumInfoFor(referendumIndex)
assert(referendumDataOpt.isSome, "referendum's data cannot be `None`")
referendumData = referendumDataOpt.unwrap()
await check(referendumData)
.redact({ removeKeys: unwantedFields })
.toMatchSnapshot("referendum info after charlie's vote")
assert(referendumData.isOngoing)
const ongoingRefFirstVote = referendumData.asOngoing
// Charlie voted with 3x conviction
votes.ayes += ayeVote * 3
votes.support += ayeVote
await check(ongoingRefFirstVote.tally).toMatchObject(votes)
// Check Charlie's locked funds
const charlieClassLocks = await client.api.query.convictionVoting.classLocksFor(devAccounts.charlie.address)
const localCharlieClassLocks = [[smallTipper[0], ayeVote]]
assert(charlieClassLocks.eq(localCharlieClassLocks))
// , and overall account's votes
const votingByCharlie: PalletConvictionVotingVoteVoting = await client.api.query.convictionVoting.votingFor(
devAccounts.charlie.address,
smallTipper[0],
)
assert(votingByCharlie.isCasting, "charlie's votes are cast, not delegated")
const charlieCastVotes: PalletConvictionVotingVoteCasting = votingByCharlie.asCasting
// The information present in the `VotingFor` storage item contains the referendum index,
// which must be removed.
const unwantedRefIx = new RegExp(`${referendumIndex},`)
await check(charlieCastVotes.votes[0][1])
.redact({ removeKeys: unwantedRefIx })
.toMatchSnapshot("charlie's votes after casting his")
assert(charlieCastVotes.votes.length === 1)
assert(charlieCastVotes.votes[0][0].eq(referendumIndex))
const charlieVotes = charlieCastVotes.votes[0][1].asStandard
assert(charlieVotes.vote.conviction.isLocked3x && charlieVotes.vote.isAye)
// After a vote the referendum's alarm is set to the block following the one the vote tx was
// included in.
ongoingRefFirstVote.alarm.unwrap()[0].eq(refNowDeciding.deciding.unwrap().since.add(new BN(1)))
// Placing a vote for a referendum should change nothing BUT:
// 1. the tally, and
// 2. its decision period, which at this point should still be counting down.
referendumCmp(refNowDeciding, ongoingRefFirstVote, ['tally', 'alarm'])
// Dave's vote
const nayVote = 1e10
voteTx = client.api.tx.convictionVoting.vote(referendumIndex, {
Split: {
aye: ayeVote,
nay: nayVote,
},
})
voteEvents = await sendTransaction(voteTx.signAsync(devAccounts.dave))
await client.dev.newBlock()
await checkEvents(voteEvents, 'convictionVoting')
.redact({ removeKeys: unwantedFields })
.toMatchSnapshot("events for dave's vote")
referendumDataOpt = await client.api.query.referenda.referendumInfoFor(referendumIndex)
assert(referendumDataOpt.isSome, "referendum's data cannot be `None`")
referendumData = referendumDataOpt.unwrap()
await check(referendumData)
.redact({ removeKeys: unwantedFields })
.toMatchSnapshot("referendum info after dave's vote")
assert(referendumData.isOngoing)
const ongoingRefSecondVote = referendumData.asOngoing
votes.ayes += ayeVote / 10
votes.nays += nayVote / 10
votes.support += ayeVote
await check(ongoingRefSecondVote.tally).toMatchObject(votes)
const daveLockedFunds = await client.api.query.convictionVoting.classLocksFor(devAccounts.dave.address)
const localDaveClassLocks = [[smallTipper[0], ayeVote + nayVote]]
// Dave voted with `split`, which does not allow expression of conviction in votes.
assert(daveLockedFunds.eq(localDaveClassLocks))
// Check Dave's overall votes
const votingByDave: PalletConvictionVotingVoteVoting = await client.api.query.convictionVoting.votingFor(
devAccounts.dave.address,
smallTipper[0],
)
assert(votingByDave.isCasting, "dave's votes are cast, not delegated")
const daveCastVotes: PalletConvictionVotingVoteCasting = votingByDave.asCasting
await check(daveCastVotes.votes[0][1])
.redact({ removeKeys: unwantedRefIx })
.toMatchSnapshot("dave's votes after casting his")
assert(daveCastVotes.votes.length === 1)
assert(daveCastVotes.votes[0][0].eq(referendumIndex))
const daveVote = daveCastVotes.votes[0][1].asSplit
assert(daveVote.aye.eq(ayeVote))
assert(daveVote.nay.eq(nayVote))
// After a vote the referendum's alarm is set to the block following the one the vote tx was
// included in.
ongoingRefSecondVote.alarm.unwrap()[0].eq(ongoingRefFirstVote.deciding.unwrap().since.add(new BN(1)))
// Placing a split vote for a referendum should change nothing BUT:
// 1. the tally, and
// 2. its decision period, still counting down.
referendumCmp(ongoingRefFirstVote, ongoingRefSecondVote, ['tally', 'alarm'])
// Eve's vote
const abstainVote = 2e10
voteTx = client.api.tx.convictionVoting.vote(referendumIndex, {
SplitAbstain: {
aye: ayeVote,
nay: nayVote,
abstain: abstainVote,
},
})
voteEvents = await sendTransaction(voteTx.signAsync(devAccounts.eve))
await client.dev.newBlock()
await checkEvents(voteEvents, 'convictionVoting')
.redact({ removeKeys: unwantedFields })
.toMatchSnapshot("events for eve's vote")
referendumDataOpt = await client.api.query.referenda.referendumInfoFor(referendumIndex)
assert(referendumDataOpt.isSome, "referendum's data cannot be `None`")
referendumData = referendumDataOpt.unwrap()
await check(referendumData).redact({ removeKeys: unwantedFields }).toMatchSnapshot("referendum info after eve's vote")
assert(referendumData.isOngoing)
const ongoingRefThirdVote = referendumData.asOngoing
votes.ayes += ayeVote / 10
votes.nays += nayVote / 10
votes.support += ayeVote + abstainVote
await check(ongoingRefThirdVote.tally).toMatchObject(votes)
const eveLockedFunds = await client.api.query.convictionVoting.classLocksFor(devAccounts.eve.address)
const localEveClassLocks = [[smallTipper[0], ayeVote + nayVote + abstainVote]]
// Eve voted with `splitAbstain`, which does not allow expression of conviction in votes.
assert(eveLockedFunds.eq(localEveClassLocks))
// Check Eve's overall votes
const votingByEve: PalletConvictionVotingVoteVoting = await client.api.query.convictionVoting.votingFor(
devAccounts.eve.address,
smallTipper[0],
)
assert(votingByEve.isCasting, "eve's votes are cast, not delegated")
const eveCastVotes: PalletConvictionVotingVoteCasting = votingByEve.asCasting
await check(eveCastVotes.votes[0][1])
.redact({ removeKeys: unwantedRefIx })
.toMatchSnapshot("eve's votes after casting hers")
assert(eveCastVotes.votes.length === 1)
assert(eveCastVotes.votes[0][0].eq(referendumIndex))
const eveVote = eveCastVotes.votes[0][1].asSplitAbstain
assert(eveVote.aye.eq(ayeVote))
assert(eveVote.nay.eq(nayVote))
assert(eveVote.abstain.eq(abstainVote))
// After a vote, the referendum's alarm is set to the block following the one the vote tx was
// included in.
ongoingRefThirdVote.alarm.unwrap()[0].eq(ongoingRefSecondVote.deciding.unwrap().since.add(new BN(1)))
// Placing a split abstain vote for a referendum should change nothing BUT:
// 1. the tally, and
// 2. its decision period, still counting down.
referendumCmp(ongoingRefSecondVote, ongoingRefThirdVote, ['tally', 'alarm'])
// Attempt to cancel the referendum with a signed origin - this should fail.
const cancelRefCall = client.api.tx.referenda.cancel(referendumIndex)
await sendTransaction(cancelRefCall.signAsync(devAccounts.alice))
await client.dev.newBlock()
await checkSystemEvents(client, { section: 'system', method: 'ExtrinsicFailed' }).toMatchSnapshot(
'cancelling referendum with signed origin',
)
// Cancel the referendum using the scheduler pallet to simulate a root origin
scheduleInlineCallWithOrigin(client, cancelRefCall.method.toHex(), { system: 'Root' })
await client.dev.newBlock()
/**
* Check cancelled ref's data
*/
// First, the emitted events
// Retrieve the events for the latest block
const events = await client.api.query.system.events()
const referendaEvents = events.filter((record) => {
const { event } = record
return event.section === 'referenda'
})
assert(referendaEvents.length === 1, 'cancelling a referendum should emit 1 event')
const cancellationEvent = referendaEvents[0]
assert(client.api.events.referenda.Cancelled.is(cancellationEvent.event))
const [index, tally] = cancellationEvent.event.data
assert(index.eq(referendumIndex))
assert(tally.eq(votes))
// Now, check the referendum's data, post-cancellation
referendumDataOpt = await client.api.query.referenda.referendumInfoFor(referendumIndex)
// cancelling a referendum does not remove it from storage
assert(referendumDataOpt.isSome, "referendum's data cannot be `None`")
assert(referendumDataOpt.unwrap().isCancelled, 'referendum should be cancelled!')
const cancelledRef: ITuple<[u32, Option<PalletReferendaDeposit>, Option<PalletReferendaDeposit>]> =
referendumDataOpt.unwrap().asCancelled
cancelledRef[0].eq(referendumIndex)
// Check that the referendum's submission deposit was refunded to Alice
cancelledRef[1].unwrap().eq({
who: encodeAddress(devAccounts.alice.address, addressEncoding),
amount: client.api.consts.referenda.submissionDeposit,
})
// Check that the referendum's submission deposit was refunded to Bob
cancelledRef[2].unwrap().eq({
who: encodeAddress(devAccounts.bob.address, addressEncoding),
amount: smallTipper[1].decisionDeposit,
})
const testAccounts = {
charlie: {
classLocks: charlieClassLocks,
localClassLocks: localCharlieClassLocks,
votingBy: votingByCharlie,
},
dave: {
classLocks: daveLockedFunds,
localClassLocks: localDaveClassLocks,
votingBy: votingByDave,
},
eve: {
classLocks: eveLockedFunds,
localClassLocks: localEveClassLocks,
votingBy: votingByEve,
},
}
// Check that cancelling the referendum has no effect on each voter's class locks
for (const account of Object.keys(testAccounts)) {
testAccounts[account].classLocks = await client.api.query.convictionVoting.classLocksFor(
devAccounts[account].address,
)
assert(
testAccounts[account].classLocks.eq(testAccounts[account].localClassLocks),
`${account}'s class locks should be unaffected by referendum cancellation`,
)
}
// Check that cancelling the referendum has no effect on accounts' votes, as seen via `votingFor`
// storage item.
for (const account of Object.keys(testAccounts)) {
const postCancellationVoting: PalletConvictionVotingVoteVoting = await client.api.query.convictionVoting.votingFor(
devAccounts[account].address as string,
smallTipper[0],
)
assert(postCancellationVoting.isCasting, `pre-referendum cancellation, ${account}'s votes were cast, not delegated`)
const postCancellationCastVotes: PalletConvictionVotingVoteCasting = postCancellationVoting.asCasting
assert(
postCancellationVoting.eq(testAccounts[account].votingBy),
`${account}'s votes should be unaffected by referendum cancellation`,
)
await check(postCancellationCastVotes.votes[0][1])
.redact({ removeKeys: unwantedRefIx })
.toMatchSnapshot(`${account}'s votes after referendum's cancellation`)
}
/**
* Vote withdrawal transactions, batched atomically.
*/
const removeCharlieVote = client.api.tx.convictionVoting.removeVote(smallTipper[0], referendumIndex).method
const removeDaveVoteAsCharlie = client.api.tx.convictionVoting.removeOtherVote(
devAccounts.dave.address,
smallTipper[0],
referendumIndex,
).method
const removeEveVoteAsCharlie = client.api.tx.convictionVoting.removeOtherVote(
devAccounts.eve.address,
smallTipper[0],
referendumIndex,
).method
const batchAllTx = client.api.tx.utility.batchAll([
removeCharlieVote,
removeDaveVoteAsCharlie,
removeEveVoteAsCharlie,
])
const batchEvents = await sendTransaction(batchAllTx.signAsync(devAccounts.charlie))
await client.dev.newBlock()
await checkEvents(batchEvents)
.redact({ removeKeys: /who/ })
.toMatchSnapshot('removal of votes in cancelled referendum')
// Check that each voter's class locks remain unaffected by vote removal - these are subject to a
// later update.
//
// Also check that voting for each account is appropriately empty.
for (const account of Object.keys(testAccounts)) {
testAccounts[account].classLocks = await client.api.query.convictionVoting.classLocksFor(
devAccounts[account].address,
)
assert(
testAccounts[account].classLocks.eq(testAccounts[account].localClassLocks),
`${account}'s class locks should be unaffected by vote removal`,
)
await check(testAccounts[account].classLocks).toMatchSnapshot(
`${account}'s class locks after their vote's rescission`,
)
testAccounts[account].votingBy = await client.api.query.convictionVoting.votingFor(
devAccounts[account].address,
smallTipper[0],
)
assert(testAccounts[account].votingBy.isCasting)
const castVotes = testAccounts[account].votingBy.asCasting
await check(castVotes).toMatchSnapshot(`${account}'s votes after rescission`)
assert(castVotes.votes.isEmpty)
}
// Check that submission and decision deposits are refunded to the respective voters.
const submissionRefundTx = client.api.tx.referenda.refundSubmissionDeposit(referendumIndex)
const submissionRefundEvents = await sendTransaction(submissionRefundTx.signAsync(devAccounts.alice))
const decisionRefundTx = client.api.tx.referenda.refundDecisionDeposit(referendumIndex)
const decisionRefundEvents = await sendTransaction(decisionRefundTx.signAsync(devAccounts.bob))
await client.dev.newBlock()
await checkEvents(submissionRefundEvents, 'referenda')
.redact({ removeKeys: /index/ })
.toMatchSnapshot('refund of submission deposit')
await checkEvents(decisionRefundEvents, 'referenda')
.redact({ removeKeys: /index/ })
.toMatchSnapshot('refund of decision deposit')
}
/**
* Test the process of
* 1. submitting a referendum for a treasury spend
* 2. placing its decision deposit
* 3. killing the referendum using the scheduler to insert a `Root`-origin call
*
* 3.1 checking that submission/decision deposits are slashed
*/
export async function referendumLifecycleKillTest<
TCustom extends Record<string, unknown> | undefined,
TInitStorages extends Record<string, Record<string, any>> | undefined,
>(client: Client<TCustom, TInitStorages>, addressEncoding: number) {
// Fund test accounts not already provisioned in the test chain spec.
await client.dev.setStorage({
System: {
account: [
[[devAccounts.alice.address], { providers: 1, data: { free: 10000e10 } }],
[[devAccounts.bob.address], { providers: 1, data: { free: 100000e10 } }],
],
},
})
/**
* Get current referendum count i.e. the next referendum's index
*/
const referendumIndex = await client.api.query.referenda.referendumCount()
/**
* Submit a new referendum
*/
const submitReferendumTx = client.api.tx.referenda.submit(
{
Origins: 'SmallTipper',
} as any,
{
Inline: client.api.tx.treasury.spendLocal(1e10, devAccounts.bob.address).method.toHex(),
},
{
After: 1,
},
)
await sendTransaction(submitReferendumTx.signAsync(devAccounts.alice))
await client.dev.newBlock()
/**
* Check the created referendum's data
*/
const referendaTracks = client.api.consts.referenda.tracks
const smallTipper = referendaTracks.find((track) => track[1].name.eq('small_tipper'))!
/**
* Place decision deposit
*/
const decisionDepTx = client.api.tx.referenda.placeDecisionDeposit(referendumIndex)
await sendTransaction(decisionDepTx.signAsync(devAccounts.bob))
await client.dev.newBlock()
// Attempt to kill the referendum with a signed origin
const killRefCall = client.api.tx.referenda.kill(referendumIndex)
await sendTransaction(killRefCall.signAsync(devAccounts.alice))
await client.dev.newBlock()
await checkSystemEvents(client, { section: 'system', method: 'ExtrinsicFailed' }).toMatchSnapshot(
'killing referendum with signed origin',
)
/**
* Kill the referendum using the scheduler pallet to simulate a root origin for the call.
*/
scheduleInlineCallWithOrigin(client, killRefCall.method.toHex(), { system: 'Root' })
await client.dev.newBlock()
/**
* Check killed ref's data
*/
// Retrieve the events for the latest block
const events = await client.api.query.system.events()
const referendaEvents = events.filter((record) => {
const { event } = record
return event.section === 'referenda'
})
assert(referendaEvents.length === 3, 'killing a referendum should emit 3 events')
referendaEvents.forEach((record) => {
const { event } = record
if (client.api.events.referenda.Killed.is(event)) {
const [index, tally] = event.data
assert(index.eq(referendumIndex))
assert(
tally.eq({
ayes: 0,
nays: 0,
support: 0,
}),
)
} else if (client.api.events.referenda.DepositSlashed.is(event)) {
const [who, amount] = event.data
if (who.eq(encodeAddress(devAccounts.alice.address, addressEncoding))) {
assert(amount.eq(client.api.consts.referenda.submissionDeposit))
} else if (who.eq(encodeAddress(devAccounts.bob.address, addressEncoding))) {
assert(amount.eq(smallTipper[1].decisionDeposit))
} else {
assert(false, 'malformed decision slashed events')
}
}
})
const referendumDataOpt = await client.api.query.referenda.referendumInfoFor(referendumIndex)
// killing a referendum does not remove it from storage, though it does prune most of its data.
assert(referendumDataOpt.isSome, "referendum's data cannot be `None`")
assert(referendumDataOpt.unwrap().isKilled, 'referendum should be cancelled!')
// The only information left from the killed referendum is the block number when it was killed.
const blockNumber = (await client.api.rpc.chain.getHeader()).number.toNumber()
const killedRef: u32 = referendumDataOpt.unwrap().asKilled
assert(killedRef.eq(blockNumber))
}
/**
* Test the registering, querying and unregistering a preimage; in this test, a `spend_local`
* treasury call.
*/
export async function preimageTest<
TCustom extends Record<string, unknown> | undefined,
TInitStorages extends Record<string, Record<string, any>> | undefined,
>(client: Client<TCustom, TInitStorages>) {
const encodedProposal = client.api.tx.treasury.spendLocal(1e10, devAccounts.bob.address).method
const preimageTx = client.api.tx.preimage.notePreimage(encodedProposal.toHex())
const preImageEvents = await sendTransaction(preimageTx.signAsync(devAccounts.alice))
await client.dev.newBlock()
await checkEvents(preImageEvents, 'preimage').toMatchSnapshot('note preimage events')
/**
* Query noted preimage
*/
let preimage = await client.api.query.preimage.preimageFor([
encodedProposal.hash.toHex(),
encodedProposal.encodedLength,
])
assert(preimage.isSome)
assert(preimage.unwrap().toHex() === encodedProposal.toHex())
/**
* Unnote preimage with the same account that had previously noted it
*/
const unnotePreimageTx = client.api.tx.preimage.unnotePreimage(encodedProposal.hash.toHex())
const unnotePreImageEvents = await sendTransaction(unnotePreimageTx.signAsync(devAccounts.alice))
await client.dev.newBlock()
await checkEvents(unnotePreImageEvents, 'preimage').toMatchSnapshot('unnote preimage events')
/**
* Query unnoted preimage, and verify it is absent
*/
preimage = await client.api.query.preimage.preimageFor([encodedProposal.hash.toHex(), encodedProposal.encodedLength])
assert(preimage.isNone)
}
export function governanceE2ETests<
TCustom extends Record<string, unknown> | undefined,
TInitStorages extends Record<string, Record<string, any>> | undefined,
>(chain: Chain<TCustom, TInitStorages>, testConfig: { testSuiteName: string; addressEncoding: number }) {
describe(testConfig.testSuiteName, async () => {
const [client] = await setupNetworks(chain)
test('referendum lifecycle test - submission, decision deposit, various voting should all work', async () => {
await referendumLifecycleTest(client, testConfig.addressEncoding)
})
test('referendum lifecycle test 2 - submission, decision deposit, and killing should work', async () => {
await referendumLifecycleKillTest(client, testConfig.addressEncoding)
})
test('preimage submission, query and removal works', async () => {
await preimageTest(client)
})
})
}