-
Notifications
You must be signed in to change notification settings - Fork 477
Fail when decoding from storage and not all bytes consumed #1897
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 4 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
be6fdc6
Add passing test to check reading and writing a value with the same size
ascjones 358c736
Add FAILING test for not consuming the entire buffer
ascjones d4a49f7
Add new DecodeAll trait, and use it.
ascjones 3008310
Fmt
ascjones 0766776
Use helper method instead of extension trait
ascjones cfccd7b
Space
ascjones 00804c1
Storable
ascjones 363e7cf
Merge branch 'master' into aj/decode-all
ascjones 3e1612f
Merge branch 'master' into aj/decode-all
ascjones f110fe6
Merge branch 'master' into aj/decode-all
ascjones e58a560
Merge branch 'master' into aj/decode-all
ascjones 61bf94a
offchain engine return populated buffer
ascjones 723a335
Fix up integration test
ascjones cf3aa7c
Test for take_storage
ascjones ece4c65
Fix tests, clippy
ascjones 82fe420
Remove Result type alias
ascjones 5d2080f
CHANGELOG.md
ascjones 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
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 |
|---|---|---|
|
|
@@ -50,6 +50,7 @@ pub use self::{ | |
| AutoStorableHint, | ||
| Packed, | ||
| Storable, | ||
| StorableDecodeAll, | ||
| StorableHint, | ||
| StorageKey, | ||
| }, | ||
|
|
||
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,9 @@ | ||
| # Ignore build artifacts from the local tests sub-crate. | ||
| /target/ | ||
|
|
||
| # Ignore backup files creates by cargo fmt. | ||
| **/*.rs.bk | ||
|
|
||
| # Remove Cargo.lock when creating an executable, leave it for libraries | ||
| # More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock | ||
| Cargo.lock |
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,23 @@ | ||
| [package] | ||
| name = "contract-storage" | ||
| version = "4.2.0" | ||
| authors = ["Parity Technologies <[email protected]>"] | ||
| edition = "2021" | ||
| publish = false | ||
|
|
||
| [dependencies] | ||
| ink = { path = "../../crates/ink", default-features = false } | ||
|
|
||
| [dev-dependencies] | ||
| ink_e2e = { path = "../../crates/e2e" } | ||
|
|
||
| [lib] | ||
| path = "lib.rs" | ||
|
|
||
| [features] | ||
| default = ["std"] | ||
| std = [ | ||
| "ink/std", | ||
| ] | ||
| ink-as-dependency = [] | ||
| e2e-tests = [] |
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,63 @@ | ||
| use super::contract_storage::*; | ||
| use ink_e2e::ContractsBackend; | ||
|
|
||
| type E2EResult<T> = std::result::Result<T, Box<dyn std::error::Error>>; | ||
|
|
||
| #[ink_e2e::test] | ||
| async fn get_contract_storage_consumes_entire_buffer<Client: E2EBackend>( | ||
| mut client: Client, | ||
| ) -> E2EResult<()> { | ||
| // given | ||
| let constructor = ContractStorageRef::new(); | ||
| let contract = client | ||
| .instantiate("contract-storage", &ink_e2e::alice(), constructor, 0, None) | ||
| .await | ||
| .expect("instantiate failed"); | ||
| let call = contract.call::<ContractStorage>(); | ||
|
|
||
| // when | ||
| let result = client | ||
| .call( | ||
| &ink_e2e::alice(), | ||
| &call.set_and_get_storage_all_data_consumed(), | ||
| 0, | ||
| None, | ||
| ) | ||
| .await | ||
| .expect("Calling `insert_balance` failed") | ||
| .return_value(); | ||
|
|
||
| assert!(result.is_ok()); | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| #[ink_e2e::test] | ||
| async fn get_contract_storage_fails_when_extra_data<Client: E2EBackend>( | ||
| mut client: Client, | ||
| ) -> E2EResult<()> { | ||
| // given | ||
| let constructor = ContractStorageRef::new(); | ||
| let contract = client | ||
| .instantiate("contract-storage", &ink_e2e::alice(), constructor, 0, None) | ||
| .await | ||
| .expect("instantiate failed"); | ||
| let call = contract.call::<ContractStorage>(); | ||
|
|
||
| // when | ||
| let result = client | ||
| .call( | ||
| &ink_e2e::alice(), | ||
| &call.set_and_get_storage_partial_data_consumed(), | ||
| 0, | ||
| None, | ||
| ) | ||
| .await; | ||
|
|
||
| assert!( | ||
| result.is_err(), | ||
| "Expected the contract to revert when only partially consuming the buffer" | ||
| ); | ||
|
|
||
| Ok(()) | ||
| } |
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,51 @@ | ||
| //! A smart contract to test reading and writing contract storage | ||
|
|
||
| #![cfg_attr(not(feature = "std"), no_std, no_main)] | ||
|
|
||
| #[ink::contract] | ||
| mod contract_storage { | ||
| use ink::prelude::{ | ||
| format, | ||
| string::String, | ||
| }; | ||
|
|
||
| /// A contract for testing reading and writing contract storage. | ||
| #[ink(storage)] | ||
| #[derive(Default)] | ||
| pub struct ContractStorage; | ||
|
|
||
| impl ContractStorage { | ||
| #[ink(constructor)] | ||
| pub fn new() -> Self { | ||
| Self {} | ||
| } | ||
|
|
||
| /// Read from the contract storage slot, consuming all the data from the buffer. | ||
| #[ink(message)] | ||
| pub fn set_and_get_storage_all_data_consumed(&self) -> Result<(), String> { | ||
| let key = 0u32; | ||
| let value = [0x42; 32]; | ||
| ink::env::set_contract_storage(&key, &value); | ||
| let loaded_value = ink::env::get_contract_storage(&key) | ||
| .map_err(|e| format!("get_contract_storage failed: {:?}", e))?; | ||
| assert_eq!(loaded_value, Some(value)); | ||
| Ok(()) | ||
| } | ||
|
|
||
| /// Read from the contract storage slot, only partially consuming data from the | ||
| /// buffer. | ||
| #[ink(message)] | ||
| pub fn set_and_get_storage_partial_data_consumed(&self) -> Result<(), String> { | ||
| let key = 0u32; | ||
| let value = [0x42; 32]; | ||
| ink::env::set_contract_storage(&key, &value); | ||
| // Only attempt to read the first byte (the `u8`) of the storage value data | ||
| let _loaded_value: Option<u8> = ink::env::get_contract_storage(&key) | ||
| .map_err(|e| format!("get_contract_storage failed: {:?}", e))?; | ||
| Ok(()) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[cfg(all(test, feature = "e2e-tests"))] | ||
| mod e2e_tests; |
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.