Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't add transactions to a pool in non-validator nodes #6713

Merged
merged 7 commits into from
May 2, 2022
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
1 change: 1 addition & 0 deletions chain/chunks/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,7 @@ impl ShardsManager {
network_adapter: Arc<dyn PeerManagerAdapter>,
rng_seed: RngSeed,
) -> Self {
TransactionPool::init_metrics();
nikurt marked this conversation as resolved.
Show resolved Hide resolved
Self {
me: me.clone(),
tx_pools: HashMap::new(),
Expand Down
16 changes: 8 additions & 8 deletions chain/client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1666,29 +1666,29 @@ impl Client {
let active_validator = self.active_validator(shard_id)?;

// If I'm not an active validator I should forward tx to next validators.
debug!(
target: "client",
"Recording a transaction. I'm {:?}, {} is_forwarded: {}",
me,
shard_id,
is_forwarded
);
self.shards_mgr.insert_transaction(shard_id, tx.clone());

// Active validator:
// possibly forward to next epoch validators
// Not active validator:
// forward to current epoch validators,
// possibly forward to next epoch validators
if active_validator {
debug!(target: "client", account=?me, shard_id, is_forwarded, "Recording a transaction.");
metrics::TRANSACTION_RECEIVED_VALIDATOR.inc();
self.shards_mgr.insert_transaction(shard_id, tx.clone());

if !is_forwarded {
self.possibly_forward_tx_to_next_epoch(tx)?;
}
nikurt marked this conversation as resolved.
Show resolved Hide resolved
Ok(NetworkClientResponses::ValidTx)
} else if !is_forwarded {
debug!(target: "client", shard_id, "Forwarding a transaction.");
metrics::TRANSACTION_RECEIVED_NON_VALIDATOR.inc();
self.forward_tx(&epoch_id, tx)?;
Ok(NetworkClientResponses::RequestRouted)
} else {
debug!(target: "client", shard_id, "Non-validator received a forwarded transaction, dropping it.");
metrics::TRANSACTION_RECEIVED_NON_VALIDATOR_FORWARDED.inc();
Ok(NetworkClientResponses::NoResponse)
}
}
Expand Down
19 changes: 19 additions & 0 deletions chain/client/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,25 @@ static NODE_BUILD_INFO: Lazy<IntCounterVec> = Lazy::new(|| {
.unwrap()
});

pub(crate) static TRANSACTION_RECEIVED_VALIDATOR: Lazy<IntGauge> = Lazy::new(|| {
try_create_int_gauge("near_transaction_received_validator", "Validator received a transaction")
.unwrap()
});
pub(crate) static TRANSACTION_RECEIVED_NON_VALIDATOR: Lazy<IntGauge> = Lazy::new(|| {
try_create_int_gauge(
"near_transaction_received_non_validator",
"Non-validator received a transaction",
)
.unwrap()
});
pub(crate) static TRANSACTION_RECEIVED_NON_VALIDATOR_FORWARDED: Lazy<IntGauge> = Lazy::new(|| {
try_create_int_gauge(
"near_transaction_received_non_validator_forwarded",
"Non-validator received a forwarded transaction",
)
.unwrap()
});

/// Exports neard, protocol and database versions via Prometheus metrics.
///
/// Sets metrics which export node’s max supported protocol version, used
Expand Down
13 changes: 7 additions & 6 deletions chain/pool/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ pub struct TransactionPool {
/// Transactions are grouped by a pair of (account ID, signer public key).
/// NOTE: It's more efficient on average to keep transactions unsorted and with potentially
/// conflicting nonce than to create a BTreeMap for every transaction.
pub transactions: BTreeMap<PoolKey, Vec<SignedTransaction>>,
transactions: BTreeMap<PoolKey, Vec<SignedTransaction>>,
/// Set of all hashes to quickly check if the given transaction is in the pool.
pub unique_transactions: HashSet<CryptoHash>,
unique_transactions: HashSet<CryptoHash>,
/// A uniquely generated key seed to randomize PoolKey order.
key_seed: RngSeed,
/// The key after which the pool iterator starts. Doesn't have to be present in the pool.
Expand All @@ -36,6 +36,11 @@ impl TransactionPool {
}
}

pub fn init_metrics() {
// A `get()` call initializes a metric even if its value is zero.
metrics::TRANSACTION_POOL_TOTAL.get();
}

fn key(&self, account_id: &AccountId, public_key: &PublicKey) -> PoolKey {
let mut v = public_key.try_to_vec().unwrap();
v.extend_from_slice(&self.key_seed);
Expand Down Expand Up @@ -108,10 +113,6 @@ impl TransactionPool {
pub fn len(&self) -> usize {
self.unique_transactions.len()
}

pub fn is_empty(&self) -> bool {
self.unique_transactions.is_empty()
}
}

/// PoolIterator is a structure to pull transactions from the pool.
Expand Down