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

Commit ed5083f

Browse files
committed
Merge branch 'master' of github.com:paritytech/substrate into kiz-fix-reward-pools-2
2 parents 3da2364 + ee3eb22 commit ed5083f

File tree

28 files changed

+785
-329
lines changed

28 files changed

+785
-329
lines changed

Cargo.lock

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

bin/node/runtime/src/lib.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ use frame_system::{
4747
};
4848
pub use node_primitives::{AccountId, Signature};
4949
use node_primitives::{AccountIndex, Balance, BlockNumber, Hash, Index, Moment};
50-
use pallet_contracts::weights::WeightInfo;
5150
use pallet_election_provider_multi_phase::SolutionAccuracyOf;
5251
use pallet_grandpa::{
5352
fg_primitives, AuthorityId as GrandpaId, AuthorityList as GrandpaAuthorityList,
@@ -1118,18 +1117,13 @@ parameter_types! {
11181117
pub const DepositPerItem: Balance = deposit(1, 0);
11191118
pub const DepositPerByte: Balance = deposit(0, 1);
11201119
pub const MaxValueSize: u32 = 16 * 1024;
1120+
pub const DeletionQueueDepth: u32 = 128;
11211121
// The lazy deletion runs inside on_initialize.
11221122
pub DeletionWeightLimit: Weight = RuntimeBlockWeights::get()
11231123
.per_class
11241124
.get(DispatchClass::Normal)
11251125
.max_total
11261126
.unwrap_or(RuntimeBlockWeights::get().max_block);
1127-
// The weight needed for decoding the queue should be less or equal than a fifth
1128-
// of the overall weight dedicated to the lazy deletion.
1129-
pub DeletionQueueDepth: u32 = ((DeletionWeightLimit::get() / (
1130-
<Runtime as pallet_contracts::Config>::WeightInfo::on_initialize_per_queue_item(1) -
1131-
<Runtime as pallet_contracts::Config>::WeightInfo::on_initialize_per_queue_item(0)
1132-
)) / 5) as u32;
11331127
pub Schedule: pallet_contracts::Schedule<Runtime> = Default::default();
11341128
}
11351129

client/consensus/babe/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1873,7 +1873,11 @@ where
18731873
{
18741874
let best_number = client.info().best_number;
18751875
let finalized = client.info().finalized_number;
1876+
18761877
let revertible = blocks.min(best_number - finalized);
1878+
if revertible == Zero::zero() {
1879+
return Ok(())
1880+
}
18771881

18781882
let revert_up_to_number = best_number - revertible;
18791883
let revert_up_to_hash = client.hash(revert_up_to_number)?.ok_or(ClientError::Backend(

client/consensus/epochs/src/lib.rs

Lines changed: 112 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,10 @@ where
755755
Err(e) => PersistedEpoch::Regular(e),
756756
}
757757
}
758-
} else if epoch.is_genesis() && !self.epochs.values().all(|e| e.is_genesis()) {
758+
} else if epoch.is_genesis() &&
759+
!self.epochs.is_empty() &&
760+
!self.epochs.values().any(|e| e.is_genesis())
761+
{
759762
// There's a genesis epoch imported when we already have an active epoch.
760763
// This happens after the warp sync as the ancient blocks download start.
761764
// We need to start tracking gap epochs here.
@@ -1170,15 +1173,17 @@ mod tests {
11701173
epoch_changes.clear_gap();
11711174

11721175
// Check that both epochs are available.
1173-
epoch_changes
1176+
let epoch_a = epoch_changes
11741177
.epoch_data_for_child_of(&is_descendent_of, b"A", 1, 101, &make_genesis)
11751178
.unwrap()
11761179
.unwrap();
11771180

1178-
epoch_changes
1181+
let epoch_x = epoch_changes
11791182
.epoch_data_for_child_of(&is_descendent_of, b"X", 1, 1001, &make_genesis)
11801183
.unwrap()
11811184
.unwrap();
1185+
1186+
assert!(epoch_a != epoch_x)
11821187
}
11831188

11841189
#[test]
@@ -1288,4 +1293,108 @@ mod tests {
12881293
epoch_changes.clear_gap();
12891294
assert!(epoch_changes.gap.is_none());
12901295
}
1296+
1297+
/// Test that ensures that the gap is not enabled when there's still genesis
1298+
/// epochs imported, regardless of whether there are already other further
1299+
/// epochs imported descending from such genesis epochs.
1300+
#[test]
1301+
fn gap_is_not_enabled_when_at_least_one_genesis_epoch_is_still_imported() {
1302+
// A (#1) - B (#201)
1303+
// /
1304+
// 0 - C (#1)
1305+
//
1306+
// The epoch duration is 100 slots, each of these blocks represents
1307+
// an epoch change block. block B starts a new epoch at #201 since the
1308+
// genesis epoch spans two epochs.
1309+
1310+
let is_descendent_of = |base: &Hash, block: &Hash| -> Result<bool, TestError> {
1311+
match (base, block) {
1312+
(b"0", _) => Ok(true),
1313+
(b"A", b"B") => Ok(true),
1314+
_ => Ok(false),
1315+
}
1316+
};
1317+
1318+
let duration = 100;
1319+
let make_genesis = |slot| Epoch { start_slot: slot, duration };
1320+
let mut epoch_changes = EpochChanges::new();
1321+
let next_descriptor = ();
1322+
1323+
// insert genesis epoch for A at slot 1
1324+
{
1325+
let genesis_epoch_a_descriptor = epoch_changes
1326+
.epoch_descriptor_for_child_of(&is_descendent_of, b"0", 0, 1)
1327+
.unwrap()
1328+
.unwrap();
1329+
1330+
let incremented_epoch = epoch_changes
1331+
.viable_epoch(&genesis_epoch_a_descriptor, &make_genesis)
1332+
.unwrap()
1333+
.increment(next_descriptor.clone());
1334+
1335+
epoch_changes
1336+
.import(&is_descendent_of, *b"A", 1, *b"0", incremented_epoch)
1337+
.unwrap();
1338+
}
1339+
1340+
// insert regular epoch for B at slot 201, descending from A
1341+
{
1342+
let epoch_b_descriptor = epoch_changes
1343+
.epoch_descriptor_for_child_of(&is_descendent_of, b"A", 1, 201)
1344+
.unwrap()
1345+
.unwrap();
1346+
1347+
let incremented_epoch = epoch_changes
1348+
.viable_epoch(&epoch_b_descriptor, &make_genesis)
1349+
.unwrap()
1350+
.increment(next_descriptor.clone());
1351+
1352+
epoch_changes
1353+
.import(&is_descendent_of, *b"B", 201, *b"A", incremented_epoch)
1354+
.unwrap();
1355+
}
1356+
1357+
// insert genesis epoch for C at slot 1000
1358+
{
1359+
let genesis_epoch_x_descriptor = epoch_changes
1360+
.epoch_descriptor_for_child_of(&is_descendent_of, b"0", 0, 1000)
1361+
.unwrap()
1362+
.unwrap();
1363+
1364+
let incremented_epoch = epoch_changes
1365+
.viable_epoch(&genesis_epoch_x_descriptor, &make_genesis)
1366+
.unwrap()
1367+
.increment(next_descriptor.clone());
1368+
1369+
epoch_changes
1370+
.import(&is_descendent_of, *b"C", 1, *b"0", incremented_epoch)
1371+
.unwrap();
1372+
}
1373+
1374+
// Clearing the gap should be a no-op.
1375+
epoch_changes.clear_gap();
1376+
1377+
// Check that all three epochs are available.
1378+
let epoch_a = epoch_changes
1379+
.epoch_data_for_child_of(&is_descendent_of, b"A", 1, 10, &make_genesis)
1380+
.unwrap()
1381+
.unwrap();
1382+
1383+
let epoch_b = epoch_changes
1384+
.epoch_data_for_child_of(&is_descendent_of, b"B", 201, 201, &make_genesis)
1385+
.unwrap()
1386+
.unwrap();
1387+
1388+
assert!(epoch_a != epoch_b);
1389+
1390+
// the genesis epoch A will span slots [1, 200] with epoch B starting at slot 201
1391+
assert_eq!(epoch_b.start_slot(), 201);
1392+
1393+
let epoch_c = epoch_changes
1394+
.epoch_data_for_child_of(&is_descendent_of, b"C", 1, 1001, &make_genesis)
1395+
.unwrap()
1396+
.unwrap();
1397+
1398+
assert!(epoch_a != epoch_c);
1399+
}
12911400
}

client/finality-grandpa/src/lib.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,11 +1170,15 @@ fn local_authority_id(
11701170
pub fn revert<Block, Client>(client: Arc<Client>, blocks: NumberFor<Block>) -> ClientResult<()>
11711171
where
11721172
Block: BlockT,
1173-
Client: AuxStore + HeaderMetadata<Block, Error = sp_blockchain::Error> + HeaderBackend<Block>,
1173+
Client: AuxStore + HeaderMetadata<Block, Error = ClientError> + HeaderBackend<Block>,
11741174
{
11751175
let best_number = client.info().best_number;
11761176
let finalized = client.info().finalized_number;
1177+
11771178
let revertible = blocks.min(best_number - finalized);
1179+
if revertible == Zero::zero() {
1180+
return Ok(())
1181+
}
11781182

11791183
let number = best_number - revertible;
11801184
let hash = client
@@ -1185,8 +1189,12 @@ where
11851189
)))?;
11861190

11871191
let info = client.info();
1192+
11881193
let persistent_data: PersistentData<Block> =
1189-
aux_schema::load_persistent(&*client, info.genesis_hash, Zero::zero(), || unreachable!())?;
1194+
aux_schema::load_persistent(&*client, info.genesis_hash, Zero::zero(), || {
1195+
const MSG: &str = "Unexpected missing grandpa data during revert";
1196+
Err(ClientError::Application(Box::from(MSG)))
1197+
})?;
11901198

11911199
let shared_authority_set = persistent_data.authority_set;
11921200
let mut authority_set = shared_authority_set.inner();

frame/beefy-mmr/primitives/Cargo.toml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ hex = { version = "0.4", default-features = false, optional = true }
1313
log = { version = "0.4", default-features = false, optional = true }
1414
tiny-keccak = { version = "2.0.2", features = ["keccak"], optional = true }
1515

16+
beefy-primitives = { version = "4.0.0-dev", default-features = false, path = "../../../primitives/beefy" }
17+
sp-api = { version = "4.0.0-dev", default-features = false, path = "../../../primitives/api" }
18+
1619
[dev-dependencies]
1720
env_logger = "0.9"
1821
hex = "0.4"
@@ -22,4 +25,7 @@ hex-literal = "0.3"
2225
debug = ["hex", "hex/std", "log"]
2326
default = ["debug", "keccak", "std"]
2427
keccak = ["tiny-keccak"]
25-
std = []
28+
std = [
29+
"beefy-primitives/std",
30+
"sp-api/std"
31+
]

frame/beefy-mmr/primitives/src/lib.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ extern crate alloc;
3636
#[cfg(not(feature = "std"))]
3737
use alloc::vec::Vec;
3838

39+
use beefy_primitives::mmr::{BeefyAuthoritySet, BeefyNextAuthoritySet};
40+
3941
/// Supported hashing output size.
4042
///
4143
/// The size is restricted to 32 bytes to allow for a more optimised implementation.
@@ -375,6 +377,21 @@ where
375377
}
376378
}
377379

380+
sp_api::decl_runtime_apis! {
381+
/// API useful for BEEFY light clients.
382+
pub trait BeefyMmrApi<H>
383+
where
384+
H: From<Hash> + Into<Hash>,
385+
BeefyAuthoritySet<H>: sp_api::Decode,
386+
{
387+
/// Return the currently active BEEFY authority set proof.
388+
fn authority_set_proof() -> BeefyAuthoritySet<H>;
389+
390+
/// Return the next/queued BEEFY authority set proof.
391+
fn next_authority_set_proof() -> BeefyNextAuthoritySet<H>;
392+
}
393+
}
394+
378395
#[cfg(test)]
379396
mod tests {
380397
use super::*;

frame/beefy-mmr/src/lib.rs

Lines changed: 48 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@
3636
use sp_runtime::traits::{Convert, Hash, Member};
3737
use sp_std::prelude::*;
3838

39-
use beefy_primitives::mmr::{BeefyDataProvider, BeefyNextAuthoritySet, MmrLeaf, MmrLeafVersion};
39+
use beefy_primitives::{
40+
mmr::{BeefyAuthoritySet, BeefyDataProvider, BeefyNextAuthoritySet, MmrLeaf, MmrLeafVersion},
41+
ValidatorSet as BeefyValidatorSet,
42+
};
4043
use pallet_mmr::{LeafDataProvider, ParentNumberAndHash};
4144

4245
use frame_support::{crypto::ecdsa::ECDSAExt, traits::Get};
@@ -124,6 +127,12 @@ pub mod pallet {
124127
type BeefyDataProvider: BeefyDataProvider<Self::LeafExtra>;
125128
}
126129

130+
/// Details of current BEEFY authority set.
131+
#[pallet::storage]
132+
#[pallet::getter(fn beefy_authorities)]
133+
pub type BeefyAuthorities<T: Config> =
134+
StorageValue<_, BeefyAuthoritySet<MerkleRootOf<T>>, ValueQuery>;
135+
127136
/// Details of next BEEFY authority set.
128137
///
129138
/// This storage entry is used as cache for calls to `update_beefy_next_authority_set`.
@@ -149,7 +158,7 @@ where
149158
version: T::LeafVersion::get(),
150159
parent_number_and_hash: ParentNumberAndHash::<T>::leaf_data(),
151160
leaf_extra: T::BeefyDataProvider::extra_data(),
152-
beefy_next_authority_set: Pallet::<T>::update_beefy_next_authority_set(),
161+
beefy_next_authority_set: Pallet::<T>::beefy_next_authorities(),
153162
}
154163
}
155164
}
@@ -163,35 +172,55 @@ where
163172
}
164173
}
165174

175+
impl<T> beefy_primitives::OnNewValidatorSet<<T as pallet_beefy::Config>::BeefyId> for Pallet<T>
176+
where
177+
T: pallet::Config,
178+
MerkleRootOf<T>: From<beefy_merkle_tree::Hash> + Into<beefy_merkle_tree::Hash>,
179+
{
180+
/// Compute and cache BEEFY authority sets based on updated BEEFY validator sets.
181+
fn on_new_validator_set(
182+
current_set: &BeefyValidatorSet<<T as pallet_beefy::Config>::BeefyId>,
183+
next_set: &BeefyValidatorSet<<T as pallet_beefy::Config>::BeefyId>,
184+
) {
185+
let current = Pallet::<T>::compute_authority_set(current_set);
186+
let next = Pallet::<T>::compute_authority_set(next_set);
187+
// cache the result
188+
BeefyAuthorities::<T>::put(&current);
189+
BeefyNextAuthorities::<T>::put(&next);
190+
}
191+
}
192+
166193
impl<T: Config> Pallet<T>
167194
where
168195
MerkleRootOf<T>: From<beefy_merkle_tree::Hash> + Into<beefy_merkle_tree::Hash>,
169196
{
170-
/// Returns details of the next BEEFY authority set.
197+
/// Return the currently active BEEFY authority set proof.
198+
pub fn authority_set_proof() -> BeefyAuthoritySet<MerkleRootOf<T>> {
199+
Pallet::<T>::beefy_authorities()
200+
}
201+
202+
/// Return the next/queued BEEFY authority set proof.
203+
pub fn next_authority_set_proof() -> BeefyNextAuthoritySet<MerkleRootOf<T>> {
204+
Pallet::<T>::beefy_next_authorities()
205+
}
206+
207+
/// Returns details of a BEEFY authority set.
171208
///
172209
/// Details contain authority set id, authority set length and a merkle root,
173210
/// constructed from uncompressed secp256k1 public keys converted to Ethereum addresses
174211
/// of the next BEEFY authority set.
175-
///
176-
/// This function will use a storage-cached entry in case the set didn't change, or compute and
177-
/// cache new one in case it did.
178-
fn update_beefy_next_authority_set() -> BeefyNextAuthoritySet<MerkleRootOf<T>> {
179-
let id = pallet_beefy::Pallet::<T>::validator_set_id() + 1;
180-
let current_next = Self::beefy_next_authorities();
181-
// avoid computing the merkle tree if validator set id didn't change.
182-
if id == current_next.id {
183-
return current_next
184-
}
185-
186-
let beefy_addresses = pallet_beefy::Pallet::<T>::next_authorities()
212+
fn compute_authority_set(
213+
validator_set: &BeefyValidatorSet<<T as pallet_beefy::Config>::BeefyId>,
214+
) -> BeefyAuthoritySet<MerkleRootOf<T>> {
215+
let id = validator_set.id();
216+
let beefy_addresses = validator_set
217+
.validators()
187218
.into_iter()
219+
.cloned()
188220
.map(T::BeefyAuthorityToMerkleLeaf::convert)
189221
.collect::<Vec<_>>();
190222
let len = beefy_addresses.len() as u32;
191223
let root = beefy_merkle_tree::merkle_root::<Self, _, _>(beefy_addresses).into();
192-
let next_set = BeefyNextAuthoritySet { id, len, root };
193-
// cache the result
194-
BeefyNextAuthorities::<T>::put(&next_set);
195-
next_set
224+
BeefyAuthoritySet { id, len, root }
196225
}
197226
}

frame/beefy-mmr/src/mock.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ impl pallet_mmr::Config for Test {
125125
impl pallet_beefy::Config for Test {
126126
type BeefyId = BeefyId;
127127
type MaxAuthorities = ConstU32<100>;
128+
type OnNewValidatorSet = BeefyMmr;
128129
}
129130

130131
parameter_types! {

0 commit comments

Comments
 (0)