Skip to content

Commit

Permalink
fix: fixed serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
nakul1010 committed Aug 1, 2023
1 parent 6cdbca3 commit 79539db
Show file tree
Hide file tree
Showing 49 changed files with 418 additions and 225 deletions.
128 changes: 59 additions & 69 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions crates/annuity/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ name = "annuity"
version = "1.0.0"

[dependencies]
serde = { version = "1.0.130", default-features = false, features = ["derive"], optional = true }
serde = { version = "1.0.130", default-features = false, features = ["derive"] }
codec = { package = "parity-scale-codec", version = "3.1.5", default-features = false, features = ["derive", "max-encoded-len"] }
scale-info = { version = "2.2.0", default-features = false, features = ["derive"] }

Expand Down Expand Up @@ -35,7 +35,7 @@ reward = { path = "../reward", default-features = false }
[features]
default = ["std"]
std = [
"serde",
"serde/std",
"codec/std",
"scale-info/std",

Expand Down
8 changes: 3 additions & 5 deletions crates/bitcoin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ edition = "2021"
[dependencies]
codec = { package = "parity-scale-codec", version = "3.1.5", default-features = false, features = ["derive", "max-encoded-len"] }
scale-info = { version = "2.2.0", default-features = false, features = ["derive"] }
serde = { version = "1.0.130", default-features = false, features = ["derive"], optional = true }
impl-serde = { version = "0.3.1", default-features = false, optional = true }
serde = { version = "1.0.130", default-features = false, features = ["derive"] }
sha2 = { version = "0.8.2", default-features = false }
hex = { version = "0.4.2", default-features = false }
spin = { version = "0.7.1", default-features = false }
Expand All @@ -26,12 +25,11 @@ default = ["std"]
std = [
"codec/std",
"scale-info/std",
"serde",
"impl-serde",
"serde/std",
"sha2/std",
"hex/alloc",
"primitive-types/std",
"primitive-types/serde",
# "primitive-types/serde",
"secp256k1/std",
]
parser = []
Expand Down
22 changes: 13 additions & 9 deletions crates/bitcoin/src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use bitcoin_hashes::{hash160::Hash as Hash160, Hash};
use codec::{Decode, Encode, MaxEncodedLen};
use primitive_types::{H160, H256};
use scale_info::TypeInfo;
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};

#[cfg(not(feature = "std"))]
Expand All @@ -13,8 +14,10 @@ use secp256k1::{constants::PUBLIC_KEY_SIZE, Error as Secp256k1Error, PublicKey a
/// A Bitcoin address is a serialized identifier that represents the destination for a payment.
/// Address prefixes are used to indicate the network as well as the format. Since the Parachain
/// follows SPV assumptions we do not need to know which network a payment is included in.
#[derive(Encode, Decode, Clone, Ord, PartialOrd, PartialEq, Eq, Debug, Copy, TypeInfo, MaxEncodedLen)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize, std::hash::Hash))]
#[derive(
Serialize, Deserialize, Encode, Decode, Clone, Ord, PartialOrd, PartialEq, Eq, Debug, Copy, TypeInfo, MaxEncodedLen,
)]
#[cfg_attr(feature = "std", derive(std::hash::Hash))]
pub enum Address {
// input: {signature} {pubkey}
// output: OP_DUP OP_HASH160 {hash160(pubkey)} OP_EQUALVERIFY OP_CHECKSIG
Expand Down Expand Up @@ -150,28 +153,29 @@ impl From<[u8; PUBLIC_KEY_SIZE]> for PublicKey {
}
}

#[cfg(feature = "std")]
// #[cfg(feature = "std")]
impl serde::Serialize for PublicKey {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let mut slice = [0u8; 2 + 2 * PUBLIC_KEY_SIZE];
impl_serde::serialize::serialize_raw(&mut slice, &self.0, serializer)
// impl_serde::serialize::serialize_raw(&mut slice, &self.0, serializer)
todo!()
}
}

#[cfg(feature = "std")]
// #[cfg(feature = "std")]
impl<'de> serde::Deserialize<'de> for PublicKey {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let mut bytes = [0u8; PUBLIC_KEY_SIZE];
impl_serde::serialize::deserialize_check_len(
deserializer,
impl_serde::serialize::ExpectedLen::Exact(&mut bytes),
)?;
// impl_serde::serialize::deserialize_check_len(
// deserializer,
// impl_serde::serialize::ExpectedLen::Exact(&mut bytes),
// )?;todo()
Ok(PublicKey(bytes))
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/bitcoin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
//! - Script (address) construction and parsing.
//! - Merkle proof construction and verification.
//! - Elliptic curve multiplication over Secp256k1.
#![deny(warnings)]
// #![allow(clippy::all)]
// #![deny(warnings)]
#![cfg_attr(test, feature(proc_macro_hygiene))]
#![cfg_attr(not(feature = "std"), no_std)]

Expand Down
17 changes: 14 additions & 3 deletions crates/bitcoin/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ use alloc::{vec, vec::Vec};
#[cfg(feature = "std")]
use codec::alloc::string::String;

#[cfg(feature = "std")]
use serde::{Deserialize, Serialize};

/// We also check the coinbase proof in order to defend against the 'leaf-node weakness'.
Expand Down Expand Up @@ -591,8 +590,20 @@ pub struct BlockChain {
}

/// Represents a bitcoin 32 bytes hash digest encoded in little-endian
#[derive(Encode, Decode, Default, PartialEq, Eq, Clone, Copy, Debug, TypeInfo, MaxEncodedLen)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[derive(
serde::Serialize,
serde::Deserialize,
Encode,
Decode,
Default,
PartialEq,
Eq,
Clone,
Copy,
Debug,
TypeInfo,
MaxEncodedLen,
)]
pub struct H256Le {
content: [u8; 32],
}
Expand Down
4 changes: 2 additions & 2 deletions crates/clients-info/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ authors = ["Interlay Ltd"]
edition = "2021"

[dependencies]
serde = { version = "1.0.130", default-features = false, features = ["derive"], optional = true }
serde = { version = "1.0.130", default-features = false, features = ["derive"] }
codec = { package = "parity-scale-codec", version = "3.1.5", default-features = false, features = ["derive", "max-encoded-len"] }
scale-info = { version = "2.2.0", default-features = false, features = ["derive"] }

Expand All @@ -28,7 +28,7 @@ frame-benchmarking = { git = "https://github.com/paritytech/substrate", branch =
[features]
default = ["std"]
std = [
"serde",
"serde/std",
"codec/std",
"scale-info/std",

Expand Down
5 changes: 4 additions & 1 deletion crates/collator-selection/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,10 @@ pub mod pallet {
#[pallet::genesis_build]
impl<T: Config> GenesisBuild<T> for GenesisConfig<T> {
fn build(&self) {
let duplicate_invulnerables = self.invulnerables.iter().collect::<std::collections::BTreeSet<_>>();
let duplicate_invulnerables = self
.invulnerables
.iter()
.collect::<sp_std::collections::btree_set::BTreeSet<_>>();
assert!(
duplicate_invulnerables.len() == self.invulnerables.len(),
"duplicate invulnerables in genesis."
Expand Down
Loading

0 comments on commit 79539db

Please sign in to comment.