Skip to content

Commit

Permalink
Introduce folders in node-api to scale down files (#596)
Browse files Browse the repository at this point in the history
* move metadata to its own folder

* create event folder

* move error to separate folder

* fix alloc
  • Loading branch information
haerdib authored Jun 22, 2023
1 parent b6a998e commit 19c6397
Show file tree
Hide file tree
Showing 10 changed files with 446 additions and 366 deletions.
123 changes: 17 additions & 106 deletions node-api/src/error.rs → node-api/src/error/dispatch_error.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
// This file was taken from subxt (Parity Technologies (UK))
// https://github.com/paritytech/subxt/
// And was adapted by Supercomputing Systems AG and Integritee AG.
//
// Copyright 2019-2022 Parity Technologies (UK) Ltd, Supercomputing Systems AG and Integritee AG.
// This file is licensed as Apache-2.0
// see LICENSE for license details.
/*
Copyright 2021 Integritee AG and Supercomputing Systems AG
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

//! General node-api Error and Substrate DispatchError implementation.
use crate::{
alloc::{
borrow::Cow,
format,
string::{String, ToString},
vec::Vec,
},
metadata::Metadata,
use crate::metadata::Metadata;
use alloc::{
borrow::Cow,
string::{String, ToString},
vec::Vec,
};

use codec::{Decode, Encode};
use core::fmt::Debug;
use derive_more::From;
Expand All @@ -32,45 +31,6 @@ pub use crate::{
pub use sp_core::crypto::SecretStringError;
pub use sp_runtime::transaction_validity::TransactionValidityError;

/// The underlying error enum, generic over the type held by the `Runtime`
/// variant. Prefer to use the [`Error<E>`] and [`Error`] aliases over
/// using this type directly.
#[derive(Debug, From)]
pub enum Error {
/// Codec error.
Codec(codec::Error),
/// Serde serialization error
Serialization(serde_json::error::Error),
/// Secret string error.
SecretString(SecretStringError),
/// Extrinsic validity error
Invalid(TransactionValidityError),
/// Invalid metadata error
InvalidMetadata(InvalidMetadataError),
/// Invalid metadata error
Metadata(MetadataError),
/// Runtime error.
Runtime(DispatchError),
/// Error decoding to a [`crate::dynamic::Value`].
DecodeValue(DecodeError),
/// Error encoding from a [`crate::dynamic::Value`].
EncodeValue(EncodeError),
/// Transaction progress error.
Transaction(TransactionError),
/// Block related error.
Block(BlockError),
/// An error encoding a storage address.
StorageAddress(StorageAddressError),
/// Other error.
Other(String),
}

impl From<&str> for Error {
fn from(error: &str) -> Self {
Error::Other(error.into())
}
}

/// An error dispatching a transaction. See Substrate DispatchError
//https://github.com/paritytech/substrate/blob/890451221db37176e13cb1a306246f02de80590a/primitives/runtime/src/lib.rs#L524
#[derive(Debug, From)]
Expand Down Expand Up @@ -295,32 +255,6 @@ pub enum TransactionalError {
NoLayer,
}

/// Block error
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum BlockError {
/// The block
BlockHashNotFound(String),
}

impl BlockError {
/// Produce an error that a block with the given hash cannot be found.
pub fn block_hash_not_found(hash: impl AsRef<[u8]>) -> BlockError {
let hash = format!("0x{}", hex::encode(hash));
BlockError::BlockHashNotFound(hash)
}
}

/// Transaction error.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum TransactionError {
/// The finality subscription expired (after ~512 blocks we give up if the
/// block hasn't yet been finalized).
FinalitySubscriptionTimeout,
/// The block hash that the transaction was added to could not be found.
/// This is probably because the block was retracted before being finalized.
BlockHashNotFound,
}

/// Details about a module error that has occurred.
#[derive(Clone, Debug)]
pub struct ModuleError {
Expand Down Expand Up @@ -352,26 +286,3 @@ impl ModuleErrorData {
self.error[0]
}
}

/// Something went wrong trying to encode a storage address.
#[derive(Clone, Debug)]
pub enum StorageAddressError {
/// Storage map type must be a composite type.
MapTypeMustBeTuple,
/// Storage lookup does not have the expected number of keys.
WrongNumberOfKeys {
/// The actual number of keys needed, based on the metadata.
actual: usize,
/// The number of keys provided in the storage address.
expected: usize,
},
/// Storage lookup requires a type that wasn't found in the metadata.
TypeNotFound(u32),
/// This storage entry in the metadata does not have the correct number of hashers to fields.
WrongNumberOfHashers {
/// The number of hashers in the metadata for this storage entry.
hashers: usize,
/// The number of fields in the metadata for this storage entry.
fields: usize,
},
}
112 changes: 112 additions & 0 deletions node-api/src/error/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// This file was taken from subxt (Parity Technologies (UK))
// https://github.com/paritytech/subxt/
// And was adapted by Supercomputing Systems AG and Integritee AG.
//
// Copyright 2019-2022 Parity Technologies (UK) Ltd, Supercomputing Systems AG and Integritee AG.
// This file is licensed as Apache-2.0
// see LICENSE for license details.

//! General node-api Error implementation.
use alloc::{format, string::String};
use core::fmt::Debug;
use derive_more::From;

// Re-expose the errors we use from other crates here:
pub use crate::{
metadata::{InvalidMetadataError, MetadataError},
scale_value::{DecodeError, EncodeError},
};
pub use sp_core::crypto::SecretStringError;
pub use sp_runtime::transaction_validity::TransactionValidityError;

mod dispatch_error;
pub use dispatch_error::*;

/// The underlying error enum, generic over the type held by the `Runtime`
/// variant. Prefer to use the [`Error<E>`] and [`Error`] aliases over
/// using this type directly.
#[derive(Debug, From)]
pub enum Error {
/// Codec error.
Codec(codec::Error),
/// Serde serialization error
Serialization(serde_json::error::Error),
/// Secret string error.
SecretString(SecretStringError),
/// Extrinsic validity error
Invalid(TransactionValidityError),
/// Invalid metadata error
InvalidMetadata(InvalidMetadataError),
/// Invalid metadata error
Metadata(MetadataError),
/// Runtime error.
Runtime(DispatchError),
/// Error decoding to a [`crate::dynamic::Value`].
DecodeValue(DecodeError),
/// Error encoding from a [`crate::dynamic::Value`].
EncodeValue(EncodeError),
/// Transaction progress error.
Transaction(TransactionError),
/// Block related error.
Block(BlockError),
/// An error encoding a storage address.
StorageAddress(StorageAddressError),
/// Other error.
Other(String),
}

impl From<&str> for Error {
fn from(error: &str) -> Self {
Error::Other(error.into())
}
}

/// Block error
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum BlockError {
/// The block
BlockHashNotFound(String),
}

impl BlockError {
/// Produce an error that a block with the given hash cannot be found.
pub fn block_hash_not_found(hash: impl AsRef<[u8]>) -> BlockError {
let hash = format!("0x{}", hex::encode(hash));
BlockError::BlockHashNotFound(hash)
}
}

/// Transaction error.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum TransactionError {
/// The finality subscription expired (after ~512 blocks we give up if the
/// block hasn't yet been finalized).
FinalitySubscriptionTimeout,
/// The block hash that the transaction was added to could not be found.
/// This is probably because the block was retracted before being finalized.
BlockHashNotFound,
}

/// Something went wrong trying to encode a storage address.
#[derive(Clone, Debug)]
pub enum StorageAddressError {
/// Storage map type must be a composite type.
MapTypeMustBeTuple,
/// Storage lookup does not have the expected number of keys.
WrongNumberOfKeys {
/// The actual number of keys needed, based on the metadata.
actual: usize,
/// The number of keys provided in the storage address.
expected: usize,
},
/// Storage lookup requires a type that wasn't found in the metadata.
TypeNotFound(u32),
/// This storage entry in the metadata does not have the correct number of hashers to fields.
WrongNumberOfHashers {
/// The number of hashers in the metadata for this storage entry.
hashers: usize,
/// The number of fields in the metadata for this storage entry.
fields: usize,
},
}
Loading

0 comments on commit 19c6397

Please sign in to comment.