Skip to content

Commit 91c28f3

Browse files
committed
block messages with a query and fix the open_channels response to include closed channels as well
1 parent 7a0675a commit 91c28f3

File tree

14 files changed

+176
-40
lines changed

14 files changed

+176
-40
lines changed

crates/subspace-fake-runtime-api/src/lib.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ use sp_domains::{
1616
use sp_domains_fraud_proof::fraud_proof::FraudProof;
1717
use sp_domains_fraud_proof::storage_proof::FraudProofStorageKeyRequest;
1818
use sp_messenger::messages::{
19-
BlockMessagesWithStorageKey, ChainId, ChannelId, CrossDomainMessage, MessageId, MessageKey,
19+
BlockMessagesQuery, BlockMessagesWithStorageKey, ChainId, ChannelId, ChannelState,
20+
CrossDomainMessage, MessageId, MessageKey,
2021
};
2122
use sp_messenger::{ChannelNonce, XdmId};
2223
use sp_runtime::traits::NumberFor;
@@ -412,6 +413,14 @@ sp_api::impl_runtime_apis! {
412413
fn open_channels() -> BTreeSet<(ChainId, ChannelId)> {
413414
unreachable!()
414415
}
416+
417+
fn block_messages_with_query(_: BlockMessagesQuery) -> BlockMessagesWithStorageKey {
418+
unreachable!()
419+
}
420+
421+
fn channels_and_state() -> Vec<(ChainId, ChannelId, ChannelState)>{
422+
unreachable!()
423+
}
415424
}
416425

417426
impl sp_domains_fraud_proof::FraudProofApi<Block, DomainHeader> for Runtime {

crates/subspace-runtime/src/lib.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ use sp_domains_fraud_proof::storage_proof::{
7171
};
7272
use sp_messenger::endpoint::{Endpoint, EndpointHandler as EndpointHandlerT, EndpointId};
7373
use sp_messenger::messages::{
74-
BlockMessagesWithStorageKey, ChainId, CrossDomainMessage, MessageId, MessageKey,
74+
BlockMessagesQuery, BlockMessagesWithStorageKey, ChainId, ChannelState, CrossDomainMessage,
75+
MessageId, MessageKey,
7576
};
7677
use sp_messenger::{ChannelNonce, XdmId};
7778
use sp_messenger_host_functions::{get_storage_key, StorageKeyRequest};
@@ -1672,7 +1673,7 @@ impl_runtime_apis! {
16721673

16731674
impl sp_messenger::RelayerApi<Block, BlockNumber, BlockNumber, BlockHashFor<Block>> for Runtime {
16741675
fn block_messages() -> BlockMessagesWithStorageKey {
1675-
Messenger::get_block_messages()
1676+
BlockMessagesWithStorageKey::default()
16761677
}
16771678

16781679
fn outbox_message_unsigned(msg: CrossDomainMessage<NumberFor<Block>, BlockHashFor<Block>, BlockHashFor<Block>>) -> Option<ExtrinsicFor<Block>> {
@@ -1702,6 +1703,14 @@ impl_runtime_apis! {
17021703
fn open_channels() -> BTreeSet<(ChainId, ChannelId)> {
17031704
Messenger::open_channels()
17041705
}
1706+
1707+
fn block_messages_with_query(query: BlockMessagesQuery) -> BlockMessagesWithStorageKey {
1708+
Messenger::get_block_messages(query)
1709+
}
1710+
1711+
fn channels_and_state() -> Vec<(ChainId, ChannelId, ChannelState)>{
1712+
Messenger::channels_and_states()
1713+
}
17051714
}
17061715

17071716
impl sp_domains_fraud_proof::FraudProofApi<Block, DomainHeader> for Runtime {

domains/pallets/messenger/src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1410,7 +1410,11 @@ mod pallet {
14101410
}
14111411

14121412
pub fn open_channels() -> BTreeSet<(ChainId, ChannelId)> {
1413-
crate::migrations::get_open_channels::<T>()
1413+
Channels::<T>::iter_keys().collect()
1414+
}
1415+
1416+
pub fn channels_and_states() -> Vec<(ChainId, ChannelId, ChannelState)> {
1417+
crate::migrations::get_channels_and_states::<T>()
14141418
}
14151419

14161420
pub fn channel_nonce(chain_id: ChainId, channel_id: ChannelId) -> Option<ChannelNonce> {

domains/pallets/messenger/src/messages.rs

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@ use alloc::collections::BTreeMap;
1616
use frame_support::ensure;
1717
use sp_messenger::endpoint::{EndpointHandler, EndpointRequest, EndpointResponse};
1818
use sp_messenger::messages::{
19-
BlockMessageWithStorageKey, BlockMessagesWithStorageKey, ChainId, ChannelOpenParamsV1, Message,
20-
MessageId, MessageWeightTag, PayloadV1, ProtocolMessageRequest, ProtocolMessageResponse,
21-
RequestResponse, VersionedPayload,
19+
BlockMessageWithStorageKey, BlockMessagesQuery, BlockMessagesWithStorageKey, ChainId,
20+
ChannelOpenParamsV1, Message, MessageId, MessageKey, MessageWeightTag, PayloadV1,
21+
ProtocolMessageRequest, ProtocolMessageResponse, RequestResponse, VersionedPayload,
2222
};
23-
use sp_runtime::traits::Get;
23+
24+
use sp_messenger::MAX_FUTURE_ALLOWED_NONCES;
25+
use sp_runtime::traits::{Get, One};
2426
use sp_runtime::{ArithmeticError, DispatchError, DispatchResult};
2527
#[cfg(feature = "std")]
2628
use std::collections::BTreeMap;
@@ -387,11 +389,23 @@ impl<T: Config> Pallet<T> {
387389
Ok(())
388390
}
389391

390-
pub fn get_block_messages() -> BlockMessagesWithStorageKey {
391-
let inbox_responses_weight_tags: BTreeMap<(ChainId, MessageId), MessageWeightTag> =
392-
InboxResponseMessageWeightTags::<T>::iter().collect();
393-
let outbox_weight_tags: BTreeMap<(ChainId, MessageId), MessageWeightTag> =
394-
OutboxMessageWeightTags::<T>::iter().collect();
392+
pub fn get_block_messages(query: BlockMessagesQuery) -> BlockMessagesWithStorageKey {
393+
let BlockMessagesQuery {
394+
chain_id,
395+
channel_id,
396+
outbox_from,
397+
inbox_responses_from,
398+
} = query;
399+
400+
let inbox_responses_weight_tags = Self::get_weight_tags(
401+
(chain_id, channel_id, inbox_responses_from),
402+
InboxResponseMessageWeightTags::<T>::get,
403+
);
404+
405+
let outbox_weight_tags = Self::get_weight_tags(
406+
(chain_id, channel_id, outbox_from),
407+
OutboxMessageWeightTags::<T>::get,
408+
);
395409

396410
if outbox_weight_tags.is_empty() && inbox_responses_weight_tags.is_empty() {
397411
return Default::default();
@@ -436,4 +450,31 @@ impl<T: Config> Pallet<T> {
436450

437451
messages_with_storage_key
438452
}
453+
454+
fn get_weight_tags<WTG>(
455+
from: MessageKey,
456+
weight_tag_getter: WTG,
457+
) -> BTreeMap<(ChainId, MessageId), MessageWeightTag>
458+
where
459+
WTG: Fn((ChainId, MessageId)) -> Option<MessageWeightTag>,
460+
{
461+
let (chain_id, channel_id, mut nonce) = from;
462+
let mut weight_tags = BTreeMap::new();
463+
while weight_tags.len() as u32 <= MAX_FUTURE_ALLOWED_NONCES {
464+
match weight_tag_getter((chain_id, (channel_id, nonce))) {
465+
// if the nonce is already processed, short circuit and return
466+
None => return weight_tags,
467+
Some(weight_tag) => {
468+
weight_tags.insert((chain_id, (channel_id, nonce)), weight_tag);
469+
}
470+
};
471+
472+
nonce = match nonce.checked_add(One::one()) {
473+
None => return weight_tags,
474+
Some(nonce) => nonce,
475+
}
476+
}
477+
478+
weight_tags
479+
}
439480
}

domains/pallets/messenger/src/migrations.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ mod v0_to_v1;
22
mod v1_to_v2;
33

44
pub use v0_to_v1::VersionCheckedMigrateDomainsV0ToV1;
5-
pub(crate) use v1_to_v2::migrate_channels::{get_channel, get_open_channels};
5+
pub(crate) use v1_to_v2::migrate_channels::{get_channel, get_channels_and_states};
66
pub use v1_to_v2::VersionCheckedMigrateDomainsV1ToV2;

domains/pallets/messenger/src/migrations/v1_to_v2.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ extern crate alloc;
55
use crate::migrations::v1_to_v2::migrate_channels::migrate_channels;
66
use crate::{BalanceOf, Channels as ChannelStorageV1, Config, Pallet};
77
#[cfg(not(feature = "std"))]
8-
use alloc::collections::BTreeSet;
8+
use alloc::vec::Vec;
99
use core::marker::PhantomData;
1010
use frame_support::migrations::VersionedMigration;
1111
use frame_support::pallet_prelude::{Decode, Encode, OptionQuery, TypeInfo};
@@ -14,8 +14,6 @@ use frame_support::weights::Weight;
1414
use frame_support::{storage_alias, Identity};
1515
use sp_domains::{ChainId, ChannelId};
1616
use sp_messenger::messages::{Channel as ChannelV1, ChannelState, Nonce};
17-
#[cfg(feature = "std")]
18-
use std::collections::BTreeSet;
1917

2018
pub type VersionCheckedMigrateDomainsV1ToV2<T> = VersionedMigration<
2119
1,
@@ -91,6 +89,7 @@ pub(crate) mod migrate_channels {
9189
Channel<BalanceOf<T>, <T as frame_system::Config>::AccountId>,
9290
OptionQuery,
9391
>;
92+
9493
pub(super) fn migrate_channels<T: Config>() -> Weight {
9594
let mut count = 0;
9695
Channels::<T>::drain().for_each(|(chain_id, channel_id, channel)| {
@@ -111,13 +110,12 @@ pub(crate) mod migrate_channels {
111110
})
112111
}
113112

114-
pub(crate) fn get_open_channels<T: Config>() -> BTreeSet<(ChainId, ChannelId)> {
115-
let keys: BTreeSet<(ChainId, ChannelId)> = ChannelStorageV1::<T>::iter_keys().collect();
113+
pub(crate) fn get_channels_and_states<T: Config>() -> Vec<(ChainId, ChannelId, ChannelState)> {
114+
let keys: Vec<(ChainId, ChannelId)> = ChannelStorageV1::<T>::iter_keys().collect();
116115
keys.into_iter()
117-
.filter(|(chain_id, channel_id)| {
118-
get_channel::<T>(*chain_id, *channel_id)
119-
.map(|channel| channel.state != ChannelState::Closed)
120-
.unwrap_or_default()
116+
.filter_map(|(chain_id, channel_id)| {
117+
get_channel::<T>(chain_id, channel_id)
118+
.map(|channel| (chain_id, channel_id, channel.state))
121119
})
122120
.collect()
123121
}

domains/pallets/messenger/src/tests.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ use sp_domains::proof_provider_and_verifier::{StorageProofVerifier, Verification
2222
use sp_domains::DomainAllowlistUpdates;
2323
use sp_messenger::endpoint::{Endpoint, EndpointPayload, EndpointRequest, Sender};
2424
use sp_messenger::messages::{
25-
ChainId, ChannelOpenParamsV1, CrossDomainMessage, MessageWeightTag, PayloadV1, Proof,
26-
ProtocolMessageRequest, RequestResponse, VersionedPayload,
25+
BlockMessagesQuery, ChainId, ChannelOpenParamsV1, CrossDomainMessage, MessageWeightTag,
26+
PayloadV1, Proof, ProtocolMessageRequest, RequestResponse, VersionedPayload,
2727
};
2828
use sp_mmr_primitives::{EncodableOpaqueLeaf, LeafProof as MmrProof};
2929
use sp_runtime::traits::{Convert, Zero};
@@ -80,7 +80,13 @@ fn create_channel(chain_id: ChainId, channel_id: ChannelId) {
8080
));
8181

8282
// check outbox relayer storage key generation
83-
let messages_with_keys = chain_a::Messenger::get_block_messages();
83+
let query = BlockMessagesQuery {
84+
chain_id,
85+
channel_id,
86+
outbox_from: Nonce::zero(),
87+
inbox_responses_from: Nonce::zero(),
88+
};
89+
let messages_with_keys = chain_a::Messenger::get_block_messages(query);
8490
assert_eq!(messages_with_keys.outbox.len(), 1);
8591
assert_eq!(messages_with_keys.inbox_responses.len(), 0);
8692
let expected_key =
@@ -294,7 +300,13 @@ fn open_channel_between_chains(
294300
}));
295301

296302
// check inbox response storage key generation
297-
let messages_with_keys = chain_b::Messenger::get_block_messages();
303+
let query = BlockMessagesQuery {
304+
chain_id: chain_a_id,
305+
channel_id,
306+
outbox_from: Nonce::zero(),
307+
inbox_responses_from: Nonce::zero(),
308+
};
309+
let messages_with_keys = chain_b::Messenger::get_block_messages(query);
298310
assert_eq!(messages_with_keys.outbox.len(), 0);
299311
assert_eq!(messages_with_keys.inbox_responses.len(), 1);
300312
let expected_key = InboxResponses::<chain_b::Runtime>::hashed_key_for((

domains/primitives/messenger/src/lib.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub mod messages;
2323
#[cfg(not(feature = "std"))]
2424
extern crate alloc;
2525

26-
use crate::messages::{MessageKey, Nonce};
26+
use crate::messages::{ChannelState, MessageKey, Nonce};
2727
#[cfg(not(feature = "std"))]
2828
use alloc::collections::BTreeMap;
2929
#[cfg(not(feature = "std"))]
@@ -37,7 +37,9 @@ use frame_support::inherent::{InherentIdentifier, IsFatalError};
3737
use frame_support::storage::storage_prefix;
3838
#[cfg(feature = "runtime-benchmarks")]
3939
use frame_support::{Identity, StorageHasher};
40-
use messages::{BlockMessagesWithStorageKey, ChannelId, CrossDomainMessage, MessageId};
40+
use messages::{
41+
BlockMessagesQuery, BlockMessagesWithStorageKey, ChannelId, CrossDomainMessage, MessageId,
42+
};
4143
use parity_scale_codec::{Decode, Encode};
4244
use scale_info::TypeInfo;
4345
use sp_domains::{ChainId, DomainAllowlistUpdates, DomainId};
@@ -266,7 +268,7 @@ pub struct ChannelNonce {
266268

267269
sp_api::decl_runtime_apis! {
268270
/// Api useful for relayers to fetch messages and submit transactions.
269-
#[api_version(2)]
271+
#[api_version(3)]
270272
pub trait RelayerApi<BlockNumber, CNumber, CHash>
271273
where
272274
BlockNumber: Encode + Decode,
@@ -301,6 +303,13 @@ sp_api::decl_runtime_apis! {
301303

302304
/// Returns all the open channels to other chains.
303305
fn open_channels() -> BTreeSet<(ChainId, ChannelId)>;
306+
307+
/// Returns outbox and inbox responses from given nonce to maximum allowed nonce per block
308+
/// Storage key is used to generate the storage proof for the message.
309+
fn block_messages_with_query(query: BlockMessagesQuery) -> BlockMessagesWithStorageKey;
310+
311+
/// Returns all the channels to other chains and their local Channel state.
312+
fn channels_and_state() -> Vec<(ChainId, ChannelId, ChannelState)>;
304313
}
305314

306315
/// Api to provide XDM extraction from Runtime Calls.

domains/primitives/messenger/src/messages.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,15 @@ impl BlockMessagesWithStorageKey {
269269
}
270270
}
271271

272+
/// A query to fetch block messages for Outbox and Inbox Responses
273+
#[derive(Debug, Encode, Decode, TypeInfo, Clone, Eq, PartialEq)]
274+
pub struct BlockMessagesQuery {
275+
pub chain_id: ChainId,
276+
pub channel_id: ChannelId,
277+
pub outbox_from: Nonce,
278+
pub inbox_responses_from: Nonce,
279+
}
280+
272281
impl<BlockNumber, BlockHash, MmrHash> CrossDomainMessage<BlockNumber, BlockHash, MmrHash> {
273282
pub fn from_relayer_msg_with_proof(
274283
r_msg: BlockMessageWithStorageKey,

domains/runtime/auto-id/src/lib.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ use sp_core::{Get, OpaqueMetadata};
4242
use sp_domains::{ChannelId, DomainAllowlistUpdates, DomainId, Transfers};
4343
use sp_messenger::endpoint::{Endpoint, EndpointHandler as EndpointHandlerT, EndpointId};
4444
use sp_messenger::messages::{
45-
BlockMessagesWithStorageKey, ChainId, CrossDomainMessage, MessageId, MessageKey,
45+
BlockMessagesQuery, BlockMessagesWithStorageKey, ChainId, ChannelState, CrossDomainMessage,
46+
MessageId, MessageKey,
4647
};
4748
use sp_messenger::{ChannelNonce, XdmId};
4849
use sp_messenger_host_functions::{get_storage_key, StorageKeyRequest};
@@ -1114,7 +1115,7 @@ impl_runtime_apis! {
11141115

11151116
impl sp_messenger::RelayerApi<Block, BlockNumber, ConsensusBlockNumber, ConsensusBlockHash> for Runtime {
11161117
fn block_messages() -> BlockMessagesWithStorageKey {
1117-
Messenger::get_block_messages()
1118+
BlockMessagesWithStorageKey::default()
11181119
}
11191120

11201121
fn outbox_message_unsigned(msg: CrossDomainMessage<NumberFor<Block>, BlockHashFor<Block>, BlockHashFor<Block>>) -> Option<ExtrinsicFor<Block>> {
@@ -1144,6 +1145,14 @@ impl_runtime_apis! {
11441145
fn open_channels() -> BTreeSet<(ChainId, ChannelId)> {
11451146
Messenger::open_channels()
11461147
}
1148+
1149+
fn block_messages_with_query(query: BlockMessagesQuery) -> BlockMessagesWithStorageKey {
1150+
Messenger::get_block_messages(query)
1151+
}
1152+
1153+
fn channels_and_state() -> Vec<(ChainId, ChannelId, ChannelState)>{
1154+
Messenger::channels_and_states()
1155+
}
11471156
}
11481157

11491158
impl sp_domain_sudo::DomainSudoApi<Block> for Runtime {

0 commit comments

Comments
 (0)