Skip to content

Commit c95ed5a

Browse files
committed
Use CkbRpcClient to read from Ckb chain
1 parent 09be2e2 commit c95ed5a

File tree

11 files changed

+545
-446
lines changed

11 files changed

+545
-446
lines changed

crates/fiber-bin/src/main.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ use ckb_resource::Resource;
33
use core::default::Default;
44
use fnn::actors::RootActor;
55
use fnn::cch::{CchArgs, CchFiberStoreWatcher};
6+
use fnn::ckb::client::CkbRpcClient;
67
use fnn::ckb::contracts::TypeIDResolver;
78
#[cfg(debug_assertions)]
89
use fnn::ckb::contracts::{get_cell_deps, Contract};
910
use fnn::ckb::{contracts::try_init_contracts_context, CkbChainActor};
10-
use fnn::fiber::{graph::NetworkGraph, network::init_chain_hash};
11+
use fnn::fiber::{graph::NetworkGraph, network::init_chain_hash, network::NetworkActorMessage};
1112
use fnn::rpc::server::start_rpc;
1213
use fnn::rpc::watchtower::{
1314
CreatePreimageParams, CreateWatchChannelParams, RemovePreimageParams, RemoveWatchChannelParams,
@@ -166,8 +167,10 @@ pub async fn main() -> Result<(), ExitMessage> {
166167

167168
info!("Starting fiber");
168169

169-
let network_actor = start_network(
170+
let chain_client = CkbRpcClient::new(&ckb_config);
171+
let network_actor: ActorRef<NetworkActorMessage> = start_network(
170172
fiber_config.clone(),
173+
chain_client,
171174
ckb_chain_actor.clone(),
172175
event_sender,
173176
new_tokio_task_tracker(),

crates/fiber-lib/src/ckb/actor.rs

Lines changed: 5 additions & 208 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
1-
use ckb_jsonrpc_types::JsonBytes;
2-
use ckb_sdk::{rpc::ckb_indexer::*, CkbRpcAsyncClient, RpcError};
3-
use ckb_types::{
4-
core::{tx_pool::TxStatus, TransactionView},
5-
packed,
6-
prelude::IntoTransactionView as _,
7-
};
1+
use ckb_sdk::RpcError;
2+
use ckb_types::{core::TransactionView, packed, prelude::IntoTransactionView as _};
83
use ractor::{concurrency::Duration, Actor, ActorProcessingErr, ActorRef, RpcReplyPort};
94
use serde::{Deserialize, Serialize};
105
use serde_with::serde_as;
@@ -19,7 +14,6 @@ use crate::{
1914

2015
use super::{
2116
funding::{FundingContext, LiveCellsExclusionMap},
22-
jsonrpc_types_convert::{transaction_view_from_json, tx_status_from_json},
2317
tx_tracing_actor::{
2418
CkbTxTracer, CkbTxTracingActor, CkbTxTracingArguments, CkbTxTracingMessage,
2519
},
@@ -39,117 +33,6 @@ pub struct CkbChainState {
3933
live_cells_exclusion_map: LiveCellsExclusionMap,
4034
}
4135

42-
#[derive(Debug, Clone)]
43-
pub struct GetBlockTimestampRequest {
44-
block_hash: Hash256,
45-
}
46-
47-
impl GetBlockTimestampRequest {
48-
pub fn from_block_hash(block_hash: Hash256) -> Self {
49-
Self { block_hash }
50-
}
51-
52-
pub fn block_hash(&self) -> Hash256 {
53-
self.block_hash
54-
}
55-
}
56-
57-
pub type GetBlockTimestampResponse = u64;
58-
59-
#[derive(Debug, Clone)]
60-
pub struct GetTxResponse {
61-
/// The transaction.
62-
pub transaction: Option<TransactionView>,
63-
pub tx_status: TxStatus,
64-
}
65-
66-
impl Default for GetTxResponse {
67-
fn default() -> Self {
68-
Self {
69-
transaction: None,
70-
tx_status: TxStatus::Unknown,
71-
}
72-
}
73-
}
74-
75-
impl From<Option<ckb_jsonrpc_types::TransactionWithStatusResponse>> for GetTxResponse {
76-
fn from(value: Option<ckb_jsonrpc_types::TransactionWithStatusResponse>) -> Self {
77-
match value {
78-
Some(response) => Self {
79-
transaction: response.transaction.map(|tx| match tx.inner {
80-
ckb_jsonrpc_types::Either::Left(json) => transaction_view_from_json(json),
81-
ckb_jsonrpc_types::Either::Right(_) => {
82-
panic!("bytes response format not used");
83-
}
84-
}),
85-
tx_status: tx_status_from_json(response.tx_status),
86-
},
87-
None => Self::default(),
88-
}
89-
}
90-
}
91-
92-
#[derive(Debug, Clone)]
93-
pub struct GetShutdownTxRequest {
94-
pub funding_lock_script: packed::Script,
95-
}
96-
97-
#[derive(Debug, Clone)]
98-
pub struct GetShutdownTxResponse {
99-
/// The transaction.
100-
pub transaction: Option<TransactionView>,
101-
pub tx_status: TxStatus,
102-
}
103-
104-
impl Default for GetShutdownTxResponse {
105-
fn default() -> Self {
106-
Self {
107-
transaction: None,
108-
tx_status: TxStatus::Unknown,
109-
}
110-
}
111-
}
112-
113-
impl From<Option<ckb_jsonrpc_types::TransactionWithStatusResponse>> for GetShutdownTxResponse {
114-
fn from(value: Option<ckb_jsonrpc_types::TransactionWithStatusResponse>) -> Self {
115-
match value {
116-
Some(response) => Self {
117-
transaction: response.transaction.map(|tx| match tx.inner {
118-
ckb_jsonrpc_types::Either::Left(json) => transaction_view_from_json(json),
119-
ckb_jsonrpc_types::Either::Right(_) => {
120-
panic!("bytes response format not used");
121-
}
122-
}),
123-
tx_status: tx_status_from_json(response.tx_status),
124-
},
125-
None => Self::default(),
126-
}
127-
}
128-
}
129-
130-
#[derive(Debug, Clone)]
131-
pub struct GetCellsRequest {
132-
pub search_key: SearchKey,
133-
pub order: Order,
134-
pub limit: u32,
135-
pub after: Option<JsonBytes>,
136-
}
137-
138-
#[derive(Serialize, Deserialize, Clone, Debug)]
139-
pub struct GetCellsResponse {
140-
pub objects: Vec<Cell>,
141-
pub last_cursor: JsonBytes,
142-
}
143-
144-
impl From<Pagination<Cell>> for GetCellsResponse {
145-
fn from(value: Pagination<Cell>) -> Self {
146-
Self {
147-
objects: value.objects,
148-
last_cursor: value.last_cursor,
149-
}
150-
}
151-
}
152-
15336
#[derive(Debug, AsRefStr)]
15437
pub enum CkbChainMessage {
15538
Fund(
@@ -174,21 +57,9 @@ pub enum CkbChainMessage {
17457
CommitFundingTx(Hash256, u64),
17558
Sign(FundingTx, RpcReplyPort<Result<FundingTx, FundingError>>),
17659
SendTx(TransactionView, RpcReplyPort<Result<(), RpcError>>),
177-
GetTx(Hash256, RpcReplyPort<Result<GetTxResponse, RpcError>>),
17860
CreateTxTracer(CkbTxTracer),
17961
RemoveTxTracers(Hash256),
180-
GetBlockTimestamp(
181-
GetBlockTimestampRequest,
182-
RpcReplyPort<Result<Option<GetBlockTimestampResponse>, RpcError>>,
183-
),
184-
GetShutdownTx(
185-
GetShutdownTxRequest,
186-
RpcReplyPort<Result<Option<GetShutdownTxResponse>, RpcError>>,
187-
),
188-
GetCells(
189-
GetCellsRequest,
190-
RpcReplyPort<Result<GetCellsResponse, RpcError>>,
191-
),
62+
19263
Stop,
19364
}
19465

@@ -325,14 +196,7 @@ impl Actor for CkbChainActor {
325196
let _ = reply_port.send(result);
326197
}
327198
}
328-
CkbChainMessage::GetTx(tx_hash, reply_port) => {
329-
let ckb_client = state.config.ckb_rpc_client();
330-
let result = ckb_client.get_transaction(tx_hash.into()).await;
331-
if !reply_port.is_closed() {
332-
// ignore error
333-
let _ = reply_port.send(result.map(Into::into));
334-
}
335-
}
199+
336200
CkbChainMessage::CreateTxTracer(tracer) => {
337201
debug!(
338202
"[{}] trace transaction {} with {} confs",
@@ -349,28 +213,7 @@ impl Actor for CkbChainActor {
349213
.ckb_tx_tracing_actor
350214
.send_message(CkbTxTracingMessage::RemoveTracers(tx_hash))?;
351215
}
352-
CkbChainMessage::GetBlockTimestamp(
353-
GetBlockTimestampRequest { block_hash },
354-
reply_port,
355-
) => {
356-
let ckb_client = state.config.ckb_rpc_client();
357-
let _ = reply_port.send(
358-
ckb_client
359-
.get_header(block_hash.into())
360-
.await
361-
.map(|x| x.map(|x| x.inner.timestamp.into())),
362-
);
363-
}
364-
CkbChainMessage::GetShutdownTx(request, reply_port) => {
365-
let client = state.config.ckb_rpc_client();
366-
let response = get_shutdown_tx(&client, request).await;
367-
let _ = reply_port.send(response);
368-
}
369-
CkbChainMessage::GetCells(request, reply_port) => {
370-
let client = state.config.ckb_rpc_client();
371-
let response = get_cells(&client, request).await;
372-
let _ = reply_port.send(response);
373-
}
216+
374217
CkbChainMessage::Stop => {
375218
myself.stop(Some("stop received".to_string()));
376219
}
@@ -458,49 +301,3 @@ async fn fund_via_shell(
458301
// Never called in WASM
459302
unreachable!();
460303
}
461-
462-
async fn get_shutdown_tx(
463-
client: &CkbRpcAsyncClient,
464-
GetShutdownTxRequest {
465-
funding_lock_script,
466-
}: GetShutdownTxRequest,
467-
) -> Result<Option<GetShutdownTxResponse>, RpcError> {
468-
// query transaction spent the funding cell
469-
let search_key = SearchKey {
470-
script: funding_lock_script.into(),
471-
script_type: ScriptType::Lock,
472-
script_search_mode: Some(SearchMode::Exact),
473-
with_data: None,
474-
filter: None,
475-
group_by_transaction: None,
476-
};
477-
let txs = client
478-
.get_transactions(search_key, Order::Desc, 1u32.into(), None)
479-
.await?;
480-
481-
let Some(Tx::Ungrouped(tx)) = txs.objects.first() else {
482-
return Ok(None);
483-
};
484-
if !matches!(tx.io_type, CellType::Input) {
485-
return Ok(None);
486-
}
487-
488-
let shutdown_tx_hash: Hash256 = tx.tx_hash.clone().into();
489-
let tx_with_status = client.get_transaction(shutdown_tx_hash.into()).await?;
490-
Ok(Some(tx_with_status.into()))
491-
}
492-
493-
async fn get_cells(
494-
client: &CkbRpcAsyncClient,
495-
GetCellsRequest {
496-
search_key,
497-
order,
498-
limit,
499-
after,
500-
}: GetCellsRequest,
501-
) -> Result<GetCellsResponse, RpcError> {
502-
client
503-
.get_cells(search_key, order, limit.into(), after)
504-
.await
505-
.map(GetCellsResponse::from)
506-
}

0 commit comments

Comments
 (0)