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
use std::{convert::TryInto, sync::Arc};
use rand::{thread_rng, Rng};
use snarkos_metrics::wrapped_mpsc;
use snarkos_storage::{Address, Digest, DynStorage, SerialBlock, SerialRecord, SerialTransaction, VMRecord};
use snarkvm_algorithms::CRH;
use snarkvm_dpc::{
testnet1::{
instantiated::{Components, Testnet1DPC},
payload::Payload,
Record as DPCRecord,
},
Account,
AccountScheme,
AleoAmount,
DPCComponents,
ProgramScheme,
};
use snarkvm_utilities::{to_bytes_le, ToBytes};
use tokio::sync::oneshot;
use crate::{error::ConsensusError, ConsensusParameters, DynLedger, MemoryPool};
use anyhow::*;
mod inner;
pub use inner::ConsensusInner;
mod message;
pub use message::*;
mod utility;
#[derive(Clone)]
pub struct Consensus {
pub parameters: ConsensusParameters,
pub dpc: Arc<Testnet1DPC>,
pub storage: DynStorage,
genesis_block: SerialBlock,
sender: wrapped_mpsc::Sender<ConsensusMessageWrapped>,
}
impl Consensus {
pub fn new(
parameters: ConsensusParameters,
dpc: Arc<Testnet1DPC>,
genesis_block: SerialBlock,
ledger: DynLedger,
storage: DynStorage,
memory_pool: MemoryPool,
) -> Arc<Self> {
let (sender, receiver) = wrapped_mpsc::channel(snarkos_metrics::queues::CONSENSUS, 256);
let created = Arc::new(Self {
parameters,
dpc,
storage: storage.clone(),
genesis_block,
sender,
});
let created2 = created.clone();
tokio::spawn(async move {
ConsensusInner {
public: created2,
ledger,
storage,
memory_pool,
}
.agent(receiver)
.await;
});
created
}
#[allow(clippy::ok_expect)]
async fn send<T: Send + Sync + 'static>(&self, message: ConsensusMessage) -> T {
let (sender, receiver) = oneshot::channel();
self.sender.send((message, sender)).await.ok();
*receiver
.await
.ok()
.expect("consensus agent missing")
.downcast()
.expect("type mismatch for consensus agent handle")
}
pub async fn receive_transaction(&self, transaction: SerialTransaction) -> bool {
self.send(ConsensusMessage::ReceiveTransaction(Box::new(transaction)))
.await
}
pub async fn verify_transactions(&self, transactions: Vec<SerialTransaction>) -> bool {
self.send(ConsensusMessage::VerifyTransactions(transactions)).await
}
pub async fn receive_block(&self, block: SerialBlock) -> bool {
self.send(ConsensusMessage::ReceiveBlock(Box::new(block))).await
}
pub async fn fetch_memory_pool(&self) -> Vec<SerialTransaction> {
self.send(ConsensusMessage::FetchMemoryPool(self.parameters.max_block_size))
.await
}
pub async fn create_transaction(
&self,
request: CreateTransactionRequest,
) -> Result<TransactionResponse, ConsensusError> {
self.send(ConsensusMessage::CreateTransaction(Box::new(request))).await
}
pub async fn create_partial_transaction(
&self,
request: CreatePartialTransactionRequest,
) -> Result<TransactionResponse, ConsensusError> {
self.send(ConsensusMessage::CreatePartialTransaction(request)).await
}
pub async fn force_decommit(&self, hash: Digest) -> Result<(), ConsensusError> {
self.send(ConsensusMessage::ForceDecommit(hash.0.to_vec())).await
}
pub async fn fast_forward(&self) -> Result<(), ConsensusError> {
self.send(ConsensusMessage::FastForward()).await
}
#[cfg(feature = "test")]
pub async fn reset(&self) -> Result<()> {
self.send(ConsensusMessage::Reset()).await
}
}