mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-12 17:01:09 +00:00
Weight annotation. (#3157)
* 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. * First draft of annotating weights. * Refactor weight to u64. * More refactoring and tests. * New convert for weight to fee * more tests. * remove merge redundancy. * Fix system test. * Bring back subkey stuff. * a few stress tests. * fix some of the grumbles. * Final nits. * Update srml/system/src/lib.rs Co-Authored-By: DemiMarie-parity <48690212+DemiMarie-parity@users.noreply.github.com> * Scale weights by 1000. * Bump. * Fix decl_storage test.
This commit is contained in:
committed by
Bastian Köcher
parent
80472956f8
commit
002acb9373
@@ -23,8 +23,9 @@ use node_runtime::{
|
||||
BabeConfig, BalancesConfig, ContractsConfig, CouncilConfig, DemocracyConfig,
|
||||
ElectionsConfig, GrandpaConfig, ImOnlineConfig, IndicesConfig, Perbill,
|
||||
SessionConfig, SessionKeys, StakerStatus, StakingConfig, SudoConfig, SystemConfig,
|
||||
TechnicalCommitteeConfig, DAYS, DOLLARS, MILLICENTS, WASM_BINARY,
|
||||
TechnicalCommitteeConfig, WASM_BINARY,
|
||||
};
|
||||
use node_runtime::constants::{time::*, currency::*};
|
||||
pub use node_runtime::GenesisConfig;
|
||||
use substrate_service;
|
||||
use hex_literal::hex;
|
||||
|
||||
@@ -263,7 +263,8 @@ mod tests {
|
||||
use babe::CompatibleDigestItem;
|
||||
use consensus_common::{Environment, Proposer, BlockImportParams, BlockOrigin, ForkChoiceStrategy};
|
||||
use node_primitives::DigestItem;
|
||||
use node_runtime::{BalancesCall, Call, CENTS, SECS_PER_BLOCK, UncheckedExtrinsic};
|
||||
use node_runtime::{BalancesCall, Call, UncheckedExtrinsic};
|
||||
use node_runtime::constants::{currency::CENTS, time::SECS_PER_BLOCK};
|
||||
use parity_codec::{Encode, Decode};
|
||||
use primitives::{
|
||||
crypto::Pair as CryptoPair, blake2_256,
|
||||
|
||||
@@ -34,3 +34,4 @@ wabt = "~0.7.4"
|
||||
|
||||
[features]
|
||||
benchmarks = []
|
||||
stress-test = []
|
||||
|
||||
@@ -35,31 +35,29 @@ native_executor_instance!(
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use runtime_io;
|
||||
use super::Executor;
|
||||
use {balances, contracts, indices, staking, system, timestamp};
|
||||
use runtime_io;
|
||||
use substrate_executor::{WasmExecutor, NativeExecutionDispatch};
|
||||
use parity_codec::{Encode, Decode, Joiner};
|
||||
use keyring::{AccountKeyring, Ed25519Keyring, Sr25519Keyring};
|
||||
use runtime_support::{Hashable, StorageValue, StorageMap, traits::{Currency, Get}};
|
||||
use runtime_support::{Hashable, StorageValue, StorageMap, assert_eq_error_rate, traits::Currency};
|
||||
use state_machine::{CodeExecutor, Externalities, TestExternalities as CoreTestExternalities};
|
||||
use primitives::{
|
||||
twox_128, blake2_256, Blake2Hasher, ChangesTrieConfiguration, NeverNativeValue,
|
||||
NativeOrEncoded
|
||||
};
|
||||
use primitives::{ twox_128, blake2_256, Blake2Hasher, ChangesTrieConfiguration, NeverNativeValue, NativeOrEncoded};
|
||||
use node_primitives::{Hash, BlockNumber, AccountId, Balance, Index};
|
||||
use runtime_primitives::traits::{Header as HeaderT, Hash as HashT};
|
||||
use runtime_primitives::traits::{Header as HeaderT, Hash as HashT, Convert};
|
||||
use runtime_primitives::{generic::Era, ApplyOutcome, ApplyError, ApplyResult, Perbill};
|
||||
use runtime_primitives::weights::{WeightMultiplier, SimpleDispatchInfo, WeighData};
|
||||
use {balances, contracts, indices, staking, system, timestamp};
|
||||
use runtime_primitives::weights::{WeightMultiplier, GetDispatchInfo};
|
||||
use contracts::ContractAddressFor;
|
||||
use system::{EventRecord, Phase};
|
||||
use node_runtime::{
|
||||
Header, Block, UncheckedExtrinsic, CheckedExtrinsic, Call, Runtime, Balances, BuildStorage,
|
||||
GenesisConfig, BalancesConfig, SessionConfig, StakingConfig, System, SystemConfig,
|
||||
GrandpaConfig, IndicesConfig, ContractsConfig, Event, SessionKeys, CreationFee,
|
||||
CENTS, DOLLARS, MILLICENTS, SignedExtra, TransactionBaseFee, TransactionByteFee,
|
||||
MaximumBlockWeight,
|
||||
GrandpaConfig, IndicesConfig, ContractsConfig, Event, SessionKeys, SignedExtra,
|
||||
TransferFee, TransactionBaseFee, TransactionByteFee,
|
||||
};
|
||||
use node_runtime::constants::currency::*;
|
||||
use node_runtime::impls::WeightToFee;
|
||||
use wabt;
|
||||
use primitives::map;
|
||||
|
||||
@@ -82,20 +80,18 @@ mod tests {
|
||||
|
||||
type TestExternalities<H> = CoreTestExternalities<H, u64>;
|
||||
|
||||
/// Default transfer fee
|
||||
fn transfer_fee<E: Encode>(extrinsic: &E) -> Balance {
|
||||
let length_fee = <TransactionBaseFee as Get<Balance>>::get() +
|
||||
<TransactionByteFee as Get<Balance>>::get() *
|
||||
let length_fee = TransactionBaseFee::get() +
|
||||
TransactionByteFee::get() *
|
||||
(extrinsic.encode().len() as Balance);
|
||||
let weight_fee = SimpleDispatchInfo::default().weigh_data(()) as Balance;
|
||||
length_fee + weight_fee
|
||||
}
|
||||
|
||||
fn multiplier_ideal() -> u32 {
|
||||
<MaximumBlockWeight as Get<u32>>::get() / 4 / 4
|
||||
}
|
||||
|
||||
fn creation_fee() -> Balance {
|
||||
<CreationFee as Get<Balance>>::get()
|
||||
let weight = default_transfer_call().get_dispatch_info().weight;
|
||||
// NOTE: this is really hard to apply, since the multiplier of each block needs to be fetched
|
||||
// before the block, while we compute this after the block.
|
||||
// weight = <system::Module<Runtime>>::next_weight_multiplier().apply_to(weight);
|
||||
let weight_fee = <Runtime as balances::Trait>::WeightToFee::convert(weight);
|
||||
length_fee + weight_fee + TransferFee::get()
|
||||
}
|
||||
|
||||
fn alice() -> AccountId {
|
||||
@@ -155,10 +151,14 @@ mod tests {
|
||||
)
|
||||
}
|
||||
|
||||
fn default_transfer_call() -> balances::Call<Runtime> {
|
||||
balances::Call::transfer::<Runtime>(bob().into(), 69 * DOLLARS)
|
||||
}
|
||||
|
||||
fn xt() -> UncheckedExtrinsic {
|
||||
sign(CheckedExtrinsic {
|
||||
signed: Some((alice(), signed_extra(0, 0))),
|
||||
function: Call::Balances(balances::Call::transfer::<Runtime>(bob().into(), 69 * DOLLARS)),
|
||||
function: Call::Balances(default_transfer_call()),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -273,7 +273,7 @@ mod tests {
|
||||
assert!(r.is_ok());
|
||||
|
||||
runtime_io::with_externalities(&mut t, || {
|
||||
assert_eq!(Balances::total_balance(&alice()), 42 * DOLLARS - transfer_fee(&xt()) - creation_fee());
|
||||
assert_eq!(Balances::total_balance(&alice()), 42 * DOLLARS - transfer_fee(&xt()));
|
||||
assert_eq!(Balances::total_balance(&bob()), 69 * DOLLARS);
|
||||
});
|
||||
}
|
||||
@@ -309,7 +309,7 @@ mod tests {
|
||||
assert!(r.is_ok());
|
||||
|
||||
runtime_io::with_externalities(&mut t, || {
|
||||
assert_eq!(Balances::total_balance(&alice()), 42 * DOLLARS - transfer_fee(&xt()) - creation_fee());
|
||||
assert_eq!(Balances::total_balance(&alice()), 42 * DOLLARS - transfer_fee(&xt()));
|
||||
assert_eq!(Balances::total_balance(&bob()), 69 * DOLLARS);
|
||||
});
|
||||
}
|
||||
@@ -552,7 +552,7 @@ mod tests {
|
||||
).0.unwrap();
|
||||
|
||||
runtime_io::with_externalities(&mut t, || {
|
||||
assert_eq!(Balances::total_balance(&alice()), 42 * DOLLARS - transfer_fee(&xt()) - creation_fee());
|
||||
assert_eq!(Balances::total_balance(&alice()), 42 * DOLLARS - transfer_fee(&xt()));
|
||||
assert_eq!(Balances::total_balance(&bob()), 169 * DOLLARS);
|
||||
let events = vec![
|
||||
EventRecord {
|
||||
@@ -587,9 +587,18 @@ mod tests {
|
||||
).0.unwrap();
|
||||
|
||||
runtime_io::with_externalities(&mut t, || {
|
||||
// TODO TODO: this needs investigating: why are we deducting creation fee twice here? and why bob also pays it?
|
||||
assert_eq!(Balances::total_balance(&alice()), 32 * DOLLARS - 2 * transfer_fee(&xt()) - 2 * creation_fee());
|
||||
assert_eq!(Balances::total_balance(&bob()), 179 * DOLLARS - transfer_fee(&xt()) - creation_fee());
|
||||
// NOTE: fees differ slightly in tests that execute more than one block due to the
|
||||
// weight update. Hence, using `assert_eq_error_rate`.
|
||||
assert_eq_error_rate!(
|
||||
Balances::total_balance(&alice()),
|
||||
32 * DOLLARS - 2 * transfer_fee(&xt()),
|
||||
10_000
|
||||
);
|
||||
assert_eq_error_rate!(
|
||||
Balances::total_balance(&bob()),
|
||||
179 * DOLLARS - transfer_fee(&xt()),
|
||||
10_000
|
||||
);
|
||||
let events = vec![
|
||||
EventRecord {
|
||||
phase: Phase::ApplyExtrinsic(0),
|
||||
@@ -644,15 +653,23 @@ mod tests {
|
||||
WasmExecutor::new().call(&mut t, 8, COMPACT_CODE, "Core_execute_block", &block1.0).unwrap();
|
||||
|
||||
runtime_io::with_externalities(&mut t, || {
|
||||
assert_eq!(Balances::total_balance(&alice()), 42 * DOLLARS - transfer_fee(&xt()) - creation_fee());
|
||||
assert_eq!(Balances::total_balance(&alice()), 42 * DOLLARS - transfer_fee(&xt()));
|
||||
assert_eq!(Balances::total_balance(&bob()), 169 * DOLLARS);
|
||||
});
|
||||
|
||||
WasmExecutor::new().call(&mut t, 8, COMPACT_CODE, "Core_execute_block", &block2.0).unwrap();
|
||||
|
||||
runtime_io::with_externalities(&mut t, || {
|
||||
assert_eq!(Balances::total_balance(&alice()), 32 * DOLLARS - 2 * transfer_fee(&xt()) - 2 * creation_fee());
|
||||
assert_eq!(Balances::total_balance(&bob()), 179 * DOLLARS - 1 * transfer_fee(&xt()) - creation_fee());
|
||||
assert_eq_error_rate!(
|
||||
Balances::total_balance(&alice()),
|
||||
32 * DOLLARS - 2 * transfer_fee(&xt()),
|
||||
10_000
|
||||
);
|
||||
assert_eq_error_rate!(
|
||||
Balances::total_balance(&bob()),
|
||||
179 * DOLLARS - 1 * transfer_fee(&xt()),
|
||||
10_000
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -814,15 +831,14 @@ mod tests {
|
||||
fn wasm_big_block_import_fails() {
|
||||
let mut t = new_test_ext(COMPACT_CODE, false);
|
||||
|
||||
assert!(
|
||||
WasmExecutor::new().call(
|
||||
&mut t,
|
||||
4,
|
||||
COMPACT_CODE,
|
||||
"Core_execute_block",
|
||||
&block_with_size(42, 0, 120_000).0
|
||||
).is_err()
|
||||
let result = WasmExecutor::new().call(
|
||||
&mut t,
|
||||
4,
|
||||
COMPACT_CODE,
|
||||
"Core_execute_block",
|
||||
&block_with_size(42, 0, 120_000).0
|
||||
);
|
||||
assert!(result.is_err()); // Err(Wasmi(Trap(Trap { kind: Host(AllocatorOutOfSpace) })))
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -897,7 +913,7 @@ mod tests {
|
||||
assert_eq!(r, Ok(ApplyOutcome::Success));
|
||||
|
||||
runtime_io::with_externalities(&mut t, || {
|
||||
assert_eq!(Balances::total_balance(&alice()), 42 * DOLLARS - 1 * transfer_fee(&xt()) - creation_fee());
|
||||
assert_eq!(Balances::total_balance(&alice()), 42 * DOLLARS - 1 * transfer_fee(&xt()));
|
||||
assert_eq!(Balances::total_balance(&bob()), 69 * DOLLARS);
|
||||
});
|
||||
}
|
||||
@@ -947,7 +963,6 @@ mod tests {
|
||||
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn weight_multiplier_increases_and_decreases_on_big_weight() {
|
||||
let mut t = new_test_ext(COMPACT_CODE, false);
|
||||
|
||||
@@ -958,25 +973,22 @@ mod tests {
|
||||
});
|
||||
|
||||
let mut tt = new_test_ext(COMPACT_CODE, false);
|
||||
// NOTE: This assumes that system::remark has the default.
|
||||
let num_to_exhaust = multiplier_ideal() * 2 / SimpleDispatchInfo::default().weigh_data(());
|
||||
println!("++ Generating {} transactions to fill {} weight units", num_to_exhaust, multiplier_ideal() * 2);
|
||||
|
||||
let mut xts = (0..num_to_exhaust).map(|i| CheckedExtrinsic {
|
||||
signed: Some((charlie(), signed_extra(i.into(), 0))),
|
||||
function: Call::System(system::Call::remark(vec![0; 1])),
|
||||
}).collect::<Vec<CheckedExtrinsic>>();
|
||||
xts.insert(0, CheckedExtrinsic {
|
||||
signed: None,
|
||||
function: Call::Timestamp(timestamp::Call::set(42)),
|
||||
});
|
||||
|
||||
// big one in terms of weight.
|
||||
let block1 = construct_block(
|
||||
&mut tt,
|
||||
1,
|
||||
GENESIS_HASH.into(),
|
||||
xts
|
||||
vec![
|
||||
CheckedExtrinsic {
|
||||
signed: None,
|
||||
function: Call::Timestamp(timestamp::Call::set(42)),
|
||||
},
|
||||
CheckedExtrinsic {
|
||||
signed: Some((charlie(), signed_extra(0, 0))),
|
||||
function: Call::System(system::Call::fill_block()),
|
||||
}
|
||||
]
|
||||
);
|
||||
|
||||
// small one in terms of weight.
|
||||
@@ -990,12 +1002,14 @@ mod tests {
|
||||
function: Call::Timestamp(timestamp::Call::set(52)),
|
||||
},
|
||||
CheckedExtrinsic {
|
||||
signed: Some((charlie(), signed_extra(num_to_exhaust.into(), 0))),
|
||||
signed: Some((charlie(), signed_extra(1, 0))),
|
||||
function: Call::System(system::Call::remark(vec![0; 1])),
|
||||
}
|
||||
]
|
||||
);
|
||||
|
||||
println!("++ Block 1 size: {} / Block 2 size {}", block1.0.encode().len(), block2.0.encode().len());
|
||||
|
||||
// execute a big block.
|
||||
executor().call::<_, NeverNativeValue, fn() -> _>(
|
||||
&mut t,
|
||||
@@ -1030,6 +1044,209 @@ mod tests {
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn transaction_fee_is_correct_ultimate() {
|
||||
// This uses the exact values of substrate-node.
|
||||
//
|
||||
// weight of transfer call as of now: 1_000_000
|
||||
// if weight of the cheapest weight would be 10^7, this would be 10^9, which is:
|
||||
// - 1 MILLICENTS in substrate node.
|
||||
// - 1 milldot based on current polkadot runtime.
|
||||
// (this baed on assigning 0.1 CENT to the cheapest tx with `weight = 100`)
|
||||
let mut t = TestExternalities::<Blake2Hasher>::new_with_code(COMPACT_CODE, map![
|
||||
blake2_256(&<balances::FreeBalance<Runtime>>::key_for(alice())).to_vec() => {
|
||||
(100 * DOLLARS).encode()
|
||||
},
|
||||
blake2_256(&<balances::FreeBalance<Runtime>>::key_for(bob())).to_vec() => {
|
||||
(10 * DOLLARS).encode()
|
||||
},
|
||||
twox_128(<balances::TotalIssuance<Runtime>>::key()).to_vec() => {
|
||||
(110 * DOLLARS).encode()
|
||||
},
|
||||
twox_128(<indices::NextEnumSet<Runtime>>::key()).to_vec() => vec![0u8; 16],
|
||||
blake2_256(&<system::BlockHash<Runtime>>::key_for(0)).to_vec() => vec![0u8; 32]
|
||||
]);
|
||||
|
||||
let tip = 1_000_000;
|
||||
let xt = sign(CheckedExtrinsic {
|
||||
signed: Some((alice(), signed_extra(0, tip))),
|
||||
function: Call::Balances(default_transfer_call()),
|
||||
});
|
||||
|
||||
let r = executor().call::<_, NeverNativeValue, fn() -> _>(
|
||||
&mut t,
|
||||
"Core_initialize_block",
|
||||
&vec![].and(&from_block_number(1u64)),
|
||||
true,
|
||||
None,
|
||||
).0;
|
||||
|
||||
assert!(r.is_ok());
|
||||
let r = executor().call::<_, NeverNativeValue, fn() -> _>(
|
||||
&mut t,
|
||||
"BlockBuilder_apply_extrinsic",
|
||||
&vec![].and(&xt.clone()),
|
||||
true,
|
||||
None,
|
||||
).0;
|
||||
assert!(r.is_ok());
|
||||
|
||||
runtime_io::with_externalities(&mut t, || {
|
||||
assert_eq!(Balances::total_balance(&bob()), (10 + 69) * DOLLARS);
|
||||
// Components deducted from alice's balances:
|
||||
// - Weight fee
|
||||
// - Length fee
|
||||
// - Tip
|
||||
// - Creation-fee of bob's account.
|
||||
let mut balance_alice = (100 - 69) * DOLLARS;
|
||||
|
||||
let length_fee = TransactionBaseFee::get() +
|
||||
TransactionByteFee::get() *
|
||||
(xt.clone().encode().len() as Balance);
|
||||
balance_alice -= length_fee;
|
||||
|
||||
let weight = default_transfer_call().get_dispatch_info().weight;
|
||||
let weight_fee = WeightToFee::convert(weight);
|
||||
|
||||
// we know that weight to fee multiplier is effect-less in block 1.
|
||||
assert_eq!(weight_fee as Balance, MILLICENTS);
|
||||
balance_alice -= weight_fee;
|
||||
|
||||
balance_alice -= tip;
|
||||
balance_alice -= TransferFee::get();
|
||||
|
||||
assert_eq!(Balances::total_balance(&alice()), balance_alice);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
#[cfg(feature = "stress-test")]
|
||||
fn block_weight_capacity_report() {
|
||||
// Just report how many transfer calls you could fit into a block. The number should at least
|
||||
// be a few hundred (250 at the time of writing but can change over time). Runs until panic.
|
||||
|
||||
// execution ext.
|
||||
let mut t = new_test_ext(COMPACT_CODE, false);
|
||||
// setup ext.
|
||||
let mut tt = new_test_ext(COMPACT_CODE, false);
|
||||
|
||||
let factor = 50;
|
||||
let mut time = 10;
|
||||
let mut nonce: Index = 0;
|
||||
let mut block_number = 1;
|
||||
let mut previous_hash: Hash = GENESIS_HASH.into();
|
||||
|
||||
loop {
|
||||
let num_transfers = block_number * factor;
|
||||
let mut xts = (0..num_transfers).map(|i| CheckedExtrinsic {
|
||||
signed: Some((charlie(), signed_extra(nonce + i as Index, 0))),
|
||||
function: Call::Balances(balances::Call::transfer(bob().into(), 0)),
|
||||
}).collect::<Vec<CheckedExtrinsic>>();
|
||||
|
||||
xts.insert(0, CheckedExtrinsic {
|
||||
signed: None,
|
||||
function: Call::Timestamp(timestamp::Call::set(time)),
|
||||
});
|
||||
|
||||
// NOTE: this is super slow. Can probably be improved.
|
||||
let block = construct_block(
|
||||
&mut tt,
|
||||
block_number,
|
||||
previous_hash,
|
||||
xts
|
||||
);
|
||||
|
||||
let len = block.0.len();
|
||||
print!(
|
||||
"++ Executing block with {} transfers. Block size = {} bytes / {} kb / {} mb",
|
||||
num_transfers,
|
||||
len,
|
||||
len / 1024,
|
||||
len / 1024 / 1024,
|
||||
);
|
||||
|
||||
let r = executor().call::<_, NeverNativeValue, fn() -> _>(
|
||||
&mut t,
|
||||
"Core_execute_block",
|
||||
&block.0,
|
||||
true,
|
||||
None,
|
||||
).0;
|
||||
|
||||
println!(" || Result = {:?}", r);
|
||||
assert!(r.is_ok());
|
||||
|
||||
previous_hash = block.1;
|
||||
nonce += num_transfers;
|
||||
time += 10;
|
||||
block_number += 1;
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
#[cfg(feature = "stress-test")]
|
||||
fn block_length_capacity_report() {
|
||||
// Just report how big a block can get. Executes until panic. Should be ignored unless if
|
||||
// manually inspected. The number should at least be a few megabytes (5 at the time of
|
||||
// writing but can change over time).
|
||||
|
||||
// execution ext.
|
||||
let mut t = new_test_ext(COMPACT_CODE, false);
|
||||
// setup ext.
|
||||
let mut tt = new_test_ext(COMPACT_CODE, false);
|
||||
|
||||
let factor = 256 * 1024;
|
||||
let mut time = 10;
|
||||
let mut nonce: Index = 0;
|
||||
let mut block_number = 1;
|
||||
let mut previous_hash: Hash = GENESIS_HASH.into();
|
||||
|
||||
loop {
|
||||
// NOTE: this is super slow. Can probably be improved.
|
||||
let block = construct_block(
|
||||
&mut tt,
|
||||
block_number,
|
||||
previous_hash,
|
||||
vec![
|
||||
CheckedExtrinsic {
|
||||
signed: None,
|
||||
function: Call::Timestamp(timestamp::Call::set(time)),
|
||||
},
|
||||
CheckedExtrinsic {
|
||||
signed: Some((charlie(), signed_extra(nonce, 0))),
|
||||
function: Call::System(system::Call::remark(vec![0u8; (block_number * factor) as usize])),
|
||||
},
|
||||
]
|
||||
);
|
||||
|
||||
let len = block.0.len();
|
||||
print!(
|
||||
"++ Executing block with big remark. Block size = {} bytes / {} kb / {} mb",
|
||||
len,
|
||||
len / 1024,
|
||||
len / 1024 / 1024,
|
||||
);
|
||||
|
||||
let r = executor().call::<_, NeverNativeValue, fn() -> _>(
|
||||
&mut t,
|
||||
"Core_execute_block",
|
||||
&block.0,
|
||||
true,
|
||||
None,
|
||||
).0;
|
||||
|
||||
println!(" || Result = {:?}", r);
|
||||
assert!(r.is_ok());
|
||||
|
||||
previous_hash = block.1;
|
||||
nonce += 1;
|
||||
time += 10;
|
||||
block_number += 1;
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "benchmarks")]
|
||||
mod benches {
|
||||
use super::*;
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
// Copyright 2019 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Substrate is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! A set of constant values used in substrate runtime.
|
||||
|
||||
/// Money matters.
|
||||
pub mod currency {
|
||||
use node_primitives::Balance;
|
||||
|
||||
pub const MILLICENTS: Balance = 1_000_000_000;
|
||||
pub const CENTS: Balance = 1_000 * MILLICENTS; // assume this is worth about a cent.
|
||||
pub const DOLLARS: Balance = 100 * CENTS;
|
||||
}
|
||||
|
||||
/// Time.
|
||||
pub mod time {
|
||||
use node_primitives::Moment;
|
||||
|
||||
pub const SECS_PER_BLOCK: Moment = 6;
|
||||
pub const MINUTES: Moment = 60 / SECS_PER_BLOCK;
|
||||
pub const HOURS: Moment = MINUTES * 60;
|
||||
pub const DAYS: Moment = HOURS * 24;
|
||||
}
|
||||
|
||||
// CRITICAL NOTE: The system module maintains two constants: a _maximum_ block weight and a
|
||||
// _ratio_ of it yielding the portion which is accessible to normal transactions (reserving the rest
|
||||
// for operational ones). `TARGET_BLOCK_FULLNESS` is entirely independent and the system module is
|
||||
// not aware of if, nor should it care about it. This constant simply denotes on which ratio of the
|
||||
// _maximum_ block weight we tweak the fees. It does NOT care about the type of the dispatch.
|
||||
//
|
||||
// For the system to be configured in a sane way, `TARGET_BLOCK_FULLNESS` should always be less than
|
||||
// the ratio that `system` module uses to find normal transaction quota.
|
||||
/// Fee-related.
|
||||
pub mod fee {
|
||||
pub use runtime_primitives::Perbill;
|
||||
|
||||
/// The block saturation level. Fees will be updates based on this value.
|
||||
pub const TARGET_BLOCK_FULLNESS: Perbill = Perbill::from_percent(25);
|
||||
}
|
||||
@@ -20,8 +20,16 @@ use node_primitives::Balance;
|
||||
use runtime_primitives::weights::{Weight, WeightMultiplier};
|
||||
use runtime_primitives::traits::{Convert, Saturating};
|
||||
use runtime_primitives::Fixed64;
|
||||
use support::traits::Get;
|
||||
use crate::{Balances, MaximumBlockWeight};
|
||||
use support::traits::{OnUnbalanced, Currency};
|
||||
use crate::{Balances, Authorship, MaximumBlockWeight, NegativeImbalance};
|
||||
use crate::constants::fee::TARGET_BLOCK_FULLNESS;
|
||||
|
||||
pub struct Author;
|
||||
impl OnUnbalanced<NegativeImbalance> for Author {
|
||||
fn on_unbalanced(amount: NegativeImbalance) {
|
||||
Balances::resolve_creating(&Authorship::author(), amount);
|
||||
}
|
||||
}
|
||||
|
||||
/// Struct that handles the conversion of Balance -> `u64`. This is used for staking's election
|
||||
/// calculation.
|
||||
@@ -39,32 +47,53 @@ impl Convert<u128, Balance> for CurrencyToVoteHandler {
|
||||
fn convert(x: u128) -> Balance { x * Self::factor() }
|
||||
}
|
||||
|
||||
/// Handles converting a weight scalar to a fee value, based on the scale and granularity of the
|
||||
/// node's balance type.
|
||||
///
|
||||
/// This should typically create a mapping between the following ranges:
|
||||
/// - [0, system::MaximumBlockWeight]
|
||||
/// - [Balance::min, Balance::max]
|
||||
///
|
||||
/// Yet, it can be used for any other sort of change to weight-fee. Some examples being:
|
||||
/// - Setting it to `0` will essentially disable the weight fee.
|
||||
/// - Setting it to `1` will cause the literal `#[weight = x]` values to be charged.
|
||||
///
|
||||
/// By default, substrate node will have a weight range of [0, 1_000_000_000].
|
||||
pub struct WeightToFee;
|
||||
impl Convert<Weight, Balance> for WeightToFee {
|
||||
fn convert(x: Weight) -> Balance {
|
||||
// substrate-node a weight of 10_000 (smallest non-zero weight) to be mapped to 10^7 units of
|
||||
// fees, hence:
|
||||
Balance::from(x).saturating_mul(1_000)
|
||||
}
|
||||
}
|
||||
|
||||
/// A struct that updates the weight multiplier based on the saturation level of the previous block.
|
||||
/// This should typically be called once per-block.
|
||||
///
|
||||
/// This assumes that weight is a numeric value in the u32 range.
|
||||
///
|
||||
/// Given `TARGET_BLOCK_FULLNESS = 1/2`, a block saturation greater than 1/2 will cause the system
|
||||
/// fees to slightly grow and the opposite for block saturations less than 1/2.
|
||||
///
|
||||
/// Formula:
|
||||
/// diff = (ideal_weight - current_block_weight)
|
||||
/// diff = (target_weight - current_block_weight)
|
||||
/// v = 0.00004
|
||||
/// next_weight = weight * (1 + (v . diff) + (v . diff)^2 / 2)
|
||||
///
|
||||
/// https://research.web3.foundation/en/latest/polkadot/Token%20Economics/#relay-chain-transaction-fees
|
||||
pub struct WeightMultiplierUpdateHandler;
|
||||
|
||||
impl Convert<(Weight, WeightMultiplier), WeightMultiplier> for WeightMultiplierUpdateHandler {
|
||||
fn convert(previous_state: (Weight, WeightMultiplier)) -> WeightMultiplier {
|
||||
let (block_weight, multiplier) = previous_state;
|
||||
// CRITICAL NOTE: what the system module interprets as maximum block weight, and a portion
|
||||
// of it (1/4 usually) as ideal weight demonstrate the gap in block weights for operational
|
||||
// transactions. What this weight multiplier interprets as the maximum, is actually the
|
||||
// maximum that is available to normal transactions. Hence,
|
||||
let max_weight = <MaximumBlockWeight as Get<u32>>::get() / 4;
|
||||
let ideal_weight = (max_weight / 4) as u128;
|
||||
let max_weight = MaximumBlockWeight::get();
|
||||
let target_weight = (TARGET_BLOCK_FULLNESS * max_weight) as u128;
|
||||
let block_weight = block_weight as u128;
|
||||
|
||||
// determines if the first_term is positive
|
||||
let positive = block_weight >= ideal_weight;
|
||||
let diff_abs = block_weight.max(ideal_weight) - block_weight.min(ideal_weight);
|
||||
let positive = block_weight >= target_weight;
|
||||
let diff_abs = block_weight.max(target_weight) - block_weight.min(target_weight);
|
||||
// diff is within u32, safe.
|
||||
let diff = Fixed64::from_rational(diff_abs as i64, max_weight as u64);
|
||||
let diff_squared = diff.saturating_mul(diff);
|
||||
@@ -81,6 +110,8 @@ impl Convert<(Weight, WeightMultiplier), WeightMultiplier> for WeightMultiplierU
|
||||
let second_term = v_squared_2.saturating_mul(diff_squared);
|
||||
|
||||
if positive {
|
||||
// Note: this is merely bounded by how big the multiplier and the inner value can go,
|
||||
// not by any economical reasoning.
|
||||
let excess = first_term.saturating_add(second_term);
|
||||
multiplier.saturating_add(WeightMultiplier::from_fixed(excess))
|
||||
} else {
|
||||
@@ -90,7 +121,7 @@ impl Convert<(Weight, WeightMultiplier), WeightMultiplier> for WeightMultiplierU
|
||||
// despite the fact that apply_to saturates weight (final fee cannot go below 0)
|
||||
// it is crucially important to stop here and don't further reduce the weight fee
|
||||
// multiplier. While at -1, it means that the network is so un-congested that all
|
||||
// transactions are practically free. We stop here and only increase if the network
|
||||
// transactions have no weight fee. We stop here and only increase if the network
|
||||
// became more busy.
|
||||
.max(WeightMultiplier::from_rational(-1, 1))
|
||||
}
|
||||
@@ -102,13 +133,15 @@ mod tests {
|
||||
use super::*;
|
||||
use runtime_primitives::weights::Weight;
|
||||
use runtime_primitives::Perbill;
|
||||
use crate::{MaximumBlockWeight, AvailableBlockRatio, Runtime};
|
||||
use crate::constants::currency::*;
|
||||
|
||||
fn max() -> Weight {
|
||||
<MaximumBlockWeight as Get<Weight>>::get()
|
||||
MaximumBlockWeight::get()
|
||||
}
|
||||
|
||||
fn ideal() -> Weight {
|
||||
max() / 4 / 4
|
||||
fn target() -> Weight {
|
||||
TARGET_BLOCK_FULLNESS * max()
|
||||
}
|
||||
|
||||
// poc reference implementation.
|
||||
@@ -120,7 +153,7 @@ mod tests {
|
||||
// maximum tx weight
|
||||
let m = max() as f32;
|
||||
// Ideal saturation in terms of weight
|
||||
let ss = ideal() as f32;
|
||||
let ss = target() as f32;
|
||||
// Current saturation in terms of weight
|
||||
let s = block_weight;
|
||||
|
||||
@@ -130,52 +163,93 @@ mod tests {
|
||||
Perbill::from_parts((fm * 1_000_000_000_f32) as u32)
|
||||
}
|
||||
|
||||
fn fm(parts: i64) -> WeightMultiplier {
|
||||
fn wm(parts: i64) -> WeightMultiplier {
|
||||
WeightMultiplier::from_parts(parts)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty_chain_simulation() {
|
||||
// just a few txs per_block.
|
||||
let block_weight = 1000;
|
||||
let mut wm = WeightMultiplier::default();
|
||||
let mut iterations: u64 = 0;
|
||||
loop {
|
||||
let next = WeightMultiplierUpdateHandler::convert((block_weight, wm));
|
||||
wm = next;
|
||||
if wm == WeightMultiplier::from_rational(-1, 1) { break; }
|
||||
iterations += 1;
|
||||
}
|
||||
println!("iteration {}, new wm = {:?}. Weight fee is now zero", iterations, wm);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn congested_chain_simulation() {
|
||||
// `cargo test congested_chain_simulation -- --nocapture` to get some insight.
|
||||
// almost full. The entire quota of normal transactions is taken.
|
||||
let block_weight = AvailableBlockRatio::get() * max();
|
||||
let tx_weight = 1000;
|
||||
let mut wm = WeightMultiplier::default();
|
||||
let mut iterations: u64 = 0;
|
||||
loop {
|
||||
let next = WeightMultiplierUpdateHandler::convert((block_weight, wm));
|
||||
if wm == next { break; }
|
||||
wm = next;
|
||||
iterations += 1;
|
||||
let fee = <Runtime as balances::Trait>::WeightToFee::convert(wm.apply_to(tx_weight));
|
||||
println!(
|
||||
"iteration {}, new wm = {:?}. Fee at this point is: {} millicents, {} cents, {} dollars",
|
||||
iterations,
|
||||
wm,
|
||||
fee / MILLICENTS,
|
||||
fee / CENTS,
|
||||
fee / DOLLARS
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stateless_weight_mul() {
|
||||
// Light block. Fee is reduced a little.
|
||||
assert_eq!(
|
||||
WeightMultiplierUpdateHandler::convert((ideal() / 4, WeightMultiplier::default())),
|
||||
fm(-7500)
|
||||
WeightMultiplierUpdateHandler::convert((target() / 4, WeightMultiplier::default())),
|
||||
wm(-7500)
|
||||
);
|
||||
// a bit more. Fee is decreased less, meaning that the fee increases as the block grows.
|
||||
assert_eq!(
|
||||
WeightMultiplierUpdateHandler::convert((ideal() / 2, WeightMultiplier::default())),
|
||||
fm(-5000)
|
||||
WeightMultiplierUpdateHandler::convert((target() / 2, WeightMultiplier::default())),
|
||||
wm(-5000)
|
||||
);
|
||||
// ideal. Original fee. No changes.
|
||||
assert_eq!(
|
||||
WeightMultiplierUpdateHandler::convert((ideal() as u32, WeightMultiplier::default())),
|
||||
fm(0)
|
||||
WeightMultiplierUpdateHandler::convert((target(), WeightMultiplier::default())),
|
||||
wm(0)
|
||||
);
|
||||
// // More than ideal. Fee is increased.
|
||||
assert_eq!(
|
||||
WeightMultiplierUpdateHandler::convert(((ideal() * 2) as u32, WeightMultiplier::default())),
|
||||
fm(10000)
|
||||
WeightMultiplierUpdateHandler::convert(((target() * 2), WeightMultiplier::default())),
|
||||
wm(10000)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stateful_weight_mul_grow_to_infinity() {
|
||||
assert_eq!(
|
||||
WeightMultiplierUpdateHandler::convert((ideal() * 2, WeightMultiplier::default())),
|
||||
fm(10000)
|
||||
WeightMultiplierUpdateHandler::convert((target() * 2, WeightMultiplier::default())),
|
||||
wm(10000)
|
||||
);
|
||||
assert_eq!(
|
||||
WeightMultiplierUpdateHandler::convert((ideal() * 2, fm(10000))),
|
||||
fm(20000)
|
||||
WeightMultiplierUpdateHandler::convert((target() * 2, wm(10000))),
|
||||
wm(20000)
|
||||
);
|
||||
assert_eq!(
|
||||
WeightMultiplierUpdateHandler::convert((ideal() * 2, fm(20000))),
|
||||
fm(30000)
|
||||
WeightMultiplierUpdateHandler::convert((target() * 2, wm(20000))),
|
||||
wm(30000)
|
||||
);
|
||||
// ...
|
||||
assert_eq!(
|
||||
WeightMultiplierUpdateHandler::convert((ideal() * 2, fm(1_000_000_000))),
|
||||
fm(1_000_000_000 + 10000)
|
||||
WeightMultiplierUpdateHandler::convert((target() * 2, wm(1_000_000_000))),
|
||||
wm(1_000_000_000 + 10000)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -183,28 +257,26 @@ mod tests {
|
||||
fn stateful_weight_mil_collapse_to_minus_one() {
|
||||
assert_eq!(
|
||||
WeightMultiplierUpdateHandler::convert((0, WeightMultiplier::default())),
|
||||
fm(-10000)
|
||||
wm(-10000)
|
||||
);
|
||||
assert_eq!(
|
||||
WeightMultiplierUpdateHandler::convert((0, fm(-10000))),
|
||||
fm(-20000)
|
||||
WeightMultiplierUpdateHandler::convert((0, wm(-10000))),
|
||||
wm(-20000)
|
||||
);
|
||||
assert_eq!(
|
||||
WeightMultiplierUpdateHandler::convert((0, fm(-20000))),
|
||||
fm(-30000)
|
||||
WeightMultiplierUpdateHandler::convert((0, wm(-20000))),
|
||||
wm(-30000)
|
||||
);
|
||||
// ...
|
||||
assert_eq!(
|
||||
WeightMultiplierUpdateHandler::convert((0, fm(1_000_000_000 * -1))),
|
||||
fm(-1_000_000_000)
|
||||
WeightMultiplierUpdateHandler::convert((0, wm(1_000_000_000 * -1))),
|
||||
wm(-1_000_000_000)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn weight_to_fee_should_not_overflow_on_large_weights() {
|
||||
// defensive-only test. at the moment we are not allowing any weight more than
|
||||
// 4 * 1024 * 1024 in a block.
|
||||
let kb = 1024_u32;
|
||||
let kb = 1024 as Weight;
|
||||
let mb = kb * kb;
|
||||
let max_fm = WeightMultiplier::from_fixed(Fixed64::from_natural(i64::max_value()));
|
||||
|
||||
@@ -214,7 +286,9 @@ mod tests {
|
||||
WeightMultiplierUpdateHandler::convert((i, WeightMultiplier::default()));
|
||||
});
|
||||
|
||||
vec![10 * mb, Weight::max_value() / 2, Weight::max_value()]
|
||||
// Some values that are all above the target and will cause an increase.
|
||||
let t = target();
|
||||
vec![t + 100, t * 2, t * 4]
|
||||
.into_iter()
|
||||
.for_each(|i| {
|
||||
let fm = WeightMultiplierUpdateHandler::convert((
|
||||
|
||||
@@ -22,7 +22,7 @@
|
||||
|
||||
use rstd::prelude::*;
|
||||
use support::{
|
||||
construct_runtime, parameter_types, traits::{SplitTwoWays, Currency, OnUnbalanced}
|
||||
construct_runtime, parameter_types, traits::{SplitTwoWays, Currency}
|
||||
};
|
||||
use substrate_primitives::u32_trait::{_1, _2, _3, _4};
|
||||
use node_primitives::{
|
||||
@@ -58,10 +58,13 @@ pub use runtime_primitives::{Permill, Perbill};
|
||||
pub use support::StorageValue;
|
||||
pub use staking::StakerStatus;
|
||||
|
||||
/// Implementations for `Convert` and other helper structs passed into runtime modules as associated
|
||||
/// types.
|
||||
/// Implementations of some helper traits passed into runtime modules as associated types.
|
||||
pub mod impls;
|
||||
use impls::{CurrencyToVoteHandler, WeightMultiplierUpdateHandler};
|
||||
use impls::{CurrencyToVoteHandler, WeightMultiplierUpdateHandler, Author, WeightToFee};
|
||||
|
||||
/// Constant values used within the runtime.
|
||||
pub mod constants;
|
||||
use constants::{time::*, currency::*};
|
||||
|
||||
// Make the WASM binary available.
|
||||
#[cfg(feature = "std")]
|
||||
@@ -76,8 +79,8 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
|
||||
// and set impl_version to equal spec_version. If only runtime
|
||||
// implementation changes and behavior does not, then leave spec_version as
|
||||
// is and increment impl_version.
|
||||
spec_version: 119,
|
||||
impl_version: 119,
|
||||
spec_version: 120,
|
||||
impl_version: 120,
|
||||
apis: RUNTIME_API_VERSIONS,
|
||||
};
|
||||
|
||||
@@ -90,20 +93,8 @@ pub fn native_version() -> NativeVersion {
|
||||
}
|
||||
}
|
||||
|
||||
pub const MILLICENTS: Balance = 1_000_000_000;
|
||||
pub const CENTS: Balance = 1_000 * MILLICENTS; // assume this is worth about a cent.
|
||||
pub const DOLLARS: Balance = 100 * CENTS;
|
||||
|
||||
type NegativeImbalance = <Balances as Currency<AccountId>>::NegativeImbalance;
|
||||
|
||||
pub struct Author;
|
||||
|
||||
impl OnUnbalanced<NegativeImbalance> for Author {
|
||||
fn on_unbalanced(amount: NegativeImbalance) {
|
||||
Balances::resolve_creating(&Authorship::author(), amount);
|
||||
}
|
||||
}
|
||||
|
||||
pub type DealWithFees = SplitTwoWays<
|
||||
Balance,
|
||||
NegativeImbalance,
|
||||
@@ -111,15 +102,11 @@ pub type DealWithFees = SplitTwoWays<
|
||||
_1, Author, // 1 part (20%) goes to the block author.
|
||||
>;
|
||||
|
||||
pub const SECS_PER_BLOCK: Moment = 6;
|
||||
pub const MINUTES: Moment = 60 / SECS_PER_BLOCK;
|
||||
pub const HOURS: Moment = MINUTES * 60;
|
||||
pub const DAYS: Moment = HOURS * 24;
|
||||
|
||||
parameter_types! {
|
||||
pub const BlockHashCount: BlockNumber = 250;
|
||||
pub const MaximumBlockWeight: Weight = 4 * 1024 * 1024;
|
||||
pub const MaximumBlockLength: u32 = 4 * 1024 * 1024;
|
||||
pub const MaximumBlockWeight: Weight = 1_000_000_000;
|
||||
pub const AvailableBlockRatio: Perbill = Perbill::from_percent(75);
|
||||
pub const MaximumBlockLength: u32 = 5 * 1024 * 1024;
|
||||
}
|
||||
|
||||
impl system::Trait for Runtime {
|
||||
@@ -136,6 +123,7 @@ impl system::Trait for Runtime {
|
||||
type BlockHashCount = BlockHashCount;
|
||||
type MaximumBlockWeight = MaximumBlockWeight;
|
||||
type MaximumBlockLength = MaximumBlockLength;
|
||||
type AvailableBlockRatio = AvailableBlockRatio;
|
||||
}
|
||||
|
||||
parameter_types! {
|
||||
@@ -174,6 +162,7 @@ impl balances::Trait for Runtime {
|
||||
type CreationFee = CreationFee;
|
||||
type TransactionBaseFee = TransactionBaseFee;
|
||||
type TransactionByteFee = TransactionByteFee;
|
||||
type WeightToFee = WeightToFee;
|
||||
}
|
||||
|
||||
parameter_types! {
|
||||
|
||||
Reference in New Issue
Block a user