Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions core/src/client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ use crate::transaction::{
LocalizedTransaction, PendingVerifiedTransactions, UnverifiedTransaction, VerifiedTransaction,
};
use crate::types::{BlockId, BlockStatus, TransactionId, VerificationQueueInfo as BlockQueueInfo};
use crate::MemPoolMinFees;
use cdb::{new_journaldb, Algorithm, AsHashDB};
use cio::IoChannel;
use ckey::{Address, NetworkId, PlatformAddress};
Expand Down Expand Up @@ -699,10 +698,6 @@ impl MiningBlockChainClient for Client {
fn register_immune_users(&self, immune_user_vec: Vec<Address>) {
self.importer.miner.register_immune_users(immune_user_vec)
}

fn mem_pool_min_fees(&self) -> MemPoolMinFees {
self.importer.miner.get_options().mem_pool_min_fees
}
}

impl FindDoubleVoteHandler for Client {
Expand Down
3 changes: 0 additions & 3 deletions core/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ use crate::blockchain_info::BlockChainInfo;
use crate::consensus::EngineError;
use crate::encoded;
use crate::error::{BlockImportError, Error as GenericError};
use crate::miner::MemPoolMinFees;
use crate::transaction::{LocalizedTransaction, PendingVerifiedTransactions, VerifiedTransaction};
use crate::types::{BlockId, BlockStatus, TransactionId, VerificationQueueInfo as BlockQueueInfo};
use cdb::DatabaseError;
Expand Down Expand Up @@ -262,8 +261,6 @@ pub trait MiningBlockChainClient: BlockChainClient + BlockProducer + FindDoubleV

/// Append designated users to the immune user list.
fn register_immune_users(&self, immune_user_vec: Vec<Address>);

fn mem_pool_min_fees(&self) -> MemPoolMinFees;
}

/// Provides methods to access database.
Expand Down
6 changes: 1 addition & 5 deletions core/src/client/test_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ use crate::consensus::EngineError;
use crate::db::{COL_STATE, NUM_COLUMNS};
use crate::encoded;
use crate::error::{BlockImportError, Error as GenericError};
use crate::miner::{MemPoolMinFees, Miner, MinerService, TransactionImportResult};
use crate::miner::{Miner, MinerService, TransactionImportResult};
use crate::scheme::Scheme;
use crate::transaction::{LocalizedTransaction, PendingVerifiedTransactions, VerifiedTransaction};
use crate::types::{BlockId, TransactionId, VerificationQueueInfo as QueueInfo};
Expand Down Expand Up @@ -362,10 +362,6 @@ impl MiningBlockChainClient for TestBlockChainClient {
fn register_immune_users(&self, immune_user_vec: Vec<Address>) {
self.miner.register_immune_users(immune_user_vec)
}

fn mem_pool_min_fees(&self) -> MemPoolMinFees {
self.miner.get_options().mem_pool_min_fees
}
}

impl AccountData for TestBlockChainClient {
Expand Down
47 changes: 0 additions & 47 deletions core/src/codechain_machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ use crate::error::Error;
use crate::transaction::{UnverifiedTransaction, VerifiedTransaction};
use ckey::Address;
use cstate::{StateError, TopState, TopStateView};
use ctypes::errors::SyntaxError;
use ctypes::transaction::Action;
use ctypes::{CommonParams, Header};
use std::convert::TryInto;

Expand All @@ -42,25 +40,6 @@ impl CodeChainMachine {
&self.params
}

/// Does basic verification of the transaction.
pub fn verify_transaction_with_params(
&self,
tx: &UnverifiedTransaction,
common_params: &CommonParams,
) -> Result<(), Error> {
let min_cost = Self::min_cost(common_params, &tx.transaction().action);
if tx.transaction().fee < min_cost {
return Err(SyntaxError::InsufficientFee {
minimal: min_cost,
got: tx.transaction().fee,
}
.into())
}
tx.verify_with_params(common_params)?;

Ok(())
}

/// Verify a particular transaction's seal is valid.
pub fn verify_transaction_seal(p: UnverifiedTransaction, _header: &Header) -> Result<VerifiedTransaction, Error> {
Ok(p.try_into()?)
Expand All @@ -78,32 +57,6 @@ impl CodeChainMachine {
Ok(())
}

pub fn min_cost(params: &CommonParams, action: &Action) -> u64 {
match action {
Action::Pay {
..
} => params.min_pay_transaction_cost(),
Action::CreateShard {
..
} => params.min_create_shard_transaction_cost(),
Action::SetShardOwners {
..
} => params.min_set_shard_owners_transaction_cost(),
Action::SetShardUsers {
..
} => params.min_set_shard_users_transaction_cost(),
Action::Custom {
..
} => params.min_custom_transaction_cost(),
Action::ShardStore {
..
} => {
// FIXME
0
}
}
}

pub fn balance(&self, live: &ExecutedBlock, address: &Address) -> Result<u64, Error> {
Ok(live.state().balance(address).map_err(StateError::from)?)
}
Expand Down
3 changes: 2 additions & 1 deletion core/src/consensus/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,8 @@ pub trait CodeChainEngine: ConsensusEngine {

action.verify(common_params)?;
}
self.machine().verify_transaction_with_params(tx, common_params)
tx.verify_with_params(common_params)?;
Ok(())
}
}

Expand Down
2 changes: 1 addition & 1 deletion core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub use crate::consensus::stake;
pub use crate::consensus::{EngineType, TimeGapParams};
pub use crate::db::{COL_STATE, NUM_COLUMNS};
pub use crate::error::{BlockImportError, Error, ImportError};
pub use crate::miner::{MemPoolMinFees, Miner, MinerOptions, MinerService};
pub use crate::miner::{Miner, MinerOptions, MinerService};
pub use crate::peer_db::PeerDb;
pub use crate::scheme::Scheme;
pub use crate::service::ClientService;
Expand Down
151 changes: 6 additions & 145 deletions core/src/miner/mem_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

use super::backup;
use super::mem_pool_types::{
AccountDetails, CurrentQueue, FutureQueue, MemPoolInput, MemPoolItem, MemPoolMinFees, MemPoolStatus,
PoolingInstant, QueueTag, TransactionOrder, TransactionOrderWithTag, TxOrigin,
AccountDetails, CurrentQueue, FutureQueue, MemPoolInput, MemPoolItem, MemPoolStatus, PoolingInstant, QueueTag,
TransactionOrder, TransactionOrderWithTag, TxOrigin,
};
use super::TransactionImportResult;
use crate::client::{AccountData, BlockChainTrait};
Expand Down Expand Up @@ -72,8 +72,6 @@ impl From<SyntaxError> for Error {
}

pub struct MemPool {
/// Fee threshold for transactions that can be imported to this pool
minimum_fees: MemPoolMinFees,
/// A value which is used to check whether a new transaciton can replace a transaction in the memory pool with the same signer and seq.
/// If the fee of the new transaction is `new_fee` and the fee of the transaction in the memory pool is `old_fee`,
/// then `new_fee > old_fee + old_fee >> mem_pool_fee_bump_shift` should be satisfied to replace.
Expand Down Expand Up @@ -113,15 +111,8 @@ pub struct MemPool {

impl MemPool {
/// Create new instance of this Queue with specified limits
pub fn with_limits(
limit: usize,
memory_limit: usize,
fee_bump_shift: usize,
db: Arc<dyn KeyValueDB>,
minimum_fees: MemPoolMinFees,
) -> Self {
pub fn with_limits(limit: usize, memory_limit: usize, fee_bump_shift: usize, db: Arc<dyn KeyValueDB>) -> Self {
MemPool {
minimum_fees,
fee_bump_shift,
max_block_number_period_in_pool: DEFAULT_POOLING_PERIOD,
current: CurrentQueue::new(),
Expand Down Expand Up @@ -700,23 +691,6 @@ impl MemPool {
origin: TxOrigin,
client_account: &AccountDetails,
) -> Result<(), Error> {
let action_min_fee = self.minimum_fees.min_cost(&tx.transaction().action);
if origin != TxOrigin::Local && tx.transaction().fee < action_min_fee {
ctrace!(
MEM_POOL,
"Dropping transaction below mempool defined minimum fee: {:?} (gp: {} < {})",
tx.hash(),
tx.transaction().fee,
action_min_fee
);

return Err(SyntaxError::InsufficientFee {
minimal: action_min_fee,
got: tx.transaction().fee,
}
.into())
}

let full_pools_lowest = self.effective_minimum_fee();
if origin != TxOrigin::Local && tx.transaction().fee < full_pools_lowest {
ctrace!(
Expand Down Expand Up @@ -1042,7 +1016,7 @@ pub mod test {
test_client.set_balance(default_addr, u64::max_value());

let db = Arc::new(kvdb_memorydb::create(crate::db::NUM_COLUMNS.unwrap_or(0)));
let mut mem_pool = MemPool::with_limits(8192, usize::max_value(), 3, db.clone(), Default::default());
let mut mem_pool = MemPool::with_limits(8192, usize::max_value(), 3, db.clone());

let fetch_account = fetch_account_creator(&test_client);

Expand Down Expand Up @@ -1076,7 +1050,7 @@ pub mod test {
inputs.push(create_mempool_input_with_pay(7u64, &keypair));
mem_pool.add(inputs, inserted_block_number, inserted_timestamp, &fetch_account);

let mut mem_pool_recovered = MemPool::with_limits(8192, usize::max_value(), 3, db, Default::default());
let mut mem_pool_recovered = MemPool::with_limits(8192, usize::max_value(), 3, db);
mem_pool_recovered.recover_from_db(&test_client);

assert_eq!(mem_pool_recovered.first_seqs, mem_pool.first_seqs);
Expand Down Expand Up @@ -1105,131 +1079,18 @@ pub mod test {
VerifiedTransaction::new_with_sign(tx, keypair.private())
}

fn create_signed_pay_with_fee(seq: u64, fee: u64, keypair: &KeyPair) -> VerifiedTransaction {
let receiver = 1u64.into();
let tx = Transaction {
seq,
fee,
network_id: "tc".into(),
action: Action::Pay {
receiver,
quantity: 100_000,
},
};
VerifiedTransaction::new_with_sign(tx, keypair.private())
}

fn create_mempool_input_with_pay(seq: u64, keypair: &KeyPair) -> MemPoolInput {
let signed = create_signed_pay(seq, &keypair);
MemPoolInput::new(signed, TxOrigin::Local)
}

fn abbreviated_mempool_add(
test_client: &TestBlockChainClient,
mem_pool: &mut MemPool,
txs: Vec<VerifiedTransaction>,
origin: TxOrigin,
) -> Vec<Result<TransactionImportResult, Error>> {
let fetch_account = fetch_account_creator(test_client);

let inserted_block_number = 1;
let inserted_timestamp = 100;
let inputs: Vec<MemPoolInput> = txs.into_iter().map(|tx| MemPoolInput::new(tx, origin)).collect();
mem_pool.add(inputs, inserted_block_number, inserted_timestamp, &fetch_account)
}

#[test]
fn local_transactions_whose_fees_are_under_the_mem_pool_min_fee_should_not_be_rejected() {
let test_client = TestBlockChainClient::new();

// Set the pay transaction minimum fee
let fees = MemPoolMinFees::create_from_options(Some(150), None, None, None, None);

let db = Arc::new(kvdb_memorydb::create(crate::db::NUM_COLUMNS.unwrap_or(0)));
let mut mem_pool = MemPool::with_limits(8192, usize::max_value(), 3, db, fees);
let keypair: KeyPair = Random.generate().unwrap();
let address = public_to_address(keypair.public());

test_client.set_balance(address, 1_000_000_000_000);

let txs = vec![
create_signed_pay_with_fee(0, 200, &keypair),
create_signed_pay_with_fee(1, 140, &keypair),
create_signed_pay_with_fee(2, 160, &keypair),
];
let result = abbreviated_mempool_add(&test_client, &mut mem_pool, txs, TxOrigin::Local);
assert_eq!(
vec![
Ok(TransactionImportResult::Current),
Ok(TransactionImportResult::Current),
Ok(TransactionImportResult::Current)
],
result
);

assert_eq!(
vec![
create_signed_pay_with_fee(0, 200, &keypair),
create_signed_pay_with_fee(1, 140, &keypair),
create_signed_pay_with_fee(2, 160, &keypair)
],
mem_pool.top_transactions(std::usize::MAX, 0..std::u64::MAX).transactions
);

assert_eq!(Vec::<VerifiedTransaction>::default(), mem_pool.future_transactions());
}

#[test]
fn external_transactions_whose_fees_are_under_the_mem_pool_min_fee_are_rejected() {
let test_client = TestBlockChainClient::new();
// Set the pay transaction minimum fee
let fees = MemPoolMinFees::create_from_options(Some(150), None, None, None, None);

let db = Arc::new(kvdb_memorydb::create(crate::db::NUM_COLUMNS.unwrap_or(0)));
let mut mem_pool = MemPool::with_limits(8192, usize::max_value(), 3, db, fees);
let keypair: KeyPair = Random.generate().unwrap();
let address = public_to_address(keypair.public());

test_client.set_balance(address, 1_000_000_000_000);

let txs = vec![
create_signed_pay_with_fee(0, 200, &keypair),
create_signed_pay_with_fee(1, 140, &keypair),
create_signed_pay_with_fee(1, 160, &keypair),
create_signed_pay_with_fee(2, 149, &keypair),
];
let result = abbreviated_mempool_add(&test_client, &mut mem_pool, txs, TxOrigin::External);
assert_eq!(
vec![
Ok(TransactionImportResult::Current),
Err(Error::Syntax(SyntaxError::InsufficientFee {
minimal: 150,
got: 140,
})),
Ok(TransactionImportResult::Current),
Err(Error::Syntax(SyntaxError::InsufficientFee {
minimal: 150,
got: 149,
})),
],
result
);

assert_eq!(
vec![create_signed_pay_with_fee(0, 200, &keypair), create_signed_pay_with_fee(1, 160, &keypair)],
mem_pool.top_transactions(std::usize::MAX, 0..std::u64::MAX).transactions
);

assert_eq!(Vec::<VerifiedTransaction>::default(), mem_pool.future_transactions());
}

#[test]
fn transactions_are_moved_to_future_queue_if_the_preceding_one_removed() {
//setup test_client
let test_client = TestBlockChainClient::new();

let db = Arc::new(kvdb_memorydb::create(crate::db::NUM_COLUMNS.unwrap_or(0)));
let mut mem_pool = MemPool::with_limits(8192, usize::max_value(), 3, db, Default::default());
let mut mem_pool = MemPool::with_limits(8192, usize::max_value(), 3, db);

let fetch_account = fetch_account_creator(&test_client);
let keypair: KeyPair = Random.generate().unwrap();
Expand Down
Loading