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

Commit 9b61628

Browse files
committed
Merge branch 'master' of github.com:paritytech/substrate into a-tx-storage2
2 parents f8d89f2 + a1eaece commit 9b61628

File tree

55 files changed

+1934
-472
lines changed

Some content is hidden

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

55 files changed

+1934
-472
lines changed

.maintain/monitoring/alerting-rules/alerting-rule-tests.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ tests:
175175
polkadot-abcdef01234-abcdef has been monotonically
176176
increasing for more than 10 minutes."
177177
- exp_labels:
178-
severity: critical
178+
severity: warning
179179
pod: polkadot-abcdef01234-abcdef
180180
instance: polkadot-abcdef01234-abcdef
181181
job: polkadot
@@ -190,7 +190,7 @@ tests:
190190
# same. Thus expect an alert.
191191
exp_alerts:
192192
- exp_labels:
193-
severity: critical
193+
severity: warning
194194
pod: polkadot-abcdef01234-abcdef
195195
instance: polkadot-abcdef01234-abcdef
196196
job: polkadot

.maintain/monitoring/alerting-rules/alerting-rules.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ groups:
7474
increase(polkadot_sub_txpool_validations_finished[5m]) > 0'
7575
for: 30m
7676
labels:
77-
severity: critical
77+
severity: warning
7878
annotations:
7979
message: 'The transaction pool size on node {{ $labels.instance }} has
8080
been monotonically increasing for more than 30 minutes.'
@@ -83,7 +83,7 @@ groups:
8383
polkadot_sub_txpool_validations_finished > 10000'
8484
for: 5m
8585
labels:
86-
severity: critical
86+
severity: warning
8787
annotations:
8888
message: 'The transaction pool size on node {{ $labels.instance }} has
8989
been above 10_000 for more than 5 minutes.'

client/api/src/backend.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,12 @@ pub trait Backend<Block: BlockT>: AuxStore + Send + Sync {
475475
revert_finalized: bool,
476476
) -> sp_blockchain::Result<(NumberFor<Block>, HashSet<Block::Hash>)>;
477477

478+
/// Discard non-best, unfinalized leaf block.
479+
fn remove_leaf_block(
480+
&self,
481+
hash: &Block::Hash,
482+
) -> sp_blockchain::Result<()>;
483+
478484
/// Insert auxiliary data into key-value store.
479485
fn insert_aux<
480486
'a,

client/api/src/in_mem.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,13 @@ impl<Block: BlockT> backend::Backend<Block> for Backend<Block> where Block::Hash
777777
Ok((Zero::zero(), HashSet::new()))
778778
}
779779

780+
fn remove_leaf_block(
781+
&self,
782+
_hash: &Block::Hash,
783+
) -> sp_blockchain::Result<()> {
784+
Ok(())
785+
}
786+
780787
fn get_import_lock(&self) -> &RwLock<()> {
781788
&self.import_lock
782789
}

client/api/src/leaves.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,8 @@ impl<H, N> LeafSet<H, N> where
216216
self.pending_removed.clear();
217217
}
218218

219-
#[cfg(test)]
220-
fn contains(&self, number: N, hash: H) -> bool {
219+
/// Check if given block is a leaf.
220+
pub fn contains(&self, number: N, hash: H) -> bool {
221221
self.storage.get(&Reverse(number)).map_or(false, |hashes| hashes.contains(&hash))
222222
}
223223

client/consensus/slots/src/slots.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,12 @@ pub fn duration_now() -> Duration {
4040
}
4141

4242
/// Returns the duration until the next slot from now.
43-
pub fn time_until_next(slot_duration: Duration) -> Duration {
44-
let remaining_full_millis = slot_duration.as_millis()
45-
- (duration_now().as_millis() % slot_duration.as_millis())
46-
- 1;
47-
Duration::from_millis(remaining_full_millis as u64)
43+
pub fn time_until_next_slot(slot_duration: Duration) -> Duration {
44+
let now = duration_now().as_millis();
45+
46+
let next_slot = (now + slot_duration.as_millis()) / slot_duration.as_millis();
47+
let remaining_millis = next_slot * slot_duration.as_millis() - now;
48+
Duration::from_millis(remaining_millis as u64)
4849
}
4950

5051
/// Information about a slot.
@@ -86,7 +87,7 @@ impl<B: BlockT> SlotInfo<B> {
8687
duration,
8788
chain_head,
8889
block_size_limit,
89-
ends_at: Instant::now() + time_until_next(duration),
90+
ends_at: Instant::now() + time_until_next_slot(duration),
9091
}
9192
}
9293
}
@@ -132,7 +133,7 @@ where
132133
self.inner_delay = match self.inner_delay.take() {
133134
None => {
134135
// schedule wait.
135-
let wait_dur = time_until_next(self.slot_duration);
136+
let wait_dur = time_until_next_slot(self.slot_duration);
136137
Some(Delay::new(wait_dur))
137138
}
138139
Some(d) => Some(d),
@@ -143,7 +144,12 @@ where
143144
}
144145
// timeout has fired.
145146

146-
let ends_at = Instant::now() + time_until_next(self.slot_duration);
147+
let ends_in = time_until_next_slot(self.slot_duration);
148+
149+
// reschedule delay for next slot.
150+
self.inner_delay = Some(Delay::new(ends_in));
151+
152+
let ends_at = Instant::now() + ends_in;
147153

148154
let chain_head = match self.client.best_chain() {
149155
Ok(x) => x,
@@ -174,10 +180,6 @@ where
174180
let slot = inherent_data_providers.slot();
175181
let inherent_data = inherent_data_providers.create_inherent_data()?;
176182

177-
// reschedule delay for next slot.
178-
let ends_in = time_until_next(self.slot_duration);
179-
self.inner_delay = Some(Delay::new(ends_in));
180-
181183
// never yield the same slot twice.
182184
if slot > self.last_slot {
183185
self.last_slot = slot;

client/db/src/lib.rs

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,7 @@ impl<Block: BlockT> HeaderMetadata<Block> for BlockchainDb<Block> {
654654
}
655655

656656
fn remove_header_metadata(&self, hash: Block::Hash) {
657+
self.header_cache.lock().remove(&hash);
657658
self.header_metadata_cache.remove_header_metadata(hash);
658659
}
659660
}
@@ -2002,6 +2003,59 @@ impl<Block: BlockT> sc_client_api::backend::Backend<Block> for Backend<Block> {
20022003
Ok((reverted, reverted_finalized))
20032004
}
20042005

2006+
fn remove_leaf_block(
2007+
&self,
2008+
hash: &Block::Hash,
2009+
) -> ClientResult<()> {
2010+
let best_hash = self.blockchain.info().best_hash;
2011+
2012+
if best_hash == *hash {
2013+
return Err(
2014+
sp_blockchain::Error::Backend(
2015+
format!("Can't remove best block {:?}", hash)
2016+
)
2017+
)
2018+
}
2019+
2020+
let hdr = self.blockchain.header_metadata(hash.clone())?;
2021+
if !self.have_state_at(&hash, hdr.number) {
2022+
return Err(
2023+
sp_blockchain::Error::UnknownBlock(
2024+
format!("State already discarded for {:?}", hash)
2025+
)
2026+
)
2027+
}
2028+
2029+
let mut leaves = self.blockchain.leaves.write();
2030+
if !leaves.contains(hdr.number, *hash) {
2031+
return Err(
2032+
sp_blockchain::Error::Backend(
2033+
format!("Can't remove non-leaf block {:?}", hash)
2034+
)
2035+
)
2036+
}
2037+
2038+
let mut transaction = Transaction::new();
2039+
if let Some(commit) = self.storage.state_db.remove(hash) {
2040+
apply_state_commit(&mut transaction, commit);
2041+
}
2042+
transaction.remove(columns::KEY_LOOKUP, hash.as_ref());
2043+
let changes_trie_cache_ops = self.changes_tries_storage.revert(
2044+
&mut transaction,
2045+
&cache::ComplexBlockId::new(
2046+
*hash,
2047+
hdr.number,
2048+
),
2049+
)?;
2050+
2051+
self.changes_tries_storage.post_commit(Some(changes_trie_cache_ops));
2052+
leaves.revert(hash.clone(), hdr.number);
2053+
leaves.prepare_transaction(&mut transaction, columns::META, meta_keys::LEAF_PREFIX);
2054+
self.storage.db.commit(transaction)?;
2055+
self.blockchain().remove_header_metadata(*hash);
2056+
Ok(())
2057+
}
2058+
20052059
fn blockchain(&self) -> &BlockchainDb<Block> {
20062060
&self.blockchain
20072061
}
@@ -3041,4 +3095,36 @@ pub(crate) mod tests {
30413095
}
30423096
}
30433097
}
3098+
3099+
#[test]
3100+
fn remove_leaf_block_works() {
3101+
let backend = Backend::<Block>::new_test_with_tx_storage(
3102+
2,
3103+
10,
3104+
TransactionStorageMode::StorageChain
3105+
);
3106+
let mut blocks = Vec::new();
3107+
let mut prev_hash = Default::default();
3108+
for i in 0 .. 2 {
3109+
let hash = insert_block(&backend, i, prev_hash, None, Default::default(), vec![i.into()], None);
3110+
blocks.push(hash);
3111+
prev_hash = hash;
3112+
}
3113+
3114+
// insert a fork at block 2, which becomes best block
3115+
let best_hash = insert_block(
3116+
&backend,
3117+
1,
3118+
blocks[0],
3119+
None,
3120+
sp_core::H256::random(),
3121+
vec![42.into()],
3122+
None
3123+
);
3124+
assert!(backend.remove_leaf_block(&best_hash).is_err());
3125+
assert!(backend.have_state_at(&prev_hash, 1));
3126+
backend.remove_leaf_block(&prev_hash).unwrap();
3127+
assert_eq!(None, backend.blockchain().header(BlockId::hash(prev_hash.clone())).unwrap());
3128+
assert!(!backend.have_state_at(&prev_hash, 1));
3129+
}
30443130
}

client/finality-grandpa/src/communication/tests.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ fn good_commit_leads_to_relay() {
295295
let _ = sender.unbounded_send(NetworkEvent::NotificationStreamOpened {
296296
remote: sender_id.clone(),
297297
protocol: GRANDPA_PROTOCOL_NAME.into(),
298+
negotiated_fallback: None,
298299
role: ObservedRole::Full,
299300
});
300301

@@ -308,6 +309,7 @@ fn good_commit_leads_to_relay() {
308309
let _ = sender.unbounded_send(NetworkEvent::NotificationStreamOpened {
309310
remote: receiver_id.clone(),
310311
protocol: GRANDPA_PROTOCOL_NAME.into(),
312+
negotiated_fallback: None,
311313
role: ObservedRole::Full,
312314
});
313315

@@ -442,6 +444,7 @@ fn bad_commit_leads_to_report() {
442444
let _ = sender.unbounded_send(NetworkEvent::NotificationStreamOpened {
443445
remote: sender_id.clone(),
444446
protocol: GRANDPA_PROTOCOL_NAME.into(),
447+
negotiated_fallback: None,
445448
role: ObservedRole::Full,
446449
});
447450
let _ = sender.unbounded_send(NetworkEvent::NotificationsReceived {

client/finality-grandpa/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,7 @@ pub struct GrandpaParams<Block: BlockT, C, N, SC, VR> {
690690
pub fn grandpa_peers_set_config() -> sc_network::config::NonDefaultSetConfig {
691691
sc_network::config::NonDefaultSetConfig {
692692
notifications_protocol: communication::GRANDPA_PROTOCOL_NAME.into(),
693+
fallback_names: Vec::new(),
693694
// Notifications reach ~256kiB in size at the time of writing on Kusama and Polkadot.
694695
max_notification_size: 1024 * 1024,
695696
set_config: sc_network::config::SetConfig {
@@ -1134,12 +1135,12 @@ fn local_authority_id(
11341135
voters: &VoterSet<AuthorityId>,
11351136
keystore: Option<&SyncCryptoStorePtr>,
11361137
) -> Option<AuthorityId> {
1137-
keystore.and_then(|keystore| {
1138+
keystore.and_then(|keystore| {
11381139
voters
11391140
.iter()
11401141
.find(|(p, _)| {
11411142
SyncCryptoStore::has_keys(&**keystore, &[(p.to_raw_vec(), AuthorityId::ID)])
11421143
})
1143-
.map(|(p, _)| p.clone())
1144+
.map(|(p, _)| p.clone())
11441145
})
11451146
}

client/light/src/backend.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,13 @@ impl<S, Block> ClientBackend<Block> for Backend<S, HashFor<Block>>
246246
Err(ClientError::NotAvailableOnLightClient)
247247
}
248248

249+
fn remove_leaf_block(
250+
&self,
251+
_hash: &Block::Hash,
252+
) -> ClientResult<()> {
253+
Err(ClientError::NotAvailableOnLightClient)
254+
}
255+
249256
fn get_import_lock(&self) -> &RwLock<()> {
250257
&self.import_lock
251258
}

client/network-gossip/src/bridge.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ impl<B: BlockT> Future for GossipEngine<B> {
188188
Event::SyncDisconnected { remote } => {
189189
this.network.remove_set_reserved(remote, this.protocol.clone());
190190
}
191-
Event::NotificationStreamOpened { remote, protocol, role } => {
191+
Event::NotificationStreamOpened { remote, protocol, role, .. } => {
192192
if protocol != this.protocol {
193193
continue;
194194
}
@@ -416,6 +416,7 @@ mod tests {
416416
Event::NotificationStreamOpened {
417417
remote: remote_peer.clone(),
418418
protocol: protocol.clone(),
419+
negotiated_fallback: None,
419420
role: ObservedRole::Authority,
420421
}
421422
).expect("Event stream is unbounded; qed.");
@@ -575,6 +576,7 @@ mod tests {
575576
Event::NotificationStreamOpened {
576577
remote: remote_peer.clone(),
577578
protocol: protocol.clone(),
579+
negotiated_fallback: None,
578580
role: ObservedRole::Authority,
579581
}
580582
).expect("Event stream is unbounded; qed.");

client/network/src/behaviour.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ pub enum BehaviourOut<B: BlockT> {
124124
remote: PeerId,
125125
/// The concerned protocol. Each protocol uses a different substream.
126126
protocol: Cow<'static, str>,
127+
/// If the negotiation didn't use the main name of the protocol (the one in
128+
/// `notifications_protocol`), then this field contains which name has actually been
129+
/// used.
130+
/// See also [`crate::Event::NotificationStreamOpened`].
131+
negotiated_fallback: Option<Cow<'static, str>>,
127132
/// Object that permits sending notifications to the peer.
128133
notifications_sink: NotificationsSink,
129134
/// Role of the remote.
@@ -324,10 +329,13 @@ Behaviour<B> {
324329
&target, &self.block_request_protocol_name, buf, pending_response, IfDisconnected::ImmediateError,
325330
);
326331
},
327-
CustomMessageOutcome::NotificationStreamOpened { remote, protocol, roles, notifications_sink } => {
332+
CustomMessageOutcome::NotificationStreamOpened {
333+
remote, protocol, negotiated_fallback, roles, notifications_sink
334+
} => {
328335
self.events.push_back(BehaviourOut::NotificationStreamOpened {
329336
remote,
330337
protocol,
338+
negotiated_fallback,
331339
role: reported_roles_to_observed_role(roles),
332340
notifications_sink: notifications_sink.clone(),
333341
});

client/network/src/config.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,13 @@ pub struct NonDefaultSetConfig {
541541
/// > **Note**: This field isn't present for the default set, as this is handled internally
542542
/// > by the networking code.
543543
pub notifications_protocol: Cow<'static, str>,
544+
/// If the remote reports that it doesn't support the protocol indicated in the
545+
/// `notifications_protocol` field, then each of these fallback names will be tried one by
546+
/// one.
547+
///
548+
/// If a fallback is used, it will be reported in
549+
/// [`crate::Event::NotificationStreamOpened::negotiated_fallback`].
550+
pub fallback_names: Vec<Cow<'static, str>>,
544551
/// Maximum allowed size of single notifications.
545552
pub max_notification_size: u64,
546553
/// Base configuration.
@@ -553,6 +560,7 @@ impl NonDefaultSetConfig {
553560
NonDefaultSetConfig {
554561
notifications_protocol,
555562
max_notification_size,
563+
fallback_names: Vec::new(),
556564
set_config: SetConfig {
557565
in_peers: 0,
558566
out_peers: 0,

client/network/src/gossip/tests.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ fn build_nodes_one_proto()
159159
extra_sets: vec![
160160
config::NonDefaultSetConfig {
161161
notifications_protocol: PROTOCOL_NAME,
162+
fallback_names: Vec::new(),
162163
max_notification_size: 1024 * 1024,
163164
set_config: Default::default()
164165
}
@@ -173,6 +174,7 @@ fn build_nodes_one_proto()
173174
extra_sets: vec![
174175
config::NonDefaultSetConfig {
175176
notifications_protocol: PROTOCOL_NAME,
177+
fallback_names: Vec::new(),
176178
max_notification_size: 1024 * 1024,
177179
set_config: config::SetConfig {
178180
reserved_nodes: vec![config::MultiaddrWithPeerId {

0 commit comments

Comments
 (0)