fuel_tx/transaction/
validity.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
use crate::{
    field::Maturity,
    input::{
        coin::{
            CoinPredicate,
            CoinSigned,
        },
        message::{
            MessageCoinPredicate,
            MessageCoinSigned,
            MessageDataPredicate,
            MessageDataSigned,
        },
    },
    output,
    policies::PolicyType,
    transaction::{
        consensus_parameters::{
            PredicateParameters,
            TxParameters,
        },
        field,
        Executable,
    },
    Chargeable,
    ConsensusParameters,
    Input,
    Output,
    Transaction,
    Witness,
};
use core::hash::Hash;
use fuel_types::{
    canonical,
    canonical::Serialize,
    Address,
    BlockHeight,
    Bytes32,
    ChainId,
};
use hashbrown::HashMap;
use itertools::Itertools;

mod error;

#[cfg(test)]
mod tests;

pub use error::ValidityError;

impl Input {
    #[cfg(any(feature = "typescript", test))]
    pub fn check(
        &self,
        index: usize,
        txhash: &Bytes32,
        outputs: &[Output],
        witnesses: &[Witness],
        predicate_params: &PredicateParameters,
        recovery_cache: &mut Option<HashMap<u16, Address>>,
    ) -> Result<(), ValidityError> {
        self.check_without_signature(index, outputs, witnesses, predicate_params)?;
        self.check_signature(index, txhash, witnesses, recovery_cache)?;

        Ok(())
    }

    pub fn check_signature(
        &self,
        index: usize,
        txhash: &Bytes32,
        witnesses: &[Witness],
        recovery_cache: &mut Option<HashMap<u16, Address>>,
    ) -> Result<(), ValidityError> {
        match self {
            Self::CoinSigned(CoinSigned {
                witness_index,
                owner,
                ..
            })
            | Self::MessageCoinSigned(MessageCoinSigned {
                witness_index,
                recipient: owner,
                ..
            })
            | Self::MessageDataSigned(MessageDataSigned {
                witness_index,
                recipient: owner,
                ..
            }) => {
                // Helper function for recovering the address from a witness
                let recover_address = || -> Result<Address, ValidityError> {
                    let witness = witnesses
                        .get(*witness_index as usize)
                        .ok_or(ValidityError::InputWitnessIndexBounds { index })?;

                    witness.recover_witness(txhash, index)
                };

                // recover the address associated with a witness, using the cache if
                // available
                let recovered_address = if let Some(cache) = recovery_cache {
                    if let Some(recovered_address) = cache.get(witness_index) {
                        *recovered_address
                    } else {
                        // if this witness hasn't been recovered before,
                        // cache ecrecover by witness index
                        let recovered_address = recover_address()?;
                        cache.insert(*witness_index, recovered_address);
                        recovered_address
                    }
                } else {
                    recover_address()?
                };

                if owner != &recovered_address {
                    return Err(ValidityError::InputInvalidSignature { index });
                }

                Ok(())
            }

            Self::CoinPredicate(CoinPredicate {
                owner, predicate, ..
            })
            | Self::MessageCoinPredicate(MessageCoinPredicate {
                recipient: owner,
                predicate,
                ..
            })
            | Self::MessageDataPredicate(MessageDataPredicate {
                recipient: owner,
                predicate,
                ..
            }) if !Input::is_predicate_owner_valid(owner, &**predicate) => {
                Err(ValidityError::InputPredicateOwner { index })
            }

            _ => Ok(()),
        }
    }

    pub fn check_without_signature(
        &self,
        index: usize,
        outputs: &[Output],
        witnesses: &[Witness],
        predicate_params: &PredicateParameters,
    ) -> Result<(), ValidityError> {
        match self {
            Self::CoinPredicate(CoinPredicate { predicate, .. })
            | Self::MessageCoinPredicate(MessageCoinPredicate { predicate, .. })
            | Self::MessageDataPredicate(MessageDataPredicate { predicate, .. })
                if predicate.is_empty() =>
            {
                Err(ValidityError::InputPredicateEmpty { index })
            }

            Self::CoinPredicate(CoinPredicate { predicate, .. })
            | Self::MessageCoinPredicate(MessageCoinPredicate { predicate, .. })
            | Self::MessageDataPredicate(MessageDataPredicate { predicate, .. })
                if predicate.len() as u64 > predicate_params.max_predicate_length() =>
            {
                Err(ValidityError::InputPredicateLength { index })
            }

            Self::CoinPredicate(CoinPredicate { predicate_data, .. })
            | Self::MessageCoinPredicate(MessageCoinPredicate {
                predicate_data, ..
            })
            | Self::MessageDataPredicate(MessageDataPredicate {
                predicate_data, ..
            }) if predicate_data.len() as u64
                > predicate_params.max_predicate_data_length() =>
            {
                Err(ValidityError::InputPredicateDataLength { index })
            }

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

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

            Self::MessageDataSigned(MessageDataSigned { data, .. })
            | Self::MessageDataPredicate(MessageDataPredicate { data, .. })
                if data.is_empty()
                    || data.len() as u64 > predicate_params.max_message_data_length() =>
            {
                Err(ValidityError::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<(), ValidityError> {
        match self {
            Self::Contract(output::contract::Contract { input_index, .. }) => {
                match inputs.get(*input_index as usize) {
                    Some(Input::Contract { .. }) => Ok(()),
                    _ => Err(ValidityError::OutputContractInputIndex { index }),
                }
            }

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

/// Contains logic for stateless validations that don't result in any reusable metadata
/// such as spendable input balances or remaining gas. Primarily involves validating that
/// transaction fields are correctly formatted and signed.
pub trait FormatValidityChecks {
    /// Performs all stateless transaction validity checks. This includes the validity
    /// of fields according to rules in the specification and validity of signatures.
    fn check(
        &self,
        block_height: BlockHeight,
        consensus_params: &ConsensusParameters,
    ) -> Result<(), ValidityError> {
        self.check_without_signatures(block_height, consensus_params)?;
        self.check_signatures(&consensus_params.chain_id())?;

        Ok(())
    }

    /// Validates that all required signatures are set in the transaction and that they
    /// are valid.
    fn check_signatures(&self, chain_id: &ChainId) -> Result<(), ValidityError>;

    /// Validates the transactions according to rules from the specification:
    /// <https://github.com/FuelLabs/fuel-specs/blob/master/src/tx-format/transaction.md>
    fn check_without_signatures(
        &self,
        block_height: BlockHeight,
        consensus_params: &ConsensusParameters,
    ) -> Result<(), ValidityError>;
}

impl FormatValidityChecks for Transaction {
    fn check_signatures(&self, chain_id: &ChainId) -> Result<(), ValidityError> {
        match self {
            Self::Script(tx) => tx.check_signatures(chain_id),
            Self::Create(tx) => tx.check_signatures(chain_id),
            Self::Mint(tx) => tx.check_signatures(chain_id),
            Self::Upgrade(tx) => tx.check_signatures(chain_id),
            Self::Upload(tx) => tx.check_signatures(chain_id),
            Self::Blob(tx) => tx.check_signatures(chain_id),
        }
    }

    fn check_without_signatures(
        &self,
        block_height: BlockHeight,
        consensus_params: &ConsensusParameters,
    ) -> Result<(), ValidityError> {
        match self {
            Self::Script(tx) => {
                tx.check_without_signatures(block_height, consensus_params)
            }
            Self::Create(tx) => {
                tx.check_without_signatures(block_height, consensus_params)
            }
            Self::Mint(tx) => tx.check_without_signatures(block_height, consensus_params),
            Self::Upgrade(tx) => {
                tx.check_without_signatures(block_height, consensus_params)
            }
            Self::Upload(tx) => {
                tx.check_without_signatures(block_height, consensus_params)
            }
            Self::Blob(tx) => tx.check_without_signatures(block_height, consensus_params),
        }
    }
}

/// Validates the size of the transaction in bytes. Transactions cannot exceed
/// the total size specified by the transaction parameters. The size of a
/// transaction is calculated as the sum of the sizes of its static and dynamic
/// parts.
pub(crate) fn check_size<T>(tx: &T, tx_params: &TxParameters) -> Result<(), ValidityError>
where
    T: canonical::Serialize,
{
    if tx.size() as u64 > tx_params.max_size() {
        Err(ValidityError::TransactionSizeLimitExceeded)?;
    }

    Ok(())
}

pub(crate) fn check_common_part<T>(
    tx: &T,
    block_height: BlockHeight,
    consensus_params: &ConsensusParameters,
) -> Result<(), ValidityError>
where
    T: canonical::Serialize + Chargeable + field::Outputs,
{
    let tx_params = consensus_params.tx_params();
    let predicate_params = consensus_params.predicate_params();
    let base_asset_id = consensus_params.base_asset_id();
    let gas_costs = consensus_params.gas_costs();
    let fee_params = consensus_params.fee_params();

    check_size(tx, tx_params)?;

    if !tx.policies().is_valid() {
        Err(ValidityError::TransactionPoliciesAreInvalid)?
    }

    if let Some(witness_limit) = tx.policies().get(PolicyType::WitnessLimit) {
        let witness_size = tx.witnesses().size_dynamic();
        if witness_size as u64 > witness_limit {
            Err(ValidityError::TransactionWitnessLimitExceeded)?
        }
    }

    let max_gas = tx.max_gas(gas_costs, fee_params);
    if max_gas > tx_params.max_gas_per_tx() {
        Err(ValidityError::TransactionMaxGasExceeded)?
    }

    if !tx.policies().is_set(PolicyType::MaxFee) {
        Err(ValidityError::TransactionMaxFeeNotSet)?
    };

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

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

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

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

    let any_spendable_input = tx.inputs().iter().find(|input| match input {
        Input::CoinSigned(_)
        | Input::CoinPredicate(_)
        | Input::MessageCoinSigned(_)
        | Input::MessageCoinPredicate(_) => true,
        Input::MessageDataSigned(_)
        | Input::MessageDataPredicate(_)
        | Input::Contract(_) => false,
    });

    if any_spendable_input.is_none() {
        Err(ValidityError::NoSpendableInput)?
    }

    tx.input_asset_ids_unique(base_asset_id)
        .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(())
                    }
                    _ => None,
                })
                .count()
                > 1
            {
                return Err(ValidityError::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(ValidityError::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(ValidityError::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) {
        return Err(ValidityError::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(),
                predicate_params,
            )
        })?;

    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(base_asset_id)
                    .any(|input_asset_id| input_asset_id == asset_id)
                {
                    return Err(ValidityError::TransactionOutputChangeAssetIdNotFound(
                        *asset_id,
                    ));
                }
            }

            if let Output::Coin { asset_id, .. } = output {
                if !tx
                    .input_asset_ids(base_asset_id)
                    .any(|input_asset_id| input_asset_id == asset_id)
                {
                    return Err(ValidityError::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"))]
    {
        iter.sorted()
            .as_slice()
            .windows(2)
            .filter_map(|u| (u[0] == u[1]).then(|| u[0]))
            .next()
    }

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

#[cfg(feature = "typescript")]
mod typescript {
    use crate::{
        transaction::consensus_parameters::typescript::PredicateParameters,
        Witness,
    };
    use fuel_types::Bytes32;
    use wasm_bindgen::JsValue;

    use alloc::{
        format,
        vec::Vec,
    };

    use crate::transaction::{
        input_ts::Input,
        output_ts::Output,
    };

    #[wasm_bindgen::prelude::wasm_bindgen]
    pub fn check_input(
        input: &Input,
        index: usize,
        txhash: &Bytes32,
        outputs: Vec<JsValue>,
        witnesses: Vec<JsValue>,
        predicate_params: &PredicateParameters,
    ) -> Result<(), js_sys::Error> {
        let outputs: Vec<crate::Output> = outputs
            .into_iter()
            .map(|v| serde_wasm_bindgen::from_value::<Output>(v).map(|v| *v.0))
            .collect::<Result<Vec<_>, _>>()
            .map_err(|e| js_sys::Error::new(&format!("{:?}", e)))?;

        let witnesses: Vec<Witness> = witnesses
            .into_iter()
            .map(serde_wasm_bindgen::from_value::<Witness>)
            .collect::<Result<Vec<_>, _>>()
            .map_err(|e| js_sys::Error::new(&format!("{:?}", e)))?;

        input
            .0
            .check(
                index,
                txhash,
                &outputs,
                &witnesses,
                predicate_params.as_ref(),
                &mut None,
            )
            .map_err(|e| js_sys::Error::new(&format!("{:?}", e)))
    }

    #[wasm_bindgen::prelude::wasm_bindgen]
    pub fn check_output(
        output: &Output,
        index: usize,
        inputs: Vec<JsValue>,
    ) -> Result<(), js_sys::Error> {
        let inputs: Vec<crate::Input> = inputs
            .into_iter()
            .map(|v| serde_wasm_bindgen::from_value::<Input>(v).map(|v| *v.0))
            .collect::<Result<Vec<_>, _>>()
            .map_err(|e| js_sys::Error::new(&format!("{:?}", e)))?;

        output
            .0
            .check(index, &inputs)
            .map_err(|e| js_sys::Error::new(&format!("{:?}", e)))
    }
}