Skip to content

Commit

Permalink
chore: impl review suggestion
Browse files Browse the repository at this point in the history
  • Loading branch information
driftluo committed Jan 17, 2025
1 parent f484708 commit ba035e3
Showing 1 changed file with 38 additions and 66 deletions.
104 changes: 38 additions & 66 deletions src/rpc/ckb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,42 +226,13 @@ impl CkbRpcClient {
self.post("get_block", (hash, Some(Uint32::from(0u32))))
}

// 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 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))?;
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()
transform_block_view_with_cycle(res)
}

pub fn get_packed_block_with_cycles(
Expand All @@ -272,7 +243,7 @@ impl CkbRpcClient {
"get_block",
(hash, Some(Uint32::from(0u32)), true),
)?;
Self::blockresponse2bytes(res)
blockresponse2bytes(res)
}

/// Same as get_block_by_number except with parameter with_cycles and return BlockResponse
Expand All @@ -289,7 +260,7 @@ impl CkbRpcClient {
) -> Result<Option<(BlockView, Vec<Cycle>)>, crate::rpc::RpcError> {
let res = self
.post::<_, Option<BlockResponse>>("get_block_by_number", (number, None::<u32>, true))?;
Self::transform_block_view_with_cycle(res)
transform_block_view_with_cycle(res)
}

pub fn get_packed_block_by_number_with_cycles(
Expand All @@ -300,7 +271,7 @@ impl CkbRpcClient {
"get_block_by_number",
(number, Some(Uint32::from(0u32)), true),
)?;
Self::blockresponse2bytes(res)
blockresponse2bytes(res)
}

pub fn get_packed_header(&self, hash: H256) -> Result<Option<JsonBytes>, crate::rpc::RpcError> {
Expand Down Expand Up @@ -402,20 +373,6 @@ impl CkbRpcAsyncClient {
.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,
Expand All @@ -424,22 +381,7 @@ impl CkbRpcAsyncClient {
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()
transform_block_view_with_cycle(res)
}

pub async fn get_packed_block_with_cycles(
Expand All @@ -449,7 +391,7 @@ impl CkbRpcAsyncClient {
let res = self
.post::<_, Option<BlockResponse>>("get_block", (hash, Some(Uint32::from(0u32)), true))
.await?;
Self::blockresponse2bytes(res)
blockresponse2bytes(res)
}

/// Same as get_block_by_number except with parameter with_cycles and return BlockResponse
Expand All @@ -468,7 +410,7 @@ impl CkbRpcAsyncClient {
let res = self
.post::<_, Option<BlockResponse>>("get_block_by_number", (number, None::<u32>, true))
.await?;
Self::transform_block_view_with_cycle(res)
transform_block_view_with_cycle(res)
}

pub async fn get_packed_block_by_number_with_cycles(
Expand All @@ -481,7 +423,7 @@ impl CkbRpcAsyncClient {
(number, Some(Uint32::from(0u32)), true),
)
.await?;
Self::blockresponse2bytes(res)
blockresponse2bytes(res)
}

pub async fn get_packed_header(
Expand Down Expand Up @@ -589,3 +531,33 @@ impl CkbRpcAsyncClient {
.await
}
}

// 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()
}

// 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()
}

0 comments on commit ba035e3

Please sign in to comment.