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

Commit

Permalink
[contracts] Implement transparent hashing for contract storage (parit…
Browse files Browse the repository at this point in the history
…ytech#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 <>
  • Loading branch information
4 people authored and DaviRain-Su committed Aug 23, 2022
1 parent 12f795c commit 5fd36b7
Show file tree
Hide file tree
Showing 13 changed files with 2,040 additions and 998 deletions.
6 changes: 5 additions & 1 deletion bin/node/executor/tests/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,11 @@ fn deploying_wasm_contract_should_work() {
t.execute_with(|| {
// Verify that the contract does exist by querying some of its storage items
// It does not matter that the storage item itself does not exist.
assert!(&pallet_contracts::Pallet::<Runtime>::get_storage(addr, Default::default()).is_ok());
assert!(&pallet_contracts::Pallet::<Runtime>::get_storage(
addr,
pallet_contracts::StorageKey::<Runtime>::default().to_vec()
)
.is_ok());
});
}

Expand Down
3 changes: 2 additions & 1 deletion bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1151,6 +1151,7 @@ impl pallet_contracts::Config for Runtime {
type ContractAccessWeight = pallet_contracts::DefaultContractAccessWeight<RuntimeBlockWeights>;
type MaxCodeLen = ConstU32<{ 128 * 1024 }>;
type RelaxedMaxCodeLen = ConstU32<{ 256 * 1024 }>;
type MaxStorageKeyLen = ConstU32<128>;
}

impl pallet_sudo::Config for Runtime {
Expand Down Expand Up @@ -1936,7 +1937,7 @@ impl_runtime_apis! {

fn get_storage(
address: AccountId,
key: [u8; 32],
key: Vec<u8>,
) -> pallet_contracts_primitives::GetStorageResult {
Contracts::get_storage(address, key)
}
Expand Down
2 changes: 2 additions & 0 deletions frame/contracts/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ pub type GetStorageResult = Result<Option<Vec<u8>>, ContractAccessError>;
pub enum ContractAccessError {
/// The given address doesn't point to a contract.
DoesntExist,
/// Storage key cannot be decoded from the provided input data.
KeyDecodingFailed,
}

bitflags! {
Expand Down
2 changes: 1 addition & 1 deletion frame/contracts/rpc/runtime-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ sp_api::decl_runtime_apis! {
/// doesn't exist, or doesn't have a contract then `Err` is returned.
fn get_storage(
address: AccountId,
key: [u8; 32],
key: Vec<u8>,
) -> GetStorageResult;
}
}
15 changes: 11 additions & 4 deletions frame/contracts/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;

Expand Down Expand Up @@ -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(),
}
}
}
Expand Down Expand Up @@ -167,7 +174,7 @@ where
fn get_storage(
&self,
address: AccountId,
key: H256,
key: Bytes,
at: Option<BlockHash>,
) -> RpcResult<Option<Bytes>>;
}
Expand Down Expand Up @@ -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);
Expand Down
Loading

0 comments on commit 5fd36b7

Please sign in to comment.