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

Commit 622f532

Browse files
agryaznovatheiParity Botrcny
authored
[contracts] Implement transparent hashing for contract storage (#11501)
* save * builds and old tests pass save: temporary value dropped while borrowed save: finally builds test updated but still fails * type names enhanced * VarSizedKey bounded to new Config param * improved wasm runtime updated funcs * unstable-interface tests fixed * benchmarks fixed * Apply suggestions from code review Co-authored-by: Alexander Theißen <[email protected]> * fixes on feedback * fixes on feedback applied + make it build * benchmarks build but fail (old) * "Original code too large" * seal_clear_storage bench fixed (code size workaround hack removal tbd) * bench_seal_clear_storage pass * bench_seal_take_storage ... ok * added new seal_set_storage + updated benchmarks * added new seal_get_storage + updated benchmarks * added new seal_contains_storage + updated benchmarks * added tests for _transparent exec functions * wasm test for clear_storage * wasm test for take_storage * wasm test for new set_storage * wasm test for new get_storage * wasm test for new contains_storage * CI fix * ci fix * ci fix * ci fix * cargo run --quiet --profile=production --features=runtime-benchmarks --manifest-path=bin/node/cli/Cargo.toml -- benchmark pallet --chain=dev --steps=50 --repeat=20 --pallet=pallet_contracts --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --output=./frame/contracts/src/weights.rs --template=./.maintain/frame-weight-template.hbs * fixes according to the review feedback * tests & benchmarks fixed * cargo run --quiet --profile=production --features=runtime-benchmarks --manifest-path=bin/node/cli/Cargo.toml -- benchmark pallet --chain=dev --steps=50 --repeat=20 --pallet=pallet_contracts --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --output=./frame/contracts/src/weights.rs --template=./.maintain/frame-weight-template.hbs * refactoring * fix to runtime api * ci fix * ctx.get_storage() factored out * ctx.contains_storage() factored out * number of batches reduced for transparent hashing storage benchmarks * contracts RPC & pallet::get_storage to use transparent hashing * node and rpc updated to use get_storage with VarSizedKey * refactored (more concize) * refactored contains_storage (DRYed) * refactored contains_storage (DRYed) * fix rpc * fmt fix * more fixes in rpc * rollback `Pallet:get_storage` to Vec<u8> and rpc and node parts related to it * added `KeyDecodingFailed` error * Revert weird "fmt fix" This reverts commit c582cff. * node-executor basic test update * fix node-executor basic test * benchmarks fix * more benchmarks fix * FixedSizedKey is hidden from pub, VarSizedKey is exported as StorageKey * ci fix * set_storage benchmark fix * ci fix * ci fix * comments improved * new error code to rpc: KEY_DECODING_FAILED * Put `rusty-cachier` before PR merge into `master` for `cargo-check-benches` job * cargo run --quiet --profile=production --features=runtime-benchmarks --manifest-path=bin/node/cli/Cargo.toml -- benchmark pallet --chain=dev --steps=50 --repeat=20 --pallet=pallet_contracts --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --output=./frame/contracts/src/weights.rs --template=./.maintain/frame-weight-template.hbs * minor optimization Co-authored-by: Alexander Theißen <[email protected]> Co-authored-by: Parity Bot <[email protected]> Co-authored-by: Vladimir Istyufeev <[email protected]> Co-authored-by: command-bot <>
1 parent ee3eb22 commit 622f532

File tree

13 files changed

+2040
-998
lines changed

13 files changed

+2040
-998
lines changed

bin/node/executor/tests/basic.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -764,7 +764,11 @@ fn deploying_wasm_contract_should_work() {
764764
t.execute_with(|| {
765765
// Verify that the contract does exist by querying some of its storage items
766766
// It does not matter that the storage item itself does not exist.
767-
assert!(&pallet_contracts::Pallet::<Runtime>::get_storage(addr, Default::default()).is_ok());
767+
assert!(&pallet_contracts::Pallet::<Runtime>::get_storage(
768+
addr,
769+
pallet_contracts::StorageKey::<Runtime>::default().to_vec()
770+
)
771+
.is_ok());
768772
});
769773
}
770774

bin/node/runtime/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1151,6 +1151,7 @@ impl pallet_contracts::Config for Runtime {
11511151
type ContractAccessWeight = pallet_contracts::DefaultContractAccessWeight<RuntimeBlockWeights>;
11521152
type MaxCodeLen = ConstU32<{ 128 * 1024 }>;
11531153
type RelaxedMaxCodeLen = ConstU32<{ 256 * 1024 }>;
1154+
type MaxStorageKeyLen = ConstU32<128>;
11541155
}
11551156

11561157
impl pallet_sudo::Config for Runtime {
@@ -1936,7 +1937,7 @@ impl_runtime_apis! {
19361937

19371938
fn get_storage(
19381939
address: AccountId,
1939-
key: [u8; 32],
1940+
key: Vec<u8>,
19401941
) -> pallet_contracts_primitives::GetStorageResult {
19411942
Contracts::get_storage(address, key)
19421943
}

frame/contracts/common/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ pub type GetStorageResult = Result<Option<Vec<u8>>, ContractAccessError>;
106106
pub enum ContractAccessError {
107107
/// The given address doesn't point to a contract.
108108
DoesntExist,
109+
/// Storage key cannot be decoded from the provided input data.
110+
KeyDecodingFailed,
109111
}
110112

111113
bitflags! {

frame/contracts/rpc/runtime-api/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ sp_api::decl_runtime_apis! {
7979
/// doesn't exist, or doesn't have a contract then `Err` is returned.
8080
fn get_storage(
8181
address: AccountId,
82-
key: [u8; 32],
82+
key: Vec<u8>,
8383
) -> GetStorageResult;
8484
}
8585
}

frame/contracts/rpc/src/lib.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use pallet_contracts_primitives::{
3333
use serde::{Deserialize, Serialize};
3434
use sp_api::ProvideRuntimeApi;
3535
use sp_blockchain::HeaderBackend;
36-
use sp_core::{Bytes, H256};
36+
use sp_core::Bytes;
3737
use sp_rpc::number::NumberOrHex;
3838
use sp_runtime::{
3939
generic::BlockId,
@@ -44,6 +44,7 @@ pub use pallet_contracts_rpc_runtime_api::ContractsApi as ContractsRuntimeApi;
4444

4545
const RUNTIME_ERROR: i32 = 1;
4646
const CONTRACT_DOESNT_EXIST: i32 = 2;
47+
const KEY_DECODING_FAILED: i32 = 3;
4748

4849
pub type Weight = u64;
4950

@@ -74,6 +75,12 @@ impl From<ContractAccessError> for JsonRpseeError {
7475
None::<()>,
7576
))
7677
.into(),
78+
KeyDecodingFailed => CallError::Custom(ErrorObject::owned(
79+
KEY_DECODING_FAILED,
80+
"Failed to decode the specified storage key.",
81+
None::<()>,
82+
))
83+
.into(),
7784
}
7885
}
7986
}
@@ -167,7 +174,7 @@ where
167174
fn get_storage(
168175
&self,
169176
address: AccountId,
170-
key: H256,
177+
key: Bytes,
171178
at: Option<BlockHash>,
172179
) -> RpcResult<Option<Bytes>>;
173180
}
@@ -292,13 +299,13 @@ where
292299
fn get_storage(
293300
&self,
294301
address: AccountId,
295-
key: H256,
302+
key: Bytes,
296303
at: Option<<Block as BlockT>::Hash>,
297304
) -> RpcResult<Option<Bytes>> {
298305
let api = self.client.runtime_api();
299306
let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash));
300307
let result = api
301-
.get_storage(&at, address, key.into())
308+
.get_storage(&at, address, key.to_vec())
302309
.map_err(runtime_error_into_rpc_err)?
303310
.map_err(ContractAccessError)?
304311
.map(Bytes);

0 commit comments

Comments
 (0)