Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose moon_getLatestSyncedBlock RPC endpoint #2858

Merged
merged 8 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions client/rpc/finality/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@

// You should have received a copy of the GNU General Public License
// along with Moonbeam. If not, see <http://www.gnu.org/licenses/>.
pub use fc_db::sql::{self};
gonzamontiel marked this conversation as resolved.
Show resolved Hide resolved
use fc_rpc::frontier_backend_client::{self, is_canon};

use jsonrpsee::types::error::ErrorObject;
use jsonrpsee::{core::RpcResult, proc_macros::rpc};
use sp_blockchain::HeaderBackend;
use sp_core::H256;
use sp_runtime::traits::Block;
use std::ops::Deref;
use std::{marker::PhantomData, sync::Arc};

/// An RPC endpoint to check for finality of blocks and transactions in Moonbeam
Expand All @@ -33,6 +37,10 @@ pub trait MoonbeamFinalityApi {
/// Returns false if the transaction is not found
#[method(name = "moon_isTxFinalized")]
async fn is_tx_finalized(&self, tx_hash: H256) -> RpcResult<bool>;

/// Gets the latest block hash that is fully indexed in frontier's backend.
#[method(name = "moon_getLatestBlockHash")]
async fn get_latest_block_hash(&self) -> RpcResult<H256>;
}

pub struct MoonbeamFinality<B: Block, C> {
Expand Down Expand Up @@ -80,6 +88,18 @@ where
Ok(false)
}
}

async fn get_latest_block_hash(&self) -> RpcResult<H256> {
let res = self.backend.deref().latest_block_hash().await;
match res {
Ok(val) => Ok(val),
Err(e) => Err(ErrorObject::owned(
jsonrpsee::types::error::UNKNOWN_ERROR_CODE,
"No synced block",
Some(e),
)),
}
}
}

async fn is_block_finalized_inner<B: Block<Hash = H256>, C: HeaderBackend<B> + 'static>(
Expand Down
5 changes: 5 additions & 0 deletions moonbeam-types-bundle/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ export const rpcDefinitions: Record<string, Record<string, DefinitionRpc | Defin
params: [{ name: "txHash", type: "Hash" }],
type: "bool",
},
getLatestSyncedBlock: {
description: "Returns the latest synced block from frontier's backend",
params: [],
type: "u32",
},
},
};

Expand Down
10 changes: 10 additions & 0 deletions test/suites/dev/moonbase/test-moon/test-moon-rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,5 +155,15 @@ describeSuite({
expect(resp, "Transaction finalization status mismatch").toBe(true);
},
});

it({
id: "T11",
title: "should return latest synced block",
test: async function () {
const expected = await context.createBlock([], { finalize: true });
const resp = await customDevRpcRequest("moon_getLatestBlockHash", []);
expect(resp, "Latest block hash").toBe(expected.block.hash);
},
});
},
});
2 changes: 2 additions & 0 deletions typescript-api/src/moonbase/interfaces/augment-api-rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,8 @@ declare module "@polkadot/rpc-core/types/jsonrpc" {
isBlockFinalized: AugmentedRpc<(blockHash: Hash | string | Uint8Array) => Observable<bool>>;
/** Returns whether an Ethereum transaction is finalized */
isTxFinalized: AugmentedRpc<(txHash: Hash | string | Uint8Array) => Observable<bool>>;
/** Returns the latest synced block from Frontier's backend */
getLatestSyncedBlock: AugmentedRpc<() => Observable<u32>>;
};
net: {
/** Returns true if client is actively listening for network connections. Otherwise false. */
Expand Down
5 changes: 5 additions & 0 deletions typescript-api/src/moonbase/interfaces/moon/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,10 @@ export default {
params: [{ name: "txHash", type: "Hash" }],
type: "bool",
},
getLatestSyncedBlock: {
description: "Returns the latest synced block from Frontier's backend",
params: [],
type: "u32",
},
},
};
Loading