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
use crate::accounting::account::{DelegationRatio, DelegationType, DELEGATION_RATIO_MAX_DECLS};
use crate::certificate::CertificateSlice;
use crate::transaction::{
    AccountBindingSignature, Payload, PayloadAuthData, PayloadData, PayloadSlice,
    UnspecifiedAccountIdentifier,
};

use chain_core::property::WriteError;
use chain_core::{
    packer::Codec,
    property::{Deserialize, ReadError, Serialize},
};
use std::marker::PhantomData;
use typed_bytes::{ByteArray, ByteBuilder};

/// A self delegation to a specific StakePoolId.
///
/// This structure is not sufficient to identify the owner, and instead we rely on a special
/// authenticated transaction, which has 1 input.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct OwnerStakeDelegation {
    pub delegation: DelegationType,
}

impl OwnerStakeDelegation {
    pub fn serialize_in(&self, bb: ByteBuilder<Self>) -> ByteBuilder<Self> {
        bb.sub(|sb| serialize_delegation_type(&self.delegation, sb))
    }
    pub fn serialize(&self) -> ByteArray<Self> {
        self.serialize_in(ByteBuilder::new()).finalize()
    }

    pub fn get_delegation_type(&self) -> &DelegationType {
        &self.delegation
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StakeDelegation {
    pub account_id: UnspecifiedAccountIdentifier,
    pub delegation: DelegationType,
}

impl StakeDelegation {
    pub fn serialize_in(&self, bb: ByteBuilder<Self>) -> ByteBuilder<Self> {
        bb.bytes(self.account_id.as_ref())
            .sub(|sb| serialize_delegation_type(&self.delegation, sb))
    }
    pub fn serialize(&self) -> ByteArray<Self> {
        self.serialize_in(ByteBuilder::new()).finalize()
    }

    pub fn get_delegation_type(&self) -> &DelegationType {
        &self.delegation
    }
}

impl Serialize for OwnerStakeDelegation {
    fn serialized_size(&self) -> usize {
        let delegation_buf =
            serialize_delegation_type(&self.delegation, ByteBuilder::new()).finalize_as_vec();
        delegation_buf.len()
    }

    fn serialize<W: std::io::Write>(&self, codec: &mut Codec<W>) -> Result<(), WriteError> {
        let delegation_buf =
            serialize_delegation_type(&self.delegation, ByteBuilder::new()).finalize_as_vec();
        codec.put_bytes(delegation_buf.as_slice())
    }
}

impl Deserialize for OwnerStakeDelegation {
    fn deserialize<R: std::io::Read>(codec: &mut Codec<R>) -> Result<Self, ReadError> {
        let delegation = deserialize_delegation_type(codec)?;
        Ok(Self { delegation })
    }
}

impl Payload for OwnerStakeDelegation {
    const HAS_DATA: bool = true;
    const HAS_AUTH: bool = false;
    type Auth = ();
    fn payload_data(&self) -> PayloadData<Self> {
        PayloadData(
            self.serialize_in(ByteBuilder::new())
                .finalize_as_vec()
                .into(),
            PhantomData,
        )
    }
    fn payload_auth_data(_: &Self::Auth) -> PayloadAuthData<Self> {
        PayloadAuthData(Vec::with_capacity(0).into(), PhantomData)
    }
    fn payload_to_certificate_slice(p: PayloadSlice<'_, Self>) -> Option<CertificateSlice<'_>> {
        Some(CertificateSlice::from(p))
    }
}

impl Serialize for StakeDelegation {
    fn serialized_size(&self) -> usize {
        let delegation_buf =
            serialize_delegation_type(&self.delegation, ByteBuilder::new()).finalize_as_vec();
        self.account_id.as_ref().len() + delegation_buf.len()
    }

    fn serialize<W: std::io::Write>(&self, codec: &mut Codec<W>) -> Result<(), WriteError> {
        let delegation_buf =
            serialize_delegation_type(&self.delegation, ByteBuilder::new()).finalize_as_vec();
        codec.put_bytes(self.account_id.as_ref())?;
        codec.put_bytes(delegation_buf.as_slice())
    }
}

impl Deserialize for StakeDelegation {
    fn deserialize<R: std::io::Read>(codec: &mut Codec<R>) -> Result<Self, ReadError> {
        let account_identifier = <[u8; 32]>::deserialize(codec)?;
        let delegation = deserialize_delegation_type(codec)?;
        Ok(StakeDelegation {
            account_id: account_identifier.into(),
            delegation,
        })
    }
}

impl Payload for StakeDelegation {
    const HAS_DATA: bool = true;
    const HAS_AUTH: bool = true;
    type Auth = AccountBindingSignature;
    fn payload_data(&self) -> PayloadData<Self> {
        PayloadData(
            self.serialize_in(ByteBuilder::new())
                .finalize_as_vec()
                .into(),
            PhantomData,
        )
    }

    fn payload_auth_data(auth: &Self::Auth) -> PayloadAuthData<Self> {
        let bb = auth.serialize_in(ByteBuilder::new()).finalize_as_vec();
        PayloadAuthData(bb.into(), PhantomData)
    }
    fn payload_to_certificate_slice(p: PayloadSlice<'_, Self>) -> Option<CertificateSlice<'_>> {
        Some(CertificateSlice::from(p))
    }
}

// Format is either:
// 0 (byte)
// 1 (byte)     POOL_ID (32 bytes)
// PARTS (byte) #POOLS (bytes) [ POOL_PART (1 byte) POOL_ID (32 bytes)] (repeated #POOLS time)
fn serialize_delegation_type(
    d: &DelegationType,
    bb: ByteBuilder<DelegationType>,
) -> ByteBuilder<DelegationType> {
    match d {
        DelegationType::NonDelegated => bb.u8(0),
        DelegationType::Full(pool_id) => bb.u8(1).bytes(pool_id.as_ref()),
        DelegationType::Ratio(ratio) => {
            let parts = ratio.parts();
            assert!(parts >= 2);
            bb.u8(parts)
                .iter8(ratio.pools().iter(), |b, (pool_id, pool_part)| {
                    b.u8(*pool_part).bytes(pool_id.as_ref())
                })
        }
    }
}

fn deserialize_delegation_type<R: std::io::Read>(
    codec: &mut Codec<R>,
) -> Result<DelegationType, ReadError> {
    let parts = codec.get_u8()?;
    match parts {
        0 => Ok(DelegationType::NonDelegated),
        1 => {
            let pool_id = <[u8; 32]>::deserialize(codec)?.into();
            Ok(DelegationType::Full(pool_id))
        }
        _ => {
            let sz = codec.get_u8()?;
            if sz as usize > DELEGATION_RATIO_MAX_DECLS {
                return Err(ReadError::SizeTooBig(
                    sz as usize,
                    DELEGATION_RATIO_MAX_DECLS,
                ));
            }
            let mut pools = Vec::with_capacity(sz as usize);
            for _ in 0..sz {
                let pool_parts = codec.get_u8()?;
                let pool_id = <[u8; 32]>::deserialize(codec)?.into();
                pools.push((pool_id, pool_parts))
            }
            match DelegationRatio::new(parts, pools) {
                None => Err(ReadError::StructureInvalid(
                    "invalid delegation ratio".to_string(),
                )),
                Some(dr) => Ok(DelegationType::Ratio(dr)),
            }
        }
    }
}