From 1783291a96f8552a96de0b270495393c88137837 Mon Sep 17 00:00:00 2001 From: James Wilson Date: Wed, 16 Feb 2022 16:29:21 +0000 Subject: [PATCH 1/3] Add hardcoded override to ElectionScore so that we can give it the traits it needs --- codegen/src/api/mod.rs | 4 ++++ subxt/src/lib.rs | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/codegen/src/api/mod.rs b/codegen/src/api/mod.rs index 1681ea9919..0606f9586a 100644 --- a/codegen/src/api/mod.rs +++ b/codegen/src/api/mod.rs @@ -147,6 +147,10 @@ impl RuntimeGenerator { "frame_support::traits::misc::WrapperKeepOpaque", parse_quote!(::subxt::WrapperKeepOpaque), ), + ( + "sp_npos_elections::ElectionScore", + parse_quote!(::subxt::ElectionScore), + ), ] .iter() .map(|(path, substitute): &(&str, syn::TypePath)| { diff --git a/subxt/src/lib.rs b/subxt/src/lib.rs index 21a0cfd494..4eb4e9eabe 100644 --- a/subxt/src/lib.rs +++ b/subxt/src/lib.rs @@ -239,3 +239,22 @@ impl PhantomDataSendSync { unsafe impl Send for PhantomDataSendSync {} unsafe impl Sync for PhantomDataSendSync {} + +/// [`ElectionScore`] overrides any generated instance of `sp_npos_elections::ElectionScore` found +/// in the metadata, so that we can add some extra derives required for it to be used as the key +/// in a [`std::collections::BTreeMap`]. +#[derive(Encode, Decode, Debug, PartialOrd, Ord, PartialEq, Eq)] +pub struct ElectionScore { + /// The minimal winner, in terms of total backing stake. + /// + /// This parameter should be maximized. + pub minimal_stake: u128, + /// The sum of the total backing of all winners. + /// + /// This parameter should maximized + pub sum_stake: u128, + /// The sum squared of the total backing of all winners, aka. the variance. + /// + /// Ths parameter should be minimized. + pub sum_stake_squared: u128, +} From 876062331009ac7eec2b23100f34b3dea79b923d Mon Sep 17 00:00:00 2001 From: James Wilson Date: Wed, 16 Feb 2022 16:33:32 +0000 Subject: [PATCH 2/3] cargo fmt + another comment --- codegen/src/api/mod.rs | 2 ++ subxt/src/lib.rs | 24 ++++++++++++------------ 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/codegen/src/api/mod.rs b/codegen/src/api/mod.rs index 0606f9586a..923c7c019b 100644 --- a/codegen/src/api/mod.rs +++ b/codegen/src/api/mod.rs @@ -147,6 +147,8 @@ impl RuntimeGenerator { "frame_support::traits::misc::WrapperKeepOpaque", parse_quote!(::subxt::WrapperKeepOpaque), ), + // We override this because it's used as a key in a BTreeMap, and so we + // need to implement some extra derives for it for that to compile. ( "sp_npos_elections::ElectionScore", parse_quote!(::subxt::ElectionScore), diff --git a/subxt/src/lib.rs b/subxt/src/lib.rs index 4eb4e9eabe..f201ac2dba 100644 --- a/subxt/src/lib.rs +++ b/subxt/src/lib.rs @@ -245,16 +245,16 @@ unsafe impl Sync for PhantomDataSendSync {} /// in a [`std::collections::BTreeMap`]. #[derive(Encode, Decode, Debug, PartialOrd, Ord, PartialEq, Eq)] pub struct ElectionScore { - /// The minimal winner, in terms of total backing stake. - /// - /// This parameter should be maximized. - pub minimal_stake: u128, - /// The sum of the total backing of all winners. - /// - /// This parameter should maximized - pub sum_stake: u128, - /// The sum squared of the total backing of all winners, aka. the variance. - /// - /// Ths parameter should be minimized. - pub sum_stake_squared: u128, + /// The minimal winner, in terms of total backing stake. + /// + /// This parameter should be maximized. + pub minimal_stake: u128, + /// The sum of the total backing of all winners. + /// + /// This parameter should maximized + pub sum_stake: u128, + /// The sum squared of the total backing of all winners, aka. the variance. + /// + /// Ths parameter should be minimized. + pub sum_stake_squared: u128, } From 5d38d304a6faeec86099499fbe468447feca308a Mon Sep 17 00:00:00 2001 From: James Wilson Date: Wed, 16 Feb 2022 16:49:03 +0000 Subject: [PATCH 3/3] copy over custom trait impls too --- subxt/src/lib.rs | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/subxt/src/lib.rs b/subxt/src/lib.rs index f201ac2dba..37376e4401 100644 --- a/subxt/src/lib.rs +++ b/subxt/src/lib.rs @@ -243,7 +243,7 @@ unsafe impl Sync for PhantomDataSendSync {} /// [`ElectionScore`] overrides any generated instance of `sp_npos_elections::ElectionScore` found /// in the metadata, so that we can add some extra derives required for it to be used as the key /// in a [`std::collections::BTreeMap`]. -#[derive(Encode, Decode, Debug, PartialOrd, Ord, PartialEq, Eq)] +#[derive(Encode, Decode, Debug, PartialEq, Eq)] pub struct ElectionScore { /// The minimal winner, in terms of total backing stake. /// @@ -258,3 +258,22 @@ pub struct ElectionScore { /// Ths parameter should be minimized. pub sum_stake_squared: u128, } + +// These are copied from the original impl; they don't affect encoding/decoding but help +// to keep the ordering the same if we then work with the decoded results. +impl Ord for ElectionScore { + fn cmp(&self, other: &Self) -> std::cmp::Ordering { + // we delegate this to the lexicographic cmp of slices`, and to incorporate that we want the + // third element to be minimized, we swap them. + [self.minimal_stake, self.sum_stake, other.sum_stake_squared].cmp(&[ + other.minimal_stake, + other.sum_stake, + self.sum_stake_squared, + ]) + } +} +impl PartialOrd for ElectionScore { + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) + } +}