Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Return cell data in get_live_cell rpc #1527

Merged
merged 1 commit into from
Sep 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions rpc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ http://localhost:8114

### `get_live_cell`

Returns the information about a cell by out_point if it is live.
Returns the information about a cell by out_point if it is live. If second with_data argument set to true, will return cell data and data_hash if it is live

#### Parameters

Expand All @@ -472,7 +472,8 @@ echo '{
{
"index": "0",
"tx_hash": "0x29f94532fb6c7a17f13bcde5adb6e2921776ee6f357adf645e5393bd13442141"
}
},
true
]
}' \
| tr -d '\n' \
Expand All @@ -486,13 +487,19 @@ http://localhost:8114
"jsonrpc": "2.0",
"result": {
"cell": {
"capacity": "34400000000",
"lock": {
"args": [],
"code_hash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"hash_type": "data"
"data": {
"content": "0x7f454c460201010000000000000000000200f3000100000078000100000000004000000000000000980000000000000005000000400038000100400003000200010000000500000000000000000000000000010000000000000001000000000082000000000000008200000000000000001000000000000001459308d00573000000002e7368737472746162002e74657874000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000b000000010000000600000000000000780001000000000078000000000000000a0000000000000000000000000000000200000000000000000000000000000001000000030000000000000000000000000000000000000082000000000000001100000000000000000000000000000001000000000000000000000000000000",
"hash": "0x28e83a1277d48add8e72fadaa9248559e1b632bab2bd60b27955ebc4c03800a5"
},
"type": null
"output": {
"capacity": "34400000000",
"lock": {
"args": [],
"code_hash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"hash_type": "data"
},
"type": null
}
},
"status": "live"
}
Expand Down
23 changes: 15 additions & 8 deletions rpc/json/rpc.json
Original file line number Diff line number Diff line change
Expand Up @@ -242,24 +242,31 @@
]
},
{
"description": "Returns the information about a cell by out_point if it is live.",
"description": "Returns the information about a cell by out_point if it is live. If second with_data argument set to true, will return cell data and data_hash if it is live",
"method": "get_live_cell",
"module": "chain",
"params": [
{
"index": "0",
"tx_hash": "0x29f94532fb6c7a17f13bcde5adb6e2921776ee6f357adf645e5393bd13442141"
}
},
true
],
"result": {
"cell": {
"capacity": "34400000000",
"lock": {
"args": [],
"code_hash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"hash_type": "data"
"output": {
"capacity": "34400000000",
"lock": {
"args": [],
"code_hash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"hash_type": "data"
},
"type": null
},
"type": null
"data": {
"content": "0x7f454c460201010000000000000000000200f3000100000078000100000000004000000000000000980000000000000005000000400038000100400003000200010000000500000000000000000000000000010000000000000001000000000082000000000000008200000000000000001000000000000001459308d00573000000002e7368737472746162002e74657874000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000b000000010000000600000000000000780001000000000078000000000000000a0000000000000000000000000000000200000000000000000000000000000001000000030000000000000000000000000000000000000082000000000000001100000000000000000000000000000001000000000000000000000000000000",
"hash": "0x28e83a1277d48add8e72fadaa9248559e1b632bab2bd60b27955ebc4c03800a5"
}
},
"status": "live"
},
Expand Down
6 changes: 3 additions & 3 deletions rpc/src/module/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub trait ChainRpc {
) -> Result<Vec<CellOutputWithOutPoint>>;

#[rpc(name = "get_live_cell")]
fn get_live_cell(&self, _out_point: OutPoint) -> Result<CellWithStatus>;
fn get_live_cell(&self, _out_point: OutPoint, _with_data: bool) -> Result<CellWithStatus>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change with_data to optional param:

Suggested change
fn get_live_cell(&self, _out_point: OutPoint, _with_data: bool) -> Result<CellWithStatus>;
fn get_live_cell(&self, _out_point: OutPoint, _with_data: Option<bool>) -> Result<CellWithStatus>;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better explicit here, if we add another more argument in the future, it will make user confused.


#[rpc(name = "get_tip_block_number")]
fn get_tip_block_number(&self) -> Result<BlockNumber>;
Expand Down Expand Up @@ -216,11 +216,11 @@ impl ChainRpc for ChainRpcImpl {
Ok(result)
}

fn get_live_cell(&self, out_point: OutPoint) -> Result<CellWithStatus> {
fn get_live_cell(&self, out_point: OutPoint, with_data: bool) -> Result<CellWithStatus> {
let cell_status = self
.shared
.snapshot()
.cell(&out_point.clone().into(), false);
.cell(&out_point.clone().into(), with_data);
Ok(cell_status.into())
}

Expand Down
2 changes: 1 addition & 1 deletion rpc/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ fn params_of(shared: &Shared, method: &str) -> Value {
| "get_transactions_by_lock_hash" => {
vec![always_success_script_hash, json!("0"), json!("2")]
}
"get_live_cell" => vec![always_success_out_point],
"get_live_cell" => vec![always_success_out_point, json!(true)],
"set_ban" => vec![
json!("192.168.0.2"),
json!("insert"),
Expand Down
6 changes: 3 additions & 3 deletions test/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,10 @@ impl RpcClient {
.expect("rpc call get_cells_by_lock_hash")
}

pub fn get_live_cell(&self, out_point: OutPoint) -> CellWithStatus {
pub fn get_live_cell(&self, out_point: OutPoint, with_data: bool) -> CellWithStatus {
self.inner
.lock()
.get_live_cell(out_point)
.get_live_cell(out_point, with_data)
.call()
.expect("rpc call get_live_cell")
}
Expand Down Expand Up @@ -363,7 +363,7 @@ jsonrpc_client!(pub struct Inner {
_from: BlockNumber,
_to: BlockNumber
) -> RpcRequest<Vec<CellOutputWithOutPoint>>;
pub fn get_live_cell(&mut self, _out_point: OutPoint) -> RpcRequest<CellWithStatus>;
pub fn get_live_cell(&mut self, _out_point: OutPoint, _with_data: bool) -> RpcRequest<CellWithStatus>;
pub fn get_tip_block_number(&mut self) -> RpcRequest<BlockNumber>;
pub fn get_current_epoch(&mut self) -> RpcRequest<EpochView>;
pub fn get_epoch_by_number(&mut self, number: EpochNumber) -> RpcRequest<Option<EpochView>>;
Expand Down
38 changes: 33 additions & 5 deletions util/jsonrpc-types/src/cell.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use crate::{Capacity, CellOutput, OutPoint, Script};
use ckb_types::{core::cell::CellStatus, H256};
use crate::{Capacity, CellOutput, JsonBytes, OutPoint, Script};
use ckb_types::{
core::cell::{CellMeta, CellStatus},
prelude::Unpack,
H256,
};
use serde_derive::{Deserialize, Serialize};

// This is used as return value of get_cells_by_lock_hash RPC:
Expand All @@ -15,19 +19,43 @@ pub struct CellOutputWithOutPoint {

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

#[derive(Serialize, Deserialize)]
pub struct CellInfo {
pub output: CellOutput,
pub data: Option<CellData>,
}

#[derive(Serialize, Deserialize)]
pub struct CellData {
pub content: JsonBytes,
pub hash: H256,
}

impl From<CellMeta> for CellInfo {
fn from(cell_meta: CellMeta) -> Self {
CellInfo {
output: cell_meta.cell_output.into(),
data: cell_meta.mem_cell_data.map(|(data, hash)| CellData {
content: JsonBytes::from_bytes(data),
hash: hash.unpack(),
}),
}
}
}

impl From<CellStatus> for CellWithStatus {
fn from(status: CellStatus) -> Self {
let (cell, status) = match status {
CellStatus::Live(cell_meta) => (Some(cell_meta.cell_output), "live"),
CellStatus::Live(cell_meta) => (Some(cell_meta), "live"),
CellStatus::Dead => (None, "dead"),
CellStatus::Unknown => (None, "unknown"),
};
Self {
cell: cell.map(Into::into),
cell: cell.map(|cell_meta| (*cell_meta).into()),
status: status.to_string(),
}
}
Expand Down