tari_wallet 0.8.1

Tari cryptocurrency wallet library
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
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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
// Copyright 2020. The Tari Project
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
// following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
// disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
// following disclaimer in the documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
// products derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

use crate::output_manager_service::{
    error::{OutputManagerError, OutputManagerProtocolError},
    handle::OutputManagerEvent,
    service::OutputManagerResources,
    storage::{database::OutputManagerBackend, models::DbUnblindedOutput},
};
use futures::{FutureExt, StreamExt};
use log::*;
use rand::{rngs::OsRng, RngCore};
use std::{cmp, collections::HashMap, convert::TryFrom, fmt, sync::Arc, time::Duration};
use tari_comms::types::CommsPublicKey;
use tari_comms_dht::domain_message::OutboundDomainMessage;
use tari_core::{
    proto::{
        base_node as proto,
        base_node::{
            base_node_service_request::Request as BaseNodeRequestProto,
            base_node_service_response::Response as BaseNodeResponseProto,
        },
    },
    transactions::{transaction::TransactionOutput, types::Commitment},
};
use tari_crypto::tari_utilities::{hash::Hashable, hex::Hex};
use tari_p2p::tari_message::TariMessageType;
use tokio::{sync::broadcast, time::delay_for};

const LOG_TARGET: &str = "wallet::output_manager_service::protocols::utxo_validation_protocol";

pub struct TxoValidationProtocol<TBackend>
where TBackend: OutputManagerBackend + 'static
{
    id: u64,
    validation_type: TxoValidationType,
    retry_strategy: TxoValidationRetry,
    resources: OutputManagerResources<TBackend>,
    base_node_public_key: CommsPublicKey,
    timeout: Duration,
    base_node_response_receiver: Option<broadcast::Receiver<Arc<proto::BaseNodeServiceResponse>>>,
    cancellation_receiver: Option<broadcast::Receiver<()>>,
    pending_queries: HashMap<u64, Vec<Vec<u8>>>,
}

/// This protocol defines the process of submitting our current UTXO set to the Base Node to validate it.
impl<TBackend> TxoValidationProtocol<TBackend>
where TBackend: OutputManagerBackend + 'static
{
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        id: u64,
        validation_type: TxoValidationType,
        retry_strategy: TxoValidationRetry,
        resources: OutputManagerResources<TBackend>,
        base_node_public_key: CommsPublicKey,
        timeout: Duration,
        base_node_response_receiver: broadcast::Receiver<Arc<proto::BaseNodeServiceResponse>>,
        cancellation_receiver: broadcast::Receiver<()>,
    ) -> Self
    {
        Self {
            id,
            validation_type,
            retry_strategy,
            resources,
            base_node_public_key,
            timeout,
            base_node_response_receiver: Some(base_node_response_receiver),
            cancellation_receiver: Some(cancellation_receiver),
            pending_queries: Default::default(),
        }
    }

    /// The task that defines the execution of the protocol.
    pub async fn execute(mut self) -> Result<u64, OutputManagerProtocolError> {
        let mut base_node_response_receiver = self
            .base_node_response_receiver
            .take()
            .ok_or_else(|| {
                OutputManagerProtocolError::new(
                    self.id,
                    OutputManagerError::ServiceError("No base node response channel provided".to_string()),
                )
            })?
            .fuse();

        let mut cancellation_receiver = self
            .cancellation_receiver
            .take()
            .ok_or_else(|| {
                OutputManagerProtocolError::new(
                    self.id,
                    OutputManagerError::ServiceError("No cancellation channel provided".to_string()),
                )
            })?
            .fuse();

        debug!(
            target: LOG_TARGET,
            "Starting TXO validation protocol (Id: {}) for {}", self.id, self.validation_type,
        );

        let outputs_to_query: Vec<Vec<u8>> = match self.validation_type {
            TxoValidationType::Unspent => self
                .resources
                .db
                .get_unspent_outputs()
                .await
                .map_err(|e| {
                    OutputManagerProtocolError::new(self.id, OutputManagerError::OutputManagerStorageError(e))
                })?
                .iter()
                .map(|uo| uo.hash.clone())
                .collect(),
            TxoValidationType::Spent => self
                .resources
                .db
                .get_spent_outputs()
                .await
                .map_err(|e| {
                    OutputManagerProtocolError::new(self.id, OutputManagerError::OutputManagerStorageError(e))
                })?
                .iter()
                .map(|uo| uo.hash.clone())
                .collect(),
            TxoValidationType::Invalid => self
                .resources
                .db
                .get_invalid_outputs()
                .await
                .map_err(|e| {
                    OutputManagerProtocolError::new(self.id, OutputManagerError::OutputManagerStorageError(e))
                })?
                .into_iter()
                .map(|uo| uo.hash)
                .collect(),
        };

        if outputs_to_query.is_empty() {
            debug!(
                target: LOG_TARGET,
                "TXO validation protocol (Id: {}) has no outputs to validate", self.id,
            );
            let _ = self
                .resources
                .event_publisher
                .send(OutputManagerEvent::TxoValidationSuccess(self.id))
                .map_err(|e| {
                    trace!(
                        target: LOG_TARGET,
                        "Error sending event {:?}, because there are no subscribers.",
                        e.0
                    );
                    e
                });
            return Ok(self.id);
        }

        let total_retries_str = match self.retry_strategy {
            TxoValidationRetry::Limited(n) => format!("{}", n),
            TxoValidationRetry::UntilSuccess => "∞".to_string(),
        };

        let mut retries = 0;
        loop {
            self.send_queries(outputs_to_query.clone()).await?;

            let mut delay = delay_for(self.timeout).fuse();

            loop {
                futures::select! {
                    base_node_response = base_node_response_receiver.select_next_some() => {
                        match base_node_response {
                            Ok(response) => if self.handle_base_node_response(response).await? {
                                trace!(target: LOG_TARGET, "Response handled with success for {} and pending_queries len: {}", self.id, self.pending_queries.len());
                                if self.pending_queries.is_empty() {
                                    let _ = self
                                        .resources
                                        .event_publisher
                                        .send(OutputManagerEvent::TxoValidationSuccess(self.id))
                                        .map_err(|e| {
                                           trace!(
                                                target: LOG_TARGET,
                                                "Error sending event {:?}, because there are no subscribers.",
                                                e.0
                                            );
                                            e
                                        });
                                    return Ok(self.id);
                                }
                            },
                            Err(e) => trace!(target: LOG_TARGET, "Error reading broadcast base_node_response: {:?}", e),
                        }

                    },
                    cancellation_trigger = cancellation_receiver.select_next_some() => {
                        if let Ok(()) = cancellation_trigger {
                            info!(target: LOG_TARGET, "TXO Validation protocol (Id: {}) is ending due to cancellation", self.id);
                            let _ = self
                                .resources
                                .event_publisher
                                .send(OutputManagerEvent::TxoValidationAborted(self.id))
                                .map_err(|e| {
                                    trace!(
                                        target: LOG_TARGET,
                                        "Error sending event {:?}, because there are no subscribers.",
                                        e.0
                                    );
                                    e
                                });
                            return Err(OutputManagerProtocolError::new(
                                self.id,
                                OutputManagerError::Cancellation,
                            ))
                        }
                    },
                    () = delay => {
                        break;
                    },
                }
            }

            debug!(
                target: LOG_TARGET,
                "TXO Validation protocol (Id: {}) attempt {} out of {} timed out.",
                self.id,
                retries + 1,
                total_retries_str
            );

            let _ = self
                .resources
                .event_publisher
                .send(OutputManagerEvent::TxoValidationTimedOut(self.id))
                .map_err(|e| {
                    trace!(
                        target: LOG_TARGET,
                        "Error sending event {:?}, because there are no subscribers.",
                        e.0
                    );
                    e
                });

            retries += 1;
            match self.retry_strategy {
                TxoValidationRetry::Limited(n) => {
                    if retries >= n {
                        break;
                    }
                },
                TxoValidationRetry::UntilSuccess => (),
            }

            self.pending_queries.clear();
        }

        info!(
            target: LOG_TARGET,
            "Maximum attempts exceeded for TXO Validation Protocol (Id: {})", self.id
        );
        Err(OutputManagerProtocolError::new(
            self.id,
            OutputManagerError::MaximumAttemptsExceeded,
        ))
    }

    async fn send_queries(&mut self, mut outputs_to_query: Vec<Vec<u8>>) -> Result<(), OutputManagerProtocolError> {
        // Determine how many rounds of base node request we need to query all the outputs in batches of
        // max_utxo_query_size
        let rounds =
            ((outputs_to_query.len() as f32) / (self.resources.config.max_utxo_query_size as f32 + 0.1)) as usize + 1;

        for r in 0..rounds {
            let mut output_hashes = Vec::new();
            for uo_hash in
                outputs_to_query.drain(..cmp::min(self.resources.config.max_utxo_query_size, outputs_to_query.len()))
            {
                output_hashes.push(uo_hash);
            }

            let request_key = if r == 0 { self.id } else { OsRng.next_u64() };

            let request = BaseNodeRequestProto::FetchMatchingUtxos(proto::HashOutputs {
                outputs: output_hashes.clone(),
            });

            let service_request = proto::BaseNodeServiceRequest {
                request_key,
                request: Some(request),
            };

            let send_message_response = self
                .resources
                .outbound_message_service
                .send_direct(
                    self.base_node_public_key.clone(),
                    OutboundDomainMessage::new(TariMessageType::BaseNodeRequest, service_request),
                )
                .await
                .map_err(|e| OutputManagerProtocolError::new(self.id, OutputManagerError::from(e)))?;

            // Here we are going to spawn a non-blocking task that will monitor and log the progress of the
            // send process.
            tokio::spawn(async move {
                match send_message_response.resolve().await {
                    Err(e) => trace!(
                        target: LOG_TARGET,
                        "Failed to send Output Manager TXO query ({}) to Base Node: {}",
                        request_key,
                        e
                    ),
                    Ok(send_states) => {
                        trace!(
                            target: LOG_TARGET,
                            "Output Manager TXO query ({}) queued for sending with Message {}",
                            request_key,
                            send_states[0].tag,
                        );
                        let message_tag = send_states[0].tag;
                        if send_states.wait_single().await {
                            trace!(
                                target: LOG_TARGET,
                                "Output Manager TXO query ({}) successfully sent to Base Node with Message {}",
                                request_key,
                                message_tag,
                            )
                        } else {
                            trace!(
                                target: LOG_TARGET,
                                "Failed to send Output Manager TXO query ({}) to Base Node with Message {}",
                                request_key,
                                message_tag,
                            );
                        }
                    },
                }
            });

            self.pending_queries.insert(request_key, output_hashes);

            info!(
                target: LOG_TARGET,
                "Output Manager {} query (Id: {}) sent to Base Node, part {} of {} requests",
                self.validation_type,
                request_key,
                r + 1,
                rounds
            );
        }

        Ok(())
    }

    async fn handle_base_node_response(
        &mut self,
        response: Arc<proto::BaseNodeServiceResponse>,
    ) -> Result<bool, OutputManagerProtocolError>
    {
        let request_key = response.request_key;
        if !response.is_synced {
            warn!(
                target: LOG_TARGET,
                "Assigned Base Node is not synced to chain tip, aborted TXO Validation protocol (id: {})", self.id
            );
            let _ = self
                .resources
                .event_publisher
                .send(OutputManagerEvent::TxoValidationAborted(self.id))
                .map_err(|e| {
                    trace!(
                        target: LOG_TARGET,
                        "Error sending event {:?}, because there are no subscribers.",
                        e.0
                    );
                    e
                });
            return Err(OutputManagerProtocolError::new(
                self.id,
                OutputManagerError::BaseNodeNotSynced,
            ));
        }
        let queried_hashes = if let Some(hashes) = self.pending_queries.remove(&request_key) {
            hashes
        } else {
            trace!(
                target: LOG_TARGET,
                "Base Node Response (Id: {}) not expected for TXO Validation protocol {}",
                request_key,
                self.id
            );
            return Ok(false);
        };

        trace!(
            target: LOG_TARGET,
            "Handling a Base Node Response for {} request (Id: {}) for TXO Validation protocol {}",
            self.validation_type,
            request_key,
            self.id
        );

        let response: Vec<tari_core::proto::types::TransactionOutput> = match (*response).clone().response {
            Some(BaseNodeResponseProto::TransactionOutputs(outputs)) => outputs.outputs,
            _ => {
                return Err(OutputManagerProtocolError::new(
                    self.id,
                    OutputManagerError::InvalidResponseError("Base Node Response of unexpected variant".to_string()),
                ));
            },
        };

        match self.validation_type {
            TxoValidationType::Unspent => {
                // Construct a HashMap of all the unspent outputs
                let unspent_outputs: Vec<DbUnblindedOutput> =
                    self.resources.db.get_unspent_outputs().await.map_err(|e| {
                        OutputManagerProtocolError::new(self.id, OutputManagerError::OutputManagerStorageError(e))
                    })?;

                // We only want to check outputs that we were expecting and are still valid
                let mut output_hashes = HashMap::new();
                for uo in unspent_outputs.iter() {
                    let hash = uo.hash.clone();
                    if queried_hashes.iter().any(|h| &hash == h) {
                        output_hashes.insert(hash, uo.clone());
                    }
                }

                // Go through all the returned UTXOs and if they are in the hashmap remove them
                for output in response.iter() {
                    let response_hash = TransactionOutput::try_from(output.clone())
                        .map_err(|_| {
                            OutputManagerProtocolError::new(
                                self.id,
                                OutputManagerError::ConversionError(
                                    "Could not convert protobuf TransactionOutput".to_string(),
                                ),
                            )
                        })?
                        .hash();

                    let _ = output_hashes.remove(&response_hash);
                }

                // If there are any remaining Unspent Outputs we will move them to the invalid collection
                for (_k, v) in output_hashes {
                    // Get the transaction these belonged to so we can display the kernel signature of the transaction
                    // this output belonged to.

                    warn!(
                        target: LOG_TARGET,
                        "Output with value {} not returned from Base Node query ({}) and is thus being invalidated",
                        v.unblinded_output.value,
                        request_key,
                    );
                    // If the output that is being invalidated has an associated TxId then get the kernel signature of
                    // the transaction and display for easier debugging
                    if let Some(tx_id) = self.resources.db.invalidate_output(v).await.map_err(|e| {
                        OutputManagerProtocolError::new(self.id, OutputManagerError::OutputManagerStorageError(e))
                    })? {
                        if let Ok(transaction) = self
                            .resources
                            .transaction_service
                            .get_completed_transaction(tx_id)
                            .await
                        {
                            info!(
                                target: LOG_TARGET,
                                "Invalidated Output is from Transaction (TxId: {}) with message: {} and Kernel \
                                 Signature: {}",
                                transaction.tx_id,
                                transaction.message,
                                transaction.transaction.body.kernels()[0]
                                    .excess_sig
                                    .get_signature()
                                    .to_hex()
                            )
                        }
                    } else {
                        info!(
                            target: LOG_TARGET,
                            "Invalidated Output does not have an associated TxId, it is likely a Coinbase output lost \
                             to a Re-Org"
                        );
                    }
                }
                debug!(
                    target: LOG_TARGET,
                    "Handled Base Node response (Id: {}) for Unspent Outputs Query {}", request_key, self.id
                );
            },
            TxoValidationType::Invalid => {
                let invalid_outputs = self.resources.db.get_invalid_outputs().await.map_err(|e| {
                    OutputManagerProtocolError::new(self.id, OutputManagerError::OutputManagerStorageError(e))
                })?;

                for output in response.iter() {
                    let response_hash = TransactionOutput::try_from(output.clone())
                        .map_err(|_| {
                            OutputManagerProtocolError::new(
                                self.id,
                                OutputManagerError::ConversionError("Could not convert Transaction Output".to_string()),
                            )
                        })?
                        .hash();

                    if let Some(output) = invalid_outputs.iter().find(|o| o.hash == response_hash) {
                        if self
                            .resources
                            .db
                            .revalidate_output(output.commitment.clone())
                            .await
                            .is_ok()
                        {
                            info!(
                                target: LOG_TARGET,
                                "Output with value {} has been restored to a valid spendable output",
                                output.unblinded_output.value
                            );
                        }
                    }
                }

                debug!(
                    target: LOG_TARGET,
                    "Handled Base Node response (Id: {}) for Invalidated Outputs Query {}", request_key, self.id
                );
            },
            TxoValidationType::Spent => {
                // Go through the response outputs and check if they are currently Spent, if they are then they can be
                // marked as Unspent because they exist in the UTXO set. Hooray!
                for output in response.iter() {
                    if let Some(Some(commitment)) = output.clone().commitment.map(|c| Commitment::try_from(c).ok()) {
                        match self.resources.db.update_spent_output_to_unspent(commitment).await {
                            Ok(uo) => info!(
                                target: LOG_TARGET,
                                "Spent output with value {} restored to Unspent output", uo.unblinded_output.value
                            ),
                            Err(e) => debug!(target: LOG_TARGET, "Unable to restore Spent output to Unspent: {}", e),
                        }
                    }
                }

                debug!(
                    target: LOG_TARGET,
                    "Handled Base Node response (Id: {}) for Spent Outputs Query {}", request_key, self.id
                );
            },
        }
        Ok(true)
    }
}

pub enum TxoValidationType {
    Unspent,
    Spent,
    Invalid,
}

impl fmt::Display for TxoValidationType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            TxoValidationType::Unspent => write!(f, "Unspent Outputs Validation"),
            TxoValidationType::Spent => write!(f, "Spent Outputs Validation"),
            TxoValidationType::Invalid => write!(f, "Invalid Outputs Validation"),
        }
    }
}

// 0 means keep retying until success
#[derive(Debug)]
pub enum TxoValidationRetry {
    Limited(u8),
    UntilSuccess,
}