mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-08-08 16:25:44 +00:00
Extrinsics root is calculated as part of block-building (#120)
* extrinsics root is calculated as part of block-building. * Fix build. * Fix xt root. * Couple of fixes. * Logging and more useful APIs. * Fix test. * Update log version. * Switch back to correct version of log.
This commit is contained in:
committed by
Robert Habermeier
parent
54d6970efc
commit
1972d612fa
@@ -12,7 +12,7 @@ ed25519 = { path = "../ed25519" }
|
||||
tokio-timer = "0.1.2"
|
||||
parking_lot = "0.4"
|
||||
error-chain = "0.11"
|
||||
log = "0.4"
|
||||
log = "0.3"
|
||||
|
||||
[dev-dependencies]
|
||||
substrate-keyring = { path = "../keyring" }
|
||||
|
||||
@@ -179,9 +179,9 @@ impl_function_executor!(this: FunctionExecutor<'e, E>,
|
||||
let key = this.memory.get(key_data, key_len as usize).map_err(|_| DummyUserError)?;
|
||||
let value = this.memory.get(value_data, value_len as usize).map_err(|_| DummyUserError)?;
|
||||
if let Some(preimage) = this.hash_lookup.get(&key) {
|
||||
info!(target: "wasm-trace", "*** Setting storage: %{} -> {} [k={}]", ascii_format(&preimage), HexDisplay::from(&value), HexDisplay::from(&key));
|
||||
trace!(target: "wasm-trace", "*** Setting storage: %{} -> {} [k={}]", ascii_format(&preimage), HexDisplay::from(&value), HexDisplay::from(&key));
|
||||
} else {
|
||||
info!(target: "wasm-trace", "*** Setting storage: {} -> {} [k={}]", ascii_format(&key), HexDisplay::from(&value), HexDisplay::from(&key));
|
||||
trace!(target: "wasm-trace", "*** Setting storage: {} -> {} [k={}]", ascii_format(&key), HexDisplay::from(&value), HexDisplay::from(&key));
|
||||
}
|
||||
this.ext.set_storage(key, value);
|
||||
Ok(())
|
||||
@@ -189,9 +189,9 @@ impl_function_executor!(this: FunctionExecutor<'e, E>,
|
||||
ext_clear_storage(key_data: *const u8, key_len: u32) => {
|
||||
let key = this.memory.get(key_data, key_len as usize).map_err(|_| DummyUserError)?;
|
||||
if let Some(preimage) = this.hash_lookup.get(&key) {
|
||||
info!(target: "wasm-trace", "*** Clearing storage: %{} [k={}]", ascii_format(&preimage), HexDisplay::from(&key));
|
||||
trace!(target: "wasm-trace", "*** Clearing storage: %{} [k={}]", ascii_format(&preimage), HexDisplay::from(&key));
|
||||
} else {
|
||||
info!(target: "wasm-trace", "*** Clearing storage: {} [k={}]", ascii_format(&key), HexDisplay::from(&key));
|
||||
trace!(target: "wasm-trace", "*** Clearing storage: {} [k={}]", ascii_format(&key), HexDisplay::from(&key));
|
||||
}
|
||||
this.ext.clear_storage(&key);
|
||||
Ok(())
|
||||
@@ -202,9 +202,9 @@ impl_function_executor!(this: FunctionExecutor<'e, E>,
|
||||
let maybe_value = this.ext.storage(&key);
|
||||
|
||||
if let Some(preimage) = this.hash_lookup.get(&key) {
|
||||
info!(target: "wasm-trace", " Getting storage: %{} == {} [k={}]", ascii_format(&preimage), if let Some(ref b) = maybe_value { format!("{}", HexDisplay::from(b)) } else { "<empty>".to_owned() }, HexDisplay::from(&key));
|
||||
trace!(target: "wasm-trace", " Getting storage: %{} == {} [k={}]", ascii_format(&preimage), if let Some(ref b) = maybe_value { format!("{}", HexDisplay::from(b)) } else { "<empty>".to_owned() }, HexDisplay::from(&key));
|
||||
} else {
|
||||
info!(target: "wasm-trace", " Getting storage: {} == {} [k={}]", ascii_format(&key), if let Some(ref b) = maybe_value { format!("{}", HexDisplay::from(b)) } else { "<empty>".to_owned() }, HexDisplay::from(&key));
|
||||
trace!(target: "wasm-trace", " Getting storage: {} == {} [k={}]", ascii_format(&key), if let Some(ref b) = maybe_value { format!("{}", HexDisplay::from(b)) } else { "<empty>".to_owned() }, HexDisplay::from(&key));
|
||||
}
|
||||
|
||||
if let Some(value) = maybe_value {
|
||||
@@ -222,9 +222,9 @@ impl_function_executor!(this: FunctionExecutor<'e, E>,
|
||||
let key = this.memory.get(key_data, key_len as usize).map_err(|_| DummyUserError)?;
|
||||
let maybe_value = this.ext.storage(&key);
|
||||
if let Some(preimage) = this.hash_lookup.get(&key) {
|
||||
info!(target: "wasm-trace", " Getting storage: %{} == {} [k={}]", ascii_format(&preimage), if let Some(ref b) = maybe_value { format!("{}", HexDisplay::from(b)) } else { "<empty>".to_owned() }, HexDisplay::from(&key));
|
||||
trace!(target: "wasm-trace", " Getting storage: %{} == {} [k={}]", ascii_format(&preimage), if let Some(ref b) = maybe_value { format!("{}", HexDisplay::from(b)) } else { "<empty>".to_owned() }, HexDisplay::from(&key));
|
||||
} else {
|
||||
info!(target: "wasm-trace", " Getting storage: {} == {} [k={}]", ascii_format(&key), if let Some(ref b) = maybe_value { format!("{}", HexDisplay::from(b)) } else { "<empty>".to_owned() }, HexDisplay::from(&key));
|
||||
trace!(target: "wasm-trace", " Getting storage: {} == {} [k={}]", ascii_format(&key), if let Some(ref b) = maybe_value { format!("{}", HexDisplay::from(b)) } else { "<empty>".to_owned() }, HexDisplay::from(&key));
|
||||
}
|
||||
if let Some(value) = maybe_value {
|
||||
let value = &value[value_offset as usize..];
|
||||
|
||||
BIN
Binary file not shown.
BIN
Binary file not shown.
@@ -5,6 +5,7 @@ authors = ["Parity Technologies <admin@parity.io>"]
|
||||
|
||||
[dependencies]
|
||||
parking_lot = "0.4"
|
||||
log = "0.3"
|
||||
error-chain = "0.11"
|
||||
jsonrpc-core = { git="https://github.com/paritytech/jsonrpc.git" }
|
||||
jsonrpc-macros = { git="https://github.com/paritytech/jsonrpc.git" }
|
||||
|
||||
@@ -28,6 +28,8 @@ extern crate substrate_state_machine as state_machine;
|
||||
extern crate error_chain;
|
||||
#[macro_use]
|
||||
extern crate jsonrpc_macros;
|
||||
#[macro_use]
|
||||
extern crate log;
|
||||
|
||||
#[cfg(test)]
|
||||
extern crate substrate_executor;
|
||||
|
||||
@@ -32,13 +32,21 @@ use self::error::Result;
|
||||
build_rpc_trait! {
|
||||
/// Polkadot state API
|
||||
pub trait StateApi {
|
||||
/// Returns a storage entry.
|
||||
#[rpc(name = "state_getStorageAt")]
|
||||
fn storage_at(&self, StorageKey, block::HeaderHash) -> Result<StorageData>;
|
||||
|
||||
/// Call a contract.
|
||||
#[rpc(name = "state_callAt")]
|
||||
fn call_at(&self, String, Vec<u8>, block::HeaderHash) -> Result<Vec<u8>>;
|
||||
|
||||
/// Returns a storage entry.
|
||||
#[rpc(name = "state_getStorage")]
|
||||
fn storage(&self, StorageKey, block::HeaderHash) -> Result<StorageData>;
|
||||
fn storage(&self, StorageKey) -> Result<StorageData>;
|
||||
|
||||
/// Call a contract.
|
||||
#[rpc(name = "state_call")]
|
||||
fn call(&self, String, Vec<u8>, block::HeaderHash) -> Result<Vec<u8>>;
|
||||
fn call(&self, String, Vec<u8>) -> Result<Vec<u8>>;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,11 +55,22 @@ impl<B, E> StateApi for Arc<Client<B, E>> where
|
||||
E: state_machine::CodeExecutor + Send + Sync + 'static,
|
||||
client::error::Error: From<<<B as client::backend::Backend>::State as state_machine::backend::Backend>::Error>,
|
||||
{
|
||||
fn storage(&self, key: StorageKey, block: block::HeaderHash) -> Result<StorageData> {
|
||||
fn storage_at(&self, key: StorageKey, block: block::HeaderHash) -> Result<StorageData> {
|
||||
Ok(self.as_ref().storage(&block::Id::Hash(block), &key)?)
|
||||
}
|
||||
|
||||
fn call(&self, method: String, data: Vec<u8>, block: block::HeaderHash) -> Result<Vec<u8>> {
|
||||
fn call_at(&self, method: String, data: Vec<u8>, block: block::HeaderHash) -> Result<Vec<u8>> {
|
||||
Ok(self.as_ref().call(&block::Id::Hash(block), &method, &data)?.return_data)
|
||||
}
|
||||
|
||||
fn storage(&self, key: StorageKey) -> Result<StorageData> {
|
||||
let at = block::Id::Hash(self.as_ref().info()?.chain.best_hash);
|
||||
use primitives::hexdisplay::HexDisplay;
|
||||
info!("Querying storage at {:?} for key {}", at, HexDisplay::from(&key.0));
|
||||
Ok(self.as_ref().storage(&at, &key)?)
|
||||
}
|
||||
|
||||
fn call(&self, method: String, data: Vec<u8>) -> Result<Vec<u8>> {
|
||||
Ok(self.as_ref().call(&block::Id::Hash(self.as_ref().info()?.chain.best_hash), &method, &data)?.return_data)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,7 +34,7 @@ fn should_return_storage() {
|
||||
let genesis_hash = test_genesis_block.blake2_256().into();
|
||||
|
||||
assert_matches!(
|
||||
StateApi::storage(&client, StorageKey(vec![10]), genesis_hash),
|
||||
StateApi::storage_at(&client, StorageKey(vec![10]), genesis_hash),
|
||||
Err(Error(ErrorKind::Client(client::error::ErrorKind::NoValueForKey(ref k)), _)) if *k == vec![10]
|
||||
)
|
||||
}
|
||||
@@ -55,7 +55,7 @@ fn should_call_contract() {
|
||||
let genesis_hash = test_genesis_block.blake2_256().into();
|
||||
|
||||
assert_matches!(
|
||||
StateApi::call(&client, "balanceOf".into(), vec![1,2,3], genesis_hash),
|
||||
StateApi::call_at(&client, "balanceOf".into(), vec![1,2,3], genesis_hash),
|
||||
Err(Error(ErrorKind::Client(client::error::ErrorKind::Execution(_)), _))
|
||||
)
|
||||
}
|
||||
|
||||
@@ -49,13 +49,7 @@ use runtime_io::Hashing;
|
||||
use runtime_support::StorageValue;
|
||||
use primitives::traits::{self, Header, Zero, One, Checkable, Applyable, CheckEqual, Executable, MakePayment};
|
||||
use codec::Slicable;
|
||||
|
||||
/// Compute the extrinsics root of a list of extrinsics.
|
||||
pub fn extrinsics_root<H: Hashing, E: Slicable>(extrinsics: &[E]) -> H::Output {
|
||||
let xts = extrinsics.iter().map(Slicable::encode).collect::<Vec<_>>();
|
||||
let xts = xts.iter().map(Vec::as_slice).collect::<Vec<_>>();
|
||||
H::enumerated_trie_root(&xts)
|
||||
}
|
||||
use system::extrinsics_root;
|
||||
|
||||
pub struct Executive<
|
||||
System,
|
||||
@@ -103,7 +97,7 @@ impl<
|
||||
|
||||
// execute transactions
|
||||
let (header, extrinsics) = block.deconstruct();
|
||||
extrinsics.into_iter().for_each(Self::apply_extrinsic);
|
||||
extrinsics.into_iter().for_each(Self::apply_extrinsic_inner);
|
||||
|
||||
// post-transactional book-keeping.
|
||||
Finalisation::execute();
|
||||
@@ -120,6 +114,9 @@ impl<
|
||||
pub fn finalise_block() -> System::Header {
|
||||
Finalisation::execute();
|
||||
|
||||
// setup extrinsics
|
||||
<system::Module<System>>::derive_extrinsics();
|
||||
|
||||
let header = <system::Module<System>>::finalise();
|
||||
Self::post_finalise(&header);
|
||||
|
||||
@@ -127,8 +124,16 @@ impl<
|
||||
}
|
||||
|
||||
/// Apply outside of the block execution function.
|
||||
/// This doesn't attempt to validate anything regarding the block.
|
||||
/// This doesn't attempt to validate anything regarding the block, but it builds a list of uxt
|
||||
/// hashes.
|
||||
pub fn apply_extrinsic(uxt: Block::Extrinsic) {
|
||||
<system::Module<System>>::note_extrinsic(uxt.encode());
|
||||
Self::apply_extrinsic_inner(uxt);
|
||||
}
|
||||
|
||||
/// Apply outside of the block execution function.
|
||||
/// This doesn't attempt to validate anything regarding the block.
|
||||
fn apply_extrinsic_inner(uxt: Block::Extrinsic) {
|
||||
// Verify the signature is good.
|
||||
let xt = match uxt.check() {
|
||||
Ok(xt) => xt,
|
||||
|
||||
@@ -47,6 +47,17 @@ use codec::Slicable;
|
||||
#[cfg(any(feature = "std", test))]
|
||||
use runtime_io::{twox_128, TestExternalities};
|
||||
|
||||
/// Compute the extrinsics root of a list of extrinsics.
|
||||
pub fn extrinsics_root<H: Hashing, E: codec::Slicable>(extrinsics: &[E]) -> H::Output {
|
||||
extrinsics_data_root::<H>(extrinsics.iter().map(codec::Slicable::encode).collect())
|
||||
}
|
||||
|
||||
/// Compute the extrinsics root of a list of extrinsics.
|
||||
pub fn extrinsics_data_root<H: Hashing>(xts: Vec<Vec<u8>>) -> H::Output {
|
||||
let xts = xts.iter().map(Vec::as_slice).collect::<Vec<_>>();
|
||||
H::enumerated_trie_root(&xts)
|
||||
}
|
||||
|
||||
pub trait Trait {
|
||||
type Index: Parameter + Default + SimpleArithmetic + Copy;
|
||||
type BlockNumber: Parameter + SimpleArithmetic + Default + Bounded + Copy;
|
||||
@@ -68,6 +79,7 @@ decl_storage! {
|
||||
pub BlockHash get(block_hash): b"sys:old" => required map [ T::BlockNumber => T::Hash ];
|
||||
|
||||
pub ExtrinsicIndex get(extrinsic_index): b"sys:xti" => required u32;
|
||||
pub ExtrinsicData get(extrinsic_data): b"sys:xtd" => required map [ u32 => Vec<u8> ];
|
||||
RandomSeed get(random_seed): b"sys:rnd" => required T::Hash;
|
||||
// The current block number being processed. Set by `execute_block`.
|
||||
Number get(block_number): b"sys:num" => required T::BlockNumber;
|
||||
@@ -170,6 +182,19 @@ impl<T: Trait> Module<T> {
|
||||
pub fn inc_account_index(who: &T::AccountId) {
|
||||
<AccountIndex<T>>::insert(who, Self::account_index(who) + T::Index::one());
|
||||
}
|
||||
|
||||
/// Note what the extrinsic data of the current extrinsic index is. If this is called, then
|
||||
/// ensure `derive_extrinsics` is also called before block-building is completed.
|
||||
pub fn note_extrinsic(encoded_xt: Vec<u8>) {
|
||||
<ExtrinsicData<T>>::insert(Self::extrinsic_index(), encoded_xt);
|
||||
}
|
||||
|
||||
/// Remove all extrinsics data and save the extrinsics trie root.
|
||||
pub fn derive_extrinsics() {
|
||||
let extrinsics = (0..Self::extrinsic_index()).map(<ExtrinsicData<T>>::take).collect();
|
||||
let xts_root = extrinsics_data_root::<T::Hashing>(extrinsics);
|
||||
<ExtrinsicsRoot<T>>::put(xts_root);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(feature = "std", test))]
|
||||
|
||||
BIN
Binary file not shown.
BIN
Binary file not shown.
Reference in New Issue
Block a user