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
use crate::{
    transaction::{
        field::{
            BytecodeWitnessIndex,
            Salt as SaltField,
            StorageSlots,
        },
        metadata::CommonMetadata,
        types::chargeable_transaction::{
            ChargeableMetadata,
            ChargeableTransaction,
            UniqueFormatValidityChecks,
        },
    },
    Chargeable,
    ConsensusParameters,
    Contract,
    GasCosts,
    Input,
    Output,
    PrepareSign,
    StorageSlot,
    TransactionRepr,
    ValidityError,
};
use derivative::Derivative;
use fuel_types::{
    bytes::WORD_SIZE,
    canonical,
    Bytes32,
    Bytes4,
    ChainId,
    ContractId,
    Salt,
    Word,
};

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

#[cfg(all(test, feature = "std"))]
mod ser_de_tests;

pub type Create = ChargeableTransaction<CreateBody, CreateMetadata>;

#[derive(Default, Debug, Clone, Derivative)]
#[derivative(Eq, PartialEq, Hash)]
pub struct CreateMetadata {
    pub contract_id: ContractId,
    pub contract_root: Bytes32,
    pub state_root: Bytes32,
}

impl CreateMetadata {
    /// Computes the `Metadata` for the `tx` transaction.
    pub fn compute(tx: &Create) -> Result<Self, ValidityError> {
        let salt = tx.salt();
        let storage_slots = tx.storage_slots();
        let contract = Contract::try_from(tx)?;
        let contract_root = contract.root();
        let state_root = Contract::initial_state_root(storage_slots.iter());
        let contract_id = contract.id(salt, &contract_root, &state_root);

        Ok(Self {
            contract_id,
            contract_root,
            state_root,
        })
    }
}

#[derive(Default, Debug, Clone, Derivative)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(fuel_types::canonical::Deserialize, fuel_types::canonical::Serialize)]
#[canonical(prefix = TransactionRepr::Create)]
#[derivative(Eq, PartialEq, Hash)]
pub struct CreateBody {
    pub(crate) bytecode_witness_index: u16,
    pub(crate) salt: Salt,
    pub(crate) storage_slots: Vec<StorageSlot>,
}

impl PrepareSign for CreateBody {
    fn prepare_sign(&mut self) {}
}

impl Chargeable for Create {
    #[inline(always)]
    fn metered_bytes_size(&self) -> usize {
        canonical::Serialize::size(self)
    }

    fn gas_used_by_metadata(&self, gas_costs: &GasCosts) -> Word {
        let Create {
            body:
                CreateBody {
                    bytecode_witness_index,
                    storage_slots,
                    ..
                },
            witnesses,
            ..
        } = self;

        let contract_len = witnesses
            .get(*bytecode_witness_index as usize)
            .map(|c| c.as_ref().len())
            .unwrap_or(0);

        let contract_root_gas = gas_costs.contract_root().resolve(contract_len as Word);
        let state_root_length = storage_slots.len() as Word;
        let state_root_gas = gas_costs.state_root().resolve(state_root_length);

        // See https://github.com/FuelLabs/fuel-specs/blob/master/src/identifiers/contract-id.md
        let contract_id_input_length =
            Bytes4::LEN + Salt::LEN + Bytes32::LEN + Bytes32::LEN;
        let contract_id_gas = gas_costs.s256().resolve(contract_id_input_length as Word);
        let bytes = canonical::Serialize::size(self);
        // Gas required to calculate the `tx_id`.
        let tx_id_gas = gas_costs.s256().resolve(bytes as u64);

        contract_root_gas
            .saturating_add(state_root_gas)
            .saturating_add(contract_id_gas)
            .saturating_add(tx_id_gas)
    }
}

impl UniqueFormatValidityChecks for Create {
    fn check_unique_rules(
        &self,
        consensus_params: &ConsensusParameters,
    ) -> Result<(), ValidityError> {
        let contract_params = consensus_params.contract_params();
        let base_asset_id = consensus_params.base_asset_id();

        let bytecode_witness_len = self
            .witnesses
            .get(self.body.bytecode_witness_index as usize)
            .map(|w| w.as_ref().len() as Word)
            .ok_or(ValidityError::TransactionCreateBytecodeWitnessIndex)?;

        if bytecode_witness_len > contract_params.contract_max_size() {
            return Err(ValidityError::TransactionCreateBytecodeLen);
        }

        // Restrict to subset of u16::MAX, allowing this to be increased in the future
        // in a non-breaking way.
        if self.body.storage_slots.len() as u64 > contract_params.max_storage_slots() {
            return Err(ValidityError::TransactionCreateStorageSlotMax);
        }

        // Verify storage slots are sorted
        if !self
            .body
            .storage_slots
            .as_slice()
            .windows(2)
            .all(|s| s[0] < s[1])
        {
            return Err(ValidityError::TransactionCreateStorageSlotOrder);
        }

        self.inputs
            .iter()
            .enumerate()
            .try_for_each(|(index, input)| {
                if let Some(asset_id) = input.asset_id(consensus_params.base_asset_id()) {
                    if asset_id != consensus_params.base_asset_id() {
                        return Err(
                            ValidityError::TransactionInputContainsNonBaseAssetId {
                                index,
                            },
                        );
                    }
                }

                match input {
                    Input::Contract(_) => {
                        Err(ValidityError::TransactionInputContainsContract { index })
                    }
                    Input::MessageDataSigned(_) | Input::MessageDataPredicate(_) => {
                        Err(ValidityError::TransactionInputContainsMessageData { index })
                    }
                    _ => Ok(()),
                }
            })?;

        debug_assert!(
            self.metadata.is_some(),
            "`check_without_signatures` is called without cached metadata"
        );
        let (state_root_calculated, contract_id_calculated) =
            if let Some(metadata) = &self.metadata {
                (metadata.body.state_root, metadata.body.contract_id)
            } else {
                let metadata = CreateMetadata::compute(self)?;
                (metadata.state_root, metadata.contract_id)
            };

        let mut contract_created = false;
        self.outputs
            .iter()
            .enumerate()
            .try_for_each(|(index, output)| match output {
                Output::Contract(_) => {
                    Err(ValidityError::TransactionOutputContainsContract { index })
                }

                Output::Variable { .. } => {
                    Err(ValidityError::TransactionOutputContainsVariable { index })
                }

                Output::Change { asset_id, .. } if asset_id != base_asset_id => {
                    Err(ValidityError::TransactionChangeChangeUsesNotBaseAsset { index })
                }

                Output::ContractCreated {
                    contract_id,
                    state_root,
                } if contract_id != &contract_id_calculated
                    || state_root != &state_root_calculated =>
                    {
                        Err(
                            ValidityError::TransactionCreateOutputContractCreatedDoesntMatch {
                                index,
                            },
                        )
                    }

                // TODO: Output::ContractCreated { contract_id, state_root } if
                // contract_id == &id && state_root == &storage_root
                //  maybe move from `fuel-vm` to here
                Output::ContractCreated { .. } if contract_created => {
                    Err(ValidityError::TransactionCreateOutputContractCreatedMultiple {
                        index,
                    })
                }

                Output::ContractCreated { .. } => {
                    contract_created = true;

                    Ok(())
                }

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

        Ok(())
    }
}

impl crate::Cacheable for Create {
    fn is_computed(&self) -> bool {
        self.metadata.is_some()
    }

    fn precompute(&mut self, chain_id: &ChainId) -> Result<(), ValidityError> {
        self.metadata = None;
        self.metadata = Some(ChargeableMetadata {
            common: CommonMetadata::compute(self, chain_id)?,
            body: CreateMetadata::compute(self)?,
        });
        Ok(())
    }
}

mod field {
    use super::*;
    use crate::field::{
        ChargeableBody,
        StorageSlotRef,
    };

    impl BytecodeWitnessIndex for Create {
        #[inline(always)]
        fn bytecode_witness_index(&self) -> &u16 {
            &self.body.bytecode_witness_index
        }

        #[inline(always)]
        fn bytecode_witness_index_mut(&mut self) -> &mut u16 {
            &mut self.body.bytecode_witness_index
        }

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

    impl SaltField for Create {
        #[inline(always)]
        fn salt(&self) -> &Salt {
            &self.body.salt
        }

        #[inline(always)]
        fn salt_mut(&mut self) -> &mut Salt {
            &mut self.body.salt
        }

        #[inline(always)]
        fn salt_offset_static() -> usize {
            Self::bytecode_witness_index_offset_static().saturating_add(WORD_SIZE)
        }
    }

    impl StorageSlots for Create {
        #[inline(always)]
        fn storage_slots(&self) -> &Vec<StorageSlot> {
            &self.body.storage_slots
        }

        #[inline(always)]
        fn storage_slots_mut(&mut self) -> StorageSlotRef {
            StorageSlotRef {
                storage_slots: &mut self.body.storage_slots,
            }
        }

        #[inline(always)]
        fn storage_slots_offset_static() -> usize {
            Self::salt_offset_static().saturating_add(
                Salt::LEN
                + WORD_SIZE // Storage slots size
                + WORD_SIZE // Policies size
                + WORD_SIZE // Inputs size
                + WORD_SIZE // Outputs size
                + WORD_SIZE, // Witnesses size
            )
        }

        fn storage_slots_offset_at(&self, idx: usize) -> Option<usize> {
            if idx < self.body.storage_slots.len() {
                Some(
                    Self::storage_slots_offset_static()
                        .checked_add(idx.checked_mul(StorageSlot::SLOT_SIZE)?)?,
                )
            } else {
                None
            }
        }
    }

    impl ChargeableBody<CreateBody> for Create {
        fn body(&self) -> &CreateBody {
            &self.body
        }

        fn body_mut(&mut self) -> &mut CreateBody {
            &mut self.body
        }

        fn body_offset_end(&self) -> usize {
            Self::storage_slots_offset_static().saturating_add(
                self.body
                    .storage_slots
                    .len()
                    .saturating_mul(StorageSlot::SLOT_SIZE),
            )
        }
    }
}

impl TryFrom<&Create> for Contract {
    type Error = ValidityError;

    fn try_from(tx: &Create) -> Result<Self, Self::Error> {
        let Create {
            body:
                CreateBody {
                    bytecode_witness_index,
                    ..
                },
            witnesses,
            ..
        } = tx;

        witnesses
            .get(*bytecode_witness_index as usize)
            .map(|c| c.as_ref().into())
            .ok_or(ValidityError::TransactionCreateBytecodeWitnessIndex)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        builder::Finalizable,
        transaction::validity::FormatValidityChecks,
    };
    use fuel_types::Bytes32;

    #[test]
    fn storage_slots_sorting() {
        // Test that storage slots must be sorted correctly
        let mut slot_data = [0u8; 64];

        let storage_slots = (0..10u64)
            .map(|i| {
                slot_data[..8].copy_from_slice(&i.to_be_bytes());
                StorageSlot::from(&slot_data.into())
            })
            .collect::<Vec<StorageSlot>>();

        let mut tx = crate::TransactionBuilder::create(
            vec![].into(),
            Salt::zeroed(),
            storage_slots,
        )
        .add_random_fee_input()
        .finalize();
        tx.body.storage_slots.reverse();

        let err = tx
            .check(0.into(), &ConsensusParameters::standard())
            .expect_err("Expected erroneous transaction");

        assert_eq!(ValidityError::TransactionCreateStorageSlotOrder, err);
    }

    #[test]
    fn storage_slots_no_duplicates() {
        let storage_slots = vec![
            StorageSlot::new(Bytes32::zeroed(), Bytes32::zeroed()),
            StorageSlot::new(Bytes32::zeroed(), Bytes32::zeroed()),
        ];

        let err = crate::TransactionBuilder::create(
            vec![].into(),
            Salt::zeroed(),
            storage_slots,
        )
        .add_random_fee_input()
        .finalize()
        .check(0.into(), &ConsensusParameters::standard())
        .expect_err("Expected erroneous transaction");

        assert_eq!(ValidityError::TransactionCreateStorageSlotOrder, err);
    }
}