Skip to content

Commit

Permalink
Copy sources from CodeChain
Browse files Browse the repository at this point in the history
  • Loading branch information
Seulgi Kim committed Jan 3, 2020
1 parent 1100849 commit a6e067f
Show file tree
Hide file tree
Showing 16 changed files with 3,100 additions and 0 deletions.
51 changes: 51 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
on: [push, pull_request]

name: test

jobs:
clippy:
name: Actions - clippy
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
with:
fetch-depth: 1
- uses: actions-rs/toolchain@v1
with:
toolchain: nightly-2019-11-06
components: clippy
profile: minimal
override: true
- run: cargo fetch --verbose
- run: cargo clippy --all --all-targets -- -D warnings

rustfmt:
name: Actions - rustfmt
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
with:
fetch-depth: 1
- uses: actions-rs/toolchain@v1
with:
toolchain: nightly-2019-11-06
components: rustfmt
profile: minimal
override: true
- run: cargo fmt -- --check

unit-test:
name: Actions - unit test
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [macOS-latest, ubuntu-latest]
steps:
- uses: actions/checkout@v1
with:
fetch-depth: 1
- run: cargo fetch --verbose
- run: cargo build
- run: cargo test --verbose --all
env:
RUST_BACKTRACE: 1
22 changes: 22 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[package]
name = "merkle-trie"
version = "0.1.0"
authors = ["CodeChain Team <[email protected]>"]
repository = "http://github.com/CodeChain-io/rust-merkle-trie"
license = "AGPL-3.0"
edition = "2018"

[dependencies]
ccrypto = { package = "codechain-crypto", git = "https://github.com/CodeChain-io/rust-codechain-crypto.git", version = "0.1" }
cdb = { package = "codechain-db", git = "https://github.com/CodeChain-io/rust-codechain-db.git", version = "0.1" }
primitives = { git = "https://github.com/CodeChain-io/rust-codechain-primitives.git", version = "0.4" }
rand = "0.6.1"
rlp = { git = "https://github.com/CodeChain-io/rlp.git", version = "0.4" }
rlp_derive = { git = "https://github.com/CodeChain-io/rlp.git", version = "0.2" }
snap = "0.2"

[dev-dependencies]
kvdb = "0.1"
kvdb-rocksdb = "0.1"
standardmap = { package = "trie-standardmap", git = "https://github.com/CodeChain-io/trie-standardmap.git", version = "0.2" }
tempfile = "3.1.0"
156 changes: 156 additions & 0 deletions benches/triedb.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
// Copyright 2019-2020 Kodebox, Inc.
// This file is part of CodeChain.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

#![feature(test)]

extern crate test;

use cdb::{new_journaldb, Algorithm, JournalDB};
use kvdb::DBTransaction;
use kvdb_rocksdb::{CompactionProfile, Database, DatabaseConfig};
use merkle_trie::{Trie, TrieFactory, TrieMut};
use primitives::H256;
use rand::random;
use std::path::Path;
use std::sync::Arc;
use tempfile::{tempdir, TempDir};
use test::Bencher;

struct TestDB {
_dir: TempDir,
db: Arc<Database>,
journal: Box<dyn JournalDB>,
root: H256,
}

impl TestDB {
// Replicate CodeChain's db config
fn config(path: &Path) -> DatabaseConfig {
let mut config = DatabaseConfig::with_columns(Some(1));
config.memory_budget = Default::default();
config.compaction = CompactionProfile::auto(path);
config
}

fn populate(path: &Path, size: usize) -> H256 {
// Create database
let config = Self::config(path);
let db = Arc::new(Database::open(&config, path.to_str().unwrap()).unwrap());
let mut journal = new_journaldb(db.clone(), Algorithm::Archive, Some(0));
let mut root = H256::zero();
{
let hashdb = journal.as_hashdb_mut();
let mut trie = TrieFactory::create(hashdb, &mut root);
for i in 0..size {
trie.insert(&i.to_be_bytes(), &i.to_be_bytes()).unwrap();
}
}
let mut batch = DBTransaction::new();
journal.journal_under(&mut batch, 0, &H256::zero()).unwrap();
db.write_buffered(batch);
db.flush().unwrap();

root
}

fn new(size: usize) -> Self {
// Create temporary directory
let dir = tempdir().unwrap();
let root = Self::populate(dir.path(), size);

// Create database
let config = Self::config(dir.path());
let db = Arc::new(Database::open(&config, dir.path().to_str().unwrap()).unwrap());
let journal = new_journaldb(db.clone(), Algorithm::Archive, Some(0));

Self {
_dir: dir,
db,
journal,
root,
}
}

fn trie<'db>(&'db self) -> impl Trie + 'db {
let hashdb = self.journal.as_hashdb();
TrieFactory::readonly(hashdb, &self.root).unwrap()
}

fn trie_mut<'db>(&'db mut self) -> impl TrieMut + 'db {
let hashdb = self.journal.as_hashdb_mut();
TrieFactory::create(hashdb, &mut self.root)
}

fn flush(&mut self) {
let mut batch = DBTransaction::new();
self.journal.journal_under(&mut batch, 0, &H256::zero()).unwrap();
self.db.write_buffered(batch);
self.db.flush().unwrap();
}
}

const DB_SIZE: usize = 10000;
const BATCH: usize = 10000;

#[bench]
fn bench_read_single(b: &mut Bencher) {
let db = TestDB::new(DB_SIZE);
b.iter(|| {
let trie = db.trie();
let item = random::<usize>() % DB_SIZE;
let _ = trie.get(&item.to_be_bytes()).unwrap().unwrap();
});
}

#[bench]
fn bench_read_multiple(b: &mut Bencher) {
let db = TestDB::new(DB_SIZE);
b.iter(|| {
let trie = db.trie();
for _ in 0..BATCH {
let item = random::<usize>() % DB_SIZE;
let _ = trie.get(&item.to_be_bytes()).unwrap().unwrap();
}
});
}

#[bench]
fn bench_write_single(b: &mut Bencher) {
let mut db = TestDB::new(DB_SIZE);
b.iter(|| {
{
let mut trie = db.trie_mut();
let item = random::<usize>() % DB_SIZE + DB_SIZE;
let _ = trie.insert(&item.to_be_bytes(), &item.to_be_bytes()).unwrap();
}
db.flush();
});
}

#[bench]
fn bench_write_multiple(b: &mut Bencher) {
let mut db = TestDB::new(DB_SIZE);
b.iter(|| {
{
let mut trie = db.trie_mut();
for _ in 0..BATCH {
let item = random::<usize>() % DB_SIZE + DB_SIZE;
let _ = trie.insert(&item.to_be_bytes(), &item.to_be_bytes()).unwrap();
}
}
db.flush();
});
}
63 changes: 63 additions & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
indent_style = "Block"
use_small_heuristics = "Off" # "Default"
binop_separator = "Front"
# combine_control_expr = true
comment_width = 120 # 80
condense_wildcard_suffixes = true # false
control_brace_style = "AlwaysSameLine"
# disable_all_formatting = false
error_on_line_overflow = false # true
# error_on_unformatted = false
fn_args_layout = "Tall"
brace_style = "PreferSameLine" # "SameLineWhere"
empty_item_single_line = true
enum_discrim_align_threshold = 0
fn_single_line = false
# where_single_line = false
force_explicit_abi = true
format_strings = false
format_macro_matchers = false
format_macro_bodies = true
hard_tabs = false
imports_indent = "Block" # "Visual"
imports_layout = "Mixed"
merge_imports = false
match_block_trailing_comma = false
max_width = 120 # 100
merge_derives = true
# force_multiline_blocks = false
newline_style = "Unix"
normalize_comments = false
remove_nested_parens = true
reorder_imports = true
reorder_modules = true
# reorder_impl_items = false
# report_todo = "Never"
# report_fixme = "Never"
space_after_colon = true
space_before_colon = false
struct_field_align_threshold = 0
spaces_around_ranges = false
## struct_lit_single_line = true
tab_spaces = 4
trailing_comma = "Vertical"
trailing_semicolon = false # true
# type_punctuation_density = "Wide"
use_field_init_shorthand = true # false
use_try_shorthand = true # false
# format_code_in_doc_comments = false
wrap_comments = false
match_arm_blocks = true
overflow_delimited_expr = true
blank_lines_upper_bound = 2 # 1
blank_lines_lower_bound = 0
# required_version
hide_parse_errors = false
color = "Always" # "Auto"
unstable_features = false
# license_template_path
# ignore
edition = "2018"
# version
normalize_doc_attributes = true # false
inline_attribute_width = 0
Loading

0 comments on commit a6e067f

Please sign in to comment.