Skip to content

Commit

Permalink
TxSource is now an enum for type safety.
Browse files Browse the repository at this point in the history
  • Loading branch information
antiochp committed Jul 4, 2019
1 parent 12ab5fc commit 2e8584b
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 40 deletions.
5 changes: 1 addition & 4 deletions api/src/handlers/pool_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,7 @@ impl PoolPushHandler {
.map_err(|e| ErrorKind::RequestError(format!("Bad request: {}", e)).into())
})
.and_then(move |tx: Transaction| {
let source = pool::TxSource {
debug_name: "push-api".to_string(),
identifier: "?.?.?.?".to_string(),
};
let source = pool::TxSource::PushApi;
info!(
"Pushing transaction {} to pool (inputs: {}, outputs: {}, kernels: {})",
tx.hash(),
Expand Down
4 changes: 2 additions & 2 deletions pool/src/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,10 @@ impl Pool {

fn log_pool_add(&self, entry: &PoolEntry, header: &BlockHeader) {
debug!(
"add_to_pool [{}]: {} ({}) [in/out/kern: {}/{}/{}] pool: {} (at block {})",
"add_to_pool [{}]: {} ({:?}) [in/out/kern: {}/{}/{}] pool: {} (at block {})",
self.name,
entry.tx.hash(),
entry.src.debug_name,
entry.src,
entry.tx.inputs().len(),
entry.tx.outputs().len(),
entry.tx.kernels().len(),
Expand Down
2 changes: 1 addition & 1 deletion pool/src/transaction_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ impl TransactionPool {
tx.validate(Weighting::AsTransaction, self.verifier_cache.clone())?;

entry.tx = tx;
entry.src.debug_name = "deagg".to_string();
entry.src = TxSource::Deaggregate;
}
}
self.txpool.add_to_pool(entry.clone(), vec![], header)?;
Expand Down
23 changes: 12 additions & 11 deletions pool/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,27 +159,28 @@ pub struct PoolEntry {
pub tx: Transaction,
}

/// Placeholder: the data representing where we heard about a tx from.
///
/// Used to make decisions based on transaction acceptance priority from
/// various sources. For example, a node may want to bypass pool size
/// restrictions when accepting a transaction from a local wallet.
///
/// Most likely this will evolve to contain some sort of network identifier,
/// once we get a better sense of what transaction building might look like.
#[derive(Clone, Debug)]
pub struct TxSource {
/// Human-readable name used for logging and errors.
pub debug_name: String,
/// Unique identifier used to distinguish this peer from others.
pub identifier: String,
#[derive(Clone, Debug, PartialEq)]
pub enum TxSource {
PushApi,
Broadcast,
Fluff,
EmbargoExpired,
Deaggregate,
}

impl TxSource {
/// We should rework TxSource to be an enum for type safety.
/// But for now we can check if it is was pushed via api based on the debug_name...
/// Convenience fn for checking if this tx was sourced via the push api.
pub fn is_pushed(&self) -> bool {
self.debug_name == "push-api"
match self {
TxSource::PushApi => true,
_ => false,
}
}
}

Expand Down
5 changes: 1 addition & 4 deletions pool/tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,10 +229,7 @@ where
}

pub fn test_source() -> TxSource {
TxSource {
debug_name: format!("test"),
identifier: format!("127.0.0.1"),
}
TxSource::Broadcast
}

pub fn clean_output_dir(db_root: String) {
Expand Down
6 changes: 4 additions & 2 deletions pool/tests/transaction_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ use self::core::core::{transaction, Block, BlockHeader, Weighting};
use self::core::libtx;
use self::core::pow::Difficulty;
use self::keychain::{ExtKeychain, Keychain};
use self::pool::TxSource;
use self::util::RwLock;
use crate::common::*;
use grin_core as core;
use grin_keychain as keychain;
use grin_pool as pool;
use grin_util as util;
use std::sync::Arc;

Expand Down Expand Up @@ -237,7 +239,7 @@ fn test_the_transaction_pool() {
assert_eq!(write_pool.total_size(), 6);
let entry = write_pool.txpool.entries.last().unwrap();
assert_eq!(entry.tx.kernels().len(), 1);
assert_eq!(entry.src.debug_name, "deagg");
assert_eq!(entry.src, TxSource::Deaggregate);
}

// Check we cannot "double spend" an output spent in a previous block.
Expand Down Expand Up @@ -447,7 +449,7 @@ fn test_the_transaction_pool() {
assert_eq!(write_pool.total_size(), 6);
let entry = write_pool.txpool.entries.last().unwrap();
assert_eq!(entry.tx.kernels().len(), 1);
assert_eq!(entry.src.debug_name, "deagg");
assert_eq!(entry.src, TxSource::Deaggregate);
}

// Check we cannot "double spend" an output spent in a previous block.
Expand Down
5 changes: 1 addition & 4 deletions servers/src/common/adapters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,7 @@ impl p2p::ChainAdapter for NetToChainAdapter {
return Ok(true);
}

let source = pool::TxSource {
debug_name: "p2p".to_string(),
identifier: "?.?.?.?".to_string(),
};
let source = pool::TxSource::Broadcast;

let header = self.chain().head_header()?;

Expand Down
14 changes: 2 additions & 12 deletions servers/src/grin/dandelion_monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,7 @@ fn process_fluff_phase(
verifier_cache.clone(),
)?;

let src = TxSource {
debug_name: "fluff".to_string(),
identifier: "?.?.?.?".to_string(),
};

tx_pool.add_to_pool(src, agg_tx, false, &header)?;
tx_pool.add_to_pool(TxSource::Fluff, agg_tx, false, &header)?;
Ok(())
}

Expand All @@ -174,14 +169,9 @@ fn process_expired_entries(

let header = tx_pool.chain_head()?;

let src = TxSource {
debug_name: "embargo_expired".to_string(),
identifier: "?.?.?.?".to_string(),
};

for entry in expired_entries {
let txhash = entry.tx.hash();
match tx_pool.add_to_pool(src.clone(), entry.tx, false, &header) {
match tx_pool.add_to_pool(TxSource::EmbargoExpired, entry.tx, false, &header) {
Ok(_) => info!(
"dand_mon: embargo expired for {}, fluffed successfully.",
txhash
Expand Down

0 comments on commit 2e8584b

Please sign in to comment.