mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-09 20:11:09 +00:00
Merge branch 'master' into author-relay-block
This commit is contained in:
@@ -13,9 +13,10 @@ pwasm-libc = { path = "../wasm-runtime/pwasm-libc", version = "0.1" }
|
||||
environmental = { path = "../environmental", version = "0.1", optional = true }
|
||||
polkadot-state-machine = { path = "../state-machine", version = "0.1", optional = true }
|
||||
polkadot-primitives = { path = "../primitives", version = "0.1", optional = true }
|
||||
triehash = { version = "0.1", optional = true }
|
||||
|
||||
[features]
|
||||
default = ["std"]
|
||||
std = ["environmental", "polkadot-state-machine", "polkadot-primitives"]
|
||||
std = ["environmental", "polkadot-state-machine", "polkadot-primitives", "triehash"]
|
||||
nightly = []
|
||||
strict = []
|
||||
|
||||
@@ -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;
|
||||
@@ -31,7 +32,7 @@ pub use std::boxed;
|
||||
pub use std::slice;
|
||||
pub use std::mem;
|
||||
|
||||
pub use polkadot_state_machine::{Externalities, ExternalitiesError};
|
||||
pub use polkadot_state_machine::{Externalities, ExternalitiesError, TestExternalities};
|
||||
use primitives::hexdisplay::HexDisplay;
|
||||
|
||||
// TODO: use the real error, not NoError.
|
||||
@@ -84,6 +85,18 @@ pub fn chain_id() -> u64 {
|
||||
).unwrap_or(0)
|
||||
}
|
||||
|
||||
/// "Commit" all existing operations and get the resultant storage root.
|
||||
pub fn storage_root() -> [u8; 32] {
|
||||
ext::with(|ext|
|
||||
ext.storage_root()
|
||||
).unwrap_or([0u8; 32])
|
||||
}
|
||||
|
||||
/// "Commit" all existing operations and get the resultant storage root.
|
||||
pub fn enumerated_trie_root(serialised_values: &[&[u8]]) -> [u8; 32] {
|
||||
triehash::ordered_trie_root(serialised_values.iter().map(|s| s.to_vec())).0
|
||||
}
|
||||
|
||||
/// Conduct a Keccak-256 hash of the given data.
|
||||
pub use primitives::{blake2_256, twox_128, twox_256};
|
||||
|
||||
@@ -134,23 +147,6 @@ macro_rules! impl_stubs {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
struct TestExternalities {
|
||||
storage: HashMap<Vec<u8>, Vec<u8>>,
|
||||
}
|
||||
impl Externalities for TestExternalities {
|
||||
fn storage(&self, key: &[u8]) -> Result<&[u8], ExternalitiesError> {
|
||||
Ok(self.storage.get(&key.to_vec()).map_or(&[] as &[u8], Vec::as_slice))
|
||||
}
|
||||
|
||||
fn set_storage(&mut self, key: Vec<u8>, value: Vec<u8>) {
|
||||
self.storage.insert(key, value);
|
||||
}
|
||||
|
||||
fn chain_id(&self) -> u64 { 42 }
|
||||
}
|
||||
|
||||
macro_rules! map {
|
||||
($( $name:expr => $value:expr ),*) => (
|
||||
|
||||
@@ -33,6 +33,8 @@ extern "C" {
|
||||
fn ext_set_storage(key_data: *const u8, key_len: u32, value_data: *const u8, value_len: u32);
|
||||
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);
|
||||
@@ -67,6 +69,30 @@ pub fn read_storage(key: &[u8], value_out: &mut [u8], value_offset: usize) -> us
|
||||
}
|
||||
}
|
||||
|
||||
/// The current storage's root.
|
||||
pub fn storage_root() -> [u8; 32] {
|
||||
let mut result: [u8; 32] = Default::default();
|
||||
unsafe {
|
||||
ext_storage_root(result.as_mut_ptr());
|
||||
}
|
||||
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 {
|
||||
@@ -77,7 +103,6 @@ pub fn chain_id() -> u64 {
|
||||
/// Conduct a 256-bit Blake2 hash.
|
||||
pub fn blake2_256(data: &[u8]) -> [u8; 32] {
|
||||
let mut result: [u8; 32] = Default::default();
|
||||
// guaranteed to write into result.
|
||||
unsafe {
|
||||
ext_blake2_256(data.as_ptr(), data.len() as u32, result.as_mut_ptr());
|
||||
}
|
||||
@@ -87,7 +112,6 @@ pub fn blake2_256(data: &[u8]) -> [u8; 32] {
|
||||
/// Conduct four XX hashes to give a 256-bit result.
|
||||
pub fn twox_256(data: &[u8]) -> [u8; 32] {
|
||||
let mut result: [u8; 32] = Default::default();
|
||||
// guaranteed to write into result.
|
||||
unsafe {
|
||||
ext_twox_256(data.as_ptr(), data.len() as u32, result.as_mut_ptr());
|
||||
}
|
||||
@@ -97,7 +121,6 @@ pub fn twox_256(data: &[u8]) -> [u8; 32] {
|
||||
/// Conduct two XX hashes to give a 128-bit result.
|
||||
pub fn twox_128(data: &[u8]) -> [u8; 16] {
|
||||
let mut result: [u8; 16] = Default::default();
|
||||
// guaranteed to write into result.
|
||||
unsafe {
|
||||
ext_twox_128(data.as_ptr(), data.len() as u32, result.as_mut_ptr());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user