Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
74 commits
Select commit Hold shift + click to select a range
af6a8fd
init from other pr, have some code up to client (not client db),
cheme Sep 23, 2019
4db0a7f
Dumb state_db implementation, mostly boiler plate, but cannot work as
cheme Sep 24, 2019
19df6e9
TODO2 variant for in memory
cheme Sep 25, 2019
3989fea
traits in place, still need to parameterized with blocknb &| branchix
cheme Sep 25, 2019
6a6606d
passing block hash as state in a convoluted way
cheme Sep 25, 2019
934c800
passing block hash as state in a convoluted way
cheme Sep 25, 2019
5d3d47e
Recover previous branch index code without persistence.
cheme Sep 25, 2019
aeab064
let's remove branch range set of changed and removed in next commit
cheme Sep 26, 2019
57e862e
removed delta in branch (not needed if no persistence)
cheme Sep 26, 2019
4edbf27
Before using range instead of hash (we move range to upper client on
cheme Sep 26, 2019
91b87d7
use also block number for query (when not in statedb we need number).
cheme Sep 26, 2019
966a09f
Fixing finalize state algo.
cheme Sep 27, 2019
a37a32f
remove keys from commit set, this likely indicates broken code, will
cheme Sep 27, 2019
4714b70
put a client test, for rest of test reset_storage and genesis build
cheme Sep 27, 2019
5e90b20
Get needed component from branch history-data
cheme Oct 1, 2019
f6956b1
use history from utils
cheme Oct 1, 2019
9ba8057
Test issue is that query on remove hash do not work
cheme Oct 1, 2019
31c1ee3
test commit set
cheme Oct 2, 2019
6209f4d
fix unpin gc
cheme Oct 2, 2019
70f69b6
missing offstate prunnig handling
cheme Oct 2, 2019
6803c86
previous pruning did not make sense, switch to only storing touched key
cheme Oct 2, 2019
2497575
Figure out pruning (see prune_one/prune_two tests).
cheme Oct 3, 2019
e86fdd2
end fixing pruning test, remainings root lib tests to adjust
cheme Oct 3, 2019
e48cf43
fix gc call block index.
cheme Oct 4, 2019
0e3c65e
state_db tests passing.
cheme Oct 4, 2019
13ea088
fix underflow
cheme Oct 4, 2019
92c699c
before switching to absolute indexing (option<u64> to u64).
cheme Oct 4, 2019
bf79879
complete first test in client db, new todo.
cheme Oct 4, 2019
3913ba7
Merge branch 'master' into chain-aux-merge
cheme Oct 4, 2019
4dc2c8d
serialized with default
cheme Oct 7, 2019
03080c4
serialized with default is useless
cheme Oct 7, 2019
8eb5e07
few warning fix
cheme Oct 7, 2019
c89274d
simplify state machine offstate traits, TODO2 will be simple hashmap
cheme Oct 7, 2019
249770b
further simplify
cheme Oct 7, 2019
69fef43
Remove stub state machine implementation.
cheme Oct 7, 2019
a547560
init offstate for tests, but tests will only be interesting with chil…
cheme Oct 7, 2019
397d510
rem todos
cheme Oct 7, 2019
c133a0b
compact casts
cheme Oct 7, 2019
f197d7d
compact casts
cheme Oct 7, 2019
9846613
quick prune test
cheme Oct 7, 2019
d51e98f
break some lines
cheme Oct 7, 2019
fff7cb5
fix
cheme Oct 7, 2019
d55211a
split commit set to have offstate pruning only when needed.
cheme Oct 8, 2019
27ca685
Fix logic of pruning in client (prior it did not take account of last
cheme Oct 8, 2019
64b4102
No need for special revert unless we create archive mode with pruning
cheme Oct 8, 2019
5072f85
Do not keep deletion in branch storage.
cheme Oct 8, 2019
8f8ecc9
sed renaming of offstate to more generic and less incorect 'kv' (as key
cheme Oct 8, 2019
35bc11e
switch overlay transaction to the inner hashmap storage, only change to
cheme Oct 8, 2019
d111288
Remove commit kv from revert.
cheme Oct 9, 2019
dd9dddb
test db better handling
cheme Oct 9, 2019
df950a5
cleaning pass.
cheme Oct 9, 2019
0ca6530
starting using keyspace, storing in the overlay next to the child trie
cheme Oct 9, 2019
0dd36d0
it is tricky, but clearly getting rid of keyspace in overlayed change
cheme Oct 9, 2019
34cf138
wrong approach (the overlay db code for kv is not strictly needed),
cheme Oct 9, 2019
a732231
a todo
cheme Oct 10, 2019
ae180ed
Slight change to 'into_committed' proto, to allow resolving
cheme Oct 10, 2019
c39385b
Merge branch 'chain-aux2' into kv-keyspace
cheme Oct 10, 2019
d53dbba
boilerplate changes.
cheme Oct 10, 2019
9a3d762
Change trie crate from child_trie_soft_min
cheme Oct 10, 2019
da23a0c
prepare change
cheme Oct 10, 2019
3931280
fix proof test
cheme Oct 10, 2019
f6268ab
need a kv_storage function in backend.
cheme Oct 11, 2019
431f93d
add 'kv_store' to state machine backend
cheme Oct 11, 2019
9ccf87f
Merge branch 'chain-aux2' into kv-keyspace
cheme Oct 11, 2019
2b4dad9
state machine missing functions.
cheme Oct 11, 2019
fc16bae
fix other modules compilation.
cheme Oct 11, 2019
ec10b6d
revert into_committed and TODO for caching that is major (child root
cheme Oct 11, 2019
1209ac7
apply renaming
cheme Oct 11, 2019
0eed411
Get and modify previous transaction test.
cheme Oct 11, 2019
42bbd9d
revert into_committed to previous implementation
cheme Oct 11, 2019
b06ec1b
Merge branch 'chain-aux2' into kv-keyspace
cheme Oct 11, 2019
b0dce4a
Merge branch 'master' into chain-aux2
cheme Oct 11, 2019
03e47c2
Merge branch 'chain-aux2' into kv-keyspace
cheme Oct 11, 2019
899e151
Add a place holder for child trie deletion in journals.
cheme Oct 16, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
use history from utils
  • Loading branch information
cheme committed Oct 1, 2019
commit f6956b1b2f860f7034fb6d54238072ce95828785
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions core/state-db/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ parking_lot = "0.9.0"
log = "0.4"
primitives = { package = "substrate-primitives", path = "../../core/primitives" }
codec = { package = "parity-scale-codec", version = "1.0.0", features = ["derive"] }
historied-data = { path = "../../core/utils/historied-data" }

[dev-dependencies]
env_logger = "0.6"
historied-data = { path = "../../core/utils/historied-data", features = ["test"] }
111 changes: 57 additions & 54 deletions core/state-db/src/branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ use std::cmp::Reverse;
use crate::Error;
use std::hash::Hash as StdHash;
use std::convert::TryInto;
use historied_data::tree::{TreeStateTrait, BranchStateTrait, StatesBranchRef, BranchStateRef};


#[derive(Clone, Default, Debug)]
/// Reference to state that is enough for query updates, but not
/// for gc.
/// Values are ordered by branch_ix,
Expand All @@ -32,20 +33,55 @@ use std::convert::TryInto;
/// Note that an alternative could be a pointer to a full state
/// branch for a given branch index, here we use an in memory
/// copied representation in relation to an actual use case.
pub type BranchRanges = Vec<StatesBranchRef>;
pub struct BranchRanges(Vec<StatesBranchRef>);

impl<'a> TreeStateTrait<bool, u64, u64> for &'a BranchRanges {
// TODO do we need parent ix field of statesbranchref??
type Branch = &'a StatesBranchRef;
type Iter = BranchRangesIter<'a>;

fn get_branch(self, i: u64) -> Option<Self::Branch> {
for (b, bi) in self.iter() {
if bi == i {
return Some(b);
} else if bi < i {
break;
}
}
None
}

fn last_index(self) -> u64 {
let l = self.0.len();
if l > 0 {
self.0[l - 1].branch_index
} else {
0
}
}

fn iter(self) -> Self::Iter {
BranchRangesIter(self, self.0.len())
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StatesBranchRef {
pub branch_index: u64,
pub state: LinearStatesRef,
}

/// This is a simple range, end non inclusive.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LinearStatesRef {
pub start: u64,
pub end: u64,
/// Iterator, contains index of last inner struct.
pub struct BranchRangesIter<'a>(&'a BranchRanges, usize);

impl<'a> Iterator for BranchRangesIter<'a> {
type Item = (&'a StatesBranchRef, u64);

fn next(&mut self) -> Option<Self::Item> {
if self.1 > 0 {
Some((
&(self.0).0[self.1 - 1],
(self.0).0[self.1 - 1].branch_index,
))
} else {
None
}
}
}

/// current branches range definition, indexed by branch
Expand Down Expand Up @@ -110,14 +146,14 @@ impl RangeSet {
// (avoid some intermediatory Vec (eg when building Hashset)
let mut result = Vec::new();
if branch_index < self.treshold {
return result;
return BranchRanges(result);
}
let mut previous_start = u64::max_value();
loop {
if let Some(Some(StatesBranch{ state, parent_branch_index, .. })) = self.storage.get(&branch_index) {
// TODO EMCH consider vecdeque ??
let state = if state.end > previous_start {
LinearStatesRef {
BranchStateRef {
start: state.start,
end: previous_start,
}
Expand All @@ -139,7 +175,7 @@ impl RangeSet {
break;
}
}
result
BranchRanges(result)
}


Expand Down Expand Up @@ -235,14 +271,14 @@ impl RangeSet {
let anchor_index = self.add_state(parent_branch_index, number)
.expect("coherent branch index state"); // TODO EMCH fail in add_state
if anchor_index == parent_branch_index {
branch_ranges.pop();
branch_ranges.0.pop();
}
branch_ranges.push(self.state_ref(anchor_index).expect("added just above"));
branch_ranges.0.push(self.state_ref(anchor_index).expect("added just above"));
(branch_ranges, anchor_index)
} else {
let anchor_index = self.add_state(parent_branch_index, number)
.expect("coherent branch index state"); // TODO EMCH fail in add_state
(vec![self.state_ref(anchor_index).expect("added just above")], anchor_index)
(BranchRanges(vec![self.state_ref(anchor_index).expect("added just above")]), anchor_index)
}
}

Expand Down Expand Up @@ -348,7 +384,7 @@ impl RangeSet {

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StatesBranch {
state: LinearStatesRef,
state: BranchStateRef,
can_append: bool,
parent_branch_index: u64,
}
Expand All @@ -369,7 +405,7 @@ impl LinearStates {
pub fn new(offset: u64, parent_branch_index: u64) -> Self {
let offset = offset as u64;
LinearStates {
state: LinearStatesRef {
state: BranchStateRef {
start: offset,
end: offset + 1,
},
Expand All @@ -378,7 +414,7 @@ impl LinearStates {
}
}

pub fn state_ref(&self) -> LinearStatesRef {
pub fn state_ref(&self) -> BranchStateRef {
self.state.clone()
}

Expand Down Expand Up @@ -426,7 +462,7 @@ impl LinearStates {
}

// TODO EMCH end from historied - data
//
#[cfg(test)]
mod test {
use super::*;

Expand Down Expand Up @@ -457,9 +493,6 @@ mod test {

#[test]
fn branch_range_finalize() {
const PREFIX: &[u8] = b"prefix";
const TRESHOLD: &[u8] = b"hijkl";
const LAST_INDEX: &[u8] = b"mnopq";
let with_full_finalize = |
full: bool,
ranges_valid: &[(u64, u64)],
Expand Down Expand Up @@ -492,33 +525,3 @@ mod test {

}


// TODO EMCH use from historied data on merge. (this is just a stub)

#[derive(Debug, Clone)]
pub struct History<V>(Vec<()>, Option<V>);

impl<V> Default for History<V> {
fn default() -> Self {
History(Vec::new(), None)
}
}


impl<V> History<V> {

pub fn set(&mut self, state: &BranchRanges, value: V) {
unimplemented!()
}

pub fn get(&self, state: &BranchRanges) -> Option<&V> {
unimplemented!()
}

pub fn gc(&mut self, state: &RangeSet) {
unimplemented!()
}

}

// End TODO EMCH
3 changes: 2 additions & 1 deletion core/state-db/src/noncanonical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ use std::collections::{HashMap, VecDeque, hash_map::Entry, HashSet};
use super::{Error, DBValue, ChangeSet, CommitSet, MetaDb, Hash, to_meta_key, OffstateKey};
use codec::{Encode, Decode};
use log::trace;
use crate::branch::{RangeSet, BranchRanges, History};
use crate::branch::{RangeSet, BranchRanges};
use historied_data::tree::History;

const NON_CANONICAL_JOURNAL: &[u8] = b"noncanonical_journal";
const NON_CANONICAL_OFFSTATE_JOURNAL: &[u8] = b"offstate_noncanonical_journal";
Expand Down
1 change: 0 additions & 1 deletion core/utils/historied-data/src/linear.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,6 @@ impl<'a, F: SerializedConfig> Serialized<'a, F> {
}

// Usize encoded as le u64 (only for internal indexing).
// TODO EMCH change usize encoding to u32?
fn read_le_usize(&self, pos: usize) -> usize {
let mut buffer = [0u8; SIZE_BYTE_LEN];
buffer.copy_from_slice(&self.0[pos..pos + SIZE_BYTE_LEN]);
Expand Down
45 changes: 31 additions & 14 deletions core/utils/historied-data/src/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ use rstd::vec::Vec;
use rstd::collections::btree_map::BTreeMap;
use rstd::convert::{TryFrom, TryInto};

/// Trait defining a state for querying or modifying a tree.
/// This is therefore the representation of a branch.
/// TODO EMCH rename to BranchesStateTrait?
pub trait TreeStateTrait<S, I, BI> {
type Branch: BranchStateTrait<S, BI>;
type Iter: Iterator<Item = (Self::Branch, I)>;
Expand All @@ -46,6 +49,8 @@ pub trait TreeStateTrait<S, I, BI> {
fn iter(self) -> Self::Iter;
}

/// Trait defining a state for querying or modifying a branch.
/// This is therefore the representation of a branch state.
pub trait BranchStateTrait<S, I> {

fn get_node(&self, i: I) -> S;
Expand All @@ -54,7 +59,6 @@ pub trait BranchStateTrait<S, I> {
fn last_index(&self) -> I;
}


impl<'a> TreeStateTrait<bool, u64, u64> for &'a StatesRef {
type Branch = (&'a StatesBranchRef, Option<u64>);
type Iter = StatesRefIter<'a>;
Expand Down Expand Up @@ -98,7 +102,7 @@ impl<'a> TreeStateTrait<bool, u64, u64> for &'a StatesRef {
}

#[derive(Debug, Clone)]
#[cfg_attr(any(test, feature = "test"), derive(PartialEq))]
#[cfg_attr(any(test, feature = "test"), derive(PartialEq, Eq))]
pub struct BranchState {
/// Index of first element (only use for indexed access).
/// Element before offset are not in state.
Expand All @@ -113,11 +117,10 @@ pub struct BranchState {
}

/// This is a simple range, end non inclusive.
#[derive(Debug, Clone)]
#[cfg_attr(any(test, feature = "test"), derive(PartialEq))]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BranchStateRef {
start: u64,
end: u64,
pub start: u64,
pub end: u64,
}

impl<'a> BranchStateTrait<bool, u64> for (&'a StatesBranchRef, Option<u64>) {
Expand All @@ -136,6 +139,21 @@ impl<'a> BranchStateTrait<bool, u64> for (&'a StatesBranchRef, Option<u64>) {

}

impl<'a> BranchStateTrait<bool, u64> for &'a StatesBranchRef {

fn get_node(&self, i: u64) -> bool {
i >= self.state.start && i < self.state.end
}

fn last_index(&self) -> u64 {
// underflow should not happen as long as branchstateref are not allowed to be empty.
self.state.end - 1
}

}



impl Default for BranchState {
// initialize with one element
fn default() -> Self {
Expand Down Expand Up @@ -243,7 +261,7 @@ impl Default for TestStates {
}

#[derive(Debug, Clone)]
#[cfg_attr(any(test, feature = "test"), derive(PartialEq))]
#[cfg_attr(any(test, feature = "test"), derive(PartialEq, Eq))]
pub struct StatesBranch {
// this is the key (need to growth unless full gc (can still have
// content pointing to it even if it seems safe to reuse a previously
Expand All @@ -256,11 +274,10 @@ pub struct StatesBranch {
state: BranchState,
}

#[derive(Debug, Clone)]
#[cfg_attr(any(test, feature = "test"), derive(PartialEq))]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StatesBranchRef {
branch_index: u64,
state: BranchStateRef,
pub branch_index: u64,
pub state: BranchStateRef,
}


Expand Down Expand Up @@ -336,7 +353,7 @@ impl TestStates {
branch_ref.state.end = previous_origin_node_index + 1;
}
previous_origin_node_index = branch.origin_node_index;
// TODO EMCH consider reversing state_ref
// vecdeque would be better suited
result.insert(0, branch_ref);
branch_index = branch.origin_branch_index;
} else {
Expand Down Expand Up @@ -447,6 +464,7 @@ impl TestStates {
/// First field is the actual history against which we run
/// the state.
/// Second field is an optional value for the no match case.
/// TODO EMCH consider removing second field (not use out of test)
#[derive(Debug, Clone)]
#[cfg_attr(any(test, feature = "test"), derive(PartialEq))]
pub struct History<V>(Vec<HistoryBranch<V>>, Option<V>);
Expand Down Expand Up @@ -564,7 +582,7 @@ impl<V> History<V> {
return self.1.as_ref();
}

// TODO EMCH reverse loops ? probably.
// TODO EMCH switch loops ? probably.
for (state_branch, state_index) in state.iter() {
while index > 0 {
index -= 1;
Expand Down Expand Up @@ -805,7 +823,6 @@ mod test {
// cannot create when dropped happen on branch
assert_eq!(Some(false), states.branch_state_mut(1).map(|ls| ls.add_state()));

// TODO add content through branching
assert!(states.get(1, 1));
// 0> 1: _ _ X
// | |> 3: 1
Expand Down