fuel_core/query/
message.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
use crate::fuel_core_graphql_api::database::ReadView;
use fuel_core_storage::{
    iter::{
        BoxedIter,
        IterDirection,
    },
    not_found,
    tables::Messages,
    Error as StorageError,
    Result as StorageResult,
    StorageAsRef,
};
use fuel_core_types::{
    blockchain::block::CompressedBlock,
    entities::relayer::message::{
        MerkleProof,
        Message,
        MessageProof,
        MessageStatus,
    },
    fuel_merkle::binary::in_memory::MerkleTree,
    fuel_tx::{
        input::message::compute_message_id,
        Receipt,
        TxId,
    },
    fuel_types::{
        Address,
        BlockHeight,
        Bytes32,
        MessageId,
        Nonce,
    },
    services::txpool::TransactionStatus,
};
use futures::{
    Stream,
    StreamExt,
    TryStreamExt,
};
use itertools::Itertools;
use std::borrow::Cow;

#[cfg(test)]
mod test;

pub trait MessageQueryData: Send + Sync {
    fn message(&self, message_id: &Nonce) -> StorageResult<Message>;

    fn owned_message_ids(
        &self,
        owner: &Address,
        start_message_id: Option<Nonce>,
        direction: IterDirection,
    ) -> BoxedIter<StorageResult<Nonce>>;

    fn owned_messages(
        &self,
        owner: &Address,
        start_message_id: Option<Nonce>,
        direction: IterDirection,
    ) -> BoxedIter<StorageResult<Message>>;

    fn all_messages(
        &self,
        start_message_id: Option<Nonce>,
        direction: IterDirection,
    ) -> BoxedIter<StorageResult<Message>>;
}

impl ReadView {
    pub fn message(&self, id: &Nonce) -> StorageResult<Message> {
        self.on_chain
            .as_ref()
            .storage::<Messages>()
            .get(id)?
            .ok_or(not_found!(Messages))
            .map(Cow::into_owned)
    }

    pub async fn messages(
        &self,
        ids: Vec<Nonce>,
    ) -> impl Iterator<Item = StorageResult<Message>> + '_ {
        // TODO: Use multiget when it's implemented.
        //  https://github.com/FuelLabs/fuel-core/issues/2344
        let messages = ids.into_iter().map(|id| self.message(&id));
        // Give a chance to other tasks to run.
        tokio::task::yield_now().await;
        messages
    }

    pub fn owned_messages<'a>(
        &'a self,
        owner: &'a Address,
        start_message_id: Option<Nonce>,
        direction: IterDirection,
    ) -> impl Stream<Item = StorageResult<Message>> + 'a {
        self.owned_message_ids(owner, start_message_id, direction)
            .chunks(self.batch_size)
            .map(|chunk| {
                let chunk = chunk.into_iter().try_collect::<_, Vec<_>, _>()?;
                Ok(chunk)
            })
            .try_filter_map(move |chunk| async move {
                let chunk = self.messages(chunk).await;
                Ok::<_, StorageError>(Some(futures::stream::iter(chunk)))
            })
            .try_flatten()
    }
}

/// Trait that specifies all the data required by the output message query.
pub trait MessageProofData {
    /// Get the block.
    fn block(&self, id: &BlockHeight) -> StorageResult<CompressedBlock>;

    /// Get the status of a transaction.
    fn transaction_status(
        &self,
        transaction_id: &TxId,
    ) -> StorageResult<TransactionStatus>;

    /// Gets the [`MerkleProof`] for the message block at `message_block_height` height
    /// relatively to the commit block where message block <= commit block.
    fn block_history_proof(
        &self,
        message_block_height: &BlockHeight,
        commit_block_height: &BlockHeight,
    ) -> StorageResult<MerkleProof>;
}

impl MessageProofData for ReadView {
    fn block(&self, id: &BlockHeight) -> StorageResult<CompressedBlock> {
        self.block(id)
    }

    fn transaction_status(
        &self,
        transaction_id: &TxId,
    ) -> StorageResult<TransactionStatus> {
        self.tx_status(transaction_id)
    }

    fn block_history_proof(
        &self,
        message_block_height: &BlockHeight,
        commit_block_height: &BlockHeight,
    ) -> StorageResult<MerkleProof> {
        self.block_history_proof(message_block_height, commit_block_height)
    }
}

/// Generate an output proof.
pub fn message_proof<T: MessageProofData + ?Sized>(
    database: &T,
    transaction_id: Bytes32,
    desired_nonce: Nonce,
    commit_block_height: BlockHeight,
) -> StorageResult<MessageProof> {
    // Get the block id from the transaction status if it's ready.
    let (message_block_height, (sender, recipient, nonce, amount, data)) = match database.transaction_status(&transaction_id) {
        Ok(TransactionStatus::Success { block_height, receipts, .. }) => (
            block_height,
            receipts.into_iter()
            .find_map(|r| match r {
                Receipt::MessageOut {
                    sender,
                    recipient,
                    nonce,
                    amount,
                    data,
                    ..
                } if r.nonce() == Some(&desired_nonce) => {
                    Some((sender, recipient, nonce, amount, data))
                }
                _ => None,
            })
            .ok_or::<StorageError>(
                anyhow::anyhow!("Desired `nonce` missing in transaction receipts").into(),
            )?
        ),
        Ok(TransactionStatus::Submitted { .. }) => {
            return Err(anyhow::anyhow!(
                "Unable to obtain the message block height. The transaction has not been processed yet"
            )
            .into())
        }
        Ok(TransactionStatus::SqueezedOut { reason }) => {
            return Err(anyhow::anyhow!(
                "Unable to obtain the message block height. The transaction was squeezed out: {reason}"
            )
            .into())
        }
        Ok(TransactionStatus::Failed { .. }) => {
            return Err(anyhow::anyhow!(
                "Unable to obtain the message block height. The transaction failed"
            )
            .into())
        }
        Err(err) => {
            return Err(anyhow::anyhow!(
                "Unable to obtain the message block height: {err}"
            )
            .into())
        }
    };

    let Some(data) = data else {
        return Err(anyhow::anyhow!("Output message doesn't contain any `data`").into())
    };

    // Get the message fuel block header.
    let (message_block_header, message_block_txs) =
        match database.block(&message_block_height) {
            Ok(message_block) => message_block.into_inner(),
            Err(err) => {
                return Err(anyhow::anyhow!(
                    "Unable to get the message block from the database: {err}"
                )
                .into())
            }
        };

    let message_id = compute_message_id(&sender, &recipient, &nonce, amount, &data);

    let message_proof = message_receipts_proof(database, message_id, &message_block_txs)?;

    // Get the commit fuel block header.
    let (commit_block_header, _) = match database.block(&commit_block_height) {
        Ok(commit_block_header) => commit_block_header.into_inner(),
        Err(err) => {
            return Err(anyhow::anyhow!(
                "Unable to get commit block header from database: {err}"
            )
            .into())
        }
    };

    let Some(verifiable_commit_block_height) = commit_block_header.height().pred() else {
        return Err(anyhow::anyhow!(
            "Impossible to generate proof beyond the genesis block"
        )
        .into())
    };
    let block_proof = database.block_history_proof(
        message_block_header.height(),
        &verifiable_commit_block_height,
    )?;

    Ok(MessageProof {
        message_proof,
        block_proof,
        message_block_header,
        commit_block_header,
        sender,
        recipient,
        nonce,
        amount,
        data,
    })
}

fn message_receipts_proof<T: MessageProofData + ?Sized>(
    database: &T,
    message_id: MessageId,
    message_block_txs: &[Bytes32],
) -> StorageResult<MerkleProof> {
    // Get the message receipts from the block.
    let leaves: Vec<Vec<Receipt>> = message_block_txs
        .iter()
        .filter_map(|id| match database.transaction_status(id) {
            Ok(TransactionStatus::Success { receipts, .. }) => Some(Ok(receipts)),
            Ok(_) => None,
            Err(err) => Some(Err(err)),
        })
        .try_collect()?;
    let leaves = leaves.into_iter()
        // Flatten the receipts after filtering on output messages
        // and mapping to message ids.
        .flat_map(|receipts|
            receipts.into_iter().filter_map(|r| r.message_id()));

    // Build the merkle proof from the above iterator.
    let mut tree = MerkleTree::new();

    let mut proof_index = None;

    for (index, id) in leaves.enumerate() {
        // Check if this is the message id being proved.
        if message_id == id {
            // Save the index of this message to use as the proof index.
            proof_index = Some(index as u64);
        }

        // Build the merkle tree.
        tree.push(id.as_ref());
    }

    // Check if we found a leaf.
    let Some(proof_index) = proof_index else {
        return Err(anyhow::anyhow!(
            "Unable to find the message receipt in the transaction to generate the proof"
        )
        .into())
    };

    // Get the proof set.
    let Some((_, proof_set)) = tree.prove(proof_index) else {
        return Err(anyhow::anyhow!(
            "Unable to generate the Merkle proof for the message from its receipts"
        )
        .into());
    };

    // Return the proof.
    Ok(MerkleProof {
        proof_set,
        proof_index,
    })
}

pub fn message_status(
    database: &ReadView,
    message_nonce: Nonce,
) -> StorageResult<MessageStatus> {
    if database.message_is_spent(&message_nonce)? {
        Ok(MessageStatus::spent())
    } else if database.message_exists(&message_nonce)? {
        Ok(MessageStatus::unspent())
    } else {
        Ok(MessageStatus::not_found())
    }
}

#[cfg(test)]
mod tests {
    use std::collections::HashMap;

    use fuel_core_storage::not_found;
    use fuel_core_types::{
        blockchain::block::CompressedBlock,
        entities::relayer::message::MerkleProof,
        fuel_tx::{
            Address,
            Bytes32,
            Receipt,
            TxId,
        },
        fuel_types::{
            BlockHeight,
            Nonce,
        },
        services::txpool::TransactionStatus,
        tai64::Tai64,
    };

    use super::{
        message_proof,
        MessageProofData,
    };

    pub struct FakeDB {
        pub blocks: HashMap<BlockHeight, CompressedBlock>,
        pub transaction_statuses: HashMap<TxId, TransactionStatus>,
        pub receipts: HashMap<TxId, Vec<Receipt>>,
    }

    impl FakeDB {
        fn new() -> Self {
            Self {
                blocks: HashMap::new(),
                transaction_statuses: HashMap::new(),
                receipts: HashMap::new(),
            }
        }

        fn insert_block(&mut self, block_height: BlockHeight, block: CompressedBlock) {
            self.blocks.insert(block_height, block);
        }

        fn insert_transaction_status(
            &mut self,
            transaction_id: TxId,
            status: TransactionStatus,
        ) {
            self.transaction_statuses.insert(transaction_id, status);
        }

        fn insert_receipts(&mut self, transaction_id: TxId, receipts: Vec<Receipt>) {
            self.receipts.insert(transaction_id, receipts);
        }
    }

    impl MessageProofData for FakeDB {
        fn block(&self, id: &BlockHeight) -> fuel_core_storage::Result<CompressedBlock> {
            self.blocks.get(id).cloned().ok_or(not_found!("Block"))
        }

        fn transaction_status(
            &self,
            transaction_id: &TxId,
        ) -> fuel_core_storage::Result<TransactionStatus> {
            self.transaction_statuses
                .get(transaction_id)
                .cloned()
                .ok_or(not_found!("Transaction status"))
        }

        fn block_history_proof(
            &self,
            _message_block_height: &BlockHeight,
            _commit_block_height: &BlockHeight,
        ) -> fuel_core_storage::Result<MerkleProof> {
            // Unused in current tests
            Ok(MerkleProof::default())
        }
    }

    // Test will try to get the message receipt proof with a block with only valid transactions
    // Then add an invalid transaction and check if the proof is still the same (meaning the invalid transaction was ignored)
    #[test]
    fn test_message_proof_ignore_failed() {
        // Create a fake database
        let mut database = FakeDB::new();

        // Given
        // Create a block with a valid transaction and receipts
        let mut block = CompressedBlock::default();
        let block_height: BlockHeight = BlockHeight::new(1);
        block.header_mut().set_block_height(block_height);
        let valid_tx_id = Bytes32::new([1; 32]);
        let mut valid_tx_receipts = vec![];
        for i in 0..100 {
            valid_tx_receipts.push(Receipt::MessageOut {
                sender: Address::default(),
                recipient: Address::default(),
                amount: 0,
                nonce: 0.into(),
                len: 32,
                digest: Bytes32::default(),
                data: Some(vec![i; 32]),
            });
        }
        block.transactions_mut().push(valid_tx_id);
        database.insert_block(block_height, block.clone());
        database.insert_transaction_status(
            valid_tx_id,
            TransactionStatus::Success {
                time: Tai64::UNIX_EPOCH,
                block_height,
                receipts: valid_tx_receipts.clone(),
                total_fee: 0,
                total_gas: 0,
                result: None,
            },
        );
        database.insert_receipts(valid_tx_id, valid_tx_receipts.clone());

        // Get the message proof with the valid transaction
        let message_proof_valid_tx =
            message_proof(&database, valid_tx_id, Nonce::default(), block_height)
                .unwrap();

        // Add an invalid transaction with receipts to the block
        let invalid_tx_id = Bytes32::new([2; 32]);
        block.transactions_mut().push(invalid_tx_id);
        database.insert_block(block_height, block.clone());
        let mut invalid_tx_receipts = vec![];
        for i in 0..100 {
            invalid_tx_receipts.push(Receipt::MessageOut {
                sender: Address::default(),
                recipient: Address::default(),
                amount: 0,
                nonce: 0.into(),
                len: 33,
                digest: Bytes32::default(),
                data: Some(vec![i; 33]),
            });
        }
        database.insert_transaction_status(
            invalid_tx_id,
            TransactionStatus::Failed {
                time: Tai64::UNIX_EPOCH,
                block_height,
                result: None,
                total_fee: 0,
                total_gas: 0,
                receipts: invalid_tx_receipts.clone(),
            },
        );
        database.insert_receipts(invalid_tx_id, invalid_tx_receipts.clone());

        // When
        // Get the message proof with the same message id
        let message_proof_invalid_tx =
            message_proof(&database, valid_tx_id, Nonce::default(), block_height)
                .unwrap();

        // Then
        // The proof should be the same because the invalid transaction was ignored
        assert_eq!(message_proof_valid_tx, message_proof_invalid_tx);
    }
}