-
Notifications
You must be signed in to change notification settings - Fork 110
Filter empty wasm hash from snapshot lookups #1905
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
Open
leighmcculloch
wants to merge
6
commits into
main
Choose a base branch
from
filter-empty-wasm-hash-snapshot
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c97a63c
filter empty wasm hash from snapshot source lookups
leighmcculloch c8053d0
move empty wasm hash const into get fn
leighmcculloch 2e24ab4
note that snapshot source filtering is an optimisation
leighmcculloch cc18098
hyphenate short-circuit in comments
leighmcculloch 89a81ad
Merge branch 'main' into filter-empty-wasm-hash-snapshot
mootz12 5361ae5
update test snapshot to protocol 27
leighmcculloch 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
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,79 @@ | ||
| use crate::{self as soroban_sdk}; | ||
| use soroban_sdk::{ | ||
| contract, contractimpl, | ||
| testutils::{HostError, SnapshotSource, SnapshotSourceInput}, | ||
| xdr, Env, | ||
| }; | ||
| use std::{cell::RefCell, rc::Rc}; | ||
|
|
||
| #[contract] | ||
| pub struct Contract; | ||
|
|
||
| #[contractimpl] | ||
| impl Contract { | ||
| pub fn hello(_env: Env) {} | ||
| } | ||
|
|
||
| /// The SHA256 of empty/zero bytes, used by the host as the synthetic WASM hash | ||
| /// for native (non-WASM) test contracts. | ||
| const EMPTY_WASM_HASH: [u8; 32] = [ | ||
| 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24, | ||
| 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, | ||
| ]; | ||
|
|
||
| /// A snapshot source that records every key it is asked for and always returns | ||
| /// `Ok(None)`. | ||
| struct RecordingSnapshotSource { | ||
| keys: Rc<RefCell<Vec<xdr::LedgerKey>>>, | ||
| } | ||
|
|
||
| impl SnapshotSource for RecordingSnapshotSource { | ||
| fn get( | ||
| &self, | ||
| key: &Rc<xdr::LedgerKey>, | ||
| ) -> Result<Option<(Rc<xdr::LedgerEntry>, Option<u32>)>, HostError> { | ||
| self.keys.borrow_mut().push(key.as_ref().clone()); | ||
| Ok(None) | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_empty_wasm_hash_not_requested_from_snapshot_source() { | ||
| let keys = Rc::new(RefCell::new(Vec::new())); | ||
| let input = SnapshotSourceInput { | ||
| source: Rc::new(RecordingSnapshotSource { keys: keys.clone() }), | ||
| ledger_info: None, | ||
| snapshot: None, | ||
| }; | ||
|
|
||
| let env = Env::from_ledger_snapshot(input); | ||
|
|
||
| // Registering a native test contract should not cause a lookup for the | ||
| // empty WASM hash ContractCode entry against the snapshot source. | ||
| let _ = env.register(Contract, ()); | ||
|
|
||
| let recorded = keys.borrow(); | ||
|
|
||
| // The empty WASM hash ContractCode entry must never be requested. | ||
| let requested_empty_wasm = recorded.iter().any(|k| { | ||
| matches!( | ||
| k, | ||
| xdr::LedgerKey::ContractCode(xdr::LedgerKeyContractCode { hash, .. }) | ||
| if hash.0 == EMPTY_WASM_HASH | ||
| ) | ||
| }); | ||
| assert!( | ||
| !requested_empty_wasm, | ||
| "empty WASM hash ContractCode entry should not be requested from the snapshot source, recorded keys: {recorded:?}" | ||
| ); | ||
|
|
||
| // Sanity check: other lookups (such as the contract instance ContractData | ||
| // entry) are still allowed to reach the snapshot source. | ||
| let requested_contract_data = recorded | ||
| .iter() | ||
| .any(|k| matches!(k, xdr::LedgerKey::ContractData(_))); | ||
| assert!( | ||
| requested_contract_data, | ||
| "expected the contract instance ContractData lookup to reach the snapshot source, recorded keys: {recorded:?}" | ||
| ); | ||
| } |
60 changes: 60 additions & 0 deletions
60
...hot_source_empty_wasm_hash/test_empty_wasm_hash_not_requested_from_snapshot_source.1.json
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,60 @@ | ||
| { | ||
| "generators": { | ||
| "address": 1, | ||
| "nonce": 0, | ||
| "mux_id": 0 | ||
| }, | ||
| "auth": [ | ||
| [] | ||
| ], | ||
| "ledger": { | ||
| "protocol_version": 26, | ||
| "sequence_number": 0, | ||
| "timestamp": 0, | ||
| "network_id": "0000000000000000000000000000000000000000000000000000000000000000", | ||
| "base_reserve": 0, | ||
| "min_persistent_entry_ttl": 4096, | ||
| "min_temp_entry_ttl": 16, | ||
| "max_entry_ttl": 6312000, | ||
| "ledger_entries": [ | ||
| { | ||
| "entry": { | ||
| "last_modified_ledger_seq": 0, | ||
| "data": { | ||
| "contract_data": { | ||
| "ext": "v0", | ||
| "contract": "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD2KM", | ||
| "key": "ledger_key_contract_instance", | ||
| "durability": "persistent", | ||
| "val": { | ||
| "contract_instance": { | ||
| "executable": { | ||
| "wasm": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" | ||
| }, | ||
| "storage": null | ||
| } | ||
| } | ||
| } | ||
| }, | ||
| "ext": "v0" | ||
| }, | ||
| "live_until": 4095 | ||
| }, | ||
| { | ||
| "entry": { | ||
| "last_modified_ledger_seq": 0, | ||
| "data": { | ||
| "contract_code": { | ||
| "ext": "v0", | ||
| "hash": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", | ||
| "code": "" | ||
| } | ||
| }, | ||
| "ext": "v0" | ||
| }, | ||
| "live_until": 4095 | ||
| } | ||
| ] | ||
| }, | ||
| "events": [] | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.