diff --git a/codegen/src/api/mod.rs b/codegen/src/api/mod.rs index 0a3dd05d0d..68ea2a4b2e 100644 --- a/codegen/src/api/mod.rs +++ b/codegen/src/api/mod.rs @@ -147,12 +147,12 @@ 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), - ), + // BTreeMap and BTreeSet impose an `Ord` constraint on their key types. This + // can cause an issue with generated code that doesn't impl `Ord` by default. + // Decoding them to Vec by default (KeyedVec is just an alias for Vec with + // suitable type params) avoids these issues. + ("BTreeMap", parse_quote!(::subxt::KeyedVec)), + ("BTreeSet", parse_quote!(::std::vec::Vec)), ] .iter() .map(|(path, substitute): &(&str, syn::TypePath)| { diff --git a/subxt/src/lib.rs b/subxt/src/lib.rs index 5fb4bdf320..ccd8a91127 100644 --- a/subxt/src/lib.rs +++ b/subxt/src/lib.rs @@ -242,40 +242,7 @@ 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, 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, -} - -// 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)) - } -} +/// This represents a key-value collection and is SCALE compatible +/// with collections like BTreeMap. This has the same type params +/// as `BTreeMap` which allows us to easily swap the two during codegen. +pub type KeyedVec = Vec<(K, V)>; diff --git a/subxt/tests/integration/codegen/polkadot.rs b/subxt/tests/integration/codegen/polkadot.rs index 290ffa0447..99641f1616 100644 --- a/subxt/tests/integration/codegen/polkadot.rs +++ b/subxt/tests/integration/codegen/polkadot.rs @@ -1,3 +1,4 @@ +// Note [jsdw]: generated from polkadot 0.9.13-82616422d0-aarch64-macos #[allow(dead_code, unused_imports, non_camel_case_types)] pub mod api { use super::api as root_mod; @@ -20073,9 +20074,7 @@ pub mod api { #[derive( :: subxt :: codec :: Encode, :: subxt :: codec :: Decode, Debug, )] - pub struct BoundedBTreeMap<_0, _1>( - pub ::std::collections::BTreeMap<_0, _1>, - ); + pub struct BoundedBTreeMap<_0, _1>(pub ::subxt::KeyedVec<_0, _1>); } pub mod bounded_vec { use super::runtime_types; @@ -23047,7 +23046,7 @@ pub mod api { #[derive(:: subxt :: codec :: Encode, :: subxt :: codec :: Decode, Debug)] pub struct EraRewardPoints<_0> { pub total: ::core::primitive::u32, - pub individual: ::std::collections::BTreeMap<_0, ::core::primitive::u32>, + pub individual: ::subxt::KeyedVec<_0, ::core::primitive::u32>, } #[derive(:: subxt :: codec :: Encode, :: subxt :: codec :: Decode, Debug)] pub struct Exposure<_0, _1> {