Skip to content

Commit 488f2af

Browse files
xxuejiedoitian
authored andcommitted
feat: adjust get_cells_by_redeem_script_hash rpc with more data
1 parent 4c9b05f commit 488f2af

3 files changed

Lines changed: 29 additions & 11 deletions

File tree

rpc/src/integration_test.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use bigint::H256;
22
use chain::chain::ChainProvider;
33
use ckb_pow::Clicker;
44
use core::header::{BlockNumber, Header};
5-
use core::transaction::{CellOutput, Transaction};
5+
use core::transaction::{OutPoint, Transaction};
66
use jsonrpc_core::{Error, IoHandler, Result};
77
use jsonrpc_http_server::ServerBuilder;
88
use jsonrpc_server_utils::cors::AccessControlAllowOrigin;
@@ -11,7 +11,7 @@ use miner::{build_block_template, BlockTemplate};
1111
use network::NetworkService;
1212
use pool::TransactionPool;
1313
use std::sync::Arc;
14-
use {BlockWithHash, Config, TransactionWithHash};
14+
use {BlockWithHash, CellOutputWithOutPoint, Config, TransactionWithHash};
1515

1616
//TODO: build_rpc_trait! do not surppot trait bounds
1717
build_rpc_trait! {
@@ -46,7 +46,7 @@ build_rpc_trait! {
4646

4747
// 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'
4848
#[rpc(name = "get_cells_by_redeem_script_hash")]
49-
fn get_cells_by_redeem_script_hash(&self, H256, u64, u64) -> Result<Vec<CellOutput>>;
49+
fn get_cells_by_redeem_script_hash(&self, H256, u64, u64) -> Result<Vec<CellOutputWithOutPoint>>;
5050

5151
#[rpc(name = "local_node_id")]
5252
fn local_node_id(&self) -> Result<Option<String>>;
@@ -108,7 +108,7 @@ impl<C: ChainProvider + 'static> IntegrationTestRpc for RpcImpl<C> {
108108
redeem_script_hash: H256,
109109
from: u64,
110110
to: u64,
111-
) -> Result<Vec<CellOutput>> {
111+
) -> Result<Vec<CellOutputWithOutPoint>> {
112112
let mut result = Vec::new();
113113
for block_number in from..=to {
114114
if let Some(block_hash) = self.chain.block_hash(block_number) {
@@ -123,7 +123,11 @@ impl<C: ChainProvider + 'static> IntegrationTestRpc for RpcImpl<C> {
123123
.ok_or_else(Error::internal_error)?;
124124
for (i, output) in transaction.outputs().iter().enumerate() {
125125
if output.lock == redeem_script_hash && (!transaction_meta.is_spent(i)) {
126-
result.push(output.clone());
126+
result.push(CellOutputWithOutPoint {
127+
outpoint: OutPoint::new(transaction.hash(), i as u32),
128+
capacity: output.capacity,
129+
lock: output.lock,
130+
});
127131
}
128132
}
129133
}

rpc/src/lib.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ extern crate ckb_pow;
1919
use bigint::H256;
2020
use core::block::Block;
2121
use core::header::Header;
22-
use core::transaction::Transaction;
22+
use core::transaction::{Capacity, OutPoint, Transaction};
2323

2424
#[cfg(feature = "integration_test")]
2525
mod integration_test;
@@ -67,6 +67,16 @@ impl From<Block> for BlockWithHash {
6767
}
6868
}
6969

70+
// This is used as return value of get_cells_by_redeem_script_hash RPC:
71+
// it contains both OutPoint data used for referencing a cell, as well as
72+
// cell's own data such as lock and capacity
73+
#[derive(Serialize)]
74+
pub struct CellOutputWithOutPoint {
75+
pub outpoint: OutPoint,
76+
pub capacity: Capacity,
77+
pub lock: H256,
78+
}
79+
7080
#[derive(Clone, Debug, PartialEq, Deserialize)]
7181
pub struct Config {
7282
pub listen_addr: String,

rpc/src/rpc.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use bigint::H256;
22
use chain::chain::ChainProvider;
33
use core::header::{BlockNumber, Header};
4-
use core::transaction::{CellOutput, Transaction};
4+
use core::transaction::{OutPoint, Transaction};
55
use jsonrpc_core::{Error, IoHandler, Result};
66
use jsonrpc_http_server::ServerBuilder;
77
use jsonrpc_server_utils::cors::AccessControlAllowOrigin;
@@ -10,7 +10,7 @@ use miner::{build_block_template, BlockTemplate};
1010
use network::NetworkService;
1111
use pool::TransactionPool;
1212
use std::sync::Arc;
13-
use {BlockWithHash, Config, TransactionWithHash};
13+
use {BlockWithHash, CellOutputWithOutPoint, Config, TransactionWithHash};
1414

1515
build_rpc_trait! {
1616
pub trait Rpc {
@@ -40,7 +40,7 @@ build_rpc_trait! {
4040

4141
// 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'
4242
#[rpc(name = "get_cells_by_redeem_script_hash")]
43-
fn get_cells_by_redeem_script_hash(&self, H256, u64, u64) -> Result<Vec<CellOutput>>;
43+
fn get_cells_by_redeem_script_hash(&self, H256, u64, u64) -> Result<Vec<CellOutputWithOutPoint>>;
4444
}
4545
}
4646

@@ -90,7 +90,7 @@ impl<C: ChainProvider + 'static> Rpc for RpcImpl<C> {
9090
redeem_script_hash: H256,
9191
from: u64,
9292
to: u64,
93-
) -> Result<Vec<CellOutput>> {
93+
) -> Result<Vec<CellOutputWithOutPoint>> {
9494
let mut result = Vec::new();
9595
for block_number in from..=to {
9696
if let Some(block_hash) = self.chain.block_hash(block_number) {
@@ -105,7 +105,11 @@ impl<C: ChainProvider + 'static> Rpc for RpcImpl<C> {
105105
.ok_or_else(Error::internal_error)?;
106106
for (i, output) in transaction.outputs().iter().enumerate() {
107107
if output.lock == redeem_script_hash && (!transaction_meta.is_spent(i)) {
108-
result.push(output.clone());
108+
result.push(CellOutputWithOutPoint {
109+
outpoint: OutPoint::new(transaction.hash(), i as u32),
110+
capacity: output.capacity,
111+
lock: output.lock,
112+
});
109113
}
110114
}
111115
}

0 commit comments

Comments
 (0)