From 546b017308d6b51013ea53aa76dab0bd2a6d4cf4 Mon Sep 17 00:00:00 2001 From: caojiafeng Date: Mon, 8 Mar 2021 21:31:28 +0800 Subject: [PATCH] rpc: list block return thin block --- rpc/api/src/types.rs | 21 ++++++++++++++++----- rpc/server/src/module/chain_rpc.rs | 2 +- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/rpc/api/src/types.rs b/rpc/api/src/types.rs index f445d776a3..8abb9a09eb 100644 --- a/rpc/api/src/types.rs +++ b/rpc/api/src/types.rs @@ -474,15 +474,18 @@ pub struct BlockView { pub uncles: Vec, } -impl TryFrom for BlockView { - type Error = anyhow::Error; - - fn try_from(block: Block) -> Result { +impl BlockView { + pub fn try_from_block(block: Block, thin: bool) -> Result { let (header, body) = block.into_inner(); let BlockBody { transactions, uncles, } = body; + let txns_view = if thin { + BlockTransactionsView::Hashes(transactions.into_iter().map(|t| t.id()).collect()) + } else { + transactions.try_into()? + }; Ok(BlockView { header: header.into(), uncles: uncles @@ -490,11 +493,19 @@ impl TryFrom for BlockView { .into_iter() .map(|h| h.into()) .collect(), - body: transactions.try_into()?, + body: txns_view, }) } } +impl TryFrom for BlockView { + type Error = anyhow::Error; + + fn try_from(block: Block) -> Result { + Self::try_from_block(block, false) + } +} + #[derive(Clone, Debug, Hash, Eq, PartialEq, Serialize, Deserialize)] pub struct BlockSummaryView { pub header: BlockHeaderView, diff --git a/rpc/server/src/module/chain_rpc.rs b/rpc/server/src/module/chain_rpc.rs index 841c88046f..b4d6687439 100644 --- a/rpc/server/src/module/chain_rpc.rs +++ b/rpc/server/src/module/chain_rpc.rs @@ -124,7 +124,7 @@ where Ok(block .into_iter() - .map(TryInto::try_into) + .map(|blk| BlockView::try_from_block(blk, true)) .collect::, _>>()?) } .map_err(map_err);