mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-26 14:37:57 +00:00
cargo +nightly fmt (#3540)
* cargo +nightly fmt * add cargo-fmt check to ci * update ci * fmt * fmt * skip macro * ignore bridges
This commit is contained in:
@@ -15,21 +15,26 @@
|
||||
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{Client, FullBackend};
|
||||
use polkadot_test_runtime::{GetLastTimestamp, UncheckedExtrinsic};
|
||||
use parity_scale_codec::{Decode, Encode};
|
||||
use polkadot_primitives::v1::{Block, InherentData as ParachainsInherentData};
|
||||
use sp_runtime::{generic::BlockId, Digest, DigestItem};
|
||||
use polkadot_test_runtime::{GetLastTimestamp, UncheckedExtrinsic};
|
||||
use sc_block_builder::{BlockBuilder, BlockBuilderProvider};
|
||||
use sp_api::ProvideRuntimeApi;
|
||||
use sp_consensus_babe::{BABE_ENGINE_ID, digests::{PreDigest, SecondaryPlainPreDigest}};
|
||||
use sc_block_builder::{BlockBuilderProvider, BlockBuilder};
|
||||
use sp_consensus_babe::{
|
||||
digests::{PreDigest, SecondaryPlainPreDigest},
|
||||
BABE_ENGINE_ID,
|
||||
};
|
||||
use sp_runtime::{generic::BlockId, Digest, DigestItem};
|
||||
use sp_state_machine::BasicExternalities;
|
||||
use parity_scale_codec::{Encode, Decode};
|
||||
|
||||
/// An extension for the test client to initialize a Polkadot specific block builder.
|
||||
pub trait InitPolkadotBlockBuilder {
|
||||
/// Init a Polkadot specific block builder that works for the test runtime.
|
||||
///
|
||||
/// This will automatically create and push the inherents for you to make the block valid for the test runtime.
|
||||
fn init_polkadot_block_builder(&self) -> sc_block_builder::BlockBuilder<Block, Client, FullBackend>;
|
||||
fn init_polkadot_block_builder(
|
||||
&self,
|
||||
) -> sc_block_builder::BlockBuilder<Block, Client, FullBackend>;
|
||||
|
||||
/// Init a Polkadot specific block builder at a specific block that works for the test runtime.
|
||||
///
|
||||
@@ -42,9 +47,7 @@ pub trait InitPolkadotBlockBuilder {
|
||||
}
|
||||
|
||||
impl InitPolkadotBlockBuilder for Client {
|
||||
fn init_polkadot_block_builder(
|
||||
&self,
|
||||
) -> BlockBuilder<Block, Client, FullBackend> {
|
||||
fn init_polkadot_block_builder(&self) -> BlockBuilder<Block, Client, FullBackend> {
|
||||
let chain_info = self.chain_info();
|
||||
self.init_polkadot_block_builder_at(&BlockId::Hash(chain_info.best_hash))
|
||||
}
|
||||
@@ -53,17 +56,16 @@ impl InitPolkadotBlockBuilder for Client {
|
||||
&self,
|
||||
at: &BlockId<Block>,
|
||||
) -> BlockBuilder<Block, Client, FullBackend> {
|
||||
let last_timestamp = self
|
||||
.runtime_api()
|
||||
.get_last_timestamp(&at)
|
||||
.expect("Get last timestamp");
|
||||
let last_timestamp =
|
||||
self.runtime_api().get_last_timestamp(&at).expect("Get last timestamp");
|
||||
|
||||
// `MinimumPeriod` is a storage parameter type that requires externalities to access the value.
|
||||
let minimum_period = BasicExternalities::new_empty()
|
||||
.execute_with(|| polkadot_test_runtime::MinimumPeriod::get());
|
||||
|
||||
let timestamp = if last_timestamp == 0 {
|
||||
std::time::SystemTime::now().duration_since(std::time::SystemTime::UNIX_EPOCH)
|
||||
std::time::SystemTime::now()
|
||||
.duration_since(std::time::SystemTime::UNIX_EPOCH)
|
||||
.expect("Time is always after UNIX_EPOCH; qed")
|
||||
.as_millis() as u64
|
||||
} else {
|
||||
@@ -77,18 +79,15 @@ impl InitPolkadotBlockBuilder for Client {
|
||||
let slot = (timestamp / slot_duration).into();
|
||||
|
||||
let digest = Digest {
|
||||
logs: vec![
|
||||
DigestItem::PreRuntime(
|
||||
BABE_ENGINE_ID,
|
||||
PreDigest::SecondaryPlain(SecondaryPlainPreDigest {
|
||||
slot,
|
||||
authority_index: 42,
|
||||
}).encode()
|
||||
),
|
||||
],
|
||||
logs: vec![DigestItem::PreRuntime(
|
||||
BABE_ENGINE_ID,
|
||||
PreDigest::SecondaryPlain(SecondaryPlainPreDigest { slot, authority_index: 42 })
|
||||
.encode(),
|
||||
)],
|
||||
};
|
||||
|
||||
let mut block_builder = self.new_block_at(at, digest, false)
|
||||
let mut block_builder = self
|
||||
.new_block_at(at, digest, false)
|
||||
.expect("Creates new block builder for test runtime");
|
||||
|
||||
let mut inherent_data = sp_inherents::InherentData::new();
|
||||
@@ -97,7 +96,8 @@ impl InitPolkadotBlockBuilder for Client {
|
||||
.put_data(sp_timestamp::INHERENT_IDENTIFIER, ×tamp)
|
||||
.expect("Put timestamp inherent data");
|
||||
|
||||
let parent_header = self.header(at)
|
||||
let parent_header = self
|
||||
.header(at)
|
||||
.expect("Get the parent block header")
|
||||
.expect("The target block header must exist");
|
||||
|
||||
@@ -105,7 +105,7 @@ impl InitPolkadotBlockBuilder for Client {
|
||||
bitfields: Vec::new(),
|
||||
backed_candidates: Vec::new(),
|
||||
disputes: Vec::new(),
|
||||
parent_header: parent_header,
|
||||
parent_header,
|
||||
};
|
||||
|
||||
inherent_data
|
||||
@@ -117,7 +117,9 @@ impl InitPolkadotBlockBuilder for Client {
|
||||
|
||||
let inherents = block_builder.create_inherents(inherent_data).expect("Creates inherents");
|
||||
|
||||
inherents.into_iter().for_each(|ext| block_builder.push(ext).expect("Pushes inherent"));
|
||||
inherents
|
||||
.into_iter()
|
||||
.for_each(|ext| block_builder.push(ext).expect("Pushes inherent"));
|
||||
|
||||
block_builder
|
||||
}
|
||||
@@ -132,15 +134,22 @@ pub trait BlockBuilderExt {
|
||||
/// the block.
|
||||
///
|
||||
/// Returns the result of the application of the extrinsic.
|
||||
fn push_polkadot_extrinsic(&mut self, ext: UncheckedExtrinsic) -> Result<(), sp_blockchain::Error>;
|
||||
fn push_polkadot_extrinsic(
|
||||
&mut self,
|
||||
ext: UncheckedExtrinsic,
|
||||
) -> Result<(), sp_blockchain::Error>;
|
||||
}
|
||||
|
||||
impl BlockBuilderExt for BlockBuilder<'_, Block, Client, FullBackend> {
|
||||
fn push_polkadot_extrinsic(&mut self, ext: UncheckedExtrinsic) -> Result<(), sp_blockchain::Error> {
|
||||
fn push_polkadot_extrinsic(
|
||||
&mut self,
|
||||
ext: UncheckedExtrinsic,
|
||||
) -> Result<(), sp_blockchain::Error> {
|
||||
let encoded = ext.encode();
|
||||
self.push(
|
||||
Decode::decode(&mut &encoded[..])
|
||||
.expect("The runtime specific extrinsic always decodes to an opaque extrinsic; qed"),
|
||||
Decode::decode(&mut &encoded[..]).expect(
|
||||
"The runtime specific extrinsic always decodes to an opaque extrinsic; qed",
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,17 +26,22 @@ use sp_core::storage::Storage;
|
||||
use sp_runtime::BuildStorage;
|
||||
|
||||
pub use block_builder::*;
|
||||
pub use substrate_test_client::*;
|
||||
pub use polkadot_test_service::{
|
||||
Client, construct_extrinsic, construct_transfer_extrinsic, PolkadotTestExecutor, FullBackend,
|
||||
};
|
||||
pub use polkadot_test_runtime as runtime;
|
||||
pub use polkadot_test_service::{
|
||||
construct_extrinsic, construct_transfer_extrinsic, Client, FullBackend, PolkadotTestExecutor,
|
||||
};
|
||||
pub use substrate_test_client::*;
|
||||
|
||||
/// Test client executor.
|
||||
pub type Executor = client::LocalCallExecutor<Block, FullBackend, sc_executor::NativeExecutor<PolkadotTestExecutor>>;
|
||||
pub type Executor = client::LocalCallExecutor<
|
||||
Block,
|
||||
FullBackend,
|
||||
sc_executor::NativeExecutor<PolkadotTestExecutor>,
|
||||
>;
|
||||
|
||||
/// Test client builder for Polkadot.
|
||||
pub type TestClientBuilder = substrate_test_client::TestClientBuilder<Block, Executor, FullBackend, GenesisParameters>;
|
||||
pub type TestClientBuilder =
|
||||
substrate_test_client::TestClientBuilder<Block, Executor, FullBackend, GenesisParameters>;
|
||||
|
||||
/// `LongestChain` type for the test runtime/client.
|
||||
pub type LongestChain = sc_consensus::LongestChain<FullBackend, Block>;
|
||||
@@ -83,7 +88,7 @@ impl DefaultTestClientBuilderExt for TestClientBuilder {
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests{
|
||||
mod tests {
|
||||
use super::*;
|
||||
use sp_consensus::BlockOrigin;
|
||||
|
||||
@@ -94,7 +99,8 @@ mod tests{
|
||||
let block_builder = client.init_polkadot_block_builder();
|
||||
let block = block_builder.build().expect("Finalizes the block").block;
|
||||
|
||||
futures::executor::block_on(client.import(BlockOrigin::Own, block)).expect("Imports the block");
|
||||
futures::executor::block_on(client.import(BlockOrigin::Own, block))
|
||||
.expect("Imports the block");
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -112,6 +118,7 @@ mod tests{
|
||||
|
||||
let block = block_builder.build().expect("Finalizes the block").block;
|
||||
|
||||
futures::executor::block_on(client.import(BlockOrigin::Own, block)).expect("Imports the block");
|
||||
futures::executor::block_on(client.import(BlockOrigin::Own, block))
|
||||
.expect("Imports the block");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user