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(())
}
}