Skip to content

Commit 95ff1c3

Browse files
chore: improve proposal and pruning coverage (#83)
Improves coverage for some governance files.
1 parent ff40ef2 commit 95ff1c3

File tree

9 files changed

+350
-91
lines changed

9 files changed

+350
-91
lines changed

.github/workflows/check.yml

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ jobs:
5656
# RUST_BACKTRACE: 1
5757
# SKIP_WASM_BUILD: 1
5858
# run: cargo test
59-
59+
6060
- uses: jwalton/gh-find-current-pr@v1
6161
id: findPr
6262

@@ -79,7 +79,7 @@ jobs:
7979
env:
8080
RUST_BACKTRACE: 1
8181
SKIP_WASM_BUILD: 1
82-
82+
8383
- name: Generate coverage summary report
8484
if: success() && steps.findPr.outputs.number && steps.extractBranch.outputs.branch
8585
uses: irongut/CodeCoverageSummary@v1.3.0
@@ -98,10 +98,11 @@ jobs:
9898
env:
9999
RUST_BACKTRACE: 1
100100
SKIP_WASM_BUILD: 1
101-
102-
- name: Upload html report to S3 Bucket
101+
102+
- name: Upload html report to S3 Bucket
103103
if: success() && steps.findPr.outputs.number && steps.extractBranch.outputs.branch
104104
id: htmlUpload
105+
continue-on-error: true
105106
run: |
106107
aws --endpoint-url $ENDPOINT s3 sync ./target/llvm-cov/html s3://$BUCKET_NAME/$BRANCH
107108
echo "link=$(aws --endpoint-url $ENDPOINT s3 presign s3://$BUCKET_NAME/$BRANCH/index.html)" >> $GITHUB_OUTPUT
@@ -112,7 +113,7 @@ jobs:
112113
AWS_ACCESS_KEY_ID: ${{ secrets.COV_AWS_ACCESS_KEY_ID }}
113114
AWS_SECRET_ACCESS_KEY: ${{ secrets.COV_AWS_SECRET_ACCESS_KEY }}
114115
AWS_DEFAULT_REGION: ${{ vars.COV_DEFAULT_REGION }}
115-
116+
116117
- name: Add coverage PR report comment
117118
if: success() && steps.findPr.outputs.number
118119
uses: marocchino/sticky-pull-request-comment@v2
@@ -131,6 +132,3 @@ jobs:
131132
recreate: true
132133
message: |
133134
[Detailed coverage report](${{ steps.htmlUpload.outputs.link }})
134-
135-
136-

pallets/governance/src/roles.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ use crate::{ensure, storage::StorageMap, AccountIdOf, Config, Error};
1010

1111
/// Generic function used to manage the Curator and Allocator maps, which behave
1212
/// similarly.
13-
pub(super) fn manage_role<T: Config, M: StorageMap<AccountIdOf<T>, ()>>(
13+
#[doc(hidden)]
14+
pub fn manage_role<T: Config, M: StorageMap<AccountIdOf<T>, ()>>(
1415
key: AccountIdOf<T>,
1516
is_add: bool,
1617
error: Error<T>,

pallets/governance/tests/application.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,12 @@ fn application_denied_doesnt_add_to_whitelist() {
255255
#[test]
256256
fn application_expires() {
257257
new_test_ext().execute_with(|| {
258+
let expiration_blocks =
259+
pallet_governance::GlobalGovernanceConfig::<Test>::mutate(|config| {
260+
config.agent_application_expiration = 200;
261+
config.agent_application_expiration
262+
});
263+
258264
let key = 0;
259265
let adding_key = 1;
260266
pallet_governance::Curators::<Test>::insert(key, ());
@@ -278,9 +284,7 @@ fn application_expires() {
278284
application_id = value.id;
279285
}
280286

281-
step_block(
282-
pallet_governance::GlobalGovernanceConfig::<Test>::get().agent_application_expiration,
283-
);
287+
step_block(expiration_blocks);
284288

285289
let application =
286290
pallet_governance::AgentApplications::<Test>::get(application_id).unwrap();
@@ -291,6 +295,12 @@ fn application_expires() {
291295
#[test]
292296
fn error_is_thrown_on_resolving_non_open_application() {
293297
new_test_ext().execute_with(|| {
298+
let expiration_blocks =
299+
pallet_governance::GlobalGovernanceConfig::<Test>::mutate(|config| {
300+
config.agent_application_expiration = 200;
301+
config.agent_application_expiration
302+
});
303+
294304
let key = 0;
295305
let adding_key = 1;
296306
pallet_governance::Curators::<Test>::insert(key, ());
@@ -345,9 +355,7 @@ fn error_is_thrown_on_resolving_non_open_application() {
345355
let application_id = 1;
346356

347357
// expires the application
348-
step_block(
349-
pallet_governance::GlobalGovernanceConfig::<Test>::get().agent_application_expiration,
350-
);
358+
step_block(expiration_blocks);
351359
let balance_after_expire = get_balance(key);
352360
assert_eq!(balance_after_expire, 1);
353361

pallets/governance/tests/curator.rs

Lines changed: 0 additions & 39 deletions
This file was deleted.

pallets/governance/tests/roles.rs

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
use pallet_governance::Curators;
2+
use polkadot_sdk::frame_support::assert_err;
3+
use test_utils::*;
4+
5+
use crate::pallet_governance::Error;
6+
7+
#[test]
8+
fn add_and_remove_curator() {
9+
new_test_ext().execute_with(|| {
10+
let curator_key = 0;
11+
12+
assert_ok!(pallet_governance::Pallet::<Test>::add_curator(
13+
RuntimeOrigin::root(),
14+
curator_key
15+
));
16+
17+
assert!(pallet_governance::Curators::<Test>::contains_key(
18+
curator_key
19+
));
20+
21+
assert_ok!(pallet_governance::Pallet::<Test>::remove_curator(
22+
RuntimeOrigin::root(),
23+
curator_key,
24+
));
25+
26+
assert!(!pallet_governance::Curators::<Test>::contains_key(
27+
curator_key
28+
));
29+
});
30+
}
31+
32+
#[test]
33+
fn add_and_remove_allocator() {
34+
new_test_ext().execute_with(|| {
35+
let allocator_key = 0;
36+
37+
assert_ok!(pallet_governance::Pallet::<Test>::add_allocator(
38+
RuntimeOrigin::root(),
39+
allocator_key
40+
));
41+
42+
assert!(pallet_governance::Allocators::<Test>::contains_key(
43+
allocator_key
44+
));
45+
46+
assert_ok!(pallet_governance::Pallet::<Test>::remove_allocator(
47+
RuntimeOrigin::root(),
48+
allocator_key,
49+
));
50+
51+
assert!(!pallet_governance::Allocators::<Test>::contains_key(
52+
allocator_key
53+
));
54+
});
55+
}
56+
57+
#[test]
58+
fn add_and_remove_from_whitelist() {
59+
new_test_ext().execute_with(|| {
60+
let curator_key = 0;
61+
let module_key = 1;
62+
Curators::<Test>::insert(curator_key, ());
63+
64+
assert_ok!(pallet_governance::Pallet::<Test>::add_to_whitelist(
65+
get_origin(curator_key),
66+
module_key
67+
));
68+
69+
assert_err!(
70+
pallet_governance::Pallet::<Test>::add_to_whitelist(
71+
get_origin(curator_key),
72+
module_key
73+
),
74+
Error::<Test>::AlreadyWhitelisted
75+
);
76+
77+
assert!(pallet_governance::whitelist::is_whitelisted::<Test>(
78+
&module_key
79+
));
80+
81+
assert_ok!(pallet_governance::Pallet::<Test>::remove_from_whitelist(
82+
get_origin(curator_key),
83+
module_key
84+
));
85+
86+
assert_err!(
87+
pallet_governance::Pallet::<Test>::remove_from_whitelist(
88+
get_origin(curator_key),
89+
module_key
90+
),
91+
Error::<Test>::NotWhitelisted
92+
);
93+
94+
assert!(!pallet_governance::whitelist::is_whitelisted::<Test>(
95+
&module_key
96+
));
97+
});
98+
}
99+
100+
#[test]
101+
fn only_curators_can_whitelist() {
102+
new_test_ext().execute_with(|| {
103+
let not_curator_key = 0;
104+
let module_key = 1;
105+
106+
assert_err!(
107+
pallet_governance::Pallet::<Test>::add_to_whitelist(
108+
get_origin(not_curator_key),
109+
module_key
110+
),
111+
Error::<Test>::NotCurator
112+
);
113+
});
114+
}

pallets/governance/tests/voting.rs

Lines changed: 79 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,7 @@ fn delegate(account: u32) {
6666
}
6767

6868
pub fn stake(account: u32, module: u32, stake: u128) {
69-
// if get_balance(account) <= stake {
7069
add_balance(account, stake);
71-
// }
7270

7371
if get_balance(account) - stake < 1 {
7472
add_balance(account, 1);
@@ -136,6 +134,24 @@ fn global_proposal_validates_parameters() {
136134
const KEY: u32 = 0;
137135
add_balance(KEY, to_nano(100_000));
138136

137+
assert_err!(
138+
pallet_governance::Pallet::<Test>::add_global_params_proposal(
139+
get_origin(KEY),
140+
Default::default(),
141+
b"".to_vec(),
142+
),
143+
Error::<Test>::ProposalDataTooSmall
144+
);
145+
146+
assert_err!(
147+
pallet_governance::Pallet::<Test>::add_global_params_proposal(
148+
get_origin(KEY),
149+
Default::default(),
150+
b"1".repeat(257),
151+
),
152+
Error::<Test>::ProposalDataTooLarge
153+
);
154+
139155
let test = |global_params| {
140156
pallet_governance::Pallet::<Test>::add_global_params_proposal(
141157
get_origin(KEY),
@@ -150,6 +166,12 @@ fn global_proposal_validates_parameters() {
150166
})
151167
.expect_err("created proposal with invalid max name length");
152168

169+
test(GlobalParamsData {
170+
proposal_cost: 50_000_000_000_000_000_000_001,
171+
..Default::default()
172+
})
173+
.expect_err("created proposal with invalid proposal cost");
174+
153175
test(GlobalParamsData::default()).expect("failed to create proposal with valid parameters");
154176
});
155177
}
@@ -189,6 +211,57 @@ fn global_custom_proposal_is_accepted_correctly() {
189211
stake_against: to_nano(5),
190212
}
191213
);
214+
215+
step_block(1);
216+
217+
assert_eq!(
218+
Proposals::<Test>::get(0).unwrap().status,
219+
ProposalStatus::Accepted {
220+
block: 100,
221+
stake_for: to_nano(10),
222+
stake_against: to_nano(5),
223+
}
224+
);
225+
});
226+
}
227+
228+
#[test]
229+
fn removes_vote_correctly() {
230+
new_test_ext().execute_with(|| {
231+
zero_min_burn();
232+
233+
const FOR: u32 = 0;
234+
const AGAINST: u32 = 1;
235+
236+
register(FOR, 0, 0, to_nano(10));
237+
register(AGAINST, 0, 1, to_nano(10));
238+
239+
config(1, 100);
240+
241+
assert_ok!(
242+
pallet_governance::Pallet::<Test>::add_global_custom_proposal(
243+
get_origin(FOR),
244+
b"metadata".to_vec()
245+
)
246+
);
247+
248+
vote(FOR, 0, true);
249+
vote(AGAINST, 0, false);
250+
251+
pallet_governance::voting::remove_vote::<Test>(FOR, 0).unwrap();
252+
pallet_governance::voting::remove_vote::<Test>(AGAINST, 0).unwrap();
253+
254+
match pallet_governance::Proposals::<Test>::get(0).unwrap().status {
255+
ProposalStatus::Open {
256+
votes_for,
257+
votes_against,
258+
..
259+
} => {
260+
assert!(!votes_for.contains(&FOR));
261+
assert!(!votes_against.contains(&AGAINST));
262+
}
263+
_ => unreachable!(),
264+
}
192265
});
193266
}
194267

@@ -267,20 +340,19 @@ fn global_proposals_counts_delegated_stake() {
267340
const FOR: u32 = 0;
268341
const AGAINST: u32 = 1;
269342
const FOR_DELEGATED: u32 = 2;
270-
const AGAINST_DELEGATED: u32 = 3;
271343

272344
let origin = get_origin(0);
273345

274346
register(FOR, 0, FOR, to_nano(5));
275-
// delegate(FOR);
276347
register(AGAINST, 0, AGAINST, to_nano(10));
277-
// delegate(AGAINST);
278348

279349
stake(FOR_DELEGATED, FOR, to_nano(10));
280350
delegate(FOR_DELEGATED);
281351

282-
stake(AGAINST_DELEGATED, AGAINST, to_nano(3));
283-
delegate(AGAINST_DELEGATED);
352+
// AGAINST does not delegate voting power, so it doesn't matter
353+
// to who it stakes.
354+
stake(AGAINST, FOR, to_nano(3));
355+
pallet_governance::voting::disable_delegation::<Test>(AGAINST).unwrap();
284356

285357
config(1, 100);
286358

0 commit comments

Comments
 (0)