Skip to content

Commit

Permalink
Fix auto-pause xcm: incoming XCMP messages where dropped when auto-pa…
Browse files Browse the repository at this point in the history
…use (#2913)

* chore: rename crate manual-xcm-rpc -> moonbeam-dev-rpc

* add RPC method test_skipRelayBlocks

* allow Root to resume XCM execution

* update moonkit pin

* add dev-test that trigger auto-pause of xcm

* prettier

* dev test auto-pause xcm: remove unused imports & improve test scenario

* add pallet-emergency-para-xcm to moonriver

* add pallet-emergency-para-xcm to moonbeam

* apply review suggestions
  • Loading branch information
librelois authored and gonzamontiel committed Sep 4, 2024
1 parent 375d009 commit 9f7daa1
Show file tree
Hide file tree
Showing 15 changed files with 265 additions and 41 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
exclude = ["bin/utils/moonkey"]
members = [
"bin/utils/moonkey",
"client/rpc/dev",
"client/rpc/finality",
"client/rpc/manual-xcm",
"client/vrf",
"node",
"node/cli",
Expand Down Expand Up @@ -117,8 +117,8 @@ moonbeam-cli = { path = "node/cli", default-features = false }
moonbeam-cli-opt = { path = "node/cli-opt", default-features = false }
moonbeam-service = { path = "node/service", default-features = false }

manual-xcm-rpc = { path = "client/rpc/manual-xcm" }
moonbeam-client-evm-tracing = { path = "client/evm-tracing" }
moonbeam-dev-rpc = { path = "client/rpc/dev" }
moonbeam-finality-rpc = { path = "client/rpc/finality" }
moonbeam-rpc-core-debug = { path = "client/rpc-core/debug" }
moonbeam-rpc-core-trace = { path = "client/rpc-core/trace" }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "manual-xcm-rpc"
name = "moonbeam-dev-rpc"
authors = { workspace = true }
edition = "2021"
homepage = "https://moonbeam.network"
Expand Down
22 changes: 16 additions & 6 deletions client/rpc/manual-xcm/src/lib.rs → client/rpc/dev/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

// You should have received a copy of the GNU General Public License
// along with Moonbeam. If not, see <http://www.gnu.org/licenses/>.

use cumulus_primitives_core::ParaId;
use cumulus_primitives_core::XcmpMessageFormat;
use jsonrpsee::{
Expand All @@ -28,12 +29,10 @@ use xcm::opaque::lts::Weight;
use xcm::v4::prelude::*;
use xcm_primitives::DEFAULT_PROOF_SIZE;

/// This RPC interface is used to manually submit XCM messages that will be injected into a
/// parachain-enabled runtime. This allows testing XCM logic in a controlled way in integration
/// tests.
/// This RPC interface is used to provide methods in dev mode only
#[rpc(server)]
#[jsonrpsee::core::async_trait]
pub trait ManualXcmApi {
pub trait DevApi {
/// Inject a downward xcm message - A message that comes from the relay chain.
/// You may provide an arbitrary message, or if you provide an emtpy byte array,
/// Then a default message (DOT transfer down to ALITH) will be injected
Expand All @@ -53,15 +52,20 @@ pub trait ManualXcmApi {
/// transfer of the sending paraId's native token will be injected.
#[method(name = "xcm_injectHrmpMessage")]
async fn inject_hrmp_message(&self, sender: ParaId, message: Vec<u8>) -> RpcResult<()>;

/// Skip N relay blocks, for testing purposes
#[method(name = "test_skipRelayBlocks")]
async fn skip_relay_blocks(&self, n: u32) -> RpcResult<()>;
}

pub struct ManualXcm {
pub struct DevRpc {
pub downward_message_channel: flume::Sender<Vec<u8>>,
pub hrmp_message_channel: flume::Sender<(ParaId, Vec<u8>)>,
pub additional_relay_offset: std::sync::Arc<std::sync::atomic::AtomicU32>,
}

#[jsonrpsee::core::async_trait]
impl ManualXcmApiServer for ManualXcm {
impl DevApiServer for DevRpc {
async fn inject_downward_message(&self, msg: Vec<u8>) -> RpcResult<()> {
let downward_message_channel = self.downward_message_channel.clone();
// If no message is supplied, inject a default one.
Expand Down Expand Up @@ -148,6 +152,12 @@ impl ManualXcmApiServer for ManualXcm {

Ok(())
}

async fn skip_relay_blocks(&self, n: u32) -> RpcResult<()> {
self.additional_relay_offset
.fetch_add(n, std::sync::atomic::Ordering::SeqCst);
Ok(())
}
}

// This bit cribbed from frontier.
Expand Down
2 changes: 1 addition & 1 deletion node/service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ tokio = { workspace = true, features = ["macros", "sync"] }
trie-root = { workspace = true }

# Moonbeam
manual-xcm-rpc = { workspace = true }
moonbeam-dev-rpc = { workspace = true }
moonbeam-cli-opt = { workspace = true }
moonbeam-core-primitives = { workspace = true }
moonbeam-finality-rpc = { workspace = true }
Expand Down
17 changes: 12 additions & 5 deletions node/service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,7 @@ where
fee_history_cache: fee_history_cache.clone(),
network: network.clone(),
sync: sync.clone(),
xcm_senders: None,
dev_rpc_data: None,
block_data_cache: block_data_cache.clone(),
overrides: overrides.clone(),
forced_parent_hashes,
Expand Down Expand Up @@ -1204,7 +1204,7 @@ where
let overrides = Arc::new(StorageOverrideHandler::new(client.clone()));
let fee_history_limit = rpc_config.fee_history_limit;
let mut command_sink = None;
let mut xcm_senders = None;
let mut dev_rpc_data = None;
let collator = config.role.is_authority();

if collator {
Expand Down Expand Up @@ -1269,7 +1269,12 @@ where
// Create channels for mocked XCM messages.
let (downward_xcm_sender, downward_xcm_receiver) = flume::bounded::<Vec<u8>>(100);
let (hrmp_xcm_sender, hrmp_xcm_receiver) = flume::bounded::<(ParaId, Vec<u8>)>(100);
xcm_senders = Some((downward_xcm_sender, hrmp_xcm_sender));
let additional_relay_offset = Arc::new(std::sync::atomic::AtomicU32::new(0));
dev_rpc_data = Some((
downward_xcm_sender,
hrmp_xcm_sender,
additional_relay_offset.clone(),
));

let client_clone = client.clone();
let keystore_clone = keystore_container.keystore().clone();
Expand Down Expand Up @@ -1304,6 +1309,7 @@ where
let maybe_current_para_head = client_set_aside_for_cidp.expect_header(block);
let downward_xcm_receiver = downward_xcm_receiver.clone();
let hrmp_xcm_receiver = hrmp_xcm_receiver.clone();
let additional_relay_offset = additional_relay_offset.clone();

let client_for_xcm = client_set_aside_for_cidp.clone();
async move {
Expand All @@ -1324,7 +1330,8 @@ where
let mocked_parachain = MockValidationDataInherentDataProvider {
current_para_block,
current_para_block_head,
relay_offset: 1000,
relay_offset: 1000
+ additional_relay_offset.load(std::sync::atomic::Ordering::SeqCst),
relay_blocks_per_para_block: 2,
// TODO: Recheck
para_blocks_per_relay_epoch: 10,
Expand Down Expand Up @@ -1440,7 +1447,7 @@ where
fee_history_cache: fee_history_cache.clone(),
network: network.clone(),
sync: sync.clone(),
xcm_senders: xcm_senders.clone(),
dev_rpc_data: dev_rpc_data.clone(),
overrides: overrides.clone(),
block_data_cache: block_data_cache.clone(),
forced_parent_hashes: None,
Expand Down
17 changes: 12 additions & 5 deletions node/service/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,11 @@ pub struct FullDeps<C, P, A: ChainApi, BE> {
/// Fee history cache.
pub fee_history_cache: FeeHistoryCache,
/// Channels for manual xcm messages (downward, hrmp)
pub xcm_senders: Option<(flume::Sender<Vec<u8>>, flume::Sender<(ParaId, Vec<u8>)>)>,
pub dev_rpc_data: Option<(
flume::Sender<Vec<u8>>,
flume::Sender<(ParaId, Vec<u8>)>,
Arc<std::sync::atomic::AtomicU32>,
)>,
/// Ethereum data access overrides.
pub overrides: Arc<dyn StorageOverride<Block>>,
/// Cache for Ethereum block data.
Expand Down Expand Up @@ -173,7 +177,7 @@ where
Eth, EthApiServer, EthFilter, EthFilterApiServer, EthPubSub, EthPubSubApiServer, Net,
NetApiServer, Web3, Web3ApiServer,
};
use manual_xcm_rpc::{ManualXcm, ManualXcmApiServer};
use moonbeam_dev_rpc::{DevApiServer, DevRpc};
use moonbeam_finality_rpc::{MoonbeamFinality, MoonbeamFinalityApiServer};
use moonbeam_rpc_debug::{Debug, DebugServer};
use moonbeam_rpc_trace::{Trace, TraceServer};
Expand All @@ -198,7 +202,7 @@ where
max_past_logs,
fee_history_limit,
fee_history_cache,
xcm_senders,
dev_rpc_data,
overrides,
block_data_cache,
forced_parent_hashes,
Expand Down Expand Up @@ -318,11 +322,14 @@ where
)?;
};

if let Some((downward_message_channel, hrmp_message_channel)) = xcm_senders {
if let Some((downward_message_channel, hrmp_message_channel, additional_relay_offset)) =
dev_rpc_data
{
io.merge(
ManualXcm {
DevRpc {
downward_message_channel,
hrmp_message_channel,
additional_relay_offset,
}
.into_rpc(),
)?;
Expand Down
2 changes: 1 addition & 1 deletion runtime/moonbase/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,7 @@ impl cumulus_pallet_parachain_system::Config for Runtime {
type SelfParaId = ParachainInfo;
type ReservedDmpWeight = ReservedDmpWeight;
type OutboundXcmpMessageSource = XcmpQueue;
type XcmpMessageHandler = EmergencyParaXcm;
type XcmpMessageHandler = XcmpQueue;
type ReservedXcmpWeight = ReservedXcmpWeight;
type CheckAssociatedRelayNumber = EmergencyParaXcm;
type ConsensusHook = ConsensusHookWrapperForRelayTimestamp<Runtime, ConsensusHook>;
Expand Down
16 changes: 12 additions & 4 deletions runtime/moonbase/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,16 +464,24 @@ impl pallet_message_queue::Config for Runtime {
type IdleMaxServiceWeight = MessageQueueServiceWeight;
}

pub type FastAuthorizeUpgradeOrigin = EitherOfDiverse<
EnsureRoot<AccountId>,
pallet_collective::EnsureProportionAtLeast<AccountId, OpenTechCommitteeInstance, 5, 9>,
>;

pub type ResumeXcmOrigin = EitherOfDiverse<
EnsureRoot<AccountId>,
pallet_collective::EnsureProportionAtLeast<AccountId, OpenTechCommitteeInstance, 5, 9>,
>;

impl pallet_emergency_para_xcm::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type CheckAssociatedRelayNumber =
cumulus_pallet_parachain_system::RelayNumberMonotonicallyIncreases;
type QueuePausedQuery = (MaintenanceMode, NarrowOriginToSibling<XcmpQueue>);
type PausedThreshold = ConstU32<300>;
type FastAuthorizeUpgradeOrigin =
pallet_collective::EnsureProportionAtLeast<AccountId, OpenTechCommitteeInstance, 5, 9>;
type PausedToNormalOrigin =
pallet_collective::EnsureProportionAtLeast<AccountId, OpenTechCommitteeInstance, 5, 9>;
type FastAuthorizeUpgradeOrigin = FastAuthorizeUpgradeOrigin;
type PausedToNormalOrigin = ResumeXcmOrigin;
}

// Our AssetType. For now we only handle Xcm Assets
Expand Down
2 changes: 2 additions & 0 deletions runtime/moonbeam/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ moonbeam-xcm-benchmarks = { workspace = true }
pallet-asset-manager = { workspace = true }
pallet-author-mapping = { workspace = true }
pallet-crowdloan-rewards = { workspace = true }
pallet-emergency-para-xcm = { workspace = true }
pallet-erc20-xcm-bridge = { workspace = true }
pallet-ethereum-xcm = { workspace = true }
pallet-evm-chain-id = { workspace = true }
Expand Down Expand Up @@ -233,6 +234,7 @@ std = [
"pallet-collective/std",
"pallet-conviction-voting/std",
"pallet-crowdloan-rewards/std",
"pallet-emergency-para-xcm/std",
"pallet-erc20-xcm-bridge/std",
"pallet-evm-chain-id/std",
"pallet-ethereum-xcm/std",
Expand Down
8 changes: 4 additions & 4 deletions runtime/moonbeam/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
//! * Moonbeam tokenomics

#![cfg_attr(not(feature = "std"), no_std)]
// `construct_runtime!` does a lot of recursion and requires us to increase the limit to 256.
#![recursion_limit = "256"]
// `construct_runtime!` does a lot of recursion and requires us to increase the limit to 512.
#![recursion_limit = "512"]

// Make the WASM binary available.
#[cfg(feature = "std")]
Expand Down Expand Up @@ -704,8 +704,7 @@ impl cumulus_pallet_parachain_system::Config for Runtime {
type OutboundXcmpMessageSource = XcmpQueue;
type XcmpMessageHandler = XcmpQueue;
type ReservedXcmpWeight = ReservedXcmpWeight;
type CheckAssociatedRelayNumber =
cumulus_pallet_parachain_system::RelayNumberMonotonicallyIncreases;
type CheckAssociatedRelayNumber = EmergencyParaXcm;
type ConsensusHook = ConsensusHookWrapperForRelayTimestamp<Runtime, ConsensusHook>;
type DmpQueue = frame_support::traits::EnqueueWithOrigin<MessageQueue, RelayOrigin>;
type WeightInfo = moonbeam_weights::cumulus_pallet_parachain_system::WeightInfo<Runtime>;
Expand Down Expand Up @@ -1448,6 +1447,7 @@ construct_runtime! {
Erc20XcmBridge: pallet_erc20_xcm_bridge::{Pallet} = 110,
MessageQueue: pallet_message_queue::{Pallet, Call, Storage, Event<T>} = 111,
EvmForeignAssets: pallet_moonbeam_foreign_assets::{Pallet, Call, Storage, Event<T>} = 114,
EmergencyParaXcm: pallet_emergency_para_xcm::{Pallet, Call, Storage, Event} = 116,

// Utils
RelayStorageRoots: pallet_relay_storage_roots::{Pallet, Storage} = 112,
Expand Down
29 changes: 25 additions & 4 deletions runtime/moonbeam/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
//!

use super::{
governance, AccountId, AssetId, AssetManager, Balance, Balances, DealWithFees, Erc20XcmBridge,
MaintenanceMode, MessageQueue, ParachainInfo, ParachainSystem, Perbill, PolkadotXcm, Runtime,
RuntimeBlockWeights, RuntimeCall, RuntimeEvent, RuntimeOrigin, Treasury, XcmpQueue,
governance, AccountId, AssetId, AssetManager, Balance, Balances, DealWithFees,
EmergencyParaXcm, Erc20XcmBridge, MaintenanceMode, MessageQueue, OpenTechCommitteeInstance,
ParachainInfo, ParachainSystem, Perbill, PolkadotXcm, Runtime, RuntimeBlockWeights,
RuntimeCall, RuntimeEvent, RuntimeOrigin, Treasury, XcmpQueue,
};

use frame_support::{
Expand Down Expand Up @@ -447,11 +448,31 @@ impl pallet_message_queue::Config for Runtime {
// The XCMP queue pallet is only ever able to handle the `Sibling(ParaId)` origin:
type QueueChangeHandler = NarrowOriginToSibling<XcmpQueue>;
// NarrowOriginToSibling calls XcmpQueue's is_paused if Origin is sibling. Allows all other origins
type QueuePausedQuery = (MaintenanceMode, NarrowOriginToSibling<XcmpQueue>);
type QueuePausedQuery = EmergencyParaXcm;
type WeightInfo = moonbeam_weights::pallet_message_queue::WeightInfo<Runtime>;
type IdleMaxServiceWeight = MessageQueueServiceWeight;
}

pub type FastAuthorizeUpgradeOrigin = EitherOfDiverse<
EnsureRoot<AccountId>,
pallet_collective::EnsureProportionAtLeast<AccountId, OpenTechCommitteeInstance, 5, 9>,
>;

pub type ResumeXcmOrigin = EitherOfDiverse<
EnsureRoot<AccountId>,
pallet_collective::EnsureProportionAtLeast<AccountId, OpenTechCommitteeInstance, 5, 9>,
>;

impl pallet_emergency_para_xcm::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type CheckAssociatedRelayNumber =
cumulus_pallet_parachain_system::RelayNumberMonotonicallyIncreases;
type QueuePausedQuery = (MaintenanceMode, NarrowOriginToSibling<XcmpQueue>);
type PausedThreshold = ConstU32<300>;
type FastAuthorizeUpgradeOrigin = FastAuthorizeUpgradeOrigin;
type PausedToNormalOrigin = ResumeXcmOrigin;
}

// Our AssetType. For now we only handle Xcm Assets
#[derive(Clone, Eq, Debug, PartialEq, Ord, PartialOrd, Encode, Decode, TypeInfo)]
pub enum AssetType {
Expand Down
2 changes: 2 additions & 0 deletions runtime/moonriver/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ moonbeam-xcm-benchmarks = { workspace = true }
pallet-asset-manager = { workspace = true }
pallet-author-mapping = { workspace = true }
pallet-crowdloan-rewards = { workspace = true }
pallet-emergency-para-xcm = { workspace = true }
pallet-erc20-xcm-bridge = { workspace = true }
pallet-ethereum-xcm = { workspace = true }
pallet-evm-chain-id = { workspace = true }
Expand Down Expand Up @@ -234,6 +235,7 @@ std = [
"pallet-collective/std",
"pallet-conviction-voting/std",
"pallet-crowdloan-rewards/std",
"pallet-emergency-para-xcm/std",
"pallet-erc20-xcm-bridge/std",
"pallet-evm-chain-id/std",
"pallet-ethereum-xcm/std",
Expand Down
8 changes: 4 additions & 4 deletions runtime/moonriver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
//! * Moonriver tokenomics

#![cfg_attr(not(feature = "std"), no_std)]
// `construct_runtime!` does a lot of recursion and requires us to increase the limit to 256.
#![recursion_limit = "256"]
// `construct_runtime!` does a lot of recursion and requires us to increase the limit to 512.
#![recursion_limit = "512"]

// Make the WASM binary available.
#[cfg(feature = "std")]
Expand Down Expand Up @@ -740,8 +740,7 @@ impl cumulus_pallet_parachain_system::Config for Runtime {
type OutboundXcmpMessageSource = XcmpQueue;
type XcmpMessageHandler = XcmpQueue;
type ReservedXcmpWeight = ReservedXcmpWeight;
type CheckAssociatedRelayNumber =
cumulus_pallet_parachain_system::RelayNumberMonotonicallyIncreases;
type CheckAssociatedRelayNumber = EmergencyParaXcm;
type ConsensusHook = ConsensusHookWrapperForRelayTimestamp<Runtime, ConsensusHook>;
type DmpQueue = frame_support::traits::EnqueueWithOrigin<MessageQueue, RelayOrigin>;
type WeightInfo = moonriver_weights::cumulus_pallet_parachain_system::WeightInfo<Runtime>;
Expand Down Expand Up @@ -1451,6 +1450,7 @@ construct_runtime! {
Erc20XcmBridge: pallet_erc20_xcm_bridge::{Pallet} = 110,
MessageQueue: pallet_message_queue::{Pallet, Call, Storage, Event<T>} = 111,
EvmForeignAssets: pallet_moonbeam_foreign_assets::{Pallet, Call, Storage, Event<T>} = 114,
EmergencyParaXcm: pallet_emergency_para_xcm::{Pallet, Call, Storage, Event} = 116,

// Utils
RelayStorageRoots: pallet_relay_storage_roots::{Pallet, Storage} = 112,
Expand Down
Loading

0 comments on commit 9f7daa1

Please sign in to comment.