mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-26 14:37:57 +00:00
Remove requirement on Hash = H256, make Proposer return StorageChanges and Proof (#3860)
* Extend `Proposer` to optionally generate a proof of the proposal * Something * Refactor sr-api to not depend on client anymore * Fix benches * Apply suggestions from code review Co-Authored-By: Tomasz Drwięga <tomusdrw@users.noreply.github.com> * Apply suggestions from code review * Introduce new `into_storage_changes` function * Switch to runtime api for `execute_block` and don't require `H256` anywhere in the code * Put the `StorageChanges` into the `Proposal` * Move the runtime api error to its own trait * Adds `StorageTransactionCache` to the runtime api This requires that we add `type NodeBlock = ` to the `impl_runtime_apis!` macro to work around some bugs in rustc :( * Remove `type NodeBlock` and switch to a "better" hack * Start using the transaction cache from the runtime api * Make it compile * Move `InMemory` to its own file * Make all tests work again * Return block, storage_changes and proof from Blockbuilder::bake() * Make sure that we use/set `storage_changes` when possible * Add test * Fix deadlock * Remove accidentally added folders * Introduce `RecordProof` as argument type to be more explicit * Update client/src/client.rs Co-Authored-By: Tomasz Drwięga <tomusdrw@users.noreply.github.com> * Update primitives/state-machine/src/ext.rs Co-Authored-By: Tomasz Drwięga <tomusdrw@users.noreply.github.com> * Integrates review feedback * Remove `unsafe` usage * Update client/block-builder/src/lib.rs Co-Authored-By: Benjamin Kampmann <ben@gnunicorn.org> * Update client/src/call_executor.rs * Bump versions Co-authored-by: Tomasz Drwięga <tomusdrw@users.noreply.github.com> Co-authored-by: Benjamin Kampmann <ben.kampmann@googlemail.com>
This commit is contained in:
@@ -20,12 +20,12 @@
|
||||
use sp_std::prelude::*;
|
||||
use sp_io::{
|
||||
storage::root as storage_root, storage::changes_root as storage_changes_root,
|
||||
hashing::blake2_256,
|
||||
hashing::blake2_256, trie,
|
||||
};
|
||||
use frame_support::storage;
|
||||
use frame_support::{decl_storage, decl_module};
|
||||
use sp_runtime::{
|
||||
traits::{Hash as HashT, BlakeTwo256, Header as _}, generic, ApplyExtrinsicResult,
|
||||
traits::Header as _, generic, ApplyExtrinsicResult,
|
||||
transaction_validity::{
|
||||
TransactionValidity, ValidTransaction, InvalidTransaction, TransactionValidityError,
|
||||
},
|
||||
@@ -109,60 +109,36 @@ pub fn execute_block(mut block: Block) {
|
||||
execute_block_with_state_root_handler(&mut block, Mode::Verify);
|
||||
}
|
||||
|
||||
fn execute_block_with_state_root_handler(
|
||||
block: &mut Block,
|
||||
mode: Mode,
|
||||
) {
|
||||
fn execute_block_with_state_root_handler(block: &mut Block, mode: Mode) {
|
||||
let header = &mut block.header;
|
||||
|
||||
// check transaction trie root represents the transactions.
|
||||
let txs = block.extrinsics.iter().map(Encode::encode).collect::<Vec<_>>();
|
||||
let txs_root = BlakeTwo256::ordered_trie_root(txs);
|
||||
info_expect_equal_hash(&txs_root, &header.extrinsics_root);
|
||||
if let Mode::Overwrite = mode {
|
||||
header.extrinsics_root = txs_root;
|
||||
} else {
|
||||
assert!(txs_root == header.extrinsics_root, "Transaction trie root must be valid.");
|
||||
}
|
||||
|
||||
// try to read something that depends on current header digest
|
||||
// so that it'll be included in execution proof
|
||||
if let Some(generic::DigestItem::Other(v)) = header.digest().logs().iter().next() {
|
||||
let _: Option<u32> = storage::unhashed::get(&v);
|
||||
}
|
||||
initialize_block(header);
|
||||
|
||||
// execute transactions
|
||||
block.extrinsics.iter().enumerate().for_each(|(i, e)| {
|
||||
storage::unhashed::put(well_known_keys::EXTRINSIC_INDEX, &(i as u32));
|
||||
let _ = execute_transaction_backend(e).unwrap_or_else(|_| panic!("Invalid transaction"));
|
||||
storage::unhashed::kill(well_known_keys::EXTRINSIC_INDEX);
|
||||
block.extrinsics.iter().for_each(|e| {
|
||||
let _ = execute_transaction(e.clone()).unwrap_or_else(|_| panic!("Invalid transaction"));
|
||||
});
|
||||
|
||||
let o_new_authorities = <NewAuthorities>::take();
|
||||
let storage_root = Hash::decode(&mut &storage_root()[..])
|
||||
.expect("`storage_root` is a valid hash");
|
||||
let new_header = finalize_block();
|
||||
|
||||
if let Mode::Overwrite = mode {
|
||||
header.state_root = storage_root;
|
||||
header.state_root = new_header.state_root;
|
||||
} else {
|
||||
// check storage root.
|
||||
info_expect_equal_hash(&storage_root, &header.state_root);
|
||||
assert!(storage_root == header.state_root, "Storage root must match that calculated.");
|
||||
}
|
||||
|
||||
// check digest
|
||||
let digest = &mut header.digest;
|
||||
if let Some(storage_changes_root) = storage_changes_root(&header.parent_hash.encode()) {
|
||||
digest.push(
|
||||
generic::DigestItem::ChangesTrieRoot(
|
||||
Hash::decode(&mut &storage_changes_root[..])
|
||||
.expect("`storage_changes_root` is a valid hash")
|
||||
)
|
||||
info_expect_equal_hash(&new_header.state_root, &header.state_root);
|
||||
assert!(
|
||||
new_header.state_root == header.state_root,
|
||||
"Storage root must match that calculated.",
|
||||
);
|
||||
}
|
||||
if let Some(new_authorities) = o_new_authorities {
|
||||
digest.push(generic::DigestItem::Consensus(*b"aura", new_authorities.encode()));
|
||||
digest.push(generic::DigestItem::Consensus(*b"babe", new_authorities.encode()));
|
||||
|
||||
if let Mode::Overwrite = mode {
|
||||
header.extrinsics_root = new_header.extrinsics_root;
|
||||
} else {
|
||||
info_expect_equal_hash(&new_header.extrinsics_root, &header.extrinsics_root);
|
||||
assert!(
|
||||
new_header.extrinsics_root == header.extrinsics_root,
|
||||
"Transaction trie root must be valid.",
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -224,7 +200,7 @@ pub fn execute_transaction(utx: Extrinsic) -> ApplyExtrinsicResult {
|
||||
pub fn finalize_block() -> Header {
|
||||
let extrinsic_index: u32 = storage::unhashed::take(well_known_keys::EXTRINSIC_INDEX).unwrap();
|
||||
let txs: Vec<_> = (0..extrinsic_index).map(ExtrinsicData::take).collect();
|
||||
let extrinsics_root = BlakeTwo256::ordered_trie_root(txs).into();
|
||||
let extrinsics_root = trie::blake2_256_ordered_root(txs).into();
|
||||
let number = <Number>::take().expect("Number is set by `initialize_block`");
|
||||
let parent_hash = <ParentHash>::take();
|
||||
let mut digest = <StorageDigest>::take().expect("StorageDigest is set by `initialize_block`");
|
||||
|
||||
Reference in New Issue
Block a user