diff --git a/rpc/README.md b/rpc/README.md index ef5e0dd91f..ae886834dc 100644 --- a/rpc/README.md +++ b/rpc/README.md @@ -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 @@ -472,7 +472,8 @@ echo '{ { "index": "0", "tx_hash": "0x29f94532fb6c7a17f13bcde5adb6e2921776ee6f357adf645e5393bd13442141" - } + }, + true ] }' \ | tr -d '\n' \ @@ -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" } diff --git a/rpc/json/rpc.json b/rpc/json/rpc.json index d45a1bea90..176a811513 100644 --- a/rpc/json/rpc.json +++ b/rpc/json/rpc.json @@ -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" }, diff --git a/rpc/src/module/chain.rs b/rpc/src/module/chain.rs index c08bbd1832..3c49f523b8 100644 --- a/rpc/src/module/chain.rs +++ b/rpc/src/module/chain.rs @@ -44,7 +44,7 @@ pub trait ChainRpc { ) -> Result>; #[rpc(name = "get_live_cell")] - fn get_live_cell(&self, _out_point: OutPoint) -> Result; + fn get_live_cell(&self, _out_point: OutPoint, _with_data: bool) -> Result; #[rpc(name = "get_tip_block_number")] fn get_tip_block_number(&self) -> Result; @@ -216,11 +216,11 @@ impl ChainRpc for ChainRpcImpl { Ok(result) } - fn get_live_cell(&self, out_point: OutPoint) -> Result { + fn get_live_cell(&self, out_point: OutPoint, with_data: bool) -> Result { let cell_status = self .shared .snapshot() - .cell(&out_point.clone().into(), false); + .cell(&out_point.clone().into(), with_data); Ok(cell_status.into()) } diff --git a/rpc/src/test.rs b/rpc/src/test.rs index d86f872859..d2fa305fc3 100644 --- a/rpc/src/test.rs +++ b/rpc/src/test.rs @@ -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"), diff --git a/test/src/rpc.rs b/test/src/rpc.rs index 3b36ea1563..5bb839bcd8 100644 --- a/test/src/rpc.rs +++ b/test/src/rpc.rs @@ -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") } @@ -363,7 +363,7 @@ jsonrpc_client!(pub struct Inner { _from: BlockNumber, _to: BlockNumber ) -> RpcRequest>; - pub fn get_live_cell(&mut self, _out_point: OutPoint) -> RpcRequest; + pub fn get_live_cell(&mut self, _out_point: OutPoint, _with_data: bool) -> RpcRequest; pub fn get_tip_block_number(&mut self) -> RpcRequest; pub fn get_current_epoch(&mut self) -> RpcRequest; pub fn get_epoch_by_number(&mut self, number: EpochNumber) -> RpcRequest>; diff --git a/util/jsonrpc-types/src/cell.rs b/util/jsonrpc-types/src/cell.rs index 7b87974bcb..6665de4220 100644 --- a/util/jsonrpc-types/src/cell.rs +++ b/util/jsonrpc-types/src/cell.rs @@ -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: @@ -15,19 +19,43 @@ pub struct CellOutputWithOutPoint { #[derive(Serialize, Deserialize)] pub struct CellWithStatus { - pub cell: Option, + pub cell: Option, pub status: String, } +#[derive(Serialize, Deserialize)] +pub struct CellInfo { + pub output: CellOutput, + pub data: Option, +} + +#[derive(Serialize, Deserialize)] +pub struct CellData { + pub content: JsonBytes, + pub hash: H256, +} + +impl From 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 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(), } }