mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-27 01:07:57 +00:00
Extensible transactions (and tips) (#3102)
* Make extrinsics extensible. Also Remove old extrinsic types. * Rest of mockup. Add tips. * Fix some build issues * Runtiem builds :) * Substrate builds. * Fix a doc test * Compact encoding * Extract out the era logic into an extension * Weight Check signed extension. (#3115) * Weight signed extension. * Revert a bit + test for check era. * Update Cargo.toml * Update node/cli/src/factory_impl.rs * Update node/executor/src/lib.rs * Update node/executor/src/lib.rs * Don't use len for weight - use data. * Operational Transaction; second attempt (#3138) * working poc added. * some fixes. * Update doc. * Fix all tests + final logic. * more refactoring. * nits. * System block limit in bytes. * Silent the storage macro warnings. * More logic more tests. * Fix import. * Refactor names. * Fix build. * Update srml/balances/src/lib.rs * Final refactor. * Bump transaction version * Fix weight mult test. * Fix more tests and improve doc. * Bump. * Make some tests work again. * Fix subkey. * Remove todos + bump. * Ignore expensive test. * Bump.
This commit is contained in:
@@ -23,13 +23,10 @@ use rand::rngs::StdRng;
|
||||
|
||||
use parity_codec::Decode;
|
||||
use keyring::sr25519::Keyring;
|
||||
use node_primitives::Hash;
|
||||
use node_runtime::{Call, CheckedExtrinsic, UncheckedExtrinsic, BalancesCall};
|
||||
use primitives::sr25519;
|
||||
use primitives::crypto::Pair;
|
||||
use node_runtime::{Call, CheckedExtrinsic, UncheckedExtrinsic, SignedExtra, BalancesCall};
|
||||
use primitives::{sr25519, crypto::Pair};
|
||||
use parity_codec::Encode;
|
||||
use sr_primitives::generic::Era;
|
||||
use sr_primitives::traits::{Block as BlockT, Header as HeaderT};
|
||||
use sr_primitives::{generic::Era, traits::{Block as BlockT, Header as HeaderT, SignedExtension}};
|
||||
use substrate_service::ServiceFactory;
|
||||
use transaction_factory::RuntimeAdapter;
|
||||
use transaction_factory::modes::Mode;
|
||||
@@ -54,6 +51,17 @@ pub struct FactoryState<N> {
|
||||
|
||||
type Number = <<node_primitives::Block as BlockT>::Header as HeaderT>::Number;
|
||||
|
||||
impl<Number> FactoryState<Number> {
|
||||
fn build_extra(index: node_primitives::Index, phase: u64) -> node_runtime::SignedExtra {
|
||||
(
|
||||
system::CheckEra::from(Era::mortal(256, phase)),
|
||||
system::CheckNonce::from(index),
|
||||
system::CheckWeight::from(),
|
||||
balances::TakeFees::from(0)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl RuntimeAdapter for FactoryState<Number> {
|
||||
type AccountId = node_primitives::AccountId;
|
||||
type Balance = node_primitives::Balance;
|
||||
@@ -132,16 +140,14 @@ impl RuntimeAdapter for FactoryState<Number> {
|
||||
let phase = self.extract_phase(*prior_block_hash);
|
||||
|
||||
sign::<service::Factory, Self>(CheckedExtrinsic {
|
||||
signed: Some((sender.clone(), index)),
|
||||
signed: Some((sender.clone(), Self::build_extra(index, phase))),
|
||||
function: Call::Balances(
|
||||
BalancesCall::transfer(
|
||||
indices::address::Address::Id(
|
||||
destination.clone().into()
|
||||
),
|
||||
indices::address::Address::Id(destination.clone().into()),
|
||||
(*amount).into()
|
||||
)
|
||||
)
|
||||
}, key, &prior_block_hash, phase)
|
||||
}, key, (prior_block_hash.clone(), (), (), ()))
|
||||
}
|
||||
|
||||
fn inherent_extrinsics(&self) -> InherentData {
|
||||
@@ -229,13 +235,11 @@ fn gen_seed_bytes(seed: u64) -> [u8; 32] {
|
||||
fn sign<F: ServiceFactory, RA: RuntimeAdapter>(
|
||||
xt: CheckedExtrinsic,
|
||||
key: &sr25519::Pair,
|
||||
prior_block_hash: &Hash,
|
||||
phase: u64,
|
||||
additional_signed: <SignedExtra as SignedExtension>::AdditionalSigned,
|
||||
) -> <RA::Block as BlockT>::Extrinsic {
|
||||
let s = match xt.signed {
|
||||
Some((signed, index)) => {
|
||||
let era = Era::mortal(256, phase);
|
||||
let payload = (index.into(), xt.function, era, prior_block_hash);
|
||||
Some((signed, extra)) => {
|
||||
let payload = (xt.function, extra.clone(), additional_signed);
|
||||
let signature = payload.using_encoded(|b| {
|
||||
if b.len() > 256 {
|
||||
key.sign(&sr_io::blake2_256(b))
|
||||
@@ -244,8 +248,8 @@ fn sign<F: ServiceFactory, RA: RuntimeAdapter>(
|
||||
}
|
||||
}).into();
|
||||
UncheckedExtrinsic {
|
||||
signature: Some((indices::address::Address::Id(signed), signature, payload.0, era)),
|
||||
function: payload.1,
|
||||
signature: Some((indices::address::Address::Id(signed), signature, extra)),
|
||||
function: payload.0,
|
||||
}
|
||||
}
|
||||
None => UncheckedExtrinsic {
|
||||
|
||||
@@ -220,7 +220,7 @@ mod tests {
|
||||
use consensus_common::{Environment, Proposer, BlockImportParams, BlockOrigin, ForkChoiceStrategy};
|
||||
use node_primitives::DigestItem;
|
||||
use node_runtime::{BalancesCall, Call, CENTS, SECS_PER_BLOCK, UncheckedExtrinsic};
|
||||
use parity_codec::{Compact, Encode, Decode};
|
||||
use parity_codec::{Encode, Decode};
|
||||
use primitives::{
|
||||
crypto::Pair as CryptoPair, ed25519::Pair, blake2_256,
|
||||
sr25519::Public as AddressPublic, H256,
|
||||
@@ -358,19 +358,24 @@ mod tests {
|
||||
let signer = charlie.clone();
|
||||
|
||||
let function = Call::Balances(BalancesCall::transfer(to.into(), amount));
|
||||
let era = Era::immortal();
|
||||
let raw_payload = (Compact(index), function, era, genesis_hash);
|
||||
|
||||
let check_era = system::CheckEra::from(Era::Immortal);
|
||||
let check_nonce = system::CheckNonce::from(index);
|
||||
let check_weight = system::CheckWeight::from();
|
||||
let take_fees = balances::TakeFees::from(0);
|
||||
let extra = (check_era, check_nonce, check_weight, take_fees);
|
||||
|
||||
let raw_payload = (function, extra.clone(), genesis_hash);
|
||||
let signature = raw_payload.using_encoded(|payload| if payload.len() > 256 {
|
||||
signer.sign(&blake2_256(payload)[..])
|
||||
} else {
|
||||
signer.sign(payload)
|
||||
});
|
||||
let xt = UncheckedExtrinsic::new_signed(
|
||||
index,
|
||||
raw_payload.1,
|
||||
raw_payload.0,
|
||||
from.into(),
|
||||
signature.into(),
|
||||
era,
|
||||
extra,
|
||||
).encode();
|
||||
let v: Vec<u8> = Decode::decode(&mut xt.as_slice()).unwrap();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user