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

Commit

Permalink
Optional storage entries (#75)
Browse files Browse the repository at this point in the history
* Block builder (substrate)

* Fix wasm build

* Bulid on any block

* Test for block builder.

* Block import tests for client.

* Tidy ups

* Repotted client

* Avoid pointless work

* All backend stuff now manages optional storage.

Also simplified a lot of the backend.

* Native runtime-io now supports empty storage items.

* Finish up the API transition.

* Build fix.

* Fix tests.

* Remerge in changes to client.

* Final fixes.

* Unrevert typos

* Remove accidentally committed change

* Bring back zero copy

* Fix merge.
  • Loading branch information
gavofyork authored and rphmeier committed Feb 19, 2018
1 parent 8efd1ea commit 305b76f
Show file tree
Hide file tree
Showing 32 changed files with 856 additions and 737 deletions.
28 changes: 14 additions & 14 deletions polkadot/executor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ mod tests {
#[test]
fn panic_execution_with_foreign_code_gives_error() {
let one = Keyring::One.to_raw_public();
let mut t = TestExternalities { storage: map![
let mut t: TestExternalities = map![
twox_128(&one.to_keyed_vec(b"sta:bal:")).to_vec() => vec![68u8, 0, 0, 0, 0, 0, 0, 0]
], };
];

let r = Executor::new().call(&mut t, BLOATY_CODE, "execute_transaction", &vec![].and(&Header::from_block_number(1u64)).and(&tx()));
assert!(r.is_err());
Expand All @@ -84,9 +84,9 @@ mod tests {
#[test]
fn panic_execution_with_native_equivalent_code_gives_error() {
let one = Keyring::One.to_raw_public();
let mut t = TestExternalities { storage: map![
let mut t: TestExternalities = map![
twox_128(&one.to_keyed_vec(b"sta:bal:")).to_vec() => vec![68u8, 0, 0, 0, 0, 0, 0, 0]
], };
];

let r = Executor::new().call(&mut t, COMPACT_CODE, "execute_transaction", &vec![].and(&Header::from_block_number(1u64)).and(&tx()));
assert!(r.is_err());
Expand All @@ -97,9 +97,9 @@ mod tests {
let one = Keyring::One.to_raw_public();
let two = Keyring::Two.to_raw_public();

let mut t = TestExternalities { storage: map![
let mut t: TestExternalities = map![
twox_128(&one.to_keyed_vec(b"sta:bal:")).to_vec() => vec![111u8, 0, 0, 0, 0, 0, 0, 0]
], };
];

let r = Executor::new().call(&mut t, COMPACT_CODE, "execute_transaction", &vec![].and(&Header::from_block_number(1u64)).and(&tx()));
assert!(r.is_ok());
Expand All @@ -115,9 +115,9 @@ mod tests {
let one = Keyring::One.to_raw_public();
let two = Keyring::Two.to_raw_public();

let mut t = TestExternalities { storage: map![
let mut t: TestExternalities = map![
twox_128(&one.to_keyed_vec(b"sta:bal:")).to_vec() => vec![111u8, 0, 0, 0, 0, 0, 0, 0]
], };
];

let r = Executor::new().call(&mut t, BLOATY_CODE, "execute_transaction", &vec![].and(&Header::from_block_number(1u64)).and(&tx()));
assert!(r.is_ok());
Expand All @@ -133,7 +133,7 @@ mod tests {
let two = Keyring::Two.to_raw_public();
let three = [3u8; 32];

TestExternalities { storage: map![
map![
twox_128(&0u64.to_keyed_vec(b"sys:old:")).to_vec() => [69u8; 32].encode(),
twox_128(b"gov:apr").to_vec() => vec![].and(&667u32),
twox_128(b"ses:len").to_vec() => vec![].and(&2u64),
Expand All @@ -149,7 +149,7 @@ mod tests {
twox_128(b"sta:vac").to_vec() => vec![].and(&3u64),
twox_128(b"sta:era").to_vec() => vec![].and(&0u64),
twox_128(&one.to_keyed_vec(b"sta:bal:")).to_vec() => vec![111u8, 0, 0, 0, 0, 0, 0, 0]
], }
]
}

fn construct_block(number: BlockNumber, parent_hash: Hash, state_root: Hash, txs: Vec<Transaction>) -> (Vec<u8>, Hash) {
Expand Down Expand Up @@ -250,9 +250,9 @@ mod tests {
#[test]
fn panic_execution_gives_error() {
let one = Keyring::One.to_raw_public();
let mut t = TestExternalities { storage: map![
let mut t: TestExternalities = map![
twox_128(&one.to_keyed_vec(b"sta:bal:")).to_vec() => vec![68u8, 0, 0, 0, 0, 0, 0, 0]
], };
];

let foreign_code = include_bytes!("../../runtime/wasm/target/wasm32-unknown-unknown/release/polkadot_runtime.wasm");
let r = WasmExecutor.call(&mut t, &foreign_code[..], "execute_transaction", &vec![].and(&Header::from_block_number(1u64)).and(&tx()));
Expand All @@ -264,9 +264,9 @@ mod tests {
let one = Keyring::One.to_raw_public();
let two = Keyring::Two.to_raw_public();

let mut t = TestExternalities { storage: map![
let mut t: TestExternalities = map![
twox_128(&one.to_keyed_vec(b"sta:bal:")).to_vec() => vec![111u8, 0, 0, 0, 0, 0, 0, 0]
], };
];

let foreign_code = include_bytes!("../../runtime/wasm/target/wasm32-unknown-unknown/release/polkadot_runtime.compact.wasm");
let r = WasmExecutor.call(&mut t, &foreign_code[..], "execute_transaction", &vec![].and(&Header::from_block_number(1u64)).and(&tx()));
Expand Down
4 changes: 2 additions & 2 deletions polkadot/runtime/src/runtime/governance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ mod tests {
let two = Keyring::Two.to_raw_public();
let three = [3u8; 32];

TestExternalities { storage: map![
map![
twox_128(APPROVALS_REQUIRED).to_vec() => vec![].and(&667u32),
twox_128(b"ses:len").to_vec() => vec![].and(&1u64),
twox_128(b"ses:val:len").to_vec() => vec![].and(&3u32),
Expand All @@ -171,7 +171,7 @@ mod tests {
twox_128(b"sta:spe").to_vec() => vec![].and(&1u64),
twox_128(b"sta:vac").to_vec() => vec![].and(&3u64),
twox_128(b"sta:era").to_vec() => vec![].and(&1u64)
], }
]
}

#[test]
Expand Down
4 changes: 2 additions & 2 deletions polkadot/runtime/src/runtime/parachains.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ mod tests {
use runtime::{consensus, session};

fn simple_setup() -> TestExternalities {
TestExternalities { storage: map![
map![
twox_128(b"ses:val:len").to_vec() => vec![].and(&8u32),
twox_128(b"par:cou").to_vec() => vec![].and(&2u32)
], }
]
}

#[test]
Expand Down
4 changes: 2 additions & 2 deletions polkadot/runtime/src/runtime/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ mod tests {
use runtime::{consensus, session};

fn simple_setup() -> TestExternalities {
TestExternalities { storage: map![
map![
twox_128(SESSION_LENGTH).to_vec() => vec![].and(&2u64),
// the validators (10, 20, ...)
twox_128(b"ses:val:len").to_vec() => vec![].and(&2u32),
Expand All @@ -155,7 +155,7 @@ mod tests {
b":auth:len".to_vec() => vec![].and(&2u32),
0u32.to_keyed_vec(b":auth:") => vec![11; 32],
1u32.to_keyed_vec(b":auth:") => vec![21; 32]
], }
]
}

#[test]
Expand Down
20 changes: 10 additions & 10 deletions polkadot/runtime/src/runtime/staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ mod tests {
let three = [3u8; 32];
let four = [4u8; 32];

let mut t = TestExternalities { storage: map![
let mut t: TestExternalities = map![
twox_128(b"ses:len").to_vec() => vec![].and(&1u64),
twox_128(b"ses:val:len").to_vec() => vec![].and(&2u32),
twox_128(&0u32.to_keyed_vec(b"ses:val:")).to_vec() => vec![10; 32],
Expand All @@ -239,7 +239,7 @@ mod tests {
twox_128(&two.to_keyed_vec(BALANCE_OF)).to_vec() => vec![].and(&20u64),
twox_128(&three.to_keyed_vec(BALANCE_OF)).to_vec() => vec![].and(&30u64),
twox_128(&four.to_keyed_vec(BALANCE_OF)).to_vec() => vec![].and(&40u64)
], };
];

with_externalities(&mut t, || {
assert_eq!(era_length(), 2u64);
Expand Down Expand Up @@ -296,10 +296,10 @@ mod tests {

#[test]
fn staking_eras_work() {
let mut t = TestExternalities { storage: map![
let mut t: TestExternalities = map![
twox_128(b"ses:len").to_vec() => vec![].and(&1u64),
twox_128(SESSIONS_PER_ERA).to_vec() => vec![].and(&2u64)
], };
];
with_externalities(&mut t, || {
assert_eq!(era_length(), 2u64);
assert_eq!(sessions_per_era(), 2u64);
Expand Down Expand Up @@ -363,9 +363,9 @@ mod tests {
let one = Keyring::One.to_raw_public();
let two = Keyring::Two.to_raw_public();

let mut t = TestExternalities { storage: map![
let mut t: TestExternalities = map![
twox_128(&one.to_keyed_vec(BALANCE_OF)).to_vec() => vec![].and(&42u64)
], };
];

with_externalities(&mut t, || {
assert_eq!(balance(&one), 42);
Expand All @@ -378,9 +378,9 @@ mod tests {
let one = Keyring::One.to_raw_public();
let two = Keyring::Two.to_raw_public();

let mut t = TestExternalities { storage: map![
let mut t: TestExternalities = map![
twox_128(&one.to_keyed_vec(BALANCE_OF)).to_vec() => vec![].and(&111u64)
], };
];

with_externalities(&mut t, || {
transfer(&one, &two, 69);
Expand All @@ -395,9 +395,9 @@ mod tests {
let one = Keyring::One.to_raw_public();
let two = Keyring::Two.to_raw_public();

let mut t = TestExternalities { storage: map![
let mut t: TestExternalities = map![
twox_128(&one.to_keyed_vec(BALANCE_OF)).to_vec() => vec![].and(&111u64)
], };
];

with_externalities(&mut t, || {
stake(&one);
Expand Down
8 changes: 4 additions & 4 deletions polkadot/runtime/src/runtime/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,9 +247,9 @@ mod tests {
let one = Keyring::One.to_raw_public();
let two = Keyring::Two.to_raw_public();

let mut t = TestExternalities { storage: map![
let mut t: TestExternalities = map![
twox_128(&one.to_keyed_vec(b"sta:bal:")).to_vec() => vec![111u8, 0, 0, 0, 0, 0, 0, 0]
], };
];

let tx = UncheckedTransaction {
transaction: Transaction {
Expand All @@ -272,7 +272,7 @@ mod tests {
let two = Keyring::Two.to_raw_public();
let three = [3u8; 32];

TestExternalities { storage: map![
map![
twox_128(&0u64.to_keyed_vec(b"sys:old:")).to_vec() => [69u8; 32].encode(),
twox_128(b"gov:apr").to_vec() => vec![].and(&667u32),
twox_128(b"ses:len").to_vec() => vec![].and(&2u64),
Expand All @@ -288,7 +288,7 @@ mod tests {
twox_128(b"sta:vac").to_vec() => vec![].and(&3u64),
twox_128(b"sta:era").to_vec() => vec![].and(&0u64),
twox_128(&one.to_keyed_vec(b"sta:bal:")).to_vec() => vec![111u8, 0, 0, 0, 0, 0, 0, 0]
], }
]
}

#[test]
Expand Down
4 changes: 2 additions & 2 deletions polkadot/runtime/src/runtime/timestamp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ mod tests {

#[test]
fn timestamp_works() {
let mut t = TestExternalities { storage: map![
let mut t: TestExternalities = map![
twox_128(CURRENT_TIMESTAMP).to_vec() => vec![].and(&42u64)
], };
];

with_externalities(&mut t, || {
assert_eq!(get(), 42);
Expand Down
Binary file modified polkadot/runtime/wasm/genesis.wasm
Binary file not shown.
Binary file not shown.
Binary file not shown.
2 changes: 1 addition & 1 deletion substrate/client/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub trait BlockImportOperation {
/// Append block data to the transaction.
fn set_block_data(&mut self, header: block::Header, body: Option<block::Body>, justification: Option<primitives::bft::Justification>, is_new_best: bool) -> error::Result<()>;
/// Inject storage data into the database.
fn set_storage<I: Iterator<Item=(Vec<u8>, Vec<u8>)>>(&mut self, iter: I) -> error::Result<()>;
fn set_storage<I: Iterator<Item=(Vec<u8>, Option<Vec<u8>>)>>(&mut self, changes: I) -> error::Result<()>;
/// Inject storage data into the database.
fn reset_storage<I: Iterator<Item=(Vec<u8>, Vec<u8>)>>(&mut self, iter: I) -> error::Result<()>;
}
Expand Down
Loading

0 comments on commit 305b76f

Please sign in to comment.