Skip to content

Commit

Permalink
Make kvdb compatible with old checkpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
kvinwang committed Oct 9, 2023
1 parent a42f5c5 commit de6a04f
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 1,829 deletions.
8 changes: 6 additions & 2 deletions crates/phala-trie-storage/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ use std::iter::FromIterator;
use parity_scale_codec::Codec;
use sp_core::storage::ChildInfo;
use sp_core::Hasher;
use sp_state_machine::{Backend, BackendTransaction, IterArgs, TrieBackend, TrieBackendBuilder};
use sp_state_machine::{Backend, IterArgs, TrieBackend, TrieBackendBuilder};
use sp_trie::{trie_types::TrieDBMutBuilderV0 as TrieDBMutBuilder, TrieMut};

pub use memdb::GenericMemoryDB as MemoryDB;
pub use sp_state_machine::BackendTransaction;

/// Storage key.
pub type StorageKey = Vec<u8>;
Expand Down Expand Up @@ -152,7 +153,10 @@ where
}

/// Apply storage changes calculated from `calc_root_if_changes`.
pub fn apply_changes(&mut self, root: H::Out, transaction: MemoryDB<H>) {
pub fn apply_changes(&mut self, root: H::Out, transaction: BackendTransaction<H>)
where
H::Out: From<[u8; 32]>,
{
let mut storage = core::mem::take(self).0.into_storage();
storage.consolidate(transaction);
let _ = core::mem::replace(&mut self.0, TrieBackendBuilder::new(storage, root).build());
Expand Down
16 changes: 11 additions & 5 deletions crates/phala-trie-storage/src/memdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use hash_db::{
AsHashDB, AsPlainDB, HashDB, HashDBRef, Hasher as KeyHasher, PlainDB, PlainDBRef, Prefix,
};
pub(crate) use im::ordmap::{Entry, OrdMap as Map};
use std::{borrow::Borrow, cmp::Eq, hash, marker::PhantomData};
use std::{borrow::Borrow, cmp::Eq, convert::TryFrom, hash, marker::PhantomData};

use sp_state_machine::{DefaultError, TrieBackendStorage};
use trie_db::DBValue;
Expand Down Expand Up @@ -261,12 +261,18 @@ where
}

/// Consolidate all the entries of `other` into `self`.
pub fn consolidate(&mut self, other: Self) {
for (key, (value, rc)) in other.data {
pub fn consolidate(&mut self, mut other: crate::BackendTransaction<H>)
where
KF::Key: From<[u8; 32]>,
T: From<Vec<u8>>,
{
for (key, (value, rc)) in other.drain() {
let key: [u8; 32] = TryFrom::try_from(&key[key.len() - 32..]).expect("Should never fail");
let key = key.into();
match self.data.entry(key) {
Entry::Occupied(mut entry) => {
if entry.get().1 < 0 {
entry.get_mut().0 = value;
entry.get_mut().0 = value.into();
}

entry.get_mut().1 += rc;
Expand All @@ -275,7 +281,7 @@ where
}
}
Entry::Vacant(entry) => {
entry.insert((value, rc));
entry.insert((value.into(), rc));
}
}
}
Expand Down
15 changes: 5 additions & 10 deletions crates/pink/runtime/src/runtime.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
mod extension;
mod pallet_pink;
mod weights;

use crate::types::{AccountId, Balance, BlockNumber, Hash, Hashing, Nonce};
use frame_support::{
Expand All @@ -10,7 +9,8 @@ use frame_support::{
};
use log::info;
use pallet_contracts::{
migration::{v09, v10, v11, v12},
migration::{v11, v12},
weights::SubstrateWeight,
Config, Frame, Migration, Schedule,
};
use sp_runtime::{traits::IdentityLookup, Perbill};
Expand Down Expand Up @@ -68,7 +68,7 @@ impl pallet_balances::Config for PinkRuntime {
type FreezeIdentifier = ();
type MaxHolds = ();
type MaxFreezes = ();
type RuntimeHoldReason = ();
type RuntimeHoldReason = RuntimeHoldReason;
}

impl frame_system::Config for PinkRuntime {
Expand Down Expand Up @@ -142,7 +142,7 @@ impl Config for PinkRuntime {
type CallFilter = frame_support::traits::Nothing;
type CallStack = [Frame<Self>; 5];
type WeightPrice = Pink;
type WeightInfo = weights::PinkWeights<Self>;
type WeightInfo = SubstrateWeight<Self>;
type ChainExtension = extension::PinkExtension;
type Schedule = DefaultSchedule;
type DepositPerByte = DepositPerStorageByte;
Expand All @@ -153,12 +153,7 @@ impl Config for PinkRuntime {
type MaxStorageKeyLen = MaxStorageKeyLen;
type UnsafeUnstableInterface = ConstBool<false>;
type MaxDebugBufferLen = MaxDebugBufferLen;
type Migrations = (
v09::Migration<Self>,
v10::Migration<Self>,
v11::Migration<Self>,
v12::Migration<Self, Balances>,
);
type Migrations = (v11::Migration<Self>, v12::Migration<Self, Balances>);
type CodeHashLockupDepositPercent = CodeHashLockupDepositPercent;
type MaxDelegateDependencies = ConstU32<32>;
type RuntimeHoldReason = RuntimeHoldReason;
Expand Down
5 changes: 0 additions & 5 deletions crates/pink/runtime/src/runtime/pallet_pink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,6 @@ pub mod pallet {
);
UncheckedFrom::unchecked_from(<T as frame_system::Config>::Hashing::hash(&buf))
}

fn deposit_address(contract_addr: &T::AccountId) -> T::AccountId {
let entropy = (b"contract_depo_v1", contract_addr).using_encoded(T::Hashing::hash);
UncheckedFrom::unchecked_from(entropy)
}
}

impl<T: Config> Pallet<T> {
Expand Down
Loading

0 comments on commit de6a04f

Please sign in to comment.