Skip to content

Commit

Permalink
remove hex utils
Browse files Browse the repository at this point in the history
remove utils

fix build

fix rebase
  • Loading branch information
haerdib committed Jan 9, 2023
1 parent bf42f66 commit a95bf01
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 177 deletions.
19 changes: 5 additions & 14 deletions src/api/api_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use crate::{
api::error::{Error, Result},
rpc::Request,
utils::FromHexString,
GetAccountInformation,
};
use ac_compose_macros::rpc_params;
Expand All @@ -35,7 +34,7 @@ use sp_runtime::MultiSignature;
///
/// ```no_run
/// use substrate_api_client::{
/// Api, FromHexString, rpc::Request, rpc::Error as RpcClientError, XtStatus, PlainTipExtrinsicParams, rpc::Result as RpcResult
/// Api, rpc::Request, rpc::Error as RpcClientError, XtStatus, PlainTipExtrinsicParams, rpc::Result as RpcResult
/// };
/// use serde::de::DeserializeOwned;
/// use ac_primitives::RpcParams;
Expand Down Expand Up @@ -178,7 +177,6 @@ where
Client: Request,
Params: ExtrinsicParams<Runtime::Index, Runtime::Hash>,
Runtime: FrameSystemConfig,
Runtime::Hash: FromHexString,
{
/// Create a new Api client with call to the node to retrieve metadata.
pub fn new(client: Client) -> Result<Self> {
Expand Down Expand Up @@ -239,7 +237,6 @@ where
Client: Request,
Params: ExtrinsicParams<Runtime::Index, Runtime::Hash>,
Runtime: FrameSystemConfig,
Runtime::Hash: FromHexString,
{
/// Get the genesis hash from node via websocket query.
fn get_genesis_hash(client: &Client) -> Result<Runtime::Hash> {
Expand All @@ -265,10 +262,7 @@ where
#[cfg(test)]
mod tests {
use super::*;
use crate::{
rpc::mocks::RpcClientMock, utils::ToHexString, GenericAdditionalParams,
PlainTipExtrinsicParams,
};
use crate::{rpc::mocks::RpcClientMock, GenericAdditionalParams, PlainTipExtrinsicParams};
use kitchensink_runtime::Runtime;
use sp_core::{sr25519::Pair, H256};
use std::{
Expand Down Expand Up @@ -321,9 +315,9 @@ mod tests {
fn api_runtime_update_works() {
let runtime_version = RuntimeVersion { spec_version: 10, ..Default::default() };
// Update metadata
let encoded_metadata = fs::read("./ksm_metadata_v14.bin").unwrap();
let encoded_metadata: Bytes = fs::read("./ksm_metadata_v14.bin").unwrap().into();
let metadata: RuntimeMetadataPrefixed =
Decode::decode(&mut encoded_metadata.as_slice()).unwrap();
Decode::decode(&mut encoded_metadata.0.as_slice()).unwrap();
let metadata = Metadata::try_from(metadata).unwrap();

let mut changed_metadata = metadata.clone();
Expand All @@ -338,10 +332,7 @@ mod tests {
"state_getRuntimeVersion".to_owned(),
serde_json::to_string(&runtime_version).unwrap(),
),
(
"state_getMetadata".to_owned(),
serde_json::to_string(&encoded_metadata.to_hex()).unwrap(),
),
("state_getMetadata".to_owned(), serde_json::to_string(&encoded_metadata).unwrap()),
]);
let mut api =
create_mock_api(Default::default(), Default::default(), changed_metadata, data);
Expand Down
70 changes: 32 additions & 38 deletions src/api/rpc_api/author.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
use crate::{
api::{Error, Result},
rpc::{HandleSubscription, Request, Subscribe},
utils, Api, Events, ExtrinsicReport, FromHexString, GetBlock, GetStorage, Phase, ToHexString,
TransactionStatus, UncheckedExtrinsicV4, XtStatus,
Api, Events, ExtrinsicReport, GetBlock, GetStorage, Phase, TransactionStatus,
UncheckedExtrinsicV4, XtStatus,
};
use ac_compose_macros::rpc_params;
use ac_node_api::EventDetails;
use ac_primitives::{ExtrinsicParams, FrameSystemConfig};
use alloc::vec::Vec;
use ac_primitives::{Bytes, ExtrinsicParams, FrameSystemConfig};
use alloc::{format, string::ToString, vec::Vec};
use codec::Encode;
use log::*;
use serde::de::DeserializeOwned;
Expand All @@ -47,7 +47,7 @@ pub trait SubmitExtrinsic {

/// Submit an encoded, opaque extrsinic to the substrate node.
/// Returns the extrinsic hash.
fn submit_opaque_extrinsic(&self, encoded_extrinsic: Vec<u8>) -> Result<Self::Hash>;
fn submit_opaque_extrinsic(&self, encoded_extrinsic: Bytes) -> Result<Self::Hash>;
}

impl<Signer, Client, Params, Runtime> SubmitExtrinsic for Api<Signer, Client, Params, Runtime>
Expand All @@ -66,14 +66,13 @@ where
Call: Encode,
SignedExtra: Encode,
{
self.submit_opaque_extrinsic(extrinsic.encode())
self.submit_opaque_extrinsic(extrinsic.encode().into())
}

fn submit_opaque_extrinsic(&self, encoded_extrinsic: Vec<u8>) -> Result<Self::Hash> {
let hex_encoded_xt = encoded_extrinsic.to_hex();
fn submit_opaque_extrinsic(&self, encoded_extrinsic: Bytes) -> Result<Self::Hash> {
let hex_encoded_xt = rpc_params![encoded_extrinsic];
debug!("sending extrinsic: {:?}", hex_encoded_xt);
let xt_hash =
self.client().request("author_submitExtrinsic", rpc_params![hex_encoded_xt])?;
let xt_hash = self.client().request("author_submitExtrinsic", hex_encoded_xt)?;
Ok(xt_hash)
}
}
Expand All @@ -97,7 +96,7 @@ where
/// watch the extrinsic progress.
fn submit_and_watch_opaque_extrinsic(
&self,
encoded_extrinsic: Vec<u8>,
encoded_extrinsic: Bytes,
) -> Result<TransactionSubscriptionFor<Client, Hash>>;

/// Submit an extrinsic and watch in until the desired status
Expand All @@ -117,7 +116,7 @@ where
// This method is blocking.
fn submit_and_watch_opaque_extrinsic_until(
&self,
encoded_extrinsic: Vec<u8>,
encoded_extrinsic: Bytes,
watch_until: XtStatus,
) -> Result<ExtrinsicReport<Hash>>;
}
Expand Down Expand Up @@ -148,7 +147,7 @@ where
// This method is blocking.
fn submit_and_watch_opaque_extrinsic_until_success(
&self,
encoded_extrinsic: Vec<u8>,
encoded_extrinsic: Bytes,
wait_for_finalized: bool,
) -> Result<ExtrinsicReport<Hash>>;
}
Expand All @@ -160,7 +159,6 @@ where
Params: ExtrinsicParams<Runtime::Index, Runtime::Hash>,
Runtime: FrameSystemConfig,
Runtime::Hashing: HashTrait<Output = Runtime::Hash>,
Runtime::Hash: FromHexString,
{
fn submit_and_watch_extrinsic<Call, SignedExtra>(
&self,
Expand All @@ -170,16 +168,16 @@ where
Call: Encode,
SignedExtra: Encode,
{
self.submit_and_watch_opaque_extrinsic(extrinsic.encode())
self.submit_and_watch_opaque_extrinsic(extrinsic.encode().into())
}
fn submit_and_watch_opaque_extrinsic(
&self,
encoded_extrinsic: Vec<u8>,
encoded_extrinsic: Bytes,
) -> Result<TransactionSubscriptionFor<Client, Runtime::Hash>> {
self.client()
.subscribe(
"author_submitAndWatchExtrinsic",
rpc_params![encoded_extrinsic.to_hex()],
rpc_params![encoded_extrinsic],
"author_unsubmitAndWatchExtrinsic",
)
.map_err(|e| e.into())
Expand All @@ -194,15 +192,15 @@ where
Call: Encode,
SignedExtra: Encode,
{
self.submit_and_watch_opaque_extrinsic_until(extrinsic.encode(), watch_until)
self.submit_and_watch_opaque_extrinsic_until(extrinsic.encode().into(), watch_until)
}

fn submit_and_watch_opaque_extrinsic_until(
&self,
encoded_extrinsic: Vec<u8>,
encoded_extrinsic: Bytes,
watch_until: XtStatus,
) -> Result<ExtrinsicReport<Runtime::Hash>> {
let tx_hash = Runtime::Hashing::hash_of(&encoded_extrinsic);
let tx_hash = Runtime::Hashing::hash_of(&encoded_extrinsic.0);
let mut subscription: TransactionSubscriptionFor<Client, Runtime::Hash> =
self.submit_and_watch_opaque_extrinsic(encoded_extrinsic)?;

Expand Down Expand Up @@ -238,7 +236,6 @@ where
Runtime: FrameSystemConfig + GetRuntimeBlockType,
Runtime::RuntimeBlock: BlockTrait + DeserializeOwned,
Runtime::Hashing: HashTrait<Output = Runtime::Hash>,
Runtime::Hash: FromHexString,
{
fn submit_and_watch_extrinsic_until_success<Call, SignedExtra>(
&self,
Expand All @@ -249,12 +246,15 @@ where
Call: Encode,
SignedExtra: Encode,
{
self.submit_and_watch_opaque_extrinsic_until_success(extrinsic.encode(), wait_for_finalized)
self.submit_and_watch_opaque_extrinsic_until_success(
extrinsic.encode().into(),
wait_for_finalized,
)
}

fn submit_and_watch_opaque_extrinsic_until_success(
&self,
encoded_extrinsic: Vec<u8>,
encoded_extrinsic: Bytes,
wait_for_finalized: bool,
) -> Result<ExtrinsicReport<Runtime::Hash>> {
let xt_status = match wait_for_finalized {
Expand Down Expand Up @@ -282,7 +282,6 @@ where
Runtime: FrameSystemConfig + GetRuntimeBlockType,
Runtime::RuntimeBlock: BlockTrait + DeserializeOwned,
Runtime::Hashing: HashTrait<Output = Runtime::Hash>,
Runtime::Hash: FromHexString,
{
/// Retrieve block details from node and search for the position of the given extrinsic.
fn retrieve_extrinsic_index_from_block(
Expand All @@ -305,7 +304,7 @@ where

/// Fetch all block events from node for the given block hash.
fn fetch_events_from_block(&self, block_hash: Runtime::Hash) -> Result<Events<Runtime::Hash>> {
let key = utils::storage_key("System", "Events");
let key = crate::storage_key("System", "Events");
let event_bytes = self
.get_opaque_storage_by_key_hash(key, Some(block_hash))?
.ok_or(Error::BlockNotFound)?;
Expand Down Expand Up @@ -344,7 +343,7 @@ mod tests {
use super::*;
use crate::{rpc::mocks::RpcClientMock, AssetTipExtrinsicParams, StorageData};
use ac_node_api::{metadata::Metadata, test_utils::*};
use ac_primitives::{FrameSystemConfig, RuntimeVersion, SignedBlock};
use ac_primitives::{Bytes, FrameSystemConfig, RuntimeVersion, SignedBlock};
use codec::{Decode, Encode};
use frame_metadata::RuntimeMetadataPrefixed;
use kitchensink_runtime::{BalancesCall, Runtime, RuntimeCall, UncheckedExtrinsic};
Expand Down Expand Up @@ -478,20 +477,15 @@ mod tests {
RuntimeCall::Balances(BalancesCall::transfer { dest: bob.clone(), value: 2000 });
let call3 = RuntimeCall::Balances(BalancesCall::transfer { dest: bob, value: 1000 });

let xt1 = UncheckedExtrinsic::new_unsigned(call1).encode();
let xt2 = UncheckedExtrinsic::new_unsigned(call2).encode();
let xt3 = UncheckedExtrinsic::new_unsigned(call3).encode();
let xt1: Bytes = UncheckedExtrinsic::new_unsigned(call1).encode().into();
let xt2: Bytes = UncheckedExtrinsic::new_unsigned(call2).encode().into();
let xt3: Bytes = UncheckedExtrinsic::new_unsigned(call3).encode().into();

let xt_hash1 = <Runtime as FrameSystemConfig>::Hashing::hash_of(&xt1);
let xt_hash2 = <Runtime as FrameSystemConfig>::Hashing::hash_of(&xt2);
let xt_hash3 = <Runtime as FrameSystemConfig>::Hashing::hash_of(&xt3);
let xt_hash1 = <Runtime as FrameSystemConfig>::Hashing::hash_of(&xt1.0);
let xt_hash2 = <Runtime as FrameSystemConfig>::Hashing::hash_of(&xt2.0);
let xt_hash3 = <Runtime as FrameSystemConfig>::Hashing::hash_of(&xt3.0);

// We have to create a Block with hex encoded extrinsic for serialization. Otherwise the deserialization will fail.
// e.g. UncheckedExtrinsic.serialize and UncheckedExtrinsic::deserialize does not work.
let block = Block {
header: default_header(),
extrinsics: vec![xt1.to_hex(), xt2.to_hex(), xt3.to_hex()],
};
let block = Block { header: default_header(), extrinsics: vec![xt1, xt2, xt3] };
let signed_block = SignedBlock { block, justifications: None };
let data = HashMap::<String, String>::from([(
"chain_getBlock".to_owned(),
Expand Down
3 changes: 0 additions & 3 deletions src/api/rpc_api/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use crate::{
api::{Api, Result},
rpc::{Request, Subscribe},
FromHexString,
};
use ac_compose_macros::rpc_params;
use ac_primitives::{ExtrinsicParams, FrameSystemConfig, SignedBlock};
Expand All @@ -37,7 +36,6 @@ where
Runtime: FrameSystemConfig,
Params: ExtrinsicParams<Runtime::Index, Runtime::Hash>,
Runtime::Header: DeserializeOwned,
Runtime::Hash: FromHexString,
{
type Header = Runtime::Header;

Expand Down Expand Up @@ -81,7 +79,6 @@ where
Runtime: FrameSystemConfig + GetRuntimeBlockType,
Params: ExtrinsicParams<Runtime::Index, Runtime::Hash>,
Runtime::RuntimeBlock: DeserializeOwned,
Runtime::Hash: FromHexString,
{
type Block = Runtime::RuntimeBlock;

Expand Down
3 changes: 1 addition & 2 deletions src/api/rpc_api/frame_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use crate::{
api::{Api, GetStorage, Result},
rpc::{Request, Subscribe},
utils,
};
use ac_compose_macros::rpc_params;
use ac_primitives::{
Expand Down Expand Up @@ -182,7 +181,7 @@ where
&self,
) -> Result<Client::Subscription<StorageChangeSet<Runtime::Hash>>> {
debug!("subscribing to events");
let key = utils::storage_key("System", "Events");
let key = crate::storage_key("System", "Events");
self.client()
.subscribe("state_subscribeStorage", rpc_params![vec![key]], "state_unsubscribeStorage")
.map_err(|e| e.into())
Expand Down
24 changes: 12 additions & 12 deletions src/api/rpc_api/pallet_transaction_payment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,27 @@
use crate::{
api::{Api, Error, Result},
rpc::Request,
utils::ToHexString,
ExtrinsicParams,
};
use ac_compose_macros::rpc_params;
use ac_primitives::{BalancesConfig, FeeDetails, InclusionFee, NumberOrHex, RuntimeDispatchInfo};
use alloc::vec::Vec;
use ac_primitives::{
BalancesConfig, Bytes, FeeDetails, InclusionFee, NumberOrHex, RuntimeDispatchInfo,
};
use core::str::FromStr;

/// Interface to common calls of the substrate transaction payment pallet.
pub trait GetTransactionPayment<Hash> {
type Balance;

fn get_fee_details(
&self,
encoded_extrinsic: Vec<u8>,
encoded_extrinsic: Bytes,
at_block: Option<Hash>,
) -> Result<Option<FeeDetails<Self::Balance>>>;

fn get_payment_info(
&self,
encoded_extrinsic: Vec<u8>,
encoded_extrinsic: Bytes,
at_block: Option<Hash>,
) -> Result<Option<RuntimeDispatchInfo<Self::Balance>>>;
}
Expand All @@ -49,13 +50,12 @@ where

fn get_fee_details(
&self,
encoded_extrinsic: Vec<u8>,
encoded_extrinsic: Bytes,
at_block: Option<Runtime::Hash>,
) -> Result<Option<FeeDetails<Self::Balance>>> {
let details: Option<FeeDetails<NumberOrHex>> = self.client().request(
"payment_queryFeeDetails",
rpc_params![encoded_extrinsic.to_hex(), at_block],
)?;
let details: Option<FeeDetails<NumberOrHex>> = self
.client()
.request("payment_queryFeeDetails", rpc_params![encoded_extrinsic, at_block])?;

let details = match details {
Some(details) => Some(convert_fee_details(details)?),
Expand All @@ -66,12 +66,12 @@ where

fn get_payment_info(
&self,
encoded_extrinsic: Vec<u8>,
encoded_extrinsic: Bytes,
at_block: Option<Runtime::Hash>,
) -> Result<Option<RuntimeDispatchInfo<Self::Balance>>> {
let res = self
.client()
.request("payment_queryInfo", rpc_params![encoded_extrinsic.to_hex(), at_block])?;
.request("payment_queryInfo", rpc_params![encoded_extrinsic, at_block])?;
Ok(res)
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/api/rpc_api/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use crate::{
api::Result,
rpc::{Request, Subscribe},
utils, Api, MetadataError, ReadProof,
Api, MetadataError, ReadProof,
};
use ac_compose_macros::rpc_params;
use ac_primitives::{
Expand Down Expand Up @@ -306,7 +306,7 @@ where
storage_key_name: &str,
) -> Result<Client::Subscription<StorageChangeSet<Runtime::Hash>>> {
debug!("subscribing to events");
let key = utils::storage_key(pallet, storage_key_name);
let key = crate::storage_key(pallet, storage_key_name);
self.client()
.subscribe("state_subscribeStorage", rpc_params![vec![key]], "state_unsubscribeStorage")
.map_err(|e| e.into())
Expand Down
Loading

0 comments on commit a95bf01

Please sign in to comment.