Skip to content

Commit

Permalink
Merge pull request #3336 from autonomys/take-reward-of-correct-offend…
Browse files Browse the repository at this point in the history
…er-voter

Take reward of correct offending voter
  • Loading branch information
nazar-pc authored Jan 6, 2025
2 parents f9b1769 + 042a9de commit c1d6159
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
2 changes: 1 addition & 1 deletion crates/pallet-subspace/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1678,7 +1678,7 @@ fn check_vote<T: Config>(
(reward_address, _signature),
) in current_reward_receivers.iter_mut()
{
if public_key != &offender {
if public_key == &offender {
// Revoke reward if assigned in current block.
reward_address.take();
}
Expand Down
45 changes: 39 additions & 6 deletions crates/pallet-subspace/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1122,8 +1122,10 @@ fn vote_equivocation_current_voters_duplicate() {

// Current block author + slot matches that of the vote
let voter_keypair = Keypair::generate();
let other_voter_keypair = Keypair::generate();
let slot = Subspace::current_slot();
let reward_address = 0;
let other_reward_address = 1;

let signed_vote = create_signed_vote(
&voter_keypair,
Expand Down Expand Up @@ -1153,15 +1155,43 @@ fn vote_equivocation_current_voters_duplicate() {
map
});

// Insert another voter that is not equivocating
{
let other_signed_vote = create_signed_vote(
&other_voter_keypair,
2,
frame_system::Pallet::<Test>::block_hash(1),
slot,
Default::default(),
Default::default(),
archived_segment,
other_reward_address,
pallet::SolutionRanges::<Test>::get().current,
pallet::SolutionRanges::<Test>::get().voting_current,
);

CurrentBlockVoters::<Test>::mutate(|map| {
map.as_mut().unwrap().insert(
(
PublicKey::from(other_voter_keypair.public.to_bytes()),
other_signed_vote.vote.solution().sector_index,
other_signed_vote.vote.solution().piece_offset,
other_signed_vote.vote.solution().chunk,
slot,
),
(Some(other_reward_address), other_signed_vote.signature),
);
});
}

// Identical vote submitted twice leads to duplicate error
assert_err!(
super::check_vote::<Test>(&signed_vote, true),
CheckVoteError::DuplicateVote
);

CurrentBlockVoters::<Test>::put({
let mut map = BTreeMap::new();
map.insert(
CurrentBlockVoters::<Test>::mutate(|map| {
map.as_mut().unwrap().insert(
(
PublicKey::from(voter_keypair.public.to_bytes()),
signed_vote.vote.solution().sector_index,
Expand All @@ -1171,14 +1201,17 @@ fn vote_equivocation_current_voters_duplicate() {
),
(Some(reward_address), RewardSignature::from([0; 64])),
);
map
});

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

// Voter doesn't get reward after equivocation
assert_eq!(Subspace::find_voting_reward_addresses().len(), 0);
// Equivocating voter doesn't get reward, but the other voter does
assert_eq!(Subspace::find_voting_reward_addresses().len(), 1);
assert_eq!(
Subspace::find_voting_reward_addresses().first().unwrap(),
&other_reward_address
);
});
}

Expand Down

0 comments on commit c1d6159

Please sign in to comment.