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
use super::{Input, Output, Transaction, Witness};
use core::hash::Hash;

use fuel_types::{AssetId, Word};

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

#[cfg(feature = "std")]
use fuel_crypto::{Message, Signature};
use itertools::Itertools;

mod error;

use crate::transaction::consensus_parameters::ConsensusParameters;
use crate::transaction::{field, Executable};
pub use error::CheckError;

impl Input {
    #[cfg(feature = "std")]
    pub fn check(
        &self,
        index: usize,
        txhash: &Bytes32,
        outputs: &[Output],
        witnesses: &[Witness],
        parameters: &ConsensusParameters,
    ) -> Result<(), CheckError> {
        self.check_without_signature(index, outputs, witnesses, parameters)?;
        self.check_signature(index, txhash, witnesses)?;

        Ok(())
    }

    #[cfg(feature = "std")]
    pub fn check_signature(
        &self,
        index: usize,
        txhash: &Bytes32,
        witnesses: &[Witness],
    ) -> Result<(), CheckError> {
        match self {
            Self::CoinSigned {
                witness_index,
                owner,
                ..
            }
            | Self::MessageSigned {
                witness_index,
                recipient: owner,
                ..
            } => {
                let witness = witnesses
                    .get(*witness_index as usize)
                    .ok_or(CheckError::InputWitnessIndexBounds { index })?
                    .as_ref();

                if witness.len() != Signature::LEN {
                    return Err(CheckError::InputInvalidSignature { index });
                }

                // Safety: checked length
                let signature = unsafe { Signature::as_ref_unchecked(witness) };

                // Safety: checked length
                let message = unsafe { Message::as_ref_unchecked(txhash.as_ref()) };

                let pk = signature
                    .recover(message)
                    .map_err(|_| CheckError::InputInvalidSignature { index })
                    .map(|pk| Input::owner(&pk))?;

                if owner != &pk {
                    return Err(CheckError::InputInvalidSignature { index });
                }

                Ok(())
            }

            Self::CoinPredicate {
                owner, predicate, ..
            }
            | Self::MessagePredicate {
                recipient: owner,
                predicate,
                ..
            } if !Input::is_predicate_owner_valid(owner, predicate) => {
                Err(CheckError::InputPredicateOwner { index })
            }

            _ => Ok(()),
        }
    }

    pub fn check_without_signature(
        &self,
        index: usize,
        outputs: &[Output],
        witnesses: &[Witness],
        parameters: &ConsensusParameters,
    ) -> Result<(), CheckError> {
        match self {
            Self::CoinPredicate { predicate, .. } | Self::MessagePredicate { predicate, .. }
                if predicate.is_empty() =>
            {
                Err(CheckError::InputPredicateEmpty { index })
            }

            Self::CoinPredicate { predicate, .. } | Self::MessagePredicate { predicate, .. }
                if predicate.len() > parameters.max_predicate_length as usize =>
            {
                Err(CheckError::InputPredicateLength { index })
            }

            Self::CoinPredicate { predicate_data, .. }
            | Self::MessagePredicate { predicate_data, .. }
                if predicate_data.len() > parameters.max_predicate_data_length as usize =>
            {
                Err(CheckError::InputPredicateDataLength { index })
            }

            Self::CoinSigned { witness_index, .. } | Self::MessageSigned { witness_index, .. }
                if *witness_index as usize >= witnesses.len() =>
            {
                Err(CheckError::InputWitnessIndexBounds { index })
            }

            // ∀ inputContract ∃! outputContract : outputContract.inputIndex = inputContract.index
            Self::Contract { .. }
                if 1 != outputs
                    .iter()
                    .filter_map(|output| match output {
                        Output::Contract { input_index, .. } if *input_index as usize == index => {
                            Some(())
                        }
                        _ => None,
                    })
                    .count() =>
            {
                Err(CheckError::InputContractAssociatedOutputContract { index })
            }

            Self::MessageSigned { data, .. } | Self::MessagePredicate { data, .. }
                if data.len() > parameters.max_message_data_length as usize =>
            {
                Err(CheckError::InputMessageDataLength { index })
            }

            // TODO If h is the block height the UTXO being spent was created, transaction is
            // invalid if `blockheight() < h + maturity`.
            _ => Ok(()),
        }
    }
}

impl Output {
    /// Validate the output of the transaction.
    ///
    /// This function is stateful - meaning it might validate a transaction during VM
    /// initialization, but this transaction will no longer be valid in post-execution because the
    /// VM might mutate the message outputs, producing invalid transactions.
    pub fn check(&self, index: usize, inputs: &[Input]) -> Result<(), CheckError> {
        match self {
            Self::Contract { input_index, .. } => match inputs.get(*input_index as usize) {
                Some(Input::Contract { .. }) => Ok(()),
                _ => Err(CheckError::OutputContractInputIndex { index }),
            },

            _ => Ok(()),
        }
    }
}

/// Means that the transaction can be validated.
pub trait Checkable {
    #[cfg(feature = "std")]
    /// Fully validates the transaction. It checks the validity of fields according to rules in
    /// the specification and validity of signatures.
    fn check(
        &self,
        block_height: Word,
        parameters: &ConsensusParameters,
    ) -> Result<(), CheckError> {
        self.check_without_signatures(block_height, parameters)?;
        self.check_signatures()?;

        Ok(())
    }

    #[cfg(feature = "std")]
    /// Validates that all required signatures are set in the transaction and that they are valid.
    fn check_signatures(&self) -> Result<(), CheckError>;

    /// Validates the transactions according to rules from the specification:
    /// https://github.com/FuelLabs/fuel-specs/blob/master/specs/protocol/tx_format.md#transaction
    fn check_without_signatures(
        &self,
        block_height: Word,
        parameters: &ConsensusParameters,
    ) -> Result<(), CheckError>;
}

impl Checkable for Transaction {
    #[cfg(feature = "std")]
    fn check_signatures(&self) -> Result<(), CheckError> {
        match self {
            Transaction::Script(script) => script.check_signatures(),
            Transaction::Create(create) => create.check_signatures(),
            Transaction::Mint(mint) => mint.check_signatures(),
        }
    }

    fn check_without_signatures(
        &self,
        block_height: Word,
        parameters: &ConsensusParameters,
    ) -> Result<(), CheckError> {
        match self {
            Transaction::Script(script) => {
                script.check_without_signatures(block_height, parameters)
            }
            Transaction::Create(create) => {
                create.check_without_signatures(block_height, parameters)
            }
            Transaction::Mint(mint) => mint.check_without_signatures(block_height, parameters),
        }
    }
}

pub(crate) fn check_common_part<T>(
    tx: &T,
    block_height: Word,
    parameters: &ConsensusParameters,
) -> Result<(), CheckError>
where
    T: field::GasPrice
        + field::GasLimit
        + field::Maturity
        + field::Inputs
        + field::Outputs
        + field::Witnesses,
{
    if tx.gas_limit() > &parameters.max_gas_per_tx {
        Err(CheckError::TransactionGasLimit)?
    }

    if tx.maturity() > &block_height {
        Err(CheckError::TransactionMaturity)?;
    }

    if tx.inputs().len() > parameters.max_inputs as usize {
        Err(CheckError::TransactionInputsMax)?
    }

    if tx.outputs().len() > parameters.max_outputs as usize {
        Err(CheckError::TransactionOutputsMax)?
    }

    if tx.witnesses().len() > parameters.max_witnesses as usize {
        Err(CheckError::TransactionWitnessesMax)?
    }

    tx.input_asset_ids_unique().try_for_each(|input_asset_id| {
        // check for duplicate change outputs
        if tx
            .outputs()
            .iter()
            .filter_map(|output| match output {
                Output::Change { asset_id, .. } if input_asset_id == asset_id => Some(()),
                Output::Change { asset_id, .. }
                    if asset_id != &AssetId::default() && input_asset_id == asset_id =>
                {
                    Some(())
                }
                _ => None,
            })
            .count()
            > 1
        {
            return Err(CheckError::TransactionOutputChangeAssetIdDuplicated(
                *input_asset_id,
            ));
        }

        Ok(())
    })?;

    // Check for duplicated input utxo id
    let duplicated_utxo_id = tx
        .inputs()
        .iter()
        .filter_map(|i| i.is_coin().then(|| i.utxo_id()).flatten());

    if let Some(utxo_id) = next_duplicate(duplicated_utxo_id).copied() {
        return Err(CheckError::DuplicateInputUtxoId { utxo_id });
    }

    // Check for duplicated input contract id
    let duplicated_contract_id = tx.inputs().iter().filter_map(Input::contract_id);

    if let Some(contract_id) = next_duplicate(duplicated_contract_id).copied() {
        return Err(CheckError::DuplicateInputContractId { contract_id });
    }

    // Check for duplicated input message id
    let duplicated_message_id = tx.inputs().iter().filter_map(Input::message_id);
    if let Some(message_id) = next_duplicate(duplicated_message_id).copied() {
        return Err(CheckError::DuplicateMessageInputId { message_id });
    }

    // Validate the inputs without checking signature
    tx.inputs()
        .iter()
        .enumerate()
        .try_for_each(|(index, input)| {
            input.check_without_signature(index, tx.outputs(), tx.witnesses(), parameters)
        })?;

    tx.outputs()
        .iter()
        .enumerate()
        .try_for_each(|(index, output)| {
            output.check(index, tx.inputs())?;

            if let Output::Change { asset_id, .. } = output {
                if !tx
                    .input_asset_ids()
                    .any(|input_asset_id| input_asset_id == asset_id)
                {
                    return Err(CheckError::TransactionOutputChangeAssetIdNotFound(
                        *asset_id,
                    ));
                }
            }

            if let Output::Coin { asset_id, .. } = output {
                if !tx
                    .input_asset_ids()
                    .any(|input_asset_id| input_asset_id == asset_id)
                {
                    return Err(CheckError::TransactionOutputCoinAssetIdNotFound(*asset_id));
                }
            }

            Ok(())
        })?;

    Ok(())
}

// TODO https://github.com/FuelLabs/fuel-tx/issues/148
pub(crate) fn next_duplicate<U>(iter: impl Iterator<Item = U>) -> Option<U>
where
    U: PartialEq + Ord + Copy + Hash,
{
    #[cfg(not(feature = "std"))]
    return iter
        .sorted()
        .as_slice()
        .windows(2)
        .filter_map(|u| (u[0] == u[1]).then(|| u[0]))
        .next();

    #[cfg(feature = "std")]
    return iter.duplicates().next();
}