Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
doitian committed Sep 7, 2019
2 parents 2cbfe88 + 28806f9 commit 9a1af87
Show file tree
Hide file tree
Showing 26 changed files with 2,052 additions and 1,312 deletions.
1,595 changes: 872 additions & 723 deletions Cargo.lock

Large diffs are not rendered by default.

20 changes: 11 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
[package]
name = "ckb-cli"
version = "0.19.0"
version = "0.20.0"
license = "MIT"
authors = ["Linfeng Qian <[email protected]>", "Nervos Core Dev <[email protected]>"]
edition = "2018"

[dependencies]
ckb-jsonrpc-types = { git = "https://github.com/nervosnetwork/ckb", branch = "rc/v0.19" }
ckb-hash = { git = "https://github.com/nervosnetwork/ckb", branch = "rc/v0.19" }
ckb-crypto = { git = "https://github.com/nervosnetwork/ckb", branch = "rc/v0.19", features = ["secp"] }
ckb-build-info = { git = "https://github.com/nervosnetwork/ckb", branch = "rc/v0.19" }
ckb-types = { git = "https://github.com/nervosnetwork/ckb", branch = "rc/v0.19" }
ckb-util = { git = "https://github.com/nervosnetwork/ckb", branch = "rc/v0.19" }
ckb-jsonrpc-types = { git = "https://github.com/nervosnetwork/ckb", branch = "rc/v0.20" }
ckb-hash = { git = "https://github.com/nervosnetwork/ckb", branch = "rc/v0.20" }
ckb-crypto = { git = "https://github.com/nervosnetwork/ckb", branch = "rc/v0.20", features = ["secp"] }
ckb-build-info = { git = "https://github.com/nervosnetwork/ckb", branch = "rc/v0.20" }
ckb-types = { git = "https://github.com/nervosnetwork/ckb", branch = "rc/v0.20" }
ckb-util = { git = "https://github.com/nervosnetwork/ckb", branch = "rc/v0.20" }
ckb-sdk = { path = "ckb-sdk" }
ckb-index = { path = "ckb-index" }
ckb-resource = { git = "https://github.com/nervosnetwork/ckb", branch = "rc/v0.19" }
ckb-resource = { git = "https://github.com/nervosnetwork/ckb", branch = "rc/v0.20" }

jsonrpc-client-core = "0.5.0"
secp256k1 = {version = "0.15.0" }
Expand All @@ -39,13 +39,15 @@ url = "1.7.2"
log = "0.4.6"
chrono = "0.4"
rpassword = "3.0.2"
ipnetwork = "0.14"
multiaddr = { package = "parity-multiaddr", version = "0.4.0" }

[target.'cfg(unix)'.dependencies]
tui = "0.6.0"
termion = "1.5"

[build-dependencies]
ckb-build-info = { git = "https://github.com/nervosnetwork/ckb", branch = "rc/v0.19" }
ckb-build-info = { git = "https://github.com/nervosnetwork/ckb", branch = "rc/v0.20" }

[workspace]
members = ["ckb-sdk", "ckb-index"]
4 changes: 2 additions & 2 deletions ckb-index/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ckb-index"
version = "0.19.0"
version = "0.20.0"
authors = ["Linfeng Qian <[email protected]>", "Nervos Core Dev <[email protected]>"]
edition = "2018"
license = "MIT"
Expand All @@ -13,5 +13,5 @@ log = "0.4.6"
rocksdb = { version = "0.12.2", default-features = false }
failure = "0.1.5"

ckb-types = { git = "https://github.com/nervosnetwork/ckb", branch = "rc/v0.19" }
ckb-types = { git = "https://github.com/nervosnetwork/ckb", branch = "rc/v0.20" }
ckb-sdk = { path = "../ckb-sdk" }
33 changes: 18 additions & 15 deletions ckb-index/src/index/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,12 @@ impl Key {
Key::TotalCapacity => KeyType::TotalCapacity.to_bytes(),
Key::GlobalHash(hash) => {
let mut bytes = KeyType::GlobalHash.to_bytes();
bytes.extend(hash.to_vec());
bytes.extend(hash.as_bytes().to_vec());
bytes
}
Key::TxMap(tx_hash) => {
let mut bytes = KeyType::TxMap.to_bytes();
bytes.extend(tx_hash.to_vec());
bytes.extend(tx_hash.as_bytes().to_vec());
bytes
}
Key::SecpAddrLock(address) => {
Expand Down Expand Up @@ -168,41 +168,41 @@ impl Key {
}
Key::LockScript(lock_hash) => {
let mut bytes = KeyType::LockScript.to_bytes();
bytes.extend(lock_hash.to_vec());
bytes.extend(lock_hash.as_bytes().to_vec());
bytes
}
Key::LockTotalCapacity(lock_hash) => {
let mut bytes = KeyType::LockTotalCapacity.to_bytes();
bytes.extend(lock_hash.to_vec());
bytes.extend(lock_hash.as_bytes().to_vec());
bytes
}
Key::LockTotalCapacityIndex(capacity, lock_hash) => {
// NOTE: large capacity stay front
let capacity = std::u64::MAX - capacity;
let mut bytes = KeyType::LockTotalCapacityIndex.to_bytes();
bytes.extend(capacity.to_be_bytes().to_vec());
bytes.extend(lock_hash.to_vec());
bytes.extend(lock_hash.as_bytes().to_vec());
bytes
}
Key::LockLiveCellIndexPrefix(lock_hash, number_opt) => {
let mut bytes = KeyType::LockLiveCellIndex.to_bytes();
bytes.extend(lock_hash.to_vec());
bytes.extend(lock_hash.as_bytes().to_vec());
if let Some(number) = number_opt {
bytes.extend(number.to_be_bytes().to_vec());
}
bytes
}
Key::LockLiveCellIndex(lock_hash, number, cell_index) => {
let mut bytes = KeyType::LockLiveCellIndex.to_bytes();
bytes.extend(lock_hash.to_vec());
bytes.extend(lock_hash.as_bytes().to_vec());
// Must use big endian for sort
bytes.extend(number.to_be_bytes().to_vec());
bytes.extend(cell_index.to_bytes());
bytes
}
Key::LockTx(lock_hash, number, tx_index) => {
let mut bytes = KeyType::LockTx.to_bytes();
bytes.extend(lock_hash.to_vec());
bytes.extend(lock_hash.as_bytes().to_vec());
// Must use big endian for sort
bytes.extend(number.to_be_bytes().to_vec());
bytes.extend(tx_index.to_be_bytes().to_vec());
Expand All @@ -211,15 +211,15 @@ impl Key {

Key::TypeLiveCellIndexPrefix(type_hash, number_opt) => {
let mut bytes = KeyType::TypeLiveCellIndex.to_bytes();
bytes.extend(type_hash.to_vec());
bytes.extend(type_hash.as_bytes().to_vec());
if let Some(number) = number_opt {
bytes.extend(number.to_be_bytes().to_vec());
}
bytes
}
Key::TypeLiveCellIndex(type_hash, number, cell_index) => {
let mut bytes = KeyType::TypeLiveCellIndex.to_bytes();
bytes.extend(type_hash.to_vec());
bytes.extend(type_hash.as_bytes().to_vec());
// Must use big endian for sort
bytes.extend(number.to_be_bytes().to_vec());
bytes.extend(cell_index.to_bytes());
Expand All @@ -228,15 +228,15 @@ impl Key {

Key::CodeLiveCellIndexPrefix(code_hash, number_opt) => {
let mut bytes = KeyType::CodeLiveCellIndex.to_bytes();
bytes.extend(code_hash.to_vec());
bytes.extend(code_hash.as_bytes().to_vec());
if let Some(number) = number_opt {
bytes.extend(number.to_be_bytes().to_vec());
}
bytes
}
Key::CodeLiveCellIndex(code_hash, number, cell_index) => {
let mut bytes = KeyType::CodeLiveCellIndex.to_bytes();
bytes.extend(code_hash.to_vec());
bytes.extend(code_hash.as_bytes().to_vec());
// Must use big endian for sort
bytes.extend(number.to_be_bytes().to_vec());
bytes.extend(cell_index.to_bytes());
Expand Down Expand Up @@ -383,7 +383,7 @@ impl Key {
}

pub(crate) fn pair_genesis_hash(value: &H256) -> (Vec<u8>, Vec<u8>) {
(Key::GenesisHash.to_bytes(), value.to_vec())
(Key::GenesisHash.to_bytes(), value.as_bytes().to_vec())
}
pub(crate) fn pair_network(value: NetworkType) -> (Vec<u8>, Vec<u8>) {
(Key::Network.to_bytes(), vec![value as u8])
Expand All @@ -405,7 +405,10 @@ impl Key {
)
}
pub(crate) fn pair_secp_addr_lock(address: Address, value: &H256) -> (Vec<u8>, Vec<u8>) {
(Key::SecpAddrLock(address).to_bytes(), value.to_vec())
(
Key::SecpAddrLock(address).to_bytes(),
value.as_bytes().to_vec(),
)
}
pub(crate) fn pair_recent_header(value: &HeaderInfo) -> (Vec<u8>, Vec<u8>) {
(
Expand Down Expand Up @@ -475,7 +478,7 @@ impl Key {
) -> (Vec<u8>, Vec<u8>) {
(
Key::LockTx(lock_hash, number, tx_index).to_bytes(),
value.to_vec(),
value.as_bytes().to_vec(),
)
}

Expand Down
61 changes: 30 additions & 31 deletions ckb-index/src/index/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ use ckb_sdk::{Address, GenesisInfo, NetworkType};
use ckb_types::{
bytes::Bytes,
core::{BlockView, HeaderView},
packed::{Header, OutPoint, Script},
packed::{Byte32, Header, OutPoint, Script},
prelude::*,
H256,
};
use rocksdb::{ColumnFamily, DB};

Expand Down Expand Up @@ -46,11 +45,11 @@ impl<'a> IndexDatabase<'a> {
let genesis_header = genesis_info.header().clone();
assert_eq!(genesis_header.number(), 0);

let (genesis_hash_opt, network_opt): (Option<H256>, Option<NetworkType>) = {
let (genesis_hash_opt, network_opt): (Option<Byte32>, Option<NetworkType>) = {
let reader = RocksReader::new(db, cf);
let genesis_hash_opt = reader
.get(&Key::GenesisHash.to_bytes())
.map(|bytes| H256::from_slice(&bytes).unwrap());
.map(|bytes| Byte32::from_slice(&bytes).unwrap());
let network_opt = reader
.get(&Key::Network.to_bytes())
.map(|bytes| NetworkType::from_u8(bytes[0]).unwrap());
Expand All @@ -63,7 +62,7 @@ impl<'a> IndexDatabase<'a> {
network, network_opt
)));
}
let hash: H256 = genesis_header.hash().unpack();
let hash: Byte32 = genesis_header.hash();
if genesis_hash != hash {
return Err(IndexError::InvalidGenesis(format!(
"{:#x}, expected: {:#x}",
Expand Down Expand Up @@ -95,12 +94,12 @@ impl<'a> IndexDatabase<'a> {

pub fn apply_next_block(&mut self, block: BlockView) -> Result<(), IndexError> {
let number = block.header().number();
let block_hash: H256 = block.header().hash().unpack();
let block_hash = block.header().hash();
if let Some(last_header) = self.last_header.clone() {
if number != last_header.number() + 1 {
return Err(IndexError::InvalidBlockNumber(number));
}
if block.header().parent_hash() != last_header.hash().unpack() {
if block.header().parent_hash() != last_header.hash() {
if number == 1 {
return Err(IndexError::IllegalBlock(block_hash));
}
Expand Down Expand Up @@ -131,7 +130,7 @@ impl<'a> IndexDatabase<'a> {
self.apply_block_unchecked(block);
Ok(())
} else if number == 0 {
let genesis_hash = self.genesis_info.header().hash().unpack();
let genesis_hash = self.genesis_info.header().hash();
if block_hash != genesis_hash {
Err(IndexError::InvalidGenesis(format!(
"{:#x}, expected: {:#x}",
Expand Down Expand Up @@ -162,9 +161,9 @@ impl<'a> IndexDatabase<'a> {
self.last_number().map(|number| number + 1)
}

fn get_address_inner(&self, reader: &RocksReader, lock_hash: H256) -> Option<Address> {
fn get_address_inner(&self, reader: &RocksReader, lock_hash: Byte32) -> Option<Address> {
reader
.get(&Key::LockScript(lock_hash).to_bytes())
.get(&Key::LockScript(lock_hash.unpack()).to_bytes())
.and_then(|bytes| {
let script = Script::new_unchecked(bytes.into());
script.args().get(0).and_then(|arg| {
Expand All @@ -174,66 +173,66 @@ impl<'a> IndexDatabase<'a> {
})
}

pub fn get_capacity(&self, lock_hash: H256) -> Option<u64> {
pub fn get_capacity(&self, lock_hash: Byte32) -> Option<u64> {
let reader = RocksReader::new(self.db, self.cf);
reader
.get(&Key::LockTotalCapacity(lock_hash).to_bytes())
.get(&Key::LockTotalCapacity(lock_hash.unpack()).to_bytes())
.map(|bytes| {
let mut data = [0u8; 8];
data.copy_from_slice(&bytes[..8]);
u64::from_le_bytes(data)
})
}

pub fn get_lock_hash_by_address(&self, address: Address) -> Option<H256> {
pub fn get_lock_hash_by_address(&self, address: Address) -> Option<Byte32> {
let reader = RocksReader::new(self.db, self.cf);
reader
.get(&Key::SecpAddrLock(address).to_bytes())
.map(|bytes| H256::from_slice(&bytes).unwrap())
.map(|bytes| Byte32::from_slice(&bytes).unwrap())
}

pub fn get_lock_script_by_hash(&self, lock_hash: H256) -> Option<Script> {
pub fn get_lock_script_by_hash(&self, lock_hash: Byte32) -> Option<Script> {
let reader = RocksReader::new(self.db, self.cf);
reader
.get(&Key::LockScript(lock_hash).to_bytes())
.get(&Key::LockScript(lock_hash.unpack()).to_bytes())
.map(|bytes| Script::new_unchecked(bytes.into()))
}

// pub fn get_address(&self, lock_hash: H256) -> Option<Address> {
// pub fn get_address(&self, lock_hash: Byte32) -> Option<Address> {
// let reader = env_read.read().unwrap();
// self.get_address_inner(&reader, lock_hash)
// }

pub fn get_live_cells_by_lock<F: FnMut(usize, &LiveCellInfo) -> (bool, bool)>(
&self,
lock_hash: H256,
lock_hash: Byte32,
from_number: Option<u64>,
terminator: F,
) -> Vec<LiveCellInfo> {
let key_prefix = Key::LockLiveCellIndexPrefix(lock_hash.clone(), None);
let key_start = Key::LockLiveCellIndexPrefix(lock_hash, from_number);
let key_prefix = Key::LockLiveCellIndexPrefix(lock_hash.clone().unpack(), None);
let key_start = Key::LockLiveCellIndexPrefix(lock_hash.unpack(), from_number);
self.get_live_cell_infos(key_prefix, key_start, terminator)
}

pub fn get_live_cells_by_type<F: FnMut(usize, &LiveCellInfo) -> (bool, bool)>(
&self,
type_hash: H256,
type_hash: Byte32,
from_number: Option<u64>,
terminator: F,
) -> Vec<LiveCellInfo> {
let key_prefix = Key::TypeLiveCellIndexPrefix(type_hash.clone(), None);
let key_start = Key::TypeLiveCellIndexPrefix(type_hash, from_number);
let key_prefix = Key::TypeLiveCellIndexPrefix(type_hash.clone().unpack(), None);
let key_start = Key::TypeLiveCellIndexPrefix(type_hash.unpack(), from_number);
self.get_live_cell_infos(key_prefix, key_start, terminator)
}

pub fn get_live_cells_by_code<F: FnMut(usize, &LiveCellInfo) -> (bool, bool)>(
&self,
code_hash: H256,
code_hash: Byte32,
from_number: Option<u64>,
terminator: F,
) -> Vec<LiveCellInfo> {
let key_prefix = Key::CodeLiveCellIndexPrefix(code_hash.clone(), None);
let key_start = Key::CodeLiveCellIndexPrefix(code_hash, from_number);
let key_prefix = Key::CodeLiveCellIndexPrefix(code_hash.clone().unpack(), None);
let key_start = Key::CodeLiveCellIndexPrefix(code_hash.unpack(), from_number);
self.get_live_cell_infos(key_prefix, key_start, terminator)
}

Expand Down Expand Up @@ -273,7 +272,7 @@ impl<'a> IndexDatabase<'a> {
infos
}

pub fn get_top_n(&self, n: usize) -> Vec<(H256, Option<Address>, u64)> {
pub fn get_top_n(&self, n: usize) -> Vec<(Byte32, Option<Address>, u64)> {
let reader = RocksReader::new(self.db, self.cf);
let key_prefix: Vec<u8> = KeyType::LockTotalCapacityIndex.to_bytes();

Expand All @@ -284,8 +283,8 @@ impl<'a> IndexDatabase<'a> {
break;
}
if let Key::LockTotalCapacityIndex(capacity, lock_hash) = Key::from_bytes(&key_bytes) {
let address_opt = self.get_address_inner(&reader, lock_hash.clone());
pairs.push((lock_hash, address_opt, capacity));
let address_opt = self.get_address_inner(&reader, lock_hash.clone().pack());
pairs.push((lock_hash.pack(), address_opt, capacity));
} else {
panic!("Got invalid key: {:?}", key_bytes);
}
Expand All @@ -298,7 +297,7 @@ impl<'a> IndexDatabase<'a> {

fn apply_block_unchecked(&mut self, block: BlockView) {
let header = block.header();
let block_hash: H256 = header.hash().unpack();
let block_hash = header.hash();
log::debug!("Block: {} => {:x}", header.number(), block_hash);

// TODO: should forbid query when Init
Expand Down Expand Up @@ -386,7 +385,7 @@ impl<'a> IndexDatabase<'a> {
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum IndexError {
BlockImmature(u64),
IllegalBlock(H256),
IllegalBlock(Byte32),
InvalidBlockNumber(u64),
BlockInvalid(String),
NotInit,
Expand Down
Loading

0 comments on commit 9a1af87

Please sign in to comment.