fuel_tx/transaction/types/
upgrade.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
use crate::{
    transaction::{
        id::PrepareSign,
        metadata::CommonMetadata,
        types::chargeable_transaction::{
            ChargeableMetadata,
            ChargeableTransaction,
            UniqueFormatValidityChecks,
        },
        Chargeable,
    },
    ConsensusParameters,
    GasCosts,
    Input,
    Output,
    TransactionRepr,
    ValidityError,
};
use derivative::Derivative;
use fuel_types::{
    bytes::WORD_SIZE,
    canonical::Serialize,
    Bytes32,
    ChainId,
    Word,
};

use fuel_crypto::Hasher;

#[cfg(feature = "alloc")]
use alloc::boxed::Box;

pub type Upgrade = ChargeableTransaction<UpgradeBody, UpgradeMetadata>;

#[derive(Default, Debug, Clone, PartialEq, Eq, Hash)]
pub enum UpgradeMetadata {
    /// The metadata for the upgrade transaction that changes the consensus parameters.
    ConsensusParameters {
        /// Deserialized consensus parameters from the witness.
        consensus_parameters: Box<ConsensusParameters>,
        /// The actual checksum of the serialized consensus parameters.
        calculated_checksum: Bytes32,
    },
    /// Currently there is no metadata for state transition upgrades, so leave it empty.
    #[default]
    StateTransition,
}

impl UpgradeMetadata {
    pub fn compute(tx: &Upgrade) -> Result<Self, ValidityError> {
        match &tx.body.purpose {
            UpgradePurpose::ConsensusParameters {
                witness_index,
                checksum,
            } => {
                let index = *witness_index as usize;
                let witness = tx
                    .witnesses
                    .get(index)
                    .ok_or(ValidityError::InputWitnessIndexBounds { index })?;

                let serialized_consensus_parameters = witness.as_vec();
                let actual_checksum = Hasher::hash(serialized_consensus_parameters);

                if &actual_checksum != checksum {
                    Err(ValidityError::TransactionUpgradeConsensusParametersChecksumMismatch)?;
                }

                // The code that creates/verifies the `Upgrade` transaction should always
                // be able to decode the current consensus parameters
                // type. The state transition function should always know
                // how to decode consensus parameters. Otherwise, the next
                // block will be impossible to produce. If deserialization fails, it is a
                // sign that the code/state transition function should be updated.
                let consensus_parameters = postcard::from_bytes::<ConsensusParameters>(
                    serialized_consensus_parameters,
                )
                .map_err(|_| {
                    ValidityError::TransactionUpgradeConsensusParametersDeserialization
                })?;

                Ok(Self::ConsensusParameters {
                    consensus_parameters: Box::new(consensus_parameters),
                    calculated_checksum: actual_checksum,
                })
            }
            UpgradePurpose::StateTransition { .. } => {
                // Nothing metadata for state transition upgrades.
                Ok(Self::StateTransition)
            }
        }
    }
}

/// The types describe the purpose of the upgrade performed by the [`Upgrade`]
/// transaction.
#[derive(
    Copy, Clone, Derivative, strum_macros::EnumCount, serde::Serialize, serde::Deserialize,
)]
#[cfg_attr(
    feature = "da-compression",
    derive(fuel_compression::Compress, fuel_compression::Decompress)
)]
#[derive(fuel_types::canonical::Deserialize, fuel_types::canonical::Serialize)]
#[derivative(Eq, PartialEq, Hash, Debug)]
pub enum UpgradePurpose {
    /// The upgrade is performed to change the consensus parameters.
    ConsensusParameters {
        /// The index of the witness in the [`Witnesses`] field that contains
        /// the serialized consensus parameters.
        witness_index: u16,
        /// The hash of the serialized consensus parameters.
        /// Since the serialized consensus parameters live inside witnesses(malleable
        /// data), any party can override them. The `checksum` is used to verify that the
        /// data was not modified.
        checksum: Bytes32,
    },
    /// The upgrade is performed to change the state transition function.
    StateTransition {
        /// The Merkle root of the new bytecode of the state transition function.
        /// The bytecode must be present on the blockchain(should be known by the
        /// network) at the moment of inclusion of this transaction.
        root: Bytes32,
    },
}

/// The body of the [`Upgrade`] transaction.
#[derive(Clone, Derivative, serde::Serialize, serde::Deserialize)]
#[cfg_attr(
    feature = "da-compression",
    derive(fuel_compression::Compress, fuel_compression::Decompress)
)]
#[derive(fuel_types::canonical::Deserialize, fuel_types::canonical::Serialize)]
#[canonical(prefix = TransactionRepr::Upgrade)]
#[derivative(Eq, PartialEq, Hash, Debug)]
pub struct UpgradeBody {
    /// The purpose of the upgrade.
    pub(crate) purpose: UpgradePurpose,
}

impl Default for UpgradeBody {
    fn default() -> Self {
        Self {
            purpose: UpgradePurpose::StateTransition {
                root: Default::default(),
            },
        }
    }
}

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

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

    #[inline(always)]
    fn gas_used_by_metadata(&self, gas_cost: &GasCosts) -> Word {
        let bytes = Serialize::size(self);
        // Gas required to calculate the `tx_id`.
        let tx_id_gas = gas_cost.s256().resolve(bytes as u64);

        let purpose_gas = match &self.body.purpose {
            UpgradePurpose::ConsensusParameters { witness_index, .. } => {
                let len = self
                    .witnesses
                    .get(*witness_index as usize)
                    .map_or(0, |w| w.as_vec().len());
                gas_cost.s256().resolve(len as u64)
            }
            UpgradePurpose::StateTransition { .. } => {
                // In the case of the state transition upgrade, we only require the
                // existence of the bytecode on the blockchain. So we
                // verify nothing and charge nothing.
                0
            }
        };

        tx_id_gas.saturating_add(purpose_gas)
    }
}

impl UniqueFormatValidityChecks for Upgrade {
    fn check_unique_rules(
        &self,
        consensus_params: &ConsensusParameters,
    ) -> Result<(), ValidityError> {
        // At least one of inputs must be owned by the privileged address.
        self.inputs
            .iter()
            .find(|input| {
                if let Some(owner) = input.input_owner() {
                    owner == consensus_params.privileged_address()
                } else {
                    false
                }
            })
            .ok_or(ValidityError::TransactionUpgradeNoPrivilegedAddress)?;

        // We verify validity of the `UpgradePurpose` in the
        // `UpgradeMetadata::compute`.
        let calculated_metadata = UpgradeMetadata::compute(self)?;

        if let Some(metadata) = self.metadata.as_ref() {
            if metadata.body != calculated_metadata {
                return Err(ValidityError::TransactionMetadataMismatch);
            }
        }

        // The upgrade transaction cant touch the contract.
        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(()),
                }
            })?;

        // The upgrade transaction can't create a contract.
        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 != consensus_params.base_asset_id() =>
                {
                    Err(ValidityError::TransactionChangeChangeUsesNotBaseAsset { index })
                }

                Output::ContractCreated { .. } => {
                    Err(ValidityError::TransactionOutputContainsContractCreated { index })
                }
                _ => Ok(()),
            })?;

        Ok(())
    }
}

impl crate::Cacheable for Upgrade {
    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: UpgradeMetadata::compute(self)?,
        });
        Ok(())
    }
}

mod field {
    use super::*;
    use crate::field::{
        ChargeableBody,
        UpgradePurpose as UpgradePurposeTrait,
    };

    impl UpgradePurposeTrait for Upgrade {
        #[inline(always)]
        fn upgrade_purpose(&self) -> &UpgradePurpose {
            &self.body.purpose
        }

        #[inline(always)]
        fn upgrade_purpose_mut(&mut self) -> &mut UpgradePurpose {
            &mut self.body.purpose
        }

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

    impl ChargeableBody<UpgradeBody> for Upgrade {
        fn body(&self) -> &UpgradeBody {
            &self.body
        }

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

        fn body_offset_end(&self) -> usize {
            Self::upgrade_purpose_offset_static()
                .saturating_add(self.body.purpose.size())
                .saturating_add(
                    WORD_SIZE  // Policies size
                    + WORD_SIZE // Inputs size
                    + WORD_SIZE // Outputs size
                    + WORD_SIZE, // Witnesses size
                )
        }
    }
}