Skip to content

Commit

Permalink
Change generic types of traits to associated types (#534)
Browse files Browse the repository at this point in the history
  • Loading branch information
haerdib authored Apr 13, 2023
1 parent 7b96370 commit 6d09a9d
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 152 deletions.
55 changes: 29 additions & 26 deletions src/api/rpc_api/author.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,16 @@ where
}
}

pub trait SubmitAndWatch<Client, Hash>
where
Client: Subscribe,
Hash: DeserializeOwned,
{
pub trait SubmitAndWatch {
type Client: Subscribe;
type Hash: DeserializeOwned;

/// Submit an extrinsic an return a Subscription
/// to watch the extrinsic progress.
fn submit_and_watch_extrinsic<Address, Call, Signature, SignedExtra>(
&self,
extrinsic: UncheckedExtrinsicV4<Address, Call, Signature, SignedExtra>,
) -> Result<TransactionSubscriptionFor<Client, Hash>>
) -> Result<TransactionSubscriptionFor<Self::Client, Self::Hash>>
where
Address: Encode,
Call: Encode,
Expand All @@ -100,7 +99,7 @@ where
fn submit_and_watch_opaque_extrinsic(
&self,
encoded_extrinsic: Bytes,
) -> Result<TransactionSubscriptionFor<Client, Hash>>;
) -> Result<TransactionSubscriptionFor<Self::Client, Self::Hash>>;

/// Submit an extrinsic and watch it until the desired status
/// is reached, if no error is encountered previously.
Expand All @@ -114,7 +113,7 @@ where
&self,
extrinsic: UncheckedExtrinsicV4<Address, Call, Signature, SignedExtra>,
watch_until: XtStatus,
) -> Result<ExtrinsicReport<Hash>>
) -> Result<ExtrinsicReport<Self::Hash>>
where
Address: Encode,
Call: Encode,
Expand All @@ -133,14 +132,13 @@ where
&self,
encoded_extrinsic: Bytes,
watch_until: XtStatus,
) -> Result<ExtrinsicReport<Hash>>;
) -> Result<ExtrinsicReport<Self::Hash>>;
}

pub trait SubmitAndWatchUntilSuccess<Client, Hash>
where
Client: Subscribe,
Hash: DeserializeOwned,
{
pub trait SubmitAndWatchUntilSuccess {
type Client: Subscribe;
type Hash;

/// Submit an extrinsic and watch it until
/// - wait_for_finalized = false => InBlock
/// - wait_for_finalized = true => Finalized
Expand All @@ -155,7 +153,7 @@ where
&self,
extrinsic: UncheckedExtrinsicV4<Address, Call, Signature, SignedExtra>,
wait_for_finalized: bool,
) -> Result<ExtrinsicReport<Hash>>
) -> Result<ExtrinsicReport<Self::Hash>>
where
Address: Encode,
Call: Encode,
Expand All @@ -176,21 +174,23 @@ where
&self,
encoded_extrinsic: Bytes,
wait_for_finalized: bool,
) -> Result<ExtrinsicReport<Hash>>;
) -> Result<ExtrinsicReport<Self::Hash>>;
}

impl<Signer, Client, Params, Runtime> SubmitAndWatch<Client, Runtime::Hash>
for Api<Signer, Client, Params, Runtime>
impl<Signer, Client, Params, Runtime> SubmitAndWatch for Api<Signer, Client, Params, Runtime>
where
Client: Subscribe,
Params: ExtrinsicParams<Runtime::Index, Runtime::Hash>,
Runtime: FrameSystemConfig,
Runtime::Hashing: HashTrait<Output = Runtime::Hash>,
{
type Client = Client;
type Hash = Runtime::Hash;

fn submit_and_watch_extrinsic<Address, Call, Signature, SignedExtra>(
&self,
extrinsic: UncheckedExtrinsicV4<Address, Call, Signature, SignedExtra>,
) -> Result<TransactionSubscriptionFor<Client, Runtime::Hash>>
) -> Result<TransactionSubscriptionFor<Self::Client, Self::Hash>>
where
Address: Encode,
Call: Encode,
Expand All @@ -202,7 +202,7 @@ where
fn submit_and_watch_opaque_extrinsic(
&self,
encoded_extrinsic: Bytes,
) -> Result<TransactionSubscriptionFor<Client, Runtime::Hash>> {
) -> Result<TransactionSubscriptionFor<Self::Client, Self::Hash>> {
self.client()
.subscribe(
"author_submitAndWatchExtrinsic",
Expand All @@ -216,7 +216,7 @@ where
&self,
extrinsic: UncheckedExtrinsicV4<Address, Call, Signature, SignedExtra>,
watch_until: XtStatus,
) -> Result<ExtrinsicReport<Runtime::Hash>>
) -> Result<ExtrinsicReport<Self::Hash>>
where
Address: Encode,
Call: Encode,
Expand All @@ -230,9 +230,9 @@ where
&self,
encoded_extrinsic: Bytes,
watch_until: XtStatus,
) -> Result<ExtrinsicReport<Runtime::Hash>> {
) -> Result<ExtrinsicReport<Self::Hash>> {
let tx_hash = Runtime::Hashing::hash_of(&encoded_extrinsic.0);
let mut subscription: TransactionSubscriptionFor<Client, Runtime::Hash> =
let mut subscription: TransactionSubscriptionFor<Self::Client, Self::Hash> =
self.submit_and_watch_opaque_extrinsic(encoded_extrinsic)?;

while let Some(transaction_status) = subscription.next() {
Expand All @@ -259,7 +259,7 @@ where
}
}

impl<Signer, Client, Params, Runtime> SubmitAndWatchUntilSuccess<Client, Runtime::Hash>
impl<Signer, Client, Params, Runtime> SubmitAndWatchUntilSuccess
for Api<Signer, Client, Params, Runtime>
where
Client: Subscribe + Request,
Expand All @@ -268,11 +268,14 @@ where
Runtime::RuntimeBlock: BlockTrait + DeserializeOwned,
Runtime::Hashing: HashTrait<Output = Runtime::Hash>,
{
type Client = Client;
type Hash = Runtime::Hash;

fn submit_and_watch_extrinsic_until_success<Address, Call, Signature, SignedExtra>(
&self,
extrinsic: UncheckedExtrinsicV4<Address, Call, Signature, SignedExtra>,
wait_for_finalized: bool,
) -> Result<ExtrinsicReport<Runtime::Hash>>
) -> Result<ExtrinsicReport<Self::Hash>>
where
Address: Encode,
Call: Encode,
Expand All @@ -289,7 +292,7 @@ where
&self,
encoded_extrinsic: Bytes,
wait_for_finalized: bool,
) -> Result<ExtrinsicReport<Runtime::Hash>> {
) -> Result<ExtrinsicReport<Self::Hash>> {
let xt_status = match wait_for_finalized {
true => XtStatus::Finalized,
false => XtStatus::InBlock,
Expand Down
72 changes: 37 additions & 35 deletions src/api/rpc_api/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,122 +21,124 @@ use log::*;
use serde::de::DeserializeOwned;
use sp_runtime::traits::GetRuntimeBlockType;

pub trait GetHeader<Hash> {
pub trait GetHeader {
type Hash;
type Header;

fn get_finalized_head(&self) -> Result<Option<Hash>>;
fn get_finalized_head(&self) -> Result<Option<Self::Hash>>;

fn get_header(&self, hash: Option<Hash>) -> Result<Option<Self::Header>>;
fn get_header(&self, hash: Option<Self::Hash>) -> Result<Option<Self::Header>>;
}

impl<Signer, Client, Params, Runtime> GetHeader<Runtime::Hash>
for Api<Signer, Client, Params, Runtime>
impl<Signer, Client, Params, Runtime> GetHeader for Api<Signer, Client, Params, Runtime>
where
Client: Request,
Runtime: FrameSystemConfig,
Params: ExtrinsicParams<Runtime::Index, Runtime::Hash>,
Runtime::Header: DeserializeOwned,
{
type Hash = Runtime::Hash;
type Header = Runtime::Header;

fn get_finalized_head(&self) -> Result<Option<Runtime::Hash>> {
fn get_finalized_head(&self) -> Result<Option<Self::Hash>> {
let finalized_block_hash =
self.client().request("chain_getFinalizedHead", rpc_params![])?;
Ok(finalized_block_hash)
}

fn get_header(&self, hash: Option<Runtime::Hash>) -> Result<Option<Runtime::Header>> {
fn get_header(&self, hash: Option<Self::Hash>) -> Result<Option<Self::Header>> {
let block_hash = self.client().request("chain_getHeader", rpc_params![hash])?;
Ok(block_hash)
}
}

pub trait GetBlock<Number, Hash> {
pub trait GetBlock {
type BlockNumber;
type Hash;
type Block;

fn get_block_hash(&self, number: Option<Number>) -> Result<Option<Hash>>;
fn get_block_hash(&self, number: Option<Self::BlockNumber>) -> Result<Option<Self::Hash>>;

fn get_block(&self, hash: Option<Hash>) -> Result<Option<Self::Block>>;
fn get_block(&self, hash: Option<Self::Hash>) -> Result<Option<Self::Block>>;

fn get_block_by_num(&self, number: Option<Number>) -> Result<Option<Self::Block>>;
fn get_block_by_num(&self, number: Option<Self::BlockNumber>) -> Result<Option<Self::Block>>;

/// A signed block is a block with Justification ,i.e., a Grandpa finality proof.
/// The interval at which finality proofs are provided is set via the
/// the `GrandpaConfig.justification_period` in a node's service.rs.
/// The Justification may be None.
fn get_signed_block(&self, hash: Option<Hash>) -> Result<Option<SignedBlock<Self::Block>>>;
fn get_signed_block(
&self,
hash: Option<Self::Hash>,
) -> Result<Option<SignedBlock<Self::Block>>>;

fn get_signed_block_by_num(
&self,
number: Option<Number>,
number: Option<Self::BlockNumber>,
) -> Result<Option<SignedBlock<Self::Block>>>;
}

impl<Signer, Client, Params, Runtime> GetBlock<Runtime::BlockNumber, Runtime::Hash>
for Api<Signer, Client, Params, Runtime>
impl<Signer, Client, Params, Runtime> GetBlock for Api<Signer, Client, Params, Runtime>
where
Client: Request,
Runtime: FrameSystemConfig + GetRuntimeBlockType,
Params: ExtrinsicParams<Runtime::Index, Runtime::Hash>,
Runtime::RuntimeBlock: DeserializeOwned,
{
type BlockNumber = Runtime::BlockNumber;
type Hash = Runtime::Hash;
type Block = Runtime::RuntimeBlock;

fn get_block_hash(
&self,
number: Option<Runtime::BlockNumber>,
) -> Result<Option<Runtime::Hash>> {
fn get_block_hash(&self, number: Option<Self::BlockNumber>) -> Result<Option<Self::Hash>> {
let block_hash = self.client().request("chain_getBlockHash", rpc_params![number])?;
Ok(block_hash)
}

fn get_block(&self, hash: Option<Runtime::Hash>) -> Result<Option<Self::Block>> {
fn get_block(&self, hash: Option<Self::Hash>) -> Result<Option<Self::Block>> {
Self::get_signed_block(self, hash).map(|sb_opt| sb_opt.map(|sb| sb.block))
}

fn get_block_by_num(
&self,
number: Option<Runtime::BlockNumber>,
) -> Result<Option<Self::Block>> {
fn get_block_by_num(&self, number: Option<Self::BlockNumber>) -> Result<Option<Self::Block>> {
Self::get_signed_block_by_num(self, number).map(|sb_opt| sb_opt.map(|sb| sb.block))
}

fn get_signed_block(
&self,
hash: Option<Runtime::Hash>,
hash: Option<Self::Hash>,
) -> Result<Option<SignedBlock<Self::Block>>> {
let block = self.client().request("chain_getBlock", rpc_params![hash])?;
Ok(block)
}

fn get_signed_block_by_num(
&self,
number: Option<Runtime::BlockNumber>,
number: Option<Self::BlockNumber>,
) -> Result<Option<SignedBlock<Self::Block>>> {
self.get_block_hash(number).map(|h| self.get_signed_block(h))?
}
}
pub trait SubscribeChain<Client, Hash>
where
Client: Subscribe,
Hash: DeserializeOwned,
{
pub trait SubscribeChain {
type Client: Subscribe;
type Header: DeserializeOwned;

fn subscribe_finalized_heads(&self) -> Result<Client::Subscription<Self::Header>>;
fn subscribe_finalized_heads(
&self,
) -> Result<<Self::Client as Subscribe>::Subscription<Self::Header>>;
}

impl<Signer, Client, Params, Runtime> SubscribeChain<Client, Runtime::Hash>
for Api<Signer, Client, Params, Runtime>
impl<Signer, Client, Params, Runtime> SubscribeChain for Api<Signer, Client, Params, Runtime>
where
Client: Subscribe,
Params: ExtrinsicParams<Runtime::Index, Runtime::Hash>,
Runtime: FrameSystemConfig,
Runtime::Header: DeserializeOwned,
{
type Client = Client;
type Header = Runtime::Header;

fn subscribe_finalized_heads(&self) -> Result<Client::Subscription<Self::Header>> {
fn subscribe_finalized_heads(
&self,
) -> Result<<Self::Client as Subscribe>::Subscription<Self::Header>> {
debug!("subscribing to finalized heads");
self.client()
.subscribe(
Expand Down
Loading

0 comments on commit 6d09a9d

Please sign in to comment.