2017-01-25 18:51:41 +01:00
|
|
|
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
|
2016-07-30 23:42:31 +02:00
|
|
|
// This file is part of Parity.
|
|
|
|
|
|
|
|
// Parity is free software: you can redistribute it and/or modify
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
// Parity is distributed in the hope that it will be useful,
|
|
|
|
// 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
|
|
|
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2016-12-08 12:03:34 +01:00
|
|
|
use engines::{Engine, Seal};
|
2018-05-16 08:58:01 +02:00
|
|
|
use parity_machine::{Machine, Transactions, TotalScoredHeader};
|
2016-07-30 23:42:31 +02:00
|
|
|
|
|
|
|
/// An engine which does not provide any consensus mechanism, just seals blocks internally.
|
2017-09-26 14:19:08 +02:00
|
|
|
/// Only seals blocks which have transactions.
|
|
|
|
pub struct InstantSeal<M> {
|
|
|
|
machine: M,
|
2016-07-30 23:42:31 +02:00
|
|
|
}
|
|
|
|
|
2017-09-26 14:19:08 +02:00
|
|
|
impl<M> InstantSeal<M> {
|
|
|
|
/// Returns new instance of InstantSeal over the given state machine.
|
|
|
|
pub fn new(machine: M) -> Self {
|
2016-07-30 23:42:31 +02:00
|
|
|
InstantSeal {
|
2017-09-26 14:19:08 +02:00
|
|
|
machine: machine,
|
2016-07-30 23:42:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-26 14:19:08 +02:00
|
|
|
impl<M: Machine> Engine<M> for InstantSeal<M>
|
2018-05-16 08:58:01 +02:00
|
|
|
where M::LiveBlock: Transactions,
|
|
|
|
M::ExtendedHeader: TotalScoredHeader,
|
|
|
|
<M::ExtendedHeader as TotalScoredHeader>::Value: Ord
|
2017-09-26 14:19:08 +02:00
|
|
|
{
|
2016-07-30 23:42:31 +02:00
|
|
|
fn name(&self) -> &str {
|
|
|
|
"InstantSeal"
|
|
|
|
}
|
|
|
|
|
2017-09-26 14:19:08 +02:00
|
|
|
fn machine(&self) -> &M { &self.machine }
|
2016-07-30 23:42:31 +02:00
|
|
|
|
2017-02-20 16:35:53 +01:00
|
|
|
fn seals_internally(&self) -> Option<bool> { Some(true) }
|
2016-09-13 15:09:07 +02:00
|
|
|
|
2017-12-07 12:17:11 +01:00
|
|
|
fn generate_seal(&self, block: &M::LiveBlock, _parent: &M::Header) -> Seal {
|
2017-08-03 16:42:56 +02:00
|
|
|
if block.transactions().is_empty() { Seal::None } else { Seal::Regular(Vec::new()) }
|
2016-07-30 23:42:31 +02:00
|
|
|
}
|
2017-09-26 14:19:08 +02:00
|
|
|
|
|
|
|
fn verify_local_seal(&self, _header: &M::Header) -> Result<(), M::Error> {
|
|
|
|
Ok(())
|
|
|
|
}
|
2018-04-05 10:11:21 +02:00
|
|
|
|
|
|
|
fn open_block_header_timestamp(&self, parent_timestamp: u64) -> u64 {
|
|
|
|
use std::{time, cmp};
|
|
|
|
|
|
|
|
let now = time::SystemTime::now().duration_since(time::UNIX_EPOCH).unwrap_or_default();
|
|
|
|
cmp::max(now.as_secs(), parent_timestamp)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_timestamp_valid(&self, header_timestamp: u64, parent_timestamp: u64) -> bool {
|
|
|
|
header_timestamp >= parent_timestamp
|
|
|
|
}
|
2018-05-16 08:58:01 +02:00
|
|
|
|
|
|
|
fn fork_choice(&self, new: &M::ExtendedHeader, current: &M::ExtendedHeader) -> super::ForkChoice {
|
|
|
|
super::total_difficulty_fork_choice(new, current)
|
|
|
|
}
|
2016-07-30 23:42:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2017-07-29 21:56:42 +02:00
|
|
|
use std::sync::Arc;
|
2018-01-10 13:35:18 +01:00
|
|
|
use ethereum_types::{H520, Address};
|
2018-04-09 16:14:33 +02:00
|
|
|
use test_helpers::get_temp_state_db;
|
2016-07-30 23:42:31 +02:00
|
|
|
use spec::Spec;
|
2016-10-24 18:35:25 +02:00
|
|
|
use header::Header;
|
2016-07-30 23:42:31 +02:00
|
|
|
use block::*;
|
2016-12-08 12:03:34 +01:00
|
|
|
use engines::Seal;
|
2016-07-30 23:42:31 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn instant_can_seal() {
|
2016-11-11 18:26:41 +01:00
|
|
|
let spec = Spec::new_instant();
|
2016-08-10 16:29:40 +02:00
|
|
|
let engine = &*spec.engine;
|
2017-04-06 19:26:17 +02:00
|
|
|
let db = spec.ensure_db_good(get_temp_state_db(), &Default::default()).unwrap();
|
2017-03-02 12:25:55 +01:00
|
|
|
let genesis_header = spec.genesis_header();
|
2016-08-03 22:03:40 +02:00
|
|
|
let last_hashes = Arc::new(vec![genesis_header.hash()]);
|
2018-05-16 08:58:01 +02:00
|
|
|
let b = OpenBlock::new(engine, Default::default(), false, db, &genesis_header, last_hashes, Address::default(), (3141562.into(), 31415620.into()), vec![], false, &mut Vec::new().into_iter()).unwrap();
|
2016-07-30 23:42:31 +02:00
|
|
|
let b = b.close_and_lock();
|
2017-12-07 12:17:11 +01:00
|
|
|
if let Seal::Regular(seal) = engine.generate_seal(b.block(), &genesis_header) {
|
2016-12-08 12:03:34 +01:00
|
|
|
assert!(b.try_seal(engine, seal).is_ok());
|
|
|
|
}
|
2016-07-30 23:42:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn instant_cant_verify() {
|
2016-11-11 18:26:41 +01:00
|
|
|
let engine = Spec::new_instant().engine;
|
2016-07-30 23:42:31 +02:00
|
|
|
let mut header: Header = Header::default();
|
|
|
|
|
2017-09-26 14:19:08 +02:00
|
|
|
assert!(engine.verify_block_basic(&header).is_ok());
|
2016-07-30 23:42:31 +02:00
|
|
|
|
2017-06-28 14:16:53 +02:00
|
|
|
header.set_seal(vec![::rlp::encode(&H520::default()).into_vec()]);
|
2016-07-30 23:42:31 +02:00
|
|
|
|
2017-09-26 14:19:08 +02:00
|
|
|
assert!(engine.verify_block_unordered(&header).is_ok());
|
2016-07-30 23:42:31 +02:00
|
|
|
}
|
|
|
|
}
|