Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Light friendly storage tracking: changes trie + extending over ranges #628

Merged
merged 25 commits into from
Sep 18, 2018
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
1962954
changes_trie
svyatonik Aug 15, 2018
17680a4
Merge branch 'master' into change_trie
svyatonik Aug 27, 2018
d24951f
changs_trie: continue
svyatonik Aug 27, 2018
6bbd7a6
changes_trie: adding tests
svyatonik Aug 28, 2018
fdfec3f
Merge branch 'master' into change_trie
svyatonik Aug 28, 2018
e30c6a2
fixed TODO
svyatonik Aug 28, 2018
4a18da0
removed obsolete ExtrinsicChanges
svyatonik Aug 29, 2018
8ba73e3
Merge branch 'master' into change_trie
svyatonik Aug 29, 2018
497e3ad
encodable ChangesTrieConfiguration
svyatonik Aug 30, 2018
2d0e4fa
Merge branch 'master' into change_trie
svyatonik Aug 30, 2018
42c8d59
removed polkadot fle
svyatonik Aug 31, 2018
6617af3
fixed grumbles
svyatonik Sep 3, 2018
24c810f
Merge branch 'master' into change_trie
svyatonik Sep 3, 2018
3354854
ext_storage_changes_root returns u32
svyatonik Sep 3, 2018
3368d14
Merge branch 'master' into change_trie
svyatonik Sep 11, 2018
78e8343
moved changes trie root to digest
svyatonik Sep 12, 2018
2a376f5
removed commented code
svyatonik Sep 12, 2018
902a8cc
Merge branch 'master' into change_trie
svyatonik Sep 12, 2018
8925838
Merge branch 'master' into change_trie
svyatonik Sep 12, 2018
6a03d58
read storage values from native code
svyatonik Sep 14, 2018
1cc7b06
Merge branch 'master' into change_trie
svyatonik Sep 14, 2018
51885f6
fixed grumbles
svyatonik Sep 17, 2018
825610e
fixed grumbles
svyatonik Sep 18, 2018
06aa5c8
Merge branch 'master' into change_trie
svyatonik Sep 18, 2018
b90e96d
missing comma
svyatonik Sep 18, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

110 changes: 67 additions & 43 deletions demo/executor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,17 @@ mod tests {
use codec::{Encode, Decode, Joiner};
use keyring::Keyring;
use runtime_support::{Hashable, StorageValue, StorageMap};
use state_machine::{CodeExecutor, TestExternalities};
use primitives::{twox_128, KeccakHasher};
use state_machine::{CodeExecutor, Externalities, TestExternalities};
use primitives::{twox_128, KeccakHasher, RlpCodec};
use demo_primitives::{Hash, BlockNumber, AccountId};
use runtime_primitives::traits::Header as HeaderT;
use runtime_primitives::{ApplyOutcome, ApplyError, ApplyResult, MaybeUnsigned};
use {staking, session, system, consensus};
use system::{EventRecord, Phase};
use demo_runtime::{Header, Block, UncheckedExtrinsic, Extrinsic, Call, Concrete, Staking,
BuildStorage, GenesisConfig, SessionConfig, StakingConfig, BareExtrinsic, System, Event};
BuildStorage, GenesisConfig, SessionConfig, StakingConfig, BareExtrinsic, System,
SystemConfig, Event};

use ed25519::{Public, Pair};

const BLOATY_CODE: &[u8] = include_bytes!("../../runtime/wasm/target/wasm32-unknown-unknown/release/demo_runtime.wasm");
Expand Down Expand Up @@ -93,7 +95,7 @@ mod tests {
}

fn from_block_number(n: u64) -> Header {
Header::new(n, Default::default(), Default::default(), [69; 32].into(), Default::default())
Header::new(n, Default::default(), Default::default(), Default::default(), [69; 32].into(), Default::default())
}

fn executor() -> ::substrate_executor::NativeExecutor<Executor> {
Expand All @@ -102,7 +104,7 @@ mod tests {

#[test]
fn panic_execution_with_foreign_code_gives_error() {
let mut t: TestExternalities<KeccakHasher> = map![
let mut t = TestExternalities::<KeccakHasher, RlpCodec>::new(map![
twox_128(&<staking::FreeBalance<Concrete>>::key_for(alice())).to_vec() => vec![69u8, 0, 0, 0, 0, 0, 0, 0],
twox_128(<staking::TotalStake<Concrete>>::key()).to_vec() => vec![69u8, 0, 0, 0, 0, 0, 0, 0],
twox_128(<staking::TransactionBaseFee<Concrete>>::key()).to_vec() => vec![70u8; 8],
Expand All @@ -111,7 +113,7 @@ mod tests {
twox_128(<staking::TransferFee<Concrete>>::key()).to_vec() => vec![0u8; 8],
twox_128(<staking::NextEnumSet<Concrete>>::key()).to_vec() => vec![0u8; 8],
twox_128(&<system::BlockHash<Concrete>>::key_for(0)).to_vec() => vec![0u8; 32]
];
]);

let r = executor().call(&mut t, BLOATY_CODE, "initialise_block", &vec![].and(&from_block_number(1u64)), true).0;
assert!(r.is_ok());
Expand All @@ -122,7 +124,7 @@ mod tests {

#[test]
fn bad_extrinsic_with_native_equivalent_code_gives_error() {
let mut t: TestExternalities<KeccakHasher> = map![
let mut t = TestExternalities::<KeccakHasher, RlpCodec>::new(map![
twox_128(&<staking::FreeBalance<Concrete>>::key_for(alice())).to_vec() => vec![69u8, 0, 0, 0, 0, 0, 0, 0],
twox_128(<staking::TotalStake<Concrete>>::key()).to_vec() => vec![69u8, 0, 0, 0, 0, 0, 0, 0],
twox_128(<staking::TransactionBaseFee<Concrete>>::key()).to_vec() => vec![70u8; 8],
Expand All @@ -131,7 +133,7 @@ mod tests {
twox_128(<staking::TransferFee<Concrete>>::key()).to_vec() => vec![0u8; 8],
twox_128(<staking::NextEnumSet<Concrete>>::key()).to_vec() => vec![0u8; 8],
twox_128(&<system::BlockHash<Concrete>>::key_for(0)).to_vec() => vec![0u8; 32]
];
]);

let r = executor().call(&mut t, COMPACT_CODE, "initialise_block", &vec![].and(&from_block_number(1u64)), true).0;
assert!(r.is_ok());
Expand All @@ -142,7 +144,7 @@ mod tests {

#[test]
fn successful_execution_with_native_equivalent_code_gives_ok() {
let mut t: TestExternalities<KeccakHasher> = map![
let mut t = TestExternalities::<KeccakHasher, RlpCodec>::new(map![
twox_128(&<staking::FreeBalance<Concrete>>::key_for(alice())).to_vec() => vec![111u8, 0, 0, 0, 0, 0, 0, 0],
twox_128(<staking::TotalStake<Concrete>>::key()).to_vec() => vec![111u8, 0, 0, 0, 0, 0, 0, 0],
twox_128(<staking::TransactionBaseFee<Concrete>>::key()).to_vec() => vec![0u8; 8],
Expand All @@ -151,7 +153,7 @@ mod tests {
twox_128(<staking::TransferFee<Concrete>>::key()).to_vec() => vec![0u8; 8],
twox_128(<staking::NextEnumSet<Concrete>>::key()).to_vec() => vec![0u8; 8],
twox_128(&<system::BlockHash<Concrete>>::key_for(0)).to_vec() => vec![0u8; 32]
];
]);

let r = executor().call(&mut t, COMPACT_CODE, "initialise_block", &vec![].and(&from_block_number(1u64)), true).0;
assert!(r.is_ok());
Expand All @@ -166,7 +168,7 @@ mod tests {

#[test]
fn successful_execution_with_foreign_code_gives_ok() {
let mut t: TestExternalities<KeccakHasher> = map![
let mut t = TestExternalities::<KeccakHasher, RlpCodec>::new(map![
twox_128(&<staking::FreeBalance<Concrete>>::key_for(alice())).to_vec() => vec![111u8, 0, 0, 0, 0, 0, 0, 0],
twox_128(<staking::TotalStake<Concrete>>::key()).to_vec() => vec![111u8, 0, 0, 0, 0, 0, 0, 0],
twox_128(<staking::TransactionBaseFee<Concrete>>::key()).to_vec() => vec![0u8; 8],
Expand All @@ -175,7 +177,7 @@ mod tests {
twox_128(<staking::TransferFee<Concrete>>::key()).to_vec() => vec![0u8; 8],
twox_128(<staking::NextEnumSet<Concrete>>::key()).to_vec() => vec![0u8; 8],
twox_128(&<system::BlockHash<Concrete>>::key_for(0)).to_vec() => vec![0u8; 32]
];
]);

let r = executor().call(&mut t, BLOATY_CODE, "initialise_block", &vec![].and(&from_block_number(1u64)), true).0;
assert!(r.is_ok());
Expand All @@ -188,12 +190,16 @@ mod tests {
});
}

fn new_test_ext() -> TestExternalities<KeccakHasher> {
fn new_test_ext(support_changes_trie: bool) -> TestExternalities<KeccakHasher, RlpCodec> {
use keyring::Keyring::*;
let three = [3u8; 32].into();
GenesisConfig {
TestExternalities::new(GenesisConfig {
consensus: Some(Default::default()),
system: Some(Default::default()),
system: Some(SystemConfig {
changes_trie_enabled: support_changes_trie,
changes_trie_digest_interval: Some(2),
changes_trie_digest_levels: Some(2),
}),
session: Some(SessionConfig {
session_length: 2,
validators: vec![One.to_raw_public().into(), Two.to_raw_public().into(), three],
Expand All @@ -219,10 +225,10 @@ mod tests {
democracy: Some(Default::default()),
council: Some(Default::default()),
timestamp: Some(Default::default()),
}.build_storage().unwrap().into()
}.build_storage().unwrap())
}

fn construct_block(number: BlockNumber, parent_hash: Hash, state_root: Hash, extrinsics: Vec<BareExtrinsic>) -> (Vec<u8>, Hash) {
fn construct_block(number: BlockNumber, parent_hash: Hash, state_root: Hash, changes_root: Option<Hash>, extrinsics: Vec<BareExtrinsic>) -> (Vec<u8>, Hash) {
use triehash::ordered_trie_root;

let extrinsics = extrinsics.into_iter().map(|extrinsic| {
Expand All @@ -242,6 +248,7 @@ mod tests {
parent_hash,
number,
state_root,
changes_root,
extrinsics_root,
digest: Default::default(),
};
Expand All @@ -250,14 +257,20 @@ mod tests {
(Block { header, extrinsics }.encode(), hash.into())
}

fn block1() -> (Vec<u8>, Hash) {
fn block1(support_changes_trie: bool) -> (Vec<u8>, Hash) {
construct_block(
1,
[69u8; 32].into(),
// Blake
// hex!("3437bf4b182ab17bb322af5c67e55f6be487a77084ad2b4e27ddac7242e4ad21").into(),
// Keccak
hex!("56c9a542e48ccf4e0821de301ea4384e87425604278b12a9db31c6d4e89ca51e").into(),
if support_changes_trie {
hex!("4011519eba01902e40392378ebc85375bf780acb68f44ebbf311ee68d4e76732").into()
} else {
hex!("3b57e52e95c1a839ce4a1a54761fb7f28c90f74d2cafdbf52265646384d74528").into()
},
if support_changes_trie {
Some(hex!("ba0c0313a9812380261438c1cbf38a9c75d5a7628d29193393a17ae7b7181dcd").into())
} else {
None
},
vec![BareExtrinsic {
signed: alice(),
index: 0,
Expand All @@ -269,11 +282,9 @@ mod tests {
fn block2() -> (Vec<u8>, Hash) {
construct_block(
2,
block1().1,
// Blake
// hex!("741fcb660e6fa9f625fbcd993b49f6c1cc4040f5e0cc8727afdedf11fd3c464b").into(),
// Keccak
hex!("166a2593d35f2d1bc87eca8cf2e320ed06759000a02aa88e51fa85b12c6f1267").into(),
block1(false).1,
hex!("e4451178254b2c0fd193c1613aa57a43f32aa9bdd6e179ae32150ee2bf5af0ee").into(),
None,
vec![
BareExtrinsic {
signed: bob(),
Expand All @@ -293,10 +304,8 @@ mod tests {
construct_block(
1,
[69u8; 32].into(),
// Blake
// hex!("2c7231a9c210a7aa4bea169d944bc4aaacd517862b244b8021236ffa7f697991").into(),
// Keccak
hex!("be186810570e437f0d803493fced9879207b064a0701fd8d68662b9563b4d33e").into(),
hex!("a5579b8696887b5a86b9adca9528af1f7e5a183f97a5e5052a904571b5323002").into(),
None,
vec![BareExtrinsic {
signed: alice(),
index: 0,
Expand All @@ -307,9 +316,9 @@ mod tests {

#[test]
fn full_native_block_import_works() {
let mut t = new_test_ext();
let mut t = new_test_ext(false);

executor().call(&mut t, COMPACT_CODE, "execute_block", &block1().0, true).0.unwrap();
executor().call(&mut t, COMPACT_CODE, "execute_block", &block1(false).0, true).0.unwrap();

runtime_io::with_externalities(&mut t, || {
assert_eq!(Staking::voting_balance(&alice()), 41);
Expand Down Expand Up @@ -342,9 +351,8 @@ mod tests {

#[test]
fn full_wasm_block_import_works() {
let mut t = new_test_ext();

WasmExecutor::new(8).call(&mut t, COMPACT_CODE, "execute_block", &block1().0).unwrap();
let mut t = new_test_ext(false);
WasmExecutor::new(8).call(&mut t, COMPACT_CODE, "execute_block", &block1(false).0).unwrap();

runtime_io::with_externalities(&mut t, || {
assert_eq!(Staking::voting_balance(&alice()), 41);
Expand All @@ -361,31 +369,31 @@ mod tests {

#[test]
fn wasm_big_block_import_fails() {
let mut t = new_test_ext();
let mut t = new_test_ext(false);

let r = WasmExecutor::new(8).call(&mut t, COMPACT_CODE, "execute_block", &block1big().0);
assert!(!r.is_ok());
}

#[test]
fn native_big_block_import_succeeds() {
let mut t = new_test_ext();
let mut t = new_test_ext(false);

let r = Executor::with_heap_pages(8).call(&mut t, COMPACT_CODE, "execute_block", &block1big().0, true).0;
assert!(r.is_ok());
}

#[test]
fn native_big_block_import_fails_on_fallback() {
let mut t = new_test_ext();
let mut t = new_test_ext(false);

let r = Executor::with_heap_pages(8).call(&mut t, COMPACT_CODE, "execute_block", &block1big().0, false).0;
assert!(!r.is_ok());
}

#[test]
fn panic_execution_gives_error() {
let mut t: TestExternalities<KeccakHasher> = map![
let mut t = TestExternalities::<KeccakHasher, RlpCodec>::new(map![
twox_128(&<staking::FreeBalance<Concrete>>::key_for(alice())).to_vec() => vec![69u8, 0, 0, 0, 0, 0, 0, 0],
twox_128(<staking::TotalStake<Concrete>>::key()).to_vec() => vec![69u8, 0, 0, 0, 0, 0, 0, 0],
twox_128(<staking::TransactionBaseFee<Concrete>>::key()).to_vec() => vec![70u8; 8],
Expand All @@ -394,7 +402,7 @@ mod tests {
twox_128(<staking::TransferFee<Concrete>>::key()).to_vec() => vec![0u8; 8],
twox_128(<staking::NextEnumSet<Concrete>>::key()).to_vec() => vec![0u8; 8],
twox_128(&<system::BlockHash<Concrete>>::key_for(0)).to_vec() => vec![0u8; 32]
];
]);

let foreign_code = include_bytes!("../../runtime/wasm/target/wasm32-unknown-unknown/release/demo_runtime.wasm");
let r = WasmExecutor::new(8).call(&mut t, &foreign_code[..], "initialise_block", &vec![].and(&from_block_number(1u64)));
Expand All @@ -406,7 +414,7 @@ mod tests {

#[test]
fn successful_execution_gives_ok() {
let mut t: TestExternalities<KeccakHasher> = map![
let mut t = TestExternalities::<KeccakHasher, RlpCodec>::new(map![
twox_128(&<staking::FreeBalance<Concrete>>::key_for(alice())).to_vec() => vec![111u8, 0, 0, 0, 0, 0, 0, 0],
twox_128(<staking::TotalStake<Concrete>>::key()).to_vec() => vec![111u8, 0, 0, 0, 0, 0, 0, 0],
twox_128(<staking::TransactionBaseFee<Concrete>>::key()).to_vec() => vec![0u8; 8],
Expand All @@ -415,7 +423,7 @@ mod tests {
twox_128(<staking::TransferFee<Concrete>>::key()).to_vec() => vec![0u8; 8],
twox_128(<staking::NextEnumSet<Concrete>>::key()).to_vec() => vec![0u8; 8],
twox_128(&<system::BlockHash<Concrete>>::key_for(0)).to_vec() => vec![0u8; 32]
];
]);

let foreign_code = include_bytes!("../../runtime/wasm/target/wasm32-unknown-unknown/release/demo_runtime.compact.wasm");
let r = WasmExecutor::new(8).call(&mut t, &foreign_code[..], "initialise_block", &vec![].and(&from_block_number(1u64)));
Expand All @@ -429,4 +437,20 @@ mod tests {
assert_eq!(Staking::voting_balance(&bob()), 69);
});
}

#[test]
fn full_native_block_import_works_with_changes_trie() {
let mut t = new_test_ext(true);
executor().call(&mut t, COMPACT_CODE, "execute_block", &block1(true).0, true).0.unwrap();

assert!(t.storage_changes_root().is_some());
}

#[test]
fn full_wasm_block_import_works_with_changes_trie() {
let mut t = new_test_ext(true);
WasmExecutor::new(8).call(&mut t, COMPACT_CODE, "execute_block", &block1(true).0).unwrap();

assert!(t.storage_changes_root().is_some());
}
}
1 change: 1 addition & 0 deletions demo/runtime/wasm/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
Binary file not shown.
Loading