Skip to content

Commit adc3de0

Browse files
committed
Replaced BoundedString with BoundedVec
1 parent 4eb423c commit adc3de0

File tree

2 files changed

+5
-61
lines changed

2 files changed

+5
-61
lines changed

node/runtime/src/lib.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use parity_scale_codec::MaxEncodedLen;
3333
use parity_scale_codec::{Decode, Encode};
3434
use scale_info::TypeInfo;
3535
use serde::{Deserialize, Serialize};
36-
use sidechain_domain::byte_string::{BoundedString, SizedByteString};
36+
use sidechain_domain::byte_string::SizedByteString;
3737
use sidechain_domain::{
3838
CrossChainPublicKey, DelegatorKey, MainchainKeyHash, PermissionedCandidateData,
3939
RegistrationData, ScEpochNumber, ScSlotNumber, StakeDelegation, StakePoolPublicKey, UtxoId,
@@ -461,13 +461,15 @@ impl AsCardanoSPO for BlockAuthor {
461461
}
462462
}
463463

464-
pub const MAX_METADATA_URL_LENGTH: u32 = 512;
464+
parameter_types! {
465+
pub const MaxMetadataUrlLength: u32 = 512;
466+
}
465467

466468
#[derive(
467469
Clone, Debug, MaxEncodedLen, Encode, Decode, Serialize, Deserialize, PartialEq, Eq, TypeInfo,
468470
)]
469471
pub struct BlockProducerMetadataType {
470-
pub url: BoundedString<MAX_METADATA_URL_LENGTH>,
472+
pub url: BoundedVec<u8, MaxMetadataUrlLength>,
471473
pub hash: SizedByteString<32>,
472474
}
473475

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
1-
use core::fmt::{Debug, Display};
2-
31
use alloc::vec::Vec;
42
use byte_string_derive::byte_string;
53
use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
64
use scale_info::TypeInfo;
7-
use serde::de::Error as DeError;
8-
use serde::ser::Error as SerError;
9-
use serde::{Deserialize, Serialize};
10-
use sp_core::bounded::BoundedVec;
11-
use sp_core::ConstU32;
125

136
/// Wrapper for bytes that is serialized as hex string
147
/// To be used for binary data that we want to display nicely but
@@ -52,54 +45,3 @@ impl<const N: usize> Default for SizedByteString<N> {
5245
Self([0; N])
5346
}
5447
}
55-
56-
/// Byte-encoded text string with bounded length
57-
#[derive(Eq, Clone, PartialEq, TypeInfo, Default, Encode, Decode, MaxEncodedLen)]
58-
pub struct BoundedString<const N: u32>(pub BoundedVec<u8, ConstU32<N>>);
59-
60-
impl<const N: u32> TryFrom<Vec<u8>> for BoundedString<N> {
61-
type Error = <BoundedVec<u8, ConstU32<N>> as TryFrom<Vec<u8>>>::Error;
62-
63-
fn try_from(value: Vec<u8>) -> Result<Self, Self::Error> {
64-
Ok(Self(value.try_into()?))
65-
}
66-
}
67-
68-
impl<'a, const N: u32> Deserialize<'a> for BoundedString<N> {
69-
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
70-
where
71-
D: serde::Deserializer<'a>,
72-
{
73-
Ok(Self(
74-
BoundedVec::try_from(
75-
alloc::string::String::deserialize(deserializer)?.as_bytes().to_vec(),
76-
)
77-
.map_err(|_| D::Error::custom("Size limit exceeded"))?,
78-
))
79-
}
80-
}
81-
82-
impl<const N: u32> Serialize for BoundedString<N> {
83-
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
84-
where
85-
S: serde::Serializer,
86-
{
87-
let str = alloc::string::String::from_utf8(self.0.to_vec())
88-
.map_err(|_| S::Error::custom("String is not valid UTF-8"))?;
89-
serializer.serialize_str(&str)
90-
}
91-
}
92-
93-
impl<const N: u32> Display for BoundedString<N> {
94-
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
95-
f.write_str(
96-
&alloc::string::String::from_utf8(self.0.to_vec()).map_err(|_| core::fmt::Error)?,
97-
)
98-
}
99-
}
100-
101-
impl<const N: u32> Debug for BoundedString<N> {
102-
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
103-
f.write_str(&alloc::format!("BoundedString<{}>({:?})", N, self.0))
104-
}
105-
}

0 commit comments

Comments
 (0)