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
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
// Copyright (c) 2022 MASSA LABS <info@massa.net>

use crate::error::ModelsError;
use crate::prehash::PreHashed;
use massa_hash::{Hash, HashDeserializer, HASH_SIZE_BYTES};
use massa_serialization::{
    DeserializeError, Deserializer, SerializeError, Serializer, U64VarIntDeserializer,
    U64VarIntSerializer,
};
use massa_signature::{PublicKey, PublicKeyV0};
use nom::error::{context, ContextError, ErrorKind, ParseError};
use nom::{IResult, Parser};
use serde::{Deserialize, Serialize};
use std::ops::Bound::{Excluded, Included};
use std::str::FromStr;
use transition::Versioned;

/// Top level address representation that can differentiate between User and SC address
#[derive(Clone, Copy, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum Address {
    #[allow(missing_docs)]
    User(UserAddress),
    #[allow(missing_docs)]
    SC(SCAddress),
}

#[allow(missing_docs)]
/// Derived from a public key.
#[transition::versioned(versions("0"))]
#[derive(Clone, Copy, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct SCAddress(pub Hash);

#[allow(missing_docs)]
/// Derived from a public key.
#[transition::versioned(versions("0"))]
#[derive(Clone, Copy, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct UserAddress(pub Hash);

const ADDRESS_PREFIX: char = 'A';
// serialized with varint
const USER_PREFIX: u64 = 0;
const SC_PREFIX: u64 = 1;

impl std::fmt::Display for Address {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            Address::User(address) => address.fmt(f),
            Address::SC(address) => address.fmt(f),
        }
    }
}

impl std::fmt::Display for UserAddress {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            UserAddress::UserAddressV0(address) => address.fmt(f),
        }
    }
}

impl std::fmt::Display for SCAddress {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            SCAddress::SCAddressV0(address) => address.fmt(f),
        }
    }
}

#[transition::impl_version(versions("0"))]
impl std::fmt::Display for UserAddress {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        let u64_serializer = U64VarIntSerializer::new();
        let mut bytes: Vec<u8> = Vec::new();
        u64_serializer
            .serialize(&Self::VERSION, &mut bytes)
            .map_err(|_| std::fmt::Error)?;
        bytes.extend(self.0.to_bytes());
        write!(
            f,
            "{}U{}",
            ADDRESS_PREFIX,
            bs58::encode(bytes).with_check().into_string()
        )
    }
}

#[transition::impl_version(versions("0"))]
impl std::fmt::Display for SCAddress {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        let u64_serializer = U64VarIntSerializer::new();
        let mut bytes: Vec<u8> = Vec::new();
        u64_serializer
            .serialize(&Self::VERSION, &mut bytes)
            .map_err(|_| std::fmt::Error)?;
        bytes.extend(self.0.to_bytes());
        write!(
            f,
            "{}S{}",
            ADDRESS_PREFIX,
            bs58::encode(bytes).with_check().into_string()
        )
    }
}

// See https://github.com/massalabs/massa/pull/3479#issuecomment-1408694720
// as to why more information is not provided
impl std::fmt::Debug for Address {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "{}", self)
    }
}

impl ::serde::Serialize for Address {
    fn serialize<S: ::serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
        match self {
            Address::User(address) => address.serialize(s),
            Address::SC(address) => address.serialize(s),
        }
    }
}

impl ::serde::Serialize for UserAddress {
    fn serialize<S: ::serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
        match self {
            UserAddress::UserAddressV0(address) => address.serialize(s),
        }
    }
}

impl ::serde::Serialize for SCAddress {
    fn serialize<S: ::serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
        match self {
            SCAddress::SCAddressV0(address) => address.serialize(s),
        }
    }
}

#[transition::impl_version(versions("0"))]
impl ::serde::Serialize for UserAddress {
    fn serialize<S: ::serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
        if s.is_human_readable() {
            s.collect_str(&self.to_string())
        } else {
            s.serialize_bytes(&self.to_prefixed_bytes())
        }
    }
}

#[transition::impl_version(versions("0"))]
impl ::serde::Serialize for SCAddress {
    fn serialize<S: ::serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
        if s.is_human_readable() {
            s.collect_str(&self.to_string())
        } else {
            s.serialize_bytes(&self.to_prefixed_bytes())
        }
    }
}

impl<'de> ::serde::Deserialize<'de> for Address {
    fn deserialize<D: ::serde::Deserializer<'de>>(d: D) -> Result<Address, D::Error> {
        if d.is_human_readable() {
            struct AddressVisitor;

            impl<'de> ::serde::de::Visitor<'de> for AddressVisitor {
                type Value = Address;

                fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
                    formatter.write_str("A + {U | S} + base58::encode(version + hash)")
                }

                fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
                where
                    E: ::serde::de::Error,
                {
                    if let Ok(v_str) = std::str::from_utf8(v) {
                        Address::from_str(v_str).map_err(E::custom)
                    } else {
                        Err(E::invalid_value(::serde::de::Unexpected::Bytes(v), &self))
                    }
                }

                fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
                where
                    E: ::serde::de::Error,
                {
                    Address::from_str(v).map_err(E::custom)
                }
            }
            d.deserialize_str(AddressVisitor)
        } else {
            struct BytesVisitor;

            impl<'de> ::serde::de::Visitor<'de> for BytesVisitor {
                type Value = Address;

                fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
                    formatter.write_str("[u64varint-of-addr-variant][u64varint-of-version][bytes]")
                }

                fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
                where
                    E: ::serde::de::Error,
                {
                    AddressDeserializer::new()
                        .deserialize::<DeserializeError>(v)
                        .map_err(E::custom)
                        .map(|r| r.1)
                }
            }

            d.deserialize_bytes(BytesVisitor)
        }
    }
}

impl FromStr for Address {
    type Err = ModelsError;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let err = Err(ModelsError::AddressParseError(s.to_string()));

        // Handle the prefix ("A{U|S}")
        let mut chars = s.chars();
        let Some(ADDRESS_PREFIX) = chars.next() else {
            return err;
        };
        let Some(pref) = chars.next() else {
            return err;
        };

        let res = match pref {
            'U' => Address::User(UserAddress::from_str_without_prefixed_type(chars.as_str())?),
            'S' => Address::SC(SCAddress::from_str_without_prefixed_type(chars.as_str())?),
            _ => return err,
        };
        Ok(res)
    }
}

impl Address {
    /// Gets the associated thread. Depends on the `thread_count`
    /// Returns None for SC addresses, even though we may want to get_thread on them in the future
    pub fn get_thread(&self, thread_count: u8) -> u8 {
        match self {
            Address::User(addr) => addr.get_thread(thread_count),
            // TODO: tmp behaviour, discuss how we would want this to work
            Address::SC(_addr) => 0,
        }
    }

    /// Computes the address associated with the given public key.
    /// Depends on the Public Key version
    pub fn from_public_key(public_key: &PublicKey) -> Self {
        Address::User(UserAddress::from_public_key(public_key))
    }

    /// Serialize the address as bytes. Includes the type and version prefixes
    pub fn to_prefixed_bytes(self) -> Vec<u8> {
        match self {
            Address::User(addr) => addr.to_prefixed_bytes(),
            Address::SC(addr) => addr.to_prefixed_bytes(),
        }
    }
}

impl UserAddress {
    /// Gets the associated thread. Depends on the `thread_count`
    fn get_thread(&self, thread_count: u8) -> u8 {
        match self {
            UserAddress::UserAddressV0(addr) => addr.get_thread(thread_count),
        }
    }

    /// Computes the address associated with the given public key
    fn from_public_key(public_key: &PublicKey) -> Self {
        match public_key {
            PublicKey::PublicKeyV0(pk) => {
                UserAddressVariant!["0"](<UserAddress!["0"]>::from_public_key(pk))
            }
        }
    }

    fn from_str_without_prefixed_type(s: &str) -> Result<Self, ModelsError> {
        let decoded_bs58_check = bs58::decode(s).with_check(None).into_vec().map_err(|err| {
            ModelsError::AddressParseError(format!(
                "in UserAddress from_str_without_prefixed_type: {}",
                err
            ))
        })?;
        let u64_deserializer = U64VarIntDeserializer::new(Included(0), Included(u64::MAX));
        let (rest, version) = u64_deserializer
            .deserialize::<DeserializeError>(&decoded_bs58_check[..])
            .map_err(|err| {
                ModelsError::AddressParseError(format!(
                    "in UserAddress from_str_without_prefixed_type: {}",
                    err
                ))
            })?;

        match version {
            <UserAddress!["0"]>::VERSION => Ok(UserAddressVariant!["0"](
                <UserAddress!["0"]>::from_bytes(rest)?,
            )),
            unhandled_version => Err(ModelsError::AddressParseError(format!(
                "version {} is not handled for UserAddress",
                unhandled_version
            ))),
        }
    }

    /// Serialize the address as bytes. Includes the type and version prefixes
    pub fn to_prefixed_bytes(self) -> Vec<u8> {
        match self {
            UserAddress::UserAddressV0(addr) => addr.to_prefixed_bytes(),
        }
    }
}

#[transition::impl_version(versions("0"))]
impl UserAddress {
    /// Fetches the version of the UserAddress
    pub fn get_version(&self) -> u64 {
        Self::VERSION
    }

    /// Serialize the address as bytes. Includes the type and version prefixes
    fn to_prefixed_bytes(self) -> Vec<u8> {
        let mut buff = vec![];
        let addr_type_ser = U64VarIntSerializer::new();
        let addr_vers_ser = U64VarIntSerializer::new();
        addr_type_ser
            .serialize(&USER_PREFIX, &mut buff)
            .expect("impl always returns Ok(())");
        addr_vers_ser
            .serialize(&Self::VERSION, &mut buff)
            .expect("impl always returns Ok(())");
        buff.extend_from_slice(&self.0.to_bytes()[..]);
        buff
    }

    /// Gets the associated thread. Depends on the `thread_count`
    fn get_thread(&self, thread_count: u8) -> u8 {
        (self.0.to_bytes()[0])
            .checked_shr(8 - thread_count.trailing_zeros())
            .unwrap_or(0)
    }

    /// Deserialize the address without considering the version byte
    fn from_bytes(data: &[u8]) -> Result<UserAddress, ModelsError> {
        Ok(UserAddress(Hash::from_bytes(&data.try_into().map_err(
            |_| {
                ModelsError::BufferError(format!(
                    "expected a buffer of size {}, but found a size of {}",
                    HASH_SIZE_BYTES,
                    &data.len()
                ))
            },
        )?)))
    }
}

#[transition::impl_version(versions("0"), structures("UserAddress", "PublicKey"))]
impl UserAddress {
    /// Computes address associated with given public key
    pub fn from_public_key(public_key: &PublicKey) -> Self {
        UserAddress(Hash::compute_from(&public_key.to_bytes()))
    }
}

#[transition::impl_version(versions("0"))]
impl UserAddress {}

impl SCAddress {
    fn from_str_without_prefixed_type(s: &str) -> Result<Self, ModelsError> {
        let decoded_bs58_check = bs58::decode(s).with_check(None).into_vec().map_err(|err| {
            ModelsError::AddressParseError(format!(
                "in SCAddress from_str_without_prefixed_type: {}",
                err
            ))
        })?;
        let u64_deserializer = U64VarIntDeserializer::new(Included(0), Included(u64::MAX));
        let (rest, version) = u64_deserializer
            .deserialize::<DeserializeError>(&decoded_bs58_check[..])
            .map_err(|err| {
                ModelsError::AddressParseError(format!(
                    "in SCAddress from_str_without_prefixed_type: {}",
                    err
                ))
            })?;

        match version {
            <SCAddress!["0"]>::VERSION => {
                Ok(SCAddressVariant!["0"](<SCAddress!["0"]>::from_bytes(rest)?))
            }
            unhandled_version => Err(ModelsError::AddressParseError(format!(
                "version {} is not handled for SCAddress",
                unhandled_version
            ))),
        }
    }

    /// Serialize the address as bytes. Includes the type and version prefixes
    pub fn to_prefixed_bytes(self) -> Vec<u8> {
        match self {
            SCAddress::SCAddressV0(addr) => addr.to_prefixed_bytes(),
        }
    }
}

#[transition::impl_version(versions("0"))]
impl SCAddress {
    /// Fetches the version of the SC Address
    pub fn get_version(&self) -> u64 {
        Self::VERSION
    }
}

#[transition::impl_version(versions("0"))]
impl SCAddress {
    /// Serialize the address as bytes. Includes the type and version prefixes
    pub fn to_prefixed_bytes(self) -> Vec<u8> {
        let mut buff = vec![];
        let addr_type_ser = U64VarIntSerializer::new();
        let addr_vers_ser = U64VarIntSerializer::new();
        addr_type_ser
            .serialize(&SC_PREFIX, &mut buff)
            .expect("impl always returns Ok(())");
        addr_vers_ser
            .serialize(&Self::VERSION, &mut buff)
            .expect("impl always returns Ok(())");
        buff.extend_from_slice(&self.0.to_bytes()[..]);
        buff
    }

    /// Deserialize the address without considering the version byte
    fn from_bytes(data: &[u8]) -> Result<SCAddress, ModelsError> {
        Ok(SCAddress(Hash::from_bytes(&data.try_into().map_err(
            |_| {
                ModelsError::BufferError(format!(
                    "expected a buffer of size {}, but found a size of {}",
                    HASH_SIZE_BYTES,
                    &data.len()
                ))
            },
        )?)))
    }
}

/* /!\ SCAddressV1 not prehashed! */
impl PreHashed for Address {}

/// Serializer for `Address`
#[derive(Default, Clone)]
pub struct AddressSerializer {
    type_serializer: U64VarIntSerializer,
    version_serializer: U64VarIntSerializer,
}

impl AddressSerializer {
    /// Serializes an `Address` into a `Vec<u8>`
    pub fn new() -> Self {
        Self {
            type_serializer: U64VarIntSerializer::new(),
            version_serializer: U64VarIntSerializer::new(),
        }
    }
}

impl Serializer<Address> for AddressSerializer {
    fn serialize(&self, value: &Address, buffer: &mut Vec<u8>) -> Result<(), SerializeError> {
        match value {
            Address::User(addr) => self.serialize(addr, buffer),
            Address::SC(addr) => self.serialize(addr, buffer),
        }
    }
}

impl Serializer<UserAddress> for AddressSerializer {
    fn serialize(&self, value: &UserAddress, buffer: &mut Vec<u8>) -> Result<(), SerializeError> {
        self.type_serializer.serialize(&USER_PREFIX, buffer)?;
        match value {
            UserAddress::UserAddressV0(addr) => self.serialize(addr, buffer),
        }
    }
}

#[transition::impl_version(versions("0"), structures("UserAddress"))]
impl Serializer<UserAddress> for AddressSerializer {
    fn serialize(&self, value: &UserAddress, buffer: &mut Vec<u8>) -> Result<(), SerializeError> {
        self.version_serializer
            .serialize(&value.get_version(), buffer)?;
        buffer.extend_from_slice(&value.0.into_bytes());
        Ok(())
    }
}

impl Serializer<SCAddress> for AddressSerializer {
    fn serialize(&self, value: &SCAddress, buffer: &mut Vec<u8>) -> Result<(), SerializeError> {
        self.type_serializer.serialize(&SC_PREFIX, buffer)?;
        match value {
            SCAddress::SCAddressV0(addr) => self.serialize(addr, buffer),
        }
    }
}

#[transition::impl_version(versions("0"), structures("SCAddress"))]
impl Serializer<SCAddress> for AddressSerializer {
    fn serialize(&self, value: &SCAddress, buffer: &mut Vec<u8>) -> Result<(), SerializeError> {
        self.version_serializer
            .serialize(&value.get_version(), buffer)?;
        buffer.extend_from_slice(&value.0.into_bytes());
        Ok(())
    }
}

/// Deserializer for `Address`
#[derive(Clone)]
pub struct AddressDeserializer {
    type_deserializer: U64VarIntDeserializer,
    version_deserializer: U64VarIntDeserializer,
    hash_deserializer: HashDeserializer,
}

impl Default for AddressDeserializer {
    fn default() -> Self {
        AddressDeserializer::new()
    }
}

impl AddressDeserializer {
    /// Creates a new deserializer for `Address`
    pub const fn new() -> Self {
        Self {
            type_deserializer: U64VarIntDeserializer::new(Included(0), Included(1)),
            version_deserializer: U64VarIntDeserializer::new(Included(0), Excluded(u64::MAX)),
            hash_deserializer: HashDeserializer::new(),
        }
    }
}

impl Deserializer<Address> for AddressDeserializer {
    fn deserialize<'a, E: ParseError<&'a [u8]> + ContextError<&'a [u8]>>(
        &self,
        buffer: &'a [u8],
    ) -> IResult<&'a [u8], Address, E> {
        // Verify that we at least have a version and something else
        if buffer.len() < 2 {
            return Err(nom::Err::Error(E::from_error_kind(buffer, ErrorKind::Eof)));
        }
        let (rest, addr_type) =
            self.type_deserializer
                .deserialize(buffer)
                .map_err(|_: nom::Err<E>| {
                    nom::Err::Error(E::from_error_kind(buffer, ErrorKind::Eof))
                })?;
        match addr_type {
            USER_PREFIX => {
                let (rest, addr) = self.deserialize(rest)?;
                Ok((rest, Address::User(addr)))
            }
            SC_PREFIX => {
                let (rest, addr) = self.deserialize(rest)?;
                Ok((rest, Address::SC(addr)))
            }
            _ => Err(nom::Err::Error(E::from_error_kind(buffer, ErrorKind::Eof))),
        }
    }
}

impl Deserializer<UserAddress> for AddressDeserializer {
    fn deserialize<'a, E: ParseError<&'a [u8]> + ContextError<&'a [u8]>>(
        &self,
        buffer: &'a [u8],
    ) -> IResult<&'a [u8], UserAddress, E> {
        // Verify that we at least have a version and something else
        if buffer.len() < 2 {
            return Err(nom::Err::Error(E::from_error_kind(buffer, ErrorKind::Eof)));
        }
        let (rest, addr_vers) =
            self.version_deserializer
                .deserialize(buffer)
                .map_err(|_: nom::Err<E>| {
                    nom::Err::Error(E::from_error_kind(buffer, ErrorKind::Eof))
                })?;
        match addr_vers {
            <UserAddress!["0"]>::VERSION => {
                let (rest, addr) = self.deserialize(rest)?;
                Ok((rest, UserAddressVariant!["0"](addr)))
            }
            _ => Err(nom::Err::Error(E::from_error_kind(buffer, ErrorKind::Eof))),
        }
    }
}

#[transition::impl_version(versions("0"), structures("UserAddress"))]
impl Deserializer<UserAddress> for AddressDeserializer {
    fn deserialize<'a, E: ParseError<&'a [u8]> + ContextError<&'a [u8]>>(
        &self,
        buffer: &'a [u8],
    ) -> IResult<&'a [u8], UserAddress, E> {
        context("Failed UserAddress deserialization", |input| {
            self.hash_deserializer.deserialize(input)
        })
        .map(UserAddress)
        .parse(buffer)
    }
}

impl Deserializer<SCAddress> for AddressDeserializer {
    fn deserialize<'a, E: ParseError<&'a [u8]> + ContextError<&'a [u8]>>(
        &self,
        buffer: &'a [u8],
    ) -> IResult<&'a [u8], SCAddress, E> {
        // Verify that we at least have a version and something else
        if buffer.len() < 2 {
            return Err(nom::Err::Error(E::from_error_kind(buffer, ErrorKind::Eof)));
        }
        let (rest, addr_vers) =
            self.version_deserializer
                .deserialize(buffer)
                .map_err(|_: nom::Err<E>| {
                    nom::Err::Error(E::from_error_kind(buffer, ErrorKind::Eof))
                })?;
        match addr_vers {
            <SCAddress!["0"]>::VERSION => {
                let (rest, addr) = self.deserialize(rest)?;
                Ok((rest, SCAddressVariant!["0"](addr)))
            }
            _ => Err(nom::Err::Error(E::from_error_kind(buffer, ErrorKind::Eof))),
        }
    }
}

#[transition::impl_version(versions("0"), structures("SCAddress"))]
impl Deserializer<SCAddress> for AddressDeserializer {
    fn deserialize<'a, E: ParseError<&'a [u8]> + ContextError<&'a [u8]>>(
        &self,
        buffer: &'a [u8],
    ) -> IResult<&'a [u8], SCAddress, E> {
        context("Failed SCAddress deserialization", |input| {
            self.hash_deserializer.deserialize(input)
        })
        .map(SCAddress)
        .parse(buffer)
    }
}

/// Info for a given address on a given cycle
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ExecutionAddressCycleInfo {
    /// cycle number
    pub cycle: u64,
    /// true if that cycle is final
    pub is_final: bool,
    /// `ok_count` blocks were created by this address during that cycle
    pub ok_count: u64,
    /// `ok_count` blocks were missed by this address during that cycle
    pub nok_count: u64,
    /// number of active rolls the address had at that cycle (if still available)
    pub active_rolls: Option<u64>,
}

#[cfg(test)]
mod test {
    use crate::config::THREAD_COUNT;

    use super::*;

    #[test]
    fn test_address() {
        let expected_user_addr =
            Address::from_str("AU12fZLkHnLED3okr8Lduyty7dz9ZKkd24xMCc2JJWPcdmfn2eUEx").unwrap();
        let expected_sc_addr =
            Address::from_str("AS12fZLkHnLED3okr8Lduyty7dz9ZKkd24xMCc2JJWPcdmfn2eUEx").unwrap();

        let hash = massa_hash::Hash::compute_from("ADDR".as_bytes());
        let actual_user_addr = Address::User(UserAddress::UserAddressV0(UserAddressV0(hash)));
        let actual_sc_addr = Address::SC(SCAddress::SCAddressV0(SCAddressV0(hash)));

        assert_eq!(actual_user_addr, expected_user_addr);
        assert_eq!(actual_sc_addr, expected_sc_addr);
    }

    #[test]
    fn test_address_errors() {
        let expected_error_0 = "address parsing error: UnexpectedAddress".to_string();
        let actual_error_0 = Address::from_str("UnexpectedAddress")
            .unwrap_err()
            .to_string();

        let expected_error_1 = "address parsing error: in SCAddress from_str_without_prefixed_type: invalid checksum, calculated checksum: '[255, 246, 254, 58]', expected checksum: [188, 126, 221, 98]".to_string();
        let actual_error_1 = Address::from_str("ASSomeUnexpectedAddress")
            .unwrap_err()
            .to_string();

        let expected_error_2 = "address parsing error: in SCAddress from_str_without_prefixed_type: provided string contained invalid character '_' at byte 0".to_string();
        let actual_error_2 = Address::from_str("AS_SomeUnexpectedAddress")
            .unwrap_err()
            .to_string();

        let expected_error_3 = "address parsing error: in UserAddress from_str_without_prefixed_type: invalid checksum, calculated checksum: '[255, 246, 254, 58]', expected checksum: [188, 126, 221, 98]".to_string();
        let actual_error_3 = Address::from_str("AUSomeUnexpectedAddress")
            .unwrap_err()
            .to_string();

        let expected_error_4 = "address parsing error: in UserAddress from_str_without_prefixed_type: provided string contained invalid character '_' at byte 0".to_string();
        let actual_error_4 = Address::from_str("AU_SomeUnexpectedAddress")
            .unwrap_err()
            .to_string();

        assert_eq!(actual_error_0, expected_error_0);
        assert_eq!(actual_error_1, expected_error_1);
        assert_eq!(actual_error_2, expected_error_2);
        assert_eq!(actual_error_3, expected_error_3);
        assert_eq!(actual_error_4, expected_error_4);
    }

    #[test]
    fn test_address_get_thread() {
        let hash = massa_hash::Hash::compute_from("ADDR".as_bytes());

        let user_addr_0 = Address::User(UserAddress::UserAddressV0(UserAddressV0(hash)));
        let thread_addr_0 = user_addr_0.get_thread(THREAD_COUNT);
        let hash = massa_hash::Hash::compute_from("ADDR2".as_bytes());

        let user_addr_1 = Address::User(UserAddress::UserAddressV0(UserAddressV0(hash)));
        let thread_addr_1 = user_addr_1.get_thread(THREAD_COUNT);

        assert_ne!(thread_addr_0, thread_addr_1);
    }

    #[test]
    fn test_address_serde() {
        let expected_addr =
            Address::from_str("AU12fZLkHnLED3okr8Lduyty7dz9ZKkd24xMCc2JJWPcdmfn2eUEx").unwrap();

        let serialized = serde_json::to_string(&expected_addr).unwrap();
        let actual_addr: Address = serde_json::from_str(&serialized).unwrap();

        assert_eq!(actual_addr, expected_addr);
    }
}