Skip to content

Commit

Permalink
Merge pull request #3014 from autonomys/serde-hex
Browse files Browse the repository at this point in the history
Make serde hex attributes shorter
  • Loading branch information
teor2345 authored Sep 12, 2024
2 parents f4ff18f + 76527ba commit 84434a9
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 21 deletions.
2 changes: 1 addition & 1 deletion crates/subspace-core-primitives/src/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ mod scalar_serde {

// Custom wrapper so we don't have to write serialization/deserialization code manually
#[derive(Serialize, Deserialize)]
struct Scalar(#[serde(with = "hex::serde")] pub(super) [u8; super::Scalar::FULL_BYTES]);
struct Scalar(#[serde(with = "hex")] pub(super) [u8; super::Scalar::FULL_BYTES]);

impl Serialize for super::Scalar {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
Expand Down
16 changes: 6 additions & 10 deletions crates/subspace-core-primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,7 @@ pub struct Blake3HashHex(#[cfg_attr(feature = "serde", serde(with = "hex"))] Bla
MaxEncodedLen,
)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Randomness(
#[cfg_attr(feature = "serde", serde(with = "hex::serde"))] [u8; RANDOMNESS_LENGTH],
);
pub struct Randomness(#[cfg_attr(feature = "serde", serde(with = "hex"))] [u8; RANDOMNESS_LENGTH]);

impl AsRef<[u8]> for Randomness {
#[inline]
Expand Down Expand Up @@ -304,7 +302,7 @@ impl PosProof {
MaxEncodedLen,
)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct PotKey(#[cfg_attr(feature = "serde", serde(with = "hex::serde"))] [u8; Self::SIZE]);
pub struct PotKey(#[cfg_attr(feature = "serde", serde(with = "hex"))] [u8; Self::SIZE]);

impl fmt::Display for PotKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Expand Down Expand Up @@ -349,7 +347,7 @@ impl PotKey {
MaxEncodedLen,
)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct PotSeed(#[cfg_attr(feature = "serde", serde(with = "hex::serde"))] [u8; Self::SIZE]);
pub struct PotSeed(#[cfg_attr(feature = "serde", serde(with = "hex"))] [u8; Self::SIZE]);

impl fmt::Display for PotSeed {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Expand Down Expand Up @@ -399,7 +397,7 @@ impl PotSeed {
MaxEncodedLen,
)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct PotOutput(#[cfg_attr(feature = "serde", serde(with = "hex::serde"))] [u8; Self::SIZE]);
pub struct PotOutput(#[cfg_attr(feature = "serde", serde(with = "hex"))] [u8; Self::SIZE]);

impl fmt::Display for PotOutput {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Expand Down Expand Up @@ -481,9 +479,7 @@ impl PotCheckpoints {
Into,
)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct PublicKey(
#[cfg_attr(feature = "serde", serde(with = "hex::serde"))] [u8; PUBLIC_KEY_LENGTH],
);
pub struct PublicKey(#[cfg_attr(feature = "serde", serde(with = "hex"))] [u8; PUBLIC_KEY_LENGTH]);

impl fmt::Display for PublicKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Expand Down Expand Up @@ -1006,7 +1002,7 @@ impl SectorSlotChallenge {
/// Data structure representing sector ID in farmer's plot
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Encode, Decode, TypeInfo)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct SectorId(#[cfg_attr(feature = "serde", serde(with = "hex::serde"))] Blake3Hash);
pub struct SectorId(#[cfg_attr(feature = "serde", serde(with = "hex"))] Blake3Hash);

impl AsRef<[u8]> for SectorId {
#[inline]
Expand Down
6 changes: 3 additions & 3 deletions crates/subspace-core-primitives/src/pieces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@ impl Record {
#[repr(transparent)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct RecordCommitment(
#[cfg_attr(feature = "serde", serde(with = "hex::serde"))] [u8; RecordCommitment::SIZE],
#[cfg_attr(feature = "serde", serde(with = "hex"))] [u8; RecordCommitment::SIZE],
);

impl Default for RecordCommitment {
Expand Down Expand Up @@ -800,7 +800,7 @@ impl TryFrom<RecordCommitment> for Commitment {
#[repr(transparent)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct RecordWitness(
#[cfg_attr(feature = "serde", serde(with = "hex::serde"))] [u8; RecordWitness::SIZE],
#[cfg_attr(feature = "serde", serde(with = "hex"))] [u8; RecordWitness::SIZE],
);

impl Default for RecordWitness {
Expand Down Expand Up @@ -919,7 +919,7 @@ impl TryFrom<RecordWitness> for Witness {
#[repr(transparent)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct ChunkWitness(
#[cfg_attr(feature = "serde", serde(with = "hex::serde"))] [u8; ChunkWitness::SIZE],
#[cfg_attr(feature = "serde", serde(with = "hex"))] [u8; ChunkWitness::SIZE],
);

impl Default for ChunkWitness {
Expand Down
2 changes: 1 addition & 1 deletion crates/subspace-core-primitives/src/segments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ impl SegmentIndex {
#[repr(transparent)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct SegmentCommitment(
#[cfg_attr(feature = "serde", serde(with = "hex::serde"))] [u8; SegmentCommitment::SIZE],
#[cfg_attr(feature = "serde", serde(with = "hex"))] [u8; SegmentCommitment::SIZE],
);

impl Default for SegmentCommitment {
Expand Down
4 changes: 2 additions & 2 deletions crates/subspace-farmer/src/single_disk_farm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ pub enum SingleDiskFarmInfo {
/// ID of the farm
id: FarmId,
/// Genesis hash of the chain used for farm creation
#[serde(with = "hex::serde")]
#[serde(with = "hex")]
genesis_hash: [u8; 32],
/// Public key of identity used for farm creation
public_key: PublicKey,
Expand All @@ -135,7 +135,7 @@ pub enum SingleDiskFarmInfo {
/// ID of the farm
id: FarmId,
/// Genesis hash of the chain used for farm creation
#[serde(with = "hex::serde")]
#[serde(with = "hex")]
genesis_hash: [u8; 32],
/// Public key of identity used for farm creation
public_key: PublicKey,
Expand Down
8 changes: 4 additions & 4 deletions crates/subspace-rpc-primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub const MAX_SEGMENT_HEADERS_PER_REQUEST: usize = 1000;
#[serde(rename_all = "camelCase")]
pub struct FarmerAppInfo {
/// Genesis hash of the chain
#[serde(with = "hex::serde")]
#[serde(with = "hex")]
pub genesis_hash: [u8; 32],
/// Bootstrap nodes for DSN
pub dsn_bootstrap_nodes: Vec<Multiaddr>,
Expand Down Expand Up @@ -138,10 +138,10 @@ pub struct SolutionResponse {
#[serde(rename_all = "camelCase")]
pub struct RewardSigningInfo {
/// Hash to be signed.
#[serde(with = "hex::serde")]
#[serde(with = "hex")]
pub hash: [u8; 32],
/// Public key of the plot identity that should create signature.
#[serde(with = "hex::serde")]
#[serde(with = "hex")]
pub public_key: [u8; 32],
}

Expand All @@ -150,7 +150,7 @@ pub struct RewardSigningInfo {
#[serde(rename_all = "camelCase")]
pub struct RewardSignatureResponse {
/// Hash that was signed.
#[serde(with = "hex::serde")]
#[serde(with = "hex")]
pub hash: [u8; 32],
/// Pre-header or vote hash signature.
pub signature: Option<RewardSignature>,
Expand Down

0 comments on commit 84434a9

Please sign in to comment.