This repository has been archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prevent account storage leakage (#270)
* WIP * Iteration over all keys with the specified prefix * Add clear_prefix in runtime-io * Introduce a custom storage impl: Double Map * Remove prefix * Impl for_keys_with_prefix for light client * Fix wasm_executor * Test storage removal leads to removal of stroage * Check for ok result in storage tests. * Add docs. * Remove commented code under decl_storage! * Add clear_prefix test in runtime-io * Add test for wasm_executor * Prefix walking test. * Rebuild binaries.
- Loading branch information
Showing
24 changed files
with
306 additions
and
19 deletions.
There are no files selected for viewing
Binary file modified
BIN
+5.37 KB
(100%)
demo/runtime/wasm/target/wasm32-unknown-unknown/release/demo_runtime.compact.wasm
Binary file not shown.
Binary file modified
BIN
+1.47 KB
(100%)
demo/runtime/wasm/target/wasm32-unknown-unknown/release/demo_runtime.wasm
Binary file not shown.
Binary file modified
BIN
+13.5 KB
(100%)
polkadot/runtime/wasm/target/wasm32-unknown-unknown/release/polkadot_runtime.compact.wasm
Binary file not shown.
Binary file modified
BIN
+7.67 KB
(100%)
polkadot/runtime/wasm/target/wasm32-unknown-unknown/release/polkadot_runtime.wasm
Binary file not shown.
This file contains 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 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 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
Binary file modified
BIN
+595 Bytes
(100%)
substrate/executor/wasm/target/wasm32-unknown-unknown/release/runtime_test.compact.wasm
Binary file not shown.
Binary file modified
BIN
+215 Bytes
(100%)
substrate/executor/wasm/target/wasm32-unknown-unknown/release/runtime_test.wasm
Binary file not shown.
This file contains 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 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 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 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 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,90 @@ | ||
// Copyright 2017 Parity Technologies (UK) Ltd. | ||
// This file is part of Substrate Demo. | ||
|
||
// Substrate Demo is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
|
||
// Substrate Demo is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
|
||
// You should have received a copy of the GNU General Public License | ||
// along with Substrate Demo. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
//! An implementation of double map backed by storage. | ||
//! | ||
//! This implementation is somewhat specialized to the tracking of the storage of accounts. | ||
use rstd::prelude::*; | ||
use codec::Slicable; | ||
use runtime_support::storage::unhashed; | ||
use runtime_io::{blake2_256, twox_128}; | ||
|
||
/// Returns only a first part of the storage key. | ||
/// | ||
/// Hashed by XX. | ||
fn first_part_of_key<M: StorageDoubleMap + ?Sized>(k1: M::Key1) -> [u8; 16] { | ||
let mut raw_prefix = Vec::new(); | ||
raw_prefix.extend(M::PREFIX); | ||
raw_prefix.extend(Slicable::encode(&k1)); | ||
twox_128(&raw_prefix) | ||
} | ||
|
||
/// Returns a compound key that consist of the two parts: (prefix, `k1`) and `k2`. | ||
/// | ||
/// The first part is hased by XX and then concatenated with a blake2 hash of `k2`. | ||
fn full_key<M: StorageDoubleMap + ?Sized>(k1: M::Key1, k2: M::Key2) -> Vec<u8> { | ||
let first_part = first_part_of_key::<M>(k1); | ||
let second_part = blake2_256(&Slicable::encode(&k2)); | ||
|
||
let mut k = Vec::new(); | ||
k.extend(&first_part); | ||
k.extend(&second_part); | ||
k | ||
} | ||
|
||
/// An implementation of a map with a two keys. | ||
/// | ||
/// It provides an important ability to efficiently remove all entries | ||
/// that have a common first key. | ||
/// | ||
/// # Mapping of keys to a storage path | ||
/// | ||
/// The storage key (i.e. the key under which the `Value` will be stored) is created from two parts. | ||
/// The first part is a XX hash of a concatenation of the `PREFIX` and `Key1`. And the second part | ||
/// is a blake2 hash of a `Key2`. | ||
/// | ||
/// Blake2 is used for `Key2` is because it will be used as a for a key for contract's storage and | ||
/// thus will be susceptible for a untrusted input. | ||
pub trait StorageDoubleMap { | ||
type Key1: Slicable; | ||
type Key2: Slicable; | ||
type Value: Slicable + Default; | ||
|
||
const PREFIX: &'static [u8]; | ||
|
||
/// Insert an entry into this map. | ||
fn insert(k1: Self::Key1, k2: Self::Key2, val: Self::Value) { | ||
unhashed::put(&full_key::<Self>(k1, k2)[..], &val); | ||
} | ||
|
||
/// Remove an entry from this map. | ||
fn remove(k1: Self::Key1, k2: Self::Key2) { | ||
unhashed::kill(&full_key::<Self>(k1, k2)[..]); | ||
} | ||
|
||
/// Get an entry from this map. | ||
/// | ||
/// If there is entry stored under the given keys, returns `None`. | ||
fn get(k1: Self::Key1, k2: Self::Key2) -> Option<Self::Value> { | ||
unhashed::get(&full_key::<Self>(k1, k2)[..]) | ||
} | ||
|
||
/// Removes all entries that shares the `k1` as the first key. | ||
fn remove_prefix(k1: Self::Key1) { | ||
unhashed::kill_prefix(&first_part_of_key::<Self>(k1)) | ||
} | ||
} |
This file contains 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 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
Oops, something went wrong.