Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce the KVDB to phala runtime #749

Closed
wants to merge 35 commits into from
Closed

Conversation

g302ge
Copy link

@g302ge g302ge commented Apr 25, 2022

This PR is targetd to replace phactory and pink MemoryDB into underlying KVDB.

Currenly, Pink and Phactory both use the MemoryDB, Morever after a specificed interval, Phactory will take a checkpoint not only over it's RuntimeState but the Pink storage. Unfortunatly, just replace the Pink storage could cause the whole system into inconsistence. For example:

Assume that, Phactory is now sync 100th block from pherry and Pink is working on 99th block, if we just simply commit the transaction in Pink into the underlying storage, when the phactory crash now, Phactory checkpoint maybe at 59th block. When phala restart, from 59th block to 99 block storage changes is duplicated,

Implementation Details

firstly we should introduce the KVDB as a crate to support base semantics used for both Pink and Phactory.

Pink is just need a TrieBackend. So simplly wraps the underlying storage as TrieBackendStorage makes thing sound. And for Phactory it just needs the RawDB interface not HashDB. Due to this, we should introduce another interface over pkvdb to supply the RawDB interface.

And then, commit all transaction both Pink changes and Phactory checkpoint per block makes thing sound.

@@ -52,6 +51,7 @@ members = [
"crates/phala-async-executor",
"crates/phala-allocator",
"crates/pink",
"crates/pkvdb",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bad indentation

@@ -5,4 +5,4 @@ edition = "2018"
description = "Platform abstraction layer for Phactory Runtime"

[dependencies]
anyhow = "1.0.43"
anyhow = "1.0.43"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Drop this change

@@ -2,7 +2,7 @@ use super::*;

// For bin_api
impl<Platform: pal::Platform + Serialize + DeserializeOwned> Phactory<Platform> {
fn get_info_json(&self) -> Result<Value, Value> {
fn get_info_json(&mut self) -> Result<Value, Value> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks not good to turn this top level state into mut.

use std::{io::Write, marker::PhantomData};
use std::{path::Path, str};
use std::{marker::PhantomData};
use std::{ str};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fmt

@@ -74,7 +74,7 @@ mod types;

// TODO: Completely remove the reference to Phala/Khala runtime. Instead we can create a minimal
// runtime definition locally.
type RuntimeHasher = <chain::Runtime as frame_system::Config>::Hashing;
pub(crate) type RuntimeHasher = <chain::Runtime as frame_system::Config>::Hashing;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't it available in sub mods without the pub(crate)?


impl<H: Hasher> Default for PhalaTrieStorage<H> {
fn default() -> Self {
Self::Empty
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where the Empty is used? Is it possible to remove this variant?

{
fn from(v: Vec<u8>) -> Self {
let (rc, val) = v.split_at(4);
let value = T::from(val);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still double allocation

db: Arc<Mutex<DB>>,
pink_overlay: Arc<Mutex<MemoryDB<H>>>,
sync_overlay: Arc<Mutex<MemoryDB<H>>>,
raw_map: BTreeMap<Vec<u8>, Vec<u8>>,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only one overlay(the raw_map) here should be enough. pink/sync are user logic.

sync_overlay: Arc<Mutex<MemoryDB<H>>>,
raw_map: BTreeMap<Vec<u8>, Vec<u8>>,
pub pink_root: Option<H::Out>,
pub sync_root: Option<H::Out>,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The root should be tracked by user, don't put them in this low level DB layer.

}
}

impl<H: Hasher> HashDB<H, DBValue> for TrieEssenceStorage<H>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So many impl HashDB for .... Can we reduce some of them?

@h4x3rotab
Copy link
Contributor

Closed. The work is continued in #928

@h4x3rotab h4x3rotab closed this Aug 31, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants