Pool crate cleanup (#1835)
* Refactor retrieve_transaction * Cleanup transaction_pool
This commit is contained in:
+39
-48
@@ -48,8 +48,8 @@ impl Pool {
|
||||
) -> Pool {
|
||||
Pool {
|
||||
entries: vec![],
|
||||
blockchain: chain.clone(),
|
||||
verifier_cache: verifier_cache.clone(),
|
||||
blockchain: chain,
|
||||
verifier_cache,
|
||||
name,
|
||||
}
|
||||
}
|
||||
@@ -74,34 +74,34 @@ impl Pool {
|
||||
&self,
|
||||
hash: Hash,
|
||||
nonce: u64,
|
||||
kern_ids: &Vec<ShortId>,
|
||||
kern_ids: &[ShortId],
|
||||
) -> (Vec<Transaction>, Vec<ShortId>) {
|
||||
let mut rehashed = HashMap::new();
|
||||
let mut txs = vec![];
|
||||
let mut found_ids = vec![];
|
||||
|
||||
// Rehash all entries in the pool using short_ids based on provided hash and nonce.
|
||||
for x in &self.entries {
|
||||
'outer: for x in &self.entries {
|
||||
for k in x.tx.kernels() {
|
||||
// rehash each kernel to calculate the block specific short_id
|
||||
let short_id = k.short_id(&hash, nonce);
|
||||
rehashed.insert(short_id, x.tx.hash());
|
||||
if kern_ids.contains(&short_id) {
|
||||
txs.push(x.tx.clone());
|
||||
found_ids.push(short_id);
|
||||
}
|
||||
if found_ids.len() == kern_ids.len() {
|
||||
break 'outer;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Retrive the txs from the pool by the set of unique hashes.
|
||||
let hashes: HashSet<_> = rehashed.values().collect();
|
||||
let txs = hashes.into_iter().filter_map(|x| self.get_tx(*x)).collect();
|
||||
|
||||
// Calculate the missing ids based on the ids passed in
|
||||
// and the ids that successfully matched txs.
|
||||
let matched_ids: HashSet<_> = rehashed.keys().collect();
|
||||
let all_ids: HashSet<_> = kern_ids.iter().collect();
|
||||
let missing_ids = all_ids
|
||||
.difference(&matched_ids)
|
||||
.map(|x| *x)
|
||||
.cloned()
|
||||
.collect();
|
||||
|
||||
(txs, missing_ids)
|
||||
txs.dedup();
|
||||
(
|
||||
txs,
|
||||
kern_ids
|
||||
.into_iter()
|
||||
.filter(|id| !found_ids.contains(id))
|
||||
.cloned()
|
||||
.collect(),
|
||||
)
|
||||
}
|
||||
|
||||
/// Take pool transactions, filtering and ordering them in a way that's
|
||||
@@ -171,10 +171,10 @@ impl Pool {
|
||||
}
|
||||
|
||||
// Transition the specified pool entries to the new state.
|
||||
pub fn transition_to_state(&mut self, txs: &Vec<Transaction>, state: PoolEntryState) {
|
||||
for x in self.entries.iter_mut() {
|
||||
pub fn transition_to_state(&mut self, txs: &[Transaction], state: PoolEntryState) {
|
||||
for x in &mut self.entries {
|
||||
if txs.contains(&x.tx) {
|
||||
x.state = state.clone();
|
||||
x.state = state;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -214,9 +214,6 @@ impl Pool {
|
||||
// Validate aggregated tx against a known chain state.
|
||||
self.validate_raw_tx(&agg_tx, header)?;
|
||||
|
||||
// If we get here successfully then we can safely add the entry to the pool.
|
||||
self.entries.push(entry.clone());
|
||||
|
||||
debug!(
|
||||
"add_to_pool [{}]: {} ({}), in/out/kern: {}/{}/{}, pool: {} (at block {})",
|
||||
self.name,
|
||||
@@ -228,6 +225,8 @@ impl Pool {
|
||||
self.size(),
|
||||
header.hash(),
|
||||
);
|
||||
// If we get here successfully then we can safely add the entry to the pool.
|
||||
self.entries.push(entry);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -311,7 +310,7 @@ impl Pool {
|
||||
}
|
||||
|
||||
for x in existing_entries {
|
||||
let _ = self.add_to_pool(x.clone(), extra_txs.clone(), header);
|
||||
let _ = self.add_to_pool(x, extra_txs.clone(), header);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
@@ -352,20 +351,7 @@ impl Pool {
|
||||
tx_buckets
|
||||
}
|
||||
|
||||
// Filter txs in the pool based on the latest block.
|
||||
// Reject any txs where we see a matching tx kernel in the block.
|
||||
// Also reject any txs where we see a conflicting tx,
|
||||
// where an input is spent in a different tx.
|
||||
fn remaining_transactions(&self, block: &Block) -> Vec<Transaction> {
|
||||
self.entries
|
||||
.iter()
|
||||
.filter(|x| !x.tx.kernels().iter().any(|y| block.kernels().contains(y)))
|
||||
.filter(|x| !x.tx.inputs().iter().any(|y| block.inputs().contains(y)))
|
||||
.map(|x| x.tx.clone())
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn find_matching_transactions(&self, kernels: Vec<TxKernel>) -> Vec<Transaction> {
|
||||
pub fn find_matching_transactions(&self, kernels: &[TxKernel]) -> Vec<Transaction> {
|
||||
// While the inputs outputs can be cut-through the kernel will stay intact
|
||||
// In order to deaggregate tx we look for tx with the same kernel
|
||||
let mut found_txs = vec![];
|
||||
@@ -375,7 +361,7 @@ impl Pool {
|
||||
|
||||
// Check each transaction in the pool
|
||||
for entry in &self.entries {
|
||||
let entry_kernel_set = entry.tx.kernels().iter().cloned().collect::<HashSet<_>>();
|
||||
let entry_kernel_set = entry.tx.kernels().iter().collect::<HashSet<_>>();
|
||||
if entry_kernel_set.is_subset(&kernel_set) {
|
||||
found_txs.push(entry.tx.clone());
|
||||
}
|
||||
@@ -385,10 +371,15 @@ impl Pool {
|
||||
|
||||
/// Quick reconciliation step - we can evict any txs in the pool where
|
||||
/// inputs or kernels intersect with the block.
|
||||
pub fn reconcile_block(&mut self, block: &Block) -> Result<(), PoolError> {
|
||||
let candidate_txs = self.remaining_transactions(block);
|
||||
self.entries.retain(|x| candidate_txs.contains(&x.tx));
|
||||
Ok(())
|
||||
pub fn reconcile_block(&mut self, block: &Block) {
|
||||
// Filter txs in the pool based on the latest block.
|
||||
// Reject any txs where we see a matching tx kernel in the block.
|
||||
// Also reject any txs where we see a conflicting tx,
|
||||
// where an input is spent in a different tx.
|
||||
self.entries.retain(|x| {
|
||||
!x.tx.kernels().iter().any(|y| block.kernels().contains(y))
|
||||
&& !x.tx.inputs().iter().any(|y| block.inputs().contains(y))
|
||||
});
|
||||
}
|
||||
|
||||
pub fn size(&self) -> usize {
|
||||
|
||||
Reference in New Issue
Block a user