mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-05-30 04:41:03 +00:00
Move sp-npos-elections-solution-type to frame-election-provider-support (#11016)
* Move `sp-npos-elections-solution-type` to `frame-election-provider-support` First stab at it, will need to amend some more stuff * Fixing tests * Fixing tests * Fixing cargo.toml for std configuration * fmt * Committing suggested changes renaming, and re exporting macro. * Removing unneeded imports
This commit is contained in:
@@ -21,6 +21,7 @@ sp-npos-elections = { version = "4.0.0-dev", default-features = false, path = ".
|
||||
sp-runtime = { version = "6.0.0", default-features = false, path = "../../primitives/runtime" }
|
||||
frame-support = { version = "4.0.0-dev", default-features = false, path = "../support" }
|
||||
frame-system = { version = "4.0.0-dev", default-features = false, path = "../system" }
|
||||
frame-election-provider-solution-type = { version = "4.0.0-dev", path = "solution-type" }
|
||||
|
||||
[dev-dependencies]
|
||||
sp-npos-elections = { version = "4.0.0-dev", path = "../../primitives/npos-elections" }
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
[package]
|
||||
name = "frame-election-provider-solution-type"
|
||||
version = "4.0.0-dev"
|
||||
authors = ["Parity Technologies <admin@parity.io>"]
|
||||
edition = "2021"
|
||||
license = "Apache-2.0"
|
||||
homepage = "https://substrate.io"
|
||||
repository = "https://github.com/paritytech/substrate/"
|
||||
description = "NPoS Solution Type"
|
||||
|
||||
[package.metadata.docs.rs]
|
||||
targets = ["x86_64-unknown-linux-gnu"]
|
||||
|
||||
[lib]
|
||||
proc-macro = true
|
||||
|
||||
[dependencies]
|
||||
syn = { version = "1.0.82", features = ["full", "visit"] }
|
||||
quote = "1.0"
|
||||
proc-macro2 = "1.0.36"
|
||||
proc-macro-crate = "1.1.0"
|
||||
|
||||
[dev-dependencies]
|
||||
parity-scale-codec = "3.0.0"
|
||||
scale-info = "2.0.1"
|
||||
sp-arithmetic = { version = "5.0.0", path = "../../../primitives/arithmetic" }
|
||||
# used by generate_solution_type:
|
||||
sp-npos-elections = { version = "4.0.0-dev", path = "../../../primitives/npos-elections" }
|
||||
trybuild = "1.0.53"
|
||||
@@ -0,0 +1,30 @@
|
||||
[package]
|
||||
name = "frame-election-solution-type-fuzzer"
|
||||
version = "2.0.0-alpha.5"
|
||||
authors = ["Parity Technologies <admin@parity.io>"]
|
||||
edition = "2021"
|
||||
license = "Apache-2.0"
|
||||
homepage = "https://substrate.io"
|
||||
repository = "https://github.com/paritytech/substrate/"
|
||||
description = "Fuzzer for phragmén solution type implementation."
|
||||
publish = false
|
||||
|
||||
[package.metadata.docs.rs]
|
||||
targets = ["x86_64-unknown-linux-gnu"]
|
||||
|
||||
[dependencies]
|
||||
clap = { version = "3.0", features = ["derive"] }
|
||||
honggfuzz = "0.5"
|
||||
rand = { version = "0.8", features = ["std", "small_rng"] }
|
||||
|
||||
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive"] }
|
||||
scale-info = { version = "2.0.1", default-features = false, features = ["derive"] }
|
||||
frame-election-provider-solution-type = { version = "4.0.0-dev", path = ".." }
|
||||
sp-arithmetic = { version = "5.0.0", path = "../../../../primitives/arithmetic" }
|
||||
sp-runtime = { version = "6.0.0", path = "../../../../primitives/runtime" }
|
||||
# used by generate_solution_type:
|
||||
sp-npos-elections = { version = "4.0.0-dev", default-features = false, path = "../../../../primitives/npos-elections" }
|
||||
|
||||
[[bin]]
|
||||
name = "compact"
|
||||
path = "src/compact.rs"
|
||||
@@ -0,0 +1,39 @@
|
||||
use frame_election_provider_solution_type::generate_solution_type;
|
||||
use honggfuzz::fuzz;
|
||||
use sp_arithmetic::Percent;
|
||||
use sp_runtime::codec::{Encode, Error};
|
||||
|
||||
fn main() {
|
||||
generate_solution_type!(#[compact] pub struct InnerTestSolutionCompact::<
|
||||
VoterIndex = u32,
|
||||
TargetIndex = u32,
|
||||
Accuracy = Percent,
|
||||
>(16));
|
||||
loop {
|
||||
fuzz!(|fuzzer_data: &[u8]| {
|
||||
let result_decoded: Result<InnerTestSolutionCompact, Error> =
|
||||
<InnerTestSolutionCompact as codec::Decode>::decode(&mut &*fuzzer_data);
|
||||
// Ignore errors as not every random sequence of bytes can be decoded as
|
||||
// InnerTestSolutionCompact
|
||||
if let Ok(decoded) = result_decoded {
|
||||
// Decoding works, let's re-encode it and compare results.
|
||||
let reencoded: std::vec::Vec<u8> = decoded.encode();
|
||||
// The reencoded value may or may not be equal to the original fuzzer output.
|
||||
// However, the original decoder should be optimal (in the sense that there is no
|
||||
// shorter encoding of the same object). So let's see if the fuzzer can find
|
||||
// something shorter:
|
||||
if fuzzer_data.len() < reencoded.len() {
|
||||
panic!("fuzzer_data.len() < reencoded.len()");
|
||||
}
|
||||
// The reencoded value should definitely be decodable (if unwrap() fails that is a
|
||||
// valid panic/finding for the fuzzer):
|
||||
let decoded2: InnerTestSolutionCompact =
|
||||
<InnerTestSolutionCompact as codec::Decode>::decode(&mut reencoded.as_slice())
|
||||
.unwrap();
|
||||
// And it should be equal to the original decoded object (resulting from directly
|
||||
// decoding fuzzer_data):
|
||||
assert_eq!(decoded, decoded2);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,242 @@
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Copyright (C) 2020-2022 Parity Technologies (UK) Ltd.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//! Code generation for the ratio assignment type' encode/decode/info impl.
|
||||
|
||||
use crate::vote_field;
|
||||
use proc_macro2::TokenStream as TokenStream2;
|
||||
use quote::quote;
|
||||
|
||||
pub(crate) fn codec_and_info_impl(
|
||||
ident: syn::Ident,
|
||||
voter_type: syn::Type,
|
||||
target_type: syn::Type,
|
||||
weight_type: syn::Type,
|
||||
count: usize,
|
||||
) -> TokenStream2 {
|
||||
let encode = encode_impl(&ident, count);
|
||||
let decode = decode_impl(&ident, &voter_type, &target_type, &weight_type, count);
|
||||
let scale_info = scale_info_impl(&ident, &voter_type, &target_type, &weight_type, count);
|
||||
|
||||
quote! {
|
||||
#encode
|
||||
#decode
|
||||
#scale_info
|
||||
}
|
||||
}
|
||||
|
||||
fn decode_impl(
|
||||
ident: &syn::Ident,
|
||||
voter_type: &syn::Type,
|
||||
target_type: &syn::Type,
|
||||
weight_type: &syn::Type,
|
||||
count: usize,
|
||||
) -> TokenStream2 {
|
||||
let decode_impl_single = {
|
||||
let name = vote_field(1);
|
||||
quote! {
|
||||
let #name =
|
||||
<
|
||||
_npos::sp_std::prelude::Vec<(_npos::codec::Compact<#voter_type>, _npos::codec::Compact<#target_type>)>
|
||||
as
|
||||
_npos::codec::Decode
|
||||
>::decode(value)?;
|
||||
let #name = #name
|
||||
.into_iter()
|
||||
.map(|(v, t)| (v.0, t.0))
|
||||
.collect::<_npos::sp_std::prelude::Vec<_>>();
|
||||
}
|
||||
};
|
||||
|
||||
let decode_impl_rest = (2..=count)
|
||||
.map(|c| {
|
||||
let name = vote_field(c);
|
||||
|
||||
let inner_impl = (0..c - 1)
|
||||
.map(|i| quote! { ( (inner[#i].0).0, (inner[#i].1).0 ), })
|
||||
.collect::<TokenStream2>();
|
||||
|
||||
quote! {
|
||||
let #name =
|
||||
<
|
||||
_npos::sp_std::prelude::Vec<(
|
||||
_npos::codec::Compact<#voter_type>,
|
||||
[(_npos::codec::Compact<#target_type>, _npos::codec::Compact<#weight_type>); #c-1],
|
||||
_npos::codec::Compact<#target_type>,
|
||||
)>
|
||||
as _npos::codec::Decode
|
||||
>::decode(value)?;
|
||||
let #name = #name
|
||||
.into_iter()
|
||||
.map(|(v, inner, t_last)| (
|
||||
v.0,
|
||||
[ #inner_impl ],
|
||||
t_last.0,
|
||||
))
|
||||
.collect::<_npos::sp_std::prelude::Vec<_>>();
|
||||
}
|
||||
})
|
||||
.collect::<TokenStream2>();
|
||||
|
||||
let all_field_names = (1..=count)
|
||||
.map(|c| {
|
||||
let name = vote_field(c);
|
||||
quote! { #name, }
|
||||
})
|
||||
.collect::<TokenStream2>();
|
||||
|
||||
quote!(
|
||||
impl _npos::codec::Decode for #ident {
|
||||
fn decode<I: _npos::codec::Input>(value: &mut I) -> Result<Self, _npos::codec::Error> {
|
||||
#decode_impl_single
|
||||
#decode_impl_rest
|
||||
|
||||
// The above code generates variables with the decoded value with the same name as
|
||||
// filed names of the struct, i.e. `let votes4 = decode_value_of_votes4`. All we
|
||||
// have to do is collect them into the main struct now.
|
||||
Ok(#ident { #all_field_names })
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
// General attitude is that we will convert inner values to `Compact` and then use the normal
|
||||
// `Encode` implementation.
|
||||
fn encode_impl(ident: &syn::Ident, count: usize) -> TokenStream2 {
|
||||
let encode_impl_single = {
|
||||
let name = vote_field(1);
|
||||
quote! {
|
||||
let #name = self.#name
|
||||
.iter()
|
||||
.map(|(v, t)| (
|
||||
_npos::codec::Compact(v.clone()),
|
||||
_npos::codec::Compact(t.clone()),
|
||||
))
|
||||
.collect::<_npos::sp_std::prelude::Vec<_>>();
|
||||
#name.encode_to(&mut r);
|
||||
}
|
||||
};
|
||||
|
||||
let encode_impl_rest = (2..=count)
|
||||
.map(|c| {
|
||||
let name = vote_field(c);
|
||||
|
||||
// we use the knowledge of the length to avoid copy_from_slice.
|
||||
let inners_solution_array = (0..c - 1)
|
||||
.map(|i| {
|
||||
quote! {(
|
||||
_npos::codec::Compact(inner[#i].0.clone()),
|
||||
_npos::codec::Compact(inner[#i].1.clone()),
|
||||
),}
|
||||
})
|
||||
.collect::<TokenStream2>();
|
||||
|
||||
quote! {
|
||||
let #name = self.#name
|
||||
.iter()
|
||||
.map(|(v, inner, t_last)| (
|
||||
_npos::codec::Compact(v.clone()),
|
||||
[ #inners_solution_array ],
|
||||
_npos::codec::Compact(t_last.clone()),
|
||||
))
|
||||
.collect::<_npos::sp_std::prelude::Vec<_>>();
|
||||
#name.encode_to(&mut r);
|
||||
}
|
||||
})
|
||||
.collect::<TokenStream2>();
|
||||
|
||||
quote!(
|
||||
impl _npos::codec::Encode for #ident {
|
||||
fn encode(&self) -> _npos::sp_std::prelude::Vec<u8> {
|
||||
let mut r = vec![];
|
||||
#encode_impl_single
|
||||
#encode_impl_rest
|
||||
r
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
fn scale_info_impl(
|
||||
ident: &syn::Ident,
|
||||
voter_type: &syn::Type,
|
||||
target_type: &syn::Type,
|
||||
weight_type: &syn::Type,
|
||||
count: usize,
|
||||
) -> TokenStream2 {
|
||||
let scale_info_impl_single = {
|
||||
let name = format!("{}", vote_field(1));
|
||||
quote! {
|
||||
.field(|f|
|
||||
f.ty::<_npos::sp_std::prelude::Vec<
|
||||
(_npos::codec::Compact<#voter_type>, _npos::codec::Compact<#target_type>)
|
||||
>>()
|
||||
.name(#name)
|
||||
)
|
||||
}
|
||||
};
|
||||
|
||||
let scale_info_impl_double = {
|
||||
let name = format!("{}", vote_field(2));
|
||||
quote! {
|
||||
.field(|f|
|
||||
f.ty::<_npos::sp_std::prelude::Vec<(
|
||||
_npos::codec::Compact<#voter_type>,
|
||||
(_npos::codec::Compact<#target_type>, _npos::codec::Compact<#weight_type>),
|
||||
_npos::codec::Compact<#target_type>
|
||||
)>>()
|
||||
.name(#name)
|
||||
)
|
||||
}
|
||||
};
|
||||
|
||||
let scale_info_impl_rest = (3..=count)
|
||||
.map(|c| {
|
||||
let name = format!("{}", vote_field(c));
|
||||
quote! {
|
||||
.field(|f|
|
||||
f.ty::<_npos::sp_std::prelude::Vec<(
|
||||
_npos::codec::Compact<#voter_type>,
|
||||
[
|
||||
(_npos::codec::Compact<#target_type>, _npos::codec::Compact<#weight_type>);
|
||||
#c - 1
|
||||
],
|
||||
_npos::codec::Compact<#target_type>
|
||||
)>>()
|
||||
.name(#name)
|
||||
)
|
||||
}
|
||||
})
|
||||
.collect::<TokenStream2>();
|
||||
|
||||
quote!(
|
||||
impl _npos::scale_info::TypeInfo for #ident {
|
||||
type Identity = Self;
|
||||
|
||||
fn type_info() -> _npos::scale_info::Type<_npos::scale_info::form::MetaForm> {
|
||||
_npos::scale_info::Type::builder()
|
||||
.path(_npos::scale_info::Path::new(stringify!(#ident), module_path!()))
|
||||
.composite(
|
||||
_npos::scale_info::build::Fields::named()
|
||||
#scale_info_impl_single
|
||||
#scale_info_impl_double
|
||||
#scale_info_impl_rest
|
||||
)
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Copyright (C) 2020-2022 Parity Technologies (UK) Ltd.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//! Helpers to generate the push code for `from_assignment` implementations. This can be shared
|
||||
//! between both single_page and double_page, thus extracted here.
|
||||
//!
|
||||
//! All of the code in this helper module assumes some variable names, namely `who` and
|
||||
//! `distribution`.
|
||||
|
||||
use proc_macro2::TokenStream as TokenStream2;
|
||||
use quote::quote;
|
||||
|
||||
pub(crate) fn from_impl_single_push_code() -> TokenStream2 {
|
||||
quote!(push((
|
||||
voter_index(&who).or_invalid_index()?,
|
||||
target_index(&distribution[0].0).or_invalid_index()?,
|
||||
)))
|
||||
}
|
||||
|
||||
pub(crate) fn from_impl_rest_push_code(count: usize) -> TokenStream2 {
|
||||
let inner = (0..count - 1).map(|i| {
|
||||
quote!(
|
||||
(
|
||||
target_index(&distribution[#i].0).or_invalid_index()?,
|
||||
distribution[#i].1
|
||||
)
|
||||
)
|
||||
});
|
||||
|
||||
let last_index = count - 1;
|
||||
let last = quote!(target_index(&distribution[#last_index].0).or_invalid_index()?);
|
||||
|
||||
quote!(
|
||||
push(
|
||||
(
|
||||
voter_index(&who).or_invalid_index()?,
|
||||
[ #( #inner ),* ],
|
||||
#last,
|
||||
)
|
||||
)
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Copyright (C) 2020-2022 Parity Technologies (UK) Ltd.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//! Code generation for getting the solution representation from the `IndexAssignment` type.
|
||||
|
||||
use crate::vote_field;
|
||||
use proc_macro2::TokenStream as TokenStream2;
|
||||
use quote::quote;
|
||||
|
||||
pub(crate) fn from_impl(struct_name: &syn::Ident, count: usize) -> TokenStream2 {
|
||||
let from_impl_single = {
|
||||
let name = vote_field(1);
|
||||
quote!(1 => #struct_name.#name.push(
|
||||
(
|
||||
*who,
|
||||
distribution[0].0,
|
||||
)
|
||||
),)
|
||||
};
|
||||
|
||||
let from_impl_rest = (2..=count)
|
||||
.map(|c| {
|
||||
let inner = (0..c - 1)
|
||||
.map(|i| quote!((distribution[#i].0, distribution[#i].1),))
|
||||
.collect::<TokenStream2>();
|
||||
|
||||
let field_name = vote_field(c);
|
||||
let last_index = c - 1;
|
||||
let last = quote!(distribution[#last_index].0);
|
||||
|
||||
quote!(
|
||||
#c => #struct_name.#field_name.push(
|
||||
(
|
||||
*who,
|
||||
[#inner],
|
||||
#last,
|
||||
)
|
||||
),
|
||||
)
|
||||
})
|
||||
.collect::<TokenStream2>();
|
||||
|
||||
quote!(
|
||||
#from_impl_single
|
||||
#from_impl_rest
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,246 @@
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Copyright (C) 2020-2022 Parity Technologies (UK) Ltd.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//! Proc macro for a npos solution type.
|
||||
|
||||
use proc_macro::TokenStream;
|
||||
use proc_macro2::{Ident, Span, TokenStream as TokenStream2};
|
||||
use proc_macro_crate::{crate_name, FoundCrate};
|
||||
use quote::quote;
|
||||
use syn::parse::{Parse, ParseStream, Result};
|
||||
|
||||
mod codec;
|
||||
mod from_assignment_helpers;
|
||||
mod index_assignment;
|
||||
mod single_page;
|
||||
|
||||
/// Get the name of a filed based on voter count.
|
||||
pub(crate) fn vote_field(n: usize) -> Ident {
|
||||
quote::format_ident!("votes{}", n)
|
||||
}
|
||||
|
||||
/// Generate a `syn::Error`.
|
||||
pub(crate) fn syn_err(message: &'static str) -> syn::Error {
|
||||
syn::Error::new(Span::call_site(), message)
|
||||
}
|
||||
|
||||
/// Generates a struct to store the election result in a small/compact way. This can encode a
|
||||
/// structure which is the equivalent of a `sp_npos_elections::Assignment<_>`.
|
||||
///
|
||||
/// The following data types can be configured by the macro.
|
||||
///
|
||||
/// - The identifier of the voter. This can be any type that supports `parity-scale-codec`'s compact
|
||||
/// encoding.
|
||||
/// - The identifier of the target. This can be any type that supports `parity-scale-codec`'s
|
||||
/// compact encoding.
|
||||
/// - The accuracy of the ratios. This must be one of the `PerThing` types defined in
|
||||
/// `sp-arithmetic`.
|
||||
///
|
||||
/// Moreover, the maximum number of edges per voter (distribution per assignment) also need to be
|
||||
/// specified. Attempting to convert from/to an assignment with more distributions will fail.
|
||||
///
|
||||
/// For example, the following generates a public struct with name `TestSolution` with `u16` voter
|
||||
/// type, `u8` target type and `Perbill` accuracy with maximum of 4 edges per voter.
|
||||
///
|
||||
/// ```
|
||||
/// # use frame_election_provider_solution_type::generate_solution_type;
|
||||
/// # use sp_arithmetic::per_things::Perbill;
|
||||
/// generate_solution_type!(pub struct TestSolution::<
|
||||
/// VoterIndex = u16,
|
||||
/// TargetIndex = u8,
|
||||
/// Accuracy = Perbill,
|
||||
/// >(4));
|
||||
/// ```
|
||||
///
|
||||
/// The output of this macro will roughly look like:
|
||||
///
|
||||
/// ```ignore
|
||||
/// struct TestSolution {
|
||||
/// voters1: vec![(u16 /* voter */, u8 /* target */)]
|
||||
/// voters2: vec![
|
||||
/// (u16 /* voter */, [u8 /* first target*/, Perbill /* proportion for first target */], u8 /* last target */)
|
||||
/// ]
|
||||
/// voters3: vec![
|
||||
/// (u16 /* voter */, [
|
||||
/// (u8 /* first target*/, Perbill /* proportion for first target */ ),
|
||||
/// (u8 /* second target */, Perbill /* proportion for second target*/)
|
||||
/// ], u8 /* last target */)
|
||||
/// ],
|
||||
/// voters4: ...,
|
||||
/// }
|
||||
///
|
||||
/// impl NposSolution for TestSolution {};
|
||||
/// impl Solution for TestSolution {};
|
||||
/// ```
|
||||
///
|
||||
/// The given struct provides function to convert from/to `Assignment` as part of
|
||||
/// `sp_npos_elections::Solution` trait:
|
||||
///
|
||||
/// - `fn from_assignment<..>(..)`
|
||||
/// - `fn into_assignment<..>(..)`
|
||||
///
|
||||
/// ## Compact Encoding
|
||||
///
|
||||
/// The generated struct is by default deriving both `Encode` and `Decode`. This is okay but could
|
||||
/// lead to many `0`s in the solution. If prefixed with `#[compact]`, then a custom compact encoding
|
||||
/// for numbers will be used, similar to how `parity-scale-codec`'s `Compact` works.
|
||||
///
|
||||
/// ```
|
||||
/// # use frame_election_provider_solution_type::generate_solution_type;
|
||||
/// # use sp_npos_elections::NposSolution;
|
||||
/// # use sp_arithmetic::per_things::Perbill;
|
||||
/// generate_solution_type!(
|
||||
/// #[compact]
|
||||
/// pub struct TestSolutionCompact::<VoterIndex = u16, TargetIndex = u8, Accuracy = Perbill>(8)
|
||||
/// );
|
||||
/// ```
|
||||
#[proc_macro]
|
||||
pub fn generate_solution_type(item: TokenStream) -> TokenStream {
|
||||
let solution_def = syn::parse_macro_input!(item as SolutionDef);
|
||||
|
||||
let imports = imports().unwrap_or_else(|e| e.to_compile_error());
|
||||
|
||||
let def = single_page::generate(solution_def).unwrap_or_else(|e| e.to_compile_error());
|
||||
|
||||
quote!(
|
||||
#imports
|
||||
#def
|
||||
)
|
||||
.into()
|
||||
}
|
||||
|
||||
struct SolutionDef {
|
||||
vis: syn::Visibility,
|
||||
ident: syn::Ident,
|
||||
voter_type: syn::Type,
|
||||
target_type: syn::Type,
|
||||
weight_type: syn::Type,
|
||||
count: usize,
|
||||
compact_encoding: bool,
|
||||
}
|
||||
|
||||
fn check_attributes(input: ParseStream) -> syn::Result<bool> {
|
||||
let mut attrs = input.call(syn::Attribute::parse_outer).unwrap_or_default();
|
||||
if attrs.len() > 1 {
|
||||
let extra_attr = attrs.pop().expect("attributes vec with len > 1 can be popped");
|
||||
return Err(syn::Error::new_spanned(
|
||||
extra_attr.clone(),
|
||||
"compact solution can accept only #[compact]",
|
||||
))
|
||||
}
|
||||
if attrs.is_empty() {
|
||||
return Ok(false)
|
||||
}
|
||||
let attr = attrs.pop().expect("attributes vec with len 1 can be popped.");
|
||||
if attr.path.is_ident("compact") {
|
||||
Ok(true)
|
||||
} else {
|
||||
Err(syn::Error::new_spanned(attr.clone(), "compact solution can accept only #[compact]"))
|
||||
}
|
||||
}
|
||||
|
||||
impl Parse for SolutionDef {
|
||||
fn parse(input: ParseStream) -> syn::Result<Self> {
|
||||
// optional #[compact]
|
||||
let compact_encoding = check_attributes(input)?;
|
||||
|
||||
// <vis> struct <name>
|
||||
let vis: syn::Visibility = input.parse()?;
|
||||
let _ = <syn::Token![struct]>::parse(input)?;
|
||||
let ident: syn::Ident = input.parse()?;
|
||||
|
||||
// ::<V, T, W>
|
||||
let _ = <syn::Token![::]>::parse(input)?;
|
||||
let generics: syn::AngleBracketedGenericArguments = input.parse()?;
|
||||
|
||||
if generics.args.len() != 3 {
|
||||
return Err(syn_err("Must provide 3 generic args."))
|
||||
}
|
||||
|
||||
let expected_types = ["VoterIndex", "TargetIndex", "Accuracy"];
|
||||
|
||||
let mut types: Vec<syn::Type> = generics
|
||||
.args
|
||||
.iter()
|
||||
.zip(expected_types.iter())
|
||||
.map(|(t, expected)| match t {
|
||||
syn::GenericArgument::Type(ty) => {
|
||||
// this is now an error
|
||||
Err(syn::Error::new_spanned(
|
||||
ty,
|
||||
format!("Expected binding: `{} = ...`", expected),
|
||||
))
|
||||
},
|
||||
syn::GenericArgument::Binding(syn::Binding { ident, ty, .. }) => {
|
||||
// check that we have the right keyword for this position in the argument list
|
||||
if ident == expected {
|
||||
Ok(ty.clone())
|
||||
} else {
|
||||
Err(syn::Error::new_spanned(ident, format!("Expected `{}`", expected)))
|
||||
}
|
||||
},
|
||||
_ => Err(syn_err("Wrong type of generic provided. Must be a `type`.")),
|
||||
})
|
||||
.collect::<Result<_>>()?;
|
||||
|
||||
let weight_type = types.pop().expect("Vector of length 3 can be popped; qed");
|
||||
let target_type = types.pop().expect("Vector of length 2 can be popped; qed");
|
||||
let voter_type = types.pop().expect("Vector of length 1 can be popped; qed");
|
||||
|
||||
// (<count>)
|
||||
let count_expr: syn::ExprParen = input.parse()?;
|
||||
let count = parse_parenthesized_number::<usize>(count_expr)?;
|
||||
|
||||
Ok(Self { vis, ident, voter_type, target_type, weight_type, count, compact_encoding })
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_parenthesized_number<N: std::str::FromStr>(input_expr: syn::ExprParen) -> syn::Result<N>
|
||||
where
|
||||
<N as std::str::FromStr>::Err: std::fmt::Display,
|
||||
{
|
||||
let expr = input_expr.expr;
|
||||
let expr_lit = match *expr {
|
||||
syn::Expr::Lit(count_lit) => count_lit.lit,
|
||||
_ => return Err(syn_err("Count must be literal.")),
|
||||
};
|
||||
let int_lit = match expr_lit {
|
||||
syn::Lit::Int(int_lit) => int_lit,
|
||||
_ => return Err(syn_err("Count must be int literal.")),
|
||||
};
|
||||
int_lit.base10_parse::<N>()
|
||||
}
|
||||
|
||||
fn imports() -> Result<TokenStream2> {
|
||||
match crate_name("sp-npos-elections") {
|
||||
Ok(FoundCrate::Itself) => Ok(quote! { use crate as _npos; }),
|
||||
Ok(FoundCrate::Name(sp_npos_elections)) => {
|
||||
let ident = syn::Ident::new(&sp_npos_elections, Span::call_site());
|
||||
Ok(quote!( extern crate #ident as _npos; ))
|
||||
},
|
||||
Err(e) => Err(syn::Error::new(Span::call_site(), e)),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[test]
|
||||
fn ui_fail() {
|
||||
let cases = trybuild::TestCases::new();
|
||||
cases.compile_fail("tests/ui/fail/*.rs");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,178 @@
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Copyright (C) 2019-2022 Parity Technologies (UK) Ltd.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//! Mock file for solution-type.
|
||||
|
||||
#![cfg(test)]
|
||||
|
||||
use std::{collections::HashMap, convert::TryInto, hash::Hash, HashSet};
|
||||
|
||||
use rand::{seq::SliceRandom, Rng};
|
||||
|
||||
/// The candidate mask allows easy disambiguation between voters and candidates: accounts
|
||||
/// for which this bit is set are candidates, and without it, are voters.
|
||||
pub const CANDIDATE_MASK: AccountId = 1 << ((std::mem::size_of::<AccountId>() * 8) - 1);
|
||||
|
||||
pub type TestAccuracy = sp_runtime::Perbill;
|
||||
|
||||
pub fn p(p: u8) -> TestAccuracy {
|
||||
TestAccuracy::from_percent(p.into())
|
||||
}
|
||||
|
||||
pub type MockAssignment = crate::Assignment<AccountId, TestAccuracy>;
|
||||
pub type Voter = (AccountId, VoteWeight, Vec<AccountId>);
|
||||
|
||||
crate::generate_solution_type! {
|
||||
pub struct TestSolution::<
|
||||
VoterIndex = u32,
|
||||
TargetIndex = u16,
|
||||
Accuracy = TestAccuracy,
|
||||
>(16)
|
||||
}
|
||||
|
||||
/// Generate voter and assignment lists. Makes no attempt to be realistic about winner or assignment
|
||||
/// fairness.
|
||||
///
|
||||
/// Maintains these invariants:
|
||||
///
|
||||
/// - candidate ids have `CANDIDATE_MASK` bit set
|
||||
/// - voter ids do not have `CANDIDATE_MASK` bit set
|
||||
/// - assignments have the same ordering as voters
|
||||
/// - `assignments.distribution.iter().map(|(_, frac)| frac).sum() == One::one()`
|
||||
/// - a coherent set of winners is chosen.
|
||||
/// - the winner set is a subset of the candidate set.
|
||||
/// - `assignments.distribution.iter().all(|(who, _)| winners.contains(who))`
|
||||
pub fn generate_random_votes(
|
||||
candidate_count: usize,
|
||||
voter_count: usize,
|
||||
mut rng: impl Rng,
|
||||
) -> (Vec<Voter>, Vec<MockAssignment>, Vec<AccountId>) {
|
||||
// cache for fast generation of unique candidate and voter ids
|
||||
let mut used_ids = HashSet::with_capacity(candidate_count + voter_count);
|
||||
|
||||
// candidates are easy: just a completely random set of IDs
|
||||
let mut candidates: Vec<AccountId> = Vec::with_capacity(candidate_count);
|
||||
while candidates.len() < candidate_count {
|
||||
let mut new = || rng.gen::<AccountId>() | CANDIDATE_MASK;
|
||||
let mut id = new();
|
||||
// insert returns `false` when the value was already present
|
||||
while !used_ids.insert(id) {
|
||||
id = new();
|
||||
}
|
||||
candidates.push(id);
|
||||
}
|
||||
|
||||
// voters are random ids, random weights, random selection from the candidates
|
||||
let mut voters = Vec::with_capacity(voter_count);
|
||||
while voters.len() < voter_count {
|
||||
let mut new = || rng.gen::<AccountId>() & !CANDIDATE_MASK;
|
||||
let mut id = new();
|
||||
// insert returns `false` when the value was already present
|
||||
while !used_ids.insert(id) {
|
||||
id = new();
|
||||
}
|
||||
|
||||
let vote_weight = rng.gen();
|
||||
|
||||
// it's not interesting if a voter chooses 0 or all candidates, so rule those cases out.
|
||||
// also, let's not generate any cases which result in a compact overflow.
|
||||
let n_candidates_chosen =
|
||||
rng.gen_range(1, candidates.len().min(<TestSolution as crate::NposSolution>::LIMIT));
|
||||
|
||||
let mut chosen_candidates = Vec::with_capacity(n_candidates_chosen);
|
||||
chosen_candidates.extend(candidates.choose_multiple(&mut rng, n_candidates_chosen));
|
||||
voters.push((id, vote_weight, chosen_candidates));
|
||||
}
|
||||
|
||||
// always generate a sensible number of winners: elections are uninteresting if nobody wins,
|
||||
// or everybody wins
|
||||
let num_winners = rng.gen_range(1, candidate_count);
|
||||
let mut winners: HashSet<AccountId> = HashSet::with_capacity(num_winners);
|
||||
winners.extend(candidates.choose_multiple(&mut rng, num_winners));
|
||||
assert_eq!(winners.len(), num_winners);
|
||||
|
||||
let mut assignments = Vec::with_capacity(voters.len());
|
||||
for (voter_id, _, votes) in voters.iter() {
|
||||
let chosen_winners = votes.iter().filter(|vote| winners.contains(vote)).cloned();
|
||||
let num_chosen_winners = chosen_winners.clone().count();
|
||||
|
||||
// distribute the available stake randomly
|
||||
let stake_distribution = if num_chosen_winners == 0 {
|
||||
continue
|
||||
} else {
|
||||
let mut available_stake = 1000;
|
||||
let mut stake_distribution = Vec::with_capacity(num_chosen_winners);
|
||||
for _ in 0..num_chosen_winners - 1 {
|
||||
let stake = rng.gen_range(0, available_stake).min(1);
|
||||
stake_distribution.push(TestAccuracy::from_perthousand(stake));
|
||||
available_stake -= stake;
|
||||
}
|
||||
stake_distribution.push(TestAccuracy::from_perthousand(available_stake));
|
||||
stake_distribution.shuffle(&mut rng);
|
||||
stake_distribution
|
||||
};
|
||||
|
||||
assignments.push(MockAssignment {
|
||||
who: *voter_id,
|
||||
distribution: chosen_winners.zip(stake_distribution).collect(),
|
||||
});
|
||||
}
|
||||
|
||||
(voters, assignments, candidates)
|
||||
}
|
||||
|
||||
fn generate_cache<Voters, Item>(voters: Voters) -> HashMap<Item, usize>
|
||||
where
|
||||
Voters: Iterator<Item = Item>,
|
||||
Item: Hash + Eq + Copy,
|
||||
{
|
||||
let mut cache = HashMap::new();
|
||||
for (idx, voter_id) in voters.enumerate() {
|
||||
cache.insert(voter_id, idx);
|
||||
}
|
||||
cache
|
||||
}
|
||||
|
||||
/// Create a function that returns the index of a voter in the voters list.
|
||||
pub fn make_voter_fn<VoterIndex>(voters: &[Voter]) -> impl Fn(&AccountId) -> Option<VoterIndex>
|
||||
where
|
||||
usize: TryInto<VoterIndex>,
|
||||
{
|
||||
let cache = generate_cache(voters.iter().map(|(id, _, _)| *id));
|
||||
move |who| {
|
||||
if cache.get(who).is_none() {
|
||||
println!("WARNING: voter {} will raise InvalidIndex", who);
|
||||
}
|
||||
cache.get(who).cloned().and_then(|i| i.try_into().ok())
|
||||
}
|
||||
}
|
||||
|
||||
/// Create a function that returns the index of a candidate in the candidates list.
|
||||
pub fn make_target_fn<TargetIndex>(
|
||||
candidates: &[AccountId],
|
||||
) -> impl Fn(&AccountId) -> Option<TargetIndex>
|
||||
where
|
||||
usize: TryInto<TargetIndex>,
|
||||
{
|
||||
let cache = generate_cache(candidates.iter().cloned());
|
||||
move |who| {
|
||||
if cache.get(who).is_none() {
|
||||
println!("WARNING: target {} will raise InvalidIndex", who);
|
||||
}
|
||||
cache.get(who).cloned().and_then(|i| i.try_into().ok())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,363 @@
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Copyright (C) 2019-2022 Parity Technologies (UK) Ltd.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
use crate::{from_assignment_helpers::*, syn_err, vote_field};
|
||||
use proc_macro2::TokenStream as TokenStream2;
|
||||
use quote::quote;
|
||||
use syn::parse::Result;
|
||||
|
||||
pub(crate) fn generate(def: crate::SolutionDef) -> Result<TokenStream2> {
|
||||
let crate::SolutionDef {
|
||||
vis,
|
||||
ident,
|
||||
count,
|
||||
voter_type,
|
||||
target_type,
|
||||
weight_type,
|
||||
compact_encoding,
|
||||
} = def;
|
||||
|
||||
if count <= 2 {
|
||||
Err(syn_err("cannot build solution struct with capacity less than 3."))?
|
||||
}
|
||||
|
||||
let single = {
|
||||
let name = vote_field(1);
|
||||
// NOTE: we use the visibility of the struct for the fields as well.. could be made better.
|
||||
quote!(
|
||||
#vis #name: _npos::sp_std::prelude::Vec<(#voter_type, #target_type)>,
|
||||
)
|
||||
};
|
||||
|
||||
let rest = (2..=count)
|
||||
.map(|c| {
|
||||
let field_name = vote_field(c);
|
||||
let array_len = c - 1;
|
||||
quote!(
|
||||
#vis #field_name: _npos::sp_std::prelude::Vec<(
|
||||
#voter_type,
|
||||
[(#target_type, #weight_type); #array_len],
|
||||
#target_type
|
||||
)>,
|
||||
)
|
||||
})
|
||||
.collect::<TokenStream2>();
|
||||
|
||||
let len_impl = len_impl(count);
|
||||
let edge_count_impl = edge_count_impl(count);
|
||||
let unique_targets_impl = unique_targets_impl(count);
|
||||
let remove_voter_impl = remove_voter_impl(count);
|
||||
|
||||
let derives_and_maybe_compact_encoding = if compact_encoding {
|
||||
// custom compact encoding.
|
||||
let compact_impl = crate::codec::codec_and_info_impl(
|
||||
ident.clone(),
|
||||
voter_type.clone(),
|
||||
target_type.clone(),
|
||||
weight_type.clone(),
|
||||
count,
|
||||
);
|
||||
quote! {
|
||||
#compact_impl
|
||||
#[derive(Default, PartialEq, Eq, Clone, Debug, PartialOrd, Ord)]
|
||||
}
|
||||
} else {
|
||||
// automatically derived.
|
||||
quote!(#[derive(
|
||||
Default,
|
||||
PartialEq,
|
||||
Eq,
|
||||
Clone,
|
||||
Debug,
|
||||
_npos::codec::Encode,
|
||||
_npos::codec::Decode,
|
||||
_npos::scale_info::TypeInfo,
|
||||
)])
|
||||
};
|
||||
|
||||
let struct_name = syn::Ident::new("solution", proc_macro2::Span::call_site());
|
||||
let assignment_name = syn::Ident::new("all_assignments", proc_macro2::Span::call_site());
|
||||
|
||||
let from_impl = from_impl(&struct_name, count);
|
||||
let into_impl = into_impl(&assignment_name, count, weight_type.clone());
|
||||
let from_index_impl = crate::index_assignment::from_impl(&struct_name, count);
|
||||
|
||||
Ok(quote! (
|
||||
/// A struct to encode a election assignment in a compact way.
|
||||
#derives_and_maybe_compact_encoding
|
||||
#vis struct #ident { #single #rest }
|
||||
|
||||
use _npos::__OrInvalidIndex;
|
||||
impl _npos::NposSolution for #ident {
|
||||
const LIMIT: usize = #count;
|
||||
type VoterIndex = #voter_type;
|
||||
type TargetIndex = #target_type;
|
||||
type Accuracy = #weight_type;
|
||||
|
||||
fn remove_voter(&mut self, to_remove: Self::VoterIndex) -> bool {
|
||||
#remove_voter_impl
|
||||
return false
|
||||
}
|
||||
|
||||
fn from_assignment<FV, FT, A>(
|
||||
assignments: &[_npos::Assignment<A, #weight_type>],
|
||||
voter_index: FV,
|
||||
target_index: FT,
|
||||
) -> Result<Self, _npos::Error>
|
||||
where
|
||||
A: _npos::IdentifierT,
|
||||
for<'r> FV: Fn(&'r A) -> Option<Self::VoterIndex>,
|
||||
for<'r> FT: Fn(&'r A) -> Option<Self::TargetIndex>,
|
||||
{
|
||||
let mut #struct_name: #ident = Default::default();
|
||||
for _npos::Assignment { who, distribution } in assignments {
|
||||
match distribution.len() {
|
||||
0 => continue,
|
||||
#from_impl
|
||||
_ => {
|
||||
return Err(_npos::Error::SolutionTargetOverflow);
|
||||
}
|
||||
}
|
||||
};
|
||||
Ok(#struct_name)
|
||||
}
|
||||
|
||||
fn into_assignment<A: _npos::IdentifierT>(
|
||||
self,
|
||||
voter_at: impl Fn(Self::VoterIndex) -> Option<A>,
|
||||
target_at: impl Fn(Self::TargetIndex) -> Option<A>,
|
||||
) -> Result<_npos::sp_std::prelude::Vec<_npos::Assignment<A, #weight_type>>, _npos::Error> {
|
||||
let mut #assignment_name: _npos::sp_std::prelude::Vec<_npos::Assignment<A, #weight_type>> = Default::default();
|
||||
#into_impl
|
||||
Ok(#assignment_name)
|
||||
}
|
||||
|
||||
fn voter_count(&self) -> usize {
|
||||
let mut all_len = 0usize;
|
||||
#len_impl
|
||||
all_len
|
||||
}
|
||||
|
||||
fn edge_count(&self) -> usize {
|
||||
let mut all_edges = 0usize;
|
||||
#edge_count_impl
|
||||
all_edges
|
||||
}
|
||||
|
||||
fn unique_targets(&self) -> _npos::sp_std::prelude::Vec<Self::TargetIndex> {
|
||||
// NOTE: this implementation returns the targets sorted, but we don't use it yet per
|
||||
// se, nor is the API enforcing it.
|
||||
use _npos::sp_std::collections::btree_set::BTreeSet;
|
||||
let mut all_targets: BTreeSet<Self::TargetIndex> = BTreeSet::new();
|
||||
let mut maybe_insert_target = |t: Self::TargetIndex| {
|
||||
all_targets.insert(t);
|
||||
};
|
||||
|
||||
#unique_targets_impl
|
||||
|
||||
all_targets.into_iter().collect()
|
||||
}
|
||||
}
|
||||
|
||||
type __IndexAssignment = _npos::IndexAssignment<
|
||||
<#ident as _npos::NposSolution>::VoterIndex,
|
||||
<#ident as _npos::NposSolution>::TargetIndex,
|
||||
<#ident as _npos::NposSolution>::Accuracy,
|
||||
>;
|
||||
impl<'a> _npos::sp_std::convert::TryFrom<&'a [__IndexAssignment]> for #ident {
|
||||
type Error = _npos::Error;
|
||||
fn try_from(index_assignments: &'a [__IndexAssignment]) -> Result<Self, Self::Error> {
|
||||
let mut #struct_name = #ident::default();
|
||||
|
||||
for _npos::IndexAssignment { who, distribution } in index_assignments {
|
||||
match distribution.len() {
|
||||
0 => {}
|
||||
#from_index_impl
|
||||
_ => {
|
||||
return Err(_npos::Error::SolutionTargetOverflow);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Ok(#struct_name)
|
||||
}
|
||||
}
|
||||
))
|
||||
}
|
||||
|
||||
fn remove_voter_impl(count: usize) -> TokenStream2 {
|
||||
let field_name = vote_field(1);
|
||||
let single = quote! {
|
||||
if let Some(idx) = self.#field_name.iter().position(|(x, _)| *x == to_remove) {
|
||||
self.#field_name.remove(idx);
|
||||
return true
|
||||
}
|
||||
};
|
||||
|
||||
let rest = (2..=count)
|
||||
.map(|c| {
|
||||
let field_name = vote_field(c);
|
||||
quote! {
|
||||
if let Some(idx) = self.#field_name.iter().position(|(x, _, _)| *x == to_remove) {
|
||||
self.#field_name.remove(idx);
|
||||
return true
|
||||
}
|
||||
}
|
||||
})
|
||||
.collect::<TokenStream2>();
|
||||
|
||||
quote! {
|
||||
#single
|
||||
#rest
|
||||
}
|
||||
}
|
||||
|
||||
fn len_impl(count: usize) -> TokenStream2 {
|
||||
(1..=count)
|
||||
.map(|c| {
|
||||
let field_name = vote_field(c);
|
||||
quote!(
|
||||
all_len = all_len.saturating_add(self.#field_name.len());
|
||||
)
|
||||
})
|
||||
.collect::<TokenStream2>()
|
||||
}
|
||||
|
||||
fn edge_count_impl(count: usize) -> TokenStream2 {
|
||||
(1..=count)
|
||||
.map(|c| {
|
||||
let field_name = vote_field(c);
|
||||
quote!(
|
||||
all_edges = all_edges.saturating_add(
|
||||
self.#field_name.len().saturating_mul(#c as usize)
|
||||
);
|
||||
)
|
||||
})
|
||||
.collect::<TokenStream2>()
|
||||
}
|
||||
|
||||
fn unique_targets_impl(count: usize) -> TokenStream2 {
|
||||
let unique_targets_impl_single = {
|
||||
let field_name = vote_field(1);
|
||||
quote! {
|
||||
self.#field_name.iter().for_each(|(_, t)| {
|
||||
maybe_insert_target(*t);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
let unique_targets_impl_rest = (2..=count)
|
||||
.map(|c| {
|
||||
let field_name = vote_field(c);
|
||||
quote! {
|
||||
self.#field_name.iter().for_each(|(_, inners, t_last)| {
|
||||
inners.iter().for_each(|(t, _)| {
|
||||
maybe_insert_target(*t);
|
||||
});
|
||||
maybe_insert_target(*t_last);
|
||||
});
|
||||
}
|
||||
})
|
||||
.collect::<TokenStream2>();
|
||||
|
||||
quote! {
|
||||
#unique_targets_impl_single
|
||||
#unique_targets_impl_rest
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn from_impl(struct_name: &syn::Ident, count: usize) -> TokenStream2 {
|
||||
let from_impl_single = {
|
||||
let field = vote_field(1);
|
||||
let push_code = from_impl_single_push_code();
|
||||
quote!(1 => #struct_name.#field.#push_code,)
|
||||
};
|
||||
|
||||
let from_impl_rest = (2..=count)
|
||||
.map(|c| {
|
||||
let field = vote_field(c);
|
||||
let push_code = from_impl_rest_push_code(c);
|
||||
quote!(#c => #struct_name.#field.#push_code,)
|
||||
})
|
||||
.collect::<TokenStream2>();
|
||||
|
||||
quote!(
|
||||
#from_impl_single
|
||||
#from_impl_rest
|
||||
)
|
||||
}
|
||||
|
||||
pub(crate) fn into_impl(
|
||||
assignments: &syn::Ident,
|
||||
count: usize,
|
||||
per_thing: syn::Type,
|
||||
) -> TokenStream2 {
|
||||
let into_impl_single = {
|
||||
let name = vote_field(1);
|
||||
quote!(
|
||||
for (voter_index, target_index) in self.#name {
|
||||
#assignments.push(_npos::Assignment {
|
||||
who: voter_at(voter_index).or_invalid_index()?,
|
||||
distribution: vec![
|
||||
(target_at(target_index).or_invalid_index()?, #per_thing::one())
|
||||
],
|
||||
})
|
||||
}
|
||||
)
|
||||
};
|
||||
|
||||
let into_impl_rest = (2..=count)
|
||||
.map(|c| {
|
||||
let name = vote_field(c);
|
||||
quote!(
|
||||
for (voter_index, inners, t_last_idx) in self.#name {
|
||||
let mut sum = #per_thing::zero();
|
||||
let mut inners_parsed = inners
|
||||
.iter()
|
||||
.map(|(ref t_idx, p)| {
|
||||
sum = _npos::sp_arithmetic::traits::Saturating::saturating_add(sum, *p);
|
||||
let target = target_at(*t_idx).or_invalid_index()?;
|
||||
Ok((target, *p))
|
||||
})
|
||||
.collect::<Result<_npos::sp_std::prelude::Vec<(A, #per_thing)>, _npos::Error>>()?;
|
||||
|
||||
if sum >= #per_thing::one() {
|
||||
return Err(_npos::Error::SolutionWeightOverflow);
|
||||
}
|
||||
|
||||
// defensive only. Since Percent doesn't have `Sub`.
|
||||
let p_last = _npos::sp_arithmetic::traits::Saturating::saturating_sub(
|
||||
#per_thing::one(),
|
||||
sum,
|
||||
);
|
||||
|
||||
inners_parsed.push((target_at(t_last_idx).or_invalid_index()?, p_last));
|
||||
|
||||
#assignments.push(_npos::Assignment {
|
||||
who: voter_at(voter_index).or_invalid_index()?,
|
||||
distribution: inners_parsed,
|
||||
});
|
||||
}
|
||||
)
|
||||
})
|
||||
.collect::<TokenStream2>();
|
||||
|
||||
quote!(
|
||||
#into_impl_single
|
||||
#into_impl_rest
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,350 @@
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Copyright (C) 2019-2022 Parity Technologies (UK) Ltd.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//! Tests for solution-type.
|
||||
|
||||
#![cfg(test)]
|
||||
|
||||
use crate::{mock::*, IndexAssignment, NposSolution};
|
||||
use rand::SeedableRng;
|
||||
use std::convert::TryInto;
|
||||
|
||||
mod solution_type {
|
||||
use super::*;
|
||||
use codec::{Decode, Encode};
|
||||
// these need to come from the same dev-dependency `sp-npos-elections`, not from the crate.
|
||||
use crate::{generate_solution_type, Assignment, Error as NposError, NposSolution};
|
||||
use sp_std::{convert::TryInto, fmt::Debug};
|
||||
|
||||
#[allow(dead_code)]
|
||||
mod __private {
|
||||
// This is just to make sure that the solution can be generated in a scope without any
|
||||
// imports.
|
||||
use crate::generate_solution_type;
|
||||
generate_solution_type!(
|
||||
#[compact]
|
||||
struct InnerTestSolutionIsolated::<VoterIndex = u32, TargetIndex = u8, Accuracy = sp_runtime::Percent>(12)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn solution_struct_works_with_and_without_compact() {
|
||||
// we use u32 size to make sure compact is smaller.
|
||||
let without_compact = {
|
||||
generate_solution_type!(
|
||||
pub struct InnerTestSolution::<
|
||||
VoterIndex = u32,
|
||||
TargetIndex = u32,
|
||||
Accuracy = TestAccuracy,
|
||||
>(16)
|
||||
);
|
||||
let solution = InnerTestSolution {
|
||||
votes1: vec![(2, 20), (4, 40)],
|
||||
votes2: vec![(1, [(10, p(80))], 11), (5, [(50, p(85))], 51)],
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
solution.encode().len()
|
||||
};
|
||||
|
||||
let with_compact = {
|
||||
generate_solution_type!(
|
||||
#[compact]
|
||||
pub struct InnerTestSolutionCompact::<
|
||||
VoterIndex = u32,
|
||||
TargetIndex = u32,
|
||||
Accuracy = TestAccuracy,
|
||||
>(16)
|
||||
);
|
||||
let compact = InnerTestSolutionCompact {
|
||||
votes1: vec![(2, 20), (4, 40)],
|
||||
votes2: vec![(1, [(10, p(80))], 11), (5, [(50, p(85))], 51)],
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
compact.encode().len()
|
||||
};
|
||||
|
||||
assert!(with_compact < without_compact);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn solution_struct_is_codec() {
|
||||
let solution = TestSolution {
|
||||
votes1: vec![(2, 20), (4, 40)],
|
||||
votes2: vec![(1, [(10, p(80))], 11), (5, [(50, p(85))], 51)],
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let encoded = solution.encode();
|
||||
|
||||
assert_eq!(solution, Decode::decode(&mut &encoded[..]).unwrap());
|
||||
assert_eq!(solution.voter_count(), 4);
|
||||
assert_eq!(solution.edge_count(), 2 + 4);
|
||||
assert_eq!(solution.unique_targets(), vec![10, 11, 20, 40, 50, 51]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn remove_voter_works() {
|
||||
let mut solution = TestSolution {
|
||||
votes1: vec![(0, 2), (1, 6)],
|
||||
votes2: vec![(2, [(0, p(80))], 1), (3, [(7, p(85))], 8)],
|
||||
votes3: vec![(4, [(3, p(50)), (4, p(25))], 5)],
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
assert!(!solution.remove_voter(11));
|
||||
assert!(solution.remove_voter(2));
|
||||
assert_eq!(
|
||||
solution,
|
||||
TestSolution {
|
||||
votes1: vec![(0, 2), (1, 6)],
|
||||
votes2: vec![(3, [(7, p(85))], 8)],
|
||||
votes3: vec![(4, [(3, p(50)), (4, p(25))], 5,)],
|
||||
..Default::default()
|
||||
},
|
||||
);
|
||||
|
||||
assert!(solution.remove_voter(4));
|
||||
assert_eq!(
|
||||
solution,
|
||||
TestSolution {
|
||||
votes1: vec![(0, 2), (1, 6)],
|
||||
votes2: vec![(3, [(7, p(85))], 8)],
|
||||
..Default::default()
|
||||
},
|
||||
);
|
||||
|
||||
assert!(solution.remove_voter(1));
|
||||
assert_eq!(
|
||||
solution,
|
||||
TestSolution {
|
||||
votes1: vec![(0, 2)],
|
||||
votes2: vec![(3, [(7, p(85))], 8),],
|
||||
..Default::default()
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn from_and_into_assignment_works() {
|
||||
let voters = vec![2 as AccountId, 4, 1, 5, 3];
|
||||
let targets = vec![
|
||||
10 as AccountId,
|
||||
11,
|
||||
20, // 2
|
||||
30,
|
||||
31, // 4
|
||||
32,
|
||||
40, // 6
|
||||
50,
|
||||
51, // 8
|
||||
];
|
||||
|
||||
let assignments = vec![
|
||||
Assignment { who: 2 as AccountId, distribution: vec![(20u64, p(100))] },
|
||||
Assignment { who: 4, distribution: vec![(40, p(100))] },
|
||||
Assignment { who: 1, distribution: vec![(10, p(80)), (11, p(20))] },
|
||||
Assignment { who: 5, distribution: vec![(50, p(85)), (51, p(15))] },
|
||||
Assignment { who: 3, distribution: vec![(30, p(50)), (31, p(25)), (32, p(25))] },
|
||||
];
|
||||
|
||||
let voter_index = |a: &AccountId| -> Option<u32> {
|
||||
voters.iter().position(|x| x == a).map(TryInto::try_into).unwrap().ok()
|
||||
};
|
||||
let target_index = |a: &AccountId| -> Option<u16> {
|
||||
targets.iter().position(|x| x == a).map(TryInto::try_into).unwrap().ok()
|
||||
};
|
||||
|
||||
let solution =
|
||||
TestSolution::from_assignment(&assignments, voter_index, target_index).unwrap();
|
||||
|
||||
// basically number of assignments that it is encoding.
|
||||
assert_eq!(solution.voter_count(), assignments.len());
|
||||
assert_eq!(
|
||||
solution.edge_count(),
|
||||
assignments.iter().fold(0, |a, b| a + b.distribution.len()),
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
solution,
|
||||
TestSolution {
|
||||
votes1: vec![(0, 2), (1, 6)],
|
||||
votes2: vec![(2, [(0, p(80))], 1), (3, [(7, p(85))], 8)],
|
||||
votes3: vec![(4, [(3, p(50)), (4, p(25))], 5)],
|
||||
..Default::default()
|
||||
}
|
||||
);
|
||||
|
||||
assert_eq!(solution.unique_targets(), vec![0, 1, 2, 3, 4, 5, 6, 7, 8]);
|
||||
|
||||
let voter_at = |a: u32| -> Option<AccountId> {
|
||||
voters.get(<u32 as TryInto<usize>>::try_into(a).unwrap()).cloned()
|
||||
};
|
||||
let target_at = |a: u16| -> Option<AccountId> {
|
||||
targets.get(<u16 as TryInto<usize>>::try_into(a).unwrap()).cloned()
|
||||
};
|
||||
|
||||
assert_eq!(solution.into_assignment(voter_at, target_at).unwrap(), assignments);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unique_targets_len_edge_count_works() {
|
||||
// we don't really care about voters here so all duplicates. This is not invalid per se.
|
||||
let solution = TestSolution {
|
||||
votes1: vec![(99, 1), (99, 2)],
|
||||
votes2: vec![(99, [(3, p(10))], 7), (99, [(4, p(10))], 8)],
|
||||
votes3: vec![(99, [(11, p(10)), (12, p(10))], 13)],
|
||||
// ensure the last one is also counted.
|
||||
votes16: vec![(
|
||||
99,
|
||||
[
|
||||
(66, p(10)),
|
||||
(66, p(10)),
|
||||
(66, p(10)),
|
||||
(66, p(10)),
|
||||
(66, p(10)),
|
||||
(66, p(10)),
|
||||
(66, p(10)),
|
||||
(66, p(10)),
|
||||
(66, p(10)),
|
||||
(66, p(10)),
|
||||
(66, p(10)),
|
||||
(66, p(10)),
|
||||
(66, p(10)),
|
||||
(66, p(10)),
|
||||
(66, p(10)),
|
||||
],
|
||||
67,
|
||||
)],
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
assert_eq!(solution.unique_targets(), vec![1, 2, 3, 4, 7, 8, 11, 12, 13, 66, 67]);
|
||||
assert_eq!(solution.edge_count(), 2 + (2 * 2) + 3 + 16);
|
||||
assert_eq!(solution.voter_count(), 6);
|
||||
|
||||
// this one has some duplicates.
|
||||
let solution = TestSolution {
|
||||
votes1: vec![(99, 1), (99, 1)],
|
||||
votes2: vec![(99, [(3, p(10))], 7), (99, [(4, p(10))], 8)],
|
||||
votes3: vec![(99, [(11, p(10)), (11, p(10))], 13)],
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
assert_eq!(solution.unique_targets(), vec![1, 3, 4, 7, 8, 11, 13]);
|
||||
assert_eq!(solution.edge_count(), 2 + (2 * 2) + 3);
|
||||
assert_eq!(solution.voter_count(), 5);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn solution_into_assignment_must_report_overflow() {
|
||||
// in votes2
|
||||
let solution = TestSolution {
|
||||
votes1: Default::default(),
|
||||
votes2: vec![(0, [(1, p(100))], 2)],
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let voter_at = |a: u32| -> Option<AccountId> { Some(a as AccountId) };
|
||||
let target_at = |a: u16| -> Option<AccountId> { Some(a as AccountId) };
|
||||
|
||||
assert_eq!(
|
||||
solution.into_assignment(&voter_at, &target_at).unwrap_err(),
|
||||
NposError::SolutionWeightOverflow,
|
||||
);
|
||||
|
||||
// in votes3 onwards
|
||||
let solution = TestSolution {
|
||||
votes1: Default::default(),
|
||||
votes2: Default::default(),
|
||||
votes3: vec![(0, [(1, p(70)), (2, p(80))], 3)],
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
assert_eq!(
|
||||
solution.into_assignment(&voter_at, &target_at).unwrap_err(),
|
||||
NposError::SolutionWeightOverflow,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn target_count_overflow_is_detected() {
|
||||
let voter_index = |a: &AccountId| -> Option<u32> { Some(*a as u32) };
|
||||
let target_index = |a: &AccountId| -> Option<u16> { Some(*a as u16) };
|
||||
|
||||
let assignments = vec![Assignment {
|
||||
who: 1 as AccountId,
|
||||
distribution: (10..27).map(|i| (i as AccountId, p(i as u8))).collect::<Vec<_>>(),
|
||||
}];
|
||||
|
||||
let solution = TestSolution::from_assignment(&assignments, voter_index, target_index);
|
||||
assert_eq!(solution.unwrap_err(), NposError::SolutionTargetOverflow);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn zero_target_count_is_ignored() {
|
||||
let voters = vec![1 as AccountId, 2];
|
||||
let targets = vec![10 as AccountId, 11];
|
||||
|
||||
let assignments = vec![
|
||||
Assignment { who: 1 as AccountId, distribution: vec![(10, p(50)), (11, p(50))] },
|
||||
Assignment { who: 2, distribution: vec![] },
|
||||
];
|
||||
|
||||
let voter_index = |a: &AccountId| -> Option<u32> {
|
||||
voters.iter().position(|x| x == a).map(TryInto::try_into).unwrap().ok()
|
||||
};
|
||||
let target_index = |a: &AccountId| -> Option<u16> {
|
||||
targets.iter().position(|x| x == a).map(TryInto::try_into).unwrap().ok()
|
||||
};
|
||||
|
||||
let solution =
|
||||
TestSolution::from_assignment(&assignments, voter_index, target_index).unwrap();
|
||||
|
||||
assert_eq!(
|
||||
solution,
|
||||
TestSolution {
|
||||
votes1: Default::default(),
|
||||
votes2: vec![(0, [(0, p(50))], 1)],
|
||||
..Default::default()
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn index_assignments_generate_same_solution_as_plain_assignments() {
|
||||
let rng = rand::rngs::SmallRng::seed_from_u64(0);
|
||||
|
||||
let (voters, assignments, candidates) = generate_random_votes(1000, 2500, rng);
|
||||
let voter_index = make_voter_fn(&voters);
|
||||
let target_index = make_target_fn(&candidates);
|
||||
|
||||
let solution =
|
||||
TestSolution::from_assignment(&assignments, &voter_index, &target_index).unwrap();
|
||||
|
||||
let index_assignments = assignments
|
||||
.into_iter()
|
||||
.map(|assignment| IndexAssignment::new(&assignment, &voter_index, &target_index))
|
||||
.collect::<Result<Vec<_>, _>>()
|
||||
.unwrap();
|
||||
|
||||
let index_compact = index_assignments.as_slice().try_into().unwrap();
|
||||
|
||||
assert_eq!(solution, index_compact);
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
use frame_election_provider_solution_type::generate_solution_type;
|
||||
|
||||
generate_solution_type!(pub struct TestSolution::<
|
||||
VoterIndex = u16,
|
||||
TargetIndex = u8,
|
||||
Perbill,
|
||||
>(8));
|
||||
|
||||
fn main() {}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
error: Expected binding: `Accuracy = ...`
|
||||
--> $DIR/missing_accuracy.rs:6:2
|
||||
|
|
||||
6 | Perbill,
|
||||
| ^^^^^^^
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
use frame_election_provider_solution_type::generate_solution_type;
|
||||
|
||||
generate_solution_type!(pub struct TestSolution::<
|
||||
VoterIndex = u16,
|
||||
u8,
|
||||
Accuracy = Perbill,
|
||||
>(8));
|
||||
|
||||
fn main() {}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
error: Expected binding: `TargetIndex = ...`
|
||||
--> $DIR/missing_target.rs:5:2
|
||||
|
|
||||
5 | u8,
|
||||
| ^^
|
||||
@@ -0,0 +1,9 @@
|
||||
use frame_election_provider_solution_type::generate_solution_type;
|
||||
|
||||
generate_solution_type!(pub struct TestSolution::<
|
||||
u16,
|
||||
TargetIndex = u8,
|
||||
Accuracy = Perbill,
|
||||
>(8));
|
||||
|
||||
fn main() {}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
error: Expected binding: `VoterIndex = ...`
|
||||
--> $DIR/missing_voter.rs:4:2
|
||||
|
|
||||
4 | u16,
|
||||
| ^^^
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
use frame_election_provider_solution_type::generate_solution_type;
|
||||
|
||||
generate_solution_type!(pub struct TestSolution::<
|
||||
u16,
|
||||
u8,
|
||||
Perbill,
|
||||
>(8));
|
||||
|
||||
fn main() {}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
error: Expected binding: `VoterIndex = ...`
|
||||
--> $DIR/no_annotations.rs:4:2
|
||||
|
|
||||
4 | u16,
|
||||
| ^^^
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
use frame_election_provider_solution_type::generate_solution_type;
|
||||
|
||||
generate_solution_type!(pub struct TestSolution::<
|
||||
TargetIndex = u16,
|
||||
VoterIndex = u8,
|
||||
Accuracy = Perbill,
|
||||
>(8));
|
||||
|
||||
fn main() {}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
error: Expected `VoterIndex`
|
||||
--> $DIR/swap_voter_target.rs:4:2
|
||||
|
|
||||
4 | TargetIndex = u16,
|
||||
| ^^^^^^^^^^^
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
use frame_election_provider_solution_type::generate_solution_type;
|
||||
|
||||
generate_solution_type!(
|
||||
#[pages(1)] pub struct TestSolution::<
|
||||
VoterIndex = u8,
|
||||
TargetIndex = u16,
|
||||
Accuracy = Perbill,
|
||||
>(8)
|
||||
);
|
||||
|
||||
fn main() {}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
error: compact solution can accept only #[compact]
|
||||
--> $DIR/wrong_attribute.rs:4:2
|
||||
|
|
||||
4 | #[pages(1)] pub struct TestSolution::<
|
||||
| ^^^^^^^^^^^
|
||||
@@ -172,6 +172,7 @@ use sp_runtime::traits::Bounded;
|
||||
use sp_std::{fmt::Debug, prelude::*};
|
||||
|
||||
/// Re-export some type as they are used in the interface.
|
||||
pub use frame_election_provider_solution_type::generate_solution_type;
|
||||
pub use sp_arithmetic::PerThing;
|
||||
pub use sp_npos_elections::{
|
||||
Assignment, ElectionResult, ExtendedBalance, IdentifierT, PerThing128, Support, Supports,
|
||||
|
||||
Reference in New Issue
Block a user