casper_types/
byte_code.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
use alloc::{format, string::String, vec::Vec};
use core::{
    array::TryFromSliceError,
    convert::TryFrom,
    fmt::{self, Debug, Display, Formatter},
};

#[cfg(feature = "datasize")]
use datasize::DataSize;
#[cfg(any(feature = "testing", test))]
use rand::{
    distributions::{Distribution, Standard},
    Rng,
};
#[cfg(feature = "json-schema")]
use schemars::{gen::SchemaGenerator, schema::Schema, JsonSchema};
use serde::{de::Error as SerdeError, Deserialize, Deserializer, Serialize, Serializer};

use crate::{
    addressable_entity, bytesrepr,
    bytesrepr::{Bytes, Error, FromBytes, ToBytes, U8_SERIALIZED_LENGTH},
    checksummed_hex, uref, CLType, CLTyped, HashAddr,
};

const BYTE_CODE_MAX_DISPLAY_LEN: usize = 16;
const KEY_HASH_LENGTH: usize = 32;
const WASM_STRING_PREFIX: &str = "byte-code-";

const BYTE_CODE_PREFIX: &str = "byte-code-";
const V1_WASM_PREFIX: &str = "v1-wasm-";
const V2_WASM_PREFIX: &str = "v2-wasm-";
const EMPTY_PREFIX: &str = "empty-";

/// Associated error type of `TryFrom<&[u8]>` for `ByteCodeHash`.
#[derive(Debug)]
pub struct TryFromSliceForContractHashError(());

#[derive(Debug)]
#[non_exhaustive]
pub enum FromStrError {
    InvalidPrefix,
    Hex(base16::DecodeError),
    Hash(TryFromSliceError),
    AccountHash(addressable_entity::FromAccountHashStrError),
    URef(uref::FromStrError),
}

impl From<base16::DecodeError> for FromStrError {
    fn from(error: base16::DecodeError) -> Self {
        FromStrError::Hex(error)
    }
}

impl From<TryFromSliceError> for FromStrError {
    fn from(error: TryFromSliceError) -> Self {
        FromStrError::Hash(error)
    }
}

impl From<addressable_entity::FromAccountHashStrError> for FromStrError {
    fn from(error: addressable_entity::FromAccountHashStrError) -> Self {
        FromStrError::AccountHash(error)
    }
}

impl From<uref::FromStrError> for FromStrError {
    fn from(error: uref::FromStrError) -> Self {
        FromStrError::URef(error)
    }
}

impl Display for FromStrError {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        match self {
            FromStrError::InvalidPrefix => write!(f, "invalid prefix"),
            FromStrError::Hex(error) => write!(f, "decode from hex: {}", error),
            FromStrError::Hash(error) => write!(f, "hash from string error: {}", error),
            FromStrError::AccountHash(error) => {
                write!(f, "account hash from string error: {:?}", error)
            }
            FromStrError::URef(error) => write!(f, "uref from string error: {:?}", error),
        }
    }
}

/// An address for ByteCode records stored in global state.
#[derive(PartialOrd, Ord, PartialEq, Eq, Hash, Clone, Copy, Serialize, Deserialize)]
#[cfg_attr(feature = "datasize", derive(DataSize))]
#[cfg_attr(feature = "json-schema", derive(JsonSchema))]
pub enum ByteCodeAddr {
    /// An address for byte code to be executed against the V1 Casper execution engine.
    V1CasperWasm(HashAddr),
    /// An address for byte code to be executed against the V2 Casper execution engine.
    V2CasperWasm(HashAddr),
    /// An empty byte code record
    Empty,
}

impl ByteCodeAddr {
    /// Constructs a new Byte code address for Wasm.
    pub const fn new_wasm_addr(hash_addr: HashAddr) -> Self {
        Self::V1CasperWasm(hash_addr)
    }

    /// Returns the tag of the byte code address.
    pub fn tag(&self) -> ByteCodeKind {
        match self {
            Self::Empty => ByteCodeKind::Empty,
            Self::V1CasperWasm(_) => ByteCodeKind::V1CasperWasm,
            Self::V2CasperWasm(_) => ByteCodeKind::V2CasperWasm,
        }
    }

    /// Formats the `ByteCodeAddr` for users getting and putting.
    pub fn to_formatted_string(&self) -> String {
        format!("{}", self)
    }

    /// Parses a string formatted as per `Self::to_formatted_string()` into a
    /// `ByteCodeAddr`.
    pub fn from_formatted_string(input: &str) -> Result<Self, FromStrError> {
        if let Some(byte_code) = input.strip_prefix(BYTE_CODE_PREFIX) {
            let (addr_str, tag) = if let Some(str) = byte_code.strip_prefix(EMPTY_PREFIX) {
                (str, ByteCodeKind::Empty)
            } else if let Some(str) = byte_code.strip_prefix(V1_WASM_PREFIX) {
                (str, ByteCodeKind::V1CasperWasm)
            } else if let Some(str) = byte_code.strip_prefix(V2_WASM_PREFIX) {
                (str, ByteCodeKind::V2CasperWasm)
            } else {
                return Err(FromStrError::InvalidPrefix);
            };
            let addr = checksummed_hex::decode(addr_str).map_err(FromStrError::Hex)?;
            let byte_code_addr = HashAddr::try_from(addr.as_ref()).map_err(FromStrError::Hash)?;
            return match tag {
                ByteCodeKind::V1CasperWasm => Ok(ByteCodeAddr::V1CasperWasm(byte_code_addr)),
                ByteCodeKind::V2CasperWasm => Ok(ByteCodeAddr::V2CasperWasm(byte_code_addr)),
                ByteCodeKind::Empty => Ok(ByteCodeAddr::Empty),
            };
        }

        Err(FromStrError::InvalidPrefix)
    }
}

impl ToBytes for ByteCodeAddr {
    fn to_bytes(&self) -> Result<Vec<u8>, bytesrepr::Error> {
        let mut buffer = bytesrepr::allocate_buffer(self)?;
        self.write_bytes(&mut buffer)?;
        Ok(buffer)
    }

    fn serialized_length(&self) -> usize {
        U8_SERIALIZED_LENGTH
            + match self {
                Self::Empty => 0,
                Self::V1CasperWasm(_) => KEY_HASH_LENGTH,
                Self::V2CasperWasm(_) => KEY_HASH_LENGTH,
            }
    }

    fn write_bytes(&self, writer: &mut Vec<u8>) -> Result<(), Error> {
        match self {
            Self::Empty => writer.push(self.tag() as u8),
            Self::V1CasperWasm(addr) => {
                writer.push(self.tag() as u8);
                writer.extend(addr.to_bytes()?);
            }
            Self::V2CasperWasm(addr) => {
                writer.push(self.tag() as u8);
                writer.extend(addr.to_bytes()?);
            }
        }
        Ok(())
    }
}

impl FromBytes for ByteCodeAddr {
    fn from_bytes(bytes: &[u8]) -> Result<(Self, &[u8]), Error> {
        let (tag, remainder): (ByteCodeKind, &[u8]) = FromBytes::from_bytes(bytes)?;
        match tag {
            ByteCodeKind::Empty => Ok((ByteCodeAddr::Empty, remainder)),
            ByteCodeKind::V1CasperWasm => {
                let (addr, remainder) = HashAddr::from_bytes(remainder)?;
                Ok((ByteCodeAddr::new_wasm_addr(addr), remainder))
            }
            ByteCodeKind::V2CasperWasm => {
                let (addr, remainder) = HashAddr::from_bytes(remainder)?;
                Ok((ByteCodeAddr::V2CasperWasm(addr), remainder))
            }
        }
    }
}

impl Display for ByteCodeAddr {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match self {
            ByteCodeAddr::V1CasperWasm(addr) => {
                write!(
                    f,
                    "{}{}{}",
                    BYTE_CODE_PREFIX,
                    V1_WASM_PREFIX,
                    base16::encode_lower(&addr)
                )
            }
            ByteCodeAddr::V2CasperWasm(addr) => {
                write!(
                    f,
                    "{}{}{}",
                    BYTE_CODE_PREFIX,
                    V2_WASM_PREFIX,
                    base16::encode_lower(&addr)
                )
            }
            ByteCodeAddr::Empty => {
                write!(
                    f,
                    "{}{}{}",
                    BYTE_CODE_PREFIX,
                    EMPTY_PREFIX,
                    base16::encode_lower(&[0u8; 32])
                )
            }
        }
    }
}

impl Debug for ByteCodeAddr {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match self {
            ByteCodeAddr::V1CasperWasm(addr) => {
                write!(f, "ByteCodeAddr::V1CasperWasm({:?})", addr)
            }
            ByteCodeAddr::V2CasperWasm(addr) => {
                write!(f, "ByteCodeAddr::V2CasperWasm({:?})", addr)
            }
            ByteCodeAddr::Empty => {
                write!(f, "ByteCodeAddr::Empty")
            }
        }
    }
}

#[cfg(any(feature = "testing", test))]
impl Distribution<ByteCodeAddr> for Standard {
    fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> ByteCodeAddr {
        match rng.gen_range(0..=2) {
            0 => ByteCodeAddr::Empty,
            1 => ByteCodeAddr::V1CasperWasm(rng.gen()),
            2 => ByteCodeAddr::V2CasperWasm(rng.gen()),
            _ => unreachable!(),
        }
    }
}

/// A newtype wrapping a `HashAddr` which is the raw bytes of
/// the ByteCodeHash
#[derive(PartialOrd, Ord, PartialEq, Eq, Hash, Clone, Copy)]
#[cfg_attr(feature = "datasize", derive(DataSize))]
pub struct ByteCodeHash(HashAddr);

impl ByteCodeHash {
    /// Constructs a new `ByteCodeHash` from the raw bytes of the contract wasm hash.
    pub const fn new(value: HashAddr) -> ByteCodeHash {
        ByteCodeHash(value)
    }

    /// Returns the raw bytes of the contract hash as an array.
    pub fn value(&self) -> HashAddr {
        self.0
    }

    /// Returns the raw bytes of the contract hash as a `slice`.
    pub fn as_bytes(&self) -> &[u8] {
        &self.0
    }

    /// Formats the `ByteCodeHash` for users getting and putting.
    pub fn to_formatted_string(self) -> String {
        format!("{}{}", WASM_STRING_PREFIX, base16::encode_lower(&self.0),)
    }

    /// Parses a string formatted as per `Self::to_formatted_string()` into a
    /// `ByteCodeHash`.
    pub fn from_formatted_str(input: &str) -> Result<Self, FromStrError> {
        let remainder = input
            .strip_prefix(WASM_STRING_PREFIX)
            .ok_or(FromStrError::InvalidPrefix)?;
        let bytes = HashAddr::try_from(checksummed_hex::decode(remainder)?.as_ref())?;
        Ok(ByteCodeHash(bytes))
    }
}

impl Default for ByteCodeHash {
    fn default() -> Self {
        Self::new([0u8; KEY_HASH_LENGTH])
    }
}

impl Display for ByteCodeHash {
    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        write!(f, "{}", base16::encode_lower(&self.0))
    }
}

impl Debug for ByteCodeHash {
    fn fmt(&self, f: &mut Formatter) -> core::fmt::Result {
        write!(f, "ByteCodeHash({})", base16::encode_lower(&self.0))
    }
}

impl CLTyped for ByteCodeHash {
    fn cl_type() -> CLType {
        CLType::ByteArray(KEY_HASH_LENGTH as u32)
    }
}

impl ToBytes for ByteCodeHash {
    #[inline(always)]
    fn to_bytes(&self) -> Result<Vec<u8>, Error> {
        self.0.to_bytes()
    }

    #[inline(always)]
    fn serialized_length(&self) -> usize {
        self.0.serialized_length()
    }

    #[inline(always)]
    fn write_bytes(&self, writer: &mut Vec<u8>) -> Result<(), Error> {
        self.0.write_bytes(writer)
    }
}

impl FromBytes for ByteCodeHash {
    fn from_bytes(bytes: &[u8]) -> Result<(Self, &[u8]), Error> {
        let (bytes, rem) = FromBytes::from_bytes(bytes)?;
        Ok((ByteCodeHash::new(bytes), rem))
    }
}

impl From<[u8; KEY_HASH_LENGTH]> for ByteCodeHash {
    fn from(bytes: [u8; KEY_HASH_LENGTH]) -> Self {
        ByteCodeHash(bytes)
    }
}

impl Serialize for ByteCodeHash {
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        if serializer.is_human_readable() {
            self.to_formatted_string().serialize(serializer)
        } else {
            self.0.serialize(serializer)
        }
    }
}

impl<'de> Deserialize<'de> for ByteCodeHash {
    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        if deserializer.is_human_readable() {
            let formatted_string = String::deserialize(deserializer)?;
            ByteCodeHash::from_formatted_str(&formatted_string).map_err(SerdeError::custom)
        } else {
            let bytes = HashAddr::deserialize(deserializer)?;
            Ok(ByteCodeHash(bytes))
        }
    }
}

impl AsRef<[u8]> for ByteCodeHash {
    fn as_ref(&self) -> &[u8] {
        self.0.as_ref()
    }
}

impl TryFrom<&[u8]> for ByteCodeHash {
    type Error = TryFromSliceForContractHashError;

    fn try_from(bytes: &[u8]) -> Result<Self, TryFromSliceForContractHashError> {
        HashAddr::try_from(bytes)
            .map(ByteCodeHash::new)
            .map_err(|_| TryFromSliceForContractHashError(()))
    }
}

impl TryFrom<&Vec<u8>> for ByteCodeHash {
    type Error = TryFromSliceForContractHashError;

    fn try_from(bytes: &Vec<u8>) -> Result<Self, Self::Error> {
        HashAddr::try_from(bytes as &[u8])
            .map(ByteCodeHash::new)
            .map_err(|_| TryFromSliceForContractHashError(()))
    }
}

#[cfg(feature = "json-schema")]
impl JsonSchema for ByteCodeHash {
    fn schema_name() -> String {
        String::from("ByteCodeHash")
    }

    fn json_schema(gen: &mut SchemaGenerator) -> Schema {
        let schema = gen.subschema_for::<String>();
        let mut schema_object = schema.into_object();
        schema_object.metadata().description =
            Some("The hash address of the contract wasm".to_string());
        schema_object.into()
    }
}

/// The type of Byte code.
#[repr(u8)]
#[cfg_attr(feature = "datasize", derive(DataSize))]
#[cfg_attr(feature = "json-schema", derive(JsonSchema))]
#[derive(PartialEq, Eq, Clone, Copy, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub enum ByteCodeKind {
    /// Empty byte code.
    Empty = 0,
    /// Byte code to be executed with the version 1 Casper execution engine.
    V1CasperWasm = 1,
    /// Byte code to be executed with the version 2 Casper execution engine.
    V2CasperWasm = 2,
}

impl ToBytes for ByteCodeKind {
    fn to_bytes(&self) -> Result<Vec<u8>, Error> {
        (*self as u8).to_bytes()
    }

    fn serialized_length(&self) -> usize {
        U8_SERIALIZED_LENGTH
    }

    fn write_bytes(&self, writer: &mut Vec<u8>) -> Result<(), Error> {
        (*self as u8).write_bytes(writer)
    }
}

impl FromBytes for ByteCodeKind {
    fn from_bytes(bytes: &[u8]) -> Result<(Self, &[u8]), Error> {
        let (byte_code_kind, remainder) = u8::from_bytes(bytes)?;
        match byte_code_kind {
            byte_code_kind if byte_code_kind == ByteCodeKind::Empty as u8 => {
                Ok((ByteCodeKind::Empty, remainder))
            }
            byte_code_kind if byte_code_kind == ByteCodeKind::V1CasperWasm as u8 => {
                Ok((ByteCodeKind::V1CasperWasm, remainder))
            }
            byte_code_kind if byte_code_kind == ByteCodeKind::V2CasperWasm as u8 => {
                Ok((ByteCodeKind::V2CasperWasm, remainder))
            }
            _ => Err(Error::Formatting),
        }
    }
}

impl Display for ByteCodeKind {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match self {
            ByteCodeKind::Empty => {
                write!(f, "empty")
            }
            ByteCodeKind::V1CasperWasm => {
                write!(f, "v1-casper-wasm")
            }
            ByteCodeKind::V2CasperWasm => {
                write!(f, "v2-casper-wasm")
            }
        }
    }
}

#[cfg(any(feature = "testing", test))]
impl Distribution<ByteCodeKind> for Standard {
    fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> ByteCodeKind {
        match rng.gen_range(0..=2) {
            0 => ByteCodeKind::Empty,
            1 => ByteCodeKind::V1CasperWasm,
            2 => ByteCodeKind::V2CasperWasm,
            _ => unreachable!(),
        }
    }
}

/// A container for contract's Wasm bytes.
#[derive(PartialEq, Eq, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "datasize", derive(DataSize))]
#[cfg_attr(feature = "json-schema", derive(JsonSchema))]
pub struct ByteCode {
    kind: ByteCodeKind,
    bytes: Bytes,
}

impl Debug for ByteCode {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        if self.bytes.len() > BYTE_CODE_MAX_DISPLAY_LEN {
            write!(
                f,
                "ByteCode(0x{}...)",
                base16::encode_lower(&self.bytes[..BYTE_CODE_MAX_DISPLAY_LEN])
            )
        } else {
            write!(f, "ByteCode(0x{})", base16::encode_lower(&self.bytes))
        }
    }
}

impl ByteCode {
    /// Creates new Wasm object from bytes.
    pub fn new(kind: ByteCodeKind, bytes: Vec<u8>) -> Self {
        ByteCode {
            kind,
            bytes: bytes.into(),
        }
    }

    /// Consumes instance of [`ByteCode`] and returns its bytes.
    pub fn take_bytes(self) -> Vec<u8> {
        self.bytes.into()
    }

    /// Returns a slice of contained Wasm bytes.
    pub fn bytes(&self) -> &[u8] {
        self.bytes.as_ref()
    }

    /// Return the type of byte code.
    pub fn kind(&self) -> ByteCodeKind {
        self.kind
    }
}

impl ToBytes for ByteCode {
    fn to_bytes(&self) -> Result<Vec<u8>, Error> {
        let mut buffer = bytesrepr::allocate_buffer(self)?;
        self.write_bytes(&mut buffer)?;
        Ok(buffer)
    }

    fn serialized_length(&self) -> usize {
        self.kind.serialized_length() + self.bytes.serialized_length()
    }

    fn write_bytes(&self, writer: &mut Vec<u8>) -> Result<(), Error> {
        self.kind.write_bytes(writer)?;
        self.bytes.write_bytes(writer)?;
        Ok(())
    }
}

impl FromBytes for ByteCode {
    fn from_bytes(bytes: &[u8]) -> Result<(Self, &[u8]), Error> {
        let (kind, remainder) = ByteCodeKind::from_bytes(bytes)?;
        let (bytes, remainder) = Bytes::from_bytes(remainder)?;
        Ok((ByteCode { kind, bytes }, remainder))
    }
}

#[cfg(test)]
mod tests {
    use rand::RngCore;

    use super::*;
    use crate::testing::TestRng;

    #[test]
    fn debug_repr_of_short_wasm() {
        const SIZE: usize = 8;
        let wasm_bytes = vec![0; SIZE];
        let byte_code = ByteCode::new(ByteCodeKind::V1CasperWasm, wasm_bytes);
        assert_eq!(format!("{:?}", byte_code), "ByteCode(0x0000000000000000)");
    }

    #[test]
    fn debug_repr_of_long_wasm() {
        const SIZE: usize = 65;
        let wasm_bytes = vec![0; SIZE];
        let byte_code = ByteCode::new(ByteCodeKind::V1CasperWasm, wasm_bytes);
        // String output is less than the bytes itself
        assert_eq!(
            format!("{:?}", byte_code),
            "ByteCode(0x00000000000000000000000000000000...)"
        );
    }

    #[test]
    fn byte_code_bytesrepr_roundtrip() {
        let rng = &mut TestRng::new();
        let byte_code = ByteCode::new(rng.gen(), vec![]);
        bytesrepr::test_serialization_roundtrip(&byte_code);

        let mut buffer = vec![0u8; rng.gen_range(1..100)];
        rng.fill_bytes(buffer.as_mut());
        let byte_code = ByteCode::new(rng.gen(), buffer);
        bytesrepr::test_serialization_roundtrip(&byte_code);
    }

    #[test]
    fn contract_wasm_hash_from_slice() {
        let bytes: Vec<u8> = (0..32).collect();
        let byte_code_hash = HashAddr::try_from(&bytes[..]).expect("should create byte code hash");
        let contract_hash = ByteCodeHash::new(byte_code_hash);
        assert_eq!(&bytes, &contract_hash.as_bytes());
    }

    #[test]
    fn contract_wasm_hash_from_str() {
        let byte_code_hash = ByteCodeHash([3; KEY_HASH_LENGTH]);
        let encoded = byte_code_hash.to_formatted_string();
        let decoded = ByteCodeHash::from_formatted_str(&encoded).unwrap();
        assert_eq!(byte_code_hash, decoded);

        let invalid_prefix =
            "contractwasm-0000000000000000000000000000000000000000000000000000000000000000";
        assert!(ByteCodeHash::from_formatted_str(invalid_prefix).is_err());

        let short_addr =
            "contract-wasm-00000000000000000000000000000000000000000000000000000000000000";
        assert!(ByteCodeHash::from_formatted_str(short_addr).is_err());

        let long_addr =
            "contract-wasm-000000000000000000000000000000000000000000000000000000000000000000";
        assert!(ByteCodeHash::from_formatted_str(long_addr).is_err());

        let invalid_hex =
            "contract-wasm-000000000000000000000000000000000000000000000000000000000000000g";
        assert!(ByteCodeHash::from_formatted_str(invalid_hex).is_err());
    }

    #[test]
    fn byte_code_addr_from_str() {
        let empty_addr = ByteCodeAddr::Empty;
        let encoded = empty_addr.to_formatted_string();
        let decoded = ByteCodeAddr::from_formatted_string(&encoded).unwrap();
        assert_eq!(empty_addr, decoded);

        let wasm_addr = ByteCodeAddr::V1CasperWasm([3; 32]);
        let encoded = wasm_addr.to_formatted_string();
        let decoded = ByteCodeAddr::from_formatted_string(&encoded).unwrap();
        assert_eq!(wasm_addr, decoded);
    }

    #[test]
    fn byte_code_serialization_roundtrip() {
        let rng = &mut TestRng::new();
        let wasm_addr = ByteCodeAddr::V1CasperWasm(rng.gen());
        bytesrepr::test_serialization_roundtrip(&wasm_addr);

        let empty_addr = ByteCodeAddr::Empty;
        bytesrepr::test_serialization_roundtrip(&empty_addr);
    }

    #[test]
    fn contract_wasm_hash_bytesrepr_roundtrip() {
        let rng = &mut TestRng::new();
        let byte_code_hash = ByteCodeHash(rng.gen());
        bytesrepr::test_serialization_roundtrip(&byte_code_hash);
    }

    #[test]
    fn contract_wasm_hash_bincode_roundtrip() {
        let rng = &mut TestRng::new();
        let byte_code_hash = ByteCodeHash(rng.gen());
        let serialized = bincode::serialize(&byte_code_hash).unwrap();
        let deserialized = bincode::deserialize(&serialized).unwrap();
        assert_eq!(byte_code_hash, deserialized)
    }

    #[test]
    fn contract_wasm_hash_json_roundtrip() {
        let rng = &mut TestRng::new();
        let byte_code_hash = ByteCodeHash(rng.gen());
        let json_string = serde_json::to_string_pretty(&byte_code_hash).unwrap();
        let decoded = serde_json::from_str(&json_string).unwrap();
        assert_eq!(byte_code_hash, decoded)
    }
}