Skip to content

Commit

Permalink
Merge pull request #30 from TheWaWaR/indexer-in-ckb
Browse files Browse the repository at this point in the history
feat: use ckb-indexer from ckb rpc
  • Loading branch information
TheWaWaR authored Oct 12, 2022
2 parents 6bbaf18 + 02ad366 commit 089c3d2
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 61 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@

# 2.2.0
* **breaking change** Use ckb-indexer from ckb rpc
- `DefaultCellCollector::new` API changed
- remove `IndexerRpcClient::get_indexer_info` rpc method
- rename `IndexerRpcClient::get_tip` to `IndexerRpcClient::get_indexer_tip`

# 2.1.0
* Support omni-lock supply mode
* Use anyhow::Error to replace Box<dyn std::error::Error>
Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ckb-sdk"
version = "2.1.0"
version = "2.2.0"
authors = ["Linfeng Qian <[email protected]>", "Nervos Core Dev <[email protected]>"]
edition = "2018"
license = "MIT"
Expand Down Expand Up @@ -44,7 +44,7 @@ ckb-mock-tx-types = { version = "0.4.0", optional = true }
ckb-chain-spec = { version = "=0.104.0", optional = true }
ckb-script = { version = "=0.104.0", optional = true }

sparse-merkle-tree = "0.5.3"
sparse-merkle-tree = "0.5.4"
lazy_static = "1.3.0"

[features]
Expand Down
7 changes: 1 addition & 6 deletions examples/transfer_from_multisig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,6 @@ struct GenTxArgs {
/// CKB rpc url
#[clap(long, value_name = "URL", default_value = "http://127.0.0.1:8114")]
ckb_rpc: String,

/// CKB indexer rpc url
#[clap(long, value_name = "URL", default_value = "http://127.0.0.1:8116")]
ckb_indexer: String,
}

#[derive(Args)]
Expand Down Expand Up @@ -246,8 +242,7 @@ fn build_transfer_tx(
DefaultCellDepResolver::from_genesis(&BlockView::from(genesis_block))?
};
let header_dep_resolver = DefaultHeaderDepResolver::new(args.ckb_rpc.as_str());
let mut cell_collector =
DefaultCellCollector::new(args.ckb_indexer.as_str(), args.ckb_rpc.as_str());
let mut cell_collector = DefaultCellCollector::new(args.ckb_rpc.as_str());
let tx_dep_provider = DefaultTransactionDependencyProvider::new(args.ckb_rpc.as_str(), 10);

// Build base transaction
Expand Down
7 changes: 1 addition & 6 deletions examples/transfer_from_omnilock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,6 @@ struct GenTxArgs {
/// CKB rpc url
#[clap(long, value_name = "URL", default_value = "http://127.0.0.1:8114")]
ckb_rpc: String,

/// CKB indexer rpc url
#[clap(long, value_name = "URL", default_value = "http://127.0.0.1:8116")]
ckb_indexer: String,
}
#[derive(Args)]
struct SignTxArgs {
Expand Down Expand Up @@ -289,8 +285,7 @@ fn build_transfer_tx(
let mut cell_dep_resolver = DefaultCellDepResolver::from_genesis(&genesis_block)?;
cell_dep_resolver.insert(cell.script_id, cell.cell_dep, "Omni Lock".to_string());
let header_dep_resolver = DefaultHeaderDepResolver::new(args.ckb_rpc.as_str());
let mut cell_collector =
DefaultCellCollector::new(args.ckb_indexer.as_str(), args.ckb_rpc.as_str());
let mut cell_collector = DefaultCellCollector::new(args.ckb_rpc.as_str());
let tx_dep_provider = DefaultTransactionDependencyProvider::new(args.ckb_rpc.as_str(), 10);

// Build base transaction
Expand Down
7 changes: 1 addition & 6 deletions examples/transfer_from_omnilock_ethereum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,6 @@ struct GenTxArgs {
/// CKB rpc url
#[clap(long, value_name = "URL", default_value = "http://127.0.0.1:8114")]
ckb_rpc: String,

/// CKB indexer rpc url
#[clap(long, value_name = "URL", default_value = "http://127.0.0.1:8116")]
ckb_indexer: String,
}
#[derive(Args)]
struct SignTxArgs {
Expand Down Expand Up @@ -296,8 +292,7 @@ fn build_transfer_tx(
let mut cell_dep_resolver = DefaultCellDepResolver::from_genesis(&genesis_block)?;
cell_dep_resolver.insert(cell.script_id, cell.cell_dep, "Omni Lock".to_string());
let header_dep_resolver = DefaultHeaderDepResolver::new(args.ckb_rpc.as_str());
let mut cell_collector =
DefaultCellCollector::new(args.ckb_indexer.as_str(), args.ckb_rpc.as_str());
let mut cell_collector = DefaultCellCollector::new(args.ckb_rpc.as_str());
let tx_dep_provider = DefaultTransactionDependencyProvider::new(args.ckb_rpc.as_str(), 10);

// Build base transaction
Expand Down
7 changes: 1 addition & 6 deletions examples/transfer_from_omnilock_multisig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,6 @@ struct GenTxArgs {
/// CKB rpc url
#[clap(long, value_name = "URL", default_value = "http://127.0.0.1:8114")]
ckb_rpc: String,

/// CKB indexer rpc url
#[clap(long, value_name = "URL", default_value = "http://127.0.0.1:8116")]
ckb_indexer: String,
}
#[derive(Args)]
struct SignTxArgs {
Expand Down Expand Up @@ -345,8 +341,7 @@ fn build_transfer_tx(
let mut cell_dep_resolver = DefaultCellDepResolver::from_genesis(&genesis_block)?;
cell_dep_resolver.insert(cell.script_id, cell.cell_dep, "Omni Lock".to_string());
let header_dep_resolver = DefaultHeaderDepResolver::new(args.ckb_rpc.as_str());
let mut cell_collector =
DefaultCellCollector::new(args.ckb_indexer.as_str(), args.ckb_rpc.as_str());
let mut cell_collector = DefaultCellCollector::new(args.ckb_rpc.as_str());
let tx_dep_provider = DefaultTransactionDependencyProvider::new(args.ckb_rpc.as_str(), 10);

// Build base transaction
Expand Down
7 changes: 1 addition & 6 deletions examples/transfer_from_sighash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,6 @@ struct Args {
/// CKB rpc url
#[clap(long, value_name = "URL", default_value = "http://127.0.0.1:8114")]
ckb_rpc: String,

/// CKB indexer rpc url
#[clap(long, value_name = "URL", default_value = "http://127.0.0.1:8116")]
ckb_indexer: String,
}

fn main() -> Result<(), Box<dyn StdErr>> {
Expand Down Expand Up @@ -114,8 +110,7 @@ fn build_transfer_tx(
DefaultCellDepResolver::from_genesis(&BlockView::from(genesis_block))?
};
let header_dep_resolver = DefaultHeaderDepResolver::new(args.ckb_rpc.as_str());
let mut cell_collector =
DefaultCellCollector::new(args.ckb_indexer.as_str(), args.ckb_rpc.as_str());
let mut cell_collector = DefaultCellCollector::new(args.ckb_rpc.as_str());
let tx_dep_provider = DefaultTransactionDependencyProvider::new(args.ckb_rpc.as_str(), 10);

// Build the transaction
Expand Down
8 changes: 1 addition & 7 deletions src/rpc/ckb_indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,6 @@ pub struct CellsCapacity {
pub block_number: BlockNumber,
}

#[derive(Deserialize, Clone, Debug)]
pub struct IndexerInfo {
pub version: String,
}

#[derive(Deserialize, Clone, Debug)]
pub struct Cell {
pub output: CellOutput,
Expand Down Expand Up @@ -131,9 +126,8 @@ pub struct Pagination<T> {
}

crate::jsonrpc!(pub struct IndexerRpcClient {
pub fn get_tip(&mut self) -> Option<Tip>;
pub fn get_indexer_tip(&mut self) -> Option<Tip>;
pub fn get_cells(&mut self, search_key: SearchKey, order: Order, limit: Uint32, after: Option<JsonBytes>) -> Pagination<Cell>;
pub fn get_transactions(&mut self, search_key: SearchKey, order: Order, limit: Uint32, after: Option<JsonBytes>) -> Pagination<Tx>;
pub fn get_cells_capacity(&mut self, search_key: SearchKey) -> Option<CellsCapacity>;
pub fn get_indexer_info(&mut self) -> IndexerInfo;
});
6 changes: 3 additions & 3 deletions src/traits/default_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,8 @@ pub struct DefaultCellCollector {
}

impl DefaultCellCollector {
pub fn new(indexer_client: &str, ckb_client: &str) -> DefaultCellCollector {
let indexer_client = IndexerRpcClient::new(indexer_client);
pub fn new(ckb_client: &str) -> DefaultCellCollector {
let indexer_client = IndexerRpcClient::new(ckb_client);
let ckb_client = CkbRpcClient::new(ckb_client);
DefaultCellCollector {
indexer_client,
Expand Down Expand Up @@ -280,7 +280,7 @@ impl DefaultCellCollector {
for _ in 0..100 {
match self
.indexer_client
.get_tip()
.get_indexer_tip()
.map_err(|err| CellCollectorError::Internal(err.into()))?
{
Some(Tip { block_number, .. }) => {
Expand Down
28 changes: 9 additions & 19 deletions src/unlock/rc_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ lazy_static! {
]);
}

#[allow(clippy::upper_case_acronyms)]
type SMT = SparseMerkleTree<CKBBlake2bHasher, SmtH256, DefaultStore<SmtH256>>;
pub type Result<T> = ::core::result::Result<T, RcDataError>;

Expand Down Expand Up @@ -109,18 +110,6 @@ impl RcRuleDataBuilder {
*self.smt.root()
}

/// get the proof of the proof_pairs.
fn proof(&self, proof_pairs: &[(SmtH256, SmtH256)]) -> Result<Vec<u8>> {
let proof = self
.smt
.merkle_proof(proof_pairs.iter().map(|(k, _)| *k).collect())
.map_err(|err| RcDataError::BuildTree(err.to_string()))?;
let compiled_proof = proof
.compile(proof_pairs.to_vec())
.map_err(|e| RcDataError::CompileProof(e.to_string()))?;
Ok(compiled_proof.into())
}

/// Build smt with the given hashes
/// # Arguments
/// * `hashes` The given the hashes.
Expand All @@ -136,13 +125,14 @@ impl RcRuleDataBuilder {
/// # Return
/// The smt_tree root and the proofs of the proof_keys.
pub fn proof_keys(&mut self, keys: &[SmtH256]) -> Result<Vec<u8>> {
let proof_v = match self.list_type {
ListType::White => *SMT_EXISTING,
ListType::Black => *SMT_NOT_EXISTING,
};
let proof_pairs: Vec<(SmtH256, SmtH256)> =
keys.iter().map(|hash| (*hash, proof_v)).collect();
self.proof(&proof_pairs)
let proof = self
.smt
.merkle_proof(keys.to_vec())
.map_err(|err| RcDataError::BuildTree(err.to_string()))?;
let compiled_proof = proof
.compile(keys.to_vec())
.map_err(|e| RcDataError::CompileProof(e.to_string()))?;
Ok(compiled_proof.into())
}

/// Build the rc_rule after key/value pairs are set.
Expand Down

0 comments on commit 089c3d2

Please sign in to comment.