Skip to content

Commit

Permalink
feat: implement Filecoin.ChainGetMessagesInTipset API (server side) (
Browse files Browse the repository at this point in the history
  • Loading branch information
hanabi1224 authored Nov 6, 2023
1 parent 6069bbd commit 7d5ef13
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ Forest v0.15.1 is a service release with support for the v0.14.1 database.

- [#3662](https://github.com/ChainSafe/forest/pull/3662) Add `--filter` and
`--fail-fast` flags to `forest-tool api compare`.
- [#3670](https://github.com/ChainSafe/forest/pull/3670) Implement the
`Filecoin.ChainGetMessagesInTipset` lotus-compatible RPC API.

### Changed

Expand Down
32 changes: 32 additions & 0 deletions src/rpc/chain_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::blocks::{BlockHeader, Tipset, TipsetKeys};
use crate::chain::index::ResolveNullTipset;
use crate::cid_collections::CidHashSet;
use crate::lotus_json::LotusJson;
use crate::rpc_api::data_types::ApiMessage;
use crate::rpc_api::{
chain_api::*,
data_types::{BlockMessages, RPCState},
Expand Down Expand Up @@ -36,6 +37,37 @@ pub(in crate::rpc) async fn chain_get_message<DB: Blockstore>(
Ok(LotusJson(ret))
}

pub(crate) async fn chain_get_messages_in_tipset<DB: Blockstore>(
data: Data<RPCState<DB>>,
Params(LotusJson((tsk,))): Params<LotusJson<(TipsetKeys,)>>,
) -> Result<LotusJson<Vec<ApiMessage>>, JsonRpcError> {
let store = data.chain_store.blockstore();
let tipset = Tipset::load_required(store, &tsk)?;
let full_tipset = tipset
.fill_from_blockstore(store)
.ok_or_else(|| anyhow::anyhow!("Failed to load full tipset"))?;
let blocks = full_tipset.into_blocks();
let mut messages = vec![];
let mut seen = CidHashSet::default();
for block in blocks {
for msg in block.bls_msgs() {
let cid = msg.cid()?;
if seen.insert(cid) {
messages.push(ApiMessage::new(cid, msg.clone()));
}
}

for msg in block.secp_msgs() {
let cid = msg.cid()?;
if seen.insert(cid) {
messages.push(ApiMessage::new(cid, msg.message.clone()));
}
}
}

Ok(LotusJson(messages))
}

pub(in crate::rpc) async fn chain_export<DB>(
data: Data<RPCState<DB>>,
Params(ChainExportParams {
Expand Down
4 changes: 4 additions & 0 deletions src/rpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ where
CHAIN_GET_MIN_BASE_FEE,
chain_api::chain_get_min_base_fee::<DB>,
)
.with_method(
CHAIN_GET_MESSAGES_IN_TIPSET,
chain_api::chain_get_messages_in_tipset::<DB>,
)
// Message Pool API
.with_method(MPOOL_PENDING, mpool_pending::<DB>)
.with_method(MPOOL_PUSH, mpool_push::<DB>)
Expand Down
8 changes: 7 additions & 1 deletion src/rpc_api/data_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,18 @@ impl Version {
}
}

#[derive(Debug, Serialize, Deserialize, Eq, PartialEq)]
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq)]
pub struct ApiMessage {
cid: Cid,
message: Message,
}

impl ApiMessage {
pub fn new(cid: Cid, message: Message) -> Self {
Self { cid, message }
}
}

#[derive(Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct ApiMessageLotusJson {
Expand Down
1 change: 1 addition & 0 deletions src/rpc_api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ pub static ACCESS_MAP: Lazy<HashMap<&str, Access>> = Lazy::new(|| {
access.insert(chain_api::CHAIN_GET_TIPSET, Access::Read);
access.insert(chain_api::CHAIN_SET_HEAD, Access::Admin);
access.insert(chain_api::CHAIN_GET_MIN_BASE_FEE, Access::Admin);
access.insert(chain_api::CHAIN_GET_MESSAGES_IN_TIPSET, Access::Read);

// Message Pool API
access.insert(mpool_api::MPOOL_PENDING, Access::Read);
Expand Down

0 comments on commit 7d5ef13

Please sign in to comment.