Skip to content

refactor: some cleanups in process_tx_internal #13003

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

Merged
merged 5 commits into from
Feb 27, 2025
Merged
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
107 changes: 53 additions & 54 deletions chain/client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2242,6 +2242,7 @@ impl Client {
self.shard_tracker.care_about_shard(me, &head.last_block_hash, shard_id, true);
let will_care_about_shard =
self.shard_tracker.will_care_about_shard(me, &head.last_block_hash, shard_id, true);

if care_about_shard || will_care_about_shard {
let shard_uid = shard_id_to_uid(self.epoch_manager.as_ref(), shard_id, &epoch_id)?;
let state_root = match self.chain.get_chunk_extra(&head.last_block_hash, &shard_uid) {
Expand All @@ -2267,69 +2268,67 @@ impl Client {
receiver_congestion_info,
) {
debug!(target: "client", ?err, "Invalid tx");
Ok(ProcessTxResponse::InvalidTx(err))
} else if check_only {
Ok(ProcessTxResponse::ValidTx)
} else {
// Transactions only need to be recorded if the node is a validator.
if me.is_some() {
match self
.chunk_producer
.sharded_tx_pool
.insert_transaction(shard_uid, tx.clone())
{
InsertTransactionResult::Success => {
trace!(target: "client", ?shard_uid, tx_hash = ?tx.get_hash(), "Recorded a transaction.");
}
InsertTransactionResult::Duplicate => {
trace!(target: "client", ?shard_uid, tx_hash = ?tx.get_hash(), "Duplicate transaction, not forwarding it.");
return Ok(ProcessTxResponse::ValidTx);
}
InsertTransactionResult::NoSpaceLeft => {
if is_forwarded {
trace!(target: "client", ?shard_uid, tx_hash = ?tx.get_hash(), "Transaction pool is full, dropping the transaction.");
} else {
trace!(target: "client", ?shard_uid, tx_hash = ?tx.get_hash(), "Transaction pool is full, trying to forward the transaction.");
}
return Ok(ProcessTxResponse::InvalidTx(err));
}
if check_only {
return Ok(ProcessTxResponse::ValidTx);
}
// Transactions only need to be recorded if the node is a validator.
if me.is_some() {
match self.chunk_producer.sharded_tx_pool.insert_transaction(shard_uid, tx.clone())
{
InsertTransactionResult::Success => {
trace!(target: "client", ?shard_uid, tx_hash = ?tx.get_hash(), "Recorded a transaction.");
}
InsertTransactionResult::Duplicate => {
trace!(target: "client", ?shard_uid, tx_hash = ?tx.get_hash(), "Duplicate transaction, not forwarding it.");
return Ok(ProcessTxResponse::ValidTx);
}
InsertTransactionResult::NoSpaceLeft => {
if is_forwarded {
trace!(target: "client", ?shard_uid, tx_hash = ?tx.get_hash(), "Transaction pool is full, dropping the transaction.");
} else {
trace!(target: "client", ?shard_uid, tx_hash = ?tx.get_hash(), "Transaction pool is full, trying to forward the transaction.");
}
}
}
}

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

if !is_forwarded {
self.possibly_forward_tx_to_next_epoch(tx, signer)?;
}
Ok(ProcessTxResponse::ValidTx)
} else if !is_forwarded {
trace!(target: "client", ?shard_id, tx_hash = ?tx.get_hash(), "Forwarding a transaction.");
metrics::TRANSACTION_RECEIVED_NON_VALIDATOR.inc();
self.forward_tx(&epoch_id, tx, signer)?;
Ok(ProcessTxResponse::RequestRouted)
} else {
trace!(target: "client", ?shard_id, tx_hash = ?tx.get_hash(), "Non-validator received a forwarded transaction, dropping it.");
metrics::TRANSACTION_RECEIVED_NON_VALIDATOR_FORWARDED.inc();
Ok(ProcessTxResponse::NoResponse)
// Active validator:
// possibly forward to next epoch validators
// Not active validator:
// forward to current epoch validators,
// possibly forward to next epoch validators
if self.active_validator(shard_id, signer)? {
trace!(target: "client", account = ?me, ?shard_id, tx_hash = ?tx.get_hash(), is_forwarded, "Recording a transaction.");
metrics::TRANSACTION_RECEIVED_VALIDATOR.inc();

if !is_forwarded {
self.possibly_forward_tx_to_next_epoch(tx, signer)?;
}
return Ok(ProcessTxResponse::ValidTx);
}
} else if check_only {
Ok(ProcessTxResponse::DoesNotTrackShard)
} else if is_forwarded {
if !is_forwarded {
trace!(target: "client", ?shard_id, tx_hash = ?tx.get_hash(), "Forwarding a transaction.");
metrics::TRANSACTION_RECEIVED_NON_VALIDATOR.inc();
self.forward_tx(&epoch_id, tx, signer)?;
return Ok(ProcessTxResponse::RequestRouted);
}
trace!(target: "client", ?shard_id, tx_hash = ?tx.get_hash(), "Non-validator received a forwarded transaction, dropping it.");
metrics::TRANSACTION_RECEIVED_NON_VALIDATOR_FORWARDED.inc();
return Ok(ProcessTxResponse::NoResponse);
}

if check_only {
return Ok(ProcessTxResponse::DoesNotTrackShard);
}
if is_forwarded {
// Received forwarded transaction but we are not tracking the shard
debug!(target: "client", ?me, ?shard_id, tx_hash = ?tx.get_hash(), "Received forwarded transaction but no tracking shard");
Ok(ProcessTxResponse::NoResponse)
} else {
// We are not tracking this shard, so there is no way to validate this tx. Just rerouting.
self.forward_tx(&epoch_id, tx, signer)?;
Ok(ProcessTxResponse::RequestRouted)
return Ok(ProcessTxResponse::NoResponse);
}
// We are not tracking this shard, so there is no way to validate this tx. Just rerouting.
self.forward_tx(&epoch_id, tx, signer).map(|()| ProcessTxResponse::RequestRouted)
Comment on lines -2330 to +2331
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original version seems more readable to me, but I’m fine with both.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I too think that the original version is easier to read however what I like about this version is that we make it explicit what forward_tx() is returning in the happy path i.e. (). And if we ever make a change there, then the compiler will help us identify all the places where the changes need to be made.

}

/// Determine if I am a validator in next few blocks for specified shard, assuming epoch doesn't change.
Expand Down
Loading