fuel-tx 0.66.3

FuelVM transaction.
Documentation
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
use crate::{
    Blob,
    CompressedUtxoId,
    Create,
    Mint,
    PrepareSign,
    Script,
    ScriptCode,
    Transaction,
    TxPointer,
    Upgrade,
    Upload,
    UtxoId,
    field,
    input::{
        AsField,
        PredicateCode,
        coin::{
            Coin,
            CoinSpecification,
        },
        message::{
            Message,
            MessageSpecification,
        },
    },
    test_helper::TransactionFactory,
    transaction::field::Inputs,
};
use bimap::BiMap;
use fuel_compression::{
    Compress,
    Compressible,
    CompressibleBy,
    ContextError,
    Decompress,
    DecompressibleBy,
    RegistryKey,
};
use fuel_types::{
    Address,
    AssetId,
    ContractId,
    Nonce,
    Word,
    bytes::Bytes,
};
use rand::{
    Rng,
    SeedableRng,
    rngs::StdRng,
};
use std::{
    collections::HashMap,
    convert::Infallible,
};

type Keyspace = &'static str;

/// When a coin is created, this data is stored
#[derive(Debug, Default, Clone, PartialEq)]
struct CoinInfo {
    owner: Address,
    amount: u64,
    asset_id: AssetId,
}

/// When a message is created, this data is stored
#[derive(Debug, Default, Clone, PartialEq)]
struct MessageInfo {
    pub sender: Address,
    pub recipient: Address,
    pub amount: Word,
    pub data: Vec<u8>,
}

/// A simple and inefficient registry for testing purposes.
/// Also just stores the latest given transaction to just return it back.
#[derive(Debug, Default, Clone, PartialEq)]
struct TestCompressionCtx {
    next_key: u32,
    registry: HashMap<Keyspace, BiMap<RegistryKey, Vec<u8>>>,
    tx_blocks: BiMap<CompressedUtxoId, UtxoId>,
    latest_tx_coins: HashMap<UtxoId, CoinInfo>,
    latest_tx_pointer: Option<TxPointer>,
    latest_tx_messages: HashMap<Nonce, MessageInfo>,
}

impl TestCompressionCtx {
    fn store_data_for_mint(tx: &Mint) -> Self {
        Self {
            latest_tx_pointer: Some(tx.tx_pointer),
            ..Default::default()
        }
    }

    fn store_tx_info<Tx>(&mut self, tx: &Tx)
    where
        Tx: Inputs,
    {
        let latest_tx_coins =
            tx.inputs()
                .iter()
                .filter(|input| input.is_coin())
                .map(|input| {
                    (
                        *input.utxo_id().unwrap(),
                        CoinInfo {
                            owner: *input.input_owner().unwrap(),
                            amount: input.amount().unwrap(),
                            asset_id: *input.asset_id(&AssetId::default()).unwrap(),
                        },
                    )
                });
        let latest_tx_messages = tx
            .inputs()
            .iter()
            .filter(|input| input.is_message())
            .map(|input| {
                (
                    *input.nonce().unwrap(),
                    MessageInfo {
                        sender: *input.sender().unwrap(),
                        recipient: *input.recipient().unwrap(),
                        amount: input.amount().unwrap(),
                        data: input.input_data().unwrap_or_default().to_vec(),
                    },
                )
            });

        self.latest_tx_coins.extend(latest_tx_coins);
        self.latest_tx_messages.extend(latest_tx_messages);
    }
}

impl ContextError for TestCompressionCtx {
    type Error = Infallible;
}

macro_rules! impl_substitutable_key {
    ($t:ty) => {
        impl CompressibleBy<TestCompressionCtx> for $t {
            async fn compress_with(
                &self,
                ctx: &mut TestCompressionCtx,
            ) -> Result<RegistryKey, Infallible> {
                let keyspace = stringify!($t);
                let value = postcard::to_stdvec(self).expect("failed to serialize");

                let entry = ctx.registry.entry(keyspace).or_default();
                if let Some(key) = entry.get_by_right(&value) {
                    return Ok(*key);
                }

                let key =
                    RegistryKey::try_from(ctx.next_key as u32).expect("key too large");
                ctx.next_key += 1;
                entry
                    .insert_no_overwrite(key, value)
                    .expect("duplicate key");
                Ok(key)
            }
        }

        impl DecompressibleBy<TestCompressionCtx> for $t {
            async fn decompress_with(
                key: RegistryKey,
                ctx: &TestCompressionCtx,
            ) -> Result<$t, Infallible> {
                let keyspace = stringify!($t);
                let values = ctx.registry.get(&keyspace).expect("key not found");
                let value = values.get_by_left(&key).expect("key not found");
                Ok(postcard::from_bytes(value).expect("failed to deserialize"))
            }
        }
    };
}

impl_substitutable_key!(Address);
impl_substitutable_key!(AssetId);
impl_substitutable_key!(ContractId);
impl_substitutable_key!(ScriptCode);
impl_substitutable_key!(PredicateCode);

impl CompressibleBy<TestCompressionCtx> for UtxoId {
    async fn compress_with(
        &self,
        ctx: &mut TestCompressionCtx,
    ) -> Result<CompressedUtxoId, Infallible> {
        if let Some(key) = ctx.tx_blocks.get_by_right(self) {
            return Ok(*key);
        }

        let key_seed = ctx.tx_blocks.len(); // Just get an unique integer key
        let key = CompressedUtxoId {
            tx_pointer: TxPointer::new((key_seed as u32).into(), 0),
            output_index: 0,
        };
        ctx.tx_blocks.insert(key, *self);
        Ok(key)
    }
}

impl DecompressibleBy<TestCompressionCtx> for UtxoId {
    async fn decompress_with(
        key: CompressedUtxoId,
        ctx: &TestCompressionCtx,
    ) -> Result<UtxoId, Infallible> {
        Ok(*ctx.tx_blocks.get_by_left(&key).expect("key not found"))
    }
}

impl<Specification> DecompressibleBy<TestCompressionCtx> for Coin<Specification>
where
    Specification: CoinSpecification,
    Specification::Predicate: DecompressibleBy<TestCompressionCtx>,
    Specification::PredicateData: DecompressibleBy<TestCompressionCtx>,
    Specification::PredicateGasUsed: DecompressibleBy<TestCompressionCtx>,
    Specification::Witness: DecompressibleBy<TestCompressionCtx>,
{
    async fn decompress_with(
        c: <Coin<Specification> as Compressible>::Compressed,
        ctx: &TestCompressionCtx,
    ) -> Result<Coin<Specification>, Infallible> {
        let utxo_id = UtxoId::decompress_with(c.utxo_id, ctx).await?;
        let coin_info = ctx.latest_tx_coins.get(&utxo_id).expect("coin not found");
        let witness_index = c.witness_index.decompress(ctx).await?;
        let predicate_gas_used = c.predicate_gas_used.decompress(ctx).await?;
        let predicate = c.predicate.decompress(ctx).await?;
        let predicate_data = c.predicate_data.decompress(ctx).await?;

        Ok(Self {
            utxo_id,
            owner: coin_info.owner,
            amount: coin_info.amount,
            asset_id: coin_info.asset_id,
            tx_pointer: Default::default(),
            witness_index,
            predicate_gas_used,
            predicate,
            predicate_data,
        })
    }
}

impl<Specification> DecompressibleBy<TestCompressionCtx> for Message<Specification>
where
    Specification: MessageSpecification,
    Specification::Data: DecompressibleBy<TestCompressionCtx> + Default,
    Specification::Predicate: DecompressibleBy<TestCompressionCtx>,
    Specification::PredicateData: DecompressibleBy<TestCompressionCtx>,
    Specification::PredicateGasUsed: DecompressibleBy<TestCompressionCtx>,
    Specification::Witness: DecompressibleBy<TestCompressionCtx>,
{
    async fn decompress_with(
        c: <Message<Specification> as Compressible>::Compressed,
        ctx: &TestCompressionCtx,
    ) -> Result<Message<Specification>, Infallible> {
        let msg = ctx
            .latest_tx_messages
            .get(&c.nonce)
            .expect("message not found");
        let witness_index = c.witness_index.decompress(ctx).await?;
        let predicate_gas_used = c.predicate_gas_used.decompress(ctx).await?;
        let predicate = c.predicate.decompress(ctx).await?;
        let predicate_data = c.predicate_data.decompress(ctx).await?;
        let mut message: Message<Specification> = Message {
            sender: msg.sender,
            recipient: msg.recipient,
            amount: msg.amount,
            nonce: c.nonce,
            witness_index,
            predicate_gas_used,
            data: Default::default(),
            predicate,
            predicate_data,
        };

        if let Some(data) = message.data.as_mut_field() {
            *data = Bytes::new(msg.data.clone())
        }

        Ok(message)
    }
}

impl DecompressibleBy<TestCompressionCtx> for Mint {
    async fn decompress_with(
        c: Self::Compressed,
        ctx: &TestCompressionCtx,
    ) -> Result<Self, Infallible> {
        Ok(Transaction::mint(
            ctx.latest_tx_pointer.expect("no latest tx pointer"),
            c.input_contract.decompress(ctx).await?,
            c.output_contract.decompress(ctx).await?,
            c.mint_amount.decompress(ctx).await?,
            c.mint_asset_id.decompress(ctx).await?,
            c.gas_price.decompress(ctx).await?,
        ))
    }
}

#[tokio::test]
async fn example_struct_postcard_roundtrip_multiple() {
    #[derive(Debug, PartialEq, Default, Compress, Decompress)]
    pub struct Example {
        pub asset_id: AssetId,
        pub array: [u8; 32],
        pub vec: Vec<u8>,
        pub integer: u32,
        pub inner: Inner,
    }

    #[derive(Debug, PartialEq, Default, Compress, Decompress)]
    pub struct Inner {
        pub asset_id: AssetId,
        pub count: u64,
    }

    let rng = &mut StdRng::seed_from_u64(8586);

    let mut ctx = TestCompressionCtx::default();
    for _ in 0..10 {
        let original = Example {
            asset_id: AssetId::new(rng.r#gen()),
            array: rng.r#gen(),
            vec: (0..rng.gen_range(0..32))
                .map(|_| rng.r#gen::<u8>())
                .collect(),
            integer: rng.r#gen(),
            inner: Inner {
                asset_id: AssetId::new(rng.r#gen()),
                count: rng.r#gen(),
            },
        };
        let compressed = original
            .compress_with(&mut ctx)
            .await
            .expect("compression failed");
        let compressed_serialized =
            postcard::to_stdvec(&compressed).expect("failed to serialize");
        let compressed_deserialized =
            postcard::from_bytes(&compressed_serialized).expect("failed to deserialize");
        let decompressed = Example::decompress_with(compressed_deserialized, &ctx)
            .await
            .expect("decompression failed");
        assert_eq!(original, decompressed);
    }
}

#[tokio::test]
async fn skipped_fields_are_not_part_of_the_compressed_output() {
    #[derive(Debug, PartialEq, Default, Compress, Decompress)]
    pub struct NoSkip {
        pub common: u64,
    }
    #[derive(Debug, PartialEq, Default, Compress, Decompress)]
    pub struct Skip {
        pub common: u64,
        #[compress(skip)]
        pub skipped: u64,
    }

    let noskip = NoSkip { common: 123 };
    let skip = Skip {
        common: 123,
        skipped: 456,
    };

    let mut ctx = TestCompressionCtx::default();
    let noskip_compressed = noskip
        .compress_with(&mut ctx)
        .await
        .expect("compression failed");
    let noskip_compressed_serialized =
        postcard::to_stdvec(&noskip_compressed).expect("failed to serialize");

    let mut ctx = TestCompressionCtx::default();
    let skip_compressed = skip
        .compress_with(&mut ctx)
        .await
        .expect("compression failed");
    let skip_compressed_serialized =
        postcard::to_stdvec(&skip_compressed).expect("failed to serialize");

    assert_eq!(noskip_compressed_serialized, skip_compressed_serialized);
}

#[tokio::test]
async fn skipped_fields_are_set_to_default_when_deserializing() {
    #[derive(Debug, PartialEq, Default, Compress, Decompress)]
    pub struct Example {
        pub not_skipped: u64,
        #[compress(skip)]
        pub automatic: u64,
        #[compress(skip)]
        pub manual: HasManualDefault,
    }

    #[derive(Debug, PartialEq)]
    pub struct HasManualDefault {
        pub value: u64,
    }
    impl Default for HasManualDefault {
        fn default() -> Self {
            Self { value: 42 }
        }
    }

    let mut ctx = TestCompressionCtx::default();
    let original = Example {
        not_skipped: 123,
        automatic: 456,
        manual: HasManualDefault { value: 789 },
    };
    let compressed = original
        .compress_with(&mut ctx)
        .await
        .expect("compression failed");
    let decompressed = Example::decompress_with(compressed, &ctx)
        .await
        .expect("decompression failed");
    assert_eq!(
        decompressed,
        Example {
            not_skipped: 123,
            automatic: 0,
            manual: HasManualDefault::default(),
        }
    );
}

async fn verify_tx_roundtrip(tx: Transaction, ctx: &mut TestCompressionCtx) {
    let compressed = tx.compress_with(ctx).await.expect("compression failed");

    let postcard_compressed =
        postcard::to_stdvec(&compressed).expect("failed to serialize");
    let postcard_decompressed =
        postcard::from_bytes(&postcard_compressed).expect("failed to deserialize");
    pretty_assertions::assert_eq!(compressed, postcard_decompressed);

    let decompressed = postcard_decompressed
        .decompress(ctx)
        .await
        .expect("decompression failed");
    pretty_assertions::assert_eq!(tx, decompressed);
}

const NUMBER_CASES: usize = 100;

#[tokio::test]
async fn can_decompress_compressed_transaction_mint() {
    for mut tx in TransactionFactory::<_, Mint>::from_seed(1234).take(NUMBER_CASES) {
        let mut ctx = TestCompressionCtx::store_data_for_mint(&tx);
        tx.prepare_sign();
        verify_tx_roundtrip(tx.into(), &mut ctx).await;
    }
}

async fn assert_can_decompress_compressed_transaction<Tx, Iterator>(iterator: Iterator)
where
    Iterator: core::iter::Iterator<Item = Tx>,
    Tx: PrepareSign + field::Inputs + Clone + Into<Transaction>,
{
    let mut ctx = TestCompressionCtx::default();
    let txs = iterator
        .take(NUMBER_CASES)
        .map(|mut tx| {
            tx.prepare_sign();
            tx
        })
        .collect::<Vec<_>>();
    for tx in txs.iter() {
        ctx.store_tx_info(tx);
        verify_tx_roundtrip(tx.clone().into(), &mut ctx).await;
    }

    // Given
    let original_ctx = ctx.clone();

    // When
    for tx in txs.iter() {
        verify_tx_roundtrip(tx.clone().into(), &mut ctx).await;
    }

    // Then
    assert_eq!(original_ctx, ctx);
}

#[tokio::test]
async fn can_decompress_compressed_transaction_script() {
    let iter = TransactionFactory::<_, Script>::from_seed(1234).map(|(tx, _)| tx);
    assert_can_decompress_compressed_transaction(iter).await;
}

#[tokio::test]
async fn can_decompress_compressed_transaction_create() {
    let iter = TransactionFactory::<_, Create>::from_seed(1234).map(|(tx, _)| tx);
    assert_can_decompress_compressed_transaction(iter).await;
}

#[tokio::test]
async fn can_decompress_compressed_transaction_upgrade() {
    let iter = TransactionFactory::<_, Upgrade>::from_seed(1234).map(|(tx, _)| tx);
    assert_can_decompress_compressed_transaction(iter).await;
}

#[tokio::test]
async fn can_decompress_compressed_transaction_upload() {
    let iter = TransactionFactory::<_, Upload>::from_seed(1234).map(|(tx, _)| tx);
    assert_can_decompress_compressed_transaction(iter).await;
}

#[tokio::test]
async fn can_decompress_compressed_transaction_blob() {
    let iter = TransactionFactory::<_, Blob>::from_seed(1234).map(|(tx, _)| tx);
    assert_can_decompress_compressed_transaction(iter).await;
}