Skip to content

Commit c1d6159

Browse files
authored
Merge pull request #3336 from autonomys/take-reward-of-correct-offender-voter
Take reward of correct offending voter
2 parents f9b1769 + 042a9de commit c1d6159

File tree

2 files changed

+40
-7
lines changed

2 files changed

+40
-7
lines changed

crates/pallet-subspace/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1678,7 +1678,7 @@ fn check_vote<T: Config>(
16781678
(reward_address, _signature),
16791679
) in current_reward_receivers.iter_mut()
16801680
{
1681-
if public_key != &offender {
1681+
if public_key == &offender {
16821682
// Revoke reward if assigned in current block.
16831683
reward_address.take();
16841684
}

crates/pallet-subspace/src/tests.rs

+39-6
Original file line numberDiff line numberDiff line change
@@ -1122,8 +1122,10 @@ fn vote_equivocation_current_voters_duplicate() {
11221122

11231123
// Current block author + slot matches that of the vote
11241124
let voter_keypair = Keypair::generate();
1125+
let other_voter_keypair = Keypair::generate();
11251126
let slot = Subspace::current_slot();
11261127
let reward_address = 0;
1128+
let other_reward_address = 1;
11271129

11281130
let signed_vote = create_signed_vote(
11291131
&voter_keypair,
@@ -1153,15 +1155,43 @@ fn vote_equivocation_current_voters_duplicate() {
11531155
map
11541156
});
11551157

1158+
// Insert another voter that is not equivocating
1159+
{
1160+
let other_signed_vote = create_signed_vote(
1161+
&other_voter_keypair,
1162+
2,
1163+
frame_system::Pallet::<Test>::block_hash(1),
1164+
slot,
1165+
Default::default(),
1166+
Default::default(),
1167+
archived_segment,
1168+
other_reward_address,
1169+
pallet::SolutionRanges::<Test>::get().current,
1170+
pallet::SolutionRanges::<Test>::get().voting_current,
1171+
);
1172+
1173+
CurrentBlockVoters::<Test>::mutate(|map| {
1174+
map.as_mut().unwrap().insert(
1175+
(
1176+
PublicKey::from(other_voter_keypair.public.to_bytes()),
1177+
other_signed_vote.vote.solution().sector_index,
1178+
other_signed_vote.vote.solution().piece_offset,
1179+
other_signed_vote.vote.solution().chunk,
1180+
slot,
1181+
),
1182+
(Some(other_reward_address), other_signed_vote.signature),
1183+
);
1184+
});
1185+
}
1186+
11561187
// Identical vote submitted twice leads to duplicate error
11571188
assert_err!(
11581189
super::check_vote::<Test>(&signed_vote, true),
11591190
CheckVoteError::DuplicateVote
11601191
);
11611192

1162-
CurrentBlockVoters::<Test>::put({
1163-
let mut map = BTreeMap::new();
1164-
map.insert(
1193+
CurrentBlockVoters::<Test>::mutate(|map| {
1194+
map.as_mut().unwrap().insert(
11651195
(
11661196
PublicKey::from(voter_keypair.public.to_bytes()),
11671197
signed_vote.vote.solution().sector_index,
@@ -1171,14 +1201,17 @@ fn vote_equivocation_current_voters_duplicate() {
11711201
),
11721202
(Some(reward_address), RewardSignature::from([0; 64])),
11731203
);
1174-
map
11751204
});
11761205

11771206
// Different vote for the same sector index and time slot leads to equivocation
11781207
Subspace::pre_dispatch_vote(&signed_vote).unwrap();
11791208

1180-
// Voter doesn't get reward after equivocation
1181-
assert_eq!(Subspace::find_voting_reward_addresses().len(), 0);
1209+
// Equivocating voter doesn't get reward, but the other voter does
1210+
assert_eq!(Subspace::find_voting_reward_addresses().len(), 1);
1211+
assert_eq!(
1212+
Subspace::find_voting_reward_addresses().first().unwrap(),
1213+
&other_reward_address
1214+
);
11821215
});
11831216
}
11841217

0 commit comments

Comments
 (0)