2019-01-07 11:33:07 +01:00
|
|
|
// Copyright 2015-2019 Parity Technologies (UK) Ltd.
|
|
|
|
// This file is part of Parity Ethereum.
|
2016-07-30 23:42:31 +02:00
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is free software: you can redistribute it and/or modify
|
2016-07-30 23:42:31 +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,
|
2016-07-30 23:42:31 +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/>.
|
2016-07-30 23:42:31 +02:00
|
|
|
|
2019-09-11 18:44:31 +02:00
|
|
|
use std::sync::atomic::{AtomicU64, Ordering};
|
|
|
|
|
2019-08-22 18:25:49 +02:00
|
|
|
use common_types::{
|
2019-07-18 12:27:08 +02:00
|
|
|
header::Header,
|
|
|
|
engines::{
|
2019-08-15 17:59:22 +02:00
|
|
|
Seal,
|
2019-07-18 12:27:08 +02:00
|
|
|
SealingState,
|
|
|
|
params::CommonParams,
|
|
|
|
},
|
|
|
|
errors::EthcoreError as Error,
|
|
|
|
};
|
2019-08-22 18:25:49 +02:00
|
|
|
use engine::Engine;
|
|
|
|
use ethjson;
|
|
|
|
use machine::{
|
|
|
|
ExecutedBlock,
|
|
|
|
Machine
|
|
|
|
};
|
2019-07-18 12:27:08 +02:00
|
|
|
|
2016-07-30 23:42:31 +02:00
|
|
|
|
2018-09-06 11:38:00 +02:00
|
|
|
/// `InstantSeal` params.
|
2018-10-04 15:08:20 +02:00
|
|
|
#[derive(Default, Debug, PartialEq)]
|
2018-09-06 11:38:00 +02:00
|
|
|
pub struct InstantSealParams {
|
|
|
|
/// Whether to use millisecond timestamp
|
|
|
|
pub millisecond_timestamp: bool,
|
|
|
|
}
|
|
|
|
|
2019-08-22 18:25:49 +02:00
|
|
|
impl From<ethjson::spec::InstantSealParams> for InstantSealParams {
|
|
|
|
fn from(p: ethjson::spec::InstantSealParams) -> Self {
|
2018-09-06 11:38:00 +02:00
|
|
|
InstantSealParams {
|
|
|
|
millisecond_timestamp: p.millisecond_timestamp,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
|
2019-06-26 14:16:05 +02:00
|
|
|
pub struct InstantSeal {
|
2018-09-06 11:38:00 +02:00
|
|
|
params: InstantSealParams,
|
2019-06-26 14:16:05 +02:00
|
|
|
machine: Machine,
|
2019-09-11 18:44:31 +02:00
|
|
|
last_sealed_block: AtomicU64,
|
2016-07-30 23:42:31 +02:00
|
|
|
}
|
|
|
|
|
2019-06-26 14:16:05 +02:00
|
|
|
impl InstantSeal {
|
2017-09-26 14:19:08 +02:00
|
|
|
/// Returns new instance of InstantSeal over the given state machine.
|
2019-06-26 14:16:05 +02:00
|
|
|
pub fn new(params: InstantSealParams, machine: Machine) -> Self {
|
2016-07-30 23:42:31 +02:00
|
|
|
InstantSeal {
|
2019-06-26 14:16:05 +02:00
|
|
|
params,
|
|
|
|
machine,
|
2019-09-11 18:44:31 +02:00
|
|
|
last_sealed_block: AtomicU64::new(0),
|
2016-07-30 23:42:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-26 14:16:05 +02:00
|
|
|
impl Engine for InstantSeal {
|
2019-08-22 18:25:49 +02:00
|
|
|
fn name(&self) -> &str { "InstantSeal" }
|
2016-07-30 23:42:31 +02:00
|
|
|
|
2019-06-26 14:16:05 +02:00
|
|
|
fn machine(&self) -> &Machine { &self.machine }
|
2016-07-30 23:42:31 +02:00
|
|
|
|
2019-05-24 10:30:31 +02:00
|
|
|
fn sealing_state(&self) -> SealingState { SealingState::Ready }
|
2016-09-13 15:09:07 +02:00
|
|
|
|
2019-03-13 11:36:13 +01:00
|
|
|
fn generate_seal(&self, block: &ExecutedBlock, _parent: &Header) -> Seal {
|
2019-09-11 18:44:31 +02:00
|
|
|
if !block.transactions.is_empty() {
|
|
|
|
let block_number = block.header.number();
|
|
|
|
let last_sealed_block = self.last_sealed_block.load(Ordering::SeqCst);
|
|
|
|
// Return a regular seal if the given block is _higher_ than
|
|
|
|
// the last sealed one
|
|
|
|
if block_number > last_sealed_block {
|
|
|
|
let prev_last_sealed_block = self.last_sealed_block.compare_and_swap(last_sealed_block, block_number, Ordering::SeqCst);
|
|
|
|
if prev_last_sealed_block == last_sealed_block {
|
|
|
|
return Seal::Regular(Vec::new())
|
|
|
|
}
|
|
|
|
}
|
2019-03-13 11:36:13 +01:00
|
|
|
}
|
2019-09-11 18:44:31 +02:00
|
|
|
Seal::None
|
2016-07-30 23:42:31 +02:00
|
|
|
}
|
2017-09-26 14:19:08 +02:00
|
|
|
|
2019-06-26 14:16:05 +02:00
|
|
|
fn verify_local_seal(&self, _header: &Header) -> Result<(), Error> {
|
2017-09-26 14:19:08 +02:00
|
|
|
Ok(())
|
|
|
|
}
|
2018-04-05 10:11:21 +02:00
|
|
|
|
|
|
|
fn open_block_header_timestamp(&self, parent_timestamp: u64) -> u64 {
|
|
|
|
use std::{time, cmp};
|
|
|
|
|
2018-09-06 11:38:00 +02:00
|
|
|
let dur = time::SystemTime::now().duration_since(time::UNIX_EPOCH).unwrap_or_default();
|
|
|
|
let mut now = dur.as_secs();
|
|
|
|
if self.params.millisecond_timestamp {
|
|
|
|
now = now * 1000 + dur.subsec_millis() as u64;
|
|
|
|
}
|
|
|
|
cmp::max(now, parent_timestamp)
|
2018-04-05 10:11:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn is_timestamp_valid(&self, header_timestamp: u64, parent_timestamp: u64) -> bool {
|
|
|
|
header_timestamp >= parent_timestamp
|
|
|
|
}
|
2019-07-18 12:27:08 +02:00
|
|
|
|
|
|
|
fn params(&self) -> &CommonParams {
|
|
|
|
self.machine.params()
|
|
|
|
}
|
|
|
|
|
2016-07-30 23:42:31 +02:00
|
|
|
}
|
|
|
|
|
2019-07-18 12:27:08 +02:00
|
|
|
|
2016-07-30 23:42:31 +02:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2017-07-29 21:56:42 +02:00
|
|
|
use std::sync::Arc;
|
2019-08-22 18:25:49 +02:00
|
|
|
use common_types::{
|
2019-08-15 17:59:22 +02:00
|
|
|
header::Header,
|
|
|
|
engines::Seal,
|
|
|
|
};
|
2019-08-22 18:25:49 +02:00
|
|
|
use ethereum_types::{H520, Address};
|
|
|
|
use ethcore::{
|
|
|
|
test_helpers::get_temp_state_db,
|
|
|
|
block::*,
|
|
|
|
};
|
2019-08-23 15:32:58 +02:00
|
|
|
use spec;
|
2016-07-30 23:42:31 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn instant_can_seal() {
|
2019-08-07 16:52:48 +02: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()]);
|
2019-07-04 13:43:20 +02:00
|
|
|
let b = OpenBlock::new(engine, Default::default(), false, db, &genesis_header, last_hashes, Address::zero(), (3141562.into(), 31415620.into()), vec![], false).unwrap();
|
2018-07-16 13:53:55 +02:00
|
|
|
let b = b.close_and_lock().unwrap();
|
2019-03-15 13:22:47 +01:00
|
|
|
if let Seal::Regular(seal) = engine.generate_seal(&b, &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() {
|
2019-08-07 16:52:48 +02: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
|
|
|
|
2019-08-22 18:25:49 +02:00
|
|
|
header.set_seal(vec![rlp::encode(&H520::default())]);
|
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
|
|
|
}
|
|
|
|
}
|