Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
no mutability
  • Loading branch information
pmikolajczyk41 committed Sep 5, 2022
commit 53607bcc799183ffab1e37091620175bd80c4da9
19 changes: 10 additions & 9 deletions utils/frame/remote-externalities/src/rpc_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use jsonrpsee::{
};
use serde::de::DeserializeOwned;
use sp_runtime::{generic::SignedBlock, traits::Block as BlockT};
use std::cell::Cell;

enum RpcCall {
GetHeader,
Expand Down Expand Up @@ -65,7 +66,7 @@ async fn make_request<'a, T: DeserializeOwned>(
/// Be careful with reusing the connection in a multithreaded environment.
pub struct RpcService {
uri: String,
client: Option<Client>,
client: Cell<Option<Client>>,
keep_connection: bool,
}

Expand All @@ -74,7 +75,7 @@ impl RpcService {
///
/// Does not connect yet.
pub fn new<S: AsRef<str>>(uri: S, keep_connection: bool) -> Self {
Self { uri: uri.as_ref().to_string(), client: None, keep_connection }
Self { uri: uri.as_ref().to_string(), client: Cell::new(None), keep_connection }
}

/// Returns the address at which requests are sent.
Expand All @@ -98,26 +99,26 @@ impl RpcService {

/// Generic method for making RPC requests.
async fn make_request<'a, T: DeserializeOwned>(
&mut self,
&self,
call: RpcCall,
params: Option<ParamsSer<'a>>,
) -> Result<T, String> {
match &self.client {
match &self.client.get() {
// `self.keep_connection` must be `true.
Some(ref client) => make_request(client, call, params).await,
None => {
let client = self.build_client().await?;
let result = make_request(&client, call, params).await;
if self.keep_connection {
self.client = Some(client)
self.client.set(Some(client))
};
result
},
}
}

/// Get the header of the block identified by `at`.
pub async fn get_header<Block>(&mut self, at: Block::Hash) -> Result<Block::Header, String>
pub async fn get_header<Block>(&self, at: Block::Hash) -> Result<Block::Header, String>
where
Block: BlockT,
Block::Header: DeserializeOwned,
Expand All @@ -126,13 +127,13 @@ impl RpcService {
}

/// Get the finalized head.
pub async fn get_finalized_head<Block: BlockT>(&mut self) -> Result<Block::Hash, String> {
pub async fn get_finalized_head<Block: BlockT>(&self) -> Result<Block::Hash, String> {
self.make_request(RpcCall::GetFinalizedHead, None).await
}

/// Get the signed block identified by `at`.
pub async fn get_block<Block: BlockT + DeserializeOwned>(
&mut self,
&self,
at: Block::Hash,
) -> Result<Block, String> {
Ok(self
Expand All @@ -143,7 +144,7 @@ impl RpcService {

/// Get the runtime version of a given chain.
pub async fn get_runtime_version<Block: BlockT + DeserializeOwned>(
&mut self,
&self,
at: Option<Block::Hash>,
) -> Result<sp_version::RuntimeVersion, String> {
self.make_request(RpcCall::GetRuntimeVersion, rpc_params!(at)).await
Expand Down
25 changes: 13 additions & 12 deletions utils/frame/try-runtime/cli/src/commands/follow_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,15 @@ where
Block::Header: HeaderT,
{
/// Awaits for the header of the block with hash `hash`.
async fn get_header(&mut self, hash: Block::Hash) -> Block::Header;
async fn get_header(&self, hash: Block::Hash) -> Block::Header;
}

#[async_trait]
impl<Block: BlockT> HeaderProvider<Block> for RpcService
where
Block::Header: DeserializeOwned,
{
async fn get_header(&mut self, hash: Block::Hash) -> Block::Header {
async fn get_header(&self, hash: Block::Hash) -> Block::Header {
self.get_header::<Block>(hash).await.unwrap()
}
}
Expand Down Expand Up @@ -149,7 +149,7 @@ where
/// them lack justification).
struct FinalizedHeaders<'a, Block: BlockT, HP: HeaderProvider<Block>, HS: HeaderSubscription<Block>>
{
header_provider: &'a mut HP,
header_provider: &'a HP,
subscription: HS,
fetched_headers: VecDeque<Block::Header>,
last_returned: Option<<Block::Header as HeaderT>::Hash>,
Expand All @@ -160,7 +160,7 @@ impl<'a, Block: BlockT, HP: HeaderProvider<Block>, HS: HeaderSubscription<Block>
where
<Block as BlockT>::Header: DeserializeOwned,
{
pub fn new(header_provider: &'a mut HP, subscription: HS) -> Self {
pub fn new(header_provider: &'a HP, subscription: HS) -> Self {
Self {
header_provider,
subscription,
Expand Down Expand Up @@ -232,7 +232,7 @@ where
let mut rpc_service = RpcService::new(&command.uri, command.keep_connection);

let mut finalized_headers: FinalizedHeaders<Block, RpcService, Subscription<Block::Header>> =
FinalizedHeaders::new(&mut rpc_service, subscription);
FinalizedHeaders::new(&rpc_service, subscription);

while let Some(header) = finalized_headers.next().await {
let hash = header.hash();
Expand Down Expand Up @@ -330,12 +330,13 @@ where
mod tests {
use super::*;
use sp_runtime::testing::{Block as TBlock, ExtrinsicWrapper, Header};
use std::cell::Cell;

type Block = TBlock<ExtrinsicWrapper<()>>;
type BlockNumber = u64;
type Hash = H256;

struct MockHeaderProvider(pub VecDeque<BlockNumber>);
struct MockHeaderProvider(pub Cell<VecDeque<BlockNumber>>);

fn headers() -> Vec<Header> {
let mut headers = vec![Header::new_from_number(0)];
Expand All @@ -350,8 +351,8 @@ mod tests {

#[async_trait]
impl HeaderProvider<Block> for MockHeaderProvider {
async fn get_header(&mut self, _hash: Hash) -> Header {
let height = self.0.pop_front().unwrap();
async fn get_header(&self, _hash: Hash) -> Header {
let height = self.0.get().pop_front().unwrap();
headers()[height as usize].clone()
}
}
Expand All @@ -369,9 +370,9 @@ mod tests {
async fn finalized_headers_works_when_every_block_comes_from_subscription() {
let heights = vec![4, 5, 6, 7];

let mut provider = MockHeaderProvider(vec![].into());
let provider = MockHeaderProvider(Cell::new(vec![].into()));
let subscription = MockHeaderSubscription(heights.clone().into());
let mut headers = FinalizedHeaders::new(&mut provider, subscription);
let mut headers = FinalizedHeaders::new(&provider, subscription);

for h in heights {
assert_eq!(h, headers.next().await.unwrap().number);
Expand All @@ -386,9 +387,9 @@ mod tests {
// Consecutive headers will be requested in the reversed order.
let heights_not_in_subscription = vec![5, 9, 8, 7];

let mut provider = MockHeaderProvider(heights_not_in_subscription.into());
let provider = MockHeaderProvider(Cell::new(heights_not_in_subscription.into()));
let subscription = MockHeaderSubscription(heights_in_subscription.into());
let mut headers = FinalizedHeaders::new(&mut provider, subscription);
let mut headers = FinalizedHeaders::new(&provider, subscription);

for h in all_heights {
assert_eq!(h, headers.next().await.unwrap().number);
Expand Down