mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-12 19:21:13 +00:00
Fix key collision for child trie (#4162)
* In progress, runtime io must switch to future proof root + child_specific (unique id) + u32 type. * Switch interface, sr-io seems ok, rpc could use similar interface to sr-io, genesis json broken if there is child trie in existing encoding genesis. * test from previous implementation. * fix proving test. * Restore Keyspacedb from other branch, only apply to child trie. * Removing unneeded child_info from child root (child info are stored if things changed, otherwhise the root does not change). * Switch rpc to use same format as ext: more future proof. * use root from child info for trie backend essence. * Breaking long lines. * Update doc and clean pr a bit. * fix error type * Restore removed doc on merge and update sr-io doc. * Switch child storage api to use directly unique id, if managed id where to be put in place, the api will change at this time. * Clean deprecated host interface from child. * Removing assertion on child info (can fail depending on root memoization). * merging child info in the overlay when possible. * child iteration by prefix using child_info. * Using ChainInfo in frame support. ChainInfo gets redesign to avoid buffers allocation on every calls. * Add length of root to the data of child info. * comments * Encode compact. * Remove child info with root. * Fix try_update condition. * Comment Ext child root caching. * Replace tuples by struct with field * remove StorageTuple alias. * Fix doc tests, and remove StorageOverlay and ChildStorageOverlay aliases.
This commit is contained in:
@@ -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, &blake2_256(location)))
|
||||
trie_id.and_then(|id| child::get_raw(id, crate::trie_unique_id(&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))
|
||||
@@ -173,13 +173,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);
|
||||
child::kill_storage(&info.trie_id, info.child_trie_unique_id());
|
||||
<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);
|
||||
child::kill_storage(&info.trie_id, info.child_trie_unique_id());
|
||||
AliveContractInfo::<T> {
|
||||
code_hash,
|
||||
storage_size: T::StorageSizeOffset::get(),
|
||||
@@ -217,14 +217,18 @@ 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[..], &blake2_256(&k)) {
|
||||
if let Some(value) = child::get_raw(
|
||||
&new_info.trie_id[..],
|
||||
new_info.child_trie_unique_id(),
|
||||
&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[..], &blake2_256(&k), &value[..]);
|
||||
child::put_raw(&new_info.trie_id[..], new_info.child_trie_unique_id(), &blake2_256(&k), &value[..]);
|
||||
} else {
|
||||
child::kill(&new_info.trie_id[..], &blake2_256(&k));
|
||||
child::kill(&new_info.trie_id[..], new_info.child_trie_unique_id(), &blake2_256(&k));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -223,6 +223,19 @@ pub struct RawAliveContractInfo<CodeHash, Balance, BlockNumber> {
|
||||
pub last_write: Option<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[..])
|
||||
}
|
||||
}
|
||||
|
||||
/// 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 type TombstoneContractInfo<T> =
|
||||
RawTombstoneContractInfo<<T as system::Trait>::Hash, <T as system::Trait>::Hashing>;
|
||||
|
||||
@@ -793,8 +806,17 @@ impl<T: Trait> Module<T> {
|
||||
|
||||
let key_values_taken = delta.iter()
|
||||
.filter_map(|key| {
|
||||
child::get_raw(&origin_contract.trie_id, &blake2_256(key)).map(|value| {
|
||||
child::kill(&origin_contract.trie_id, &blake2_256(key));
|
||||
child::get_raw(
|
||||
&origin_contract.trie_id,
|
||||
origin_contract.child_trie_unique_id(),
|
||||
&blake2_256(key),
|
||||
).map(|value| {
|
||||
child::kill(
|
||||
&origin_contract.trie_id,
|
||||
origin_contract.child_trie_unique_id(),
|
||||
&blake2_256(key),
|
||||
);
|
||||
|
||||
(key, value)
|
||||
})
|
||||
})
|
||||
@@ -803,13 +825,20 @@ 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.
|
||||
&sp_io::storage::child_root(&origin_contract.trie_id)[..],
|
||||
&child::child_root(
|
||||
&origin_contract.trie_id,
|
||||
)[..],
|
||||
code_hash,
|
||||
);
|
||||
|
||||
if tombstone != dest_tombstone {
|
||||
for (key, value) in key_values_taken {
|
||||
child::put_raw(&origin_contract.trie_id, &blake2_256(key), &value);
|
||||
child::put_raw(
|
||||
&origin_contract.trie_id,
|
||||
origin_contract.child_trie_unique_id(),
|
||||
&blake2_256(key),
|
||||
&value,
|
||||
);
|
||||
}
|
||||
|
||||
return Err("Tombstones don't match");
|
||||
@@ -887,7 +916,7 @@ decl_storage! {
|
||||
impl<T: Trait> OnFreeBalanceZero<T::AccountId> for Module<T> {
|
||||
fn on_free_balance_zero(who: &T::AccountId) {
|
||||
if let Some(ContractInfo::Alive(info)) = <ContractInfoOf<T>>::take(who) {
|
||||
child::kill_storage(&info.trie_id);
|
||||
child::kill_storage(&info.trie_id, info.child_trie_unique_id());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ use sp_runtime::traits::{Bounded, CheckedDiv, CheckedMul, Saturating, Zero,
|
||||
SaturatedConversion};
|
||||
use support::traits::{Currency, ExistenceRequirement, Get, WithdrawReason, OnUnbalanced};
|
||||
use support::StorageMap;
|
||||
use support::storage::child;
|
||||
|
||||
#[derive(PartialEq, Eq, Copy, Clone)]
|
||||
#[must_use]
|
||||
@@ -99,7 +100,7 @@ fn try_evict_or_and_pay_rent<T: Trait>(
|
||||
if balance < subsistence_threshold {
|
||||
// The contract cannot afford to leave a tombstone, so remove the contract info altogether.
|
||||
<ContractInfoOf<T>>::remove(account);
|
||||
sp_io::storage::child_storage_kill(&contract.trie_id);
|
||||
child::kill_storage(&contract.trie_id, contract.child_trie_unique_id());
|
||||
return (RentOutcome::Evicted, None);
|
||||
}
|
||||
|
||||
@@ -146,7 +147,9 @@ fn try_evict_or_and_pay_rent<T: Trait>(
|
||||
// threshold, so it leaves a tombstone.
|
||||
|
||||
// Note: this operation is heavy.
|
||||
let child_storage_root = sp_io::storage::child_root(&contract.trie_id);
|
||||
let child_storage_root = child::child_root(
|
||||
&contract.trie_id,
|
||||
);
|
||||
|
||||
let tombstone = <TombstoneContractInfo<T>>::new(
|
||||
&child_storage_root[..],
|
||||
@@ -155,7 +158,7 @@ fn try_evict_or_and_pay_rent<T: Trait>(
|
||||
let tombstone_info = ContractInfo::Tombstone(tombstone);
|
||||
<ContractInfoOf<T>>::insert(account, &tombstone_info);
|
||||
|
||||
sp_io::storage::child_storage_kill(&contract.trie_id);
|
||||
child::kill_storage(&contract.trie_id, contract.child_trie_unique_id());
|
||||
|
||||
return (RentOutcome::Evicted, Some(tombstone_info));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user