Skip to content

Commit

Permalink
Add RPC method moon_getEthSyncBlockRange (#2922)
Browse files Browse the repository at this point in the history
* update frontier pin

* add RPC method moon_getFrontierSyncBlockRange

* Use Eth instead of Frontier in method name

* fix rust tests

* tests: add codeMetadata key

* fix tests

* test: fix gas in test-randomness

---------

Co-authored-by: Andrea Giacobino <no.andrea@gmail.com>
Co-authored-by: Rodrigo Quelhas <rodrigo_quelhas@outlook.pt>
  • Loading branch information
3 people authored Sep 10, 2024
1 parent 1076dc2 commit 3398a8f
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 40 deletions.
52 changes: 26 additions & 26 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 15 additions & 8 deletions client/rpc/finality/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ pub trait MoonbeamFinalityApi {
#[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>;
/// Gets the range of blocks that are fully indexed in frontier's backend.
#[method(name = "moon_getEthSyncBlockRange")]
async fn get_frontier_sync_block_range(&self) -> RpcResult<(H256, H256)>;
}

pub struct MoonbeamFinality<B: Block, C> {
Expand Down Expand Up @@ -88,11 +88,18 @@ where
}
}

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(
async fn get_frontier_sync_block_range(&self) -> RpcResult<(H256, H256)> {
match (
self.backend.deref().first_block_hash().await,
self.backend.deref().latest_block_hash().await,
) {
(Ok(first), Ok(last)) => Ok((first, last)),
(Err(e), _) => Err(ErrorObject::owned(
jsonrpsee::types::error::UNKNOWN_ERROR_CODE,
"No synced block",
Some(e),
)),
(_, Err(e)) => Err(ErrorObject::owned(
jsonrpsee::types::error::UNKNOWN_ERROR_CODE,
"No synced block",
Some(e),
Expand Down
21 changes: 21 additions & 0 deletions precompiles/proxy/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,13 @@ fn fails_if_called_by_smart_contract() {
.execute_with(|| {
// Set code to Alice address as it if was a smart contract.
pallet_evm::AccountCodes::<Runtime>::insert(H160::from(Alice), vec![10u8]);
pallet_evm::AccountCodesMetadata::<Runtime>::insert(
H160::from(Alice),
pallet_evm::CodeMetadata {
size: 10,
hash: H256::default(),
},
);

PrecompilesValue::get()
.prepare_test(
Expand Down Expand Up @@ -777,7 +784,21 @@ fn proxy_proxy_should_fail_if_called_by_smart_contract_for_a_non_eoa_account() {
.execute_with(|| {
// Set code to Alice & Bob addresses as if they are smart contracts.
pallet_evm::AccountCodes::<Runtime>::insert(H160::from(Alice), vec![10u8]);
pallet_evm::AccountCodesMetadata::<Runtime>::insert(
H160::from(Alice),
pallet_evm::CodeMetadata {
size: 10,
hash: H256::default(),
},
);
pallet_evm::AccountCodes::<Runtime>::insert(H160::from(Bob), vec![10u8]);
pallet_evm::AccountCodesMetadata::<Runtime>::insert(
H160::from(Bob),
pallet_evm::CodeMetadata {
size: 10,
hash: H256::default(),
},
);

// Bob allows Alice to make calls on his behalf
assert_ok!(RuntimeCall::Proxy(ProxyCall::add_proxy {
Expand Down
7 changes: 7 additions & 0 deletions precompiles/xcm-utils/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,13 @@ fn execute_fails_if_called_by_smart_contract() {
.execute_with(|| {
// Set code to Alice address as it if was a smart contract.
pallet_evm::AccountCodes::<Runtime>::insert(H160::from(Alice), vec![10u8]);
pallet_evm::AccountCodesMetadata::<Runtime>::insert(
H160::from(Alice),
pallet_evm::CodeMetadata {
size: 10,
hash: sp_core::H256::default(),
},
);

let xcm_to_execute = VersionedXcm::<()>::V4(Xcm(vec![ClearOrigin])).encode();

Expand Down
21 changes: 19 additions & 2 deletions test/helpers/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ import { BN, hexToU8a, u8aToHex } from "@polkadot/util";
import { expect, DevModeContext } from "@moonwall/cli";
import { blake2AsU8a, xxhashAsU8a } from "@polkadot/util-crypto";
import { KeyringPair } from "@polkadot/keyring/types";
import type { PalletAssetsAssetAccount, PalletAssetsAssetDetails } from "@polkadot/types/lookup";
import type {
PalletAssetsAssetAccount,
PalletAssetsAssetDetails,
PalletEvmCodeMetadata,
} from "@polkadot/types/lookup";
import type { AccountId20 } from "@polkadot/types/interfaces/runtime";
import { encodeFunctionData, parseAbi } from "viem";
import { encodeFunctionData, keccak256, parseAbi } from "viem";

export const EVM_FOREIGN_ASSETS_PALLET_ACCOUNT = "0x6d6f646c666f7267617373740000000000000000";
export const ARBITRARY_ASSET_ID = 42259045809535163221576417993425387648n;
Expand Down Expand Up @@ -237,6 +241,18 @@ export async function mockOldAssetBalance(
const assetKey = xxhashAsU8a(new TextEncoder().encode("Asset"), 128);
const overallAssetKey = new Uint8Array([...module, ...assetKey, ...blake2concatAssetId]);
const evmCodeAssetKey = api.query.evm.accountCodes.key("0xFfFFfFff" + assetId.toHex().slice(2));
const evmCodesMetadataAssetKey = api.query.evm.accountCodesMetadata.key(
"0xFfFFfFff" + assetId.toHex().slice(2)
);

const codeSize = DUMMY_REVERT_BYTECODE.slice(2).length / 2;
const codeMetadataHash = keccak256(DUMMY_REVERT_BYTECODE);
const mockPalletEvmCodeMetadata: PalletEvmCodeMetadata = context
.polkadotJs()
.createType("PalletEvmCodeMetadata", {
size: codeSize,
hash: codeMetadataHash,
});

await context.createBlock(
api.tx.sudo
Expand All @@ -250,6 +266,7 @@ export async function mockOldAssetBalance(
.toString(16)
.padStart(2)}${DUMMY_REVERT_BYTECODE.slice(2)}`,
],
[evmCodesMetadataAssetKey, u8aToHex(mockPalletEvmCodeMetadata.toU8a())],
])
)
.signAsync(sudoAccount)
Expand Down
6 changes: 4 additions & 2 deletions test/suites/dev/moonbase/test-moon/test-moon-rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,10 @@ describeSuite({
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);
const firstBlockHash = (await context.polkadotJs().rpc.chain.getBlockHash(0)).toHex();
const resp = await customDevRpcRequest("moon_getEthSyncBlockRange", []);
expect(resp[0], "First block hash").toBe(firstBlockHash);
expect(resp[1], "Latest block hash").toBe(expected.block.hash);
},
});
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describeSuite({
functionName: "fulfillRequest",
args: [0],
rawTxOnly: true,
gas: 500_000n,
gas: 700_000n, // TODO: estimate gas and snapshot the estimation
privateKey: BALTATHAR_PRIVATE_KEY,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describeSuite({
precompileName: "Randomness",
functionName: "fulfillRequest",
args: [0],
gas: 500_000n,
gas: 700_000n, // TODO: estimate gas and snapshot the estimation
rawTxOnly: true,
});
const { result } = await context.createBlock(rawTxn);
Expand Down

0 comments on commit 3398a8f

Please sign in to comment.