-
Notifications
You must be signed in to change notification settings - Fork 257
Check the execution receipt received from network properly #236
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
8e11150
Include the list of storage roots in ExecutionReceipt
liuchengxu 7493a63
Only authorty executors are allowed to broadcast ER
liuchengxu 4934c1c
Reduce Executor generics by merging `runtime_api` into `client`
liuchengxu 2856ded
Write local ER to disk and check external ER against the local one
liuchengxu d983d4d
Remove the validated local execution receipt from disk
liuchengxu 992cfb2
Apply the suggestions from code review
liuchengxu 0098a20
Use Merkle Tree instead of Merkle Patricia Tree for execution trace
liuchengxu c383418
Prune the too old execution receipts while writing a new one
liuchengxu 6eb1c30
Store Execution Receipt Keyed by block hash
liuchengxu b22f827
Replicate the state root when constructing the trace merkle tree from…
liuchengxu 9ccc995
Merge the `BS` generic parameter into `Client` in Executor struct
liuchengxu be7a07c
Use the new best_hash instead of parent_hash when calling `intermedia…
liuchengxu 872b1b4
Apply the suggstions from code review
liuchengxu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,193 @@ | ||
| //! Schema for executor in the aux-db. | ||
|
|
||
| use codec::{Decode, Encode}; | ||
| use sc_client_api::backend::AuxStore; | ||
| use sp_blockchain::{Error as ClientError, Result as ClientResult}; | ||
| use sp_executor::ExecutionReceipt; | ||
| use sp_runtime::{ | ||
| traits::{Block as BlockT, Header as HeaderT, One, Saturating}, | ||
| SaturatedConversion, | ||
| }; | ||
|
|
||
| const EXECUTION_RECEIPT_KEY: &[u8] = b"execution_receipt"; | ||
| const EXECUTION_RECEIPT_START: &[u8] = b"execution_receipt_start"; | ||
| const EXECUTION_RECEIPT_BLOCK_NUMBER: &[u8] = b"execution_receipt_block_number"; | ||
| /// Prune the execution receipts when they reach this number. | ||
| const PRUNING_DEPTH: u64 = 1000; | ||
|
|
||
| fn execution_receipt_key(block_hash: impl Encode) -> Vec<u8> { | ||
| (EXECUTION_RECEIPT_KEY, block_hash).encode() | ||
| } | ||
|
|
||
| fn load_decode<Backend: AuxStore, T: Decode>( | ||
| backend: &Backend, | ||
| key: &[u8], | ||
| ) -> ClientResult<Option<T>> { | ||
| match backend.get_aux(key)? { | ||
| None => Ok(None), | ||
| Some(t) => T::decode(&mut &t[..]) | ||
| .map_err(|e| { | ||
| ClientError::Backend(format!("Executor DB is corrupted. Decode error: {}", e)) | ||
| }) | ||
| .map(Some), | ||
| } | ||
| } | ||
|
|
||
| /// Write the execution receipt of a block to aux storage, optionally prune the receipts that are | ||
| /// too old. | ||
| pub(super) fn write_execution_receipt<Backend: AuxStore, Block: BlockT>( | ||
| backend: &Backend, | ||
| block_hash: Block::Hash, | ||
| block_number: <<Block as BlockT>::Header as HeaderT>::Number, | ||
| execution_receipt: &ExecutionReceipt<Block::Hash>, | ||
| ) -> Result<(), sp_blockchain::Error> { | ||
| let block_number_key = (EXECUTION_RECEIPT_BLOCK_NUMBER, block_number).encode(); | ||
| let mut hashes_at_block_number = | ||
| load_decode::<_, Vec<Block::Hash>>(backend, block_number_key.as_slice())? | ||
| .unwrap_or_default(); | ||
| hashes_at_block_number.push(block_hash); | ||
|
|
||
| let first_saved_receipt = load_decode::<_, <<Block as BlockT>::Header as HeaderT>::Number>( | ||
| backend, | ||
| EXECUTION_RECEIPT_START, | ||
| )? | ||
| .unwrap_or(block_number); | ||
|
|
||
| let mut new_first_saved_receipt = first_saved_receipt; | ||
|
|
||
| let keys_to_delete = if block_number - first_saved_receipt >= PRUNING_DEPTH.saturated_into() { | ||
| new_first_saved_receipt = block_number.saturating_sub((PRUNING_DEPTH - 1).saturated_into()); | ||
|
|
||
| let mut keys_to_delete = vec![]; | ||
| let mut to_delete_start = first_saved_receipt; | ||
| while to_delete_start < new_first_saved_receipt { | ||
| let delete_block_number_key = | ||
| (EXECUTION_RECEIPT_BLOCK_NUMBER, to_delete_start).encode(); | ||
| if let Some(hashes_to_delete) = | ||
| load_decode::<_, Vec<Block::Hash>>(backend, delete_block_number_key.as_slice())? | ||
| { | ||
| keys_to_delete.extend( | ||
| hashes_to_delete.into_iter().map(|h| (EXECUTION_RECEIPT_KEY, h).encode()), | ||
| ); | ||
| keys_to_delete.push(delete_block_number_key); | ||
| } | ||
| to_delete_start = to_delete_start.saturating_add(One::one()); | ||
| } | ||
|
|
||
| keys_to_delete | ||
| } else { | ||
| vec![] | ||
| }; | ||
|
|
||
| backend.insert_aux( | ||
| &[ | ||
| (execution_receipt_key(block_hash).as_slice(), execution_receipt.encode().as_slice()), | ||
| (block_number_key.as_slice(), hashes_at_block_number.encode().as_slice()), | ||
| ((EXECUTION_RECEIPT_START, new_first_saved_receipt.encode().as_slice())), | ||
| ], | ||
| &keys_to_delete.iter().map(|k| &k[..]).collect::<Vec<&[u8]>>()[..], | ||
| ) | ||
| } | ||
|
|
||
| /// Load the execution receipt associated with a block. | ||
| pub(super) fn load_execution_receipt<Backend: AuxStore, Block: BlockT>( | ||
| backend: &Backend, | ||
| block_hash: Block::Hash, | ||
| ) -> ClientResult<Option<ExecutionReceipt<Block::Hash>>> { | ||
| load_decode(backend, execution_receipt_key(block_hash).as_slice()) | ||
| } | ||
|
|
||
| pub(super) fn target_receipt_is_pruned<Block: BlockT>( | ||
| current_block: <<Block as BlockT>::Header as HeaderT>::Number, | ||
| target_block: <<Block as BlockT>::Header as HeaderT>::Number, | ||
| ) -> bool { | ||
| current_block - target_block >= PRUNING_DEPTH.saturated_into() | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use sp_core::hash::H256; | ||
| use substrate_test_runtime::{Block, BlockNumber, Hash}; | ||
|
|
||
| type ExecutionReceipt = sp_executor::ExecutionReceipt<Hash>; | ||
|
|
||
| fn create_execution_receipt() -> ExecutionReceipt { | ||
| ExecutionReceipt { | ||
| primary_hash: H256::random(), | ||
| secondary_hash: H256::random(), | ||
| trace: Default::default(), | ||
| trace_root: Default::default(), | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn prune_execution_receipt_works() { | ||
| let client = substrate_test_runtime_client::new(); | ||
|
|
||
| let receipt_start = || { | ||
| load_decode::<_, BlockNumber>(&client, EXECUTION_RECEIPT_START.to_vec().as_slice()) | ||
| .unwrap() | ||
| }; | ||
|
|
||
| let hashes_at = |number: BlockNumber| { | ||
| load_decode::<_, Vec<Hash>>( | ||
| &client, | ||
| (EXECUTION_RECEIPT_BLOCK_NUMBER, number).encode().as_slice(), | ||
| ) | ||
| .unwrap() | ||
| }; | ||
|
|
||
| let receipt_at = | ||
| |block_hash: Hash| load_execution_receipt::<_, Block>(&client, block_hash).unwrap(); | ||
|
|
||
| let write_receipt_at = |hash: Hash, number: BlockNumber, receipt: &ExecutionReceipt| { | ||
| write_execution_receipt::<_, Block>(&client, hash, number, receipt).unwrap() | ||
| }; | ||
|
|
||
| assert_eq!(receipt_start(), None); | ||
|
|
||
| // Create PRUNING_DEPTH receipts. | ||
| let block_hash_list = (1..=PRUNING_DEPTH) | ||
| .map(|block_number| { | ||
| let receipt = create_execution_receipt(); | ||
| let block_hash = Hash::random(); | ||
| write_receipt_at(block_hash, block_number, &receipt); | ||
| assert_eq!(receipt_at(block_hash), Some(receipt)); | ||
| assert_eq!(hashes_at(block_number), Some(vec![block_hash])); | ||
| assert_eq!(receipt_start(), Some(1)); | ||
| block_hash | ||
| }) | ||
| .collect::<Vec<_>>(); | ||
|
|
||
| assert!(!target_receipt_is_pruned::<Block>(PRUNING_DEPTH, 1)); | ||
|
|
||
| // Create PRUNING_DEPTH + 1 receipt. | ||
| let block_hash = Hash::random(); | ||
| assert!(receipt_at(block_hash).is_none()); | ||
| write_receipt_at(block_hash, PRUNING_DEPTH + 1, &create_execution_receipt()); | ||
| assert!(receipt_at(block_hash).is_some()); | ||
| // ER of block #1 should be pruned. | ||
| assert!(receipt_at(block_hash_list[0]).is_none()); | ||
| // block number mapping should be pruned as well. | ||
| assert!(hashes_at(1).is_none()); | ||
| assert!(target_receipt_is_pruned::<Block>(PRUNING_DEPTH + 1, 1)); | ||
| assert_eq!(receipt_start(), Some(2)); | ||
|
|
||
| // Create PRUNING_DEPTH + 2 receipt. | ||
| let block_hash = Hash::random(); | ||
| write_receipt_at(block_hash, PRUNING_DEPTH + 2, &create_execution_receipt()); | ||
| assert!(receipt_at(block_hash).is_some()); | ||
| // ER of block #2 should be pruned. | ||
| assert!(receipt_at(block_hash_list[1]).is_none()); | ||
| assert!(target_receipt_is_pruned::<Block>(PRUNING_DEPTH + 2, 2)); | ||
| assert!(!target_receipt_is_pruned::<Block>(PRUNING_DEPTH + 2, 3)); | ||
| assert_eq!(receipt_start(), Some(3)); | ||
|
|
||
| // Multiple hashes attached to the block #(PRUNING_DEPTH + 2) | ||
| let block_hash2 = Hash::random(); | ||
| write_receipt_at(block_hash2, PRUNING_DEPTH + 2, &create_execution_receipt()); | ||
| assert!(receipt_at(block_hash2).is_some()); | ||
| assert_eq!(hashes_at(PRUNING_DEPTH + 2), Some(vec![block_hash, block_hash2])); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In these cases I would honestly prefer
.expect()since we don't expect these numbers to ever be beyondu32::MAX.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Feel free to refactor it directly as I don't see another style that is satisfying to me using
expect():P.