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

Add RPC method moon_getEthSyncBlockRange #2922

Merged
merged 13 commits into from
Sep 10, 2024
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
Loading