Skip to content

Commit

Permalink
Revert "Client TX limiting (#1657)"
Browse files Browse the repository at this point in the history
This reverts commit 07941ad.
  • Loading branch information
SkidanovAlex committed Nov 19, 2019
1 parent f10adcd commit a5c0d04
Show file tree
Hide file tree
Showing 17 changed files with 257 additions and 580 deletions.
8 changes: 6 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion chain/chain/Cargo.toml
Expand Up @@ -22,7 +22,7 @@ near-crypto = { path = "../../core/crypto" }
near-primitives = { path = "../../core/primitives" }
near-store = { path = "../../core/store" }
near-metrics = { path = "../../core/metrics" }
near-pool = { path = "../pool" }


[features]
# if enabled, we assert in most situations that are impossible unless some byzantine behavior is observed.
Expand Down
3 changes: 2 additions & 1 deletion chain/chain/src/lib.rs
Expand Up @@ -6,7 +6,8 @@ pub use error::{Error, ErrorKind};
pub use finality::{FinalityGadget, FinalityGadgetQuorums};
pub use store::{ChainStore, ChainStoreAccess, ChainStoreUpdate};
pub use types::{
Block, BlockHeader, BlockStatus, Provenance, ReceiptResult, RuntimeAdapter, Tip, Weight,
Block, BlockHeader, BlockStatus, Provenance, ReceiptResult, RuntimeAdapter, Tip,
ValidTransaction, Weight,
};

pub mod chain;
Expand Down
48 changes: 21 additions & 27 deletions chain/chain/src/test_utils.rs
Expand Up @@ -7,19 +7,10 @@ use borsh::{BorshDeserialize, BorshSerialize};
use chrono::Utc;
use log::debug;

use crate::error::{Error, ErrorKind};
use crate::store::ChainStoreAccess;
use crate::types::{
ApplyTransactionResult, BlockHeader, RuntimeAdapter, ValidatorSignatureVerificationResult,
Weight,
};
use crate::{Chain, ChainGenesis};
use near_crypto::{InMemorySigner, KeyType, PublicKey, SecretKey, Signature};
use near_pool::types::PoolIterator;
use near_primitives::account::Account;
use near_primitives::block::Approval;
use near_primitives::challenge::ChallengesResult;
use near_primitives::errors::InvalidTxError;
use near_primitives::errors::RuntimeError;
use near_primitives::hash::{hash, CryptoHash};
use near_primitives::receipt::{ActionReceipt, Receipt, ReceiptEnum};
use near_primitives::serialize::to_base;
Expand All @@ -38,6 +29,15 @@ use near_store::{
PartialStorage, Store, StoreUpdate, Trie, TrieChanges, WrappedTrieChanges, COL_BLOCK_HEADER,
};

use crate::error::{Error, ErrorKind};
use crate::store::ChainStoreAccess;
use crate::types::{
ApplyTransactionResult, BlockHeader, RuntimeAdapter, ValidatorSignatureVerificationResult,
Weight,
};
use crate::{Chain, ChainGenesis, ValidTransaction};
use near_primitives::block::Approval;

#[derive(BorshSerialize, BorshDeserialize, Hash, PartialEq, Eq, Ord, PartialOrd, Clone, Debug)]
struct AccountNonce(AccountId, Nonce);

Expand Down Expand Up @@ -433,33 +433,27 @@ impl RuntimeAdapter for KeyValueRuntime {
false
}

fn validate_tx(
fn filter_transactions(
&self,
_block_index: BlockIndex,
_block_index: u64,
_block_timestamp: u64,
_gas_price: Balance,
_state_update: StateRoot,
_transaction: &SignedTransaction,
) -> Result<Option<InvalidTxError>, Error> {
Ok(None)
_gas_limit: Gas,
_state_root: StateRoot,
transactions: Vec<SignedTransaction>,
) -> Vec<SignedTransaction> {
transactions
}

fn prepare_transactions(
fn validate_tx(
&self,
_block_index: BlockIndex,
_block_timestamp: u64,
_gas_price: Balance,
_gas_limit: Gas,
_state_root: StateRoot,
_max_number_of_transactions: usize,
transactions: &mut dyn PoolIterator,
_chain_validate: &mut dyn FnMut(&SignedTransaction) -> bool,
) -> Result<Vec<SignedTransaction>, Error> {
let mut res = vec![];
while let Some(iter) = transactions.next() {
res.push(iter.next().unwrap());
}
Ok(res)
transaction: SignedTransaction,
) -> Result<ValidTransaction, RuntimeError> {
Ok(ValidTransaction { transaction })
}

fn add_validator_proposals(
Expand Down
38 changes: 16 additions & 22 deletions chain/chain/src/types.rs
Expand Up @@ -6,6 +6,7 @@ use near_crypto::Signature;
use near_primitives::block::{Approval, WeightAndScore};
pub use near_primitives::block::{Block, BlockHeader, Weight};
use near_primitives::challenge::ChallengesResult;
use near_primitives::errors::RuntimeError;
use near_primitives::hash::{hash, CryptoHash};
use near_primitives::merkle::{merklize, MerklePath};
use near_primitives::receipt::Receipt;
Expand All @@ -19,8 +20,6 @@ use near_primitives::views::{EpochValidatorInfo, QueryResponse};
use near_store::{PartialStorage, StoreUpdate, WrappedTrieChanges};

use crate::error::Error;
use near_pool::types::PoolIterator;
use near_primitives::errors::InvalidTxError;

#[derive(PartialEq, Eq, Clone, Debug, BorshSerialize, BorshDeserialize)]
pub struct ReceiptResponse(pub CryptoHash, pub Vec<Receipt>);
Expand Down Expand Up @@ -77,6 +76,12 @@ pub struct AcceptedBlock {
pub provenance: Provenance,
}

/// Information about valid transaction that was processed by chain + runtime.
#[derive(Debug)]
pub struct ValidTransaction {
pub transaction: SignedTransaction,
}

/// Map of shard to list of receipts to send to it.
pub type ReceiptResult = HashMap<ShardId, Vec<Receipt>>;

Expand Down Expand Up @@ -134,38 +139,27 @@ pub trait RuntimeAdapter: Send + Sync {
header: &BlockHeader,
) -> Result<Weight, Error>;

/// Validates a given signed transaction on top of the given state root.
/// Returns an option of `InvalidTxError`, it contains `Some(InvalidTxError)` if there is
/// a validation error, or `None` in case the transaction succeeded.
/// Throws an `Error` with `ErrorKind::StorageError` in case the runtime throws
/// `RuntimeError::StorageError`.
/// Validate transaction and return transaction information relevant to ordering it in the mempool.
fn validate_tx(
&self,
block_index: BlockIndex,
block_timestamp: u64,
gas_price: Balance,
state_root: StateRoot,
transaction: &SignedTransaction,
) -> Result<Option<InvalidTxError>, Error>;

/// Returns an ordered list of valid transactions from the pool up the given limits.
/// Pulls transactions from the given pool iterators one by one. Validates each transaction
/// against the given `chain_validate` closure and runtime's transaction verifier.
/// If the transaction is valid for both, it's added to the result and the temporary state
/// update is preserved for validation of next transactions.
/// Throws an `Error` with `ErrorKind::StorageError` in case the runtime throws
/// `RuntimeError::StorageError`.
fn prepare_transactions(
transaction: SignedTransaction,
) -> Result<ValidTransaction, RuntimeError>;

/// Filter transactions by verifying each one by one in the given order. Every successful
/// verification stores the updated account balances to be used by next transactions.
fn filter_transactions(
&self,
block_index: BlockIndex,
block_timestamp: u64,
gas_price: Balance,
gas_limit: Gas,
state_root: StateRoot,
max_number_of_transactions: usize,
pool_iterator: &mut dyn PoolIterator,
chain_validate: &mut dyn FnMut(&SignedTransaction) -> bool,
) -> Result<Vec<SignedTransaction>, Error>;
transactions: Vec<SignedTransaction>,
) -> Vec<SignedTransaction>;

/// Verify validator signature for the given epoch.
/// Note: doesnt't account for slashed accounts within given epoch. USE WITH CAUTION.
Expand Down
22 changes: 15 additions & 7 deletions chain/chunks/src/lib.rs
Expand Up @@ -11,12 +11,12 @@ use rand::seq::SliceRandom;
use near_chain::validate::validate_chunk_proofs;
use near_chain::{
byzantine_assert, collect_receipts, ChainStore, ChainStoreAccess, ChainStoreUpdate, ErrorKind,
RuntimeAdapter,
RuntimeAdapter, ValidTransaction,
};
use near_crypto::Signer;
use near_network::types::PartialEncodedChunkRequestMsg;
use near_network::NetworkRequests;
use near_pool::{PoolIteratorWrapper, TransactionPool};
use near_pool::TransactionPool;
use near_primitives::hash::CryptoHash;
use near_primitives::merkle::{merklize, verify_path, MerklePath};
use near_primitives::receipt::Receipt;
Expand Down Expand Up @@ -190,8 +190,16 @@ impl ShardsManager {
);
}

pub fn get_pool_iterator(&mut self, shard_id: ShardId) -> Option<PoolIteratorWrapper> {
self.tx_pools.get_mut(&shard_id).map(|pool| pool.pool_iterator())
pub fn prepare_transactions(
&mut self,
shard_id: ShardId,
expected_weight: u32,
) -> Result<Vec<SignedTransaction>, Error> {
if let Some(tx_pool) = self.tx_pools.get_mut(&shard_id) {
tx_pool.prepare_transactions(expected_weight).map_err(|err| err.into())
} else {
Ok(vec![])
}
}

pub fn cares_about_shard_this_or_next_epoch(
Expand Down Expand Up @@ -416,7 +424,7 @@ impl ShardsManager {
self.block_hash_to_chunk_headers.remove(&prev_block_hash).unwrap_or_else(|| vec![])
}

pub fn insert_transaction(&mut self, shard_id: ShardId, tx: SignedTransaction) {
pub fn insert_transaction(&mut self, shard_id: ShardId, tx: ValidTransaction) {
self.tx_pools
.entry(shard_id)
.or_insert_with(TransactionPool::default)
Expand All @@ -441,7 +449,7 @@ impl ShardsManager {
self.tx_pools
.entry(shard_id)
.or_insert_with(TransactionPool::default)
.reintroduce_transactions(transactions.clone());
.reintroduce_transactions(transactions);
}

pub fn receipts_recipient_filter(
Expand Down Expand Up @@ -826,7 +834,7 @@ impl ShardsManager {
validator_reward: Balance,
balance_burnt: Balance,
validator_proposals: Vec<ValidatorStake>,
transactions: Vec<SignedTransaction>,
transactions: &Vec<SignedTransaction>,
outgoing_receipts: &Vec<Receipt>,
outgoing_receipts_root: CryptoHash,
tx_root: CryptoHash,
Expand Down
7 changes: 7 additions & 0 deletions chain/chunks/src/types.rs
Expand Up @@ -11,6 +11,7 @@ pub enum Error {
KnownPart,
ChainError(near_chain::Error),
IOError(std::io::Error),
PoolError(near_pool::Error),
}

impl std::fmt::Display for Error {
Expand All @@ -30,3 +31,9 @@ impl From<near_chain::Error> for Error {
Error::ChainError(err)
}
}

impl From<near_pool::Error> for Error {
fn from(err: near_pool::Error) -> Self {
Error::PoolError(err)
}
}

0 comments on commit a5c0d04

Please sign in to comment.