From 2539a5aa698539edab154ef9d04777528262d6f5 Mon Sep 17 00:00:00 2001 From: Nikolay Kurtov Date: Mon, 27 Nov 2023 18:50:40 +0100 Subject: [PATCH] use header_head to determine the validators to forward transactions to --- chain/chain/src/chain.rs | 19 ------------------- chain/client/src/client.rs | 11 +++++++---- chain/client/src/view_client.rs | 10 +++++++++- 3 files changed, 16 insertions(+), 24 deletions(-) diff --git a/chain/chain/src/chain.rs b/chain/chain/src/chain.rs index 1c5f506fb9e..e00a90ea0a5 100644 --- a/chain/chain/src/chain.rs +++ b/chain/chain/src/chain.rs @@ -3776,25 +3776,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 { - 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 { - 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], diff --git a/chain/client/src/client.rs b/chain/client/src/client.rs index cc0eb9cec1a..cce202f5a96 100644 --- a/chain/client/src/client.rs +++ b/chain/client/src/client.rs @@ -2003,24 +2003,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); } diff --git a/chain/client/src/view_client.rs b/chain/client/src/view_client.rs index 080238a83d5..420adbb4bc7 100644 --- a/chain/client/src/view_client.rs +++ b/chain/client/src/view_client.rs @@ -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, @@ -526,7 +527,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),