Skip to content
This repository has been archived by the owner on Sep 13, 2022. It is now read-only.

Commit

Permalink
fix(mempool): no peer id in context for trust metric feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
zeroqn committed Apr 15, 2020
1 parent 9b2731c commit aa5cb46
Showing 1 changed file with 53 additions and 32 deletions.
85 changes: 53 additions & 32 deletions core/mempool/src/adapter/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use super::TxContext;

pub mod message;

use std::{
Expand Down Expand Up @@ -248,10 +250,12 @@ where
let sig = tx.signature.as_ref();

C::verify_signature(hash.as_ref(), sig, pub_key).map_err(|_| {
self.network.report(
ctx,
TrustFeedback::Worse(format!("Mempool wrong signature of tx {:?}", tx.tx_hash)),
);
if ctx.is_network_origin_txs() {
self.network.report(
ctx,
TrustFeedback::Worse(format!("Mempool wrong signature of tx {:?}", tx.tx_hash)),
);
}
MemPoolError::CheckSig {
tx_hash: tx.tx_hash,
}
Expand All @@ -269,10 +273,12 @@ where
let tx_hash = Hash::digest(fixed_bytes);

if tx_hash != stx.tx_hash {
self.network.report(
ctx,
TrustFeedback::Worse(format!("Mempool wrong tx_hash of tx {:?}", stx.tx_hash)),
);
if ctx.is_network_origin_txs() {
self.network.report(
ctx,
TrustFeedback::Worse(format!("Mempool wrong tx_hash of tx {:?}", stx.tx_hash)),
);
}
let wrong_hash = MemPoolError::CheckHash {
expect: stx.tx_hash,
actual: tx_hash,
Expand All @@ -284,10 +290,15 @@ where
// check tx size
let max_tx_size = self.max_tx_size.load(Ordering::SeqCst);
if size > max_tx_size {
self.network.report(
ctx,
TrustFeedback::Bad(format!("Mempool exceed size limit of tx {:?}", stx.tx_hash)),
);
if ctx.is_network_origin_txs() {
self.network.report(
ctx,
TrustFeedback::Bad(format!(
"Mempool exceed size limit of tx {:?}",
stx.tx_hash
)),
);
}
return Err(MemPoolError::ExceedSizeLimit {
tx_hash,
max_tx_size,
Expand All @@ -300,13 +311,15 @@ where
let cycles_limit_config = self.cycles_limit.load(Ordering::SeqCst);
let cycles_limit_tx = stx.raw.cycles_limit;
if cycles_limit_tx > cycles_limit_config {
self.network.report(
ctx,
TrustFeedback::Bad(format!(
"Mempool exceed cycle limit of tx {:?}",
stx.tx_hash
)),
);
if ctx.is_network_origin_txs() {
self.network.report(
ctx,
TrustFeedback::Bad(format!(
"Mempool exceed cycle limit of tx {:?}",
stx.tx_hash
)),
);
}
return Err(MemPoolError::ExceedCyclesLimit {
tx_hash,
cycles_limit_tx,
Expand All @@ -318,10 +331,12 @@ where
// Verify chain id
let latest_block = self.storage.get_latest_block().await?;
if latest_block.header.chain_id != stx.raw.chain_id {
self.network.report(
ctx,
TrustFeedback::Worse(format!("Mempool wrong chain of tx {:?}", stx.tx_hash)),
);
if ctx.is_network_origin_txs() {
self.network.report(
ctx,
TrustFeedback::Worse(format!("Mempool wrong chain of tx {:?}", stx.tx_hash)),
);
}
let wrong_chain_id = MemPoolError::WrongChain {
tx_hash: stx.tx_hash,
};
Expand All @@ -334,10 +349,12 @@ where
let timeout_gap = self.timeout_gap.load(Ordering::SeqCst);

if stx.raw.timeout > latest_height + timeout_gap {
self.network.report(
ctx,
TrustFeedback::Bad(format!("Mempool invalid timeout of tx {:?}", stx.tx_hash)),
);
if ctx.is_network_origin_txs() {
self.network.report(
ctx,
TrustFeedback::Bad(format!("Mempool invalid timeout of tx {:?}", stx.tx_hash)),
);
}
let invalid_timeout = MemPoolError::InvalidTimeout {
tx_hash: stx.tx_hash,
};
Expand All @@ -346,10 +363,12 @@ where
}

if stx.raw.timeout < latest_height {
self.network.report(
ctx,
TrustFeedback::Bad(format!("Mempool timeout of tx {:?}", stx.tx_hash)),
);
if ctx.is_network_origin_txs() {
self.network.report(
ctx,
TrustFeedback::Bad(format!("Mempool timeout of tx {:?}", stx.tx_hash)),
);
}
let timeout = MemPoolError::Timeout {
tx_hash: stx.tx_hash,
timeout: stx.raw.timeout,
Expand Down Expand Up @@ -381,7 +400,9 @@ where
}

fn report_good(&self, ctx: Context) {
self.network.report(ctx, TrustFeedback::Good);
if ctx.is_network_origin_txs() {
self.network.report(ctx, TrustFeedback::Good);
}
}

fn set_args(&self, timeout_gap: u64, cycles_limit: u64, max_tx_size: u64) {
Expand Down

0 comments on commit aa5cb46

Please sign in to comment.