Skip to content

Commit

Permalink
Fix validation and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
boundless-forest committed Oct 9, 2024
1 parent d2b0de6 commit 66025c1
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 31 deletions.
83 changes: 52 additions & 31 deletions node/src/service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"))]
Expand Down Expand Up @@ -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| {
Expand Down Expand Up @@ -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;
Expand All @@ -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,
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 66025c1

Please sign in to comment.