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

feat: add indexer related rpc #905

Merged
merged 26 commits into from
Jun 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
5656a11
feat: add IterableKeyValueDB trait and remove unused CacheDB
quake May 19, 2019
7b5f5f4
chore: doc tweak
quake May 19, 2019
8885b07
chore: avoiding store duplicate CellOutput
quake May 22, 2019
d5faba0
feat: add sync_index_stats
quake May 25, 2019
a09c66a
test: add more test
quake May 27, 2019
88e33be
refactor: remove notification
quake May 28, 2019
bfbf840
chore: revert change of chain/notify
quake May 28, 2019
3b95202
test: fix rpc test
quake May 28, 2019
df7e57d
test: fix integration test
quake May 28, 2019
bec2b6b
chore: rename wallet to indexer
quake May 28, 2019
32d57a8
chore: typo
quake May 28, 2019
b3eccdf
chore: rebase and fix test
quake Jun 5, 2019
b0ff3a7
chore: rebase with develop
quake Jun 6, 2019
c89062e
chore: rebase develop
quake Jun 8, 2019
81b049e
chore: avoids zombie process
quake Jun 9, 2019
6a421f3
chore: rebase with develop and pass integration test
quake Jun 9, 2019
6e13a87
chore: rpc doc style
quake Jun 9, 2019
4855751
fix: panic when gives a `index_from` higher than tip
quake Jun 14, 2019
c7ae717
chore: change log level
quake Jun 14, 2019
484f9da
perf: avoids chain state lock and rebase with develop
quake Jun 14, 2019
d2d16d1
feat: add `reverse_order` paramter to rpc
quake Jun 14, 2019
8bf4e89
perf: avoids chain state lock
quake Jun 14, 2019
0893840
chore: rebase develop
quake Jun 16, 2019
74fab86
fix: unit test hash changed
quake Jun 16, 2019
1c99774
chore: update doc
quake Jun 17, 2019
942467d
chore: modify according review
quake Jun 17, 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
25 changes: 25 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ members = [
"pow",
"benches",
"resource",
"indexer",
]

[profile.release]
Expand Down
1 change: 1 addition & 0 deletions ckb-bin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ ckb-store = { path = "../store" }
ckb-chain-spec = {path = "../spec"}
ckb-notify = { path = "../notify"}
ckb-miner = { path = "../miner" }
ckb-indexer = { path = "../indexer" }
ckb-db = { path = "../db" }
ckb-pow = { path = "../pow" }
ckb-network = { path = "../network"}
Expand Down
3 changes: 2 additions & 1 deletion ckb-bin/src/subcommand/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ pub fn run(args: RunArgs, version: Version) -> Result<(), ExitCode> {
network_controller.clone(),
chain_controller.clone(),
)
.enable_alert(alert_verifier, alert_notifier, network_controller);
.enable_alert(alert_verifier, alert_notifier, network_controller)
.enable_indexer(&args.config.indexer_db, shared.clone());
let io_handler = builder.build();

let rpc_server = RpcServer::new(args.config.rpc, io_handler);
Expand Down
150 changes: 0 additions & 150 deletions db/src/cachedb.rs

This file was deleted.

17 changes: 15 additions & 2 deletions db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,17 @@ use failure::Fail;
use std::ops::Range;
use std::result;

pub mod cachedb;
pub mod config;
pub mod memorydb;
pub mod rocksdb;

pub use crate::cachedb::CacheDB;
pub use crate::config::DBConfig;
pub use crate::memorydb::MemoryKeyValueDB;
pub use crate::rocksdb::RocksDB;

pub type Col = u32;
pub type Result<T> = result::Result<T, Error>;
pub type KeyValueIteratorItem = (Box<[u8]>, Box<[u8]>);

#[derive(Clone, Debug, PartialEq, Eq, Fail)]
pub enum Error {
Expand All @@ -36,6 +35,20 @@ pub trait KeyValueDB: Sync + Send {
F: FnMut(&[u8], &[u8]) -> Result<()>;
}

pub trait IterableKeyValueDB: KeyValueDB {
fn iter<'a>(
&'a self,
col: Col,
from_key: &'a [u8],
direction: Direction,
) -> Result<Box<Iterator<Item = KeyValueIteratorItem> + 'a>>;
}

pub enum Direction {
Forward,
Reverse,
}

pub trait DbBatch {
fn insert(&mut self, col: Col, key: &[u8], value: &[u8]) -> Result<()>;
fn delete(&mut self, col: Col, key: &[u8]) -> Result<()>;
Expand Down
75 changes: 73 additions & 2 deletions db/src/rocksdb.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
use crate::{Col, DBConfig, DbBatch, Error, KeyValueDB, Result};
use crate::{
Col, DBConfig, DbBatch, Direction, Error, IterableKeyValueDB, KeyValueDB, KeyValueIteratorItem,
Result,
};
use log::{info, warn};
use rocksdb::{ColumnFamily, Error as RdbError, IteratorMode, Options, WriteBatch, DB};
use rocksdb::{
ColumnFamily, Direction as RdbDirection, Error as RdbError, IteratorMode, Options, WriteBatch,
DB,
};
use std::ops::Range;
use std::sync::Arc;

Expand Down Expand Up @@ -159,6 +165,26 @@ impl KeyValueDB for RocksDB {
}
}

impl IterableKeyValueDB for RocksDB {
fn iter<'a>(
&'a self,
col: Col,
from_key: &'a [u8],
direction: Direction,
) -> Result<Box<Iterator<Item = KeyValueIteratorItem> + 'a>> {
let cf = cf_handle(&self.inner, col)?;
let iter_direction = match direction {
Direction::Forward => RdbDirection::Forward,
Direction::Reverse => RdbDirection::Reverse,
};
let mode = IteratorMode::From(from_key, iter_direction);
self.inner
.iterator_cf(cf, mode)
.map(|iter| Box::new(iter) as Box<_>)
.map_err(Into::into)
}
}

pub struct RocksdbBatch {
db: Arc<DB>,
wb: WriteBatch,
Expand Down Expand Up @@ -318,4 +344,49 @@ mod tests {
let _ = RocksDB::open_with_check(&config, 1, VERSION_KEY, VERSION_VALUE).unwrap();
let _ = RocksDB::open_with_check(&config, 1, VERSION_KEY, VERSION_VALUE).unwrap();
}

#[test]
fn iter() {
let db = setup_db("iter", 1);

let mut batch = db.batch().unwrap();
batch.insert(0, &[0, 0, 1], &[0, 0, 1]).unwrap();
batch.insert(0, &[0, 1, 1], &[0, 1, 1]).unwrap();
batch.insert(0, &[0, 1, 2], &[0, 1, 2]).unwrap();
batch.insert(0, &[0, 1, 3], &[0, 1, 3]).unwrap();
batch.insert(0, &[0, 2, 1], &[0, 2, 1]).unwrap();
batch.commit().unwrap();

let mut iter = db.iter(0, &[0, 1], Direction::Forward).unwrap();
assert_eq!(
(
vec![0, 1, 1].into_boxed_slice(),
vec![0, 1, 1].into_boxed_slice()
),
iter.next().unwrap()
);
assert_eq!(
(
vec![0, 1, 2].into_boxed_slice(),
vec![0, 1, 2].into_boxed_slice()
),
iter.next().unwrap()
);

let mut iter = db.iter(0, &[0, 2], Direction::Reverse).unwrap();
assert_eq!(
(
vec![0, 1, 3].into_boxed_slice(),
vec![0, 1, 3].into_boxed_slice()
),
iter.next().unwrap()
);
assert_eq!(
(
vec![0, 1, 2].into_boxed_slice(),
vec![0, 1, 2].into_boxed_slice()
),
iter.next().unwrap()
);
}
}
27 changes: 27 additions & 0 deletions indexer/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[package]
name = "ckb-indexer"
version = "0.15.0-pre"
license = "MIT"
authors = ["Nervos Core Dev <[email protected]>"]
edition = "2018"

[dependencies]
ckb-core = { path = "../core" }
ckb-db = { path = "../db" }
ckb-notify = { path = "../notify" }
ckb-shared = { path = "../shared" }
ckb-store = { path = "../store" }
ckb-traits = { path = "../traits" }
jsonrpc-types = { path = "../util/jsonrpc-types" }
numext-fixed-hash = { version = "0.1", features = ["support_rand", "support_heapsize", "support_serde"] }
bincode = "1.1"
serde = "1.0"
serde_derive = "1.0"
crossbeam-channel = "0.3"
log = "0.4"

[dev-dependencies]
tempfile = "3.0"
ckb-chain = { path = "../chain" }
ckb-chain-spec = { path = "../spec" }
numext-fixed-uint = { version = "0.1", features = ["support_rand", "support_heapsize", "support_serde"] }
5 changes: 5 additions & 0 deletions indexer/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mod store;
mod types;

pub use store::{DefaultIndexerStore, IndexerStore};
pub use types::{CellTransaction, LiveCell, TransactionPoint};
Loading