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
use crate::transaction::{
    field::{Outputs, TxPointer as TxPointerField},
    validity::FormatValidityChecks,
};
use crate::{CheckError, ConsensusParameters, Output, TxPointer};
use derivative::Derivative;
use fuel_types::bytes::{SizedBytes, WORD_SIZE};
use fuel_types::{Bytes32, Word};

#[cfg(feature = "std")]
use std::io;

#[cfg(feature = "alloc")]
use alloc::vec::Vec;

#[cfg(feature = "std")]
use fuel_types::bytes::{self, Deserializable, SerializableVec};

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub(crate) struct MintMetadata {
    pub id: Bytes32,
    pub outputs_offset: usize,
    pub outputs_offset_at: Vec<usize>,
}

#[cfg(feature = "std")]
impl MintMetadata {
    fn compute<Tx>(tx: &Tx) -> Self
    where
        Tx: crate::UniqueIdentifier,
        Tx: Outputs,
        Tx: SizedBytes,
    {
        use itertools::Itertools;

        let id = tx.id();

        let mut offset = tx.outputs_offset();

        let outputs_offset = offset;
        #[cfg(feature = "internals")]
        assert_eq!(outputs_offset, tx.outputs_offset());

        let outputs_offset_at = tx
            .outputs()
            .iter()
            .map(|output| {
                let i = offset;
                offset += output.serialized_size();
                i
            })
            .collect_vec();

        Self {
            id,
            outputs_offset,
            outputs_offset_at,
        }
    }
}

/// The definition of the `Mint` transaction from the specification:
/// https://github.com/FuelLabs/fuel-specs/blob/master/src/protocol/tx_format/transaction.md#transactionmint
///
/// This transaction can be created by the block producer and included in the block only by it.
#[derive(Default, Debug, Clone, Derivative)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derivative(Eq, PartialEq, Hash)]
pub struct Mint {
    /// The location of the transaction in the block.
    pub(crate) tx_pointer: TxPointer,
    /// The list of `Output::Coin` generated by the block producer.
    pub(crate) outputs: Vec<Output>,
    #[cfg_attr(feature = "serde", serde(skip))]
    #[derivative(PartialEq = "ignore", Hash = "ignore")]
    pub(crate) metadata: Option<MintMetadata>,
}

#[cfg(feature = "std")]
impl crate::UniqueIdentifier for Mint {
    fn id(&self) -> Bytes32 {
        if let Some(MintMetadata { id, .. }) = self.metadata {
            return id;
        }

        let mut clone = self.clone();
        fuel_crypto::Hasher::hash(clone.to_bytes().as_slice())
    }
}

impl FormatValidityChecks for Mint {
    #[cfg(feature = "std")]
    fn check_signatures(&self) -> Result<(), CheckError> {
        Ok(())
    }

    fn check_without_signatures(&self, block_height: Word, parameters: &ConsensusParameters) -> Result<(), CheckError> {
        if self.outputs().len() > parameters.max_outputs as usize {
            return Err(CheckError::TransactionOutputsMax);
        }
        if self.tx_pointer().block_height() as u64 != block_height {
            return Err(CheckError::TransactionMintIncorrectBlockHeight);
        }

        let mut assets = Vec::new();
        for output in self.outputs() {
            if let Output::Coin { asset_id, .. } = output {
                if assets.contains(asset_id) {
                    return Err(CheckError::TransactionOutputCoinAssetIdDuplicated(*asset_id));
                } else {
                    assets.push(*asset_id);
                }
            } else {
                return Err(CheckError::TransactionMintOutputIsNotCoin);
            }
        }

        Ok(())
    }
}

#[cfg(feature = "std")]
impl crate::Cacheable for Mint {
    fn is_computed(&self) -> bool {
        self.metadata.is_some()
    }

    fn precompute(&mut self) {
        self.metadata = None;
        self.metadata = Some(MintMetadata::compute(self));
    }
}

impl SizedBytes for Mint {
    fn serialized_size(&self) -> usize {
        self.outputs_offset() + self.outputs().iter().map(|w| w.serialized_size()).sum::<usize>()
    }
}

mod field {
    use super::*;

    impl TxPointerField for Mint {
        #[inline(always)]
        fn tx_pointer(&self) -> &TxPointer {
            &self.tx_pointer
        }

        #[inline(always)]
        fn tx_pointer_mut(&mut self) -> &mut TxPointer {
            &mut self.tx_pointer
        }

        #[inline(always)]
        fn tx_pointer_static() -> usize {
            WORD_SIZE /* `Transaction` enum discriminant */
        }
    }

    impl Outputs for Mint {
        #[inline(always)]
        fn outputs(&self) -> &Vec<Output> {
            &self.outputs
        }

        #[inline(always)]
        fn outputs_mut(&mut self) -> &mut Vec<Output> {
            &mut self.outputs
        }

        #[inline(always)]
        fn outputs_offset(&self) -> usize {
            if let Some(MintMetadata { outputs_offset, .. }) = &self.metadata {
                return *outputs_offset;
            }

            self.tx_pointer_offset() + TxPointer::LEN + WORD_SIZE /* Outputs size */
        }

        #[inline(always)]
        fn outputs_offset_at(&self, idx: usize) -> Option<usize> {
            if let Some(MintMetadata { outputs_offset_at, .. }) = &self.metadata {
                return outputs_offset_at.get(idx).cloned();
            }

            if idx < self.outputs.len() {
                Some(
                    self.outputs_offset()
                        + self
                            .outputs()
                            .iter()
                            .take(idx)
                            .map(|i| i.serialized_size())
                            .sum::<usize>(),
                )
            } else {
                None
            }
        }
    }
}

#[cfg(feature = "std")]
impl io::Read for Mint {
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        let n = self.serialized_size();
        if buf.len() < n {
            return Err(bytes::eof());
        }

        let buf = bytes::store_number_unchecked(buf, crate::TransactionRepr::Mint as Word);
        let Mint {
            tx_pointer, outputs, ..
        } = self;

        let skip = tx_pointer.read(buf)?;
        let buf = &mut buf[skip..];
        let mut buf = bytes::store_number_unchecked(buf, outputs.len() as Word);

        for output in outputs {
            let output_len = output.read(buf)?;
            buf = &mut buf[output_len..];
        }

        Ok(n)
    }
}

#[cfg(feature = "std")]
impl io::Write for Mint {
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        let mut n = crate::consts::TRANSACTION_MINT_FIXED_SIZE;
        if buf.len() < n {
            return Err(bytes::eof());
        }

        let (identifier, buf): (Word, _) = unsafe { bytes::restore_number_unchecked(buf) };
        let identifier = crate::TransactionRepr::try_from(identifier)?;
        if identifier != crate::TransactionRepr::Mint {
            return Err(io::Error::new(
                io::ErrorKind::InvalidData,
                "The provided identifier to the `Script` is invalid!",
            ));
        }

        // Safety: buffer size is checked
        let tx_pointer = TxPointer::from_bytes(buf)?;
        let buf = &buf[tx_pointer.serialized_size()..];
        let (outputs_len, mut buf) = unsafe { bytes::restore_usize_unchecked(buf) };

        let mut outputs = vec![Output::default(); outputs_len];
        for output in outputs.iter_mut() {
            let output_len = output.write(buf)?;
            buf = &buf[output_len..];
            n += output_len;
        }

        *self = Mint {
            tx_pointer,
            outputs,
            metadata: None,
        };

        Ok(n)
    }

    fn flush(&mut self) -> io::Result<()> {
        self.outputs.iter_mut().try_for_each(|output| output.flush())?;

        Ok(())
    }
}