Use correct prefix needed by child trie in contract (#2292)

* resolve child trie usage

* update locks

* increase version

* fix test

* Revert "update locks"

This reverts commit 6f537458b39df4a3bf05a311253986f29289f391.
This commit is contained in:
thiolliere
2019-04-16 15:38:12 +02:00
committed by Sergei Pepyakin
parent fc0b348de4
commit f7fc02fdf2
2 changed files with 30 additions and 15 deletions
+6 -1
View File
@@ -105,6 +105,7 @@ use srml_support::dispatch::{Result, Dispatchable};
use srml_support::{Parameter, StorageMap, StorageValue, decl_module, decl_event, decl_storage, storage::child}; use srml_support::{Parameter, StorageMap, StorageValue, decl_module, decl_event, decl_storage, storage::child};
use srml_support::traits::{OnFreeBalanceZero, OnUnbalanced, Currency}; use srml_support::traits::{OnFreeBalanceZero, OnUnbalanced, Currency};
use system::{ensure_signed, RawOrigin}; use system::{ensure_signed, RawOrigin};
use substrate_primitives::storage::well_known_keys::CHILD_STORAGE_KEY_PREFIX;
use timestamp; use timestamp;
pub type CodeHash<T> = <T as system::Trait>::Hash; pub type CodeHash<T> = <T as system::Trait>::Hash;
@@ -158,7 +159,11 @@ where
let mut buf = Vec::new(); let mut buf = Vec::new();
buf.extend_from_slice(account_id.as_ref()); buf.extend_from_slice(account_id.as_ref());
buf.extend_from_slice(&new_seed.to_le_bytes()[..]); buf.extend_from_slice(&new_seed.to_le_bytes()[..]);
T::Hashing::hash(&buf[..]).as_ref().into()
CHILD_STORAGE_KEY_PREFIX.iter()
.chain(T::Hashing::hash(&buf[..]).as_ref().iter())
.cloned()
.collect()
} }
} }
+24 -14
View File
@@ -34,11 +34,12 @@ use assert_matches::assert_matches;
use crate::{ use crate::{
ContractAddressFor, GenesisConfig, Module, RawEvent, ContractAddressFor, GenesisConfig, Module, RawEvent,
Trait, ComputeDispatchFee, TrieIdGenerator, TrieId, Trait, ComputeDispatchFee, TrieIdGenerator, TrieId,
AccountInfo, AccountInfoOf, AccountInfo, AccountInfoOf, TrieIdFromParentCounter
}; };
use substrate_primitives::storage::well_known_keys; use substrate_primitives::storage::well_known_keys;
use parity_codec::{Encode, Decode, KeyedVec}; use parity_codec::{Encode, Decode, KeyedVec};
use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::atomic::{AtomicUsize, Ordering};
use crate::account_db::{DirectAccountDb, OverlayAccountDb, AccountDb};
mod contract { mod contract {
// Re-export contents of the root. This basically // Re-export contents of the root. This basically
@@ -238,29 +239,37 @@ fn refunds_unused_gas() {
#[test] #[test]
fn account_removal_removes_storage() { fn account_removal_removes_storage() {
let unique_id1 = b"unique_id1";
let unique_id2 = b"unique_id2";
with_externalities( with_externalities(
&mut ExtBuilder::default().existential_deposit(100).build(), &mut ExtBuilder::default().existential_deposit(100).build(),
|| { || {
let trie_id1 = <Test as Trait>::TrieIdGenerator::trie_id(&1);
let trie_id2 = <Test as Trait>::TrieIdGenerator::trie_id(&2);
let key1 = &[1; 32];
let key2 = &[2; 32];
// Set up two accounts with free balance above the existential threshold. // Set up two accounts with free balance above the existential threshold.
{ {
Balances::deposit_creating(&1, 110); Balances::deposit_creating(&1, 110);
AccountInfoOf::<Test>::insert(1, &AccountInfo { AccountInfoOf::<Test>::insert(1, &AccountInfo {
trie_id: unique_id1.to_vec(), trie_id: trie_id1.clone(),
storage_size: 0, storage_size: 0,
}); });
child::put(&unique_id1[..], &b"foo".to_vec(), &b"1".to_vec());
assert_eq!(child::get(&unique_id1[..], &b"foo".to_vec()), Some(b"1".to_vec())); let mut overlay = OverlayAccountDb::<Test>::new(&DirectAccountDb);
child::put(&unique_id1[..], &b"bar".to_vec(), &b"2".to_vec()); overlay.set_storage(&1, key1.clone(), Some(b"1".to_vec()));
overlay.set_storage(&1, key2.clone(), Some(b"2".to_vec()));
DirectAccountDb.commit(overlay.into_change_set());
Balances::deposit_creating(&2, 110); Balances::deposit_creating(&2, 110);
AccountInfoOf::<Test>::insert(2, &AccountInfo { AccountInfoOf::<Test>::insert(2, &AccountInfo {
trie_id: unique_id2.to_vec(), trie_id: trie_id2.clone(),
storage_size: 0, storage_size: 0,
}); });
child::put(&unique_id2[..], &b"hello".to_vec(), &b"3".to_vec());
child::put(&unique_id2[..], &b"world".to_vec(), &b"4".to_vec()); let mut overlay = OverlayAccountDb::<Test>::new(&DirectAccountDb);
overlay.set_storage(&2, key1.clone(), Some(b"3".to_vec()));
overlay.set_storage(&2, key2.clone(), Some(b"4".to_vec()));
DirectAccountDb.commit(overlay.into_change_set());
} }
// Transfer funds from account 1 of such amount that after this transfer // Transfer funds from account 1 of such amount that after this transfer
@@ -272,15 +281,16 @@ fn account_removal_removes_storage() {
// Verify that all entries from account 1 is removed, while // Verify that all entries from account 1 is removed, while
// entries from account 2 is in place. // entries from account 2 is in place.
{ {
assert_eq!(child::get_raw(&unique_id1[..], &b"foo".to_vec()), None); // let a: <Test as system::Trait>::AccountId = 1;
assert_eq!(child::get_raw(&unique_id1[..], &b"bar".to_vec()), None); assert!(<AccountDb<Test>>::get_storage(&DirectAccountDb, &1, Some(&trie_id1), key1).is_none());
assert!(<AccountDb<Test>>::get_storage(&DirectAccountDb, &1, Some(&trie_id1), key2).is_none());
assert_eq!( assert_eq!(
child::get(&unique_id2[..], &b"hello".to_vec()), <AccountDb<Test>>::get_storage(&DirectAccountDb, &2, Some(&trie_id2), key1),
Some(b"3".to_vec()) Some(b"3".to_vec())
); );
assert_eq!( assert_eq!(
child::get(&unique_id2[..], &b"world".to_vec()), <AccountDb<Test>>::get_storage(&DirectAccountDb, &2, Some(&trie_id2), key2),
Some(b"4".to_vec()) Some(b"4".to_vec())
); );
} }