Skip to content

Commit

Permalink
Merge pull request #3057 from autonomys/refactor-public-key-signature
Browse files Browse the repository at this point in the history
Refactor public key and signature types
  • Loading branch information
nazar-pc committed Sep 24, 2024
2 parents 476b081 + 8d465b0 commit 53b9b9e
Show file tree
Hide file tree
Showing 45 changed files with 405 additions and 584 deletions.
5 changes: 2 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions crates/pallet-offences-subspace/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ scale-info = { version = "2.11.2", default-features = false, features = ["derive
sp-consensus-subspace = { version = "0.1.0", default-features = false, path = "../sp-consensus-subspace" }
sp-runtime = { default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "5626154d0781ac9a6ffd5a6207ed237f425ae631" }
sp-std = { default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "5626154d0781ac9a6ffd5a6207ed237f425ae631" }
subspace-core-primitives = { version = "0.1.0", default-features = false, path = "../subspace-core-primitives" }

[dev-dependencies]
sp-io = { git = "https://github.com/subspace/polkadot-sdk", rev = "5626154d0781ac9a6ffd5a6207ed237f425ae631" }
Expand All @@ -36,5 +37,6 @@ std = [
"sp-consensus-subspace/std",
"sp-runtime/std",
"sp-std/std",
"subspace-core-primitives/std",
]
try-runtime = ["frame-support/try-runtime"]
26 changes: 13 additions & 13 deletions crates/pallet-offences-subspace/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ pub use pallet::*;
use sp_consensus_subspace::offence::{
Offence, OffenceDetails, OffenceError, OnOffenceHandler, ReportOffence,
};
use sp_consensus_subspace::FarmerPublicKey;
use sp_runtime::traits::Hash;
use sp_std::prelude::*;
use subspace_core_primitives::PublicKey;

/// A binary blob which represents a SCALE codec-encoded `O::TimeSlot`.
type OpaqueTimeSlot = Vec<u8>;
Expand All @@ -44,8 +44,8 @@ mod pallet {
use super::{OpaqueTimeSlot, ReportIdOf};
use frame_support::pallet_prelude::*;
use sp_consensus_subspace::offence::{Kind, OffenceDetails, OnOffenceHandler};
use sp_consensus_subspace::FarmerPublicKey;
use sp_std::prelude::*;
use subspace_core_primitives::PublicKey;

#[pallet::pallet]
#[pallet::without_storage_info]
Expand All @@ -57,14 +57,14 @@ mod pallet {
/// The overarching event type.
type RuntimeEvent: From<Event> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
/// A handler called for every offence report.
type OnOffenceHandler: OnOffenceHandler<FarmerPublicKey>;
type OnOffenceHandler: OnOffenceHandler<PublicKey>;
}

/// The primary structure that holds all offence records keyed by report identifiers.
#[pallet::storage]
#[pallet::getter(fn reports)]
pub type Reports<T: Config> =
StorageMap<_, Twox64Concat, ReportIdOf<T>, OffenceDetails<FarmerPublicKey>>;
StorageMap<_, Twox64Concat, ReportIdOf<T>, OffenceDetails<PublicKey>>;

/// A vector of reports of the same kind that happened at the same time slot.
#[pallet::storage]
Expand Down Expand Up @@ -107,7 +107,7 @@ mod pallet {
}
}

impl<T: Config, O: Offence<FarmerPublicKey>> ReportOffence<FarmerPublicKey, O> for Pallet<T> {
impl<T: Config, O: Offence<PublicKey>> ReportOffence<PublicKey, O> for Pallet<T> {
fn report_offence(offence: O) -> Result<(), OffenceError> {
let offenders = offence.offenders();
let time_slot = offence.time_slot();
Expand All @@ -133,7 +133,7 @@ impl<T: Config, O: Offence<FarmerPublicKey>> ReportOffence<FarmerPublicKey, O> f
Ok(())
}

fn is_known_offence(offenders: &[FarmerPublicKey], time_slot: &O::TimeSlot) -> bool {
fn is_known_offence(offenders: &[PublicKey], time_slot: &O::TimeSlot) -> bool {
let any_unknown = offenders.iter().any(|offender| {
let report_id = Self::report_id::<O>(time_slot, offender);
!<Reports<T>>::contains_key(report_id)
Expand All @@ -147,18 +147,18 @@ impl<T: Config> Pallet<T> {
/// Compute the ID for the given report properties.
///
/// The report id depends on the offence kind, time slot and the id of offender.
fn report_id<O: Offence<FarmerPublicKey>>(
fn report_id<O: Offence<PublicKey>>(
time_slot: &O::TimeSlot,
offender: &FarmerPublicKey,
offender: &PublicKey,
) -> ReportIdOf<T> {
(O::ID, time_slot.encode(), offender).using_encoded(T::Hashing::hash)
}

/// Triages the offence report and returns the set of offenders that was involved in unique
/// reports along with the list of the concurrent offences.
fn triage_offence_report<O: Offence<FarmerPublicKey>>(
fn triage_offence_report<O: Offence<PublicKey>>(
time_slot: &O::TimeSlot,
offenders: Vec<FarmerPublicKey>,
offenders: Vec<PublicKey>,
) -> Option<TriageOutcome> {
let mut storage = ReportIndexStorage::<T, O>::load(time_slot);

Expand Down Expand Up @@ -195,7 +195,7 @@ impl<T: Config> Pallet<T> {

struct TriageOutcome {
/// Other reports for the same report kinds.
concurrent_offenders: Vec<OffenceDetails<FarmerPublicKey>>,
concurrent_offenders: Vec<OffenceDetails<PublicKey>>,
}

/// An auxiliary struct for working with storage of indexes localized for a specific offence
Expand All @@ -204,13 +204,13 @@ struct TriageOutcome {
/// This struct is responsible for aggregating storage writes and the underlying storage should not
/// accessed directly meanwhile.
#[must_use = "The changes are not saved without called `save`"]
struct ReportIndexStorage<T: Config, O: Offence<FarmerPublicKey>> {
struct ReportIndexStorage<T: Config, O: Offence<PublicKey>> {
opaque_time_slot: OpaqueTimeSlot,
concurrent_reports: Vec<ReportIdOf<T>>,
same_kind_reports: Vec<(O::TimeSlot, ReportIdOf<T>)>,
}

impl<T: Config, O: Offence<FarmerPublicKey>> ReportIndexStorage<T, O> {
impl<T: Config, O: Offence<PublicKey>> ReportIndexStorage<T, O> {
/// Preload indexes from the storage for the specific `time_slot` and the kind of the offence.
fn load(time_slot: &O::TimeSlot) -> Self {
let opaque_time_slot = time_slot.encode();
Expand Down
8 changes: 4 additions & 4 deletions crates/pallet-offences-subspace/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ use frame_support::derive_impl;
use frame_support::weights::constants::ParityDbWeight;
use frame_support::weights::Weight;
use sp_consensus_subspace::offence::{self, Kind, OffenceDetails};
use sp_consensus_subspace::FarmerPublicKey;
use sp_core::H256;
use sp_runtime::{BuildStorage, Perbill};
use std::cell::RefCell;
use subspace_core_primitives::PublicKey;

pub struct OnOffenceHandler;

Expand Down Expand Up @@ -81,7 +81,7 @@ pub fn new_test_ext() -> sp_io::TestExternalities {
pub const KIND: [u8; 16] = *b"test_report_1234";

/// Returns all offence details for the specific `kind` happened at the specific time slot.
pub fn offence_reports(kind: Kind, time_slot: u128) -> Vec<OffenceDetails<FarmerPublicKey>> {
pub fn offence_reports(kind: Kind, time_slot: u128) -> Vec<OffenceDetails<PublicKey>> {
<crate::ConcurrentReportsIndex<Runtime>>::get(kind, time_slot.encode())
.into_iter()
.map(|report_id| {
Expand Down Expand Up @@ -111,6 +111,6 @@ impl<T: Clone> offence::Offence<T> for Offence<T> {
}

/// Create the report id for the given `offender` and `time_slot` combination.
pub fn report_id(time_slot: u128, offender: FarmerPublicKey) -> H256 {
OffencesSubspace::report_id::<Offence<FarmerPublicKey>>(&time_slot, &offender)
pub fn report_id(time_slot: u128, offender: PublicKey) -> H256 {
OffencesSubspace::report_id::<Offence<PublicKey>>(&time_slot, &offender)
}
25 changes: 12 additions & 13 deletions crates/pallet-offences-subspace/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,12 @@ use codec::{Decode, Encode};
use frame_system::{EventRecord, Phase};
use schnorrkel::Keypair;
use sp_consensus_subspace::offence::{OffenceDetails, OffenceError, ReportOffence};
use sp_consensus_subspace::FarmerPublicKey;
use sp_core::crypto::UncheckedFrom;
use sp_runtime::Perbill;
use subspace_core_primitives::PublicKey;

fn generate_farmer_public_key() -> FarmerPublicKey {
fn generate_farmer_public_key() -> PublicKey {
let keypair = Keypair::generate();
FarmerPublicKey::unchecked_from(keypair.public.to_bytes())
PublicKey::from(keypair.public.to_bytes())
}

#[test]
Expand Down Expand Up @@ -189,7 +188,7 @@ fn doesnt_deposit_event_for_dups() {

#[test]
fn reports_if_an_offence_is_dup() {
type TestOffence = Offence<FarmerPublicKey>;
type TestOffence = Offence<PublicKey>;

new_test_ext().execute_with(|| {
let time_slot = 42;
Expand All @@ -203,7 +202,7 @@ fn reports_if_an_offence_is_dup() {
offenders,
};

let mut test_offence = offence(time_slot, vec![farmer_0.clone()]);
let mut test_offence = offence(time_slot, vec![farmer_0]);

// the report for farmer 0 at time slot 42 should not be a known
// offence
Expand Down Expand Up @@ -232,7 +231,7 @@ fn reports_if_an_offence_is_dup() {
);

// after adding a new offender to the offence report
test_offence.offenders.push(farmer_1.clone());
test_offence.offenders.push(farmer_1);

// it should not be a known offence anymore
assert!(
Expand Down Expand Up @@ -274,11 +273,11 @@ fn should_properly_count_offences() {

let offence1 = Offence {
time_slot,
offenders: vec![farmer_1.clone()],
offenders: vec![farmer_1],
};
let offence2 = Offence {
time_slot,
offenders: vec![farmer_2.clone()],
offenders: vec![farmer_2],
};
OffencesSubspace::report_offence(offence1).unwrap();
with_on_offence_fractions(|f| {
Expand Down Expand Up @@ -319,19 +318,19 @@ fn should_properly_sort_offences() {

let offence1 = Offence {
time_slot,
offenders: vec![farmer_5.clone()],
offenders: vec![farmer_5],
};
let offence2 = Offence {
time_slot,
offenders: vec![farmer_4.clone()],
offenders: vec![farmer_4],
};
let offence3 = Offence {
time_slot: time_slot + 1,
offenders: vec![farmer_6.clone(), farmer_7.clone()],
offenders: vec![farmer_6, farmer_7],
};
let offence4 = Offence {
time_slot: time_slot - 1,
offenders: vec![farmer_3.clone()],
offenders: vec![farmer_3],
};
OffencesSubspace::report_offence(offence1).unwrap();
with_on_offence_fractions(|f| {
Expand Down
13 changes: 6 additions & 7 deletions crates/pallet-subspace/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ frame-system = { default-features = false, git = "https://github.com/subspace/po
log = { version = "0.4.22", default-features = false }
scale-info = { version = "2.11.2", default-features = false, features = ["derive"] }
schnorrkel = { version = "0.11.4", default-features = false }
serde = { version = "1.0.206", optional = true, default-features = false, features = ["derive"] }
serde = { version = "1.0.206", optional = true, default-features = false, features = ["alloc", "derive"] }
sp-consensus-subspace = { version = "0.1.0", default-features = false, path = "../sp-consensus-subspace" }
sp-consensus-slots = { default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "5626154d0781ac9a6ffd5a6207ed237f425ae631" }
sp-core = { default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "5626154d0781ac9a6ffd5a6207ed237f425ae631", optional = true }
sp-runtime = { default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "5626154d0781ac9a6ffd5a6207ed237f425ae631" }
sp-std = { default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "5626154d0781ac9a6ffd5a6207ed237f425ae631" }
subspace-core-primitives = { version = "0.1.0", default-features = false, path = "../subspace-core-primitives" }
Expand All @@ -36,7 +35,6 @@ futures = "0.3.29"
pallet-balances = { git = "https://github.com/subspace/polkadot-sdk", rev = "5626154d0781ac9a6ffd5a6207ed237f425ae631" }
pallet-offences-subspace = { version = "0.1.0", path = "../pallet-offences-subspace" }
rand = { version = "0.8.5", features = ["min_const_gen"] }
sp-core = { git = "https://github.com/subspace/polkadot-sdk", rev = "5626154d0781ac9a6ffd5a6207ed237f425ae631" }
sp-io = { git = "https://github.com/subspace/polkadot-sdk", rev = "5626154d0781ac9a6ffd5a6207ed237f425ae631" }
subspace-archiving = { version = "0.1.0", path = "../subspace-archiving" }
subspace-core-primitives = { version = "0.1.0", path = "../subspace-core-primitives" }
Expand All @@ -46,6 +44,10 @@ subspace-proof-of-space = { version = "0.1.0", path = "../subspace-proof-of-spac

[features]
default = ["std"]
serde = [
"dep:serde",
"subspace-core-primitives/serde",
]
std = [
"codec/std",
"frame-benchmarking?/std",
Expand All @@ -54,11 +56,9 @@ std = [
"log/std",
"scale-info/std",
"schnorrkel/std",
"serde",
"serde/std",
"serde?/std",
"sp-consensus-subspace/std",
"sp-consensus-slots/std",
"sp-core?/std",
"sp-io/std",
"sp-runtime/std",
"sp-std/std",
Expand All @@ -70,5 +70,4 @@ try-runtime = ["frame-support/try-runtime"]
runtime-benchmarks = [
"frame-benchmarking",
"frame-benchmarking/runtime-benchmarks",
"sp-core",
]
17 changes: 7 additions & 10 deletions crates/pallet-subspace/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,16 @@ mod benchmarks {
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
use frame_benchmarking::v2::*;
use frame_support::traits::Get;
use frame_system::pallet_prelude::*;
use frame_system::{Pallet as System, RawOrigin};
use sp_consensus_subspace::{
EquivocationProof, FarmerPublicKey, FarmerSignature, SignedVote, Vote,
};
use sp_core::crypto::UncheckedFrom;
use sp_core::Get;
use sp_consensus_subspace::{EquivocationProof, SignedVote, Vote};
use sp_runtime::traits::{Block, Header};
use sp_std::boxed::Box;
use sp_std::num::NonZeroU32;
use subspace_core_primitives::{
ArchivedBlockProgress, Blake3Hash, LastArchivedBlock, PotCheckpoints, PotOutput,
SegmentHeader, SegmentIndex, Solution, SolutionRange,
ArchivedBlockProgress, Blake3Hash, LastArchivedBlock, PotCheckpoints, PotOutput, PublicKey,
RewardSignature, SegmentHeader, SegmentIndex, Solution, SolutionRange,
};

const SEED: u32 = 0;
Expand All @@ -36,7 +33,7 @@ mod benchmarks {
fn report_equivocation() {
// Construct a dummy equivocation proof which is invalid but it is okay because the
// proof is not validate during the call
let offender = FarmerPublicKey::unchecked_from([0u8; 32]);
let offender = PublicKey::from([0u8; 32]);
let header = <T::Block as Block>::Header::new(
System::<T>::block_number(),
Default::default(),
Expand Down Expand Up @@ -110,13 +107,13 @@ mod benchmarks {
parent_hash: System::<T>::parent_hash(),
slot: Pallet::<T>::current_slot(),
solution: Solution::genesis_solution(
FarmerPublicKey::unchecked_from([1u8; 32]),
PublicKey::from([1u8; 32]),
account("user1", 1, SEED),
),
proof_of_time: PotOutput::default(),
future_proof_of_time: PotOutput::default(),
};
let signature = FarmerSignature::unchecked_from([2u8; 64]);
let signature = RewardSignature::from([2u8; 64]);
let signed_vote = SignedVote {
vote: unsigned_vote,
signature,
Expand Down
Loading

0 comments on commit 53b9b9e

Please sign in to comment.