Skip to content

Commit afc8da0

Browse files
committed
remove redundant data from runtime calls and rename confusing runtime apis
1 parent b2fdda4 commit afc8da0

File tree

12 files changed

+144
-127
lines changed

12 files changed

+144
-127
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use sp_domains_fraud_proof::fraud_proof::FraudProof;
1717
use sp_domains_fraud_proof::storage_proof::FraudProofStorageKeyRequest;
1818
use sp_messenger::messages::{
1919
BlockMessagesQuery, BlockMessagesWithStorageKey, ChainId, ChannelId, ChannelStateWithNonce,
20-
CrossDomainMessage, MessageId, MessageKey, Nonce as XdmNonce,
20+
CrossDomainMessage, MessageId, MessageKey, MessagesWithStorageKey, Nonce as XdmNonce,
2121
};
2222
use sp_messenger::{ChannelNonce, XdmId};
2323
use sp_runtime::traits::NumberFor;
@@ -414,19 +414,19 @@ sp_api::impl_runtime_apis! {
414414
unreachable!()
415415
}
416416

417-
fn block_messages_with_query(_: BlockMessagesQuery) -> BlockMessagesWithStorageKey {
417+
fn block_messages_with_query(_: BlockMessagesQuery) -> MessagesWithStorageKey {
418418
unreachable!()
419419
}
420420

421421
fn channels_and_state() -> Vec<(ChainId, ChannelId, ChannelStateWithNonce)> {
422422
unreachable!()
423423
}
424424

425-
fn should_relay_outbox_messages(_: ChainId, _: ChannelId, _: XdmNonce) -> Option<XdmNonce> {
425+
fn first_outbox_message_nonce_to_relay(_: ChainId, _: ChannelId, _: XdmNonce) -> Option<XdmNonce> {
426426
unreachable!()
427427
}
428428

429-
fn should_relay_inbox_message_responses(_: ChainId, _: ChannelId, _: XdmNonce) -> Option<XdmNonce> {
429+
fn first_inbox_message_response_nonce_to_relay(_: ChainId, _: ChannelId, _: XdmNonce) -> Option<XdmNonce> {
430430
unreachable!()
431431
}
432432
}

crates/subspace-runtime/src/lib.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ use sp_domains_fraud_proof::storage_proof::{
7272
use sp_messenger::endpoint::{Endpoint, EndpointHandler as EndpointHandlerT, EndpointId};
7373
use sp_messenger::messages::{
7474
BlockMessagesQuery, BlockMessagesWithStorageKey, ChainId, ChannelStateWithNonce,
75-
CrossDomainMessage, MessageId, MessageKey, Nonce as XdmNonce,
75+
CrossDomainMessage, MessageId, MessageKey, MessagesWithStorageKey, Nonce as XdmNonce,
7676
};
7777
use sp_messenger::{ChannelNonce, XdmId};
7878
use sp_messenger_host_functions::{get_storage_key, StorageKeyRequest};
@@ -1704,20 +1704,20 @@ impl_runtime_apis! {
17041704
Messenger::open_channels()
17051705
}
17061706

1707-
fn block_messages_with_query(query: BlockMessagesQuery) -> BlockMessagesWithStorageKey {
1707+
fn block_messages_with_query(query: BlockMessagesQuery) -> MessagesWithStorageKey {
17081708
Messenger::get_block_messages(query)
17091709
}
17101710

17111711
fn channels_and_state() -> Vec<(ChainId, ChannelId, ChannelStateWithNonce)> {
17121712
Messenger::channels_and_states()
17131713
}
17141714

1715-
fn should_relay_outbox_messages(dst_chain_id: ChainId, channel_id: ChannelId, from_nonce: XdmNonce) -> Option<XdmNonce> {
1716-
Messenger::should_relay_outbox_messages(dst_chain_id, channel_id, from_nonce)
1715+
fn first_outbox_message_nonce_to_relay(dst_chain_id: ChainId, channel_id: ChannelId, from_nonce: XdmNonce) -> Option<XdmNonce> {
1716+
Messenger::first_outbox_message_nonce_to_relay(dst_chain_id, channel_id, from_nonce)
17171717
}
17181718

1719-
fn should_relay_inbox_message_responses(dst_chain_id: ChainId, channel_id: ChannelId, from_nonce: XdmNonce) -> Option<XdmNonce> {
1720-
Messenger::should_relay_inbox_message_responses(dst_chain_id, channel_id, from_nonce)
1719+
fn first_inbox_message_response_nonce_to_relay(dst_chain_id: ChainId, channel_id: ChannelId, from_nonce: XdmNonce) -> Option<XdmNonce> {
1720+
Messenger::first_inbox_message_response_nonce_to_relay(dst_chain_id, channel_id, from_nonce)
17211721
}
17221722
}
17231723

domains/client/relayer/src/lib.rs

Lines changed: 53 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ use sp_api::{ApiExt, ApiRef, ProvideRuntimeApi};
2222
use sp_core::H256;
2323
use sp_domains::{ChannelId, DomainsApi};
2424
use sp_messenger::messages::{
25-
BlockMessageWithStorageKey, BlockMessagesQuery, BlockMessagesWithStorageKey, ChainId,
26-
ChannelState, ChannelStateWithNonce, CrossDomainMessage, Nonce, Proof,
25+
BlockMessagesQuery, ChainId, ChannelState, ChannelStateWithNonce, CrossDomainMessage,
26+
MessageNonceWithStorageKey, MessagesWithStorageKey, Nonce, Proof,
2727
};
2828
use sp_messenger::{MessengerApi, RelayerApi};
2929
use sp_mmr_primitives::MmrApi;
@@ -148,29 +148,36 @@ where
148148
}
149149

150150
fn construct_cross_chain_message_and_submit<CNumber, CHash, Submitter, ProofConstructor>(
151-
msgs: Vec<BlockMessageWithStorageKey>,
151+
msgs: (ChainId, ChainId, ChannelId, Vec<MessageNonceWithStorageKey>),
152152
proof_constructor: ProofConstructor,
153153
submitter: Submitter,
154154
) -> Result<(), Error>
155155
where
156156
Submitter: Fn(CrossDomainMessage<CNumber, CHash, H256>) -> Result<(), Error>,
157157
ProofConstructor: Fn(&[u8], ChainId) -> Result<Proof<CNumber, CHash, H256>, Error>,
158158
{
159+
let (src_chain_id, dst_chain_id, channel_id, msgs) = msgs;
159160
for msg in msgs {
160-
let proof = match proof_constructor(&msg.storage_key, msg.dst_chain_id) {
161+
let proof = match proof_constructor(&msg.storage_key, dst_chain_id) {
161162
Ok(proof) => proof,
162163
Err(err) => {
163164
tracing::error!(
164165
target: LOG_TARGET,
165166
"Failed to construct storage proof for message: {:?} bound to chain: {:?} with error: {:?}",
166-
(msg.channel_id, msg.nonce),
167-
msg.dst_chain_id,
167+
(channel_id, msg.nonce),
168+
dst_chain_id,
168169
err
169170
);
170171
continue;
171172
}
172173
};
173-
let msg = CrossDomainMessage::from_relayer_msg_with_proof(msg, proof);
174+
let msg = CrossDomainMessage::from_relayer_msg_with_proof(
175+
src_chain_id,
176+
dst_chain_id,
177+
channel_id,
178+
msg,
179+
proof,
180+
);
174181
let (dst_domain, msg_id) = (msg.dst_chain_id, (msg.channel_id, msg.nonce));
175182
if let Err(err) = submitter(msg) {
176183
tracing::error!(
@@ -246,7 +253,7 @@ fn fetch_and_filter_messages<Client, Block, CClient, CBlock>(
246253
fetch_message_at: Block::Hash,
247254
consensus_client: &Arc<CClient>,
248255
self_chain_id: ChainId,
249-
) -> Result<BlockMessagesWithStorageKey, Error>
256+
) -> Result<Vec<(ChainId, ChannelId, MessagesWithStorageKey)>, Error>
250257
where
251258
CBlock: BlockT,
252259
CClient: AuxStore + HeaderBackend<CBlock>,
@@ -256,7 +263,7 @@ where
256263
{
257264
// return no messages for previous relayer version
258265
if !is_relayer_api_version_available::<_, Block, CBlock>(client, 3, fetch_message_at) {
259-
return Ok(BlockMessagesWithStorageKey::default());
266+
return Ok(vec![]);
260267
}
261268

262269
fetch_messages::<_, _, Block, CBlock>(
@@ -379,35 +386,37 @@ where
379386
}
380387
};
381388

382-
construct_cross_chain_message_and_submit::<NumberFor<CBlock>, CBlock::Hash, _, _>(
383-
block_messages.outbox,
384-
|key, dst_chain_id| {
385-
Self::construct_xdm_proof(
386-
consensus_chain_client,
387-
domain_client,
388-
dst_chain_id,
389-
mmr_consensus_block,
390-
key,
391-
xdm_proof_data.clone(),
392-
)
393-
},
394-
|msg| gossip_outbox_message(domain_client, msg, gossip_message_sink),
395-
)?;
396-
397-
construct_cross_chain_message_and_submit::<NumberFor<CBlock>, CBlock::Hash, _, _>(
398-
block_messages.inbox_responses,
399-
|key, dst_chain_id| {
400-
Self::construct_xdm_proof(
401-
consensus_chain_client,
402-
domain_client,
403-
dst_chain_id,
404-
mmr_consensus_block,
405-
key,
406-
xdm_proof_data.clone(),
407-
)
408-
},
409-
|msg| gossip_inbox_message_response(domain_client, msg, gossip_message_sink),
410-
)?;
389+
for (dst_chain_id, channel_id, messages) in block_messages {
390+
construct_cross_chain_message_and_submit::<NumberFor<CBlock>, CBlock::Hash, _, _>(
391+
(chain_id, dst_chain_id, channel_id, messages.outbox),
392+
|key, dst_chain_id| {
393+
Self::construct_xdm_proof(
394+
consensus_chain_client,
395+
domain_client,
396+
dst_chain_id,
397+
mmr_consensus_block,
398+
key,
399+
xdm_proof_data.clone(),
400+
)
401+
},
402+
|msg| gossip_outbox_message(domain_client, msg, gossip_message_sink),
403+
)?;
404+
405+
construct_cross_chain_message_and_submit::<NumberFor<CBlock>, CBlock::Hash, _, _>(
406+
(chain_id, dst_chain_id, channel_id, messages.inbox_responses),
407+
|key, dst_chain_id| {
408+
Self::construct_xdm_proof(
409+
consensus_chain_client,
410+
domain_client,
411+
dst_chain_id,
412+
mmr_consensus_block,
413+
key,
414+
xdm_proof_data.clone(),
415+
)
416+
},
417+
|msg| gossip_inbox_message_response(domain_client, msg, gossip_message_sink),
418+
)?;
419+
}
411420

412421
Ok(())
413422
}
@@ -471,7 +480,7 @@ fn filter_block_messages<Client, Block, CBlock>(
471480
api: &ApiRef<'_, Client::Api>,
472481
best_hash: Block::Hash,
473482
query: BlockMessagesQuery,
474-
messages: &mut BlockMessagesWithStorageKey,
483+
messages: &mut MessagesWithStorageKey,
475484
) -> Result<(), Error>
476485
where
477486
Block: BlockT,
@@ -486,8 +495,8 @@ where
486495
inbox_responses_from,
487496
} = query;
488497
let maybe_outbox_nonce =
489-
api.should_relay_outbox_messages(best_hash, chain_id, channel_id, outbox_from)?;
490-
let maybe_inbox_response_nonce = api.should_relay_inbox_message_responses(
498+
api.first_outbox_message_nonce_to_relay(best_hash, chain_id, channel_id, outbox_from)?;
499+
let maybe_inbox_response_nonce = api.first_inbox_message_response_nonce_to_relay(
491500
best_hash,
492501
chain_id,
493502
channel_id,
@@ -515,7 +524,7 @@ fn fetch_messages<Backend, Client, Block, CBlock>(
515524
client: &Arc<Client>,
516525
fetch_message_at: Block::Hash,
517526
self_chain_id: ChainId,
518-
) -> Result<BlockMessagesWithStorageKey, Error>
527+
) -> Result<Vec<(ChainId, ChannelId, MessagesWithStorageKey)>, Error>
519528
where
520529
Block: BlockT,
521530
CBlock: BlockT,
@@ -612,16 +621,9 @@ where
612621
.ok()?;
613622
}
614623

615-
Some(messages)
624+
Some((dst_chain_id, channel_id, messages))
616625
})
617-
.fold(
618-
BlockMessagesWithStorageKey::default(),
619-
|mut acc, mut messages| {
620-
acc.outbox.append(&mut messages.outbox);
621-
acc.inbox_responses.append(&mut messages.inbox_responses);
622-
acc
623-
},
624-
);
626+
.collect();
625627

626628
Ok(total_messages)
627629
}

domains/pallets/messenger/src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1513,12 +1513,12 @@ where
15131513
}
15141514

15151515
/// Returns the first outbox message nonce that should be relayed to the dst_chain.
1516-
pub fn should_relay_outbox_messages(
1516+
pub fn first_outbox_message_nonce_to_relay(
15171517
dst_chain_id: ChainId,
15181518
channel_id: ChannelId,
15191519
from_nonce: Nonce,
15201520
) -> Option<Nonce> {
1521-
Self::should_relay_message(
1521+
Self::first_relay_message(
15221522
dst_chain_id,
15231523
channel_id,
15241524
from_nonce,
@@ -1527,20 +1527,20 @@ where
15271527
}
15281528

15291529
/// Returns the first inbox response message nonce that should be relayed to the dst_chain.
1530-
pub fn should_relay_inbox_message_responses(
1530+
pub fn first_inbox_message_response_nonce_to_relay(
15311531
dst_chain_id: ChainId,
15321532
channel_id: ChannelId,
15331533
from_nonce: Nonce,
15341534
) -> Option<Nonce> {
1535-
Self::should_relay_message(
1535+
Self::first_relay_message(
15361536
dst_chain_id,
15371537
channel_id,
15381538
from_nonce,
15391539
InboxResponses::<T>::contains_key,
15401540
)
15411541
}
15421542

1543-
fn should_relay_message<Check>(
1543+
fn first_relay_message<Check>(
15441544
dst_chain_id: ChainId,
15451545
channel_id: ChannelId,
15461546
from_nonce: Nonce,

domains/pallets/messenger/src/messages.rs

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ use alloc::collections::BTreeMap;
1616
use frame_support::ensure;
1717
use sp_messenger::endpoint::{EndpointHandler, EndpointRequest, EndpointResponse};
1818
use sp_messenger::messages::{
19-
BlockMessageWithStorageKey, BlockMessagesQuery, BlockMessagesWithStorageKey, ChainId,
20-
ChannelOpenParamsV1, Message, MessageId, MessageKey, MessageWeightTag, PayloadV1,
19+
BlockMessagesQuery, ChainId, ChannelOpenParamsV1, Message, MessageId, MessageKey,
20+
MessageNonceWithStorageKey, MessageWeightTag, MessagesWithStorageKey, PayloadV1,
2121
ProtocolMessageRequest, ProtocolMessageResponse, RequestResponse, VersionedPayload,
2222
};
2323

@@ -389,7 +389,7 @@ impl<T: Config> Pallet<T> {
389389
Ok(())
390390
}
391391

392-
pub fn get_block_messages(query: BlockMessagesQuery) -> BlockMessagesWithStorageKey {
392+
pub fn get_block_messages(query: BlockMessagesQuery) -> MessagesWithStorageKey {
393393
let BlockMessagesQuery {
394394
chain_id,
395395
channel_id,
@@ -411,37 +411,31 @@ impl<T: Config> Pallet<T> {
411411
return Default::default();
412412
}
413413

414-
let mut messages_with_storage_key = BlockMessagesWithStorageKey::default();
414+
let mut messages_with_storage_key = MessagesWithStorageKey::default();
415415

416416
// create storage keys for inbox responses
417-
inbox_responses_weight_tags.into_iter().for_each(
418-
|((chain_id, (channel_id, nonce)), weight_tag)| {
417+
inbox_responses_weight_tags
418+
.into_iter()
419+
.for_each(|(nonce, weight_tag)| {
419420
let storage_key =
420421
InboxResponses::<T>::hashed_key_for((chain_id, channel_id, nonce));
421422
messages_with_storage_key
422423
.inbox_responses
423-
.push(BlockMessageWithStorageKey {
424-
src_chain_id: T::SelfChainId::get(),
425-
dst_chain_id: chain_id,
426-
channel_id,
424+
.push(MessageNonceWithStorageKey {
427425
nonce,
428426
storage_key,
429427
weight_tag,
430428
});
431-
},
432-
);
429+
});
433430

434431
// create storage keys for outbox
435432
outbox_weight_tags
436433
.into_iter()
437-
.for_each(|((chain_id, (channel_id, nonce)), weight_tag)| {
434+
.for_each(|(nonce, weight_tag)| {
438435
let storage_key = Outbox::<T>::hashed_key_for((chain_id, channel_id, nonce));
439436
messages_with_storage_key
440437
.outbox
441-
.push(BlockMessageWithStorageKey {
442-
src_chain_id: T::SelfChainId::get(),
443-
dst_chain_id: chain_id,
444-
channel_id,
438+
.push(MessageNonceWithStorageKey {
445439
nonce,
446440
storage_key,
447441
weight_tag,
@@ -454,7 +448,7 @@ impl<T: Config> Pallet<T> {
454448
fn get_weight_tags<WTG>(
455449
from: MessageKey,
456450
weight_tag_getter: WTG,
457-
) -> BTreeMap<(ChainId, MessageId), MessageWeightTag>
451+
) -> BTreeMap<Nonce, MessageWeightTag>
458452
where
459453
WTG: Fn((ChainId, MessageId)) -> Option<MessageWeightTag>,
460454
{
@@ -465,7 +459,7 @@ impl<T: Config> Pallet<T> {
465459
// if the nonce is already processed, short circuit and return
466460
None => return weight_tags,
467461
Some(weight_tag) => {
468-
weight_tags.insert((chain_id, (channel_id, nonce)), weight_tag);
462+
weight_tags.insert(nonce, weight_tag);
469463
}
470464
};
471465

0 commit comments

Comments
 (0)