Skip to content

Commit

Permalink
feat: add get_current_cell rpc for fetching unspent cells
Browse files Browse the repository at this point in the history
  • Loading branch information
xxuejie committed Nov 27, 2018
1 parent ef65f8b commit 781d5f5
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
8 changes: 8 additions & 0 deletions rpc/src/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ build_rpc_trait! {
#[rpc(name = "get_cells_by_type_hash")]
fn get_cells_by_type_hash(&self, H256, u64, u64) -> Result<Vec<CellOutputWithOutPoint>>;

// curl -d '{"id": 2, "jsonrpc": "2.0", "method":"get_current_cell","params": [{"hash": "0x1b1c832d02fdb4339f9868c8a8636c3d9dd10bd53ac7ce99595825bd6beeffb3", "index": 1}]}' -H 'content-type:application/json' 'http://localhost:3030'
#[rpc(name = "get_current_cell")]
fn get_current_cell(&self, OutPoint) -> Result<CellWithStatus>;

#[rpc(name = "local_node_id")]
fn local_node_id(&self) -> Result<Option<String>>;

Expand Down Expand Up @@ -151,6 +155,10 @@ impl<CI: ChainIndex + 'static> IntegrationTestRpc for RpcImpl<CI> {
Ok(result)
}

fn get_current_cell(&self, out_point: OutPoint) -> Result<CellWithStatus> {
Ok(self.shared.cell(&out_point).into())
}

fn local_node_id(&self) -> Result<Option<String>> {
Ok(self.network.external_url())
}
Expand Down
23 changes: 22 additions & 1 deletion rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ extern crate fnv;

use bigint::H256;
use ckb_core::block::Block;
use ckb_core::cell::CellStatus;
use ckb_core::header::Header;
use ckb_core::transaction::{Capacity, OutPoint, Transaction};
use ckb_core::transaction::{Capacity, CellOutput, OutPoint, Transaction};

mod service;

Expand Down Expand Up @@ -92,6 +93,26 @@ pub struct CellOutputWithOutPoint {
pub lock: H256,
}

#[derive(Serialize)]
pub struct CellWithStatus {
pub cell: Option<CellOutput>,
pub status: String,
}

impl From<CellStatus> for CellWithStatus {
fn from(status: CellStatus) -> Self {
let (cell, status) = match status {
CellStatus::Current(cell) => (Some(cell), "current"),
CellStatus::Old => (None, "old"),
CellStatus::Unknown => (None, "unknown"),
};
Self {
cell,
status: status.to_string(),
}
}
}

#[derive(Clone, Debug, PartialEq, Deserialize)]
pub struct Config {
pub listen_addr: String,
Expand Down
11 changes: 10 additions & 1 deletion rpc/src/server.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::service::{BlockTemplate, RpcController};
use super::{BlockWithHash, CellOutputWithOutPoint, Config, TransactionWithHash};
use super::{BlockWithHash, CellOutputWithOutPoint, CellWithStatus, Config, TransactionWithHash};
use bigint::H256;
use ckb_core::cell::CellProvider;
use ckb_core::header::{BlockNumber, Header};
use ckb_core::transaction::{OutPoint, Transaction};
use ckb_network::NetworkService;
Expand Down Expand Up @@ -45,6 +46,10 @@ build_rpc_trait! {
// curl -d '{"id": 2, "jsonrpc": "2.0", "method":"get_cells_by_type_hash","params": ["0x1b1c832d02fdb4339f9868c8a8636c3d9dd10bd53ac7ce99595825bd6beeffb3", 1, 10]}' -H 'content-type:application/json' 'http://localhost:8114'
#[rpc(name = "get_cells_by_type_hash")]
fn get_cells_by_type_hash(&self, H256, u64, u64) -> Result<Vec<CellOutputWithOutPoint>>;

// curl -d '{"id": 2, "jsonrpc": "2.0", "method":"get_current_cell","params": [{"hash": "0x1b1c832d02fdb4339f9868c8a8636c3d9dd10bd53ac7ce99595825bd6beeffb3", "index": 1}]}' -H 'content-type:application/json' 'http://localhost:3030'
#[rpc(name = "get_current_cell")]
fn get_current_cell(&self, OutPoint) -> Result<CellWithStatus>;
}
}

Expand Down Expand Up @@ -131,6 +136,10 @@ impl<CI: ChainIndex + 'static> Rpc for RpcImpl<CI> {
.get_block_template(H256::from(0), 20000, 20000)
.map_err(|_| Error::internal_error())
}

fn get_current_cell(&self, out_point: OutPoint) -> Result<CellWithStatus> {
Ok(self.shared.cell(&out_point).into())
}
}

pub struct RpcServer {
Expand Down

0 comments on commit 781d5f5

Please sign in to comment.