use crate::Client;
use codec::Encode;
use cumulus_primitives_core::{ParachainBlockData, PersistedValidationData};
use cumulus_primitives_parachain_inherent::{ParachainInherentData, INHERENT_IDENTIFIER};
use cumulus_test_relay_sproof_builder::RelayStateSproofBuilder;
use cumulus_test_runtime::{Block, GetLastTimestamp, Hash, Header};
use polkadot_primitives::{BlockNumber as PBlockNumber, Hash as PHash};
use sc_block_builder::BlockBuilderBuilder;
use sp_api::ProvideRuntimeApi;
use sp_consensus_aura::Slot;
use sp_runtime::{
traits::{Block as BlockT, Header as HeaderT},
Digest, DigestItem,
};
pub struct BlockBuilderAndSupportData<'a> {
pub block_builder: sc_block_builder::BlockBuilder<'a, Block, Client>,
pub persisted_validation_data: PersistedValidationData<PHash, PBlockNumber>,
pub slot: Slot,
}
pub trait InitBlockBuilder {
fn init_block_builder(
&self,
validation_data: Option<PersistedValidationData<PHash, PBlockNumber>>,
relay_sproof_builder: RelayStateSproofBuilder,
) -> BlockBuilderAndSupportData;
fn init_block_builder_at(
&self,
at: Hash,
validation_data: Option<PersistedValidationData<PHash, PBlockNumber>>,
relay_sproof_builder: RelayStateSproofBuilder,
) -> BlockBuilderAndSupportData;
fn init_block_builder_with_timestamp(
&self,
at: Hash,
validation_data: Option<PersistedValidationData<PHash, PBlockNumber>>,
relay_sproof_builder: RelayStateSproofBuilder,
timestamp: u64,
) -> BlockBuilderAndSupportData;
}
fn init_block_builder(
client: &Client,
at: Hash,
validation_data: Option<PersistedValidationData<PHash, PBlockNumber>>,
mut relay_sproof_builder: RelayStateSproofBuilder,
timestamp: u64,
) -> BlockBuilderAndSupportData<'_> {
let slot: Slot = (timestamp / cumulus_test_runtime::SLOT_DURATION).into();
relay_sproof_builder.current_slot = slot;
let aura_pre_digest = Digest {
logs: vec![DigestItem::PreRuntime(sp_consensus_aura::AURA_ENGINE_ID, slot.encode())],
};
let mut block_builder = BlockBuilderBuilder::new(client)
.on_parent_block(at)
.fetch_parent_block_number(client)
.unwrap()
.enable_proof_recording()
.with_inherent_digests(aura_pre_digest)
.build()
.expect("Creates new block builder for test runtime");
let mut inherent_data = sp_inherents::InherentData::new();
inherent_data
.put_data(sp_timestamp::INHERENT_IDENTIFIER, ×tamp)
.expect("Put timestamp failed");
let (relay_parent_storage_root, relay_chain_state) =
relay_sproof_builder.into_state_root_and_proof();
let mut validation_data = validation_data.unwrap_or_default();
validation_data.relay_parent_storage_root = relay_parent_storage_root;
inherent_data
.put_data(
INHERENT_IDENTIFIER,
&ParachainInherentData {
validation_data: validation_data.clone(),
relay_chain_state,
downward_messages: Default::default(),
horizontal_messages: Default::default(),
},
)
.expect("Put validation function params failed");
let inherents = block_builder.create_inherents(inherent_data).expect("Creates inherents");
inherents
.into_iter()
.for_each(|ext| block_builder.push(ext).expect("Pushes inherent"));
BlockBuilderAndSupportData { block_builder, persisted_validation_data: validation_data, slot }
}
impl InitBlockBuilder for Client {
fn init_block_builder(
&self,
validation_data: Option<PersistedValidationData<PHash, PBlockNumber>>,
relay_sproof_builder: RelayStateSproofBuilder,
) -> BlockBuilderAndSupportData {
let chain_info = self.chain_info();
self.init_block_builder_at(chain_info.best_hash, validation_data, relay_sproof_builder)
}
fn init_block_builder_at(
&self,
at: Hash,
validation_data: Option<PersistedValidationData<PHash, PBlockNumber>>,
relay_sproof_builder: RelayStateSproofBuilder,
) -> BlockBuilderAndSupportData {
let last_timestamp = self.runtime_api().get_last_timestamp(at).expect("Get last timestamp");
let timestamp = if last_timestamp == 0 {
std::time::SystemTime::now()
.duration_since(std::time::SystemTime::UNIX_EPOCH)
.expect("Time is always after UNIX_EPOCH; qed")
.as_millis() as u64
} else {
last_timestamp + cumulus_test_runtime::SLOT_DURATION
};
init_block_builder(self, at, validation_data, relay_sproof_builder, timestamp)
}
fn init_block_builder_with_timestamp(
&self,
at: Hash,
validation_data: Option<PersistedValidationData<PHash, PBlockNumber>>,
relay_sproof_builder: RelayStateSproofBuilder,
timestamp: u64,
) -> BlockBuilderAndSupportData {
init_block_builder(self, at, validation_data, relay_sproof_builder, timestamp)
}
}
pub trait BuildParachainBlockData {
fn build_parachain_block(self, parent_state_root: Hash) -> ParachainBlockData<Block>;
}
impl<'a> BuildParachainBlockData for sc_block_builder::BlockBuilder<'a, Block, Client> {
fn build_parachain_block(self, parent_state_root: Hash) -> ParachainBlockData<Block> {
let built_block = self.build().expect("Builds the block");
let storage_proof = built_block
.proof
.expect("We enabled proof recording before.")
.into_compact_proof::<<Header as HeaderT>::Hashing>(parent_state_root)
.expect("Creates the compact proof");
let (header, extrinsics) = built_block.block.deconstruct();
ParachainBlockData::new(header, extrinsics, storage_proof)
}
}