[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 <alex.theissen@me.com>

* 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 c582cfff4b5cb2c9929fd5e3b45519bb24aeb657.

* 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 <alex.theissen@me.com>
Co-authored-by: Parity Bot <admin@parity.io>
Co-authored-by: Vladimir Istyufeev <vladimir@parity.io>
Co-authored-by: command-bot <>
This commit is contained in:
Alexander Gryaznov
2022-06-23 15:10:35 +03:00
committed by GitHub
parent 7d2ecc8995
commit 68ea2ab039
13 changed files with 2200 additions and 1158 deletions
+11 -4
View File
@@ -33,7 +33,7 @@ use pallet_contracts_primitives::{
use serde::{Deserialize, Serialize};
use sp_api::ProvideRuntimeApi;
use sp_blockchain::HeaderBackend;
use sp_core::{Bytes, H256};
use sp_core::Bytes;
use sp_rpc::number::NumberOrHex;
use sp_runtime::{
generic::BlockId,
@@ -44,6 +44,7 @@ pub use pallet_contracts_rpc_runtime_api::ContractsApi as ContractsRuntimeApi;
const RUNTIME_ERROR: i32 = 1;
const CONTRACT_DOESNT_EXIST: i32 = 2;
const KEY_DECODING_FAILED: i32 = 3;
pub type Weight = u64;
@@ -74,6 +75,12 @@ impl From<ContractAccessError> for JsonRpseeError {
None::<()>,
))
.into(),
KeyDecodingFailed => CallError::Custom(ErrorObject::owned(
KEY_DECODING_FAILED,
"Failed to decode the specified storage key.",
None::<()>,
))
.into(),
}
}
}
@@ -167,7 +174,7 @@ where
fn get_storage(
&self,
address: AccountId,
key: H256,
key: Bytes,
at: Option<BlockHash>,
) -> RpcResult<Option<Bytes>>;
}
@@ -292,13 +299,13 @@ where
fn get_storage(
&self,
address: AccountId,
key: H256,
key: Bytes,
at: Option<<Block as BlockT>::Hash>,
) -> RpcResult<Option<Bytes>> {
let api = self.client.runtime_api();
let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash));
let result = api
.get_storage(&at, address, key.into())
.get_storage(&at, address, key.to_vec())
.map_err(runtime_error_into_rpc_err)?
.map_err(ContractAccessError)?
.map(Bytes);