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
use std::sync::Arc;
use std::cmp;
use std::collections::{HashMap, HashSet};
use bytes::Bytes;
use vapcore_miner::pool;
use vapory_types::{H256, U256, Address};
use tetsy_util_mem::MallocSizeOfExt;
use crypto::publickey::Signature;
use crate::messages::PrivateTransaction;
use parking_lot::RwLock;
use types::transaction::{UnverifiedTransaction, SignedTransaction};
use txpool;
use txpool::{VerifiedTransaction, Verifier};
use crate::error::Error;
type Pool = txpool::Pool<VerifiedPrivateTransaction, pool::scoring::NonceAndGasPrice>;
const MAX_QUEUE_LEN: usize = 8312;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct VerifiedPrivateTransaction {
pub private_transaction: PrivateTransaction,
pub validator_account: Option<Address>,
pub transaction: SignedTransaction,
pub transaction_hash: H256,
pub transaction_sender: Address,
}
impl txpool::VerifiedTransaction for VerifiedPrivateTransaction {
type Hash = H256;
type Sender = Address;
fn hash(&self) -> &H256 {
&self.transaction_hash
}
fn mem_usage(&self) -> usize {
self.transaction.malloc_size_of()
}
fn sender(&self) -> &Address {
&self.transaction_sender
}
}
impl pool::ScoredTransaction for VerifiedPrivateTransaction {
fn priority(&self) -> pool::Priority {
pool::Priority::Regular
}
fn gas_price(&self) -> &U256 {
&self.transaction.gas_price
}
fn nonce(&self) -> U256 {
self.transaction.nonce
}
}
#[derive(Debug)]
pub struct PrivateReadyState<C> {
senders: HashSet<Address>,
state: C,
}
impl<C> PrivateReadyState<C> {
pub fn new(
state: C,
) -> Self {
PrivateReadyState {
senders: Default::default(),
state,
}
}
}
impl<C: pool::client::NonceClient> txpool::Ready<VerifiedPrivateTransaction> for PrivateReadyState<C> {
fn is_ready(&mut self, tx: &VerifiedPrivateTransaction) -> txpool::Readiness {
let sender = tx.sender();
let state = &self.state;
let state_nonce = state.account_nonce(sender);
if self.senders.contains(sender) {
txpool::Readiness::Future
} else {
self.senders.insert(*sender);
match tx.transaction.nonce.cmp(&state_nonce) {
cmp::Ordering::Greater => txpool::Readiness::Future,
cmp::Ordering::Less => txpool::Readiness::Stale,
cmp::Ordering::Equal => txpool::Readiness::Ready,
}
}
}
}
pub struct VerificationStore {
verification_pool: RwLock<Pool>,
verification_options: pool::verifier::Options,
}
impl Default for VerificationStore {
fn default() -> Self {
VerificationStore {
verification_pool: RwLock::new(
txpool::Pool::new(
txpool::NoopListener,
pool::scoring::NonceAndGasPrice(pool::PrioritizationStrategy::GasPriceOnly),
pool::Options {
max_count: MAX_QUEUE_LEN,
max_per_sender: MAX_QUEUE_LEN / 10,
max_mem_usage: 8 * 1024 * 1024,
},
)
),
verification_options: pool::verifier::Options {
minimal_gas_price: 0.into(),
block_gas_limit: 8_000_000.into(),
tx_gas_limit: U256::max_value(),
no_early_reject: false,
},
}
}
}
impl VerificationStore {
pub fn add_transaction<C: pool::client::Client + pool::client::NonceClient + Clone>(
&self,
transaction: UnverifiedTransaction,
validator_account: Option<Address>,
private_transaction: PrivateTransaction,
client: C,
) -> Result<(), Error> {
let options = self.verification_options.clone();
let verifier = pool::verifier::Verifier::new(client.clone(), options, Default::default(), None);
let unverified = pool::verifier::Transaction::Unverified(transaction);
let verified_tx = verifier.verify_transaction(unverified)?;
let signed_tx: SignedTransaction = verified_tx.signed().clone();
let signed_hash = signed_tx.hash();
let signed_sender = signed_tx.sender();
let verified = VerifiedPrivateTransaction {
private_transaction,
validator_account,
transaction: signed_tx,
transaction_hash: signed_hash,
transaction_sender: signed_sender,
};
let replace = pool::replace::ReplaceByScoreAndReadiness::new(
self.verification_pool.read().scoring().clone(), client);
self.verification_pool.write().import(verified, &replace)?;
Ok(())
}
pub fn drain<C: pool::client::NonceClient>(&self, client: C) -> Vec<Arc<VerifiedPrivateTransaction>> {
let ready = PrivateReadyState::new(client);
let transactions: Vec<_> = self.verification_pool.read().pending(ready).collect();
let mut pool = self.verification_pool.write();
for tx in &transactions {
pool.remove(tx.hash(), true);
}
transactions
}
}
#[derive(Debug, Clone)]
pub struct PrivateTransactionSigningDesc {
pub original_transaction: SignedTransaction,
pub validators: Vec<Address>,
pub received_signatures: Vec<Signature>,
pub state: Bytes,
pub contract_nonce: U256,
}
#[derive(Default)]
pub struct SigningStore {
transactions: HashMap<H256, PrivateTransactionSigningDesc>,
}
impl SigningStore {
pub fn add_transaction(
&mut self,
private_hash: H256,
transaction: SignedTransaction,
validators: &Vec<Address>,
state: Bytes,
contract_nonce: U256,
) -> Result<(), Error> {
if self.transactions.len() > MAX_QUEUE_LEN {
return Err(Error::QueueIsFull);
}
self.transactions.insert(private_hash, PrivateTransactionSigningDesc {
original_transaction: transaction.clone(),
validators: validators.clone(),
received_signatures: Vec::new(),
state,
contract_nonce,
});
Ok(())
}
pub fn get(&self, private_hash: &H256) -> Option<PrivateTransactionSigningDesc> {
self.transactions.get(private_hash).cloned()
}
pub fn remove(&mut self, private_hash: &H256) -> Result<(), Error> {
self.transactions.remove(private_hash);
Ok(())
}
pub fn add_signature(&mut self, private_hash: &H256, signature: Signature) -> Result<(), Error> {
let desc = self.transactions.get_mut(private_hash).ok_or_else(|| Error::PrivateTransactionNotFound)?;
if !desc.received_signatures.contains(&signature) {
desc.received_signatures.push(signature);
}
Ok(())
}
}