Skip to content

Commit

Permalink
Merge pull request #2094 from subspace/fix-vote-quality-check
Browse files Browse the repository at this point in the history
Fix vote quality check
  • Loading branch information
nazar-pc authored Oct 12, 2023
2 parents 14b552d + 1484eee commit a81c999
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 51 deletions.
6 changes: 5 additions & 1 deletion crates/pallet-subspace/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1558,7 +1558,11 @@ fn check_vote<T: Config>(
.into(),
) {
Ok(solution_distance) => {
if solution_distance <= vote_verification_data.solution_range {
if solution_distance <= vote_verification_data.solution_range / 2 {
debug!(
target: "runtime::subspace",
"Vote quality is too high"
);
return Err(CheckVoteError::QualityTooHigh);
}
}
Expand Down
127 changes: 78 additions & 49 deletions crates/pallet-subspace/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -768,7 +768,8 @@ fn vote_past_future_slot() {
);

// Reset so that any solution works for votes
crate::pallet::SolutionRanges::<Test>::mutate(|solution_ranges| {
pallet::SolutionRanges::<Test>::mutate(|solution_ranges| {
solution_ranges.current = u64::MIN;
solution_ranges.voting_current = u64::MAX;
});

Expand All @@ -785,8 +786,8 @@ fn vote_past_future_slot() {
Default::default(),
&archived_segment.pieces,
1,
SolutionRange::MIN,
SolutionRange::MAX,
pallet::SolutionRanges::<Test>::get().current,
pallet::SolutionRanges::<Test>::get().voting_current,
);

assert_err!(
Expand All @@ -810,8 +811,8 @@ fn vote_past_future_slot() {
Default::default(),
&archived_segment.pieces,
1,
SolutionRange::MIN,
SolutionRange::MAX,
pallet::SolutionRanges::<Test>::get().current,
pallet::SolutionRanges::<Test>::get().voting_current,
);

assert_err!(
Expand All @@ -834,8 +835,8 @@ fn vote_past_future_slot() {
Default::default(),
&archived_segment.pieces,
1,
SolutionRange::MIN,
SolutionRange::MAX,
pallet::SolutionRanges::<Test>::get().current,
pallet::SolutionRanges::<Test>::get().voting_current,
);

assert_ok!(super::check_vote::<Test>(&signed_vote, false));
Expand All @@ -860,6 +861,7 @@ fn vote_same_slot() {

// Reset so that any solution works for votes
pallet::SolutionRanges::<Test>::mutate(|solution_ranges| {
solution_ranges.current = u64::MIN;
solution_ranges.voting_current = u64::MAX;
});

Expand All @@ -875,8 +877,8 @@ fn vote_same_slot() {
Default::default(),
&archived_segment.pieces,
1,
SolutionRange::MIN,
SolutionRange::MAX,
pallet::SolutionRanges::<Test>::get().current,
pallet::SolutionRanges::<Test>::get().voting_current,
);

assert_ok!(super::check_vote::<Test>(&signed_vote, true));
Expand All @@ -895,8 +897,8 @@ fn vote_same_slot() {
Default::default(),
&archived_segment.pieces,
1,
SolutionRange::MIN,
SolutionRange::MAX,
pallet::SolutionRanges::<Test>::get().current,
pallet::SolutionRanges::<Test>::get().voting_current,
);

assert_err!(
Expand Down Expand Up @@ -1018,28 +1020,49 @@ fn vote_solution_quality_too_high() {
// Reset so that any solution works for votes, but also block solution range is almost the
// same, resulting in quality being too high
pallet::SolutionRanges::<Test>::mutate(|solution_ranges| {
solution_ranges.current = u64::MAX - 1;
solution_ranges.current = u64::MAX / 5;
solution_ranges.voting_current = u64::MAX;
});

// Finally correct signature
let signed_vote = create_signed_vote(
&keypair,
2,
frame_system::Pallet::<Test>::block_hash(1),
Subspace::current_slot() + 1,
Default::default(),
Default::default(),
&archived_segment.pieces,
1,
SolutionRange::MIN,
SolutionRange::MAX,
);
{
let signed_vote = create_signed_vote(
&keypair,
2,
frame_system::Pallet::<Test>::block_hash(1),
Subspace::current_slot() + 1,
Default::default(),
Default::default(),
&archived_segment.pieces,
1,
pallet::SolutionRanges::<Test>::get().current,
pallet::SolutionRanges::<Test>::get().voting_current,
);

assert_matches!(
super::check_vote::<Test>(&signed_vote, false),
Err(CheckVoteError::QualityTooHigh)
);
// Good solution quality
assert_ok!(super::check_vote::<Test>(&signed_vote, false));
}

{
let signed_vote = create_signed_vote(
&keypair,
2,
frame_system::Pallet::<Test>::block_hash(1),
Subspace::current_slot() + 1,
Default::default(),
Default::default(),
&archived_segment.pieces,
1,
SolutionRange::MIN,
// Create vote for block level of quality
pallet::SolutionRanges::<Test>::get().current,
);

// Quality is too high
assert_matches!(
super::check_vote::<Test>(&signed_vote, false),
Err(CheckVoteError::QualityTooHigh)
);
}
});
}

Expand Down Expand Up @@ -1070,6 +1093,7 @@ fn vote_invalid_proof_of_time() {

// Reset so that any solution works for votes
pallet::SolutionRanges::<Test>::mutate(|solution_ranges| {
solution_ranges.current = u64::MIN;
solution_ranges.voting_current = u64::MAX;
});

Expand All @@ -1093,8 +1117,8 @@ fn vote_invalid_proof_of_time() {
Default::default(),
&archived_segment.pieces,
1,
SolutionRange::MIN,
SolutionRange::MAX,
pallet::SolutionRanges::<Test>::get().current,
pallet::SolutionRanges::<Test>::get().voting_current,
);

assert_err!(
Expand All @@ -1121,8 +1145,8 @@ fn vote_invalid_proof_of_time() {
Default::default(),
&archived_segment.pieces,
1,
SolutionRange::MIN,
SolutionRange::MAX,
pallet::SolutionRanges::<Test>::get().current,
pallet::SolutionRanges::<Test>::get().voting_current,
);

assert_ok!(super::check_vote::<Test>(&signed_vote, false));
Expand All @@ -1139,8 +1163,8 @@ fn vote_invalid_proof_of_time() {
Default::default(),
&archived_segment.pieces,
1,
SolutionRange::MIN,
SolutionRange::MAX,
pallet::SolutionRanges::<Test>::get().current,
pallet::SolutionRanges::<Test>::get().voting_current,
);

assert_err!(
Expand All @@ -1167,8 +1191,8 @@ fn vote_invalid_proof_of_time() {
Default::default(),
&archived_segment.pieces,
1,
SolutionRange::MIN,
SolutionRange::MAX,
pallet::SolutionRanges::<Test>::get().current,
pallet::SolutionRanges::<Test>::get().voting_current,
);

assert_err!(
Expand All @@ -1195,8 +1219,8 @@ fn vote_invalid_proof_of_time() {
test_future_proof_of_time,
&archived_segment.pieces,
1,
SolutionRange::MIN,
SolutionRange::MAX,
pallet::SolutionRanges::<Test>::get().current,
pallet::SolutionRanges::<Test>::get().voting_current,
);

assert_ok!(super::check_vote::<Test>(&signed_vote, true));
Expand All @@ -1219,6 +1243,7 @@ fn vote_correct_signature() {

// Reset so that any solution works for votes
pallet::SolutionRanges::<Test>::mutate(|solution_ranges| {
solution_ranges.current = u64::MIN;
solution_ranges.voting_current = u64::MAX;
});

Expand All @@ -1232,8 +1257,8 @@ fn vote_correct_signature() {
Default::default(),
&archived_segment.pieces,
1,
SolutionRange::MIN,
SolutionRange::MAX,
pallet::SolutionRanges::<Test>::get().current,
pallet::SolutionRanges::<Test>::get().voting_current,
);

assert_ok!(super::check_vote::<Test>(&signed_vote, false));
Expand All @@ -1255,6 +1280,7 @@ fn vote_equivocation_current_block_plus_vote() {

// Reset so that any solution works for votes
pallet::SolutionRanges::<Test>::mutate(|solution_ranges| {
solution_ranges.current = u64::MIN;
solution_ranges.voting_current = u64::MAX;
});

Expand All @@ -1270,8 +1296,8 @@ fn vote_equivocation_current_block_plus_vote() {
Default::default(),
&archived_segment.pieces,
reward_address,
SolutionRange::MIN,
SolutionRange::MAX,
pallet::SolutionRanges::<Test>::get().current,
pallet::SolutionRanges::<Test>::get().voting_current,
);

// Parent block author + sector index + chunk + audit chunk index + slot matches that of the
Expand Down Expand Up @@ -1311,6 +1337,7 @@ fn vote_equivocation_parent_block_plus_vote() {

// Reset so that any solution works for votes
pallet::SolutionRanges::<Test>::mutate(|solution_ranges| {
solution_ranges.current = u64::MIN;
solution_ranges.voting_current = u64::MAX;
});

Expand All @@ -1326,8 +1353,8 @@ fn vote_equivocation_parent_block_plus_vote() {
Default::default(),
&archived_segment.pieces,
reward_address,
SolutionRange::MIN,
SolutionRange::MAX,
pallet::SolutionRanges::<Test>::get().current,
pallet::SolutionRanges::<Test>::get().voting_current,
);

// Parent block author + sector index + chunk + audit chunk index + slot matches that of the
Expand Down Expand Up @@ -1375,6 +1402,7 @@ fn vote_equivocation_current_voters_duplicate() {

// Reset so that any solution works for votes
pallet::SolutionRanges::<Test>::mutate(|solution_ranges| {
solution_ranges.current = u64::MIN;
solution_ranges.voting_current = u64::MAX;
});

Expand All @@ -1392,8 +1420,8 @@ fn vote_equivocation_current_voters_duplicate() {
Default::default(),
&archived_segment.pieces,
reward_address,
SolutionRange::MIN,
SolutionRange::MAX,
pallet::SolutionRanges::<Test>::get().current,
pallet::SolutionRanges::<Test>::get().voting_current,
);

CurrentBlockVoters::<Test>::put({
Expand Down Expand Up @@ -1461,6 +1489,7 @@ fn vote_equivocation_parent_voters_duplicate() {

// Reset so that any solution works for votes
pallet::SolutionRanges::<Test>::mutate(|solution_ranges| {
solution_ranges.current = u64::MIN;
solution_ranges.voting_current = u64::MAX;
});

Expand All @@ -1477,8 +1506,8 @@ fn vote_equivocation_parent_voters_duplicate() {
Default::default(),
&archived_segment.pieces,
reward_address,
SolutionRange::MIN,
SolutionRange::MAX,
pallet::SolutionRanges::<Test>::get().current,
pallet::SolutionRanges::<Test>::get().voting_current,
);

ParentBlockVoters::<Test>::put({
Expand Down
2 changes: 1 addition & 1 deletion crates/subspace-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: create_runtime_str!("subspace"),
impl_name: create_runtime_str!("subspace"),
authoring_version: 0,
spec_version: 1,
spec_version: 2,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 0,
Expand Down

0 comments on commit a81c999

Please sign in to comment.