Skip to content

Commit

Permalink
feat: adjust get_cells_by_redeem_script_hash rpc with more data
Browse files Browse the repository at this point in the history
  • Loading branch information
xxuejie authored and doitian committed Nov 19, 2018
1 parent 4c9b05f commit 488f2af
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
14 changes: 9 additions & 5 deletions rpc/src/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use bigint::H256;
use chain::chain::ChainProvider;
use ckb_pow::Clicker;
use core::header::{BlockNumber, Header};
use core::transaction::{CellOutput, Transaction};
use core::transaction::{OutPoint, Transaction};
use jsonrpc_core::{Error, IoHandler, Result};
use jsonrpc_http_server::ServerBuilder;
use jsonrpc_server_utils::cors::AccessControlAllowOrigin;
Expand All @@ -11,7 +11,7 @@ use miner::{build_block_template, BlockTemplate};
use network::NetworkService;
use pool::TransactionPool;
use std::sync::Arc;
use {BlockWithHash, Config, TransactionWithHash};
use {BlockWithHash, CellOutputWithOutPoint, Config, TransactionWithHash};

//TODO: build_rpc_trait! do not surppot trait bounds
build_rpc_trait! {
Expand Down Expand Up @@ -46,7 +46,7 @@ build_rpc_trait! {

// curl -d '{"id": 2, "jsonrpc": "2.0", "method":"get_cells_by_redeem_script_hash","params": ["0x1b1c832d02fdb4339f9868c8a8636c3d9dd10bd53ac7ce99595825bd6beeffb3", 1, 10]}' -H 'content-type:application/json' 'http://localhost:3030'
#[rpc(name = "get_cells_by_redeem_script_hash")]
fn get_cells_by_redeem_script_hash(&self, H256, u64, u64) -> Result<Vec<CellOutput>>;
fn get_cells_by_redeem_script_hash(&self, H256, u64, u64) -> Result<Vec<CellOutputWithOutPoint>>;

#[rpc(name = "local_node_id")]
fn local_node_id(&self) -> Result<Option<String>>;
Expand Down Expand Up @@ -108,7 +108,7 @@ impl<C: ChainProvider + 'static> IntegrationTestRpc for RpcImpl<C> {
redeem_script_hash: H256,
from: u64,
to: u64,
) -> Result<Vec<CellOutput>> {
) -> Result<Vec<CellOutputWithOutPoint>> {
let mut result = Vec::new();
for block_number in from..=to {
if let Some(block_hash) = self.chain.block_hash(block_number) {
Expand All @@ -123,7 +123,11 @@ impl<C: ChainProvider + 'static> IntegrationTestRpc for RpcImpl<C> {
.ok_or_else(Error::internal_error)?;
for (i, output) in transaction.outputs().iter().enumerate() {
if output.lock == redeem_script_hash && (!transaction_meta.is_spent(i)) {
result.push(output.clone());
result.push(CellOutputWithOutPoint {
outpoint: OutPoint::new(transaction.hash(), i as u32),
capacity: output.capacity,
lock: output.lock,
});
}
}
}
Expand Down
12 changes: 11 additions & 1 deletion rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ extern crate ckb_pow;
use bigint::H256;
use core::block::Block;
use core::header::Header;
use core::transaction::Transaction;
use core::transaction::{Capacity, OutPoint, Transaction};

#[cfg(feature = "integration_test")]
mod integration_test;
Expand Down Expand Up @@ -67,6 +67,16 @@ impl From<Block> for BlockWithHash {
}
}

// This is used as return value of get_cells_by_redeem_script_hash RPC:
// it contains both OutPoint data used for referencing a cell, as well as
// cell's own data such as lock and capacity
#[derive(Serialize)]
pub struct CellOutputWithOutPoint {
pub outpoint: OutPoint,
pub capacity: Capacity,
pub lock: H256,
}

#[derive(Clone, Debug, PartialEq, Deserialize)]
pub struct Config {
pub listen_addr: String,
Expand Down
14 changes: 9 additions & 5 deletions rpc/src/rpc.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use bigint::H256;
use chain::chain::ChainProvider;
use core::header::{BlockNumber, Header};
use core::transaction::{CellOutput, Transaction};
use core::transaction::{OutPoint, Transaction};
use jsonrpc_core::{Error, IoHandler, Result};
use jsonrpc_http_server::ServerBuilder;
use jsonrpc_server_utils::cors::AccessControlAllowOrigin;
Expand All @@ -10,7 +10,7 @@ use miner::{build_block_template, BlockTemplate};
use network::NetworkService;
use pool::TransactionPool;
use std::sync::Arc;
use {BlockWithHash, Config, TransactionWithHash};
use {BlockWithHash, CellOutputWithOutPoint, Config, TransactionWithHash};

build_rpc_trait! {
pub trait Rpc {
Expand Down Expand Up @@ -40,7 +40,7 @@ build_rpc_trait! {

// curl -d '{"id": 2, "jsonrpc": "2.0", "method":"get_cells_by_redeem_script_hash","params": ["0x1b1c832d02fdb4339f9868c8a8636c3d9dd10bd53ac7ce99595825bd6beeffb3", 1, 10]}' -H 'content-type:application/json' 'http://localhost:3030'
#[rpc(name = "get_cells_by_redeem_script_hash")]
fn get_cells_by_redeem_script_hash(&self, H256, u64, u64) -> Result<Vec<CellOutput>>;
fn get_cells_by_redeem_script_hash(&self, H256, u64, u64) -> Result<Vec<CellOutputWithOutPoint>>;
}
}

Expand Down Expand Up @@ -90,7 +90,7 @@ impl<C: ChainProvider + 'static> Rpc for RpcImpl<C> {
redeem_script_hash: H256,
from: u64,
to: u64,
) -> Result<Vec<CellOutput>> {
) -> Result<Vec<CellOutputWithOutPoint>> {
let mut result = Vec::new();
for block_number in from..=to {
if let Some(block_hash) = self.chain.block_hash(block_number) {
Expand All @@ -105,7 +105,11 @@ impl<C: ChainProvider + 'static> Rpc for RpcImpl<C> {
.ok_or_else(Error::internal_error)?;
for (i, output) in transaction.outputs().iter().enumerate() {
if output.lock == redeem_script_hash && (!transaction_meta.is_spent(i)) {
result.push(output.clone());
result.push(CellOutputWithOutPoint {
outpoint: OutPoint::new(transaction.hash(), i as u32),
capacity: output.capacity,
lock: output.lock,
});
}
}
}
Expand Down

0 comments on commit 488f2af

Please sign in to comment.