More renaming to move away from phragmen. (#6886)

This commit is contained in:
Kian Paimani
2020-08-13 23:30:22 +02:00
committed by GitHub
parent 0777a93532
commit 775e84cc04
8 changed files with 166 additions and 166 deletions
@@ -78,7 +78,7 @@ fn into_impl(count: usize, per_thing: syn::Type) -> TokenStream2 {
let name = field_name_for(1);
quote!(
for (voter_index, target_index) in self.#name {
assignments.push(_phragmen::Assignment {
assignments.push(_npos::Assignment {
who: voter_at(voter_index).or_invalid_index()?,
distribution: vec![
(target_at(target_index).or_invalid_index()?, #per_thing::one())
@@ -93,16 +93,16 @@ fn into_impl(count: usize, per_thing: syn::Type) -> TokenStream2 {
quote!(
for (voter_index, (t1_idx, p1), t2_idx) in self.#name {
if p1 >= #per_thing::one() {
return Err(_phragmen::Error::CompactStakeOverflow);
return Err(_npos::Error::CompactStakeOverflow);
}
// defensive only. Since Percent doesn't have `Sub`.
let p2 = _phragmen::sp_arithmetic::traits::Saturating::saturating_sub(
let p2 = _npos::sp_arithmetic::traits::Saturating::saturating_sub(
#per_thing::one(),
p1,
);
assignments.push( _phragmen::Assignment {
assignments.push( _npos::Assignment {
who: voter_at(voter_index).or_invalid_index()?,
distribution: vec![
(target_at(t1_idx).or_invalid_index()?, p1),
@@ -121,25 +121,25 @@ fn into_impl(count: usize, per_thing: syn::Type) -> TokenStream2 {
let mut inners_parsed = inners
.iter()
.map(|(ref t_idx, p)| {
sum = _phragmen::sp_arithmetic::traits::Saturating::saturating_add(sum, *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<Vec<(A, #per_thing)>, _phragmen::Error>>()?;
.collect::<Result<Vec<(A, #per_thing)>, _npos::Error>>()?;
if sum >= #per_thing::one() {
return Err(_phragmen::Error::CompactStakeOverflow);
return Err(_npos::Error::CompactStakeOverflow);
}
// defensive only. Since Percent doesn't have `Sub`.
let p_last = _phragmen::sp_arithmetic::traits::Saturating::saturating_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(_phragmen::Assignment {
assignments.push(_npos::Assignment {
who: voter_at(voter_index).or_invalid_index()?,
distribution: inners_parsed,
});
@@ -165,38 +165,38 @@ pub(crate) fn assignment(
let into_impl = into_impl(count, weight_type.clone());
quote!(
use _phragmen::__OrInvalidIndex;
use _npos::__OrInvalidIndex;
impl #ident {
pub fn from_assignment<FV, FT, A>(
assignments: Vec<_phragmen::Assignment<A, #weight_type>>,
assignments: Vec<_npos::Assignment<A, #weight_type>>,
index_of_voter: FV,
index_of_target: FT,
) -> Result<Self, _phragmen::Error>
) -> Result<Self, _npos::Error>
where
A: _phragmen::IdentifierT,
A: _npos::IdentifierT,
for<'r> FV: Fn(&'r A) -> Option<#voter_type>,
for<'r> FT: Fn(&'r A) -> Option<#target_type>,
{
let mut compact: #ident = Default::default();
for _phragmen::Assignment { who, distribution } in assignments {
for _npos::Assignment { who, distribution } in assignments {
match distribution.len() {
0 => continue,
#from_impl
_ => {
return Err(_phragmen::Error::CompactTargetOverflow);
return Err(_npos::Error::CompactTargetOverflow);
}
}
};
Ok(compact)
}
pub fn into_assignment<A: _phragmen::IdentifierT>(
pub fn into_assignment<A: _npos::IdentifierT>(
self,
voter_at: impl Fn(#voter_type) -> Option<A>,
target_at: impl Fn(#target_type) -> Option<A>,
) -> Result<Vec<_phragmen::Assignment<A, #weight_type>>, _phragmen::Error> {
let mut assignments: Vec<_phragmen::Assignment<A, #weight_type>> = Default::default();
) -> Result<Vec<_npos::Assignment<A, #weight_type>>, _npos::Error> {
let mut assignments: Vec<_npos::Assignment<A, #weight_type>> = Default::default();
#into_impl
Ok(assignments)
}
@@ -49,9 +49,9 @@ fn decode_impl(
quote! {
let #name =
<
Vec<(_phragmen::codec::Compact<#voter_type>, _phragmen::codec::Compact<#target_type>)>
Vec<(_npos::codec::Compact<#voter_type>, _npos::codec::Compact<#target_type>)>
as
_phragmen::codec::Decode
_npos::codec::Decode
>::decode(value)?;
let #name = #name
.into_iter()
@@ -66,12 +66,12 @@ fn decode_impl(
let #name =
<
Vec<(
_phragmen::codec::Compact<#voter_type>,
(_phragmen::codec::Compact<#target_type>, _phragmen::codec::Compact<#weight_type>),
_phragmen::codec::Compact<#target_type>,
_npos::codec::Compact<#voter_type>,
(_npos::codec::Compact<#target_type>, _npos::codec::Compact<#weight_type>),
_npos::codec::Compact<#target_type>,
)>
as
_phragmen::codec::Decode
_npos::codec::Decode
>::decode(value)?;
let #name = #name
.into_iter()
@@ -91,11 +91,11 @@ fn decode_impl(
let #name =
<
Vec<(
_phragmen::codec::Compact<#voter_type>,
[(_phragmen::codec::Compact<#target_type>, _phragmen::codec::Compact<#weight_type>); #c-1],
_phragmen::codec::Compact<#target_type>,
_npos::codec::Compact<#voter_type>,
[(_npos::codec::Compact<#target_type>, _npos::codec::Compact<#weight_type>); #c-1],
_npos::codec::Compact<#target_type>,
)>
as _phragmen::codec::Decode
as _npos::codec::Decode
>::decode(value)?;
let #name = #name
.into_iter()
@@ -115,8 +115,8 @@ fn decode_impl(
}).collect::<TokenStream2>();
quote!(
impl _phragmen::codec::Decode for #ident {
fn decode<I: _phragmen::codec::Input>(value: &mut I) -> Result<Self, _phragmen::codec::Error> {
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_double
#decode_impl_rest
@@ -139,8 +139,8 @@ fn encode_impl(ident: syn::Ident, count: usize) -> TokenStream2 {
let #name = self.#name
.iter()
.map(|(v, t)| (
_phragmen::codec::Compact(v.clone()),
_phragmen::codec::Compact(t.clone()),
_npos::codec::Compact(v.clone()),
_npos::codec::Compact(t.clone()),
))
.collect::<Vec<_>>();
#name.encode_to(&mut r);
@@ -153,12 +153,12 @@ fn encode_impl(ident: syn::Ident, count: usize) -> TokenStream2 {
let #name = self.#name
.iter()
.map(|(v, (t1, w), t2)| (
_phragmen::codec::Compact(v.clone()),
_npos::codec::Compact(v.clone()),
(
_phragmen::codec::Compact(t1.clone()),
_phragmen::codec::Compact(w.clone())
_npos::codec::Compact(t1.clone()),
_npos::codec::Compact(w.clone())
),
_phragmen::codec::Compact(t2.clone()),
_npos::codec::Compact(t2.clone()),
))
.collect::<Vec<_>>();
#name.encode_to(&mut r);
@@ -171,8 +171,8 @@ fn encode_impl(ident: syn::Ident, count: usize) -> TokenStream2 {
// we use the knowledge of the length to avoid copy_from_slice.
let inners_compact_array = (0..c-1).map(|i|
quote!{(
_phragmen::codec::Compact(inner[#i].0.clone()),
_phragmen::codec::Compact(inner[#i].1.clone()),
_npos::codec::Compact(inner[#i].0.clone()),
_npos::codec::Compact(inner[#i].1.clone()),
),}
).collect::<TokenStream2>();
@@ -180,9 +180,9 @@ fn encode_impl(ident: syn::Ident, count: usize) -> TokenStream2 {
let #name = self.#name
.iter()
.map(|(v, inner, t_last)| (
_phragmen::codec::Compact(v.clone()),
_npos::codec::Compact(v.clone()),
[ #inners_compact_array ],
_phragmen::codec::Compact(t_last.clone()),
_npos::codec::Compact(t_last.clone()),
))
.collect::<Vec<_>>();
#name.encode_to(&mut r);
@@ -190,7 +190,7 @@ fn encode_impl(ident: syn::Ident, count: usize) -> TokenStream2 {
}).collect::<TokenStream2>();
quote!(
impl _phragmen::codec::Encode for #ident {
impl _npos::codec::Encode for #ident {
fn encode(&self) -> Vec<u8> {
let mut r = vec![];
#encode_impl_single
@@ -181,7 +181,7 @@ fn struct_def(
}
} else {
// automatically derived.
quote!(#[derive(Default, PartialEq, Eq, Clone, Debug, _phragmen::codec::Encode, _phragmen::codec::Decode)])
quote!(#[derive(Default, PartialEq, Eq, Clone, Debug, _npos::codec::Encode, _npos::codec::Decode)])
};
Ok(quote! (
@@ -189,7 +189,7 @@ fn struct_def(
#derives_and_maybe_compact_encoding
#vis struct #ident { #singles #doubles #rest }
impl _phragmen::VotingLimit for #ident {
impl _npos::VotingLimit for #ident {
const LIMIT: usize = #count;
}
@@ -220,13 +220,13 @@ fn struct_def(
fn imports() -> Result<TokenStream2> {
if std::env::var("CARGO_PKG_NAME").unwrap() == "sp-npos-elections" {
Ok(quote! {
use crate as _phragmen;
use crate as _npos;
})
} else {
match crate_name("sp-npos-elections") {
Ok(sp_npos_elections) => {
let ident = syn::Ident::new(&sp_npos_elections, Span::call_site());
Ok(quote!( extern crate #ident as _phragmen; ))
Ok(quote!( extern crate #ident as _npos; ))
},
Err(e) => Err(syn::Error::new(Span::call_site(), &e)),
}