diff --git a/node/src/service/mod.rs b/node/src/service/mod.rs index 7efddf423..20dfddfe1 100644 --- a/node/src/service/mod.rs +++ b/node/src/service/mod.rs @@ -40,12 +40,10 @@ use futures::FutureExt; // darwinia use dc_primitives::*; // polkadot-sdk -use cumulus_primitives_core::PersistedValidationData; -use cumulus_primitives_parachain_inherent::ParachainInherentData; -use cumulus_test_relay_sproof_builder::RelayStateSproofBuilder; 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"))] @@ -392,16 +390,33 @@ 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| { @@ -889,7 +904,7 @@ where prometheus_registry, ); let rpc_extensions_builder = { - let client = client.clone(); + let client_for_cidp = client.clone(); let pool = transaction_pool.clone(); let network = network.clone(); let filter_pool = filter_pool; @@ -900,32 +915,38 @@ 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), ); + // 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) = - RelayStateSproofBuilder::default().into_state_root_and_proof(); - let parachain_inherent_data = ParachainInherentData { - validation_data: PersistedValidationData { - relay_parent_number: u32::MAX, - relay_parent_storage_root, - ..Default::default() - }, - relay_chain_state, - downward_messages: Default::default(), - horizontal_messages: Default::default(), - }; - Ok((slot, timestamp, parachain_inherent_data)) + 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| { let deps = crate::rpc::FullDeps { - client: client.clone(), + client: client_for_cidp.clone(), pool: pool.clone(), graph: pool.pool().clone(), deny_unsafe, diff --git a/tests/ethereum/test-contract.ts b/tests/ethereum/test-contract.ts index 8e76865c9..22ef8e870 100644 --- a/tests/ethereum/test-contract.ts +++ b/tests/ethereum/test-contract.ts @@ -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;