2018-06-04 10:19:50 +02:00
|
|
|
// Copyright 2015-2018 Parity Technologies (UK) Ltd.
|
2018-04-09 16:14:33 +02:00
|
|
|
// This file is part of Parity.
|
|
|
|
|
|
|
|
// Parity is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// Parity is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2018-04-13 17:34:27 +02:00
|
|
|
use std::sync::Arc;
|
|
|
|
use std::collections::{HashMap, HashSet};
|
|
|
|
|
2018-04-09 16:14:33 +02:00
|
|
|
use bytes::Bytes;
|
2018-04-13 17:34:27 +02:00
|
|
|
use ethcore_miner::pool;
|
2018-04-09 16:14:33 +02:00
|
|
|
use ethereum_types::{H256, U256, Address};
|
2018-04-13 17:34:27 +02:00
|
|
|
use ethkey::Signature;
|
2018-04-09 16:14:33 +02:00
|
|
|
use transaction::{UnverifiedTransaction, SignedTransaction};
|
2018-04-13 17:34:27 +02:00
|
|
|
|
2018-04-09 16:14:33 +02:00
|
|
|
use error::{Error, ErrorKind};
|
|
|
|
|
|
|
|
/// Maximum length for private transactions queues.
|
|
|
|
const MAX_QUEUE_LEN: usize = 8312;
|
|
|
|
|
|
|
|
/// Desriptor for private transaction stored in queue for verification
|
|
|
|
#[derive(Default, Debug, Clone, PartialEq, Eq)]
|
|
|
|
pub struct PrivateTransactionDesc {
|
|
|
|
/// Hash of the private transaction
|
|
|
|
pub private_hash: H256,
|
|
|
|
/// Contract's address used in private transaction
|
|
|
|
pub contract: Address,
|
|
|
|
/// Address that should be used for verification
|
|
|
|
pub validator_account: Address,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Storage for private transactions for verification
|
|
|
|
pub struct VerificationStore {
|
|
|
|
/// Descriptors for private transactions in queue for verification with key - hash of the original transaction
|
|
|
|
descriptors: HashMap<H256, PrivateTransactionDesc>,
|
|
|
|
/// Queue with transactions for verification
|
2018-04-13 17:34:27 +02:00
|
|
|
///
|
|
|
|
/// TODO [ToDr] Might actually be better to use `txpool` directly and:
|
|
|
|
/// 1. Store descriptors inside `VerifiedTransaction`
|
|
|
|
/// 2. Use custom `ready` implementation to only fetch one transaction per sender.
|
|
|
|
/// 3. Get rid of passing dummy `block_number` and `timestamp`
|
|
|
|
transactions: pool::TransactionQueue,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for VerificationStore {
|
|
|
|
fn default() -> Self {
|
|
|
|
VerificationStore {
|
|
|
|
descriptors: Default::default(),
|
|
|
|
transactions: pool::TransactionQueue::new(
|
|
|
|
pool::Options {
|
|
|
|
max_count: MAX_QUEUE_LEN,
|
|
|
|
max_per_sender: MAX_QUEUE_LEN / 10,
|
|
|
|
max_mem_usage: 8 * 1024 * 1024,
|
|
|
|
},
|
|
|
|
pool::verifier::Options {
|
|
|
|
// TODO [ToDr] This should probably be based on some real values?
|
|
|
|
minimal_gas_price: 0.into(),
|
|
|
|
block_gas_limit: 8_000_000.into(),
|
|
|
|
tx_gas_limit: U256::max_value(),
|
2018-07-24 15:04:48 +02:00
|
|
|
no_early_reject: false
|
2018-04-13 17:34:27 +02:00
|
|
|
},
|
|
|
|
pool::PrioritizationStrategy::GasPriceOnly,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2018-04-09 16:14:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl VerificationStore {
|
|
|
|
/// Adds private transaction for verification into the store
|
2018-04-13 17:34:27 +02:00
|
|
|
pub fn add_transaction<C: pool::client::Client>(
|
2018-04-09 16:14:33 +02:00
|
|
|
&mut self,
|
|
|
|
transaction: UnverifiedTransaction,
|
|
|
|
contract: Address,
|
|
|
|
validator_account: Address,
|
|
|
|
private_hash: H256,
|
2018-04-13 17:34:27 +02:00
|
|
|
client: C,
|
2018-04-09 16:14:33 +02:00
|
|
|
) -> Result<(), Error> {
|
|
|
|
if self.descriptors.len() > MAX_QUEUE_LEN {
|
|
|
|
bail!(ErrorKind::QueueIsFull);
|
|
|
|
}
|
|
|
|
|
2018-04-13 17:34:27 +02:00
|
|
|
let transaction_hash = transaction.hash();
|
|
|
|
if self.descriptors.get(&transaction_hash).is_some() {
|
2018-04-09 16:14:33 +02:00
|
|
|
bail!(ErrorKind::PrivateTransactionAlreadyImported);
|
|
|
|
}
|
2018-04-13 17:34:27 +02:00
|
|
|
|
|
|
|
let results = self.transactions.import(
|
|
|
|
client,
|
|
|
|
vec![pool::verifier::Transaction::Unverified(transaction)],
|
|
|
|
);
|
|
|
|
|
|
|
|
// Verify that transaction was imported
|
|
|
|
results.into_iter()
|
|
|
|
.next()
|
|
|
|
.expect("One transaction inserted; one result returned; qed")?;
|
|
|
|
|
|
|
|
self.descriptors.insert(transaction_hash, PrivateTransactionDesc {
|
|
|
|
private_hash,
|
|
|
|
contract,
|
|
|
|
validator_account,
|
|
|
|
});
|
|
|
|
|
|
|
|
Ok(())
|
2018-04-09 16:14:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns transactions ready for verification
|
|
|
|
/// Returns only one transaction per sender because several cannot be verified in a row without verification from other peers
|
2018-04-13 17:34:27 +02:00
|
|
|
pub fn ready_transactions<C: pool::client::NonceClient>(&self, client: C) -> Vec<Arc<pool::VerifiedTransaction>> {
|
|
|
|
// We never store PendingTransactions and we don't use internal cache,
|
|
|
|
// so we don't need to provide real block number of timestamp here
|
|
|
|
let block_number = 0;
|
|
|
|
let timestamp = 0;
|
|
|
|
let nonce_cap = None;
|
|
|
|
|
|
|
|
self.transactions.collect_pending(client, block_number, timestamp, nonce_cap, |transactions| {
|
|
|
|
// take only one transaction per sender
|
|
|
|
let mut senders = HashSet::with_capacity(self.descriptors.len());
|
|
|
|
transactions.filter(move |tx| senders.insert(tx.signed().sender())).collect()
|
|
|
|
})
|
2018-04-09 16:14:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns descriptor of the corresponding private transaction
|
|
|
|
pub fn private_transaction_descriptor(&self, transaction_hash: &H256) -> Result<&PrivateTransactionDesc, Error> {
|
|
|
|
self.descriptors.get(transaction_hash).ok_or(ErrorKind::PrivateTransactionNotFound.into())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Remove transaction from the queue for verification
|
2018-04-13 17:34:27 +02:00
|
|
|
pub fn remove_private_transaction(&mut self, transaction_hash: &H256) {
|
2018-04-09 16:14:33 +02:00
|
|
|
self.descriptors.remove(transaction_hash);
|
2018-04-13 17:34:27 +02:00
|
|
|
self.transactions.remove(&[*transaction_hash], true);
|
2018-04-09 16:14:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Desriptor for private transaction stored in queue for signing
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct PrivateTransactionSigningDesc {
|
|
|
|
/// Original unsigned transaction
|
|
|
|
pub original_transaction: SignedTransaction,
|
|
|
|
/// Supposed validators from the contract
|
|
|
|
pub validators: Vec<Address>,
|
|
|
|
/// Already obtained signatures
|
|
|
|
pub received_signatures: Vec<Signature>,
|
|
|
|
/// State after transaction execution to compare further with received from validators
|
|
|
|
pub state: Bytes,
|
|
|
|
/// Build-in nonce of the contract
|
|
|
|
pub contract_nonce: U256,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Storage for private transactions for signing
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct SigningStore {
|
|
|
|
/// Transactions and descriptors for signing
|
|
|
|
transactions: HashMap<H256, PrivateTransactionSigningDesc>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SigningStore {
|
|
|
|
/// Adds new private transaction into the store for signing
|
|
|
|
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 {
|
|
|
|
bail!(ErrorKind::QueueIsFull);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.transactions.insert(private_hash, PrivateTransactionSigningDesc {
|
|
|
|
original_transaction: transaction.clone(),
|
|
|
|
validators: validators.clone(),
|
|
|
|
received_signatures: Vec::new(),
|
|
|
|
state,
|
|
|
|
contract_nonce,
|
|
|
|
});
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get copy of private transaction's description from the storage
|
|
|
|
pub fn get(&self, private_hash: &H256) -> Option<PrivateTransactionSigningDesc> {
|
|
|
|
self.transactions.get(private_hash).cloned()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Removes desc from the store (after verification is completed)
|
|
|
|
pub fn remove(&mut self, private_hash: &H256) -> Result<(), Error> {
|
|
|
|
self.transactions.remove(private_hash);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Adds received signature for the stored private transaction
|
|
|
|
pub fn add_signature(&mut self, private_hash: &H256, signature: Signature) -> Result<(), Error> {
|
|
|
|
let desc = self.transactions.get_mut(private_hash).ok_or_else(|| ErrorKind::PrivateTransactionNotFound)?;
|
|
|
|
if !desc.received_signatures.contains(&signature) {
|
|
|
|
desc.received_signatures.push(signature);
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|