2019-01-07 11:33:07 +01:00
|
|
|
// Copyright 2015-2019 Parity Technologies (UK) Ltd.
|
|
|
|
// This file is part of Parity Ethereum.
|
2017-05-17 12:41:33 +02:00
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is free software: you can redistribute it and/or modify
|
2017-05-17 12:41:33 +02:00
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is distributed in the hope that it will be useful,
|
2017-05-17 12:41:33 +02:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
2019-01-07 11:33:07 +01:00
|
|
|
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.
|
2017-05-17 12:41:33 +02:00
|
|
|
|
|
|
|
//! Helper for ancient block import.
|
|
|
|
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
2017-09-26 14:19:08 +02:00
|
|
|
use engines::{EthEngine, EpochVerifier};
|
2017-05-17 12:41:33 +02:00
|
|
|
|
2019-01-04 14:05:46 +01:00
|
|
|
use blockchain::BlockChain;
|
2017-09-02 20:09:13 +02:00
|
|
|
use parking_lot::RwLock;
|
2019-01-04 14:05:46 +01:00
|
|
|
use rand::Rng;
|
|
|
|
use types::header::Header;
|
2017-05-17 12:41:33 +02:00
|
|
|
|
|
|
|
// do "heavy" verification on ~1/50 blocks, randomly sampled.
|
|
|
|
const HEAVY_VERIFY_RATE: f32 = 0.02;
|
|
|
|
|
|
|
|
/// Ancient block verifier: import an ancient sequence of blocks in order from a starting
|
|
|
|
/// epoch.
|
|
|
|
pub struct AncientVerifier {
|
2019-06-26 14:16:05 +02:00
|
|
|
cur_verifier: RwLock<Option<Box<dyn EpochVerifier>>>,
|
2019-06-14 18:48:35 +02:00
|
|
|
engine: Arc<dyn EthEngine>,
|
2017-05-17 12:41:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AncientVerifier {
|
2018-05-09 08:49:34 +02:00
|
|
|
/// Create a new ancient block verifier with the given engine.
|
2019-06-14 18:48:35 +02:00
|
|
|
pub fn new(engine: Arc<dyn EthEngine>) -> Self {
|
2017-05-17 12:41:33 +02:00
|
|
|
AncientVerifier {
|
2018-05-09 08:49:34 +02:00
|
|
|
cur_verifier: RwLock::new(None),
|
|
|
|
engine,
|
2017-05-17 12:41:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Verify the next block header, randomly choosing whether to do heavy or light
|
|
|
|
/// verification. If the block is the end of an epoch, updates the epoch verifier.
|
2017-06-28 13:17:36 +02:00
|
|
|
pub fn verify<R: Rng>(
|
2017-05-17 12:41:33 +02:00
|
|
|
&self,
|
|
|
|
rng: &mut R,
|
|
|
|
header: &Header,
|
2017-06-28 13:17:36 +02:00
|
|
|
chain: &BlockChain,
|
2017-05-17 12:41:33 +02:00
|
|
|
) -> Result<(), ::error::Error> {
|
2018-05-09 08:49:34 +02:00
|
|
|
// perform verification
|
|
|
|
let verified = if let Some(ref cur_verifier) = *self.cur_verifier.read() {
|
|
|
|
match rng.gen::<f32>() <= HEAVY_VERIFY_RATE {
|
|
|
|
true => cur_verifier.verify_heavy(header)?,
|
|
|
|
false => cur_verifier.verify_light(header)?,
|
|
|
|
}
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
};
|
|
|
|
|
|
|
|
// when there is no verifier initialize it.
|
|
|
|
// We use a bool flag to avoid double locking in the happy case
|
|
|
|
if !verified {
|
|
|
|
{
|
|
|
|
let mut cur_verifier = self.cur_verifier.write();
|
|
|
|
if cur_verifier.is_none() {
|
|
|
|
*cur_verifier = Some(self.initial_verifier(header, chain)?);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Call again to verify.
|
|
|
|
return self.verify(rng, header, chain);
|
2017-05-17 12:41:33 +02:00
|
|
|
}
|
|
|
|
|
2017-06-28 13:17:36 +02:00
|
|
|
// ancient import will only use transitions obtained from the snapshot.
|
|
|
|
if let Some(transition) = chain.epoch_transition(header.number(), header.hash()) {
|
|
|
|
let v = self.engine.epoch_verifier(&header, &transition.proof).known_confirmed()?;
|
2018-05-09 08:49:34 +02:00
|
|
|
*self.cur_verifier.write() = Some(v);
|
2017-05-17 12:41:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2018-05-09 08:49:34 +02:00
|
|
|
|
|
|
|
fn initial_verifier(&self, header: &Header, chain: &BlockChain)
|
2019-06-26 14:16:05 +02:00
|
|
|
-> Result<Box<dyn EpochVerifier>, ::error::Error>
|
2018-05-09 08:49:34 +02:00
|
|
|
{
|
|
|
|
trace!(target: "client", "Initializing ancient block restoration.");
|
|
|
|
let current_epoch_data = chain.epoch_transitions()
|
|
|
|
.take_while(|&(_, ref t)| t.block_number < header.number())
|
|
|
|
.last()
|
|
|
|
.map(|(_, t)| t.proof)
|
|
|
|
.expect("At least one epoch entry (genesis) always stored; qed");
|
|
|
|
|
|
|
|
self.engine.epoch_verifier(&header, ¤t_epoch_data).known_confirmed()
|
|
|
|
}
|
2017-05-17 12:41:33 +02:00
|
|
|
}
|