Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Manual Seal (#4143)
Browse files Browse the repository at this point in the history
* instant/manual seal

unbounded queues are evil

Apply suggestions from code review

Co-Authored-By: Robert Habermeier <rphmeier@gmail.com>

add fork tests, docs, remove todos

moar docs

Update client/consensus/manual-seal/src/rpc.rs

Co-Authored-By: Robert Habermeier <rphmeier@gmail.com>

remove unbound generic, parameter, docs, deps, code style changes

Apply suggestions from code review

Co-Authored-By: Tomasz Drwięga <tomusdrw@users.noreply.github.com>

code style chnges

remove unused deps, remove dep renames, check if block is empty before importing, use ? for error propagation

fix tests

log errors for instant seal

use debug

code style changes, updated copyright dates

use txpool::Pool instead of BasicPool, code style changes

fixed tests

* fix tests

* requested changes from review

* check inherents len

* rebase
  • Loading branch information
seunlanlege committed Jan 27, 2020
1 parent d56db2b commit 03ffa5a
Show file tree
Hide file tree
Showing 18 changed files with 1,521 additions and 469 deletions.
Empty file modified .maintain/rename-crates-for-2.0.sh
100644 → 100755
Empty file.
636 changes: 332 additions & 304 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ members = [
"client/cli",
"client/consensus/aura",
"client/consensus/babe",
"client/consensus/manual-seal",
"client/consensus/pow",
"client/consensus/slots",
"client/consensus/uncles",
Expand Down
2 changes: 1 addition & 1 deletion bin/node/cli/src/browser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ async fn start_inner(wasm_ext: Transport) -> Result<Client, Box<dyn std::error::

info!("Substrate browser node");
info!(" version {}", config.full_version());
info!(" by Parity Technologies, 2017-2019");
info!(" by Parity Technologies, 2017-2020");
info!("Chain specification: {}", config.chain_spec.name());
info!("Node name: {}", config.name);
info!("Roles: {:?}", config.roles);
Expand Down
2 changes: 1 addition & 1 deletion bin/node/cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ pub fn run<I, T, E>(args: I, exit: E, version: sc_cli::VersionInfo) -> error::Re
|exit, _cli_args, _custom_args, mut config: Config<_, _>| {
info!("{}", version.name);
info!(" version {}", config.full_version());
info!(" by Parity Technologies, 2017-2019");
info!(" by Parity Technologies, 2017-2020");
info!("Chain specification: {}", config.chain_spec.name());
info!("Node name: {}", config.name);
info!("Roles: {}", display_role(&config));
Expand Down
32 changes: 32 additions & 0 deletions client/consensus/manual-seal/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
[package]
name = "sc-consensus-manual-seal"
version = "0.8.0"
authors = ["Parity Technologies <admin@parity.io>"]
description = "Manual sealing engine for Substrate"
edition = "2018"

[dependencies]
derive_more = "0.99.2"
futures = "0.3.1"
jsonrpc-core = "14.0.5"
jsonrpc-core-client = "14.0.5"
jsonrpc-derive = "14.0.5"
log = "0.4.8"
parking_lot = "0.10"
serde = { version = "1.0", features=["derive"] }

sc-client = { path = "../../../client" }
sc-client-api = { path = "../../../client/api" }
sc-transaction-pool = { path = "../../transaction-pool", features = ["test-helpers"] }
sp-blockchain = { path = "../../../primitives/blockchain" }
sp-consensus = { package = "sp-consensus", path = "../../../primitives/consensus/common" }
sp-inherents = { path = "../../../primitives/inherents" }
sp-runtime = { path = "../../../primitives/runtime" }
sp-transaction-pool = { path = "../../../primitives/transaction-pool" }

[dev-dependencies]
sc-basic-authorship = { path = "../../basic-authorship" }
substrate-test-runtime-client = { path = "../../../test-utils/runtime/client" }
tokio = { version = "0.2", features = ["rt-core", "macros"] }
env_logger = "0.7.0"
tempfile = "3.1.0"
98 changes: 98 additions & 0 deletions client/consensus/manual-seal/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// Copyright 2020 Parity Technologies (UK) Ltd.
// This file is part of Substrate.

// Substrate 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.

// Substrate 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 Substrate. If not, see <http://www.gnu.org/licenses/>.

//! A manual sealing engine: the engine listens for rpc calls to seal blocks and create forks.
//! This is suitable for a testing environment.
use sp_consensus::{Error as ConsensusError, ImportResult};
use sp_blockchain::Error as BlockchainError;
use sp_inherents::Error as InherentsError;
use futures::channel::{oneshot, mpsc::SendError};

/// Error code for rpc
mod codes {
pub const SERVER_SHUTTING_DOWN: i64 = 10_000;
pub const BLOCK_IMPORT_FAILED: i64 = 11_000;
pub const EMPTY_TRANSACTION_POOL: i64 = 12_000;
pub const BLOCK_NOT_FOUND: i64 = 13_000;
pub const CONSENSUS_ERROR: i64 = 14_000;
pub const INHERENTS_ERROR: i64 = 15_000;
pub const BLOCKCHAIN_ERROR: i64 = 16_000;
pub const UNKNOWN_ERROR: i64 = 20_000;
}

/// errors encountered by background block authorship task
#[derive(Debug, derive_more::Display, derive_more::From)]
pub enum Error {
/// An error occurred while importing the block
#[display(fmt = "Block import failed: {:?}", _0)]
BlockImportError(ImportResult),
/// Transaction pool is empty, cannot create a block
#[display(fmt = "Transaction pool is empty, set create_empty to true,\
if you want to create empty blocks")]
EmptyTransactionPool,
/// encountered during creation of Proposer.
#[display(fmt = "Consensus Error: {}", _0)]
ConsensusError(ConsensusError),
/// Failed to create Inherents data
#[display(fmt = "Inherents Error: {}", _0)]
InherentError(InherentsError),
/// error encountered during finalization
#[display(fmt = "Finalization Error: {}", _0)]
BlockchainError(BlockchainError),
/// Supplied parent_hash doesn't exist in chain
#[display(fmt = "Supplied parent_hash: {} doesn't exist in chain", _0)]
#[from(ignore)]
BlockNotFound(String),
/// Some string error
#[display(fmt = "{}", _0)]
#[from(ignore)]
StringError(String),
///send error
#[display(fmt = "Consensus process is terminating")]
Canceled(oneshot::Canceled),
///send error
#[display(fmt = "Consensus process is terminating")]
SendError(SendError),
/// Some other error.
#[display(fmt="Other error: {}", _0)]
Other(Box<dyn std::error::Error + Send>),
}

impl Error {
fn to_code(&self) -> i64 {
use Error::*;
match self {
BlockImportError(_) => codes::BLOCK_IMPORT_FAILED,
BlockNotFound(_) => codes::BLOCK_NOT_FOUND,
EmptyTransactionPool => codes::EMPTY_TRANSACTION_POOL,
ConsensusError(_) => codes::CONSENSUS_ERROR,
InherentError(_) => codes::INHERENTS_ERROR,
BlockchainError(_) => codes::BLOCKCHAIN_ERROR,
SendError(_) | Canceled(_) => codes::SERVER_SHUTTING_DOWN,
_ => codes::UNKNOWN_ERROR
}
}
}

impl std::convert::From<Error> for jsonrpc_core::Error {
fn from(error: Error) -> Self {
jsonrpc_core::Error {
code: jsonrpc_core::ErrorCode::ServerError(error.to_code()),
message: format!("{}", error),
data: None
}
}
}
64 changes: 64 additions & 0 deletions client/consensus/manual-seal/src/finalize_block.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright 2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.

// Substrate 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.

// Substrate 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 Substrate. If not, see <http://www.gnu.org/licenses/>.

//! Block finalization utilities

use crate::rpc;
use sp_runtime::{
Justification,
traits::Block as BlockT,
generic::BlockId,
};
use std::sync::Arc;
use sc_client_api::backend::Backend as ClientBackend;

/// params for block finalization.
pub struct FinalizeBlockParams<B: BlockT, CB> {
/// hash of the block
pub hash: <B as BlockT>::Hash,
/// sender to report errors/success to the rpc.
pub sender: rpc::Sender<()>,
/// finalization justification
pub justification: Option<Justification>,
/// client backend
pub backend: Arc<CB>,
}

/// finalizes a block in the backend with the given params.
pub async fn finalize_block<B, CB>(params: FinalizeBlockParams<B, CB>)
where
B: BlockT,
CB: ClientBackend<B>,
{
let FinalizeBlockParams {
hash,
mut sender,
justification,
backend: back_end,
..
} = params;

match back_end.finalize_block(BlockId::Hash(hash), justification) {
Err(e) => {
log::warn!("Failed to finalize block {:?}", e);
rpc::send_result(&mut sender, Err(e.into()))
}
Ok(()) => {
log::info!("Successfully finalized block: {}", hash);
rpc::send_result(&mut sender, Ok(()))
}
}
}
Loading

0 comments on commit 03ffa5a

Please sign in to comment.