Skip to content

Commit

Permalink
Fix pending runtime api (#1608)
Browse files Browse the repository at this point in the history
* Fix pending runtime api

* Fix validation and add test

* Revert useless change

* Format

---------

Co-authored-by: fisher <[email protected]>
  • Loading branch information
boundless-forest and hackfisher authored Oct 12, 2024
1 parent 61cdb96 commit 0da4f43
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 15 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ cumulus-primitives-core = { git = "https://github.com/darwini
cumulus-primitives-parachain-inherent = { git = "https://github.com/darwinia-network/polkadot-sdk", branch = "release-polkadot-v1.7.2", default-features = false }
cumulus-primitives-utility = { git = "https://github.com/darwinia-network/polkadot-sdk", branch = "release-polkadot-v1.7.2", default-features = false }
cumulus-relay-chain-interface = { git = "https://github.com/darwinia-network/polkadot-sdk", branch = "release-polkadot-v1.7.2" }
cumulus-test-relay-sproof-builder = { git = "https://github.com/darwinia-network/polkadot-sdk", branch = "release-polkadot-v1.7.2" }
frame-benchmarking = { git = "https://github.com/darwinia-network/polkadot-sdk", branch = "release-polkadot-v1.7.2", default-features = false }
frame-benchmarking-cli = { git = "https://github.com/darwinia-network/polkadot-sdk", branch = "release-polkadot-v1.7.2" }
frame-executive = { git = "https://github.com/darwinia-network/polkadot-sdk", branch = "release-polkadot-v1.7.2", default-features = false }
Expand Down
1 change: 1 addition & 0 deletions node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ cumulus-primitives-aura = { workspace = true, features = ["std"] }
cumulus-primitives-core = { workspace = true, features = ["std"] }
cumulus-primitives-parachain-inherent = { workspace = true, features = ["std"] }
cumulus-relay-chain-interface = { workspace = true }
cumulus-test-relay-sproof-builder = { workspace = true }
frame-benchmarking = { workspace = true, optional = true, features = ["std"] }
frame-benchmarking-cli = { workspace = true }
pallet-transaction-payment-rpc = { workspace = true }
Expand Down
68 changes: 53 additions & 15 deletions node/src/service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ use dc_primitives::*;
use sc_client_api::{Backend, HeaderBackend};
use sc_consensus::ImportQueue;
use sc_network::NetworkBlock;
use sp_consensus_aura::{Slot, SlotDuration};
use sp_core::Encode;

#[cfg(all(feature = "runtime-benchmarks", feature = "evm-tracing"))]
Expand Down Expand Up @@ -389,16 +390,34 @@ where
let collator = parachain_config.role.is_authority();
let eth_rpc_config = eth_rpc_config.clone();
let sync_service = sync_service.clone();
let slot_duration = sc_consensus_aura::slot_duration(&*client)?;
let pending_create_inherent_data_providers = move |_, ()| async move {
let current = sp_timestamp::InherentDataProvider::from_system_time();
let next_slot = current.timestamp().as_millis() + slot_duration.as_millis();
let timestamp = sp_timestamp::InherentDataProvider::new(next_slot.into());
let slot = sp_consensus_aura::inherents::InherentDataProvider::from_timestamp_and_slot_duration(
*timestamp,
slot_duration,
let timestamp = sp_timestamp::InherentDataProvider::from_system_time();
let relay_chain_slot = Slot::from_timestamp(
timestamp.timestamp(),
SlotDuration::from_millis(RELAY_CHAIN_SLOT_DURATION_MILLIS as u64),
);
Ok((slot, timestamp))

// Create a mocked parachain inherent data provider to pass all validations in the
// parachain system. Without this, the pending functionality will fail.
let mut state_proof_builder =
cumulus_test_relay_sproof_builder::RelayStateSproofBuilder::default();
state_proof_builder.para_id = para_id;
state_proof_builder.current_slot = relay_chain_slot;
state_proof_builder.included_para_head = Some(polkadot_primitives::HeadData(vec![]));
let (relay_parent_storage_root, relay_chain_state) =
state_proof_builder.into_state_root_and_proof();
let parachain_inherent_data =
cumulus_primitives_parachain_inherent::ParachainInherentData {
validation_data: cumulus_primitives_core::PersistedValidationData {
relay_parent_number: u32::MAX,
relay_parent_storage_root,
..Default::default()
},
relay_chain_state,
downward_messages: Default::default(),
horizontal_messages: Default::default(),
};
Ok((timestamp, parachain_inherent_data))
};

Box::new(move |deny_unsafe, subscription_task_executor| {
Expand Down Expand Up @@ -897,15 +916,34 @@ where
let collator = config.role.is_authority();
let eth_rpc_config = eth_rpc_config.clone();
let sync_service = sync_service.clone();

let pending_create_inherent_data_providers = move |_, ()| async move {
let current = sp_timestamp::InherentDataProvider::from_system_time();
let next_slot = current.timestamp().as_millis() + slot_duration.as_millis();
let timestamp = sp_timestamp::InherentDataProvider::new(next_slot.into());
let slot = sp_consensus_aura::inherents::InherentDataProvider::from_timestamp_and_slot_duration(
*timestamp,
slot_duration,
let timestamp = sp_timestamp::InherentDataProvider::from_system_time();
let relay_chain_slot = Slot::from_timestamp(
timestamp.timestamp(),
SlotDuration::from_millis(RELAY_CHAIN_SLOT_DURATION_MILLIS as u64),
);
Ok((slot, timestamp))
// Create a mocked parachain inherent data provider to pass all validations in the
// parachain system. Without this, the pending functionality will fail.
let mut state_proof_builder =
cumulus_test_relay_sproof_builder::RelayStateSproofBuilder::default();
state_proof_builder.para_id = para_id;
state_proof_builder.current_slot = relay_chain_slot;
state_proof_builder.included_para_head = Some(polkadot_primitives::HeadData(vec![]));
let (relay_parent_storage_root, relay_chain_state) =
state_proof_builder.into_state_root_and_proof();
let parachain_inherent_data =
cumulus_primitives_parachain_inherent::ParachainInherentData {
validation_data: cumulus_primitives_core::PersistedValidationData {
relay_parent_number: u32::MAX,
relay_parent_storage_root,
..Default::default()
},
relay_chain_state,
downward_messages: Default::default(),
horizontal_messages: Default::default(),
};
Ok((timestamp, parachain_inherent_data))
};

Box::new(move |deny_unsafe, subscription_task_executor| {
Expand Down
11 changes: 11 additions & 0 deletions tests/ethereum/test-block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@ describe("Test Block RPC", () => {
expect(await web3.eth.getBlockNumber()).to.not.equal(0);
});

it("Get block by tags", async () => {
let earliest = await web3.eth.getBlock("earliest");
expect(earliest.number).to.equal(0);

let latest = await web3.eth.getBlock("latest");
expect(latest.number).to.be.a("number");

let pending = await web3.eth.getBlock("pending");
expect(pending.number).to.be.a("number");
});

it("Get block by hash", async () => {
let latest_block = await web3.eth.getBlock("latest");
let block = await web3.eth.getBlock(latest_block.hash);
Expand Down
11 changes: 11 additions & 0 deletions tests/ethereum/test-contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ describe("Test contract", () => {
expect(await inc.methods.number().call()).to.be.equal("5");
});

step("Get default number in pending block", async function () {
const result = await web3.eth.call(
{
to: inc.options.address,
data: inc.methods.number().encodeABI(),
},
"pending"
);
expect(web3.utils.hexToNumberString(result)).to.be.equal("5");
});

step("Increase number", async function () {
let receipt = await inc.methods.increment(3).send();
transact_hash = receipt.transactionHash;
Expand Down

0 comments on commit 0da4f43

Please sign in to comment.