diff --git a/Cargo.lock b/Cargo.lock index 67a3e9b..a3a2877 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -484,25 +484,31 @@ version = "0.6.2" dependencies = [ "avail-core", "binary-merkle-tree", + "blake2b_simd", "bounded-collections", "derive_more", "ethabi-decode", "frame-support", + "hash-db", "hash256-std-hasher", "hex", "hex-literal", + "impl-serde 0.5.0", "log", "parity-scale-codec", + "primitive-types", "rand", "scale-info", "serde", "serde_json", + "sha2 0.10.8", + "sha3", "sp-arithmetic", - "sp-core", - "sp-io", + "sp-debug-derive", "sp-runtime", "sp-runtime-interface", "sp-std", + "sp-storage", "sp-trie", "static_assertions", "test-case", @@ -1991,6 +1997,15 @@ dependencies = [ "serde", ] +[[package]] +name = "impl-serde" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4a143eada6a1ec4aefa5049037a26a6d597bfd64f8c026d07b77133e02b7dd0b" +dependencies = [ + "serde", +] + [[package]] name = "impl-trait-for-tuples" version = "0.2.2" @@ -2132,7 +2147,6 @@ dependencies = [ "serde", "serde_json", "sp-arithmetic", - "sp-core", "static_assertions", "test-case", "thiserror-no-std", @@ -2154,7 +2168,6 @@ dependencies = [ "rand_chacha", "serde", "sp-arithmetic", - "sp-io", "sp-std", "static_assertions", "test-case", @@ -2685,7 +2698,7 @@ checksum = "0b34d9fd68ae0b74a41b21c03c2f62847aa0ffea044eee893b4c140b37e244e2" dependencies = [ "fixed-hash", "impl-codec", - "impl-serde", + "impl-serde 0.4.0", "scale-info", "uint", ] @@ -3442,7 +3455,7 @@ dependencies = [ "futures", "hash-db", "hash256-std-hasher", - "impl-serde", + "impl-serde 0.4.0", "itertools 0.10.5", "libsecp256k1", "log", @@ -3719,7 +3732,7 @@ name = "sp-storage" version = "19.0.0" source = "git+https://github.com/availproject/polkadot-sdk?tag=polkadot-1.7.1-patch-10#f5587b380ee596f90482d402844c49aa140781d8" dependencies = [ - "impl-serde", + "impl-serde 0.4.0", "parity-scale-codec", "ref-cast", "serde", @@ -3768,7 +3781,7 @@ name = "sp-version" version = "29.0.0" source = "git+https://github.com/availproject/polkadot-sdk?tag=polkadot-1.7.1-patch-10#f5587b380ee596f90482d402844c49aa140781d8" dependencies = [ - "impl-serde", + "impl-serde 0.4.0", "parity-scale-codec", "parity-wasm", "scale-info", diff --git a/Cargo.toml b/Cargo.toml index 0cf1341..80f1027 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,13 +11,15 @@ scale-info = { version = "2", default-features = false, features = ["derive"] } # Polkadot SDK binary-merkle-tree = { git = "https://github.com/availproject/polkadot-sdk", tag = "polkadot-1.7.1-patch-10", default-features = false } sp-arithmetic = { git = "https://github.com/availproject/polkadot-sdk", tag = "polkadot-1.7.1-patch-10", default-features = false } -sp-core = { git = "https://github.com/availproject/polkadot-sdk", tag = "polkadot-1.7.1-patch-10", default-features = false } -sp-io = { git = "https://github.com/availproject/polkadot-sdk", tag = "polkadot-1.7.1-patch-10", default-features = false } sp-std = { git = "https://github.com/availproject/polkadot-sdk", tag = "polkadot-1.7.1-patch-10", default-features = false } sp-trie = { git = "https://github.com/availproject/polkadot-sdk", tag = "polkadot-1.7.1-patch-10", default-features = false } sp-runtime = { git = "https://github.com/availproject/polkadot-sdk", tag = "polkadot-1.7.1-patch-10", default-features = false } frame-support = { git = "https://github.com/availproject/polkadot-sdk", tag = "polkadot-1.7.1-patch-10", default-features = false } sp-runtime-interface = { git = "https://github.com/availproject/polkadot-sdk", tag = "polkadot-1.7.1-patch-10", default-features = false } +sp-storage = { git = "https://github.com/availproject/polkadot-sdk.git", tag = "polkadot-1.7.1-patch-10", default-features = false } +sp-debug-derive = { git = "https://github.com/availproject/polkadot-sdk.git", tag = "polkadot-1.7.1-patch-10", default-features = false } + +primitive-types = { version = "0.12.2", default-features = false, features = ["codec"] } # Macros derive_more = { version = "0.99.17", default-features = false, features = ["constructor", "from", "add", "deref", "mul", "into"] } @@ -36,16 +38,22 @@ serde = { version = "1.0.196", default-features = false, features = ["derive", " hex = { version = "0.4", default-features = false, features = ["alloc", "serde"] } serde_json = { version = "1" } dusk-bytes = { version = "0.1.6", default-features = false } +impl-serde = { version = "0.5.0", default-features = false } # Crypto hash256-std-hasher = { version = "0.15.2", default-features = false } tiny-keccak = { version = "2.0.2", default-features = false, features = ["keccak"] } rand = { version = "0.8.4", features = ["alloc", "small_rng"], default-features = false } rand_chacha = { version = "0.3", default-features = false } +blake2b_simd = { version = "1.0.2", default-features = false } +sha2 = { version = "0.10.7", default-features = false } +sha3 = { version = "0.10.0", default-features = false } poly-multiproof = { git = "https://github.com/availproject/poly-multiproof", default-features = false, tag = "v0.0.1" } dusk-plonk = { git = "https://github.com/availproject/plonk.git", tag = "v0.12.0-polygon-2" } +hash-db = { version = "0.16.0", default-features = false } + # Others rayon = "1.5.2" once_cell = "1.8.0" diff --git a/core/Cargo.toml b/core/Cargo.toml index 4421c6d..0a15d60 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -16,21 +16,28 @@ serde = { workspace = true, optional = true } static_assertions.workspace = true thiserror-no-std.workspace = true tiny-keccak.workspace = true +impl-serde = { workspace = true, optional = true } # Parity binary-merkle-tree = { workspace = true, optional = true } bounded-collections.workspace = true codec = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive", "max-encoded-len"] } -scale-info.workspace = true +scale-info = { workspace = true, optional = true } frame-support = { workspace = true, optional = true } sp-arithmetic.workspace = true -sp-core.workspace = true sp-runtime = { workspace = true, optional = true } sp-runtime-interface = { workspace = true, optional = true } sp-std.workspace = true sp-trie.workspace = true -sp-io.workspace = true +sp-storage.workspace = true +sp-debug-derive.workspace = true + +blake2b_simd.workspace = true +sha2.workspace = true +sha3.workspace = true +primitive-types.workspace = true +hash-db.workspace = true [dev-dependencies] hex-literal.workspace = true @@ -55,8 +62,6 @@ std = [ "scale-info/std", "serde/std", "sp-arithmetic/std", - "sp-core/std", - "sp-io/std", "sp-runtime-interface?/std", "sp-runtime?/std", "sp-std/std", @@ -68,8 +73,8 @@ runtime = [ "serde", "sp-runtime", "sp-runtime-interface", + "scale-info" ] -disable_panic_handler = [ "sp-io/disable_panic_handler" ] try-runtime = [ "runtime", "sp-runtime/try-runtime" ] -serde = [ "dep:serde", "hex/serde", "log/serde" ] +serde = [ "dep:serde", "hex/serde", "log/serde", "impl-serde" ] diff --git a/core/src/app_extrinsic.rs b/core/src/app_extrinsic.rs index d8ad9b8..cb593e1 100644 --- a/core/src/app_extrinsic.rs +++ b/core/src/app_extrinsic.rs @@ -1,19 +1,18 @@ use crate::traits::GetAppId; -#[cfg(feature = "runtime")] -use codec::Codec; +use crate::AppId; use codec::{Decode, Encode}; use derive_more::Constructor; -use scale_info::TypeInfo; -#[cfg(feature = "serde")] -use serde::{Deserialize, Serialize}; -use sp_core::RuntimeDebug; use sp_std::vec::Vec; -use crate::AppId; +#[cfg(feature = "serde")] +use serde::{Deserialize, Serialize}; +#[cfg(feature = "runtime")] +use {codec::Codec, scale_info::TypeInfo, sp_debug_derive::RuntimeDebug}; /// Raw Extrinsic with application id. -#[derive(Clone, TypeInfo, Default, Encode, Decode, RuntimeDebug, Constructor)] +#[derive(Clone, Default, Encode, Decode, Constructor)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "runtime", derive(RuntimeDebug, TypeInfo))] pub struct AppExtrinsic { pub app_id: AppId, #[cfg_attr(feature = "serde", serde(with = "hex"))] diff --git a/core/src/asdr.rs b/core/src/asdr.rs index 7343e83..58d00e2 100644 --- a/core/src/asdr.rs +++ b/core/src/asdr.rs @@ -21,9 +21,9 @@ use crate::{ AppId, OpaqueExtrinsic, }; +use crate::from_substrate::blake2_256; use codec::{Codec, Compact, Decode, Encode, EncodeLike, Error, Input}; use scale_info::{build::Fields, meta_type, Path, StaticTypeInfo, Type, TypeInfo, TypeParameter}; -use sp_io::hashing::blake2_256; use sp_runtime::MultiAddress; use sp_std::{ fmt::{Debug, Formatter, Result as FmtResult}, @@ -31,22 +31,23 @@ use sp_std::{ vec::Vec, }; +#[cfg(all(not(feature = "std"), feature = "serde"))] +use sp_std::alloc::format; #[cfg(feature = "runtime")] -use frame_support::{ - dispatch::{DispatchInfo, GetDispatchInfo}, - traits::ExtrinsicCall, -}; -#[cfg(feature = "runtime")] -use sp_runtime::{ - generic::CheckedExtrinsic, - traits::{ - self, Checkable, Extrinsic, ExtrinsicMetadata, IdentifyAccount, MaybeDisplay, Member, - SignedExtension, +use { + frame_support::{ + dispatch::{DispatchInfo, GetDispatchInfo}, + traits::ExtrinsicCall, + }, + sp_runtime::{ + generic::CheckedExtrinsic, + traits::{ + self, Checkable, Extrinsic, ExtrinsicMetadata, IdentifyAccount, MaybeDisplay, Member, + SignedExtension, + }, + transaction_validity::{InvalidTransaction, TransactionValidityError}, }, - transaction_validity::{InvalidTransaction, TransactionValidityError}, }; -#[cfg(all(not(feature = "std"), feature = "serde"))] -use sp_std::alloc::format; /// Current version of the [`UncheckedExtrinsic`] encoded format. /// @@ -433,7 +434,7 @@ where S: ::serde::Serializer, { let encoded = self.encode(); - sp_core::bytes::serialize(&encoded, s) + impl_serde::serialize::serialize(&encoded, s) } } @@ -449,7 +450,7 @@ where where D: serde::Deserializer<'a>, { - let r = sp_core::bytes::deserialize(de)?; + let r = impl_serde::serialize::deserialize(de)?; Decode::decode(&mut &r[..]) .map_err(|e| serde::de::Error::custom(format!("Decode error: {}", e))) } @@ -638,7 +639,8 @@ mod tests { TEST_ACCOUNT, TestSig( TEST_ACCOUNT, - (vec![0u8; 257], TestExtra).using_encoded(blake2_256)[..].to_owned(), + (vec![0u8; 257], TestExtra).using_encoded(crate::from_substrate::blake2_256)[..] + .to_owned(), ), TestExtra, ); diff --git a/core/src/da_block.rs b/core/src/da_block.rs index 3897f89..9336d0a 100644 --- a/core/src/da_block.rs +++ b/core/src/da_block.rs @@ -22,9 +22,6 @@ use std::fmt; use crate::traits::{ExtendedBlock, ExtendedHeader}; use codec::{Codec, Decode, Encode}; -#[cfg(feature = "serde")] -use serde::{Deserialize, Serialize}; -use sp_core::RuntimeDebug; use sp_runtime::{ traits::{ self, Block as BlockT, Header as HeaderT, MaybeSerializeDeserialize, Member, NumberFor, @@ -33,8 +30,14 @@ use sp_runtime::{ }; use sp_std::prelude::*; +#[cfg(feature = "serde")] +use serde::{Deserialize, Serialize}; +#[cfg(feature = "runtime")] +use sp_debug_derive::RuntimeDebug; + /// Something to identify a block. -#[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug)] +#[derive(PartialEq, Eq, Clone, Encode, Decode)] +#[cfg_attr(feature = "runtime", derive(RuntimeDebug))] pub enum BlockId { /// Identify by block header hash. Hash(Block::Hash), diff --git a/core/src/data_lookup/mod.rs b/core/src/data_lookup/mod.rs index b637730..3855b45 100644 --- a/core/src/data_lookup/mod.rs +++ b/core/src/data_lookup/mod.rs @@ -1,7 +1,7 @@ use codec::{Decode, Encode, Input}; use core::convert::TryFrom; use scale_info::{Type, TypeInfo}; -use sp_core::RuntimeDebug; +use sp_debug_derive::RuntimeDebug; use sp_std::vec; use sp_std::{ops::Range, vec::Vec}; use thiserror_no_std::Error; diff --git a/core/src/data_proof.rs b/core/src/data_proof.rs index 2584130..2b05131 100644 --- a/core/src/data_proof.rs +++ b/core/src/data_proof.rs @@ -1,16 +1,14 @@ use bounded_collections::BoundedVec; +use bounded_collections::ConstU32; use codec::{Decode, Encode}; use derive_more::Constructor; -use scale_info::TypeInfo; -use sp_core::{ConstU32, H256}; +use primitive_types::H256; use sp_std::vec::Vec; -#[cfg(feature = "runtime")] -use binary_merkle_tree::MerkleProof; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; #[cfg(feature = "runtime")] -use sp_io::hashing::keccak_256; +use {binary_merkle_tree::MerkleProof, scale_info::TypeInfo}; /// Max data supported on bridge (Ethereum calldata limits) pub const BOUNDED_DATA_MAX_LENGTH: u32 = 102_400; @@ -41,9 +39,10 @@ pub fn tx_uid_deconstruct(uid: u64) -> (u32, u32) { (block, tx_index) } -#[derive(Clone, Debug, Encode, Decode, TypeInfo, Constructor)] +#[derive(Clone, Debug, Encode, Decode, Constructor)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))] +#[cfg_attr(feature = "runtime", derive(TypeInfo))] pub struct ProofResponse { pub data_proof: DataProof, #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))] @@ -56,9 +55,10 @@ pub enum SubTrie { Bridge, } -#[derive(Debug, Clone, Copy, Encode, Decode, PartialEq, Eq, Default, TypeInfo)] +#[derive(Debug, Clone, Copy, Encode, Decode, PartialEq, Eq, Default)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))] +#[cfg_attr(feature = "runtime", derive(TypeInfo))] pub struct TxDataRoots { /// Global Merkle root pub data_root: H256, @@ -71,6 +71,8 @@ pub struct TxDataRoots { #[cfg(feature = "runtime")] impl TxDataRoots { pub fn new(submitted: H256, bridged: H256) -> Self { + use crate::from_substrate::keccak_256; + // keccak_256(submitted, bridged) let sub_roots = [submitted.to_fixed_bytes(), bridged.to_fixed_bytes()].concat(); let root = keccak_256(sub_roots.as_slice()).into(); @@ -84,9 +86,10 @@ impl TxDataRoots { } /// Wrapper of `binary-merkle-tree::MerkleProof` with codec support. -#[derive(Clone, Debug, PartialEq, Eq, Encode, Decode, TypeInfo, Default)] +#[derive(Clone, Debug, PartialEq, Eq, Encode, Decode, Default)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))] +#[cfg_attr(feature = "runtime", derive(TypeInfo))] pub struct DataProof { pub roots: TxDataRoots, /// Proof items (does not contain the leaf hash, nor the root obviously). @@ -110,6 +113,8 @@ pub struct DataProof { #[cfg(feature = "runtime")] impl DataProof { pub fn new(sub_trie: SubTrie, roots: TxDataRoots, m_proof: MerkleProof>) -> Self { + use crate::from_substrate::keccak_256; + let leaf = match sub_trie { SubTrie::DataSubmit => H256::from_slice(m_proof.leaf.as_slice()), SubTrie::Bridge => keccak_256(m_proof.leaf.as_slice()).into(), diff --git a/core/src/data_proof/message.rs b/core/src/data_proof/message.rs index 3a9f04e..3951810 100644 --- a/core/src/data_proof/message.rs +++ b/core/src/data_proof/message.rs @@ -2,8 +2,8 @@ use super::BoundedData; use codec::{Decode, Encode}; use derive_more::{Constructor, From}; +use primitive_types::H256; use scale_info::TypeInfo; -use sp_core::H256; use sp_std::{vec, vec::Vec}; use ethabi_decode::{encode, Token, U256}; diff --git a/core/src/from_substrate.rs b/core/src/from_substrate.rs new file mode 100644 index 0000000..0fe8316 --- /dev/null +++ b/core/src/from_substrate.rs @@ -0,0 +1,63 @@ +use sha3::Digest; + +#[inline(always)] +fn blake2(data: &[u8]) -> [u8; N] { + blake2b_simd::Params::new() + .hash_length(N) + .hash(data) + .as_bytes() + .try_into() + .expect("slice is always the necessary length") +} + +/// Do a Blake2 256-bit hash and return result. +pub fn blake2_256(data: &[u8]) -> [u8; 32] { + blake2(data) +} + +/// Do a Blake2 128-bit hash and return result. +pub fn blake2_128(data: &[u8]) -> [u8; 16] { + blake2(data) +} + +/// Do a keccak 256-bit hash and return result. +pub fn keccak_256(data: &[u8]) -> [u8; 32] { + sha3::Keccak256::digest(data).into() +} + +/// Do a sha2 256-bit hash and return result. +pub fn sha2_256(data: &[u8]) -> [u8; 32] { + sha2::Sha256::digest(data).into() +} + +pub struct HexDisplay<'a>(pub &'a [u8]); + +impl<'a> sp_std::fmt::Display for HexDisplay<'a> { + fn fmt(&self, f: &mut sp_std::fmt::Formatter) -> Result<(), sp_std::fmt::Error> { + let len = self.0.len(); + if len < 1027 { + for byte in self.0 { + f.write_fmt(format_args!("{:02x}", byte))?; + } + } else { + for byte in &self.0[0..512] { + f.write_fmt(format_args!("{:02x}", byte))?; + } + f.write_str("...")?; + let start = len.saturating_sub(512); + for byte in &self.0[start..] { + f.write_fmt(format_args!("{:02x}", byte))?; + } + } + Ok(()) + } +} + +impl<'a> sp_std::fmt::Debug for HexDisplay<'a> { + fn fmt(&self, f: &mut sp_std::fmt::Formatter) -> Result<(), sp_std::fmt::Error> { + for byte in self.0 { + f.write_fmt(format_args!("{:02x}", byte))?; + } + Ok(()) + } +} diff --git a/core/src/header/extension/mod.rs b/core/src/header/extension/mod.rs index d204487..acc22e6 100644 --- a/core/src/header/extension/mod.rs +++ b/core/src/header/extension/mod.rs @@ -1,19 +1,20 @@ +use crate::{DataLookup, HeaderVersion}; use codec::{Decode, Encode}; -use scale_info::TypeInfo; +use primitive_types::H256; + #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; -use sp_core::{RuntimeDebug, H256}; #[cfg(feature = "runtime")] -use sp_runtime_interface::pass_by::PassByCodec; - -use crate::{DataLookup, HeaderVersion}; +use { + scale_info::TypeInfo, sp_debug_derive::RuntimeDebug, sp_runtime_interface::pass_by::PassByCodec, +}; pub mod v3; /// Header extension data. -#[derive(PartialEq, Eq, Clone, RuntimeDebug, TypeInfo, Encode, Decode)] +#[derive(PartialEq, Eq, Clone, Encode, Decode)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] -#[cfg_attr(feature = "runtime", derive(PassByCodec))] +#[cfg_attr(feature = "runtime", derive(PassByCodec, RuntimeDebug, TypeInfo))] #[repr(u8)] pub enum HeaderExtension { V3(v3::HeaderExtension) = 2, diff --git a/core/src/header/extension/v3.rs b/core/src/header/extension/v3.rs index 6d8a603..3c8cd69 100644 --- a/core/src/header/extension/v3.rs +++ b/core/src/header/extension/v3.rs @@ -1,15 +1,17 @@ +use crate::{v3::KateCommitment, DataLookup}; use codec::{Decode, Encode}; -use scale_info::TypeInfo; -#[cfg(feature = "serde")] -use serde::{Deserialize, Serialize}; -use sp_core::{RuntimeDebug, H256}; +use primitive_types::H256; use sp_std::{vec, vec::Vec}; -use crate::{v3::KateCommitment, DataLookup}; +#[cfg(feature = "serde")] +use serde::{Deserialize, Serialize}; +#[cfg(feature = "runtime")] +use {scale_info::TypeInfo, sp_debug_derive::RuntimeDebug}; -#[derive(PartialEq, Eq, Clone, RuntimeDebug, TypeInfo, Encode, Decode, Default)] +#[derive(PartialEq, Eq, Clone, Encode, Decode, Default)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))] +#[cfg_attr(feature = "runtime", derive(TypeInfo, RuntimeDebug))] pub struct HeaderExtension { pub app_lookup: DataLookup, pub commitment: KateCommitment, diff --git a/core/src/header/mod.rs b/core/src/header/mod.rs index 931df17..f2e949f 100644 --- a/core/src/header/mod.rs +++ b/core/src/header/mod.rs @@ -17,22 +17,26 @@ //! Data-Avail implementation of a block header. +use crate::from_substrate::HexDisplay; +use crate::traits::ExtendedHeader; use codec::{Decode, Encode}; -use scale_info::TypeInfo; -#[cfg(feature = "serde")] -use serde::{Deserialize, Serialize}; -use sp_core::{hexdisplay::HexDisplay, U256}; -use sp_runtime::{ - traits::{BlockNumber, Hash as HashT, Header as HeaderT}, - Digest, -}; -use sp_runtime_interface::pass_by::{Codec as PassByCodecImpl, PassBy}; +use primitive_types::U256; use sp_std::{ convert::TryFrom, fmt::{Debug, Formatter}, }; -use crate::traits::ExtendedHeader; +#[cfg(feature = "serde")] +use serde::{Deserialize, Serialize}; +#[cfg(feature = "runtime")] +use { + scale_info::TypeInfo, + sp_runtime::{ + traits::{BlockNumber, Hash as HashT, Header as HeaderT}, + Digest, + }, + sp_runtime_interface::pass_by::{Codec as PassByCodecImpl, PassBy}, +}; #[cfg(feature = "std")] const LOG_TARGET: &str = "header"; @@ -115,10 +119,10 @@ where let extrinsics_root = self.extrinsics_root.as_ref(); f.debug_struct("Header") - .field("parent_hash", &HexDisplay::from(&parent_hash)) + .field("parent_hash", &HexDisplay(&parent_hash)) .field("number", &self.number) - .field("state_root", &HexDisplay::from(&state_root)) - .field("extrinsics_root", &HexDisplay::from(&extrinsics_root)) + .field("state_root", &HexDisplay(&state_root)) + .field("extrinsics_root", &HexDisplay(&extrinsics_root)) .field("digest", &self.digest) .field("extension", &self.extension) .finish() @@ -169,6 +173,7 @@ where } } +#[cfg(feature = "runtime")] impl PassBy for Header where N: BlockNumber, @@ -283,7 +288,7 @@ where mod tests { use codec::Error; use hex_literal::hex; - use sp_core::H256; + use primitive_types::H256; use sp_runtime::{traits::BlakeTwo256, DigestItem}; use test_case::test_case; diff --git a/core/src/header_version/mod.rs b/core/src/header_version/mod.rs index 0afdabf..4092df8 100644 --- a/core/src/header_version/mod.rs +++ b/core/src/header_version/mod.rs @@ -1,10 +1,9 @@ use codec::{Decode, Encode}; -use scale_info::TypeInfo; #[cfg(feature = "runtime")] -use sp_runtime_interface::pass_by::PassByCodec; +use {scale_info::TypeInfo, sp_runtime_interface::pass_by::PassByCodec}; -#[derive(Debug, Clone, Copy, Eq, PartialEq, Encode, Decode, TypeInfo)] -#[cfg_attr(feature = "runtime", derive(PassByCodec))] +#[derive(Debug, Clone, Copy, Eq, PartialEq, Encode, Decode)] +#[cfg_attr(feature = "runtime", derive(PassByCodec, TypeInfo))] pub enum HeaderVersion { V3 = 2, // Current one } diff --git a/core/src/kate_commitment.rs b/core/src/kate_commitment.rs index 9edb0aa..077eaf7 100644 --- a/core/src/kate_commitment.rs +++ b/core/src/kate_commitment.rs @@ -1,14 +1,11 @@ +use crate::from_substrate::HexDisplay; use codec::{Decode, Encode}; +use primitive_types::H256; use scale_info::TypeInfo; -use sp_core::H256; -use sp_std::vec::Vec; +use sp_std::{fmt, vec::Vec}; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; -#[cfg(feature = "serde")] -use sp_core::hexdisplay::HexDisplay; -#[cfg(feature = "serde")] -use sp_std::fmt; pub mod v3 { use super::*; @@ -42,17 +39,16 @@ pub mod v3 { } } - #[cfg(feature = "serde")] impl fmt::Debug for KateCommitment { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let commitment = self.commitment.as_slice(); - let data_root = self.data_root.as_ref(); + let commitment: &[u8] = self.commitment.as_slice(); + let data_root: &[u8] = self.data_root.as_ref(); f.debug_struct("KateCommitment(v3)") .field("rows", &self.rows) .field("cols", &self.cols) - .field("commitment", &HexDisplay::from(&commitment)) - .field("data_root", &HexDisplay::from(&data_root)) + .field("commitment", &HexDisplay(commitment)) + .field("data_root", &HexDisplay(data_root)) .finish() } } diff --git a/core/src/keccak256.rs b/core/src/keccak256.rs index d1949e1..1752299 100644 --- a/core/src/keccak256.rs +++ b/core/src/keccak256.rs @@ -1,21 +1,23 @@ -use scale_info::TypeInfo; -use sp_core::{Hasher, RuntimeDebug}; +use hash_db::Hasher; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; +#[cfg(feature = "runtime")] +use {scale_info::TypeInfo, sp_debug_derive::RuntimeDebug}; /// Keccak 256 wrapper which supports `binary-merkle-tree::Hasher`. -#[derive(PartialEq, Eq, Clone, RuntimeDebug, TypeInfo)] +#[derive(PartialEq, Eq, Clone)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "runtime", derive(RuntimeDebug, TypeInfo))] pub struct Keccak256 {} impl Hasher for Keccak256 { - type Out = sp_core::H256; + type Out = primitive_types::H256; type StdHasher = hash256_std_hasher::Hash256StdHasher; const LENGTH: usize = 32; fn hash(s: &[u8]) -> Self::Out { - let keccak_out = sp_io::hashing::keccak_256(s); + let keccak_out = crate::from_substrate::keccak_256(s); keccak_out.into() } } @@ -23,12 +25,12 @@ impl Hasher for Keccak256 { #[cfg(feature = "runtime")] pub mod hash { use super::*; - use sp_core::storage::StateVersion; use sp_std::vec::Vec; + use sp_storage::StateVersion; use sp_trie::{LayoutV0, LayoutV1, TrieConfiguration as _}; impl sp_runtime::traits::Hash for Keccak256 { - type Output = sp_core::H256; + type Output = primitive_types::H256; fn trie_root(input: Vec<(Vec, Vec)>, version: StateVersion) -> Self::Output { match version { diff --git a/core/src/lib.rs b/core/src/lib.rs index d846ae4..6ee9cba 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -9,7 +9,9 @@ use scale_info::TypeInfo; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; use sp_arithmetic::traits::Zero; -use sp_core::RuntimeDebug; +use sp_debug_derive::RuntimeDebug; + +pub mod from_substrate; pub mod opaque_extrinsic; pub use opaque_extrinsic::*; @@ -199,7 +201,7 @@ macro_rules! keccak256_concat{ let mut hasher = tiny_keccak::Keccak::v256(); $crate::keccak256_concat_update!(hasher, $($arg)*); hasher.finalize(&mut output); - sp_core::H256::from(output) + primitive_types::H256::from(output) } }} } diff --git a/core/src/opaque_extrinsic.rs b/core/src/opaque_extrinsic.rs index 010374c..3e1cdc5 100644 --- a/core/src/opaque_extrinsic.rs +++ b/core/src/opaque_extrinsic.rs @@ -1,3 +1,4 @@ +use crate::from_substrate::HexDisplay; use codec::{Decode, Encode}; use scale_info::prelude::format; use scale_info::TypeInfo; @@ -22,7 +23,7 @@ impl OpaqueExtrinsic { impl sp_std::fmt::Debug for OpaqueExtrinsic { #[cfg(feature = "std")] fn fmt(&self, fmt: &mut sp_std::fmt::Formatter) -> sp_std::fmt::Result { - write!(fmt, "{}", sp_core::hexdisplay::HexDisplay::from(&self.0)) + write!(fmt, "{}", HexDisplay(&self.0)) } #[cfg(not(feature = "std"))] @@ -37,7 +38,9 @@ impl ::serde::Serialize for OpaqueExtrinsic { where S: ::serde::Serializer, { - codec::Encode::using_encoded(&self.0, |bytes| ::sp_core::bytes::serialize(bytes, seq)) + codec::Encode::using_encoded(&self.0, |bytes| { + ::impl_serde::serialize::serialize(bytes, seq) + }) } } @@ -47,7 +50,7 @@ impl<'a> ::serde::Deserialize<'a> for OpaqueExtrinsic { where D: ::serde::Deserializer<'a>, { - let r = ::sp_core::bytes::deserialize(de)?; + let r = ::impl_serde::serialize::deserialize(de)?; Decode::decode(&mut &r[..]) .map_err(|e| ::serde::de::Error::custom(format!("Decode error: {e}"))) } diff --git a/core/src/sha2.rs b/core/src/sha2.rs index a91cb06..1067e65 100644 --- a/core/src/sha2.rs +++ b/core/src/sha2.rs @@ -1,7 +1,8 @@ +use hash_db::Hasher; use scale_info::TypeInfo; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; -use sp_core::{Hasher, RuntimeDebug}; +use sp_debug_derive::RuntimeDebug; /// Sha2 256 wrapper which supports `binary-merkle-tree::Hasher`. #[derive(PartialEq, Eq, Clone, RuntimeDebug, TypeInfo)] @@ -9,12 +10,12 @@ use sp_core::{Hasher, RuntimeDebug}; pub struct ShaTwo256 {} impl Hasher for ShaTwo256 { - type Out = sp_core::H256; + type Out = primitive_types::H256; type StdHasher = hash256_std_hasher::Hash256StdHasher; const LENGTH: usize = 32; fn hash(s: &[u8]) -> Self::Out { - let sha2_out = sp_io::hashing::sha2_256(s); + let sha2_out = crate::from_substrate::keccak_256(s); sha2_out.into() } } @@ -22,12 +23,12 @@ impl Hasher for ShaTwo256 { #[cfg(feature = "runtime")] pub mod hash { use super::*; - use sp_core::storage::StateVersion; use sp_std::vec::Vec; + use sp_storage::StateVersion; use sp_trie::{LayoutV0, LayoutV1, TrieConfiguration as _}; impl sp_runtime::traits::Hash for ShaTwo256 { - type Output = sp_core::H256; + type Output = primitive_types::H256; fn trie_root(input: Vec<(Vec, Vec)>, version: StateVersion) -> Self::Output { match version { diff --git a/kate/Cargo.toml b/kate/Cargo.toml index 513bc88..7de4bf6 100644 --- a/kate/Cargo.toml +++ b/kate/Cargo.toml @@ -19,7 +19,6 @@ poly-multiproof = { workspace = true, optional = true } # Parity & Substrate codec = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] } sp-arithmetic.workspace = true -sp-core.workspace = true # 3rd-party derive_more.workspace = true @@ -66,7 +65,6 @@ std = [ "serde", "serde_json", "sp-arithmetic/std", - "sp-core/std", ] serde = [ "dep:serde" ] diff --git a/kate/recovery/Cargo.toml b/kate/recovery/Cargo.toml index d3e7c1b..16544f2 100644 --- a/kate/recovery/Cargo.toml +++ b/kate/recovery/Cargo.toml @@ -7,7 +7,6 @@ license = "Apache-2.0" [target.'cfg(target_arch = "wasm32")'.dependencies] getrandom = { version = "0.2.15", features = ["js"] } -sp-io = { workspace = true, features = [ "disable_panic_handler" ] } [dependencies] # Internals diff --git a/kate/src/com.rs b/kate/src/com.rs index d9eb36a..589ba7f 100644 --- a/kate/src/com.rs +++ b/kate/src/com.rs @@ -501,6 +501,8 @@ pub fn par_build_commitments( rng_seed: Seed, metrics: &M, ) -> Result<(XtsLayout, Vec, BlockDimensions, DMatrix), Error> { + use avail_core::from_substrate; + let start = Instant::now(); // generate data matrix first @@ -523,7 +525,7 @@ pub fn par_build_commitments( if log::log_enabled!(target: LOG_TARGET, log::Level::Debug) { let raw_pp = public_params.to_raw_var_bytes(); - let hash_pp = hex::encode(sp_core::hashing::blake2_128(&raw_pp)); + let hash_pp = hex::encode(from_substrate::blake2_128(&raw_pp)); let hex_pp = hex::encode(raw_pp); log::debug!( target: LOG_TARGET,