Skip to content

Commit

Permalink
feat(rpc): add rpc method estimate_cycles
Browse files Browse the repository at this point in the history
  • Loading branch information
yangby-cryptape committed Jan 18, 2024
1 parent 5fc2a81 commit 1935c4f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ Submits a new transaction and broadcast it to network peers
curl http://localhost:9000/ -X POST -H "Content-Type: application/json" -d '{"jsonrpc": "2.0", "method": "send_transaction", "params": [{"cell_deps":[{"dep_type":"dep_group","out_point":{"index":"0x0","tx_hash":"0xf8de3bb47d055cdf460d93a2a6e1b05f7432f9777c8c474abf4eec1d4aee5d37"}}],"header_deps":[],"inputs":[{"previous_output":{"index":"0x7","tx_hash":"0x8f8c79eb6671709633fe6a46de93c0fedc9c1b8a6527a18d3983879542635c9f"},"since":"0x0"}],"outputs":[{"capacity":"0x470de4df820000","lock":{"args":"0xff5094c2c5f476fc38510018609a3fd921dd28ad","code_hash":"0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8","hash_type":"type"},"type":null},{"capacity":"0xb61134e5a35e800","lock":{"args":"0x64257f00b6b63e987609fa9be2d0c86d351020fb","code_hash":"0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8","hash_type":"type"},"type":null}],"outputs_data":["0x","0x"],"version":"0x0","witnesses":["0x5500000010000000550000005500000041000000af34b54bebf8c5971da6a880f2df5a186c3f8d0b5c9a1fe1a90c95b8a4fb89ef3bab1ccec13797dcb3fee80400f953227dd7741227e08032e3598e16ccdaa49c00"]}], "id": 1}'
```

### `estimate_cycles`

This RPC method is the same as CKB's, please refer to [CKB JSON-RPC Protocols](https://github.com/nervosnetwork/ckb/tree/develop/rpc#method-estimate_cycles).

### `get_tip_header`

Returns the header with the highest block number in the canonical chain
Expand Down
27 changes: 23 additions & 4 deletions src/service.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use ckb_chain_spec::consensus::Consensus;
use ckb_jsonrpc_types::{
BlockNumber, BlockView, Capacity, CellOutput, Cycle, HeaderView, JsonBytes, NodeAddress,
OutPoint, RemoteNodeProtocol, Script, Transaction, TransactionView, Uint32, Uint64,
BlockNumber, BlockView, Capacity, CellOutput, Cycle, EstimateCycles, HeaderView, JsonBytes,
NodeAddress, OutPoint, RemoteNodeProtocol, Script, Transaction, TransactionView, Uint32,
Uint64,
};
use ckb_network::{extract_peer_id, NetworkController};
use ckb_systemtime::unix_time_as_millis;
Expand Down Expand Up @@ -90,6 +91,9 @@ pub trait ChainRpc {

#[rpc(name = "fetch_header")]
fn fetch_header(&self, block_hash: H256) -> Result<FetchStatus<HeaderView>>;

#[rpc(name = "estimate_cycles")]
fn estimate_cycles(&self, tx: Transaction) -> Result<EstimateCycles>;
}

#[rpc(server)]
Expand Down Expand Up @@ -387,6 +391,7 @@ pub struct TransactionRpcImpl {

pub struct ChainRpcImpl {
pub(crate) swc: StorageWithChainData,
pub(crate) consensus: Arc<Consensus>,
}

pub struct NetRpcImpl {
Expand Down Expand Up @@ -1292,6 +1297,16 @@ impl ChainRpc for ChainRpcImpl {
timestamp: now.into(),
})
}

fn estimate_cycles(&self, tx: Transaction) -> Result<EstimateCycles> {
let tx: packed::Transaction = tx.into();
let tx = tx.into_view();
let cycles = verify_tx(tx.clone(), &self.swc, Arc::clone(&self.consensus))
.map_err(|e| Error::invalid_params(format!("invalid transaction: {:?}", e)))?;
Ok(EstimateCycles {
cycles: cycles.into(),
})
}
}

pub(crate) struct Service {
Expand All @@ -1315,12 +1330,16 @@ impl Service {
) -> Server {
let mut io_handler = IoHandler::new();
let swc = StorageWithChainData::new(storage, Arc::clone(&peers));
let consensus = Arc::new(consensus);
let block_filter_rpc_impl = BlockFilterRpcImpl { swc: swc.clone() };
let chain_rpc_impl = ChainRpcImpl { swc: swc.clone() };
let chain_rpc_impl = ChainRpcImpl {
swc: swc.clone(),
consensus: Arc::clone(&consensus),
};
let transaction_rpc_impl = TransactionRpcImpl {
pending_txs,
swc,
consensus: Arc::new(consensus),
consensus,
};
let net_rpc_impl = NetRpcImpl {
network_controller,
Expand Down
5 changes: 4 additions & 1 deletion src/tests/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -776,7 +776,10 @@ fn rpc() {

let swc = StorageWithChainData::new(storage.clone(), Arc::clone(&peers));

let rpc = ChainRpcImpl { swc };
let rpc = ChainRpcImpl {
swc,
consensus: Arc::new(Consensus::default()),
};
let header = rpc
.get_header(pre_block.header().hash().unpack())
.unwrap()
Expand Down

0 comments on commit 1935c4f

Please sign in to comment.