Child trie api changes BREAKING (#4857)

Co-Authored-By: thiolliere <gui.thiolliere@gmail.com>
This commit is contained in:
cheme
2020-04-20 15:21:22 +02:00
committed by GitHub
parent 7d9aa81bfc
commit 4ffcf98d8d
64 changed files with 1514 additions and 1655 deletions
+6 -7
View File
@@ -128,7 +128,7 @@ impl<T: Trait> AccountDb<T> for DirectAccountDb {
trie_id: Option<&TrieId>,
location: &StorageKey
) -> Option<Vec<u8>> {
trie_id.and_then(|id| child::get_raw(id, crate::trie_unique_id(&id[..]), &blake2_256(location)))
trie_id.and_then(|id| child::get_raw(&crate::child_trie_info(&id[..]), &blake2_256(location)))
}
fn get_code_hash(&self, account: &T::AccountId) -> Option<CodeHash<T>> {
<ContractInfoOf<T>>::get(account).and_then(|i| i.as_alive().map(|i| i.code_hash))
@@ -167,13 +167,13 @@ impl<T: Trait> AccountDb<T> for DirectAccountDb {
(false, Some(info), _) => info,
// Existing contract is being removed.
(true, Some(info), None) => {
child::kill_storage(&info.trie_id, info.child_trie_unique_id());
child::kill_storage(&info.child_trie_info());
<ContractInfoOf<T>>::remove(&address);
continue;
}
// Existing contract is being replaced by a new one.
(true, Some(info), Some(code_hash)) => {
child::kill_storage(&info.trie_id, info.child_trie_unique_id());
child::kill_storage(&info.child_trie_info());
AliveContractInfo::<T> {
code_hash,
storage_size: T::StorageSizeOffset::get(),
@@ -212,17 +212,16 @@ impl<T: Trait> AccountDb<T> for DirectAccountDb {
for (k, v) in changed.storage.into_iter() {
if let Some(value) = child::get_raw(
&new_info.trie_id[..],
new_info.child_trie_unique_id(),
&new_info.child_trie_info(),
&blake2_256(&k),
) {
new_info.storage_size -= value.len() as u32;
}
if let Some(value) = v {
new_info.storage_size += value.len() as u32;
child::put_raw(&new_info.trie_id[..], new_info.child_trie_unique_id(), &blake2_256(&k), &value[..]);
child::put_raw(&new_info.child_trie_info(), &blake2_256(&k), &value[..]);
} else {
child::kill(&new_info.trie_id[..], new_info.child_trie_unique_id(), &blake2_256(&k));
child::kill(&new_info.child_trie_info(), &blake2_256(&k));
}
}
+12 -27
View File
@@ -125,12 +125,11 @@ use sp_runtime::{
use frame_support::dispatch::{DispatchResult, Dispatchable};
use frame_support::weights::{SimpleDispatchInfo, MINIMUM_WEIGHT};
use frame_support::{
Parameter, decl_module, decl_event, decl_storage, decl_error, storage::child,
parameter_types, IsSubType,
Parameter, decl_module, decl_event, decl_storage, decl_error,
parameter_types, IsSubType, storage::child::{self, ChildInfo},
};
use frame_support::traits::{OnUnbalanced, Currency, Get, Time, Randomness};
use frame_system::{self as system, ensure_signed, RawOrigin, ensure_root};
use sp_core::storage::well_known_keys::CHILD_STORAGE_KEY_PREFIX;
use pallet_contracts_primitives::{RentProjection, ContractAccessError};
pub type CodeHash<T> = <T as frame_system::Trait>::Hash;
@@ -229,15 +228,14 @@ pub struct RawAliveContractInfo<CodeHash, Balance, BlockNumber> {
impl<CodeHash, Balance, BlockNumber> RawAliveContractInfo<CodeHash, Balance, BlockNumber> {
/// Associated child trie unique id is built from the hash part of the trie id.
pub fn child_trie_unique_id(&self) -> child::ChildInfo {
trie_unique_id(&self.trie_id[..])
pub fn child_trie_info(&self) -> ChildInfo {
child_trie_info(&self.trie_id[..])
}
}
/// Associated child trie unique id is built from the hash part of the trie id.
pub(crate) fn trie_unique_id(trie_id: &[u8]) -> child::ChildInfo {
let start = CHILD_STORAGE_KEY_PREFIX.len() + b"default:".len();
child::ChildInfo::new_default(&trie_id[start ..])
pub(crate) fn child_trie_info(trie_id: &[u8]) -> ChildInfo {
ChildInfo::new_default(trie_id)
}
pub type TombstoneContractInfo<T> =
@@ -270,10 +268,6 @@ pub trait TrieIdGenerator<AccountId> {
///
/// The implementation must ensure every new trie id is unique: two consecutive calls with the
/// same parameter needs to return different trie id values.
///
/// Also, the implementation is responsible for ensuring that `TrieId` starts with
/// `:child_storage:`.
/// TODO: We want to change this, see https://github.com/paritytech/substrate/issues/2325
fn trie_id(account_id: &AccountId) -> TrieId;
}
@@ -297,13 +291,7 @@ where
let mut buf = Vec::new();
buf.extend_from_slice(account_id.as_ref());
buf.extend_from_slice(&new_seed.to_le_bytes()[..]);
// TODO: see https://github.com/paritytech/substrate/issues/2325
CHILD_STORAGE_KEY_PREFIX.iter()
.chain(b"default:")
.chain(T::Hashing::hash(&buf[..]).as_ref().iter())
.cloned()
.collect()
T::Hashing::hash(&buf[..]).as_ref().into()
}
}
@@ -824,13 +812,11 @@ impl<T: Trait> Module<T> {
let key_values_taken = delta.iter()
.filter_map(|key| {
child::get_raw(
&origin_contract.trie_id,
origin_contract.child_trie_unique_id(),
&origin_contract.child_trie_info(),
&blake2_256(key),
).map(|value| {
child::kill(
&origin_contract.trie_id,
origin_contract.child_trie_unique_id(),
&origin_contract.child_trie_info(),
&blake2_256(key),
);
@@ -842,8 +828,8 @@ impl<T: Trait> Module<T> {
let tombstone = <TombstoneContractInfo<T>>::new(
// This operation is cheap enough because last_write (delta not included)
// is not this block as it has been checked earlier.
&child::child_root(
&origin_contract.trie_id,
&child::root(
&origin_contract.child_trie_info(),
)[..],
code_hash,
);
@@ -851,8 +837,7 @@ impl<T: Trait> Module<T> {
if tombstone != dest_tombstone {
for (key, value) in key_values_taken {
child::put_raw(
&origin_contract.trie_id,
origin_contract.child_trie_unique_id(),
&origin_contract.child_trie_info(),
&blake2_256(key),
&value,
);
+5 -5
View File
@@ -223,8 +223,7 @@ fn enact_verdict<T: Trait>(
Verdict::Kill => {
<ContractInfoOf<T>>::remove(account);
child::kill_storage(
&alive_contract_info.trie_id,
alive_contract_info.child_trie_unique_id(),
&alive_contract_info.child_trie_info(),
);
<Module<T>>::deposit_event(RawEvent::Evicted(account.clone(), false));
None
@@ -235,7 +234,9 @@ fn enact_verdict<T: Trait>(
}
// Note: this operation is heavy.
let child_storage_root = child::child_root(&alive_contract_info.trie_id);
let child_storage_root = child::root(
&alive_contract_info.child_trie_info(),
);
let tombstone = <TombstoneContractInfo<T>>::new(
&child_storage_root[..],
@@ -245,8 +246,7 @@ fn enact_verdict<T: Trait>(
<ContractInfoOf<T>>::insert(account, &tombstone_info);
child::kill_storage(
&alive_contract_info.trie_id,
alive_contract_info.child_trie_unique_id(),
&alive_contract_info.child_trie_info(),
);
<Module<T>>::deposit_event(RawEvent::Evicted(account.clone(), true));
-3
View File
@@ -200,10 +200,7 @@ impl TrieIdGenerator<u64> for DummyTrieIdGenerator {
*v
});
// TODO: see https://github.com/paritytech/substrate/issues/2325
let mut res = vec![];
res.extend_from_slice(well_known_keys::CHILD_STORAGE_KEY_PREFIX);
res.extend_from_slice(b"default:");
res.extend_from_slice(&new_seed.to_le_bytes());
res.extend_from_slice(&account_id.to_le_bytes());
res