Return cumulative dispatch weight of messages from the messageLane_proveMessages (#469)

* implement OutboundLaneApi and InboundLaneApi for Millau /Rialto runtimes

* fixed typo
This commit is contained in:
Svyatoslav Nikolsky
2020-10-28 21:35:29 +03:00
committed by Bastian Köcher
parent 51f39c4427
commit ef9357596f
8 changed files with 129 additions and 12 deletions
+32 -10
View File
@@ -18,12 +18,14 @@
use crate::error::{Error, FutureResult};
use bp_message_lane::{LaneId, MessageNonce};
use bp_message_lane::{LaneId, MessageNonce, OutboundLaneApi};
use bp_runtime::InstanceId;
use frame_support::weights::Weight;
use futures::{FutureExt, TryFutureExt};
use jsonrpc_core::futures::Future as _;
use jsonrpc_derive::rpc;
use sc_client_api::Backend as BackendT;
use sp_api::ProvideRuntimeApi;
use sp_blockchain::{Error as BlockchainError, HeaderBackend};
use sp_core::{storage::StorageKey, Bytes};
use sp_runtime::{codec::Encode, generic::BlockId, traits::Block as BlockT};
@@ -54,7 +56,8 @@ pub trait Runtime: Send + Sync + 'static {
/// Provides RPC methods for interacting with message-lane pallet.
#[rpc]
pub trait MessageLaneApi<BlockHash> {
/// Returns proof-of-message(s) in given inclusive range.
/// Returns cumulative dispatch weight of messages in given inclusive range and their storage proof.
/// The state of outbound lane is included in the proof if `include_outbound_lane_state` is true.
#[rpc(name = "messageLane_proveMessages")]
fn prove_messages(
&self,
@@ -64,7 +67,7 @@ pub trait MessageLaneApi<BlockHash> {
end: MessageNonce,
include_outbound_lane_state: bool,
block: Option<BlockHash>,
) -> FutureResult<MessagesProof>;
) -> FutureResult<(Weight, MessagesProof)>;
/// Returns proof-of-message(s) delivery.
#[rpc(name = "messageLane_proveMessagesDelivery")]
@@ -77,16 +80,18 @@ pub trait MessageLaneApi<BlockHash> {
}
/// Implements the MessageLaneApi trait for interacting with message lanes.
pub struct MessageLaneRpcHandler<Block, Backend, R> {
pub struct MessageLaneRpcHandler<Block, Client, Backend, R> {
client: Arc<Client>,
backend: Arc<Backend>,
runtime: Arc<R>,
_phantom: std::marker::PhantomData<Block>,
}
impl<Block, Backend, R> MessageLaneRpcHandler<Block, Backend, R> {
impl<Block, Client, Backend, R> MessageLaneRpcHandler<Block, Client, Backend, R> {
/// Creates new mesage lane RPC handler.
pub fn new(backend: Arc<Backend>, runtime: Arc<R>) -> Self {
pub fn new(client: Arc<Client>, backend: Arc<Backend>, runtime: Arc<R>) -> Self {
Self {
client,
backend,
runtime,
_phantom: Default::default(),
@@ -94,9 +99,11 @@ impl<Block, Backend, R> MessageLaneRpcHandler<Block, Backend, R> {
}
}
impl<Block, Backend, R> MessageLaneApi<Block::Hash> for MessageLaneRpcHandler<Block, Backend, R>
impl<Block, Client, Backend, R> MessageLaneApi<Block::Hash> for MessageLaneRpcHandler<Block, Client, Backend, R>
where
Block: BlockT,
Client: ProvideRuntimeApi<Block> + Send + Sync + 'static,
Client::Api: OutboundLaneApi<Block>,
Backend: BackendT<Block> + 'static,
R: Runtime,
{
@@ -108,7 +115,22 @@ where
end: MessageNonce,
include_outbound_lane_state: bool,
block: Option<Block::Hash>,
) -> FutureResult<MessagesProof> {
) -> FutureResult<(Weight, MessagesProof)> {
let block = unwrap_or_best(&*self.backend, block);
let messages_dispatch_weight_result =
self.client
.runtime_api()
.messages_dispatch_weight(&BlockId::Hash(block), lane, begin, end);
let messages_dispatch_weight = match messages_dispatch_weight_result {
Ok(messages_dispatch_weight) => messages_dispatch_weight,
Err(error) => {
return Box::new(jsonrpc_core::futures::future::err(
blockchain_err(BlockchainError::Execution(Box::new(format!("{:?}", error)))).into(),
))
}
};
let runtime = self.runtime.clone();
let outbound_lane_data_key = if include_outbound_lane_state {
Some(runtime.inbound_lane_data_key(&instance, &lane))
@@ -118,14 +140,14 @@ where
Box::new(
prove_keys_read(
self.backend.clone(),
block,
Some(block),
(begin..=end)
.map(move |nonce| runtime.message_key(&instance, &lane, nonce))
.chain(outbound_lane_data_key.into_iter()),
)
.boxed()
.compat()
.map(serialize_storage_proof)
.map(move |proof| (messages_dispatch_weight, serialize_storage_proof(proof)))
.map_err(Into::into),
)
}