Skip to content

Commit

Permalink
revert
Browse files Browse the repository at this point in the history
  • Loading branch information
driftluo committed Dec 24, 2024
1 parent f3fc490 commit bbf3902
Show file tree
Hide file tree
Showing 2 changed files with 197 additions and 2 deletions.
194 changes: 194 additions & 0 deletions src/rpc/ckb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,3 +395,197 @@ impl CkbRpcClient {
self.post::<_, Option<JsonBytes>>("get_fork_block", (block_hash, Some(Uint32::from(0u32))))
}
}

impl CkbRpcAsyncClient {
pub async fn get_packed_block(&self, hash: H256) -> Result<Option<JsonBytes>, crate::RpcError> {
self.post("get_block", (hash, Some(Uint32::from(0u32))))
.await
}

// turn block response into BlockView and cycle vec
fn transform_block_view_with_cycle(
opt_resp: Option<BlockResponse>,
) -> Result<Option<(BlockView, Vec<Cycle>)>, crate::rpc::RpcError> {
opt_resp
.map(|resp| match resp {
BlockResponse::Regular(block_view) => Ok((block_view.get_value()?, vec![])),
BlockResponse::WithCycles(block_cycles) => {
let cycles = transform_cycles(block_cycles.cycles);
Ok((block_cycles.block.get_value()?, cycles))
}
})
.transpose()
}
/// Same as get_block except with parameter with_cycles and return BlockResponse
pub async fn get_block_with_cycles(
&self,
hash: H256,
) -> Result<Option<(BlockView, Vec<Cycle>)>, crate::rpc::RpcError> {
let res = self
.post::<_, Option<BlockResponse>>("get_block", (hash, None::<u32>, true))
.await?;
Self::transform_block_view_with_cycle(res)
}

// turn BlockResponse to JsonBytes and Cycle tuple
fn blockresponse2bytes(
opt_resp: Option<BlockResponse>,
) -> Result<Option<(JsonBytes, Vec<Cycle>)>, crate::rpc::RpcError> {
opt_resp
.map(|resp| match resp {
BlockResponse::Regular(block_view) => Ok((block_view.get_json_bytes()?, vec![])),
BlockResponse::WithCycles(block_cycles) => {
let cycles = transform_cycles(block_cycles.cycles);
Ok((block_cycles.block.get_json_bytes()?, cycles))
}
})
.transpose()
}

pub async fn get_packed_block_with_cycles(
&self,
hash: H256,
) -> Result<Option<(JsonBytes, Vec<Cycle>)>, crate::rpc::RpcError> {
let res = self
.post::<_, Option<BlockResponse>>("get_block", (hash, Some(Uint32::from(0u32)), true))
.await?;
Self::blockresponse2bytes(res)
}

/// Same as get_block_by_number except with parameter with_cycles and return BlockResponse
pub async fn get_packed_block_by_number(
&self,
number: BlockNumber,
) -> Result<Option<JsonBytes>, crate::rpc::RpcError> {
self.post("get_block_by_number", (number, Some(Uint32::from(0u32))))
.await
}

pub async fn get_block_by_number_with_cycles(
&self,
number: BlockNumber,
) -> Result<Option<(BlockView, Vec<Cycle>)>, crate::rpc::RpcError> {
let res = self
.post::<_, Option<BlockResponse>>("get_block_by_number", (number, None::<u32>, true))
.await?;
Self::transform_block_view_with_cycle(res)
}

pub async fn get_packed_block_by_number_with_cycles(
&self,
number: BlockNumber,
) -> Result<Option<(JsonBytes, Vec<Cycle>)>, crate::rpc::RpcError> {
let res = self
.post::<_, Option<BlockResponse>>(
"get_block_by_number",
(number, Some(Uint32::from(0u32)), true),
)
.await?;
Self::blockresponse2bytes(res)
}

pub async fn get_packed_header(
&self,
hash: H256,
) -> Result<Option<JsonBytes>, crate::rpc::RpcError> {
self.post::<_, Option<JsonBytes>>("get_header", (hash, Some(Uint32::from(0u32))))
.await
}

pub async fn get_packed_header_by_number(
&self,
number: BlockNumber,
) -> Result<Option<JsonBytes>, crate::rpc::RpcError> {
self.post::<_, Option<JsonBytes>>(
"get_header_by_number",
(number, Some(Uint32::from(0u32))),
)
.await
}

pub async fn get_live_cell_with_include_tx_pool(
&self,
out_point: OutPoint,
with_data: bool,
include_tx_pool: bool,
) -> Result<CellWithStatus, crate::rpc::RpcError> {
self.post::<_, CellWithStatus>(
"get_live_cell",
(out_point, with_data, Some(include_tx_pool)),
)
.await
}

// get transaction with only_committed=true
pub async fn get_only_committed_transaction(
&self,
hash: H256,
) -> Result<TransactionWithStatusResponse, crate::rpc::RpcError> {
self.post::<_, TransactionWithStatusResponse>(
"get_transaction",
(hash, Some(Uint32::from(2u32)), true),
)
.await
}

// get transaction with verbosity=0
pub async fn get_packed_transaction(
&self,
hash: H256,
) -> Result<TransactionWithStatusResponse, crate::rpc::RpcError> {
self.post::<_, TransactionWithStatusResponse>(
"get_transaction",
(hash, Some(Uint32::from(0u32))),
)
.await
}

// get transaction with verbosity=0 and only_committed=true
pub async fn get_only_committed_packed_transaction(
&self,
hash: H256,
) -> Result<TransactionWithStatusResponse, crate::rpc::RpcError> {
self.post::<_, TransactionWithStatusResponse>(
"get_transaction",
(hash, Some(Uint32::from(0u32)), true),
)
.await
}

// get transaction with verbosity=1, so the result transaction field is None
pub async fn get_transaction_status(
&self,
hash: H256,
) -> Result<TransactionWithStatusResponse, crate::rpc::RpcError> {
self.post::<_, TransactionWithStatusResponse>(
"get_transaction",
(hash, Some(Uint32::from(1u32))),
)
.await
}

// get transaction with verbosity=1 and only_committed=true, so the result transaction field is None
pub async fn get_only_committed_transaction_status(
&self,
hash: H256,
) -> Result<TransactionWithStatusResponse, crate::rpc::RpcError> {
self.post::<_, TransactionWithStatusResponse>(
"get_transaction",
(hash, Some(Uint32::from(1u32)), true),
)
.await
}

pub async fn get_packed_tip_header(&self) -> Result<JsonBytes, crate::rpc::RpcError> {
self.post::<_, JsonBytes>("get_tip_header", (Some(Uint32::from(0u32)),))
.await
}

pub async fn get_packed_fork_block(
&self,
block_hash: H256,
) -> Result<Option<JsonBytes>, crate::rpc::RpcError> {
self.post::<_, Option<JsonBytes>>("get_fork_block", (block_hash, Some(Uint32::from(0u32))))
.await
}
}
5 changes: 3 additions & 2 deletions src/tests/ckb_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,9 @@ fn test_cargo_hang_2() {

#[test]
fn test_cargo_hang_3() {
let ckb_client = CkbRpcClient::new(TEST_CKB_RPC_URL);
let block = ckb_client.get_block_with_cycles(BLOCK_HASH_NOT_EXIST.clone());
let ckb_client = crate::rpc::CkbRpcAsyncClient::new(TEST_CKB_RPC_URL);
let block =
crate::rpc::block_on(ckb_client.get_block_with_cycles(BLOCK_HASH_NOT_EXIST.clone()));
let block = block.unwrap();
// assert!(block.is_none());
println!("999");
Expand Down

0 comments on commit bbf3902

Please sign in to comment.