Skip to content

Commit

Permalink
Improve some gateway common types (paritytech#404)
Browse files Browse the repository at this point in the history
* Impl `From` instead of impl `Into`
* Remove `Deref` impl of `GenericTrusteeSessionInfo`
* Add doc for some types
* Remove useless `GenericAllSessionInfo` type

Signed-off-by: koushiro <koushiro.cqx@gmail.com>
  • Loading branch information
koushiro authored Dec 1, 2020
1 parent af6b803 commit a63da74
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 108 deletions.
2 changes: 1 addition & 1 deletion xpallets/gateway/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ impl<T: Trait> Module<T> {
info: &GenericTrusteeSessionInfo<T::AccountId>,
) -> Result<T::AccountId, DispatchError> {
let multi_addr =
T::DetermineMultisigAddress::calc_multisig(&info.trustee_list, info.threshold);
T::DetermineMultisigAddress::calc_multisig(&info.0.trustee_list, info.0.threshold);

// Each chain must have a distinct multisig address,
// duplicated multisig address is not allowed.
Expand Down
154 changes: 47 additions & 107 deletions xpallets/gateway/common/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
// Copyright 2019-2020 ChainX Project Authors. Licensed under GPL-3.0.

use sp_runtime::RuntimeDebug;
use sp_std::{convert::TryFrom, ops::Deref, prelude::Vec};

use codec::{Decode, Encode};
#[cfg(feature = "std")]
use serde::{Deserialize, Serialize};

// ChainX
use sp_runtime::RuntimeDebug;
use sp_std::{convert::TryFrom, prelude::Vec};

use chainx_primitives::Text;

use crate::traits::BytesLike;
use crate::utils::two_thirds_unsafe;

/// The config of trustee info.
#[derive(PartialEq, Clone, Encode, Decode, Default, RuntimeDebug)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "std", serde(rename_all = "camelCase"))]
Expand All @@ -21,16 +20,7 @@ pub struct TrusteeInfoConfig {
pub max_trustee_count: u32,
}

#[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "std", serde(rename_all = "camelCase"))]
pub struct TrusteeIntentionProps<TrusteeEntity: BytesLike> {
#[cfg_attr(feature = "std", serde(with = "xp_rpc::serde_text"))]
pub about: Text,
pub hot_entity: TrusteeEntity,
pub cold_entity: TrusteeEntity,
}

/// The trustee session info.
#[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "std", serde(rename_all = "camelCase"))]
Expand All @@ -41,51 +31,66 @@ pub struct TrusteeSessionInfo<AccountId, TrusteeAddress: BytesLike> {
pub cold_address: TrusteeAddress,
}

// generic
#[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "std", serde(rename_all = "camelCase"))]
pub struct GenericTrusteeIntentionProps(pub TrusteeIntentionProps<Vec<u8>>);

/// The generic trustee session info.
#[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "std", serde(rename_all = "camelCase"))]
pub struct GenericTrusteeSessionInfo<AccountId>(pub TrusteeSessionInfo<AccountId, Vec<u8>>);

impl<AccountId> Deref for GenericTrusteeSessionInfo<AccountId> {
type Target = TrusteeSessionInfo<AccountId, Vec<u8>>;
impl<AccountId, TrusteeAddress: BytesLike> From<TrusteeSessionInfo<AccountId, TrusteeAddress>>
for GenericTrusteeSessionInfo<AccountId>
{
fn from(info: TrusteeSessionInfo<AccountId, TrusteeAddress>) -> Self {
GenericTrusteeSessionInfo(TrusteeSessionInfo {
trustee_list: info.trustee_list,
threshold: info.threshold,
hot_address: info.hot_address.into(),
cold_address: info.cold_address.into(),
})
}
}

impl<AccountId, TrusteeAddress: BytesLike> TryFrom<GenericTrusteeSessionInfo<AccountId>>
for TrusteeSessionInfo<AccountId, TrusteeAddress>
{
// TODO, may use a better error
type Error = ();

fn deref(&self) -> &Self::Target {
&self.0
fn try_from(info: GenericTrusteeSessionInfo<AccountId>) -> Result<Self, Self::Error> {
Ok(TrusteeSessionInfo::<AccountId, TrusteeAddress> {
trustee_list: info.0.trustee_list,
threshold: info.0.threshold,
hot_address: TrusteeAddress::try_from(info.0.hot_address).map_err(|_| ())?,
cold_address: TrusteeAddress::try_from(info.0.cold_address).map_err(|_| ())?,
})
}
}

/// The trustee intention properties.
#[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "std", serde(rename_all = "camelCase"))]
pub struct GenericAllSessionInfo<AccountId> {
pub hot_entity: Vec<u8>,
pub cold_entity: Vec<u8>,
pub counts: Counts,
pub trustees_info: Vec<(AccountId, GenericTrusteeIntentionProps)>,
pub struct TrusteeIntentionProps<TrusteeEntity: BytesLike> {
#[cfg_attr(feature = "std", serde(with = "xp_rpc::serde_text"))]
pub about: Text,
pub hot_entity: TrusteeEntity,
pub cold_entity: TrusteeEntity,
}

/// The generic trustee intention properties.
#[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "std", serde(rename_all = "camelCase"))]
pub struct Counts {
pub required: u32,
pub total: u32,
}
pub struct GenericTrusteeIntentionProps(pub TrusteeIntentionProps<Vec<u8>>);

impl<TrusteeEntity: BytesLike> Into<GenericTrusteeIntentionProps>
for TrusteeIntentionProps<TrusteeEntity>
impl<TrusteeEntity: BytesLike> From<TrusteeIntentionProps<TrusteeEntity>>
for GenericTrusteeIntentionProps
{
fn into(self) -> GenericTrusteeIntentionProps {
fn from(props: TrusteeIntentionProps<TrusteeEntity>) -> Self {
GenericTrusteeIntentionProps(TrusteeIntentionProps {
about: self.about,
hot_entity: self.hot_entity.into(),
cold_entity: self.cold_entity.into(),
about: props.about,
hot_entity: props.hot_entity.into(),
cold_entity: props.cold_entity.into(),
})
}
}
Expand All @@ -97,75 +102,10 @@ impl<TrusteeEntity: BytesLike> TryFrom<GenericTrusteeIntentionProps>
type Error = ();

fn try_from(value: GenericTrusteeIntentionProps) -> Result<Self, Self::Error> {
let hot = TrusteeEntity::try_from(value.0.hot_entity).map_err(|_| ())?;
let cold = TrusteeEntity::try_from(value.0.cold_entity).map_err(|_| ())?;
Ok(TrusteeIntentionProps::<TrusteeEntity> {
about: value.0.about,
hot_entity: hot,
cold_entity: cold,
hot_entity: TrusteeEntity::try_from(value.0.hot_entity).map_err(|_| ())?,
cold_entity: TrusteeEntity::try_from(value.0.cold_entity).map_err(|_| ())?,
})
}
}

impl<AccountId, TrusteeAddress: BytesLike> Into<GenericTrusteeSessionInfo<AccountId>>
for TrusteeSessionInfo<AccountId, TrusteeAddress>
{
fn into(self) -> GenericTrusteeSessionInfo<AccountId> {
GenericTrusteeSessionInfo(TrusteeSessionInfo {
trustee_list: self.trustee_list,
threshold: self.threshold,
hot_address: self.hot_address.into(),
cold_address: self.cold_address.into(),
})
}
}

impl<AccountId, TrusteeAddress: BytesLike> TryFrom<GenericTrusteeSessionInfo<AccountId>>
for TrusteeSessionInfo<AccountId, TrusteeAddress>
{
// TODO, may use a better error
type Error = ();

fn try_from(value: GenericTrusteeSessionInfo<AccountId>) -> Result<Self, Self::Error> {
let hot = TrusteeAddress::try_from(value.0.hot_address).map_err(|_| ())?;
let cold = TrusteeAddress::try_from(value.0.cold_address).map_err(|_| ())?;
Ok(TrusteeSessionInfo::<AccountId, TrusteeAddress> {
trustee_list: value.0.trustee_list,
threshold: value.0.threshold,
hot_address: hot,
cold_address: cold,
})
}
}

pub fn into_generic_all_info<
AccountId: Clone,
TrusteeEntity: BytesLike,
TrusteeAddress: BytesLike,
F,
>(
session_info: TrusteeSessionInfo<AccountId, TrusteeAddress>,
get_props: F,
) -> GenericAllSessionInfo<AccountId>
where
F: Fn(&AccountId) -> Option<TrusteeIntentionProps<TrusteeEntity>>,
{
let session_info: GenericTrusteeSessionInfo<AccountId> = session_info.into();

let total = session_info.0.trustee_list.len() as u32;
let required = two_thirds_unsafe(total);

let mut trustees_info: Vec<(AccountId, GenericTrusteeIntentionProps)> = Vec::new();
for accountid in session_info.0.trustee_list {
if let Some(props) = get_props(&accountid) {
trustees_info.push((accountid, props.into()))
}
}

GenericAllSessionInfo {
hot_entity: session_info.0.hot_address,
cold_entity: session_info.0.cold_address,
counts: Counts { required, total },
trustees_info,
}
}

0 comments on commit a63da74

Please sign in to comment.