2016-07-30 23:42:31 +02:00
|
|
|
// Copyright 2015, 2016 Ethcore (UK) Ltd.
|
|
|
|
// 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;
|
2016-08-15 15:09:00 +02:00
|
|
|
use util::Address;
|
2016-07-30 23:42:31 +02:00
|
|
|
use builtin::Builtin;
|
|
|
|
use engines::Engine;
|
|
|
|
use spec::CommonParams;
|
|
|
|
use evm::Schedule;
|
|
|
|
use env_info::EnvInfo;
|
|
|
|
use block::ExecutedBlock;
|
|
|
|
use common::Bytes;
|
|
|
|
use account_provider::AccountProvider;
|
|
|
|
|
|
|
|
/// An engine which does not provide any consensus mechanism, just seals blocks internally.
|
|
|
|
pub struct InstantSeal {
|
|
|
|
params: CommonParams,
|
|
|
|
builtins: BTreeMap<Address, Builtin>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl InstantSeal {
|
|
|
|
/// Returns new instance of InstantSeal with default VM Factory
|
|
|
|
pub fn new(params: CommonParams, builtins: BTreeMap<Address, Builtin>) -> Self {
|
|
|
|
InstantSeal {
|
|
|
|
params: params,
|
|
|
|
builtins: builtins,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Engine for InstantSeal {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"InstantSeal"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn params(&self) -> &CommonParams {
|
|
|
|
&self.params
|
|
|
|
}
|
|
|
|
|
|
|
|
fn builtins(&self) -> &BTreeMap<Address, Builtin> {
|
|
|
|
&self.builtins
|
|
|
|
}
|
|
|
|
|
|
|
|
fn schedule(&self, _env_info: &EnvInfo) -> Schedule {
|
|
|
|
Schedule::new_homestead()
|
|
|
|
}
|
|
|
|
|
2016-09-15 12:12:15 +02:00
|
|
|
fn is_sealer(&self, _author: &Address) -> Option<bool> { Some(true) }
|
2016-09-13 15:09:07 +02:00
|
|
|
|
2016-07-30 23:42:31 +02:00
|
|
|
fn generate_seal(&self, _block: &ExecutedBlock, _accounts: Option<&AccountProvider>) -> Option<Vec<Bytes>> {
|
|
|
|
Some(Vec::new())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use common::*;
|
|
|
|
use tests::helpers::*;
|
|
|
|
use account_provider::AccountProvider;
|
|
|
|
use spec::Spec;
|
|
|
|
use block::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn instant_can_seal() {
|
|
|
|
let tap = AccountProvider::transient_provider();
|
|
|
|
let addr = tap.insert_account("".sha3(), "").unwrap();
|
|
|
|
|
2016-09-13 15:09:07 +02:00
|
|
|
let spec = Spec::new_test_instant();
|
2016-08-10 16:29:40 +02:00
|
|
|
let engine = &*spec.engine;
|
2016-07-30 23:42:31 +02:00
|
|
|
let genesis_header = spec.genesis_header();
|
|
|
|
let mut db_result = get_temp_journal_db();
|
|
|
|
let mut db = db_result.take();
|
2016-08-03 18:35:48 +02:00
|
|
|
spec.ensure_db_good(db.as_hashdb_mut()).unwrap();
|
2016-08-03 22:03:40 +02:00
|
|
|
let last_hashes = Arc::new(vec![genesis_header.hash()]);
|
2016-08-24 16:53:36 +02:00
|
|
|
let b = OpenBlock::new(engine, Default::default(), false, db, &genesis_header, last_hashes, addr, (3141562.into(), 31415620.into()), vec![]).unwrap();
|
2016-07-30 23:42:31 +02:00
|
|
|
let b = b.close_and_lock();
|
|
|
|
// Seal with empty AccountProvider.
|
|
|
|
let seal = engine.generate_seal(b.block(), Some(&tap)).unwrap();
|
2016-08-10 16:29:40 +02:00
|
|
|
assert!(b.try_seal(engine, seal).is_ok());
|
2016-07-30 23:42:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn instant_cant_verify() {
|
2016-09-13 15:09:07 +02:00
|
|
|
let engine = Spec::new_test_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());
|
|
|
|
}
|
|
|
|
}
|