mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-16 00:21:05 +00:00
chore/error: remove from str conversion and add deprecation notificat… (#7472)
* chore/error: remove from str conversion and add deprecation notifications * fixup changes * fix test looking for gone ::Msg variant * another test fix * one is duplicate, the other is not, so duplicates reported are n-1 * darn spaces Co-authored-by: Andronik Ordian <write@reusable.software> * remove pointless doc comments of error variants without any value * low hanging fruits (for a tall person) * moar error type variants * avoid the storage modules for now They are in need of a refactor, and the pain is rather large removing all String error and DefaultError occurences. * chore remove pointless error generic * fix test for mocks, add a bunch of non_exhaustive * max line width * test fixes due to error changes * fin * error outputs... again * undo stderr adjustments * Update client/consensus/slots/src/lib.rs Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com> * remove closure clutter Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com> * more error types * introduce ApiError * extract Mock error * ApiError refactor * even more error types * the last for now * chore unused deps * another extraction * reduce should panic, due to extended error messages * error test happiness * shift error lines by one * doc tests * white space Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com> * Into -> From Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com> * remove pointless codec Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com> * avoid pointless self import Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com> Co-authored-by: Bernhard Schuster <bernhard@parity.io> Co-authored-by: Andronik Ordian <write@reusable.software> Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
6722a83ba6
commit
8c7d217091
@@ -46,3 +46,4 @@ prometheus-endpoint = { package = "substrate-prometheus-endpoint", version = "0.
|
||||
kvdb-memorydb = "0.7.0"
|
||||
sp-test-primitives = { version = "2.0.0", path = "../../primitives/test-primitives" }
|
||||
substrate-test-runtime = { version = "2.0.0", path = "../../test-utils/runtime" }
|
||||
thiserror = "1.0.21"
|
||||
|
||||
@@ -312,13 +312,21 @@ pub mod tests {
|
||||
use sp_test_primitives::{Block, Header, Extrinsic};
|
||||
use super::*;
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
#[error("Not implemented on test node")]
|
||||
struct MockError;
|
||||
|
||||
impl Into<ClientError> for MockError {
|
||||
fn into(self) -> ClientError {
|
||||
ClientError::Application(Box::new(self))
|
||||
}
|
||||
}
|
||||
|
||||
pub type OkCallFetcher = Mutex<Vec<u8>>;
|
||||
|
||||
fn not_implemented_in_tests<T, E>() -> Ready<Result<T, E>>
|
||||
where
|
||||
E: std::convert::From<&'static str>,
|
||||
fn not_implemented_in_tests<T>() -> Ready<Result<T, ClientError>>
|
||||
{
|
||||
futures::future::ready(Err("Not implemented on test node".into()))
|
||||
futures::future::ready(Err(MockError.into()))
|
||||
}
|
||||
|
||||
impl Fetcher<Block> for OkCallFetcher {
|
||||
|
||||
@@ -208,10 +208,7 @@ impl<A, B, Block, C> sp_consensus::Proposer<Block> for
|
||||
}));
|
||||
|
||||
async move {
|
||||
match rx.await {
|
||||
Ok(x) => x,
|
||||
Err(err) => Err(sp_blockchain::Error::Msg(err.to_string()))
|
||||
}
|
||||
rx.await?
|
||||
}.boxed()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -212,7 +212,7 @@ where
|
||||
&state,
|
||||
changes_trie_state.as_ref(),
|
||||
parent_hash,
|
||||
)?;
|
||||
).map_err(|e| sp_blockchain::Error::StorageChanges(e))?;
|
||||
|
||||
Ok(BuiltBlock {
|
||||
block: <Block as BlockT>::new(header, self.extrinsics),
|
||||
|
||||
@@ -25,35 +25,32 @@ pub type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
/// Error type for the CLI.
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
#[allow(missing_docs)]
|
||||
pub enum Error {
|
||||
/// Io error
|
||||
#[error(transparent)]
|
||||
Io(#[from] std::io::Error),
|
||||
/// Cli error
|
||||
|
||||
#[error(transparent)]
|
||||
Cli(#[from] structopt::clap::Error),
|
||||
/// Service error
|
||||
|
||||
#[error(transparent)]
|
||||
Service(#[from] sc_service::Error),
|
||||
/// Client error
|
||||
|
||||
#[error(transparent)]
|
||||
Client(#[from] sp_blockchain::Error),
|
||||
/// scale codec error
|
||||
|
||||
#[error(transparent)]
|
||||
Codec(#[from] parity_scale_codec::Error),
|
||||
/// Input error
|
||||
|
||||
#[error("Invalid input: {0}")]
|
||||
Input(String),
|
||||
/// Invalid listen multiaddress
|
||||
|
||||
#[error("Invalid listen multiaddress")]
|
||||
InvalidListenMultiaddress,
|
||||
/// Application specific error chain sequence forwarder.
|
||||
#[error(transparent)]
|
||||
Application(#[from] Box<dyn std::error::Error + Send + Sync + 'static>),
|
||||
/// URI error.
|
||||
|
||||
#[error("Invalid URI; expecting either a secret URI or a public URI.")]
|
||||
InvalidUri(crypto::PublicError),
|
||||
/// Signature length mismatch.
|
||||
|
||||
#[error("Signature has an invalid length. Read {read} bytes, expected {expected} bytes")]
|
||||
SignatureInvalidLength {
|
||||
/// Amount of signature bytes read.
|
||||
@@ -61,28 +58,28 @@ pub enum Error {
|
||||
/// Expected number of signature bytes.
|
||||
expected: usize,
|
||||
},
|
||||
/// Missing base path argument.
|
||||
|
||||
#[error("The base path is missing, please provide one")]
|
||||
MissingBasePath,
|
||||
/// Unknown key type specifier or missing key type specifier.
|
||||
|
||||
#[error("Unknown key type, must be a known 4-character sequence")]
|
||||
KeyTypeInvalid,
|
||||
/// Signature verification failed.
|
||||
|
||||
#[error("Signature verification failed")]
|
||||
SignatureInvalid,
|
||||
/// Storing a given key failed.
|
||||
|
||||
#[error("Key store operation failed")]
|
||||
KeyStoreOperation,
|
||||
/// An issue with the underlying key storage was encountered.
|
||||
|
||||
#[error("Key storage issue encountered")]
|
||||
KeyStorage(#[from] sc_keystore::Error),
|
||||
/// Bytes are not decodable when interpreted as hexadecimal string.
|
||||
#[error("Invalid hex base data")]
|
||||
|
||||
#[error("Invalid hexadecimal string data")]
|
||||
HexDataConversion(#[from] hex::FromHexError),
|
||||
/// Shortcut type to specify types on the fly, discouraged.
|
||||
#[deprecated = "Use `Forwarded` with an error type instead."]
|
||||
#[error("Other: {0}")]
|
||||
Other(String),
|
||||
|
||||
/// Application specific error chain sequence forwarder.
|
||||
#[error(transparent)]
|
||||
Application(#[from] Box<dyn std::error::Error + Send + Sync + 'static>),
|
||||
}
|
||||
|
||||
impl std::convert::From<&str> for Error {
|
||||
@@ -93,7 +90,7 @@ impl std::convert::From<&str> for Error {
|
||||
|
||||
impl std::convert::From<String> for Error {
|
||||
fn from(s: String) -> Error {
|
||||
Error::Input(s.to_string())
|
||||
Error::Input(s)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -31,7 +31,8 @@ sp-inherents = { version = "2.0.0", path = "../../../primitives/inherents" }
|
||||
futures = "0.3.4"
|
||||
futures-timer = "3.0.1"
|
||||
parking_lot = "0.10.0"
|
||||
log = "0.4.8"
|
||||
log = "0.4.11"
|
||||
thiserror = "1.0.21"
|
||||
|
||||
[dev-dependencies]
|
||||
substrate-test-runtime-client = { version = "2.0.0", path = "../../../test-utils/runtime/client" }
|
||||
|
||||
@@ -20,7 +20,8 @@
|
||||
//! time during which certain events can and/or must occur. This crate
|
||||
//! provides generic functionality for slots.
|
||||
|
||||
#![forbid(unsafe_code, missing_docs)]
|
||||
#![forbid(unsafe_code)]
|
||||
#![deny(missing_docs)]
|
||||
|
||||
mod slots;
|
||||
mod aux_schema;
|
||||
@@ -470,6 +471,15 @@ pub enum CheckedHeader<H, S> {
|
||||
Checked(H, S),
|
||||
}
|
||||
|
||||
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
#[allow(missing_docs)]
|
||||
pub enum Error<T> where T: SlotData + Clone + Debug + Send + Sync + 'static {
|
||||
#[error("Slot duration is invalid: {0:?}")]
|
||||
SlotDurationInvalid(SlotDuration<T>),
|
||||
}
|
||||
|
||||
/// A slot duration. Create with `get_or_compute`.
|
||||
// The internal member should stay private here to maintain invariants of
|
||||
// `get_or_compute`.
|
||||
@@ -494,7 +504,7 @@ impl<T: SlotData + Clone> SlotData for SlotDuration<T> {
|
||||
const SLOT_KEY: &'static [u8] = T::SLOT_KEY;
|
||||
}
|
||||
|
||||
impl<T: Clone> SlotDuration<T> {
|
||||
impl<T: Clone + Send + Sync + 'static> SlotDuration<T> {
|
||||
/// Either fetch the slot duration from disk or compute it from the
|
||||
/// genesis state.
|
||||
///
|
||||
@@ -532,10 +542,8 @@ impl<T: Clone> SlotDuration<T> {
|
||||
}
|
||||
}?;
|
||||
|
||||
if slot_duration.slot_duration() == 0 {
|
||||
return Err(sp_blockchain::Error::Msg(
|
||||
"Invalid value for slot_duration: the value must be greater than 0.".into(),
|
||||
))
|
||||
if slot_duration.slot_duration() == 0u64 {
|
||||
return Err(sp_blockchain::Error::Application(Box::new(Error::SlotDurationInvalid(slot_duration))))
|
||||
}
|
||||
|
||||
Ok(slot_duration)
|
||||
@@ -939,7 +947,7 @@ mod test {
|
||||
true, true, true, true,
|
||||
];
|
||||
|
||||
assert_eq!(backoff, expected);
|
||||
assert_eq!(backoff.as_slice(), &expected[..]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -891,9 +891,7 @@ impl<Block: BlockT> Backend<Block> {
|
||||
let is_archive_pruning = config.pruning.is_archive();
|
||||
let blockchain = BlockchainDb::new(db.clone())?;
|
||||
let meta = blockchain.meta.clone();
|
||||
let map_e = |e: sc_state_db::Error<io::Error>| sp_blockchain::Error::from(
|
||||
format!("State database error: {:?}", e)
|
||||
);
|
||||
let map_e = |e: sc_state_db::Error<io::Error>| sp_blockchain::Error::from_state_db(e);
|
||||
let state_db: StateDb<_, _> = StateDb::new(
|
||||
config.pruning.clone(),
|
||||
!config.source.supports_ref_counting(),
|
||||
@@ -1082,7 +1080,7 @@ impl<Block: BlockT> Backend<Block> {
|
||||
|
||||
trace!(target: "db", "Canonicalize block #{} ({:?})", new_canonical, hash);
|
||||
let commit = self.storage.state_db.canonicalize_block(&hash)
|
||||
.map_err(|e: sc_state_db::Error<io::Error>| sp_blockchain::Error::from(format!("State database error: {:?}", e)))?;
|
||||
.map_err(|e: sc_state_db::Error<io::Error>| sp_blockchain::Error::from_state_db(e))?;
|
||||
apply_state_commit(transaction, commit);
|
||||
};
|
||||
|
||||
@@ -1212,9 +1210,7 @@ impl<Block: BlockT> Backend<Block> {
|
||||
number_u64,
|
||||
&pending_block.header.parent_hash(),
|
||||
changeset,
|
||||
).map_err(|e: sc_state_db::Error<io::Error>|
|
||||
sp_blockchain::Error::from(format!("State database error: {:?}", e))
|
||||
)?;
|
||||
).map_err(|e: sc_state_db::Error<io::Error>| sp_blockchain::Error::from_state_db(e))?;
|
||||
apply_state_commit(&mut transaction, commit);
|
||||
|
||||
// Check if need to finalize. Genesis is always finalized instantly.
|
||||
@@ -1379,7 +1375,7 @@ impl<Block: BlockT> Backend<Block> {
|
||||
transaction.set_from_vec(columns::META, meta_keys::FINALIZED_BLOCK, lookup_key);
|
||||
|
||||
let commit = self.storage.state_db.canonicalize_block(&f_hash)
|
||||
.map_err(|e: sc_state_db::Error<io::Error>| sp_blockchain::Error::from(format!("State database error: {:?}", e)))?;
|
||||
.map_err(|e: sc_state_db::Error<io::Error>| sp_blockchain::Error::from_state_db(e))?;
|
||||
apply_state_commit(transaction, commit);
|
||||
|
||||
if !f_num.is_zero() {
|
||||
|
||||
@@ -14,7 +14,6 @@ readme = "README.md"
|
||||
targets = ["x86_64-unknown-linux-gnu"]
|
||||
|
||||
[dependencies]
|
||||
log = "0.4.8"
|
||||
derive_more = "0.99.2"
|
||||
parity-wasm = "0.41.0"
|
||||
codec = { package = "parity-scale-codec", version = "1.3.4" }
|
||||
@@ -22,8 +21,8 @@ wasmi = "0.6.2"
|
||||
sp-core = { version = "2.0.0", path = "../../../primitives/core" }
|
||||
sp-allocator = { version = "2.0.0", path = "../../../primitives/allocator" }
|
||||
sp-wasm-interface = { version = "2.0.0", path = "../../../primitives/wasm-interface" }
|
||||
sp-runtime-interface = { version = "2.0.0", path = "../../../primitives/runtime-interface" }
|
||||
sp-serializer = { version = "2.0.0", path = "../../../primitives/serializer" }
|
||||
thiserror = "1.0.21"
|
||||
|
||||
[features]
|
||||
default = []
|
||||
|
||||
@@ -25,92 +25,95 @@ use wasmi;
|
||||
pub type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
/// Error type.
|
||||
#[derive(Debug, derive_more::Display, derive_more::From)]
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
#[allow(missing_docs)]
|
||||
pub enum Error {
|
||||
/// Unserializable Data
|
||||
InvalidData(sp_serializer::Error),
|
||||
#[error("Unserializable data encountered")]
|
||||
InvalidData(#[from] sp_serializer::Error),
|
||||
/// Trap occurred during execution
|
||||
Trap(wasmi::Trap),
|
||||
#[error(transparent)]
|
||||
Trap(#[from] wasmi::Trap),
|
||||
/// Wasmi loading/instantiating error
|
||||
Wasmi(wasmi::Error),
|
||||
#[error(transparent)]
|
||||
Wasmi(#[from] wasmi::Error),
|
||||
/// Error in the API. Parameter is an error message.
|
||||
#[from(ignore)]
|
||||
#[error("API Error: {0}")]
|
||||
ApiError(String),
|
||||
/// Method is not found
|
||||
#[display(fmt="Method not found: '{}'", _0)]
|
||||
#[from(ignore)]
|
||||
#[error("Method not found: '{0}'")]
|
||||
MethodNotFound(String),
|
||||
/// Code is invalid (expected single byte)
|
||||
#[display(fmt="Invalid Code: {}", _0)]
|
||||
#[from(ignore)]
|
||||
#[error("Invalid Code: '{0}'")]
|
||||
InvalidCode(String),
|
||||
/// Could not get runtime version.
|
||||
#[display(fmt="On-chain runtime does not specify version")]
|
||||
#[error("On-chain runtime does not specify version")]
|
||||
VersionInvalid,
|
||||
/// Externalities have failed.
|
||||
#[display(fmt="Externalities error")]
|
||||
#[error("Externalities error")]
|
||||
Externalities,
|
||||
/// Invalid index.
|
||||
#[display(fmt="Invalid index provided")]
|
||||
#[error("Invalid index provided")]
|
||||
InvalidIndex,
|
||||
/// Invalid return type.
|
||||
#[display(fmt="Invalid type returned (should be u64)")]
|
||||
#[error("Invalid type returned (should be u64)")]
|
||||
InvalidReturn,
|
||||
/// Runtime failed.
|
||||
#[display(fmt="Runtime error")]
|
||||
#[error("Runtime error")]
|
||||
Runtime,
|
||||
/// Runtime panicked.
|
||||
#[display(fmt="Runtime panicked: {}", _0)]
|
||||
#[from(ignore)]
|
||||
#[error("Runtime panicked: {0}")]
|
||||
RuntimePanicked(String),
|
||||
/// Invalid memory reference.
|
||||
#[display(fmt="Invalid memory reference")]
|
||||
#[error("Invalid memory reference")]
|
||||
InvalidMemoryReference,
|
||||
/// The runtime must provide a global named `__heap_base` of type i32 for specifying where the
|
||||
/// allocator is allowed to place its data.
|
||||
#[display(fmt="The runtime doesn't provide a global named `__heap_base`")]
|
||||
#[error("The runtime doesn't provide a global named `__heap_base`")]
|
||||
HeapBaseNotFoundOrInvalid,
|
||||
/// The runtime WebAssembly module is not allowed to have the `start` function.
|
||||
#[display(fmt="The runtime has the `start` function")]
|
||||
#[error("The runtime has the `start` function")]
|
||||
RuntimeHasStartFn,
|
||||
/// Some other error occurred
|
||||
#[error("Other: {0}")]
|
||||
Other(String),
|
||||
/// Some error occurred in the allocator
|
||||
#[display(fmt="Error in allocator: {}", _0)]
|
||||
Allocator(sp_allocator::Error),
|
||||
#[error("Allocation Error")]
|
||||
Allocator(#[from] sp_allocator::Error),
|
||||
/// Execution of a host function failed.
|
||||
#[display(fmt="Host function {} execution failed with: {}", _0, _1)]
|
||||
#[error("Host function {0} execution failed with: {1}")]
|
||||
FunctionExecution(String, String),
|
||||
/// No table is present.
|
||||
///
|
||||
/// Call was requested that requires table but none was present in the instance.
|
||||
#[display(fmt="No table exported by wasm blob")]
|
||||
#[error("No table exported by wasm blob")]
|
||||
NoTable,
|
||||
/// No table entry is present.
|
||||
///
|
||||
/// Call was requested that requires specific entry in the table to be present.
|
||||
#[display(fmt="No table entry with index {} in wasm blob exported table", _0)]
|
||||
#[from(ignore)]
|
||||
#[error("No table entry with index {0} in wasm blob exported table")]
|
||||
NoTableEntryWithIndex(u32),
|
||||
/// Table entry is not a function.
|
||||
#[display(fmt="Table element with index {} is not a function in wasm blob exported table", _0)]
|
||||
#[from(ignore)]
|
||||
#[error("Table element with index {0} is not a function in wasm blob exported table")]
|
||||
TableElementIsNotAFunction(u32),
|
||||
/// Function in table is null and thus cannot be called.
|
||||
#[display(fmt="Table entry with index {} in wasm blob is null", _0)]
|
||||
#[from(ignore)]
|
||||
#[error("Table entry with index {0} in wasm blob is null")]
|
||||
FunctionRefIsNull(u32),
|
||||
}
|
||||
|
||||
impl std::error::Error for Error {
|
||||
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
|
||||
match self {
|
||||
Error::InvalidData(ref err) => Some(err),
|
||||
Error::Trap(ref err) => Some(err),
|
||||
Error::Wasmi(ref err) => Some(err),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
#[error(transparent)]
|
||||
RuntimeConstruction(#[from] WasmError),
|
||||
|
||||
#[error("Shared memory is not supported")]
|
||||
SharedMemUnsupported,
|
||||
|
||||
#[error("Imported globals are not supported yet")]
|
||||
ImportedGlobalsUnsupported,
|
||||
|
||||
#[error("initializer expression can have only up to 2 expressions in wasm 1.0")]
|
||||
InitializerHasTooManyExpressions,
|
||||
|
||||
#[error("Invalid initializer expression provided {0}")]
|
||||
InvalidInitializerExpression(String),
|
||||
}
|
||||
|
||||
impl wasmi::HostError for Error {}
|
||||
@@ -121,9 +124,9 @@ impl From<&'static str> for Error {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<WasmError> for Error {
|
||||
fn from(err: WasmError) -> Error {
|
||||
Error::Other(err.to_string())
|
||||
impl From<String> for Error {
|
||||
fn from(err: String) -> Error {
|
||||
Error::Other(err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -151,3 +154,5 @@ pub enum WasmError {
|
||||
/// Other error happenend.
|
||||
Other(String),
|
||||
}
|
||||
|
||||
impl std::error::Error for WasmError {}
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
//! A set of common definitions that are needed for defining execution engines.
|
||||
|
||||
#![warn(missing_docs)]
|
||||
#![deny(unused_crate_dependencies)]
|
||||
|
||||
pub mod error;
|
||||
pub mod sandbox;
|
||||
|
||||
@@ -87,15 +87,12 @@ impl DataSegmentsSnapshot {
|
||||
let init_expr = match segment.offset() {
|
||||
Some(offset) => offset.code(),
|
||||
// Return if the segment is passive
|
||||
None => return Err(Error::from("Shared memory is not supported".to_string())),
|
||||
None => return Err(Error::SharedMemUnsupported),
|
||||
};
|
||||
|
||||
// [op, End]
|
||||
if init_expr.len() != 2 {
|
||||
return Err(Error::from(
|
||||
"initializer expression can have only up to 2 expressions in wasm 1.0"
|
||||
.to_string(),
|
||||
));
|
||||
return Err(Error::InitializerHasTooManyExpressions);
|
||||
}
|
||||
let offset = match &init_expr[0] {
|
||||
Instruction::I32Const(v) => *v as u32,
|
||||
@@ -106,15 +103,10 @@ impl DataSegmentsSnapshot {
|
||||
// At the moment of writing the Substrate Runtime Interface does not provide
|
||||
// any globals. There is nothing that prevents us from supporting this
|
||||
// if/when we gain those.
|
||||
return Err(Error::from(
|
||||
"Imported globals are not supported yet".to_string(),
|
||||
));
|
||||
return Err(Error::ImportedGlobalsUnsupported);
|
||||
}
|
||||
insn => {
|
||||
return Err(Error::from(format!(
|
||||
"{:?} is not supported as initializer expression in wasm 1.0",
|
||||
insn
|
||||
)))
|
||||
return Err(Error::InvalidInitializerExpression(format!("{:?}", insn)))
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -523,7 +523,7 @@ fn offchain_http_should_work(wasm_method: WasmExecutionMethod) {
|
||||
|
||||
#[test_case(WasmExecutionMethod::Interpreted)]
|
||||
#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))]
|
||||
#[should_panic(expected = "Allocator ran out of space")]
|
||||
#[should_panic]
|
||||
fn should_trap_when_heap_exhausted(wasm_method: WasmExecutionMethod) {
|
||||
let mut ext = TestExternalities::default();
|
||||
|
||||
|
||||
@@ -276,7 +276,8 @@ pub fn check_execution_proof_with_make_header<Header, E, H, MakeNextHeader>(
|
||||
|
||||
// TODO: Remove when solved: https://github.com/paritytech/substrate/issues/5047
|
||||
let backend_runtime_code = sp_state_machine::backend::BackendRuntimeCode::new(&trie_backend);
|
||||
let runtime_code = backend_runtime_code.runtime_code()?;
|
||||
let runtime_code = backend_runtime_code.runtime_code()
|
||||
.map_err(|_e| ClientError::RuntimeCodeMissing)?;
|
||||
|
||||
execution_proof_check_on_trie_backend::<H, Header::Number, _, _>(
|
||||
&trie_backend,
|
||||
|
||||
@@ -239,7 +239,7 @@ impl<E, Block, H, S> FetchChecker<Block> for LightDataChecker<E, H, Block, S>
|
||||
convert_hash(request.header.state_root()),
|
||||
remote_proof,
|
||||
request.keys.iter(),
|
||||
).map_err(Into::into)
|
||||
).map_err(|e| ClientError::from(e))
|
||||
}
|
||||
|
||||
fn check_read_child_proof(
|
||||
@@ -249,14 +249,14 @@ impl<E, Block, H, S> FetchChecker<Block> for LightDataChecker<E, H, Block, S>
|
||||
) -> ClientResult<HashMap<Vec<u8>, Option<Vec<u8>>>> {
|
||||
let child_info = match ChildType::from_prefixed_key(&request.storage_key) {
|
||||
Some((ChildType::ParentKeyId, storage_key)) => ChildInfo::new_default(storage_key),
|
||||
None => return Err("Invalid child type".into()),
|
||||
None => return Err(ClientError::InvalidChildType),
|
||||
};
|
||||
read_child_proof_check::<H, _>(
|
||||
convert_hash(request.header.state_root()),
|
||||
remote_proof,
|
||||
&child_info,
|
||||
request.keys.iter(),
|
||||
).map_err(Into::into)
|
||||
).map_err(|e| ClientError::from(e))
|
||||
}
|
||||
|
||||
fn check_execution_proof(
|
||||
@@ -292,10 +292,10 @@ impl<E, Block, H, S> FetchChecker<Block> for LightDataChecker<E, H, Block, S>
|
||||
if *request.header.extrinsics_root() == extrinsics_root {
|
||||
Ok(body)
|
||||
} else {
|
||||
Err(format!("RemoteBodyRequest: invalid extrinsics root expected: {} but got {}",
|
||||
*request.header.extrinsics_root(),
|
||||
extrinsics_root,
|
||||
).into())
|
||||
Err(ClientError::ExtrinsicRootInvalid {
|
||||
received: request.header.extrinsics_root().to_string(),
|
||||
expected: extrinsics_root.to_string(),
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -628,7 +628,7 @@ where
|
||||
let prefixed_key = PrefixedStorageKey::new_ref(&request.storage_key);
|
||||
let child_info = match ChildType::from_prefixed_key(prefixed_key) {
|
||||
Some((ChildType::ParentKeyId, storage_key)) => Ok(ChildInfo::new_default(storage_key)),
|
||||
None => Err("Invalid child storage key".into()),
|
||||
None => Err(sp_blockchain::Error::InvalidChildStorageKey),
|
||||
};
|
||||
let proof = match child_info.and_then(|child_info| self.chain.read_child_proof(
|
||||
&BlockId::Hash(block),
|
||||
|
||||
@@ -51,6 +51,17 @@ pub struct OnDemand<B: BlockT> {
|
||||
requests_send: TracingUnboundedSender<light_client_handler::Request<B>>,
|
||||
}
|
||||
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
#[error("AlwaysBadChecker")]
|
||||
struct ErrorAlwaysBadChecker;
|
||||
|
||||
impl Into<ClientError> for ErrorAlwaysBadChecker {
|
||||
fn into(self) -> ClientError {
|
||||
ClientError::Application(Box::new(self))
|
||||
}
|
||||
}
|
||||
|
||||
/// Dummy implementation of `FetchChecker` that always assumes that responses are bad.
|
||||
///
|
||||
/// Considering that it is the responsibility of the client to build the fetcher, it can use this
|
||||
@@ -65,7 +76,7 @@ impl<Block: BlockT> FetchChecker<Block> for AlwaysBadChecker {
|
||||
_remote_header: Option<Block::Header>,
|
||||
_remote_proof: StorageProof,
|
||||
) -> Result<Block::Header, ClientError> {
|
||||
Err(ClientError::Msg("AlwaysBadChecker".into()))
|
||||
Err(ErrorAlwaysBadChecker.into())
|
||||
}
|
||||
|
||||
fn check_read_proof(
|
||||
@@ -73,7 +84,7 @@ impl<Block: BlockT> FetchChecker<Block> for AlwaysBadChecker {
|
||||
_request: &RemoteReadRequest<Block::Header>,
|
||||
_remote_proof: StorageProof,
|
||||
) -> Result<HashMap<Vec<u8>,Option<Vec<u8>>>, ClientError> {
|
||||
Err(ClientError::Msg("AlwaysBadChecker".into()))
|
||||
Err(ErrorAlwaysBadChecker.into())
|
||||
}
|
||||
|
||||
fn check_read_child_proof(
|
||||
@@ -81,7 +92,7 @@ impl<Block: BlockT> FetchChecker<Block> for AlwaysBadChecker {
|
||||
_request: &RemoteReadChildRequest<Block::Header>,
|
||||
_remote_proof: StorageProof,
|
||||
) -> Result<HashMap<Vec<u8>, Option<Vec<u8>>>, ClientError> {
|
||||
Err(ClientError::Msg("AlwaysBadChecker".into()))
|
||||
Err(ErrorAlwaysBadChecker.into())
|
||||
}
|
||||
|
||||
fn check_execution_proof(
|
||||
@@ -89,7 +100,7 @@ impl<Block: BlockT> FetchChecker<Block> for AlwaysBadChecker {
|
||||
_request: &RemoteCallRequest<Block::Header>,
|
||||
_remote_proof: StorageProof,
|
||||
) -> Result<Vec<u8>, ClientError> {
|
||||
Err(ClientError::Msg("AlwaysBadChecker".into()))
|
||||
Err(ErrorAlwaysBadChecker.into())
|
||||
}
|
||||
|
||||
fn check_changes_proof(
|
||||
@@ -97,7 +108,7 @@ impl<Block: BlockT> FetchChecker<Block> for AlwaysBadChecker {
|
||||
_request: &RemoteChangesRequest<Block::Header>,
|
||||
_remote_proof: ChangesProof<Block::Header>
|
||||
) -> Result<Vec<(NumberFor<Block>, u32)>, ClientError> {
|
||||
Err(ClientError::Msg("AlwaysBadChecker".into()))
|
||||
Err(ErrorAlwaysBadChecker.into())
|
||||
}
|
||||
|
||||
fn check_body_proof(
|
||||
@@ -105,7 +116,7 @@ impl<Block: BlockT> FetchChecker<Block> for AlwaysBadChecker {
|
||||
_request: &RemoteBodyRequest<Block::Header>,
|
||||
_body: Vec<Block::Extrinsic>
|
||||
) -> Result<Vec<Block::Extrinsic>, ClientError> {
|
||||
Err(ClientError::Msg("AlwaysBadChecker".into()))
|
||||
Err(ErrorAlwaysBadChecker.into())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -541,7 +541,7 @@ impl<BE, Block, Client> ChildStateBackend<Block, Client> for FullState<BE, Block
|
||||
.and_then(|block| {
|
||||
let child_info = match ChildType::from_prefixed_key(&storage_key) {
|
||||
Some((ChildType::ParentKeyId, storage_key)) => ChildInfo::new_default(storage_key),
|
||||
None => return Err("Invalid child storage key".into()),
|
||||
None => return Err(sp_blockchain::Error::InvalidChildStorageKey),
|
||||
};
|
||||
self.client.child_storage_keys(
|
||||
&BlockId::Hash(block),
|
||||
@@ -563,7 +563,7 @@ impl<BE, Block, Client> ChildStateBackend<Block, Client> for FullState<BE, Block
|
||||
.and_then(|block| {
|
||||
let child_info = match ChildType::from_prefixed_key(&storage_key) {
|
||||
Some((ChildType::ParentKeyId, storage_key)) => ChildInfo::new_default(storage_key),
|
||||
None => return Err("Invalid child storage key".into()),
|
||||
None => return Err(sp_blockchain::Error::InvalidChildStorageKey),
|
||||
};
|
||||
self.client.child_storage(
|
||||
&BlockId::Hash(block),
|
||||
@@ -585,7 +585,7 @@ impl<BE, Block, Client> ChildStateBackend<Block, Client> for FullState<BE, Block
|
||||
.and_then(|block| {
|
||||
let child_info = match ChildType::from_prefixed_key(&storage_key) {
|
||||
Some((ChildType::ParentKeyId, storage_key)) => ChildInfo::new_default(storage_key),
|
||||
None => return Err("Invalid child storage key".into()),
|
||||
None => return Err(sp_blockchain::Error::InvalidChildStorageKey),
|
||||
};
|
||||
self.client.child_storage_hash(
|
||||
&BlockId::Hash(block),
|
||||
|
||||
@@ -24,7 +24,7 @@ wasmtime = [
|
||||
test-helpers = []
|
||||
|
||||
[dependencies]
|
||||
derive_more = "0.99.2"
|
||||
thiserror = "1.0.21"
|
||||
futures01 = { package = "futures", version = "0.1.29" }
|
||||
futures = { version = "0.3.4", features = ["compat"] }
|
||||
jsonrpc-pubsub = "15.1"
|
||||
@@ -32,7 +32,7 @@ jsonrpc-core = "15.1"
|
||||
rand = "0.7.3"
|
||||
parking_lot = "0.10.0"
|
||||
lazy_static = "1.4.0"
|
||||
log = "0.4.8"
|
||||
log = "0.4.11"
|
||||
slog = { version = "2.5.2", features = ["nested-values"] }
|
||||
futures-timer = "3.0.1"
|
||||
wasm-timer = "0.2"
|
||||
|
||||
@@ -137,7 +137,9 @@ where
|
||||
)?;
|
||||
let state = self.backend.state_at(*id)?;
|
||||
let state_runtime_code = sp_state_machine::backend::BackendRuntimeCode::new(&state);
|
||||
let runtime_code = self.check_override(state_runtime_code.runtime_code()?, id)?;
|
||||
let runtime_code = state_runtime_code.runtime_code()
|
||||
.map_err(sp_blockchain::Error::RuntimeCode)?;
|
||||
let runtime_code = self.check_override(runtime_code, id)?;
|
||||
|
||||
let return_data = StateMachine::new(
|
||||
&state,
|
||||
@@ -211,7 +213,10 @@ where
|
||||
let state_runtime_code = sp_state_machine::backend::BackendRuntimeCode::new(&trie_state);
|
||||
// It is important to extract the runtime code here before we create the proof
|
||||
// recorder.
|
||||
let runtime_code = self.check_override(state_runtime_code.runtime_code()?, at)?;
|
||||
|
||||
let runtime_code = state_runtime_code.runtime_code()
|
||||
.map_err(sp_blockchain::Error::RuntimeCode)?;
|
||||
let runtime_code = self.check_override(runtime_code, at)?;
|
||||
|
||||
let backend = sp_state_machine::ProvingBackend::new_with_recorder(
|
||||
trie_state,
|
||||
@@ -236,7 +241,9 @@ where
|
||||
},
|
||||
None => {
|
||||
let state_runtime_code = sp_state_machine::backend::BackendRuntimeCode::new(&state);
|
||||
let runtime_code = self.check_override(state_runtime_code.runtime_code()?, at)?;
|
||||
let runtime_code = state_runtime_code.runtime_code()
|
||||
.map_err(sp_blockchain::Error::RuntimeCode)?;
|
||||
let runtime_code = self.check_override(runtime_code, at)?;
|
||||
|
||||
let mut state_machine = StateMachine::new(
|
||||
&state,
|
||||
@@ -273,7 +280,9 @@ where
|
||||
None,
|
||||
);
|
||||
let state_runtime_code = sp_state_machine::backend::BackendRuntimeCode::new(&state);
|
||||
self.executor.runtime_version(&mut ext, &state_runtime_code.runtime_code()?)
|
||||
let runtime_code = state_runtime_code.runtime_code()
|
||||
.map_err(sp_blockchain::Error::RuntimeCode)?;
|
||||
self.executor.runtime_version(&mut ext, &runtime_code)
|
||||
.map_err(|e| sp_blockchain::Error::VersionInvalid(format!("{:?}", e)).into())
|
||||
}
|
||||
|
||||
@@ -284,6 +293,9 @@ where
|
||||
method: &str,
|
||||
call_data: &[u8]
|
||||
) -> Result<(Vec<u8>, StorageProof), sp_blockchain::Error> {
|
||||
let state_runtime_code = sp_state_machine::backend::BackendRuntimeCode::new(trie_state);
|
||||
let runtime_code = state_runtime_code.runtime_code()
|
||||
.map_err(sp_blockchain::Error::RuntimeCode)?;
|
||||
sp_state_machine::prove_execution_on_trie_backend::<_, _, NumberFor<Block>, _, _>(
|
||||
trie_state,
|
||||
overlay,
|
||||
@@ -291,7 +303,7 @@ where
|
||||
self.spawn_handle.clone(),
|
||||
method,
|
||||
call_data,
|
||||
&sp_state_machine::backend::BackendRuntimeCode::new(trie_state).runtime_code()?,
|
||||
&runtime_code,
|
||||
)
|
||||
.map_err(Into::into)
|
||||
}
|
||||
|
||||
@@ -297,7 +297,8 @@ impl<B, E, Block, RA> Client<B, E, Block, RA> where
|
||||
config: ClientConfig,
|
||||
) -> sp_blockchain::Result<Self> {
|
||||
if backend.blockchain().header(BlockId::Number(Zero::zero()))?.is_none() {
|
||||
let genesis_storage = build_genesis_storage.build_storage()?;
|
||||
let genesis_storage = build_genesis_storage.build_storage()
|
||||
.map_err(sp_blockchain::Error::Storage)?;
|
||||
let mut op = backend.begin_operation()?;
|
||||
backend.begin_state_operation(&mut op, BlockId::Hash(Default::default()))?;
|
||||
let state_root = op.reset_storage(genesis_storage)?;
|
||||
@@ -880,7 +881,7 @@ impl<B, E, Block, RA> Client<B, E, Block, RA> where
|
||||
&state,
|
||||
changes_trie_state.as_ref(),
|
||||
*parent_hash,
|
||||
)?;
|
||||
).map_err(sp_blockchain::Error::Storage)?;
|
||||
|
||||
if import_block.header.state_root()
|
||||
!= &gen_storage_changes.transaction_storage_root
|
||||
|
||||
@@ -37,7 +37,8 @@
|
||||
//! needed must be provided in the given directory.
|
||||
//!
|
||||
use std::{
|
||||
fs, collections::{HashMap, hash_map::DefaultHasher}, path::Path,
|
||||
fs, collections::{HashMap, hash_map::DefaultHasher},
|
||||
path::{Path, PathBuf},
|
||||
hash::Hasher as _,
|
||||
};
|
||||
use sp_core::traits::FetchRuntimeCode;
|
||||
@@ -82,6 +83,29 @@ impl FetchRuntimeCode for WasmBlob {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
#[allow(missing_docs)]
|
||||
pub enum WasmOverrideError {
|
||||
#[error("Failed to get runtime version: {0}")]
|
||||
VersionInvalid(String),
|
||||
|
||||
#[error("WASM override IO error")]
|
||||
Io(PathBuf, #[source] std::io::Error),
|
||||
|
||||
#[error("Overwriting WASM requires a directory where local \
|
||||
WASM is stored. {} is not a directory", .0.display())]
|
||||
NotADirectory(PathBuf),
|
||||
|
||||
#[error("Duplicate WASM Runtimes found: \n{}\n", .0.join("\n") )]
|
||||
DuplicateRuntime(Vec<String>),
|
||||
}
|
||||
|
||||
impl From<WasmOverrideError> for sp_blockchain::Error {
|
||||
fn from(err: WasmOverrideError) -> Self {
|
||||
Self::Application(Box::new(err))
|
||||
}
|
||||
}
|
||||
|
||||
/// Scrapes WASM from a folder and returns WASM from that folder
|
||||
/// if the runtime spec version matches.
|
||||
#[derive(Clone, Debug)]
|
||||
@@ -119,16 +143,13 @@ where
|
||||
/// Scrapes a folder for WASM runtimes.
|
||||
/// Returns a hashmap of the runtime version and wasm runtime code.
|
||||
fn scrape_overrides(dir: &Path, executor: &E) -> Result<HashMap<u32, WasmBlob>> {
|
||||
|
||||
let handle_err = |e: std::io::Error | -> sp_blockchain::Error {
|
||||
sp_blockchain::Error::Msg(format!("{}", e.to_string()))
|
||||
WasmOverrideError::Io(dir.to_owned(), e).into()
|
||||
};
|
||||
|
||||
if !dir.is_dir() {
|
||||
return Err(sp_blockchain::Error::Msg(format!(
|
||||
"Overwriting WASM requires a directory where \
|
||||
local WASM is stored. {:?} is not a directory",
|
||||
dir,
|
||||
)));
|
||||
return Err(WasmOverrideError::NotADirectory(dir.to_owned()).into());
|
||||
}
|
||||
|
||||
let mut overrides = HashMap::new();
|
||||
@@ -149,9 +170,7 @@ where
|
||||
}
|
||||
|
||||
if !duplicates.is_empty() {
|
||||
let duplicate_file_list = duplicates.join("\n");
|
||||
let msg = format!("Duplicate WASM Runtimes found: \n{}\n", duplicate_file_list);
|
||||
return Err(sp_blockchain::Error::Msg(msg));
|
||||
return Err(WasmOverrideError::DuplicateRuntime(duplicates).into());
|
||||
}
|
||||
|
||||
Ok(overrides)
|
||||
@@ -164,7 +183,7 @@ where
|
||||
) -> Result<RuntimeVersion> {
|
||||
let mut ext = BasicExternalities::default();
|
||||
executor.runtime_version(&mut ext, &code.runtime_code(heap_pages))
|
||||
.map_err(|e| sp_blockchain::Error::VersionInvalid(format!("{:?}", e)).into())
|
||||
.map_err(|e| WasmOverrideError::VersionInvalid(format!("{:?}", e)).into())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -236,14 +255,10 @@ mod tests {
|
||||
let scraped = WasmOverride::scrape_overrides(dir, exec);
|
||||
|
||||
match scraped {
|
||||
Err(e) => {
|
||||
match e {
|
||||
sp_blockchain::Error::Msg(msg) => {
|
||||
let is_match = msg
|
||||
.matches("Duplicate WASM Runtimes found")
|
||||
.map(ToString::to_string)
|
||||
.collect::<Vec<String>>();
|
||||
assert!(is_match.len() >= 1)
|
||||
Err(sp_blockchain::Error::Application(e)) => {
|
||||
match e.downcast_ref::<WasmOverrideError>() {
|
||||
Some(WasmOverrideError::DuplicateRuntime(duplicates)) => {
|
||||
assert_eq!(duplicates.len(), 1);
|
||||
},
|
||||
_ => panic!("Test should end with Msg Error Variant")
|
||||
}
|
||||
|
||||
@@ -27,25 +27,38 @@ use sp_blockchain;
|
||||
pub type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
/// Service errors.
|
||||
#[derive(Debug, derive_more::Display, derive_more::From)]
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
#[allow(missing_docs)]
|
||||
#[non_exhaustive]
|
||||
pub enum Error {
|
||||
/// Client error.
|
||||
Client(sp_blockchain::Error),
|
||||
/// IO error.
|
||||
Io(std::io::Error),
|
||||
/// Consensus error.
|
||||
Consensus(sp_consensus::Error),
|
||||
/// Network error.
|
||||
Network(sc_network::error::Error),
|
||||
/// Keystore error.
|
||||
Keystore(sc_keystore::Error),
|
||||
/// Best chain selection strategy is missing.
|
||||
#[display(fmt="Best chain selection strategy (SelectChain) is not provided.")]
|
||||
#[error(transparent)]
|
||||
Client(#[from] sp_blockchain::Error),
|
||||
|
||||
#[error(transparent)]
|
||||
Io(#[from] std::io::Error),
|
||||
|
||||
#[error(transparent)]
|
||||
Consensus(#[from] sp_consensus::Error),
|
||||
|
||||
#[error(transparent)]
|
||||
Network(#[from] sc_network::error::Error),
|
||||
|
||||
#[error(transparent)]
|
||||
Keystore(#[from] sc_keystore::Error),
|
||||
|
||||
#[error("Best chain selection strategy (SelectChain) is not provided.")]
|
||||
SelectChainRequired,
|
||||
/// Tasks executor is missing.
|
||||
#[display(fmt="Tasks executor hasn't been provided.")]
|
||||
|
||||
#[error("Tasks executor hasn't been provided.")]
|
||||
TaskExecutorRequired,
|
||||
/// Other error.
|
||||
|
||||
#[error("Prometheus metrics error")]
|
||||
Prometheus(#[from] prometheus_endpoint::PrometheusError),
|
||||
|
||||
#[error("Application")]
|
||||
Application(#[from] Box<dyn std::error::Error + Send + Sync + 'static>),
|
||||
|
||||
#[error("Other: {0}")]
|
||||
Other(String),
|
||||
}
|
||||
|
||||
@@ -55,21 +68,8 @@ impl<'a> From<&'a str> for Error {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<prometheus_endpoint::PrometheusError> for Error {
|
||||
fn from(e: prometheus_endpoint::PrometheusError) -> Self {
|
||||
Error::Other(format!("Prometheus error: {}", e))
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for Error {
|
||||
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
|
||||
match self {
|
||||
Error::Client(ref err) => Some(err),
|
||||
Error::Io(ref err) => Some(err),
|
||||
Error::Consensus(ref err) => Some(err),
|
||||
Error::Network(ref err) => Some(err),
|
||||
Error::Keystore(ref err) => Some(err),
|
||||
_ => None,
|
||||
}
|
||||
impl<'a> From<String> for Error {
|
||||
fn from(s: String) -> Self {
|
||||
Error::Other(s)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,8 +13,9 @@ readme = "README.md"
|
||||
targets = ["x86_64-unknown-linux-gnu"]
|
||||
|
||||
[dependencies]
|
||||
thiserror = "1.0.21"
|
||||
parking_lot = "0.10.0"
|
||||
log = "0.4.8"
|
||||
log = "0.4.11"
|
||||
sc-client-api = { version = "2.0.0", path = "../api" }
|
||||
sp-core = { version = "2.0.0", path = "../../primitives/core" }
|
||||
codec = { package = "parity-scale-codec", version = "1.3.4", features = ["derive"] }
|
||||
|
||||
@@ -13,6 +13,7 @@ readme = "README.md"
|
||||
targets = ["x86_64-unknown-linux-gnu"]
|
||||
|
||||
[dependencies]
|
||||
thiserror = "1.0.21"
|
||||
jsonrpc-core = "15.0"
|
||||
jsonrpc-core-client = "15.0"
|
||||
jsonrpc-derive = "15.0"
|
||||
|
||||
@@ -17,6 +17,8 @@
|
||||
//! A RPC handler to create sync states for light clients.
|
||||
//! Currently only usable with BABE + GRANDPA.
|
||||
|
||||
#![deny(unused_crate_dependencies)]
|
||||
|
||||
use sp_runtime::traits::{Block as BlockT, NumberFor};
|
||||
use sp_blockchain::HeaderBackend;
|
||||
use std::sync::Arc;
|
||||
@@ -28,12 +30,27 @@ type SharedAuthoritySet<TBl> =
|
||||
sc_finality_grandpa::SharedAuthoritySet<<TBl as BlockT>::Hash, NumberFor<TBl>>;
|
||||
type SharedEpochChanges<TBl> = sc_consensus_epochs::SharedEpochChanges<TBl, sc_consensus_babe::Epoch>;
|
||||
|
||||
struct Error(sp_blockchain::Error);
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
#[allow(missing_docs)]
|
||||
enum Error<Block: BlockT> {
|
||||
#[error(transparent)]
|
||||
Blockchain(#[from] sp_blockchain::Error),
|
||||
|
||||
#[error("Failed to load the block weight for block {0:?}")]
|
||||
LoadingBlockWeightFailed(<Block as BlockT>::Hash),
|
||||
|
||||
impl From<Error> for jsonrpc_core::Error {
|
||||
fn from(error: Error) -> Self {
|
||||
#[error("JsonRpc error: {0}")]
|
||||
JsonRpc(String),
|
||||
}
|
||||
|
||||
impl<Block: BlockT> From<Error<Block>> for jsonrpc_core::Error {
|
||||
fn from(error: Error<Block>) -> Self {
|
||||
let message = match error {
|
||||
Error::JsonRpc(s) => s,
|
||||
_ => error.to_string(),
|
||||
};
|
||||
jsonrpc_core::Error {
|
||||
message: error.0.to_string(),
|
||||
message,
|
||||
code: jsonrpc_core::ErrorCode::ServerError(1),
|
||||
data: None,
|
||||
}
|
||||
@@ -76,20 +93,16 @@ impl<TBl, TCl> SyncStateRpcHandler<TBl, TCl>
|
||||
}
|
||||
}
|
||||
|
||||
fn build_sync_state(&self) -> Result<sc_chain_spec::LightSyncState<TBl>, sp_blockchain::Error> {
|
||||
fn build_sync_state(&self) -> Result<sc_chain_spec::LightSyncState<TBl>, Error<TBl>> {
|
||||
let finalized_hash = self.client.info().finalized_hash;
|
||||
let finalized_header = self.client.header(BlockId::Hash(finalized_hash))?
|
||||
.ok_or_else(|| sp_blockchain::Error::Msg(
|
||||
format!("Failed to get the header for block {:?}", finalized_hash)
|
||||
))?;
|
||||
.ok_or_else(|| sp_blockchain::Error::MissingHeader(finalized_hash.to_string()))?;
|
||||
|
||||
let finalized_block_weight = sc_consensus_babe::aux_schema::load_block_weight(
|
||||
&*self.client,
|
||||
finalized_hash,
|
||||
)?
|
||||
.ok_or_else(|| sp_blockchain::Error::Msg(
|
||||
format!("Failed to load the block weight for block {:?}", finalized_hash)
|
||||
))?;
|
||||
&*self.client,
|
||||
finalized_hash,
|
||||
)?
|
||||
.ok_or_else(|| Error::LoadingBlockWeightFailed(finalized_hash))?;
|
||||
|
||||
Ok(sc_chain_spec::LightSyncState {
|
||||
finalized_block_header: finalized_header,
|
||||
@@ -114,15 +127,16 @@ impl<TBl, TCl> SyncStateRpcApi for SyncStateRpcHandler<TBl, TCl>
|
||||
|
||||
let mut chain_spec = self.chain_spec.cloned_box();
|
||||
|
||||
let sync_state = self.build_sync_state().map_err(Error)?;
|
||||
let sync_state = self.build_sync_state()
|
||||
.map_err(map_error::<TBl,Error<TBl>>)?;
|
||||
|
||||
chain_spec.set_light_sync_state(sync_state.to_serializable());
|
||||
let string = chain_spec.as_json(raw).map_err(map_error)?;
|
||||
let string = chain_spec.as_json(raw).map_err(map_error::<TBl,_>)?;
|
||||
|
||||
serde_json::from_str(&string).map_err(|err| map_error(err.to_string()))
|
||||
serde_json::from_str(&string).map_err(|err| map_error::<TBl,_>(err))
|
||||
}
|
||||
}
|
||||
|
||||
fn map_error(error: String) -> jsonrpc_core::Error {
|
||||
Error(sp_blockchain::Error::Msg(error)).into()
|
||||
fn map_error<Block: BlockT, S: ToString>(error: S) -> jsonrpc_core::Error {
|
||||
Error::<Block>::JsonRpc(error.to_string()).into()
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ targets = ["x86_64-unknown-linux-gnu"]
|
||||
|
||||
[dependencies]
|
||||
codec = { package = "parity-scale-codec", version = "1.3.4" }
|
||||
derive_more = "0.99.2"
|
||||
thiserror = "1.0.21"
|
||||
futures = { version = "0.3.1", features = ["compat"] }
|
||||
futures-diagnose = "1.0"
|
||||
intervalier = "0.4.0"
|
||||
|
||||
@@ -14,6 +14,7 @@ targets = ["x86_64-unknown-linux-gnu"]
|
||||
|
||||
[dependencies]
|
||||
derive_more = "0.99.2"
|
||||
thiserror = "1.0.21"
|
||||
futures = "0.3.4"
|
||||
log = "0.4.8"
|
||||
parking_lot = "0.10.0"
|
||||
|
||||
@@ -26,28 +26,29 @@ use sp_runtime::transaction_validity::{
|
||||
pub type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
/// Transaction pool error type.
|
||||
#[derive(Debug, derive_more::Display, derive_more::From)]
|
||||
#[derive(Debug, thiserror::Error, derive_more::From)]
|
||||
#[allow(missing_docs)]
|
||||
pub enum Error {
|
||||
/// Transaction is not verifiable yet, but might be in the future.
|
||||
#[display(fmt="Unknown transaction validity: {:?}", _0)]
|
||||
#[error("Unknown transaction validity: {0:?}")]
|
||||
UnknownTransaction(UnknownTransaction),
|
||||
/// Transaction is invalid.
|
||||
#[display(fmt="Invalid transaction validity: {:?}", _0)]
|
||||
#[error("Invalid transaction validity: {0:?}")]
|
||||
InvalidTransaction(InvalidTransaction),
|
||||
/// The transaction validity returned no "provides" tag.
|
||||
///
|
||||
/// Such transactions are not accepted to the pool, since we use those tags
|
||||
/// to define identity of transactions (occupance of the same "slot").
|
||||
#[display(fmt="The transaction does not provide any tags, so the pool can't identify it.")]
|
||||
#[error("The transaction does not provide any tags, so the pool can't identify it.")]
|
||||
NoTagsProvided,
|
||||
/// The transaction is temporarily banned.
|
||||
#[display(fmt="Temporarily Banned")]
|
||||
|
||||
#[error("Temporarily Banned")]
|
||||
TemporarilyBanned,
|
||||
/// The transaction is already in the pool.
|
||||
#[display(fmt="[{:?}] Already imported", _0)]
|
||||
#[error("[{0:?}] Already imported")]
|
||||
AlreadyImported(Box<dyn std::any::Any + Send>),
|
||||
/// The transaction cannot be imported cause it's a replacement and has too low priority.
|
||||
#[display(fmt="Too low priority ({} > {})", old, new)]
|
||||
#[error("Too low priority ({0} > {1})", old, new)]
|
||||
TooLowPriority {
|
||||
/// Transaction already in the pool.
|
||||
old: Priority,
|
||||
@@ -55,17 +56,16 @@ pub enum Error {
|
||||
new: Priority
|
||||
},
|
||||
/// Deps cycle detected and we couldn't import transaction.
|
||||
#[display(fmt="Cycle Detected")]
|
||||
#[error("Cycle Detected")]
|
||||
CycleDetected,
|
||||
/// Transaction was dropped immediately after it got inserted.
|
||||
#[display(fmt="Transaction couldn't enter the pool because of the limit.")]
|
||||
#[error("Transaction couldn't enter the pool because of the limit.")]
|
||||
ImmediatelyDropped,
|
||||
/// Invalid block id.
|
||||
#[error("Invlaid block id: {0}")]
|
||||
InvalidBlockId(String),
|
||||
}
|
||||
|
||||
impl std::error::Error for Error {}
|
||||
|
||||
/// Transaction pool error conversion.
|
||||
pub trait IntoPoolError: ::std::error::Error + Send + Sized {
|
||||
/// Try to extract original `Error`
|
||||
|
||||
@@ -24,30 +24,22 @@ use sp_transaction_pool::error::Error as TxPoolError;
|
||||
pub type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
/// Transaction pool error type.
|
||||
#[derive(Debug, derive_more::Display, derive_more::From)]
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
#[allow(missing_docs)]
|
||||
pub enum Error {
|
||||
/// Pool error.
|
||||
Pool(TxPoolError),
|
||||
/// Blockchain error.
|
||||
Blockchain(sp_blockchain::Error),
|
||||
/// Error while converting a `BlockId`.
|
||||
#[from(ignore)]
|
||||
#[error("Transaction pool error")]
|
||||
Pool(#[from] TxPoolError),
|
||||
|
||||
#[error("Blockchain error")]
|
||||
Blockchain(#[from] sp_blockchain::Error),
|
||||
|
||||
#[error("Block conversion error: {0}")]
|
||||
BlockIdConversion(String),
|
||||
/// Error while calling the runtime api.
|
||||
#[from(ignore)]
|
||||
|
||||
#[error("Runtime error: {0}")]
|
||||
RuntimeApi(String),
|
||||
}
|
||||
|
||||
impl std::error::Error for Error {
|
||||
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
|
||||
match self {
|
||||
Error::Pool(ref err) => Some(err),
|
||||
Error::Blockchain(ref err) => Some(err),
|
||||
Error::BlockIdConversion(_) => None,
|
||||
Error::RuntimeApi(_) => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl sp_transaction_pool::error::IntoPoolError for Error {
|
||||
fn into_pool_error(self) -> std::result::Result<TxPoolError, Self> {
|
||||
|
||||
Reference in New Issue
Block a user