Skip to content

Commit

Permalink
Expose moon_getLatestSyncedBlock RPC endpoint (#2858)
Browse files Browse the repository at this point in the history
* Expose RPC endpoint
* add test
* apply review suggestions
  • Loading branch information
gonzamontiel authored Jul 10, 2024
1 parent f0ec9b4 commit 0cdea88
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 0 deletions.
19 changes: 19 additions & 0 deletions client/rpc/finality/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@
// You should have received a copy of the GNU General Public License
// along with Moonbeam. If not, see <http://www.gnu.org/licenses/>.
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 +36,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 +87,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",
},
},
};

0 comments on commit 0cdea88

Please sign in to comment.