From 9dc913ccddaf1ba43f62f63d917b5696ab7d5bc8 Mon Sep 17 00:00:00 2001 From: Nazar Mokrynskyi Date: Thu, 12 Oct 2023 14:18:10 +0300 Subject: [PATCH 1/3] Fix vote quality check --- crates/pallet-subspace/src/lib.rs | 6 ++- crates/pallet-subspace/src/tests.rs | 57 ++++++++++++++++++++--------- 2 files changed, 44 insertions(+), 19 deletions(-) diff --git a/crates/pallet-subspace/src/lib.rs b/crates/pallet-subspace/src/lib.rs index df4d4151c6..e7dfe1f25c 100644 --- a/crates/pallet-subspace/src/lib.rs +++ b/crates/pallet-subspace/src/lib.rs @@ -1558,7 +1558,11 @@ fn check_vote( .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); } } diff --git a/crates/pallet-subspace/src/tests.rs b/crates/pallet-subspace/src/tests.rs index f55c844934..adc440c259 100644 --- a/crates/pallet-subspace/src/tests.rs +++ b/crates/pallet-subspace/src/tests.rs @@ -1018,28 +1018,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::::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::::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::::block_hash(1), + Subspace::current_slot() + 1, + Default::default(), + Default::default(), + &archived_segment.pieces, + 1, + pallet::SolutionRanges::::get().current, + pallet::SolutionRanges::::get().voting_current, + ); - assert_matches!( - super::check_vote::(&signed_vote, false), - Err(CheckVoteError::QualityTooHigh) - ); + // Good solution quality + assert_ok!(super::check_vote::(&signed_vote, false)); + } + + { + let signed_vote = create_signed_vote( + &keypair, + 2, + frame_system::Pallet::::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::::get().current, + ); + + // Quality is too high + assert_matches!( + super::check_vote::(&signed_vote, false), + Err(CheckVoteError::QualityTooHigh) + ); + } }); } From a9edaead0e2f0e3d9c6e83507680cb4b9ea39566 Mon Sep 17 00:00:00 2001 From: Nazar Mokrynskyi Date: Thu, 12 Oct 2023 14:18:42 +0300 Subject: [PATCH 2/3] Update runtime version --- crates/subspace-runtime/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/subspace-runtime/src/lib.rs b/crates/subspace-runtime/src/lib.rs index 267993a488..702ef1fc29 100644 --- a/crates/subspace-runtime/src/lib.rs +++ b/crates/subspace-runtime/src/lib.rs @@ -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, From 1484eee91707f883b7939b8eeaf1beb8a05d652e Mon Sep 17 00:00:00 2001 From: Nazar Mokrynskyi Date: Thu, 12 Oct 2023 18:09:44 +0300 Subject: [PATCH 3/3] Fix flaky vote tests --- crates/pallet-subspace/src/tests.rs | 70 ++++++++++++++++------------- 1 file changed, 39 insertions(+), 31 deletions(-) diff --git a/crates/pallet-subspace/src/tests.rs b/crates/pallet-subspace/src/tests.rs index adc440c259..a76fdbf535 100644 --- a/crates/pallet-subspace/src/tests.rs +++ b/crates/pallet-subspace/src/tests.rs @@ -768,7 +768,8 @@ fn vote_past_future_slot() { ); // Reset so that any solution works for votes - crate::pallet::SolutionRanges::::mutate(|solution_ranges| { + pallet::SolutionRanges::::mutate(|solution_ranges| { + solution_ranges.current = u64::MIN; solution_ranges.voting_current = u64::MAX; }); @@ -785,8 +786,8 @@ fn vote_past_future_slot() { Default::default(), &archived_segment.pieces, 1, - SolutionRange::MIN, - SolutionRange::MAX, + pallet::SolutionRanges::::get().current, + pallet::SolutionRanges::::get().voting_current, ); assert_err!( @@ -810,8 +811,8 @@ fn vote_past_future_slot() { Default::default(), &archived_segment.pieces, 1, - SolutionRange::MIN, - SolutionRange::MAX, + pallet::SolutionRanges::::get().current, + pallet::SolutionRanges::::get().voting_current, ); assert_err!( @@ -834,8 +835,8 @@ fn vote_past_future_slot() { Default::default(), &archived_segment.pieces, 1, - SolutionRange::MIN, - SolutionRange::MAX, + pallet::SolutionRanges::::get().current, + pallet::SolutionRanges::::get().voting_current, ); assert_ok!(super::check_vote::(&signed_vote, false)); @@ -860,6 +861,7 @@ fn vote_same_slot() { // Reset so that any solution works for votes pallet::SolutionRanges::::mutate(|solution_ranges| { + solution_ranges.current = u64::MIN; solution_ranges.voting_current = u64::MAX; }); @@ -875,8 +877,8 @@ fn vote_same_slot() { Default::default(), &archived_segment.pieces, 1, - SolutionRange::MIN, - SolutionRange::MAX, + pallet::SolutionRanges::::get().current, + pallet::SolutionRanges::::get().voting_current, ); assert_ok!(super::check_vote::(&signed_vote, true)); @@ -895,8 +897,8 @@ fn vote_same_slot() { Default::default(), &archived_segment.pieces, 1, - SolutionRange::MIN, - SolutionRange::MAX, + pallet::SolutionRanges::::get().current, + pallet::SolutionRanges::::get().voting_current, ); assert_err!( @@ -1091,6 +1093,7 @@ fn vote_invalid_proof_of_time() { // Reset so that any solution works for votes pallet::SolutionRanges::::mutate(|solution_ranges| { + solution_ranges.current = u64::MIN; solution_ranges.voting_current = u64::MAX; }); @@ -1114,8 +1117,8 @@ fn vote_invalid_proof_of_time() { Default::default(), &archived_segment.pieces, 1, - SolutionRange::MIN, - SolutionRange::MAX, + pallet::SolutionRanges::::get().current, + pallet::SolutionRanges::::get().voting_current, ); assert_err!( @@ -1142,8 +1145,8 @@ fn vote_invalid_proof_of_time() { Default::default(), &archived_segment.pieces, 1, - SolutionRange::MIN, - SolutionRange::MAX, + pallet::SolutionRanges::::get().current, + pallet::SolutionRanges::::get().voting_current, ); assert_ok!(super::check_vote::(&signed_vote, false)); @@ -1160,8 +1163,8 @@ fn vote_invalid_proof_of_time() { Default::default(), &archived_segment.pieces, 1, - SolutionRange::MIN, - SolutionRange::MAX, + pallet::SolutionRanges::::get().current, + pallet::SolutionRanges::::get().voting_current, ); assert_err!( @@ -1188,8 +1191,8 @@ fn vote_invalid_proof_of_time() { Default::default(), &archived_segment.pieces, 1, - SolutionRange::MIN, - SolutionRange::MAX, + pallet::SolutionRanges::::get().current, + pallet::SolutionRanges::::get().voting_current, ); assert_err!( @@ -1216,8 +1219,8 @@ fn vote_invalid_proof_of_time() { test_future_proof_of_time, &archived_segment.pieces, 1, - SolutionRange::MIN, - SolutionRange::MAX, + pallet::SolutionRanges::::get().current, + pallet::SolutionRanges::::get().voting_current, ); assert_ok!(super::check_vote::(&signed_vote, true)); @@ -1240,6 +1243,7 @@ fn vote_correct_signature() { // Reset so that any solution works for votes pallet::SolutionRanges::::mutate(|solution_ranges| { + solution_ranges.current = u64::MIN; solution_ranges.voting_current = u64::MAX; }); @@ -1253,8 +1257,8 @@ fn vote_correct_signature() { Default::default(), &archived_segment.pieces, 1, - SolutionRange::MIN, - SolutionRange::MAX, + pallet::SolutionRanges::::get().current, + pallet::SolutionRanges::::get().voting_current, ); assert_ok!(super::check_vote::(&signed_vote, false)); @@ -1276,6 +1280,7 @@ fn vote_equivocation_current_block_plus_vote() { // Reset so that any solution works for votes pallet::SolutionRanges::::mutate(|solution_ranges| { + solution_ranges.current = u64::MIN; solution_ranges.voting_current = u64::MAX; }); @@ -1291,8 +1296,8 @@ fn vote_equivocation_current_block_plus_vote() { Default::default(), &archived_segment.pieces, reward_address, - SolutionRange::MIN, - SolutionRange::MAX, + pallet::SolutionRanges::::get().current, + pallet::SolutionRanges::::get().voting_current, ); // Parent block author + sector index + chunk + audit chunk index + slot matches that of the @@ -1332,6 +1337,7 @@ fn vote_equivocation_parent_block_plus_vote() { // Reset so that any solution works for votes pallet::SolutionRanges::::mutate(|solution_ranges| { + solution_ranges.current = u64::MIN; solution_ranges.voting_current = u64::MAX; }); @@ -1347,8 +1353,8 @@ fn vote_equivocation_parent_block_plus_vote() { Default::default(), &archived_segment.pieces, reward_address, - SolutionRange::MIN, - SolutionRange::MAX, + pallet::SolutionRanges::::get().current, + pallet::SolutionRanges::::get().voting_current, ); // Parent block author + sector index + chunk + audit chunk index + slot matches that of the @@ -1396,6 +1402,7 @@ fn vote_equivocation_current_voters_duplicate() { // Reset so that any solution works for votes pallet::SolutionRanges::::mutate(|solution_ranges| { + solution_ranges.current = u64::MIN; solution_ranges.voting_current = u64::MAX; }); @@ -1413,8 +1420,8 @@ fn vote_equivocation_current_voters_duplicate() { Default::default(), &archived_segment.pieces, reward_address, - SolutionRange::MIN, - SolutionRange::MAX, + pallet::SolutionRanges::::get().current, + pallet::SolutionRanges::::get().voting_current, ); CurrentBlockVoters::::put({ @@ -1482,6 +1489,7 @@ fn vote_equivocation_parent_voters_duplicate() { // Reset so that any solution works for votes pallet::SolutionRanges::::mutate(|solution_ranges| { + solution_ranges.current = u64::MIN; solution_ranges.voting_current = u64::MAX; }); @@ -1498,8 +1506,8 @@ fn vote_equivocation_parent_voters_duplicate() { Default::default(), &archived_segment.pieces, reward_address, - SolutionRange::MIN, - SolutionRange::MAX, + pallet::SolutionRanges::::get().current, + pallet::SolutionRanges::::get().voting_current, ); ParentBlockVoters::::put({