redgold 0.1.0

A relative proof conflict resolution & irreversibility service with trust modeling custodial pooling and executor contracts
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
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
use std::collections::{HashMap, HashSet};
use std::sync::Arc;
use std::time::{Duration, SystemTime};

use crossbeam_channel::{unbounded, Receiver, Sender};
use dashmap::mapref::entry::Entry;
use flume::TryRecvError;
use futures::{TryFutureExt, TryStreamExt};
use itertools::Itertools;
use log::{debug, error, info};
use metrics::increment_counter;
use tokio::runtime::Runtime;
use tokio::select;
use tokio::task::{JoinError, JoinHandle};
use uuid::Uuid;
use redgold_schema::{json_or, ProtoHashable, SafeOption, struct_metadata_new, structs, task_local, task_local_map, WithMetadataHashableFields};
use redgold_schema::structs::{FixedUtxoId, GossipTransactionRequest, Hash, PublicResponse, QueryObservationProofRequest, Request, Response, ValidationType};

use crate::core::internal_message::{Channel, new_bounded_channel, PeerMessage, RecvAsyncErrorInfo, SendErrorInfo, TransactionMessage};
use crate::core::relay::Relay;
use crate::core::transaction::{TransactionTestContext, validate_utxo};
use crate::data::data_store::DataStore;
use crate::schema::structs::{Error, ResponseMetadata};
use crate::schema::structs::{HashType, ObservationMetadata, State, Transaction};
use crate::schema::structs::{QueryTransactionResponse, SubmitTransactionResponse};
use crate::schema::{SafeBytesAccess, WithMetadataHashable};
use crate::util::runtimes::build_runtime;
// TODO config
use crate::schema::structs::ErrorInfo;
use crate::schema::structs::ObservationProof;
use crate::schema::{empty_public_response, error_info, error_message};
use crate::util;
use futures::{stream::FuturesUnordered, StreamExt};
use redgold_schema::output::tx_output_data;
use crate::core::resolver::resolve_transaction;
use crate::core::transact::utxo_conflict_resolver::check_utxo_conflicts;
use crate::util::current_time_millis_i64;
use redgold_schema::EasyJson;
use crate::util::logging::Loggable;

#[derive(Clone)]
pub struct Conflict {
    //  Not really necessary but put other info here.
    transaction_hash: Hash,
    abort: bool,
    processing_start_time: i64,
    request_processer: RequestProcessor,
}

#[derive(Clone)]
pub enum ProcessTransactionMessage {
    ProofReceived(ObservationProof),
    // TODO: Do we need a type here for representing an immediate pending signature? probably not
}

#[derive(Clone)]
pub struct RequestProcessor {
    sender: flume::Sender<Conflict>,
    receiver: flume::Receiver<Conflict>,
    request_id: String,
    pub transaction_hash: Hash,
    pub transaction: Transaction,
    pub internal_channel: Channel<ProcessTransactionMessage>
}

#[derive(Clone)]
pub struct UTXOContentionPool {
    active_requests: Vec<Conflict>,
}

impl RequestProcessor {
    fn new(transaction_hash: &Hash, request_id: String, transaction: Transaction) -> RequestProcessor {
        let (s, r) = flume::unbounded::<Conflict>();
        return RequestProcessor {
            sender: s,
            receiver: r,
            request_id,
            transaction_hash: transaction_hash.clone(),
            transaction,
            internal_channel: new_bounded_channel(200),
        };
    }
}

#[derive(Clone)]
pub struct TransactionProcessContext {
    relay: Relay,
    request_processor: Option<RequestProcessor>,
    transaction_hash: Option<Hash>,
    utxo_ids: Option<Vec<FixedUtxoId>>
}


// TODO: Refactor
async fn resolve_conflict(relay: Relay, conflicts: Vec<Conflict>) -> Result<Hash, ErrorInfo> {
    //relay.ds.select_peer_trust()
    // TODO: Add hasher interface to relay / node_config
    // let leaf_hash = util::dhash_vec(&transaction_hash).to_vec();

    // TODO: add fields pct_acceptance network 0.8 for instance fraction of peers / fraction of trust accepted.
    // TODO: Trust within partition id. partitions should be flexible in size relative to node size.
    // or rather node size determines minimum -- partition?

    // TODO: replace this func
    let peer_publics = relay.ds.select_broadcast_peers().unwrap();
    let mut map = HashMap::<Vec<u8>, f64>::new();
    for peer in peer_publics {
        map.insert(peer.public_key, peer.trust);
    }

    let mut trust_conflicts = vec![];
    for c in conflicts {
        let res = relay
            .ds
            .observation.select_observation_edge(&c.transaction_hash.clone())
            .await?;
        // TODO: .toMap trait.
        // TODO: group by peer id instead, more accurate
        let sum_trust = res
            .iter()
            // TODO: drop functional syntax here or deal with error somehow
            .into_group_map_by(|x| {
                let ret: Vec<u8> = x
                    .proof
                    .as_ref()
                    .map(|p| p.public_key_bytes().as_ref().unwrap().clone())
                    .ok_or("")
                    .unwrap();
                ret
            })
            .iter()
            .map(|(k, _)| map.get(k).unwrap_or(&(0 as f64)))
            .sum::<f64>();
        trust_conflicts.push((
            (-1 as f64) * sum_trust,
            c.processing_start_time,
            c.transaction_hash.clone(),
        ));
    }
    // TODO: Order hash directly in proto schema generation
    trust_conflicts.sort_by(|(_, _, h1), (_, _, h2)| h1.vec().cmp(&h2.vec()));
    Ok(trust_conflicts.get(0).unwrap().2.clone())
}

impl TransactionProcessContext {

    // Init
    pub fn new(relay: Relay
               // , tx_process_listener: Arc<Runtime>
               // , tx_process: Arc<Runtime>
    ) -> JoinHandle<Result<(), ErrorInfo>> {
        let context = Self {
            relay,
            // tx_process: tx_process.clone(),
            request_processor: None,
            transaction_hash: None,
            utxo_ids: None,
        };

        return tokio::spawn(async move { context.run().await });
    }

    /// Loop to check messages and process them
    // TODO: Abstract this out
    async fn run(&self) -> Result<(), ErrorInfo> {
        increment_counter!("redgold.node.async_started");
        // let mut fut = FutLoopPoll::new();
        // TODO: Change to queue
        let receiver = self.relay.transaction.receiver.clone();
        // TODO this accomplishes queue, we can also move the spawn function into FutLoopPoll so that
        // the only function we have here is to call one function
        // do that later
        // receiver.stream().try_for_each_concurrent()
        // We can also use a Context type parameter over it to keep track of some internal context
        // per request i.e. RequestContext.
        receiver.into_stream().map(|x| {
            // info!("Transaction receiver map stream");
            Ok(x)
        })
            .try_for_each_concurrent(100, |transaction| {
            // info!("Transaction receiver try for each stream");
            let mut x = self.clone();
            async move {
                x.scoped_process_and_respond(transaction).await
            }
        }).await
    }

    async fn scoped_process_and_respond(&mut self, transaction_message: TransactionMessage) -> Result<(), ErrorInfo> {
        let request_uuid = Uuid::new_v4().to_string();
        let hex = transaction_message.transaction.calculate_hash().hex();
        let time = transaction_message.transaction.time().map(|x| x.clone()).unwrap_or(0);
        let current_time = util::current_time_millis_i64();
        let input_address = transaction_message.transaction.first_input_address().clone()
            .and_then(|a| a.render_string().ok()).unwrap_or("".to_string());
        let output_address = transaction_message.transaction.first_output_address()
            .and_then(|a| a.render_string().ok()).unwrap_or("".to_string());
        let node_id = self.relay.node_config.short_id()?;
        let mut hm = HashMap::new();
        hm.insert("request_uuid".to_string(), request_uuid.clone());
        hm.insert("transaction_hash".to_string(), hex.clone());
        hm.insert("transaction_time".to_string(), time.to_string());
        hm.insert("current_time".to_string(), current_time.to_string());
        hm.insert("input_address".to_string(), input_address.clone());
        hm.insert("output_address".to_string(), output_address.clone());
        hm.insert("node_id".to_string(), node_id.clone());

        let res = task_local_map(hm, async move {
            self.transaction(
                transaction_message, request_uuid, hex,
                time, current_time, input_address, output_address,
                node_id
            ).await
        }).await;
        res
    }

    /*
    let span = Span::current();
    span.record("extra_field", &"extra_value");
     */

    #[allow(unused_variables)]
    #[tracing::instrument(skip(self, transaction_message, transaction_time, current_time, input_address, output_address))]
    async fn transaction(
        &mut self,
        transaction_message: TransactionMessage,
        request_uuid: String,
        transaction_hash: String,
        transaction_time: i64,
        current_time: i64,
        input_address: String,
        output_address: String,
        node_id: String
    ) -> Result<(), ErrorInfo> {

        if self.check_peer_message(&transaction_message.transaction).await? {
            return Ok(());
        }

        let result_or_error = {
            match self.immediate_validation(&transaction_message.transaction).await {
                Ok(_) => {
                    let res = self.process(&transaction_message.transaction.clone(), current_time, request_uuid).await;
                    self.cleanup(None)?;
                    res
                }
                Err(e) => {Err(e)}
            }
        };

        // Use this as whether or not the request was successful
        let mut metadata = ResponseMetadata::default();
        // Change these to raw Response instead of public response
        let mut pr = structs::Response::default();
        match result_or_error {
            Ok(o) => {
                metadata.success = true;
                pr.submit_transaction_response = Some(o);
            }
            Err(ee) => {
                metadata.success = false;
                metadata.error_info = Some(ee);
            }
        }
        pr.response_metadata = Some(metadata);

        match transaction_message.response_channel {
            None => {
                // increment_counter!("redgold.transaction.missing_response_channel");
                // let details = ErrorInfo::error_info("Missing response channel for transaction");
                // error!("Missing response channel for transaction {:?}", json_or(&details));
            }
            Some(r) => if let Some(e) = r.send_err(pr).err() {
                error!("Error sending transaction response to channel {}", json_or(&e));
            }
        };
        Ok(())
    }

    // TODO: okay so in this loop below thre's a time which should also check if all
    // available peers have heard about a double spend etc. and potentially terminate quicker?
    // maybe? or not hell not really necessary.

    fn cleanup(&mut self, ii: Option<usize>) -> Result<(), ErrorInfo> {
        if let Some(request_processor) = &self.request_processor {
            if let Some(utxo_ids) = &self.utxo_ids {
                self.clean_utxo(&request_processor, utxo_ids, ii);
                self.relay.transaction_channels.remove(self.transaction_hash.safe_get()?);
            }
        }
        Ok(())
    }

    async fn observe(&self, validation_type: ValidationType, state: State) -> Result<ObservationProof, ErrorInfo> {
        let mut hash: Hash = self.transaction_hash.safe_get()?.clone();
        hash.hash_type = HashType::Transaction as i32;
        let mut om = structs::ObservationMetadata::default();
        om.observed_hash = Some(hash);
        om.state = Some(state as i32);
        om.struct_metadata = struct_metadata_new();
        om.observation_type = validation_type as i32;
        // TODO: It might be nice to grab the proof of a signature here?
        self.relay.observe(om).await
    }

    async fn immediate_validation(&mut self, transaction: &Transaction) -> Result<(), ErrorInfo> {

        // Check if we already have a rejection reason for this transaction and abort if so
        // returning the previous rejection reason.
        let ds = self.relay.ds.clone();
        if let Some((_, Some(pre_rejection))) = ds.transaction_store.query_maybe_transaction(&transaction.hash_or()).await? {
            return Err(pre_rejection);
        }

        // Validate obvious schema related errors / local errors requiring no other context information
        transaction.prevalidate()?;
        Ok(())

    }

    // TODO: Add a debug info thing here? to include data about debug calls? Thread local info? something ?
    async fn process(&mut self, transaction: &Transaction, processing_time_start: i64, request_uuid: String) -> Result<SubmitTransactionResponse, ErrorInfo> {
        increment_counter!("redgold.transaction.received");
        let hash = transaction.hash_or();
        self.transaction_hash = Some(hash.clone());

        // Establish channels for other transaction threads to communicate conflicts with this one.
        let request_processor = self.create_receiver_or_err(&hash, request_uuid, &transaction)?;

        // Attempt to resolve all the transaction inputs and outputs for context-aware validation
        // This is the place where balances checks and signature verifications are performed.
        let resolver_data = resolve_transaction(&transaction,
                                                self.relay.clone(),
                                                // self.tx_process.clone()
        ).await?;
        resolver_data.validate()?;

        let fixed_utxo_ids = transaction.fixed_utxo_ids_of_inputs()?;
        self.utxo_ids = Some(fixed_utxo_ids.clone());
        // TODO: Check for conflicts via peer query -- currently unimplemented
        check_utxo_conflicts(self.relay.clone(), &fixed_utxo_ids, &hash).await?;

        let mut conflict_detected = false;
        let self_conflict = Conflict {
            transaction_hash: hash.clone(),
            abort: false,
            processing_start_time: processing_time_start.clone(),
            request_processer: request_processor.clone(),
        };
        let mut conflicts: Vec<Conflict> = vec![];

        // Change to UTXO stream processor sink message?
        // TODO: Change this so the UTXO pool is responsible for this, remove the request processor from here
        // And create a spawned thread for each UTXO pool to handle this that returns any conflicts back here.
        for utxo_id in fixed_utxo_ids {
            // Acquire locks to determine if another transaction is re-using some output
            let entry = self.relay.utxo_channels.entry(utxo_id.clone());
            match entry {
                Entry::Occupied(mut entry) => {
                    conflict_detected = true;
                    // issue! another pending transaction is using this
                    // this indicates a double spend
                    //receiver.
                    for active_request in entry.get().active_requests.iter() {
                        // TODO: Handle unwrap issues
                        conflicts.push(active_request.clone());
                        info!(
                            "Conflict between current: {} and {} on utxo_id {} output: {}",
                            hash.hex(), active_request.transaction_hash.hex(),
                            utxo_id.clone().transaction_hash.expect("h").hex(),
                            utxo_id.output_index.clone()
                        );
                        // Need to capture this conflict LOCALLY here for use later.
                        active_request
                            .request_processer
                            .sender
                            .send_err(self_conflict.clone())?
                    }
                    entry.get_mut().active_requests.push(self_conflict.clone());
                }
                Entry::Vacant(entry) => {
                    entry.insert(UTXOContentionPool {
                        active_requests: vec![self_conflict.clone()],
                    });
                    // Re-validate after acquiring lock. Alternative?
                    // TODO: remove this clone
                }
            };
        }

        // TODO: Don't remember the purpose of this duplicate validation here, we should really
        // seed this with existing data to reduce request size and just verify it's still valid
        let resolver_data = resolve_transaction(&transaction,
                                                self.relay.clone(),
                                                // self.tx_process.clone()
        ).await?;
        resolver_data.validate()?;
        // Change this to 'revalidate' and only issue some queries again not all of them.

        let validation_type = if resolver_data.resolved_internally {
            ValidationType::Full
        } else {
            ValidationType::Partial
        };

        let mut observation_proofs = HashSet::new();
        let mut self_signed_pending = false;

        if !conflict_detected {
            tracing::info!("Signing pending transaction");
            let prf = self.observe(validation_type, State::Pending).await?;
            self_signed_pending = true;
            observation_proofs.insert(prf);
            // TODO: Await the initial observations here, gossip about them as well?
        } else {
            tracing::info!("Conflict detected on current {}", hash.hex());
        }

        // tracing::info!("Pending transaction stage started");

        // We need to distinguish between a pending and a conflict detected here.
        // TODO: Broadcast with no concern about response, or do we check for conflicts here?
        // TODO: we really need here a mechanism to deal with incoming conflict notifications
        // A request type to handle that that'll alert this thread? OR should that just be the same
        // As transaction request? We might benefit from including additional information.
        // TODO: Replace all this with a relay method.
        self.relay.gossip(transaction).await?;

        // tracing::info!("Gossiped transaction");

        let elapsed_time = || {
            current_time_millis_i64() - processing_time_start
        };

        // This thread should listen for any incoming messages that alert to conflicts
        // And or any messages that are related to proofs for this transaction.


        // conflicts.push(watch2.unwrap());
        // let mut conflicts = vec![watch2.unwrap()];
        // Potentially receive abort code here to terminate thread early.
        // if another transaction was finalized.

        // TODO: This should really be a select between polling peers to find out if we're finished
        // And the maximum elapsed time before acceptance.
        // I.e. poll early acceptance, for now that doesn't really matter as much
        // What we should really do is notify the request processor that there is new data available
        // I.e. if some data comes in indicative of a proof or whatever, we should recv a message here
        // and process it, by adding up the observation proofs and checking if we're done.
        while elapsed_time() < (self.relay.node_config.transaction_finalization_time.as_millis() as i64) {
            let res = tokio::time::timeout(
                Duration::from_secs(1), request_processor.receiver.recv_async_err())
                .await.ok();
            if let Some(o) = res {
                let conflict = o?;
                if conflict.abort {
                    // translate error codes for db access / etc. into internal server error.
                    // TODO: This error code just indicates a conflict, not necessarily a deliberate double spend
                    // LiveConflictDetected to distinguish it.
                    return Err(error_message(Error::TransactionRejectedDoubleSpend, "Double spend detected in live context"));
                }
                conflicts.push(conflict);
            }
        }
        // TODO: we shouldn't be attempting to deal with this thread as a conflict
        /*
        Rather, what should be happening is, if we see a conflict, then we should allow that
        conflict to essentially 'take over' this thread and the other transaction threads
        and then resolve itself in ONE place, and then respond to all the response channels?
        Yeah that.
         */

        // let this_tx_trust = self.mem_pool.total_trust(&hash, &self.ds).unwrap_or(0f64);

        // TODO: Return all conflicts if not chosen as part of submit response
        // And/or return all conflicts anyways as part of the response.

        // TODO: Move to own function
        // if let Some(value) = Self::poll_internal_proof_messages(&request_processor, &mut observation_proofs) {
        //     value?;
        // }

        // Completion stage

        tracing::info!("Conflict resolution stage started with {:?} conflicts", conflicts.len());

        if !conflicts.is_empty() {

            let this_as_conflict = Conflict {
                transaction_hash: hash.clone(),
                abort: true,
                processing_start_time: processing_time_start.clone(),
                request_processer: request_processor.clone(),
            };
            let mut all_conflicts = conflicts.clone();
            all_conflicts.push(this_as_conflict.clone());

            let winner = resolve_conflict(self.relay.clone(), all_conflicts)
                .await?;

            if winner != hash {
                Err(error_message(Error::TransactionRejectedDoubleSpend,
                                  format!("Lost conflict to other transaction winner: {}", winner.hex())))?;
            } else {
                for c in &conflicts {
                    c.request_processer
                        .sender
                        .send_err(this_as_conflict.clone())?;
                }
            }
        }

        // tracing::info!("Finalizing transaction stage started");

        let prf = self.observe(validation_type, State::Finalized).await?;
        observation_proofs.insert(prf);
        self.insert_transaction(&transaction).await?;
        increment_counter!("redgold.transaction.accepted");
        tracing::info!("Accepted transaction");

        // tracing::info!("Finalize end on current {} with num conflicts {:?}", hash.hex(), conflicts.len());

        // Await until it has appeared in an observation and other nodes observations.


        tokio::time::sleep(Duration::from_secs(6)).await;

        let stored_proofs = self.relay.ds.observation.select_observation_edge(&hash).await?;

        tracing::info!("Found {:?} stored proofs in ds", stored_proofs.len());

        observation_proofs.extend(stored_proofs);
        // TODO: Query our internal datastore for all obs proofs, and extend based on that

        // TODO: periodic process to clean mempool in event of thread processing crash?
        let peers = self.relay.ds.peer_store.active_nodes(None).await?;
        let mut obs_proof_req = Request::default();
        obs_proof_req.query_observation_proof_request = Some(QueryObservationProofRequest {
            hash: Some(hash.clone())
        });

        if !peers.is_empty() {
            tracing::info!("Collecting observation proofs from {} peers", peers.len());
            let results = Relay::broadcast(self.relay.clone(),
                                           peers, obs_proof_req,
                                           // self.tx_process.clone(),
                                           Some(
                                               Duration::from_secs(5))).await;
            for (pk, r) in results {
                match r {
                    Ok(r) => {
                        let num_proofs = r.clone().query_observation_proof_response.map(|o| o.observation_proof.len()).unwrap_or(0);
                        tracing::info!("Received {:?} observation proofs from peer: {}", num_proofs,  pk.short_id());
                        if let Some(obs_proof) = r.query_observation_proof_response {
                            observation_proofs.extend(obs_proof.observation_proof);
                        }
                    }
                    Err(e) => {
                        tracing::error!("Error collecting observation proofs from peer: {} error: {}", pk.short_id(), e.json_or())
                    }
                }
            }
        } else {
            tracing::info!("No peers to collect observation proofs from");
        }

        observation_proofs.extend(self.relay.ds.observation.select_observation_edge(&hash).await?);

        let mut submit_response = SubmitTransactionResponse::default();
        let mut query_transaction_response = QueryTransactionResponse::default();
        query_transaction_response.observation_proofs = observation_proofs.iter().map(|o| o.clone()).collect_vec();
        submit_response.query_transaction_response = Some(query_transaction_response);
        submit_response.transaction = Some(transaction.clone());
        submit_response.transaction_hash = Some(hash.clone());

        // TODO: Task local metrics update here
        // let hm: HashMap<String, String> = HashMap::new();
        // hm
        let counts = submit_response.count_unique_by_state()?;

        tracing::info!("Finished processing transaction \
        num_observation_proofs {} self_signed_pending {:?} num_pending: {:?} num_accepted: {:?}",
            observation_proofs.len(), self_signed_pending, counts.get(&(State::Pending as i32)).unwrap_or(&0),
            counts.get(&(State::Finalized as i32)).unwrap_or(&0),
        );
        // TODO: Rename Finalized to Accepted

        Ok(submit_response)
    }

    // TODO: WArning this is actually wrong and will break everything, fix the done condition
    fn poll_internal_proof_messages(request_processor: &RequestProcessor, observation_proofs: &mut HashSet<ObservationProof>) -> Option<Result<SubmitTransactionResponse, ErrorInfo>> {
        while {
            let err = request_processor.internal_channel.receiver.try_recv();
            let mut done = true;
            match err {
                Ok(o) => {
                    match o {
                        ProcessTransactionMessage::ProofReceived(o) => {
                            observation_proofs.insert(o);
                        }
                    }
                }
                Err(e) => {
                    match e {
                        TryRecvError::Empty => {
                            done = true;
                        }
                        TryRecvError::Disconnected => {
                            return Some(Err(error_info("request processor channel closed unexpectedly")));
                        }
                    }
                }
            }
            done
        } {}
        None
    }

    async fn insert_transaction(
        &self,
        transaction: &Transaction,
    ) -> Result<(), ErrorInfo> {
        // self.observation_buffer.push();
        // Commit transaction internally to database.

        // TODO: Handle these errors properly.
        // Add loggers, do as a single commit with a rollback.
        // Preserve all old data / inputs while committing new transaction, or do retries?
        // or fail the entire node?
        self.relay
                .ds
                .transaction_store
                .insert_transaction(
                    &transaction, util::current_time_millis_i64(), true, None
                ).await?;

        for fixed in transaction.fixed_utxo_ids_of_inputs()? {
            // This should maybe just put the whole node into a corrupted state? Or a retry?
            tracing::info!("Attempting to delete utxo: {}", fixed.format_str());
            self.relay.ds.transaction_store.delete_utxo(&fixed).await?;
            let utxo_valid = self.relay.ds.transaction_store.query_utxo_id_valid(
                &fixed.transaction_hash.safe_get()?.clone(), fixed.output_index
            ).await?;
            let deleted = !utxo_valid;
            tracing::info!("Utxo should be deleted: {}", deleted);
        }

        return Ok(());
    }

    fn clean_utxo(
        &self,
        request_processor: &RequestProcessor,
        utxo_ids: &Vec<FixedUtxoId>,
        i: Option<usize>,
    ) {
        // Cleanup all existing locks.
        // TODO: How to slice utxo_ids to 0..i ?
        let i = i.unwrap_or(utxo_ids.len());
        for utxo_id_cleanup in &utxo_ids[0..i] {
            let cleanup = self.relay.utxo_channels.get_mut(utxo_id_cleanup);
            match cleanup {
                None => {}
                Some(mut pool) => {
                    let idx = pool
                        .active_requests
                        .iter()
                        .enumerate()
                        .find(|(_, req)| {
                            req.request_processer.request_id == request_processor.request_id
                        })
                        .map(|x| x.0);
                    match idx {
                        None => {}
                        Some(idx_actual) => {
                            pool.active_requests.remove(idx_actual);
                        }
                    }
                }
            }
        }
    }

    fn create_receiver_or_err(
        &self,
        transaction_hash: &Hash,
        request_uuid: String,
        transaction: &Transaction
    ) -> Result<RequestProcessor, ErrorInfo> {
        let entry = self
            .relay
            .transaction_channels
            .entry(transaction_hash.clone());
        let res = match entry {
            Entry::Occupied(_) => Err(error_message(Error::TransactionAlreadyProcessing, "Duplicate TX hash found in processing queue")),
            Entry::Vacant(entry) => {
                let req = RequestProcessor::new(&transaction_hash, request_uuid, transaction.clone());
                entry.insert(req.clone());
                Ok(req)
            }
        };
        res
    }
    async fn check_peer_message(&self, p0: &Transaction) -> Result<bool, ErrorInfo> {
        let is_peer_tx = p0.node_metadata().is_ok();
        if is_peer_tx {
            // Ignore for now, causes a giant loop think we need to avoid gossiping about self?
            // self.relay.add_peer_flow(p0).await.log_error().ok();
        }
        Ok(is_peer_tx)
    }
}

// TODO: really just make this a full proper wallet keeping track of all this info.
// all these are broke ?
//
// #[test]
// fn test_processing() {
//     util::init_logger();
//     let ttc = TransactionTestContext::default();
//     let ds = ttc.relay.ds.clone();
//     info!("Data store path in outer context {}", ds.connection_path);
//     let utxos = ds.query_utxo_all_debug().unwrap();
//     info!(
//         "Data store outside genesis query {}",
//         serde_json::to_string(&utxos.clone()).unwrap()
//     );
//     for u in utxos {
//         info!(
//             "{:?}, {}",
//             rounded_balance(u.amount()),
//             hex::encode(u.address)
//         )
//     }
//     info!("{:?}", ds.query_all_balance().unwrap());
//     info!(
//         "ttc.t transaction ser: {}",
//         serde_json::to_string(&ttc.t.clone()).unwrap()
//     );
//     info!("{:?}", ttc.t.clone().output_amounts());
//     info!(
//         "genesis hash hex {:?}",
//         create_genesis_transaction().hash_hex()
//     );
//
//     let inp = ttc.t.iter_utxo_inputs().clone().get(0).unwrap().clone().0;
//     info!("Inputs of transaction: {}", hex::encode(inp.clone()));
//
//     info!("Query utxo of above: {:?}", ds.query_utxo(&inp, 0));
//
//     ttc.t.clone().validate_currency_utxo(&ds.clone()).unwrap();
//
//     let submitter = build_runtime(1, "test_tx_gen");
//
//     let tpc = TransactionProcessContext {
//         relay: ttc.relay.clone(),
//         tx_process: build_runtime(1, "test_tx_proc"),
//     };
//
//     let res = submitter.block_on(tpc.process(&ttc.t.clone()));
//     if res.is_err() {
//         info!("Error encountered {:?}", res.err());
//     }
//     assert!(res.is_ok());
//     for (hash, i) in ttc.t.iter_utxo_outputs() {
//         let res = tpc.relay.ds.query_utxo(&hash, i).unwrap();
//         assert!(res.is_some());
//     }
//     info!("{:?}", ds.query_all_balance().unwrap());
//     let utxos2 = ds.query_utxo_all_debug().unwrap();
//     info!(
//         "Data store outside genesis query {}",
//         serde_json::to_string(&utxos2.clone()).unwrap()
//     );
//     for u in utxos2.clone() {
//         info!(
//             "{:?}, {}",
//             rounded_balance(u.amount()),
//             hex::encode(u.address)
//         )
//     }
//     assert_eq!(utxos2.len(), 2);
// }
//
// #[test]
// fn test_duplicate_tx_hash() {
//     util::init_logger();
//     let ttc = TransactionTestContext::default();
//     let ds = ttc.relay.ds.clone();
//
//     let submitter = build_runtime(10, "test_tx_gen");
//
//     let tpc = TransactionProcessContext {
//         relay: ttc.relay.clone(),
//         tx_process: build_runtime(10, "test_tx_proc"),
//     };
//
//     let res = submitter.spawn({
//         let context = tpc.clone();
//         let transaction = ttc.t.clone();
//         async move { context.process(&transaction).await }
//     });
//     let res2 = submitter.spawn({
//         let context = tpc.clone();
//         let transaction = ttc.t.clone();
//         async move { context.process(&transaction).await }
//     });
//     let r1 = submitter.block_on(res).unwrap();
//     let r2 = submitter.block_on(res2).unwrap();
//
//     info!("{:?}", r1);
//     info!("{:?}", r2);
//
//     let results = vec![r1, r2];
//     assert!(results.iter().find(|x| x.is_err()).is_some());
//     assert!(results.iter().find(|x| x.is_ok()).is_some());
// }
//
// // Re-enable later
// #[ignore]
// #[test]
// fn test_double_spend() {
//     util::init_logger().ok();
//     let submitter = build_runtime(10, "test_tx_gen");
//     let mut ttc = submitter.block_on(TransactionTestContext::default());
//     let ds = ttc.relay.ds.clone();
//     info!("Addr balance: {:?}", ds.query_balance(&ttc.tc.addr.clone()));
//
//     info!("{:?}", ds.query_all_balance());
//
//     let tpc = TransactionProcessContext {
//         relay: ttc.relay.clone(),
//         tx_process: build_runtime(10, "test_tx_proc"),
//     };
//
//     let (dbl1, dbl2) = ttc.tx_gen.generate_double_spend_tx();
//
//     let res = submitter.spawn({
//         let context = tpc.clone();
//         let transaction = dbl1.clone().transaction;
//         async move { context.process(&transaction).await }
//     });
//     let res2 = submitter.spawn({
//         let context = tpc.clone();
//         let transaction = dbl2.clone().transaction;
//         async move { context.process(&transaction).await }
//     });
//     let r1 = submitter.block_on(res).unwrap();
//     let r2 = submitter.block_on(res2).unwrap();
//
//     info!("{:?}", r1);
//     info!("{:?}", r2);
//     info!("{:?}", ds.query_all_balance());
//
//     let results = vec![r1, r2];
//     assert!(results.iter().find(|x| x.is_err()).is_some());
//     assert!(results.iter().find(|x| x.is_ok()).is_some());
// }

// https://docs.rs/crossbeam-channel/0.5.1/crossbeam_channel/index.html
// select timeout after duration
// try iter
// try non blocking async