Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit 5fc8e30

Browse files
author
parity-processbot
committed
Merge remote-tracking branch 'origin/master' into mku-genesis-build-for-runtime-genesis-config
2 parents 477d7ad + e434882 commit 5fc8e30

File tree

49 files changed

+2478
-585
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+2478
-585
lines changed

Cargo.lock

Lines changed: 525 additions & 341 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,10 @@ members = [
106106
"frame/election-provider-support/benchmarking",
107107
"frame/election-provider-support/solution-type",
108108
"frame/election-provider-support/solution-type/fuzzer",
109+
"frame/examples",
109110
"frame/examples/basic",
110111
"frame/examples/offchain-worker",
112+
"frame/examples/kitchensink",
111113
"frame/examples/dev-mode",
112114
"frame/examples/default-config",
113115
"frame/executive",
@@ -199,6 +201,7 @@ members = [
199201
"primitives/core",
200202
"primitives/core/hashing",
201203
"primitives/core/hashing/proc-macro",
204+
"primitives/crypto/ec-utils",
202205
"primitives/database",
203206
"primitives/debug-derive",
204207
"primitives/externalities",

bin/node/inspect/src/command.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,12 @@ use crate::{
2525
use sc_cli::{CliConfiguration, ImportParams, Result, SharedParams};
2626
use sc_service::{Configuration, NativeExecutionDispatch};
2727
use sp_runtime::traits::Block;
28-
use std::str::FromStr;
2928

3029
impl InspectCmd {
3130
/// Run the inspect command, passing the inspector.
3231
pub fn run<B, RA, D>(&self, config: Configuration) -> Result<()>
3332
where
3433
B: Block,
35-
B::Hash: FromStr,
3634
RA: Send + Sync + 'static,
3735
D: NativeExecutionDispatch + 'static,
3836
{

client/api/src/in_mem.rs

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -487,10 +487,7 @@ pub struct BlockImportOperation<Block: BlockT> {
487487
set_head: Option<Block::Hash>,
488488
}
489489

490-
impl<Block: BlockT> BlockImportOperation<Block>
491-
where
492-
Block::Hash: Ord,
493-
{
490+
impl<Block: BlockT> BlockImportOperation<Block> {
494491
fn apply_storage(
495492
&mut self,
496493
storage: Storage,
@@ -519,10 +516,7 @@ where
519516
}
520517
}
521518

522-
impl<Block: BlockT> backend::BlockImportOperation<Block> for BlockImportOperation<Block>
523-
where
524-
Block::Hash: Ord,
525-
{
519+
impl<Block: BlockT> backend::BlockImportOperation<Block> for BlockImportOperation<Block> {
526520
type State = InMemoryBackend<HashFor<Block>>;
527521

528522
fn state(&self) -> sp_blockchain::Result<Option<&Self::State>> {
@@ -611,20 +605,14 @@ where
611605
///
612606
/// > **Warning**: Doesn't support all the features necessary for a proper database. Only use this
613607
/// > struct for testing purposes. Do **NOT** use in production.
614-
pub struct Backend<Block: BlockT>
615-
where
616-
Block::Hash: Ord,
617-
{
608+
pub struct Backend<Block: BlockT> {
618609
states: RwLock<HashMap<Block::Hash, InMemoryBackend<HashFor<Block>>>>,
619610
blockchain: Blockchain<Block>,
620611
import_lock: RwLock<()>,
621612
pinned_blocks: RwLock<HashMap<Block::Hash, i64>>,
622613
}
623614

624-
impl<Block: BlockT> Backend<Block>
625-
where
626-
Block::Hash: Ord,
627-
{
615+
impl<Block: BlockT> Backend<Block> {
628616
/// Create a new instance of in-mem backend.
629617
///
630618
/// # Warning
@@ -650,10 +638,7 @@ where
650638
}
651639
}
652640

653-
impl<Block: BlockT> backend::AuxStore for Backend<Block>
654-
where
655-
Block::Hash: Ord,
656-
{
641+
impl<Block: BlockT> backend::AuxStore for Backend<Block> {
657642
fn insert_aux<
658643
'a,
659644
'b: 'a,
@@ -673,10 +658,7 @@ where
673658
}
674659
}
675660

676-
impl<Block: BlockT> backend::Backend<Block> for Backend<Block>
677-
where
678-
Block::Hash: Ord,
679-
{
661+
impl<Block: BlockT> backend::Backend<Block> for Backend<Block> {
680662
type BlockImportOperation = BlockImportOperation<Block>;
681663
type Blockchain = Blockchain<Block>;
682664
type State = InMemoryBackend<HashFor<Block>>;
@@ -809,7 +791,7 @@ where
809791
}
810792
}
811793

812-
impl<Block: BlockT> backend::LocalBackend<Block> for Backend<Block> where Block::Hash: Ord {}
794+
impl<Block: BlockT> backend::LocalBackend<Block> for Backend<Block> {}
813795

814796
/// Check that genesis storage is valid.
815797
pub fn check_genesis_storage(storage: &Storage) -> sp_blockchain::Result<()> {

client/cli/src/commands/check_block_cmd.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ impl CheckBlockCmd {
5454
B: BlockT + for<'de> serde::Deserialize<'de>,
5555
C: BlockBackend<B> + HeaderBackend<B> + Send + Sync + 'static,
5656
IQ: sc_service::ImportQueue<B> + 'static,
57-
B::Hash: FromStr,
5857
<B::Hash as FromStr>::Err: Debug,
5958
<<B::Header as HeaderT>::Number as FromStr>::Err: Debug,
6059
{

client/cli/src/commands/export_state_cmd.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ impl ExportStateCmd {
5959
B: BlockT,
6060
C: UsageProvider<B> + StorageProvider<B, BA> + HeaderBackend<B>,
6161
BA: sc_client_api::backend::Backend<B>,
62-
B::Hash: FromStr,
6362
<B::Hash as FromStr>::Err: Debug,
6463
<<B::Header as HeaderT>::Number as FromStr>::Err: Debug,
6564
{

client/cli/src/params/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@ impl BlockNumberOrHash {
113113
/// Parse the inner value as `BlockId`.
114114
pub fn parse<B: BlockT>(&self) -> Result<BlockId<B>, String>
115115
where
116-
B::Hash: FromStr,
117116
<B::Hash as FromStr>::Err: std::fmt::Debug,
118117
NumberFor<B>: FromStr,
119118
<NumberFor<B> as FromStr>::Err: std::fmt::Debug,

client/consensus/grandpa/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,6 @@ pub fn run_grandpa_voter<Block: BlockT, BE: 'static, C, N, S, SC, VR>(
718718
grandpa_params: GrandpaParams<Block, C, N, S, SC, VR>,
719719
) -> sp_blockchain::Result<impl Future<Output = ()> + Send>
720720
where
721-
Block::Hash: Ord,
722721
BE: Backend<Block> + 'static,
723722
N: NetworkT<Block> + Sync + 'static,
724723
S: SyncingT<Block> + Sync + 'static,

client/db/src/bench.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -423,10 +423,7 @@ impl<B: BlockT> StateBackend<HashFor<B>> for BenchmarkingState<B> {
423423
&self,
424424
delta: impl Iterator<Item = (&'a [u8], Option<&'a [u8]>)>,
425425
state_version: StateVersion,
426-
) -> (B::Hash, Self::Transaction)
427-
where
428-
B::Hash: Ord,
429-
{
426+
) -> (B::Hash, Self::Transaction) {
430427
self.state
431428
.borrow()
432429
.as_ref()
@@ -438,10 +435,7 @@ impl<B: BlockT> StateBackend<HashFor<B>> for BenchmarkingState<B> {
438435
child_info: &ChildInfo,
439436
delta: impl Iterator<Item = (&'a [u8], Option<&'a [u8]>)>,
440437
state_version: StateVersion,
441-
) -> (B::Hash, bool, Self::Transaction)
442-
where
443-
B::Hash: Ord,
444-
{
438+
) -> (B::Hash, bool, Self::Transaction) {
445439
self.state
446440
.borrow()
447441
.as_ref()

client/db/src/lib.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -243,10 +243,7 @@ impl<B: BlockT> StateBackend<HashFor<B>> for RefTrackingState<B> {
243243
&self,
244244
delta: impl Iterator<Item = (&'a [u8], Option<&'a [u8]>)>,
245245
state_version: StateVersion,
246-
) -> (B::Hash, Self::Transaction)
247-
where
248-
B::Hash: Ord,
249-
{
246+
) -> (B::Hash, Self::Transaction) {
250247
self.state.storage_root(delta, state_version)
251248
}
252249

@@ -255,10 +252,7 @@ impl<B: BlockT> StateBackend<HashFor<B>> for RefTrackingState<B> {
255252
child_info: &ChildInfo,
256253
delta: impl Iterator<Item = (&'a [u8], Option<&'a [u8]>)>,
257254
state_version: StateVersion,
258-
) -> (B::Hash, bool, Self::Transaction)
259-
where
260-
B::Hash: Ord,
261-
{
255+
) -> (B::Hash, bool, Self::Transaction) {
262256
self.state.child_storage_root(child_info, delta, state_version)
263257
}
264258

client/db/src/record_stats_state.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,7 @@ impl<S: StateBackend<HashFor<B>>, B: BlockT> StateBackend<HashFor<B>> for Record
171171
&self,
172172
delta: impl Iterator<Item = (&'a [u8], Option<&'a [u8]>)>,
173173
state_version: StateVersion,
174-
) -> (B::Hash, Self::Transaction)
175-
where
176-
B::Hash: Ord,
177-
{
174+
) -> (B::Hash, Self::Transaction) {
178175
self.state.storage_root(delta, state_version)
179176
}
180177

@@ -183,10 +180,7 @@ impl<S: StateBackend<HashFor<B>>, B: BlockT> StateBackend<HashFor<B>> for Record
183180
child_info: &ChildInfo,
184181
delta: impl Iterator<Item = (&'a [u8], Option<&'a [u8]>)>,
185182
state_version: StateVersion,
186-
) -> (B::Hash, bool, Self::Transaction)
187-
where
188-
B::Hash: Ord,
189-
{
183+
) -> (B::Hash, bool, Self::Transaction) {
190184
self.state.child_storage_root(child_info, delta, state_version)
191185
}
192186

client/service/src/client/client.rs

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ use sp_api::{
5252
};
5353
use sp_blockchain::{
5454
self as blockchain, Backend as ChainBackend, CachedHeaderMetadata, Error,
55-
HeaderBackend as ChainHeaderBackend, HeaderMetadata,
55+
HeaderBackend as ChainHeaderBackend, HeaderMetadata, Info as BlockchainInfo,
5656
};
5757
use sp_consensus::{BlockOrigin, BlockStatus, Error as ConsensusError};
5858

@@ -717,7 +717,7 @@ where
717717
operation,
718718
parent_hash,
719719
None,
720-
info.best_hash,
720+
&info,
721721
make_notifications,
722722
)?;
723723
}
@@ -907,52 +907,56 @@ where
907907
fn apply_finality_with_block_hash(
908908
&self,
909909
operation: &mut ClientImportOperation<Block, B>,
910-
block: Block::Hash,
910+
hash: Block::Hash,
911911
justification: Option<Justification>,
912-
best_block: Block::Hash,
912+
info: &BlockchainInfo<Block>,
913913
notify: bool,
914914
) -> sp_blockchain::Result<()> {
915-
// find tree route from last finalized to given block.
916-
let last_finalized = self.backend.blockchain().last_finalized()?;
917-
918-
if block == last_finalized {
915+
if hash == info.finalized_hash {
919916
warn!(
920917
"Possible safety violation: attempted to re-finalize last finalized block {:?} ",
921-
last_finalized
918+
hash,
922919
);
923920
return Ok(())
924921
}
925922

923+
// Find tree route from last finalized to given block.
926924
let route_from_finalized =
927-
sp_blockchain::tree_route(self.backend.blockchain(), last_finalized, block)?;
925+
sp_blockchain::tree_route(self.backend.blockchain(), info.finalized_hash, hash)?;
928926

929927
if let Some(retracted) = route_from_finalized.retracted().get(0) {
930928
warn!(
931929
"Safety violation: attempted to revert finalized block {:?} which is not in the \
932930
same chain as last finalized {:?}",
933-
retracted, last_finalized
931+
retracted, info.finalized_hash
934932
);
935933

936934
return Err(sp_blockchain::Error::NotInFinalizedChain)
937935
}
938936

939-
// If there is only one leaf, best block is guaranteed to be
940-
// a descendant of the new finalized block. If not,
941-
// we need to check.
942-
if self.backend.blockchain().leaves()?.len() > 1 {
937+
// We may need to coercively update the best block if there is more than one
938+
// leaf or if the finalized block number is greater than last best number recorded
939+
// by the backend. This last condition may apply in case of consensus implementations
940+
// not always checking this condition.
941+
let block_number = self
942+
.backend
943+
.blockchain()
944+
.number(hash)?
945+
.ok_or(Error::MissingHeader(format!("{hash:?}")))?;
946+
if self.backend.blockchain().leaves()?.len() > 1 || info.best_number < block_number {
943947
let route_from_best =
944-
sp_blockchain::tree_route(self.backend.blockchain(), best_block, block)?;
948+
sp_blockchain::tree_route(self.backend.blockchain(), info.best_hash, hash)?;
945949

946-
// if the block is not a direct ancestor of the current best chain,
950+
// If the block is not a direct ancestor of the current best chain,
947951
// then some other block is the common ancestor.
948-
if route_from_best.common_block().hash != block {
952+
if route_from_best.common_block().hash != hash {
949953
// NOTE: we're setting the finalized block as best block, this might
950954
// be slightly inaccurate since we might have a "better" block
951955
// further along this chain, but since best chain selection logic is
952956
// plugable we cannot make a better choice here. usages that need
953957
// an accurate "best" block need to go through `SelectChain`
954958
// instead.
955-
operation.op.mark_head(block)?;
959+
operation.op.mark_head(hash)?;
956960
}
957961
}
958962

@@ -962,8 +966,8 @@ where
962966
operation.op.mark_finalized(finalize_new.hash, None)?;
963967
}
964968

965-
assert_eq!(enacted.last().map(|e| e.hash), Some(block));
966-
operation.op.mark_finalized(block, justification)?;
969+
assert_eq!(enacted.last().map(|e| e.hash), Some(hash));
970+
operation.op.mark_finalized(hash, justification)?;
967971

968972
if notify {
969973
let finalized =
@@ -985,7 +989,7 @@ where
985989
let header = self
986990
.backend
987991
.blockchain()
988-
.header(block)?
992+
.header(hash)?
989993
.expect("Block to finalize expected to be onchain; qed");
990994

991995
operation.notify_finalized = Some(FinalizeSummary { header, finalized, stale_heads });
@@ -1129,7 +1133,7 @@ where
11291133
}
11301134

11311135
/// Get blockchain info.
1132-
pub fn chain_info(&self) -> blockchain::Info<Block> {
1136+
pub fn chain_info(&self) -> BlockchainInfo<Block> {
11331137
self.backend.blockchain().info()
11341138
}
11351139

@@ -1896,8 +1900,8 @@ where
18961900
justification: Option<Justification>,
18971901
notify: bool,
18981902
) -> sp_blockchain::Result<()> {
1899-
let last_best = self.backend.blockchain().info().best_hash;
1900-
self.apply_finality_with_block_hash(operation, hash, justification, last_best, notify)
1903+
let info = self.backend.blockchain().info();
1904+
self.apply_finality_with_block_hash(operation, hash, justification, &info, notify)
19011905
}
19021906

19031907
fn finalize_block(

client/service/test/src/client/mod.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2001,3 +2001,41 @@ fn use_dalek_ext_works() {
20012001
.verify_ed25519(a1.hash(), zero_ed_sig(), zero_ed_pub(), vec![])
20022002
.unwrap());
20032003
}
2004+
2005+
#[test]
2006+
fn finalize_after_best_block_updates_best() {
2007+
let mut client = substrate_test_runtime_client::new();
2008+
2009+
// G -> A1
2010+
let a1 = client.new_block(Default::default()).unwrap().build().unwrap().block;
2011+
block_on(client.import(BlockOrigin::Own, a1.clone())).unwrap();
2012+
2013+
// A1 -> A2
2014+
let a2 = client
2015+
.new_block_at(a1.hash(), Default::default(), false)
2016+
.unwrap()
2017+
.build()
2018+
.unwrap()
2019+
.block;
2020+
block_on(client.import(BlockOrigin::Own, a2.clone())).unwrap();
2021+
2022+
// A2 -> A3
2023+
let a3 = client
2024+
.new_block_at(a2.hash(), Default::default(), false)
2025+
.unwrap()
2026+
.build()
2027+
.unwrap()
2028+
.block;
2029+
let (header, extrinsics) = a3.clone().deconstruct();
2030+
let mut import_params = BlockImportParams::new(BlockOrigin::Own, header);
2031+
import_params.body = Some(extrinsics);
2032+
import_params.fork_choice = Some(ForkChoiceStrategy::Custom(false));
2033+
block_on(client.import_block(import_params)).unwrap();
2034+
2035+
assert_eq!(client.chain_info().best_hash, a2.hash());
2036+
2037+
client.finalize_block(a3.hash(), None).unwrap();
2038+
2039+
assert_eq!(client.chain_info().finalized_hash, a3.hash());
2040+
assert_eq!(client.chain_info().best_hash, a3.hash());
2041+
}

0 commit comments

Comments
 (0)