2019-01-07 11:33:07 +01:00
|
|
|
// Copyright 2015-2019 Parity Technologies (UK) Ltd.
|
|
|
|
// This file is part of Parity Ethereum.
|
2016-02-05 13:40:41 +01:00
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is free software: you can redistribute it and/or modify
|
2016-02-05 13:40:41 +01: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-02-05 13:40:41 +01: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-02-05 13:40:41 +01:00
|
|
|
|
2019-08-15 17:59:22 +02:00
|
|
|
use engine::snapshot::SnapshotComponents;
|
|
|
|
use engine::Engine;
|
2018-05-07 12:57:03 +02:00
|
|
|
use engines::block_reward::{self, RewardKind};
|
2019-01-04 14:05:46 +01:00
|
|
|
use ethereum_types::U256;
|
2019-08-13 12:33:34 +02:00
|
|
|
use machine::{
|
|
|
|
ExecutedBlock,
|
|
|
|
Machine,
|
|
|
|
};
|
2019-07-18 12:27:08 +02:00
|
|
|
use types::{
|
|
|
|
BlockNumber,
|
|
|
|
header::Header,
|
|
|
|
engines::params::CommonParams,
|
|
|
|
errors::EthcoreError as Error,
|
|
|
|
};
|
2017-09-26 14:19:08 +02:00
|
|
|
|
|
|
|
/// Params for a null engine.
|
|
|
|
#[derive(Clone, Default)]
|
|
|
|
pub struct NullEngineParams {
|
|
|
|
/// base reward for a block.
|
|
|
|
pub block_reward: U256,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<::ethjson::spec::NullEngineParams> for NullEngineParams {
|
|
|
|
fn from(p: ::ethjson::spec::NullEngineParams) -> Self {
|
|
|
|
NullEngineParams {
|
|
|
|
block_reward: p.block_reward.map_or_else(Default::default, Into::into),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-12-20 21:45:43 +01:00
|
|
|
|
2016-07-28 20:32:20 +02:00
|
|
|
/// An engine which does not provide any consensus mechanism and does not seal blocks.
|
2019-06-26 14:16:05 +02:00
|
|
|
pub struct NullEngine {
|
2017-09-26 14:19:08 +02:00
|
|
|
params: NullEngineParams,
|
2019-06-26 14:16:05 +02:00
|
|
|
machine: Machine,
|
2015-12-20 21:45:43 +01:00
|
|
|
}
|
|
|
|
|
2019-06-26 14:16:05 +02:00
|
|
|
impl NullEngine {
|
2016-01-21 16:08:09 +01:00
|
|
|
/// Returns new instance of NullEngine with default VM Factory
|
2019-06-26 14:16:05 +02:00
|
|
|
pub fn new(params: NullEngineParams, machine: Machine) -> Self {
|
2017-09-26 14:19:08 +02:00
|
|
|
NullEngine {
|
2019-06-26 14:16:05 +02:00
|
|
|
params,
|
|
|
|
machine,
|
2016-04-09 19:20:35 +02:00
|
|
|
}
|
2015-12-20 21:45:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-26 14:16:05 +02:00
|
|
|
impl Engine for NullEngine {
|
2016-04-09 19:20:35 +02:00
|
|
|
fn name(&self) -> &str {
|
|
|
|
"NullEngine"
|
|
|
|
}
|
|
|
|
|
2019-06-26 14:16:05 +02:00
|
|
|
fn machine(&self) -> &Machine { &self.machine }
|
2016-04-09 19:20:35 +02:00
|
|
|
|
2019-07-18 12:27:08 +02:00
|
|
|
fn maximum_uncle_count(&self, _block: BlockNumber) -> usize { 2 }
|
|
|
|
|
2019-07-04 13:43:20 +02:00
|
|
|
fn on_close_block(
|
|
|
|
&self,
|
|
|
|
block: &mut ExecutedBlock,
|
|
|
|
_parent_header: &Header
|
|
|
|
) -> Result<(), Error> {
|
2017-09-26 14:19:08 +02:00
|
|
|
use std::ops::Shr;
|
2016-03-20 19:20:37 +01:00
|
|
|
|
2019-03-13 11:36:13 +01:00
|
|
|
let author = *block.header.author();
|
|
|
|
let number = block.header.number();
|
2017-04-19 20:31:53 +02:00
|
|
|
|
2017-09-26 14:19:08 +02:00
|
|
|
let reward = self.params.block_reward;
|
|
|
|
if reward == U256::zero() { return Ok(()) }
|
2017-07-25 12:04:37 +02:00
|
|
|
|
2019-03-13 11:36:13 +01:00
|
|
|
let n_uncles = block.uncles.len();
|
2017-07-25 12:04:37 +02:00
|
|
|
|
2018-05-07 12:57:03 +02:00
|
|
|
let mut rewards = Vec::new();
|
|
|
|
|
2017-09-26 14:19:08 +02:00
|
|
|
// Bestow block reward
|
|
|
|
let result_block_reward = reward + reward.shr(5) * U256::from(n_uncles);
|
2018-05-07 12:57:03 +02:00
|
|
|
rewards.push((author, RewardKind::Author, result_block_reward));
|
2017-07-25 12:04:37 +02:00
|
|
|
|
2017-09-26 14:19:08 +02:00
|
|
|
// bestow uncle rewards.
|
2019-03-13 11:36:13 +01:00
|
|
|
for u in &block.uncles {
|
2017-09-26 14:19:08 +02:00
|
|
|
let uncle_author = u.author();
|
|
|
|
let result_uncle_reward = (reward * U256::from(8 + u.number() - number)).shr(3);
|
2018-08-29 17:17:18 +02:00
|
|
|
rewards.push((*uncle_author, RewardKind::uncle(number, u.number()), result_uncle_reward));
|
2017-07-25 12:04:37 +02:00
|
|
|
}
|
2017-09-26 14:19:08 +02:00
|
|
|
|
2018-05-07 12:57:03 +02:00
|
|
|
block_reward::apply_block_rewards(&rewards, block, &self.machine)
|
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-08-03 15:55:58 +02:00
|
|
|
Ok(())
|
2017-07-25 12:04:37 +02:00
|
|
|
}
|
2017-09-26 14:19:08 +02:00
|
|
|
|
2019-08-15 17:59:22 +02:00
|
|
|
fn snapshot_components(&self) -> Option<Box<dyn (SnapshotComponents)>> {
|
2017-09-26 14:19:08 +02:00
|
|
|
Some(Box::new(::snapshot::PowSnapshot::new(10000, 10000)))
|
|
|
|
}
|
2019-07-18 12:27:08 +02:00
|
|
|
|
|
|
|
fn params(&self) -> &CommonParams {
|
|
|
|
self.machine.params()
|
|
|
|
}
|
2015-12-20 21:45:43 +01:00
|
|
|
}
|