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

Commit fdb6f1c

Browse files
author
command-bot
committed
2 parents be8e5b5 + 59ca8df commit fdb6f1c

File tree

23 files changed

+1163
-564
lines changed

23 files changed

+1163
-564
lines changed

Cargo.lock

Lines changed: 24 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ members = [
4141
"client/finality-grandpa",
4242
"client/informant",
4343
"client/keystore",
44+
"client/merkle-mountain-range",
4445
"client/network",
4546
"client/network-gossip",
4647
"client/network/bitswap",

bin/node/runtime/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2066,6 +2066,10 @@ impl_runtime_apis! {
20662066
Ok(Mmr::mmr_root())
20672067
}
20682068

2069+
fn mmr_leaf_count() -> Result<mmr::LeafIndex, mmr::Error> {
2070+
Ok(Mmr::mmr_leaves())
2071+
}
2072+
20692073
fn generate_proof(
20702074
block_numbers: Vec<BlockNumber>,
20712075
best_known_block_number: Option<BlockNumber>,

client/consensus/babe/src/lib.rs

Lines changed: 20 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ use sp_api::{ApiExt, ProvideRuntimeApi};
111111
use sp_application_crypto::AppKey;
112112
use sp_block_builder::BlockBuilder as BlockBuilderApi;
113113
use sp_blockchain::{
114-
Backend as _, Error as ClientError, HeaderBackend, HeaderMetadata, Result as ClientResult,
114+
Backend as _, Error as ClientError, ForkBackend, HeaderBackend, HeaderMetadata,
115+
Result as ClientResult,
115116
};
116117
use sp_consensus::{
117118
BlockOrigin, CacheKeyId, Environment, Error as ConsensusError, Proposer, SelectChain,
@@ -123,7 +124,7 @@ use sp_inherents::{CreateInherentDataProviders, InherentData, InherentDataProvid
123124
use sp_keystore::{SyncCryptoStore, SyncCryptoStorePtr};
124125
use sp_runtime::{
125126
generic::{BlockId, OpaqueDigestItemId},
126-
traits::{Block as BlockT, Header, NumberFor, SaturatedConversion, Saturating, Zero},
127+
traits::{Block as BlockT, Header, NumberFor, SaturatedConversion, Zero},
127128
DigestItem,
128129
};
129130

@@ -515,12 +516,12 @@ fn aux_storage_cleanup<C: HeaderMetadata<Block> + HeaderBackend<Block>, Block: B
515516
client: &C,
516517
notification: &FinalityNotification<Block>,
517518
) -> AuxDataOperations {
518-
let mut aux_keys = HashSet::new();
519+
let mut hashes = HashSet::new();
519520

520521
let first = notification.tree_route.first().unwrap_or(&notification.hash);
521522
match client.header_metadata(*first) {
522523
Ok(meta) => {
523-
aux_keys.insert(aux_schema::block_weight_key(meta.parent));
524+
hashes.insert(meta.parent);
524525
},
525526
Err(err) => warn!(
526527
target: "babe",
@@ -531,53 +532,29 @@ fn aux_storage_cleanup<C: HeaderMetadata<Block> + HeaderBackend<Block>, Block: B
531532
}
532533

533534
// Cleans data for finalized block's ancestors
534-
aux_keys.extend(
535+
hashes.extend(
535536
notification
536537
.tree_route
537538
.iter()
538539
// Ensure we don't prune latest finalized block.
539540
// This should not happen, but better be safe than sorry!
540-
.filter(|h| **h != notification.hash)
541-
.map(aux_schema::block_weight_key),
541+
.filter(|h| **h != notification.hash),
542542
);
543543

544-
// Cleans data for stale branches.
545-
546-
for head in notification.stale_heads.iter() {
547-
let mut hash = *head;
548-
// Insert stale blocks hashes until canonical chain is reached.
549-
// If we reach a block that is already part of the `aux_keys` we can stop the processing the
550-
// head.
551-
while aux_keys.insert(aux_schema::block_weight_key(hash)) {
552-
match client.header_metadata(hash) {
553-
Ok(meta) => {
554-
hash = meta.parent;
555-
556-
// If the parent is part of the canonical chain or there doesn't exist a block
557-
// hash for the parent number (bug?!), we can abort adding blocks.
558-
if client
559-
.hash(meta.number.saturating_sub(1u32.into()))
560-
.ok()
561-
.flatten()
562-
.map_or(true, |h| h == hash)
563-
{
564-
break
565-
}
566-
},
567-
Err(err) => {
568-
warn!(
569-
target: "babe",
570-
"Header lookup fail while cleaning data for block {:?}: {}",
571-
hash,
572-
err,
573-
);
574-
break
575-
},
576-
}
577-
}
578-
}
544+
// Cleans data for stale forks.
545+
let stale_forks = match client.expand_forks(&notification.stale_heads) {
546+
Ok(stale_forks) => stale_forks,
547+
Err((stale_forks, e)) => {
548+
warn!(target: "babe", "{:?}", e,);
549+
stale_forks
550+
},
551+
};
552+
hashes.extend(stale_forks.iter());
579553

580-
aux_keys.into_iter().map(|val| (val, None)).collect()
554+
hashes
555+
.into_iter()
556+
.map(|val| (aux_schema::block_weight_key(val), None))
557+
.collect()
581558
}
582559

583560
async fn answer_requests<B: BlockT, C>(
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
[package]
2+
name = "mmr-gadget"
3+
version = "4.0.0-dev"
4+
authors = ["Parity Technologies <[email protected]>"]
5+
edition = "2021"
6+
license = "GPL-3.0-or-later WITH Classpath-exception-2.0"
7+
repository = "https://github.com/paritytech/substrate"
8+
description = "MMR Client gadget for substrate"
9+
homepage = "https://substrate.io"
10+
11+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
12+
13+
[dependencies]
14+
codec = { package = "parity-scale-codec", version = "3.0.0" }
15+
futures = "0.3"
16+
log = "0.4"
17+
beefy-primitives = { version = "4.0.0-dev", path = "../../primitives/beefy" }
18+
sc-client-api = { version = "4.0.0-dev", path = "../api" }
19+
sp-api = { version = "4.0.0-dev", path = "../../primitives/api" }
20+
sp-blockchain = { version = "4.0.0-dev", path = "../../primitives/blockchain" }
21+
sp-consensus = { version = "0.10.0-dev", path = "../../primitives/consensus/common" }
22+
sp-core = { version = "7.0.0", path = "../../primitives/core" }
23+
sp-io = { version = "7.0.0", path = "../../primitives/io" }
24+
sp-mmr-primitives = { version = "4.0.0-dev", path = "../../primitives/merkle-mountain-range" }
25+
sc-offchain = { version = "4.0.0-dev", path = "../offchain" }
26+
sp-runtime = { version = "7.0.0", path = "../../primitives/runtime" }
27+
28+
[dev-dependencies]
29+
tokio = "1.17.0"
30+
sc-block-builder = { version = "0.10.0-dev", path = "../block-builder" }
31+
substrate-test-runtime-client = { version = "2.0.0", path = "../../test-utils/runtime/client" }
32+
async-std = { version = "1.11.0", default-features = false }

0 commit comments

Comments
 (0)