mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-15 19:41:05 +00:00
Implement enumerated trie root throughout.
This commit is contained in:
Generated
+2
-1
@@ -986,7 +986,6 @@ dependencies = [
|
||||
"assert_matches 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"byteorder 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"error-chain 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"libc 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"native-runtime 0.1.0",
|
||||
"parity-wasm 0.15.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"polkadot-primitives 0.1.0",
|
||||
@@ -996,6 +995,7 @@ dependencies = [
|
||||
"rustc-hex 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde 1.0.20 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde_derive 1.0.20 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"triehash 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -1235,6 +1235,7 @@ dependencies = [
|
||||
"parking_lot 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"polkadot-primitives 0.1.0",
|
||||
"polkadot-state-machine 0.1.0",
|
||||
"triehash 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
||||
@@ -15,7 +15,7 @@ byteorder = "1.1"
|
||||
rustc-hex = "1.0.0"
|
||||
native-runtime = { path = "../native-runtime", version = "0.1" }
|
||||
runtime-std = { path = "../native-runtime/std", version = "0.1" }
|
||||
libc = { version = "0.2.33" }
|
||||
triehash = "0.1.0"
|
||||
|
||||
[dev-dependencies]
|
||||
assert_matches = "1.1"
|
||||
|
||||
@@ -36,7 +36,7 @@ extern crate byteorder;
|
||||
extern crate rustc_hex;
|
||||
extern crate native_runtime;
|
||||
extern crate runtime_std;
|
||||
extern crate libc;
|
||||
extern crate triehash;
|
||||
|
||||
#[macro_use]
|
||||
extern crate error_chain;
|
||||
|
||||
@@ -29,6 +29,7 @@ use wasm_utils::{MemoryInstance, UserDefinedElements,
|
||||
AddModuleWithoutFullDependentInstance};
|
||||
use primitives::{ed25519, blake2_256, twox_128, twox_256};
|
||||
use primitives::hexdisplay::HexDisplay;
|
||||
use triehash::ordered_trie_root;
|
||||
|
||||
struct Heap {
|
||||
end: u32,
|
||||
@@ -78,6 +79,17 @@ impl WritePrimitive<u32> for MemoryInstance {
|
||||
}
|
||||
}
|
||||
|
||||
trait ReadPrimitive<T: Sized> {
|
||||
fn read_primitive(&self, offset: u32) -> ::std::result::Result<T, DummyUserError>;
|
||||
}
|
||||
|
||||
impl ReadPrimitive<u32> for MemoryInstance {
|
||||
fn read_primitive(&self, offset: u32) -> ::std::result::Result<u32, DummyUserError> {
|
||||
use byteorder::{LittleEndian, ByteOrder};
|
||||
Ok(LittleEndian::read_u32(&self.get(offset, 4).map_err(|_| DummyUserError)?))
|
||||
}
|
||||
}
|
||||
|
||||
impl_function_executor!(this: FunctionExecutor<'e, E>,
|
||||
ext_print_utf8(utf8_data: *const u8, utf8_len: u32) => {
|
||||
if let Ok(utf8) = this.memory.get(utf8_data, utf8_len as usize) {
|
||||
@@ -157,6 +169,20 @@ impl_function_executor!(this: FunctionExecutor<'e, E>,
|
||||
let r = this.ext.storage_root();
|
||||
this.memory.set(result, &r[..]).map_err(|_| DummyUserError)?;
|
||||
},
|
||||
ext_enumerated_trie_root(values_data: *const u8, values_len: u32, lens_data: *const u32, lens_len: u32, result: *mut u8) => {
|
||||
let offsets = (0..lens_len)
|
||||
.map(|i| this.memory.read_primitive(lens_data + i * 4))
|
||||
.collect::<::std::result::Result<Vec<u32>, DummyUserError>>()?;
|
||||
let values = offsets.into_iter()
|
||||
.scan(0u32, |acc, v| { let o = *acc; *acc += v; Some((o, v)) })
|
||||
.map(|(offset, len)|
|
||||
this.memory.get(values_data + offset, len as usize)
|
||||
.map_err(|_| DummyUserError)
|
||||
)
|
||||
.collect::<::std::result::Result<Vec<_>, DummyUserError>>()?;
|
||||
let r = ordered_trie_root(values.into_iter());
|
||||
this.memory.set(result, &r[..]).map_err(|_| DummyUserError)?;
|
||||
},
|
||||
ext_chain_id() -> u64 => {
|
||||
this.ext.chain_id()
|
||||
},
|
||||
@@ -356,6 +382,16 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn enumerated_trie_root_should_work() {
|
||||
let mut ext = TestExternalities::default();
|
||||
let test_code = include_bytes!("../../wasm-runtime/target/wasm32-unknown-unknown/release/runtime_test.compact.wasm");
|
||||
assert_eq!(
|
||||
WasmExecutor.call(&mut ext, &test_code[..], "test_enumerated_trie_root", &CallData(vec![])).unwrap(),
|
||||
vec![0]
|
||||
);
|
||||
}
|
||||
|
||||
fn tx() -> Vec<u8> { "679fcf0a846b4224c84ecad7d91a26241c46d00cb53d6480a363274e8965ee34b0b80b4b2e3836d3d8f8f12c0c1aef7350af587d9aee3883561d11726068ac0a2f8c6129d816cf51c374bc7f08c3e63ed156cf78aefb4a6550d97b87997977ee00000000000000000228000000d75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f707511a4500000000000000".convert() }
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -12,3 +12,4 @@ parking_lot = "0.5"
|
||||
polkadot-state-machine = { path = "../../state_machine" , version = "0.1" }
|
||||
environmental = { path = "../../environmental", version = "0.1.0" }
|
||||
polkadot-primitives = { path = "../../primitives", version = "0.1.0" }
|
||||
triehash = "0.1.0"
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
extern crate environmental;
|
||||
extern crate polkadot_state_machine;
|
||||
extern crate polkadot_primitives as primitives;
|
||||
extern crate triehash;
|
||||
|
||||
use std::fmt;
|
||||
use primitives::ed25519;
|
||||
@@ -99,6 +100,13 @@ pub fn storage_root() -> [u8; 32] {
|
||||
).unwrap_or([0u8; 32])
|
||||
}
|
||||
|
||||
/// "Commit" all existing operations and get the resultant storage root.
|
||||
pub fn enumerated_trie_root(serialised_values: &[&[u8]]) -> [u8; 32] {
|
||||
ext::with(|ext|
|
||||
triehash::ordered_trie_root(serialised_values.iter().map(|s| s.to_vec())).0
|
||||
).unwrap_or([0u8; 32])
|
||||
}
|
||||
|
||||
/// Conduct a Keccak-256 hash of the given data.
|
||||
pub use primitives::{blake2_256, twox_128, twox_256};
|
||||
|
||||
|
||||
@@ -25,6 +25,7 @@ extern crate runtime_std;
|
||||
#[cfg(feature = "with-std")]
|
||||
extern crate rustc_hex;
|
||||
|
||||
#[cfg(test)]
|
||||
#[macro_use]
|
||||
extern crate hex_literal;
|
||||
|
||||
|
||||
@@ -18,8 +18,8 @@
|
||||
//! and depositing logs.
|
||||
|
||||
use runtime_std::prelude::*;
|
||||
use runtime_std::{mem, print, storage_root};
|
||||
use codec::KeyedVec;
|
||||
use runtime_std::{mem, print, storage_root, enumerated_trie_root};
|
||||
use codec::{KeyedVec, Slicable};
|
||||
use support::{Hashable, storage, with_env};
|
||||
use primitives::{Block, BlockNumber, Hash, UncheckedTransaction, TxOrder};
|
||||
use runtime::{staking, session};
|
||||
@@ -74,9 +74,11 @@ pub mod internal {
|
||||
"Parent hash should be valid."
|
||||
);
|
||||
|
||||
// TODO: check transaction trie root represents the transactions.
|
||||
// this requires non-trivial changes to the externals API or compiling trie rooting into wasm
|
||||
// so will wait until a little later.
|
||||
// check transaction trie root represents the transactions.
|
||||
let txs = block.transactions.iter().map(Slicable::to_vec).collect::<Vec<_>>();
|
||||
let txs_root = enumerated_trie_root(&txs.iter().map(Vec::as_slice).collect::<Vec<_>>());
|
||||
// println!("TR: {}", ::support::HexDisplay::from(&txs_root));
|
||||
assert!(header.transaction_root == txs_root, "Transaction trie root must be valid.");
|
||||
|
||||
// execute transactions
|
||||
block.transactions.iter().for_each(execute_transaction);
|
||||
@@ -87,7 +89,6 @@ pub mod internal {
|
||||
// any final checks
|
||||
final_checks(&block);
|
||||
|
||||
// println!("SR: {}", ::support::HexDisplay::from(&storage_root()));
|
||||
|
||||
// check storage root.
|
||||
assert!(header.state_root == storage_root(), "Storage root must match that calculated.");
|
||||
@@ -210,7 +211,7 @@ mod tests {
|
||||
parent_hash: [69u8; 32],
|
||||
number: 1,
|
||||
state_root: hex!("2481853da20b9f4322f34650fea5f240dcbfb266d02db94bfa0153c31f4a29db"),
|
||||
transaction_root: [0u8; 32], // Unchecked currently.
|
||||
transaction_root: hex!("91fab88ad8c30a6d05ad8e0cf9ab139bf1b8cdddc69abd51cdfa6d2699038af1"),
|
||||
digest: Digest { logs: vec![], },
|
||||
};
|
||||
|
||||
|
||||
@@ -43,6 +43,7 @@ extern "C" {
|
||||
fn ext_get_allocated_storage(key_data: *const u8, key_len: u32, written_out: *mut u32) -> *mut u8;
|
||||
fn ext_get_storage_into(key_data: *const u8, key_len: u32, value_data: *mut u8, value_len: u32, value_offset: u32) -> u32;
|
||||
fn ext_storage_root(result: *mut u8);
|
||||
fn ext_enumerated_trie_root(values_data: *const u8, values_len: u32, lens_data: *const u32, lens_len: u32, result: *mut u8);
|
||||
fn ext_chain_id() -> u64;
|
||||
fn ext_blake2_256(data: *const u8, len: u32, out: *mut u8);
|
||||
fn ext_twox_128(data: *const u8, len: u32, out: *mut u8);
|
||||
@@ -82,6 +83,21 @@ pub fn storage_root() -> [u8; 32] {
|
||||
result
|
||||
}
|
||||
|
||||
/// A trie root calculated from enumerated values.
|
||||
pub fn enumerated_trie_root(values: &[&[u8]]) -> [u8; 32] {
|
||||
let lens = values.iter().map(|v| (v.len() as u32).to_le()).collect::<Vec<_>>();
|
||||
let values = values.iter().fold(Vec::new(), |mut acc, sl| { acc.extend_from_slice(sl); acc });
|
||||
let mut result: [u8; 32] = Default::default();
|
||||
unsafe {
|
||||
ext_enumerated_trie_root(
|
||||
values.as_ptr(), values.len() as u32,
|
||||
lens.as_ptr(), lens.len() as u32,
|
||||
result.as_mut_ptr()
|
||||
);
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
/// The current relay chain identifier.
|
||||
pub fn chain_id() -> u64 {
|
||||
unsafe {
|
||||
|
||||
BIN
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user