From 83bdcc1a9e6c6829bf99bb46f5bddf50b5426055 Mon Sep 17 00:00:00 2001 From: baichuan3 Date: Tue, 10 Dec 2024 17:55:13 +0800 Subject: [PATCH] support get block filter by block type --- .../rooch-open-rpc-spec/schemas/openrpc.json | 93 +++++++++++++++++++ crates/rooch-rpc-api/src/api/rooch_api.rs | 2 + .../src/jsonrpc_types/block_view.rs | 18 +++- .../src/server/rooch_server.rs | 4 +- crates/rooch-types/src/block.rs | 32 +++++++ 5 files changed, 147 insertions(+), 2 deletions(-) diff --git a/crates/rooch-open-rpc-spec/schemas/openrpc.json b/crates/rooch-open-rpc-spec/schemas/openrpc.json index 617ad036d..6d2a99045 100644 --- a/crates/rooch-open-rpc-spec/schemas/openrpc.json +++ b/crates/rooch-open-rpc-spec/schemas/openrpc.json @@ -249,6 +249,36 @@ } } }, + { + "name": "rooch_getBlocksByNumber", + "params": [ + { + "name": "cursor", + "schema": { + "$ref": "#/components/schemas/u128" + } + }, + { + "name": "limit", + "schema": { + "$ref": "#/components/schemas/u64" + } + }, + { + "name": "descending_order", + "schema": { + "type": "boolean" + } + } + ], + "result": { + "name": "BlockPageView", + "required": true, + "schema": { + "$ref": "#/components/schemas/PageView_for_BlockView_and_u128" + } + } + }, { "name": "rooch_getChainID", "params": [], @@ -1062,6 +1092,40 @@ } } }, + "BlockView": { + "type": "object", + "required": [ + "block_hash", + "block_number", + "time" + ], + "properties": { + "block_hash": { + "description": "How many transactions in the block The hash of the block, made by DA", + "allOf": [ + { + "$ref": "#/components/schemas/primitive_types::H256" + } + ] + }, + "block_number": { + "description": "The index if the block", + "allOf": [ + { + "$ref": "#/components/schemas/u128" + } + ] + }, + "time": { + "description": "The previous tx accumulator root of the block The tx accumulator root after the last transaction append to the accumulator The last transaction's state root the block generate timestamp", + "allOf": [ + { + "$ref": "#/components/schemas/u64" + } + ] + } + } + }, "DAInfoView": { "type": "object", "properties": { @@ -2594,6 +2658,35 @@ } } }, + "PageView_for_BlockView_and_u128": { + "description": "`next_cursor` points to the last item in the page; Reading with `next_cursor` will start from the next item after `next_cursor` if `next_cursor` is `Some`, otherwise it will start from the first item.", + "type": "object", + "required": [ + "data", + "has_next_page" + ], + "properties": { + "data": { + "type": "array", + "items": { + "$ref": "#/components/schemas/BlockView" + } + }, + "has_next_page": { + "type": "boolean" + }, + "next_cursor": { + "anyOf": [ + { + "$ref": "#/components/schemas/u128" + }, + { + "type": "null" + } + ] + } + } + }, "PageView_for_EventView_and_u64": { "description": "`next_cursor` points to the last item in the page; Reading with `next_cursor` will start from the next item after `next_cursor` if `next_cursor` is `Some`, otherwise it will start from the first item.", "type": "object", diff --git a/crates/rooch-rpc-api/src/api/rooch_api.rs b/crates/rooch-rpc-api/src/api/rooch_api.rs index ee0e7e224..a67945c87 100644 --- a/crates/rooch-rpc-api/src/api/rooch_api.rs +++ b/crates/rooch-rpc-api/src/api/rooch_api.rs @@ -3,6 +3,7 @@ use crate::jsonrpc_types::account_view::BalanceInfoView; use crate::jsonrpc_types::address::UnitedAddressView; +use crate::jsonrpc_types::block_view::BlockTypeView; use crate::jsonrpc_types::event_view::{EventFilterView, IndexerEventIDView}; use crate::jsonrpc_types::repair_view::{RepairIndexerParamsView, RepairIndexerTypeView}; use crate::jsonrpc_types::transaction_view::{TransactionFilterView, TransactionWithInfoView}; @@ -218,6 +219,7 @@ pub trait RoochAPI { #[method(name = "getBlocksByNumber")] async fn get_blocks_by_number( &self, + block_type: Option, cursor: Option>, limit: Option>, descending_order: Option, diff --git a/crates/rooch-rpc-api/src/jsonrpc_types/block_view.rs b/crates/rooch-rpc-api/src/jsonrpc_types/block_view.rs index 3ab8ab88c..27985b09e 100644 --- a/crates/rooch-rpc-api/src/jsonrpc_types/block_view.rs +++ b/crates/rooch-rpc-api/src/jsonrpc_types/block_view.rs @@ -2,9 +2,10 @@ // SPDX-License-Identifier: Apache-2.0 use crate::jsonrpc_types::{H256View, StrView}; -use rooch_types::block::Block; +use rooch_types::block::{Block, BlockType}; use schemars::JsonSchema; use serde::{Deserialize, Serialize}; +use std::str::FromStr; #[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)] pub struct BlockView { @@ -33,3 +34,18 @@ impl From for BlockView { } } } + +pub type BlockTypeView = StrView; + +impl std::fmt::Display for BlockTypeView { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", self.0) + } +} + +impl FromStr for BlockTypeView { + type Err = anyhow::Error; + fn from_str(s: &str) -> anyhow::Result { + Ok(StrView(BlockType::from_str(s)?)) + } +} diff --git a/crates/rooch-rpc-server/src/server/rooch_server.rs b/crates/rooch-rpc-server/src/server/rooch_server.rs index cc5324846..c721494ef 100644 --- a/crates/rooch-rpc-server/src/server/rooch_server.rs +++ b/crates/rooch-rpc-server/src/server/rooch_server.rs @@ -15,7 +15,7 @@ use moveos_types::{ moveos_std::{move_module::MoveModule, object::ObjectID}, state::{AnnotatedState, FieldKey}, }; -use rooch_rpc_api::jsonrpc_types::block_view::BlockView; +use rooch_rpc_api::jsonrpc_types::block_view::{BlockTypeView, BlockView}; use rooch_rpc_api::jsonrpc_types::{ account_view::BalanceInfoView, event_view::{EventFilterView, EventView, IndexerEventIDView, IndexerEventView}, @@ -862,10 +862,12 @@ impl RoochAPIServer for RoochServer { async fn get_blocks_by_number( &self, + block_type: Option, cursor: Option>, limit: Option>, descending_order: Option, ) -> RpcResult { + // TODO filter by blcok type let latest_block_number = self.rpc_service.latest_block_number().await?; let limit_of = min( diff --git a/crates/rooch-types/src/block.rs b/crates/rooch-types/src/block.rs index eb78bc7ad..1e434f369 100644 --- a/crates/rooch-types/src/block.rs +++ b/crates/rooch-types/src/block.rs @@ -1,8 +1,12 @@ // Copyright (c) RoochNetwork // SPDX-License-Identifier: Apache-2.0 +use anyhow::format_err; use moveos_types::h256::H256; +use schemars::JsonSchema; use serde::{Deserialize, Serialize}; +use std::fmt::Display; +use std::str::FromStr; /// The block in Rooch is constructed by the proposer, representing a batch of transactions #[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] @@ -44,3 +48,31 @@ impl Block { } } } + +#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)] +#[serde(rename_all = "snake_case")] +pub enum BlockType { + All, + Finalized, +} + +impl Display for BlockType { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + BlockType::All => write!(f, "all"), + BlockType::Finalized => write!(f, "finalized"), + } + } +} + +impl FromStr for BlockType { + type Err = anyhow::Error; + + fn from_str(s: &str) -> anyhow::Result { + match s.to_lowercase().as_str() { + "all" => Ok(BlockType::All), + "finalized" => Ok(BlockType::Finalized), + s => Err(format_err!("Invalid block type str: {}", s)), + } + } +}