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/>.
|
|
|
|
|
|
|
|
use std::collections::BTreeMap;
|
2017-03-02 12:25:55 +01:00
|
|
|
use util::{Address, HashMap};
|
2016-07-30 23:42:31 +02:00
|
|
|
use builtin::Builtin;
|
2016-12-08 12:03:34 +01:00
|
|
|
use engines::{Engine, Seal};
|
2016-07-30 23:42:31 +02:00
|
|
|
use spec::CommonParams;
|
|
|
|
use evm::Schedule;
|
|
|
|
use block::ExecutedBlock;
|
2017-04-19 14:30:00 +02:00
|
|
|
use header::BlockNumber;
|
2016-07-30 23:42:31 +02:00
|
|
|
|
|
|
|
/// An engine which does not provide any consensus mechanism, just seals blocks internally.
|
|
|
|
pub struct InstantSeal {
|
|
|
|
params: CommonParams,
|
2017-03-02 12:25:55 +01:00
|
|
|
registrar: Address,
|
2016-07-30 23:42:31 +02:00
|
|
|
builtins: BTreeMap<Address, Builtin>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl InstantSeal {
|
|
|
|
/// Returns new instance of InstantSeal with default VM Factory
|
2017-03-02 12:25:55 +01:00
|
|
|
pub fn new(params: CommonParams, registrar: Address, builtins: BTreeMap<Address, Builtin>) -> Self {
|
2016-07-30 23:42:31 +02:00
|
|
|
InstantSeal {
|
|
|
|
params: params,
|
2017-03-02 12:25:55 +01:00
|
|
|
registrar: registrar,
|
2016-07-30 23:42:31 +02:00
|
|
|
builtins: builtins,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Engine for InstantSeal {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"InstantSeal"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn params(&self) -> &CommonParams {
|
|
|
|
&self.params
|
|
|
|
}
|
|
|
|
|
2017-03-02 12:25:55 +01:00
|
|
|
fn additional_params(&self) -> HashMap<String, String> {
|
|
|
|
hash_map!["registrar".to_owned() => self.registrar.hex()]
|
|
|
|
}
|
|
|
|
|
2016-07-30 23:42:31 +02:00
|
|
|
fn builtins(&self) -> &BTreeMap<Address, Builtin> {
|
|
|
|
&self.builtins
|
|
|
|
}
|
|
|
|
|
2017-04-19 14:30:00 +02:00
|
|
|
fn schedule(&self, block_number: BlockNumber) -> Schedule {
|
|
|
|
let eip86 = block_number >= self.params.eip86_transition;
|
|
|
|
Schedule::new_post_eip150(usize::max_value(), true, true, true, eip86)
|
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
|
|
|
|
2016-12-08 12:03:34 +01:00
|
|
|
fn generate_seal(&self, _block: &ExecutedBlock) -> Seal {
|
|
|
|
Seal::Regular(Vec::new())
|
2016-07-30 23:42:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2016-10-24 18:35:25 +02:00
|
|
|
use util::*;
|
2016-07-30 23:42:31 +02:00
|
|
|
use tests::helpers::*;
|
|
|
|
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()]);
|
2016-12-05 18:08:16 +01:00
|
|
|
let b = OpenBlock::new(engine, Default::default(), false, db, &genesis_header, last_hashes, Address::default(), (3141562.into(), 31415620.into()), vec![]).unwrap();
|
2016-07-30 23:42:31 +02:00
|
|
|
let b = b.close_and_lock();
|
2016-12-08 12:03:34 +01:00
|
|
|
if let Seal::Regular(seal) = engine.generate_seal(b.block()) {
|
|
|
|
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();
|
|
|
|
|
|
|
|
assert!(engine.verify_block_basic(&header, None).is_ok());
|
|
|
|
|
2016-09-01 14:29:59 +02:00
|
|
|
header.set_seal(vec![::rlp::encode(&H520::default()).to_vec()]);
|
2016-07-30 23:42:31 +02:00
|
|
|
|
|
|
|
assert!(engine.verify_block_unordered(&header, None).is_ok());
|
|
|
|
}
|
|
|
|
}
|