Skip to content

Commit

Permalink
support get block filter by block type
Browse files Browse the repository at this point in the history
  • Loading branch information
baichuan3 committed Dec 10, 2024
1 parent b5ba1d7 commit 83bdcc1
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 2 deletions.
93 changes: 93 additions & 0 deletions crates/rooch-open-rpc-spec/schemas/openrpc.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [],
Expand Down Expand Up @@ -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": {
Expand Down Expand Up @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions crates/rooch-rpc-api/src/api/rooch_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -218,6 +219,7 @@ pub trait RoochAPI {
#[method(name = "getBlocksByNumber")]
async fn get_blocks_by_number(
&self,
block_type: Option<BlockTypeView>,
cursor: Option<StrView<u128>>,
limit: Option<StrView<u64>>,
descending_order: Option<bool>,
Expand Down
18 changes: 17 additions & 1 deletion crates/rooch-rpc-api/src/jsonrpc_types/block_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -33,3 +34,18 @@ impl From<Block> for BlockView {
}
}
}

pub type BlockTypeView = StrView<BlockType>;

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<Self, Self::Err> {
Ok(StrView(BlockType::from_str(s)?))
}
}
4 changes: 3 additions & 1 deletion crates/rooch-rpc-server/src/server/rooch_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -862,10 +862,12 @@ impl RoochAPIServer for RoochServer {

async fn get_blocks_by_number(
&self,
block_type: Option<BlockTypeView>,
cursor: Option<StrView<u128>>,
limit: Option<StrView<u64>>,
descending_order: Option<bool>,
) -> RpcResult<BlockPageView> {
// TODO filter by blcok type
let latest_block_number = self.rpc_service.latest_block_number().await?;

let limit_of = min(
Expand Down
32 changes: 32 additions & 0 deletions crates/rooch-types/src/block.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down Expand Up @@ -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<Self, Self::Err> {
match s.to_lowercase().as_str() {
"all" => Ok(BlockType::All),
"finalized" => Ok(BlockType::Finalized),
s => Err(format_err!("Invalid block type str: {}", s)),
}
}
}

0 comments on commit 83bdcc1

Please sign in to comment.