Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make primitives generic over Index, Hash and Balance #334

Merged
merged 6 commits into from
Nov 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 2 additions & 8 deletions examples/example_batch_payout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ use sp_keyring::AccountKeyring;
#[cfg(feature = "staking-xt")]
use sp_runtime::{app_crypto::Ss58Codec, AccountId32};
#[cfg(feature = "staking-xt")]
use substrate_api_client::{
rpc::WsRpcClient, Api, BaseExtrinsicParams, PlainTip, PlainTipExtrinsicParams, XtStatus,
};
use substrate_api_client::{rpc::WsRpcClient, Api, PlainTipExtrinsicParams, XtStatus};

#[cfg(feature = "staking-xt")]
fn main() {
Expand Down Expand Up @@ -104,11 +102,7 @@ pub fn get_node_url_from_cli() -> String {
#[cfg(feature = "staking-xt")]
pub fn get_last_reward(
validator_address: &str,
api: &substrate_api_client::Api<
sp_core::sr25519::Pair,
WsRpcClient,
BaseExtrinsicParams<PlainTip>,
>,
api: &substrate_api_client::Api<sp_core::sr25519::Pair, WsRpcClient, PlainTipExtrinsicParams>,
) -> u32 {
let api = api;
let account = match AccountId32::from_ss58check(validator_address) {
Expand Down
8 changes: 4 additions & 4 deletions primitives/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ac-primitives"
version = "0.2.0"
version = "0.2.1"
authors = ["Supercomputing Systems AG <[email protected]>"]
license = "Apache-2.0"
edition = "2021"
Expand All @@ -12,9 +12,9 @@ hex = { version = "0.4.3", default-features = false, features = ["alloc"] }
# substrate
frame-system = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "master" }
pallet-balances = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "master" }
sp-core = { version = "6.0.0", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "master" }
sp-runtime = { version = "6.0.0", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "master" }
sp-std = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "master" }
sp-core = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "master" }
sp-runtime = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "master" }
sp-std = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "master" }

[features]
default = ["std"]
Expand Down
111 changes: 66 additions & 45 deletions primitives/src/extrinsic_params.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,55 @@
/*
Copyright 2019 Supercomputing Systems AG

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

*/

use crate::Balance;
use codec::{Decode, Encode};
use core::marker::PhantomData;
use sp_core::H256;
use sp_runtime::{
generic::Era,
traits::{BlakeTwo256, Hash},
};
use sp_std::prelude::*;
use sp_std::{hash::Hash as HashTrait, prelude::*};

/// Default SignedExtra.
/// Simple generic extra mirroring the SignedExtra currently used in extrinsics.
#[derive(Decode, Encode, Copy, Clone, Eq, PartialEq, Debug)]
pub struct SubstrateDefaultSignedExtra<Tip> {
pub struct SubstrateDefaultSignedExtra<Tip, Index> {
pub era: Era,
#[codec(compact)]
pub nonce: u32,
pub nonce: Index,
pub tip: Tip,
}

impl<Tip> SubstrateDefaultSignedExtra<Tip> {
pub fn new(era: Era, nonce: u32, tip: Tip) -> Self {
impl<Tip, Index> SubstrateDefaultSignedExtra<Tip, Index> {
pub fn new(era: Era, nonce: Index, tip: Tip) -> Self {
Self { era, nonce, tip }
}
}

/// Default AdditionalSigned fields of the respective SignedExtra fields.
/// The Order is (CheckNonZeroSender, CheckSpecVersion, CheckTxVersion, CheckGenesis, Check::Era, CheckNonce, CheckWeight, transactionPayment::ChargeTransactionPayment).
pub type SubstrateDefaultAdditionalSigned = ((), u32, u32, H256, H256, (), (), ());
pub type SubstrateDefaultAdditionalSigned<Hash> = ((), u32, u32, Hash, Hash, (), (), ());

/// This trait allows you to configure the "signed extra" and
/// "additional" parameters that are signed and used in transactions.
/// see [`BaseExtrinsicParams`] for an implementation that is compatible with
/// a Polkadot node.
pub trait ExtrinsicParams {
pub trait ExtrinsicParams<Index, Hash> {
/// These parameters can be provided to the constructor along with
/// some default parameters in order to help construct your [`ExtrinsicParams`] object.
type OtherParams: Default + Clone;
Expand All @@ -46,8 +64,8 @@ pub trait ExtrinsicParams {
fn new(
spec_version: u32,
transaction_version: u32,
nonce: u32,
genesis_hash: H256,
nonce: Index,
genesis_hash: Hash,
other_params: Self::OtherParams,
) -> Self;

Expand All @@ -63,43 +81,43 @@ pub trait ExtrinsicParams {

/// A struct representing the signed extra and additional parameters required
/// to construct a transaction and pay in asset fees
pub type AssetTipExtrinsicParams = BaseExtrinsicParams<AssetTip>;
pub type AssetTipExtrinsicParams = BaseExtrinsicParams<AssetTip<Balance>, u32, H256>;
/// A builder which leads to [`AssetTipExtrinsicParams`] being constructed.
/// This is what you provide to methods like `sign_and_submit()`.
pub type AssetTipExtrinsicParamsBuilder = BaseExtrinsicParamsBuilder<AssetTip>;
pub type AssetTipExtrinsicParamsBuilder = BaseExtrinsicParamsBuilder<AssetTip<Balance>, H256>;

/// A struct representing the signed extra and additional parameters required
/// to construct a transaction and pay in token fees
pub type PlainTipExtrinsicParams = BaseExtrinsicParams<PlainTip>;
pub type PlainTipExtrinsicParams = BaseExtrinsicParams<PlainTip<Balance>, u32, H256>;
/// A builder which leads to [`PlainTipExtrinsicParams`] being constructed.
/// This is what you provide to methods like `sign_and_submit()`.
pub type PlainTipExtrinsicParamsBuilder = BaseExtrinsicParamsBuilder<PlainTip>;
pub type PlainTipExtrinsicParamsBuilder = BaseExtrinsicParamsBuilder<PlainTip<Balance>, H256>;

/// An implementation of [`ExtrinsicParams`] that is suitable for constructing
/// extrinsics that can be sent to a node with the same signed extra and additional
/// parameters as a Polkadot/Substrate node.
#[derive(Decode, Encode, Clone, Eq, PartialEq, Debug)]
pub struct BaseExtrinsicParams<Tip> {
pub struct BaseExtrinsicParams<Tip, Index, Hash> {
era: Era,
nonce: u32,
nonce: Index,
tip: Tip,
spec_version: u32,
transaction_version: u32,
genesis_hash: H256,
mortality_checkpoint: H256,
genesis_hash: Hash,
mortality_checkpoint: Hash,
marker: PhantomData<()>,
}

/// This builder allows you to provide the parameters that can be configured in order to
/// construct a [`BaseExtrinsicParams`] value.
#[derive(Decode, Encode, Copy, Clone, Eq, PartialEq, Debug)]
pub struct BaseExtrinsicParamsBuilder<Tip> {
pub struct BaseExtrinsicParamsBuilder<Tip, Hash> {
era: Era,
mortality_checkpoint: Option<H256>,
mortality_checkpoint: Option<Hash>,
tip: Tip,
}

impl<Tip: Default> BaseExtrinsicParamsBuilder<Tip> {
impl<Tip: Default, Hash> BaseExtrinsicParamsBuilder<Tip, Hash> {
/// Instantiate the default set of [`BaseExtrinsicParamsBuilder`]
pub fn new() -> Self {
Self::default()
Expand All @@ -110,7 +128,7 @@ impl<Tip: Default> BaseExtrinsicParamsBuilder<Tip> {
/// of time). The second argument is the block hash after which the transaction
/// becomes valid, and must align with the era phase (see the [`Era::Mortal`] docs
/// for more detail on that).
pub fn era(mut self, era: Era, checkpoint: H256) -> Self {
pub fn era(mut self, era: Era, checkpoint: Hash) -> Self {
self.era = era;
self.mortality_checkpoint = Some(checkpoint);
self
Expand All @@ -124,26 +142,29 @@ impl<Tip: Default> BaseExtrinsicParamsBuilder<Tip> {
}
}

impl<Tip: Default> Default for BaseExtrinsicParamsBuilder<Tip> {
impl<Tip: Default, Hash> Default for BaseExtrinsicParamsBuilder<Tip, Hash> {
fn default() -> Self {
Self { era: Era::Immortal, mortality_checkpoint: None, tip: Tip::default() }
}
}

impl<Tip: Encode> ExtrinsicParams for BaseExtrinsicParams<Tip>
impl<Tip, Index, Hash> ExtrinsicParams<Index, Hash> for BaseExtrinsicParams<Tip, Index, Hash>
where
u128: From<Tip>,
Tip: Copy + Default,
Tip: Copy + Default + Encode,
Index: Copy + Default + Encode,
Hash: HashTrait + Encode + Copy,
SubstrateDefaultSignedExtra<Tip, Index>: Encode,
{
type OtherParams = BaseExtrinsicParamsBuilder<Tip>;
type SignedExtra = SubstrateDefaultSignedExtra<Tip>;
type AdditionalSigned = SubstrateDefaultAdditionalSigned;
type OtherParams = BaseExtrinsicParamsBuilder<Tip, Hash>;
type SignedExtra = SubstrateDefaultSignedExtra<Tip, Index>;
type AdditionalSigned = SubstrateDefaultAdditionalSigned<Hash>;

fn new(
spec_version: u32,
transaction_version: u32,
nonce: u32,
genesis_hash: H256,
nonce: Index,
genesis_hash: Hash,
other_params: Self::OtherParams,
) -> Self {
BaseExtrinsicParams {
Expand Down Expand Up @@ -207,41 +228,41 @@ where

/// A tip payment.
#[derive(Copy, Clone, Debug, Default, Decode, Encode, Eq, PartialEq)]
pub struct PlainTip {
pub struct PlainTip<Balance> {
#[codec(compact)]
tip: u128,
tip: Balance,
}

impl PlainTip {
impl<Balance> PlainTip<Balance> {
/// Create a new tip of the amount provided.
pub fn new(amount: u128) -> Self {
pub fn new(amount: Balance) -> Self {
PlainTip { tip: amount }
}
}

impl From<u128> for PlainTip {
fn from(n: u128) -> Self {
impl<Balance> From<Balance> for PlainTip<Balance> {
fn from(n: Balance) -> Self {
PlainTip::new(n)
}
}

impl From<PlainTip> for u128 {
fn from(tip: PlainTip) -> Self {
impl From<PlainTip<Balance>> for Balance {
fn from(tip: PlainTip<Balance>) -> Self {
tip.tip
}
}

/// A tip payment made in the form of a specific asset.
#[derive(Copy, Clone, Debug, Default, Decode, Encode, Eq, PartialEq)]
pub struct AssetTip {
pub struct AssetTip<Balance> {
#[codec(compact)]
tip: u128,
tip: Balance,
asset: Option<u32>,
}

impl AssetTip {
impl<Balance> AssetTip<Balance> {
/// Create a new tip of the amount provided.
pub fn new(amount: u128) -> Self {
pub fn new(amount: Balance) -> Self {
AssetTip { tip: amount, asset: None }
}

Expand All @@ -253,14 +274,14 @@ impl AssetTip {
}
}

impl From<u128> for AssetTip {
fn from(n: u128) -> Self {
impl<Balance> From<Balance> for AssetTip<Balance> {
fn from(n: Balance) -> Self {
AssetTip::new(n)
}
}

impl From<AssetTip> for u128 {
fn from(tip: AssetTip) -> Self {
impl From<AssetTip<Balance>> for Balance {
fn from(tip: AssetTip<Balance>) -> Self {
tip.tip
}
}
2 changes: 1 addition & 1 deletion primitives/src/extrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ mod tests {
let tx_params =
PlainTipExtrinsicParamsBuilder::new().era(Era::mortal(8, 0), Hash::from([0u8; 32]));

let default_extra = BaseExtrinsicParams::new(0, 0, 0, Hash::from([0u8; 32]), tx_params);
let default_extra = BaseExtrinsicParams::new(0, 0, 0u32, Hash::from([0u8; 32]), tx_params);
let xt = UncheckedExtrinsicV4::new_signed(
vec![1, 1, 1],
account.into(),
Expand Down
19 changes: 17 additions & 2 deletions primitives/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
/*
Copyright 2019 Supercomputing Systems AG

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

*/

#![cfg_attr(not(feature = "std"), no_std)]

pub use extrinsic_params::*;
Expand All @@ -11,12 +28,10 @@ pub type BlockNumber = u64;
/// The timestamp moment type used in this runtime.
pub type Moment = u64;
/// Index of a transaction.
//fixme: make generic
pub type Index = u32;

pub type Hash = sp_core::H256;

//fixme: make generic
pub type Balance = u128;

pub use frame_system::AccountInfo as GenericAccountInfo;
Expand Down
7 changes: 5 additions & 2 deletions src/extrinsic/balances.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@

//! Extrinsics for `pallet-balances`.

use crate::std::{Api, RpcClient};
use crate::{
std::{Api, RpcClient},
Hash, Index,
};
use ac_compose_macros::compose_extrinsic;
use ac_primitives::{Balance, CallIndex, ExtrinsicParams, GenericAddress, UncheckedExtrinsicV4};
use codec::Compact;
Expand All @@ -41,7 +44,7 @@ where
MultiSignature: From<P::Signature>,
MultiSigner: From<P::Public>,
Client: RpcClient,
Params: ExtrinsicParams,
Params: ExtrinsicParams<Index, Hash>,
{
pub fn balance_transfer(
&self,
Expand Down
7 changes: 5 additions & 2 deletions src/extrinsic/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
//! Extrinsics for `pallet-contract`.
//! Contracts module is community maintained and not CI tested, therefore it may not work as is.

use crate::std::{Api, RpcClient};
use crate::{
std::{Api, RpcClient},
Index,
};
use ac_compose_macros::compose_extrinsic;
use ac_primitives::{Balance, CallIndex, ExtrinsicParams, GenericAddress, UncheckedExtrinsicV4};
use codec::Compact;
Expand Down Expand Up @@ -61,7 +64,7 @@ where
MultiSignature: From<P::Signature>,
MultiSigner: From<P::Public>,
Client: RpcClient,
Params: ExtrinsicParams,
Params: ExtrinsicParams<Index, Hash>,
{
pub fn contract_put_code(
&self,
Expand Down
Loading