Skip to content

Commit

Permalink
[rpc] add raw field into BlockView to support get raw block
Browse files Browse the repository at this point in the history
  • Loading branch information
coldnight committed Jun 29, 2022
1 parent baae183 commit 786e7ff
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 7 deletions.
19 changes: 17 additions & 2 deletions cmd/starcoin/src/chain/get_block_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ pub enum HashOrNumber {
pub struct GetBlockOpt {
#[clap(name = "hash-or-number")]
hash_or_number: HashOrNumber,

#[clap(name = "contains-raw-block")]
raw: bool,
}

impl FromStr for HashOrNumber {
Expand Down Expand Up @@ -52,10 +55,22 @@ impl CommandAction for GetBlockCommand {
let opt = ctx.opt();
let block = match opt.hash_or_number {
HashOrNumber::Hash(hash) => client
.chain_get_block_by_hash(hash, Some(GetBlockOption { decode: true }))?
.chain_get_block_by_hash(
hash,
Some(GetBlockOption {
decode: true,
raw: opt.raw,
}),
)?
.ok_or_else(|| anyhow::format_err!("block of hash {} not found", hash))?,
HashOrNumber::Number(number) => client
.chain_get_block_by_number(number, Some(GetBlockOption { decode: true }))?
.chain_get_block_by_number(
number,
Some(GetBlockOption {
decode: true,
raw: opt.raw,
}),
)?
.ok_or_else(|| anyhow::format_err!("block of height {} not found", number))?,
};
Ok(block)
Expand Down
3 changes: 3 additions & 0 deletions rpc/api/src/chain/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ pub struct GetTransactionOption {
pub struct GetBlockOption {
#[serde(default)]
pub decode: bool,

#[serde(default)]
pub raw: bool,
}

#[derive(Copy, Clone, Default, Serialize, Deserialize, JsonSchema)]
Expand Down
32 changes: 30 additions & 2 deletions rpc/api/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -744,15 +744,42 @@ impl From<Vec<HashValue>> for BlockTransactionsView {
}
}

#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize, JsonSchema)]
pub struct RawBlockView {
/// Raw block header that encoded in hex format.
pub header: String,

/// Raw block body that encoded in hex format.
pub body: String,
}

impl TryFrom<&Block> for RawBlockView {
type Error = anyhow::Error;

fn try_from(value: &Block) -> Result<Self, Self::Error> {
let header = hex::encode(bcs_ext::to_bytes(value.header())?);
let body = hex::encode(bcs_ext::to_bytes(&value.body)?);
Ok(Self { header, body })
}
}

#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize, JsonSchema)]
pub struct BlockView {
pub header: BlockHeaderView,
pub body: BlockTransactionsView,
pub uncles: Vec<BlockHeaderView>,

/// Raw block data that can be verified by block_hash and body_hash.
pub raw: Option<RawBlockView>,
}

impl BlockView {
pub fn try_from_block(block: Block, thin: bool) -> Result<Self, anyhow::Error> {
pub fn try_from_block(block: Block, thin: bool, raw: bool) -> Result<Self, anyhow::Error> {
let raw_block: Option<RawBlockView> = if raw {
Some(RawBlockView::try_from(&block)?)
} else {
None
};
let (header, body) = block.into_inner();
let BlockBody {
transactions,
Expand All @@ -771,6 +798,7 @@ impl BlockView {
.map(|h| h.into())
.collect(),
body: txns_view,
raw: raw_block,
})
}
}
Expand All @@ -779,7 +807,7 @@ impl TryFrom<Block> for BlockView {
type Error = anyhow::Error;

fn try_from(block: Block) -> Result<Self, Self::Error> {
Self::try_from_block(block, false)
Self::try_from_block(block, false, false)
}
}

Expand Down
71 changes: 71 additions & 0 deletions rpc/generated_rpc_schema/chain.json
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,10 @@
"decode": {
"default": false,
"type": "boolean"
},
"raw": {
"default": false,
"type": "boolean"
}
}
}
Expand Down Expand Up @@ -664,6 +668,27 @@
}
}
},
"raw": {
"description": "Raw block data that can be verified by block_hash and body_hash.",
"type": [
"object",
"null"
],
"required": [
"body",
"header"
],
"properties": {
"body": {
"description": "Block body that encoded in hex format.",
"type": "string"
},
"header": {
"description": "Block header that encoded in hex format.",
"type": "string"
}
}
},
"uncles": {
"type": "array",
"items": {
Expand Down Expand Up @@ -791,6 +816,10 @@
"decode": {
"default": false,
"type": "boolean"
},
"raw": {
"default": false,
"type": "boolean"
}
}
}
Expand Down Expand Up @@ -1190,6 +1219,27 @@
}
}
},
"raw": {
"description": "Raw block data that can be verified by block_hash and body_hash.",
"type": [
"object",
"null"
],
"required": [
"body",
"header"
],
"properties": {
"body": {
"description": "Block body that encoded in hex format.",
"type": "string"
},
"header": {
"description": "Block header that encoded in hex format.",
"type": "string"
}
}
},
"uncles": {
"type": "array",
"items": {
Expand Down Expand Up @@ -1711,6 +1761,27 @@
}
}
},
"raw": {
"description": "Raw block data that can be verified by block_hash and body_hash.",
"type": [
"object",
"null"
],
"required": [
"body",
"header"
],
"properties": {
"body": {
"description": "Block body that encoded in hex format.",
"type": "string"
},
"header": {
"description": "Block header that encoded in hex format.",
"type": "string"
}
}
},
"uncles": {
"type": "array",
"items": {
Expand Down
12 changes: 9 additions & 3 deletions rpc/server/src/module/chain_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,13 @@ where
) -> FutureResult<Option<BlockView>> {
let service = self.service.clone();
let decode = option.unwrap_or_default().decode;
let raw = option.unwrap_or_default().raw;
let storage = self.storage.clone();
let fut = async move {
let result = service.get_block_by_hash(hash).await?;
let mut block: Option<BlockView> = result.map(|b| b.try_into()).transpose()?;
let mut block: Option<BlockView> = result
.map(|b| BlockView::try_from_block(b, false, raw))
.transpose()?;
if decode {
let state = ChainStateDB::new(
storage,
Expand All @@ -110,11 +113,14 @@ where
) -> FutureResult<Option<BlockView>> {
let service = self.service.clone();
let decode = option.unwrap_or_default().decode;
let raw = option.unwrap_or_default().raw;
let storage = self.storage.clone();

let fut = async move {
let result = service.main_block_by_number(number).await?;
let mut block: Option<BlockView> = result.map(|b| b.try_into()).transpose()?;
let mut block: Option<BlockView> = result
.map(|b| BlockView::try_from_block(b, false, raw))
.transpose()?;
if decode {
let state = ChainStateDB::new(
storage,
Expand Down Expand Up @@ -153,7 +159,7 @@ where

block
.into_iter()
.map(|blk| BlockView::try_from_block(blk, true))
.map(|blk| BlockView::try_from_block(blk, true, false))
.collect::<Result<Vec<_>, _>>()
}
.map_err(map_err);
Expand Down
1 change: 1 addition & 0 deletions rpc/server/src/module/pubsub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,7 @@ impl EventHandler<Notification<ThinBlock>> for NewHeadHandler {
header: block.header.into(),
body: block.body.into(),
uncles: vec![],
raw: None,
})))]
}
}
Expand Down

0 comments on commit 786e7ff

Please sign in to comment.