babe: support online configuration upgrades (#5514)

* babe: support online configuration upgrades

* Switch to use NextConfigDescriptor instead of changing runtime interface

* Fix tests

* epoch-changes: map function that allows converting with different epoch types

* Add migration script for the epoch config change

* Fix migration tests

* Fix migration: Epoch should be EpochV0

* Update client/consensus/babe/src/lib.rs

Co-Authored-By: André Silva <123550+andresilva@users.noreply.github.com>

* Fix new epochChanges version

* Fix unused imports

Co-authored-by: André Silva <123550+andresilva@users.noreply.github.com>
This commit is contained in:
Wei Tang
2020-04-24 15:59:14 +02:00
committed by GitHub
parent a01e608dff
commit 770cc24c47
12 changed files with 300 additions and 86 deletions
@@ -335,6 +335,55 @@ impl<Hash, Number, E: Epoch> EpochChanges<Hash, Number, E> where
self.inner.rebalance()
}
/// Map the epoch changes from one storing data to a different one.
pub fn map<B, F>(self, mut f: F) -> EpochChanges<Hash, Number, B> where
B: Epoch<SlotNumber=E::SlotNumber>,
F: FnMut(&Hash, &Number, E) -> B,
{
EpochChanges {
inner: self.inner.map(&mut |_, _, header| {
match header {
PersistedEpochHeader::Genesis(epoch_0, epoch_1) => {
PersistedEpochHeader::Genesis(
EpochHeader {
start_slot: epoch_0.start_slot,
end_slot: epoch_0.end_slot,
},
EpochHeader {
start_slot: epoch_1.start_slot,
end_slot: epoch_1.end_slot,
},
)
},
PersistedEpochHeader::Regular(epoch_n) => {
PersistedEpochHeader::Regular(
EpochHeader {
start_slot: epoch_n.start_slot,
end_slot: epoch_n.end_slot,
},
)
},
}
}),
epochs: self.epochs.into_iter().map(|((hash, number), epoch)| {
let bepoch = match epoch {
PersistedEpoch::Genesis(epoch_0, epoch_1) => {
PersistedEpoch::Genesis(
f(&hash, &number, epoch_0),
f(&hash, &number, epoch_1),
)
},
PersistedEpoch::Regular(epoch_n) => {
PersistedEpoch::Regular(
f(&hash, &number, epoch_n)
)
},
};
((hash, number), bepoch)
}).collect(),
}
}
/// Prune out finalized epochs, except for the ancestor of the finalized
/// block. The given slot should be the slot number at which the finalized
/// block was authored.