Skip to content

Commit

Permalink
move bitcoin genesis trustee process to bitcoin module, not in common (
Browse files Browse the repository at this point in the history
…paritytech#271)

* move bitcoin genesis trustee process to bitcoin module, not in common

* improve generic type in common rpc

* change develop/local_testnet bitcoin from mainnet to testnet

* fix all tests

* replace old testnet wasm
  • Loading branch information
atenjin committed Sep 27, 2020
1 parent f001447 commit f674ae5
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 31 deletions.
20 changes: 18 additions & 2 deletions cli/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ pub fn development_config() -> Result<ChainSpec, String> {
("Alice//stash", endowed_balance),
("Bob//stash", endowed_balance),
],
crate::res::load_mainnet_btc_genesis_header_info,
crate::res::load_testnet_btc_genesis_header_info,
crate::genesis::trustees::local_testnet_trustees(),
)
};
Expand Down Expand Up @@ -225,7 +225,7 @@ pub fn local_testnet_config() -> Result<ChainSpec, String> {
("Eve//stash", endowed_balance),
("Ferdie//stash", endowed_balance),
],
crate::res::load_mainnet_btc_genesis_header_info,
crate::res::load_testnet_btc_genesis_header_info,
crate::genesis::trustees::local_testnet_trustees(),
)
};
Expand Down Expand Up @@ -659,6 +659,21 @@ where
.into_iter()
.map(|((val, referral_id), _, _, _, _, _)| (val, referral_id, STAKING_LOCKED))
.collect::<Vec<_>>();
let btc_genesis_trustees = trustees
.iter()
.find_map(|(chain, _, trustee_params)| {
if *chain == Chain::Bitcoin {
Some(
trustee_params
.iter()
.map(|i| (i.0).clone())
.collect::<Vec<_>>(),
)
} else {
None
}
})
.expect("must success for bitcoin trustee info");

GenesisConfig {
frame_system: Some(SystemConfig {
Expand Down Expand Up @@ -729,6 +744,7 @@ where
confirmed_count,
} = bitcoin_info(); // crate::res::load_mainnet_btc_genesis_header_info();
Some(XGatewayBitcoinConfig {
genesis_trustees: btc_genesis_trustees,
genesis_info,
genesis_hash,
network_id: network,
Expand Down
12 changes: 6 additions & 6 deletions cli/src/genesis/trustees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,18 @@ pub fn local_testnet_trustees() -> Vec<(Chain, TrusteeInfoConfig, Vec<TrusteePar
let btc_trustees = vec![
btc_trustee_gen(
"Alice",
"035b8fb240f808f4d3d0d024fdf3b185b942e984bba81b6812b8610f66d59f3a84", // hot key
"0227e54b65612152485a812b8856e92f41f64788858466cc4d8df674939a5538c3", // colde key
"0376b9649206c74cc3dad6332c3a86d925a251bf9a55e6381f5d67b29a47559634", // hot key
"0300849497d4f88ebc3e1bc2583677c5abdbd3b63640b3c5c50cd4628a33a2a2ca", // colde key
),
btc_trustee_gen(
"Bob",
"02a79800dfed17ad4c78c52797aa3449925692bc8c83de469421080f42d27790ee",
"020699bf931859cafdacd8ac4d3e055eae7551427487e281e3efba618bdd395f2f",
"0285eed6fa121c3a82ba6d0c37fa37e72bb06740761bfe9f294d2fa95fe237d5ba",
"032122032ae9656f9a133405ffe02101469a8d62002270a33ceccf0e40dda54d08",
),
btc_trustee_gen(
"Charlie",
"0306117a360e5dbe10e1938a047949c25a86c0b0e08a0a7c1e611b97de6b2917dd",
"02a83c80e371ddf0a29006096765d060190bb607ec015ba6023b40ace582e13b99",
"036e1b175cc285b62a8b86e4ea94f32d627b36d60673b37eb3dd07d7b8c9ae6ddb",
"02b3cc747f572d33f12870fa6866aebbfd2b992ba606b8dc89b676b3697590ad63",
),
];

Expand Down
Binary file modified cli/src/wasm/chainx_runtime_testnet.compact.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -964,7 +964,7 @@ construct_runtime!(
// Crypto gateway stuff.
XGatewayRecords: xpallet_gateway_records::{Module, Call, Storage, Event<T>},
XGatewayCommon: xpallet_gateway_common::{Module, Call, Storage, Event<T>, Config<T>},
XGatewayBitcoin: xpallet_gateway_bitcoin::{Module, Call, Storage, Event<T>, Config},
XGatewayBitcoin: xpallet_gateway_bitcoin::{Module, Call, Storage, Event<T>, Config<T>},

// DEX
XSpot: xpallet_dex_spot::{Module, Call, Storage, Event<T>, Config<T>},
Expand Down
9 changes: 5 additions & 4 deletions xpallets/gateway/bitcoin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ decl_storage! {
}
add_extra_genesis {
config(genesis_hash): H256;
config(genesis_trustees): Vec<T::AccountId>;
build(|config| {
let genesis_header = config.genesis_info.0;
let genesis_hash = genesis_header.hash();
Expand Down Expand Up @@ -262,11 +263,11 @@ decl_storage! {
MainChain::insert(&genesis_hash, ());

BestIndex::put(genesis_index);
// ConfirmedIndex::put(genesis_index);

Module::<T>::deposit_event(RawEvent::InsertHeader(
genesis_hash,
));
// init trustee (not this action should ha)
if !config.genesis_trustees.is_empty() {
T::TrusteeSessionProvider::genesis_trustee(Module::<T>::chain(), &config.genesis_trustees);
}
})
}
}
Expand Down
33 changes: 25 additions & 8 deletions xpallets/gateway/bitcoin/src/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,8 @@ impl ExtBuilder {
// let (genesis_info, genesis_hash, network_id) = load_mock_btc_genesis_header_info();
let genesis_hash = btc_genesis.0.hash();
let network_id = btc_network;
let _ = GenesisConfig {
let _ = GenesisConfig::<Test> {
genesis_trustees: vec![],
genesis_info: btc_genesis,
genesis_hash,
network_id,
Expand All @@ -238,7 +239,7 @@ impl ExtBuilder {
btc_withdrawal_fee: 500000,
max_withdrawal_count: 100,
}
.assimilate_storage::<Test>(&mut storage);
.assimilate_storage(&mut storage);

let ext = sp_io::TestExternalities::new(storage);
ext
Expand Down Expand Up @@ -274,14 +275,30 @@ impl ExtBuilder {
}
.assimilate_storage(&mut storage);

let _ = xpallet_gateway_common::GenesisConfig::<Test> {
trustees: trustees_info(),
}
.assimilate_storage(&mut storage);
let info = trustees_info();
let genesis_trustees = info
.iter()
.find_map(|(chain, _, trustee_params)| {
if *chain == Chain::Bitcoin {
Some(
trustee_params
.iter()
.map(|i| (i.0).clone())
.collect::<Vec<_>>(),
)
} else {
None
}
})
.unwrap();

let _ = xpallet_gateway_common::GenesisConfig::<Test> { trustees: info }
.assimilate_storage(&mut storage);

let (genesis_info, genesis_hash, network_id) = load_mainnet_btc_genesis_header_info();

let _ = GenesisConfig {
let _ = GenesisConfig::<Test> {
genesis_trustees,
genesis_info,
genesis_hash,
network_id,
Expand All @@ -298,7 +315,7 @@ impl ExtBuilder {
btc_withdrawal_fee: 500000,
max_withdrawal_count: 100,
}
.assimilate_storage::<Test>(&mut storage);
.assimilate_storage(&mut storage);

let ext = sp_io::TestExternalities::new(storage);
ext
Expand Down
6 changes: 1 addition & 5 deletions xpallets/gateway/common/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,7 @@ pub trait XGatewayCommonApi<BlockHash, AccountId, Balance> {
/// A struct that implements the [`XStakingApi`].
pub struct XGatewayCommon<C, B, AccountId, Balance> {
client: Arc<C>,
_marker: std::marker::PhantomData<B>,
_marker2: std::marker::PhantomData<AccountId>,
_marker3: std::marker::PhantomData<Balance>,
_marker: std::marker::PhantomData<(B, AccountId, Balance)>,
}

impl<C, B, AccountId, Balance> XGatewayCommon<C, B, AccountId, Balance> {
Expand All @@ -94,8 +92,6 @@ impl<C, B, AccountId, Balance> XGatewayCommon<C, B, AccountId, Balance> {
Self {
client,
_marker: Default::default(),
_marker2: Default::default(),
_marker3: Default::default(),
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions xpallets/gateway/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,10 +248,9 @@ decl_storage! {
.expect("setup trustee can not fail; qed");
trustees.push(who.clone());
}
// config set should before transitino
// config set should before transition
TrusteeInfoConfigOf::insert(chain, info_config.clone());
Module::<T>::transition_trustee_session_impl(*chain, trustees)
.expect("trustee session transition can not fail; qed");
// trustee init should happen in chain module like gateway_bitcoin/gateway_ethereum.
}
})
}
Expand Down
6 changes: 6 additions & 0 deletions xpallets/gateway/common/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ pub trait TrusteeSession<AccountId, TrusteeAddress: BytesLike> {

fn last_trustee_session(
) -> result::Result<TrusteeSessionInfo<AccountId, TrusteeAddress>, DispatchError>;

#[cfg(feature = "std")]
fn genesis_trustee(chain: Chain, init: &[AccountId]);
}

impl<AccountId, TrusteeAddress: BytesLike> TrusteeSession<AccountId, TrusteeAddress> for () {
Expand All @@ -65,6 +68,9 @@ impl<AccountId, TrusteeAddress: BytesLike> TrusteeSession<AccountId, TrusteeAddr
) -> result::Result<TrusteeSessionInfo<AccountId, TrusteeAddress>, DispatchError> {
Err("NoTrustee".into())
}

#[cfg(feature = "std")]
fn genesis_trustee(_: Chain, _: &[AccountId]) {}
}

pub trait ChannelBinding<AccountId> {
Expand Down
6 changes: 6 additions & 0 deletions xpallets/gateway/common/src/trustees/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ impl<T: Trait, TrusteeAddress: BytesLike + ChainProvider>
e
})
}

#[cfg(feature = "std")]
fn genesis_trustee(chain: Chain, trustees: &[T::AccountId]) {
Module::<T>::transition_trustee_session_impl(chain, trustees.to_vec())
.expect("trustee session transition can not fail; qed");
}
}

pub struct TrusteeMultisigProvider<T: Trait, C: ChainProvider>(PhantomData<T>, PhantomData<C>);
Expand Down
5 changes: 3 additions & 2 deletions xpallets/gateway/common/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,8 @@ impl ExtBuilder {

let (genesis_info, genesis_hash, network_id) = load_mainnet_btc_genesis_header_info();

let _ = xpallet_gateway_bitcoin::GenesisConfig {
let _ = xpallet_gateway_bitcoin::GenesisConfig::<Test> {
genesis_trustees: vec![],
genesis_info,
genesis_hash,
network_id,
Expand All @@ -254,7 +255,7 @@ impl ExtBuilder {
btc_withdrawal_fee: 500000,
max_withdrawal_count: 100,
}
.assimilate_storage::<Test>(&mut storage);
.assimilate_storage(&mut storage);

let _ = xpallet_gateway_common::GenesisConfig::<Test> {
trustees: trustees(),
Expand Down

0 comments on commit f674ae5

Please sign in to comment.