Skip to content

Commit

Permalink
Allow Substrate Pallet to be Initialized via Dipatchable (paritytech#481
Browse files Browse the repository at this point in the history
)

* Add dispatchable for intializing pallet

* Add Polkadot JS types for Substrate bridge pallet

* Ensure Root is the only one that can initialize the pallet

* Add some tests

* Pack initialization data into struct

* Only allow pallet to be initialized once

* Use new initialization config in nodes

* Rename ScheduledChange in Ethereum pallet

We're renaming it to prevent clashes with Substrate bridge pallet type
of the same name. This is relevant when importing types to Polkadot JS
Apps.

* Move all Polkadot JS types into one file

* Appease Clippy
  • Loading branch information
HCastano authored and serban300 committed Apr 10, 2024
1 parent 7aa1603 commit 9d7e742
Show file tree
Hide file tree
Showing 8 changed files with 193 additions and 76 deletions.
14 changes: 4 additions & 10 deletions bridges/bin/millau/node/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,10 @@ fn testnet_genesis(
pallet_grandpa: Some(GrandpaConfig {
authorities: Vec::new(),
}),
pallet_substrate_bridge: load_rialto_bridge_config(),
pallet_substrate_bridge: Some(BridgeRialtoConfig {
// We'll initialize the pallet with a dispatchable instead.
init_data: None,
}),
pallet_sudo: Some(SudoConfig { key: root_key }),
pallet_session: Some(SessionConfig {
keys: initial_authorities
Expand All @@ -165,12 +168,3 @@ fn testnet_genesis(
}),
}
}

fn load_rialto_bridge_config() -> Option<BridgeRialtoConfig> {
Some(BridgeRialtoConfig {
initial_header: Some(millau_runtime::rialto::initial_header()),
initial_authority_list: millau_runtime::rialto::initial_authority_set().authorities,
initial_set_id: millau_runtime::rialto::initial_authority_set().set_id,
first_scheduled_change: None,
})
}
5 changes: 1 addition & 4 deletions bridges/bin/rialto/node/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,6 @@ fn load_kovan_bridge_config() -> Option<BridgeKovanConfig> {

fn load_millau_bridge_config() -> Option<BridgeMillauConfig> {
Some(BridgeMillauConfig {
initial_header: Some(rialto_runtime::millau::initial_header()),
initial_authority_list: rialto_runtime::millau::initial_authority_set().authorities,
initial_set_id: rialto_runtime::millau::initial_authority_set().set_id,
first_scheduled_change: None,
init_data: Some(rialto_runtime::millau::init_data()),
})
}
14 changes: 13 additions & 1 deletion bridges/bin/rialto/runtime/src/millau.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,23 @@

use bp_rialto::Header;
use hex_literal::hex;
use pallet_substrate_bridge::AuthoritySet;
use pallet_substrate_bridge::{AuthoritySet, InitializationData};
use sp_core::crypto::Public;
use sp_finality_grandpa::AuthorityId;
use sp_std::vec;

/// Information about where the bridge palelt should start syncing from. This includes things like
/// the initial header and the initial authorities of the briged chain.
pub fn init_data() -> InitializationData<Header> {
let authority_set = initial_authority_set();
InitializationData {
header: initial_header(),
authority_list: authority_set.authorities,
set_id: authority_set.set_id,
scheduled_change: None,
}
}

/// The first header known to the pallet.
///
/// Note that this does not need to be the genesis header of the Millau
Expand Down
16 changes: 8 additions & 8 deletions bridges/modules/ethereum/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ pub struct ValidatorsSet {
/// Validators set change as it is stored in the runtime storage.
#[derive(Encode, Decode, PartialEq, RuntimeDebug)]
#[cfg_attr(test, derive(Clone))]
pub struct ScheduledChange {
pub struct AuraScheduledChange {
/// Validators of this set.
pub validators: Vec<Address>,
/// Hash of the block which has emitted previous validators change signal.
Expand Down Expand Up @@ -187,7 +187,7 @@ pub struct ImportContext<Submitter> {
parent_hash: H256,
parent_header: AuraHeader,
parent_total_difficulty: U256,
parent_scheduled_change: Option<ScheduledChange>,
parent_scheduled_change: Option<AuraScheduledChange>,
validators_set_id: u64,
validators_set: ValidatorsSet,
last_signal_block: Option<HeaderId>,
Expand All @@ -210,7 +210,7 @@ impl<Submitter> ImportContext<Submitter> {
}

/// Returns the validator set change if the parent header has signaled a change.
pub fn parent_scheduled_change(&self) -> Option<&ScheduledChange> {
pub fn parent_scheduled_change(&self) -> Option<&AuraScheduledChange> {
self.parent_scheduled_change.as_ref()
}

Expand Down Expand Up @@ -293,7 +293,7 @@ pub trait Storage {
) -> Option<ImportContext<Self::Submitter>>;
/// Get new validators that are scheduled by given header and hash of the previous
/// block that has scheduled change.
fn scheduled_change(&self, hash: &H256) -> Option<ScheduledChange>;
fn scheduled_change(&self, hash: &H256) -> Option<AuraScheduledChange>;
/// Insert imported header.
fn insert_header(&mut self, header: HeaderToImport<Self::Submitter>);
/// Finalize given block and schedules pruning of all headers
Expand Down Expand Up @@ -479,7 +479,7 @@ decl_storage! {
/// When it reaches zero, we are free to prune validator set as well.
ValidatorsSetsRc: map hasher(twox_64_concat) u64 => Option<u64>;
/// Map of validators set changes scheduled by given header.
ScheduledChanges: map hasher(identity) H256 => Option<ScheduledChange>;
ScheduledChanges: map hasher(identity) H256 => Option<AuraScheduledChange>;
}
add_extra_genesis {
config(initial_header): AuraHeader;
Expand Down Expand Up @@ -766,7 +766,7 @@ impl<T: Trait<I>, I: Instance> Storage for BridgeStorage<T, I> {
})
}

fn scheduled_change(&self, hash: &H256) -> Option<ScheduledChange> {
fn scheduled_change(&self, hash: &H256) -> Option<AuraScheduledChange> {
ScheduledChanges::<I>::get(hash)
}

Expand All @@ -777,7 +777,7 @@ impl<T: Trait<I>, I: Instance> Storage for BridgeStorage<T, I> {
if let Some(scheduled_change) = header.scheduled_change {
ScheduledChanges::<I>::insert(
&header.id.hash,
ScheduledChange {
AuraScheduledChange {
validators: scheduled_change,
prev_signal_block: header.context.last_signal_block,
},
Expand Down Expand Up @@ -1119,7 +1119,7 @@ pub(crate) mod tests {
if i == 7 && j == 1 {
ScheduledChanges::<DefaultInstance>::insert(
hash,
ScheduledChange {
AuraScheduledChange {
validators: validators_addresses(5),
prev_signal_block: None,
},
Expand Down
4 changes: 2 additions & 2 deletions bridges/modules/ethereum/src/validators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ pub(crate) mod tests {
use super::*;
use crate::mock::{run_test, validators_addresses, validators_change_receipt, TestRuntime};
use crate::DefaultInstance;
use crate::{BridgeStorage, Headers, ScheduledChange, ScheduledChanges, StoredHeader};
use crate::{AuraScheduledChange, BridgeStorage, Headers, ScheduledChanges, StoredHeader};
use bp_eth_poa::compute_merkle_root;
use frame_support::StorageMap;

Expand Down Expand Up @@ -428,7 +428,7 @@ pub(crate) mod tests {
next_validators_set_id: 0,
last_signal_block: scheduled_at,
};
let scheduled_change = ScheduledChange {
let scheduled_change = AuraScheduledChange {
validators: validators_addresses(1),
prev_signal_block: None,
};
Expand Down
6 changes: 3 additions & 3 deletions bridges/modules/ethereum/src/verification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

use crate::error::Error;
use crate::validators::{Validators, ValidatorsConfiguration};
use crate::{AuraConfiguration, ChainTime, ImportContext, PoolConfiguration, ScheduledChange, Storage};
use crate::{AuraConfiguration, AuraScheduledChange, ChainTime, ImportContext, PoolConfiguration, Storage};
use bp_eth_poa::{
public_to_address, step_validator, Address, AuraHeader, HeaderId, Receipt, SealedEmptyStep, H256, H520, U128, U256,
};
Expand Down Expand Up @@ -341,7 +341,7 @@ fn find_next_validators_signal<S: Storage>(storage: &S, context: &ImportContext<
// if parent schedules validators set change, then it may be our set
// else we'll start with last known change
let mut current_set_signal_block = context.last_signal_block();
let mut next_scheduled_set: Option<ScheduledChange> = None;
let mut next_scheduled_set: Option<AuraScheduledChange> = None;

loop {
// if we have reached block that signals finalized change, then
Expand Down Expand Up @@ -456,7 +456,7 @@ mod tests {
});
ScheduledChanges::<DefaultInstance>::insert(
header.header.parent_hash,
ScheduledChange {
AuraScheduledChange {
validators: signalled_set,
prev_signal_block: None,
},
Expand Down
Loading

0 comments on commit 9d7e742

Please sign in to comment.