Skip to content

Commit

Permalink
fix: use header_head to determine validators for forwarding transacti…
Browse files Browse the repository at this point in the history
…ons (#10256)

Functions `find_chunk_producer_for_forwarding()`,
`find_validator_for_forwarding()` are not used much, one calls another,
and they both call `chain.head()`.

That seems unnecessary. Refactored to call `get_chunk_producer()`
directly, and not worry about 3 layers of functions using different
types of heads.

See #10251 for context

(cherry picked from commit 3a33ff5)
  • Loading branch information
nikurt committed Dec 6, 2023
1 parent b0c5d14 commit 873a8d2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 24 deletions.
19 changes: 0 additions & 19 deletions chain/chain/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3703,25 +3703,6 @@ impl Chain {
Ok(FinalExecutionOutcomeWithReceiptView { final_outcome, receipts })
}

/// Find a validator to forward transactions to
pub fn find_chunk_producer_for_forwarding(
&self,
epoch_id: &EpochId,
shard_id: ShardId,
horizon: BlockHeight,
) -> Result<AccountId, Error> {
let head = self.head()?;
let target_height = head.height + horizon - 1;
Ok(self.epoch_manager.get_chunk_producer(epoch_id, target_height, shard_id)?)
}

/// Find a validator that is responsible for a given shard to forward requests to
pub fn find_validator_for_forwarding(&self, shard_id: ShardId) -> Result<AccountId, Error> {
let head = self.head()?;
let epoch_id = self.epoch_manager.get_epoch_id_from_prev_block(&head.last_block_hash)?;
self.find_chunk_producer_for_forwarding(&epoch_id, shard_id, TX_ROUTING_HEIGHT_HORIZON)
}

pub fn check_blocks_final_and_canonical(
&self,
block_headers: &[&BlockHeader],
Expand Down
11 changes: 7 additions & 4 deletions chain/client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1884,24 +1884,27 @@ impl Client {
fn forward_tx(&self, epoch_id: &EpochId, tx: &SignedTransaction) -> Result<(), Error> {
let shard_id =
self.epoch_manager.account_id_to_shard_id(&tx.transaction.signer_id, epoch_id)?;
let head = self.chain.head()?;
// Use the header head to make sure the list of validators is as
// up-to-date as possible.
let head = self.chain.header_head()?;
let maybe_next_epoch_id = self.get_next_epoch_id_if_at_boundary(&head)?;

let mut validators = HashSet::new();
for horizon in
(2..=TX_ROUTING_HEIGHT_HORIZON).chain(vec![TX_ROUTING_HEIGHT_HORIZON * 2].into_iter())
{
let target_height = head.height + horizon - 1;
let validator =
self.chain.find_chunk_producer_for_forwarding(epoch_id, shard_id, horizon)?;
self.epoch_manager.get_chunk_producer(epoch_id, target_height, shard_id)?;
validators.insert(validator);
if let Some(next_epoch_id) = &maybe_next_epoch_id {
let next_shard_id = self
.epoch_manager
.account_id_to_shard_id(&tx.transaction.signer_id, next_epoch_id)?;
let validator = self.chain.find_chunk_producer_for_forwarding(
let validator = self.epoch_manager.get_chunk_producer(
next_epoch_id,
target_height,
next_shard_id,
horizon,
)?;
validators.insert(validator);
}
Expand Down
10 changes: 9 additions & 1 deletion chain/client/src/view_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::{
};
use actix::{Actor, Addr, Handler, SyncArbiter, SyncContext};
use near_async::messaging::CanSend;
use near_chain::chain::TX_ROUTING_HEIGHT_HORIZON;
use near_chain::types::{RuntimeAdapter, Tip};
use near_chain::{
get_epoch_block_producers_view, Chain, ChainGenesis, ChainStoreAccess, DoomslugThresholdMode,
Expand Down Expand Up @@ -475,7 +476,14 @@ impl ViewClientActor {
.epoch_manager
.account_id_to_shard_id(&signer_account_id, &head.epoch_id)
.map_err(|err| TxStatusError::InternalError(err.to_string()))?;
let validator = self.chain.find_validator_for_forwarding(target_shard_id)?;
let validator = self
.epoch_manager
.get_chunk_producer(
&head.epoch_id,
head.height + TX_ROUTING_HEIGHT_HORIZON - 1,
target_shard_id,
)
.map_err(|err| TxStatusError::ChainError(err.into()))?;

self.network_adapter.send(PeerManagerMessageRequest::NetworkRequests(
NetworkRequests::TxStatus(validator, signer_account_id, tx_hash),
Expand Down

0 comments on commit 873a8d2

Please sign in to comment.