Skip to content

Commit

Permalink
Merge branch 'master' into contract-dist-stabilize
Browse files Browse the repository at this point in the history
  • Loading branch information
tayfunelmas authored Nov 8, 2024
2 parents 2690ea1 + 1cacbad commit 6da4f87
Show file tree
Hide file tree
Showing 16 changed files with 220 additions and 102 deletions.
9 changes: 8 additions & 1 deletion chain/chain/src/blocks_delay_tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use near_primitives::block::{Block, Tip};
use near_primitives::hash::CryptoHash;
use near_primitives::shard_layout::ShardLayout;
use near_primitives::sharding::{ChunkHash, ShardChunkHeader};
use near_primitives::stateless_validation::ChunkProductionKey;
use near_primitives::types::{BlockHeight, ShardId};
use near_primitives::views::{
BlockProcessingInfo, BlockProcessingStatus, ChainProcessingInfo, ChunkProcessingInfo,
Expand Down Expand Up @@ -104,7 +105,13 @@ impl ChunkTrackingStats {
let created_by = epoch_manager
.get_epoch_id_from_prev_block(&self.prev_block_hash)
.and_then(|epoch_id| {
epoch_manager.get_chunk_producer(&epoch_id, self.height_created, self.shard_id)
epoch_manager
.get_chunk_producer_info(&ChunkProductionKey {
epoch_id,
height_created: self.height_created,
shard_id: self.shard_id,
})
.map(|info| info.take_account_id())
})
.ok();
let request_duration = if let Some(requested_timestamp) = self.requested_timestamp {
Expand Down
14 changes: 8 additions & 6 deletions chain/chain/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use near_epoch_manager::shard_tracker::ShardTracker;
use near_epoch_manager::{EpochManager, EpochManagerHandle};
use near_primitives::block::Block;
use near_primitives::hash::CryptoHash;
use near_primitives::stateless_validation::ChunkProductionKey;
use near_primitives::test_utils::create_test_signer;
use near_primitives::types::{AccountId, NumBlocks, NumShards};
use near_primitives::utils::MaybeValidated;
Expand Down Expand Up @@ -231,12 +232,13 @@ pub fn display_chain(me: &Option<AccountId>, chain: &mut Chain, tail: bool) {
if let Some(block) = maybe_block {
for chunk_header in block.chunks().iter_deprecated() {
let chunk_producer = epoch_manager
.get_chunk_producer(
&epoch_id,
chunk_header.height_created(),
chunk_header.shard_id(),
)
.unwrap();
.get_chunk_producer_info(&ChunkProductionKey {
epoch_id,
height_created: chunk_header.height_created(),
shard_id: chunk_header.shard_id(),
})
.unwrap()
.take_account_id();
if let Ok(chunk) = chain_store.get_chunk(&chunk_header.chunk_hash()) {
debug!(
" {: >3} {} | {} | {: >10} | tx = {: >2}, receipts = {: >2}",
Expand Down
13 changes: 8 additions & 5 deletions chain/chain/src/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use near_primitives::congestion_info::CongestionInfo;
use near_primitives::hash::CryptoHash;
use near_primitives::merkle::merklize;
use near_primitives::sharding::{ShardChunk, ShardChunkHeader};
use near_primitives::stateless_validation::ChunkProductionKey;
use near_primitives::transaction::SignedTransaction;
use near_primitives::types::chunk_extra::ChunkExtra;
use near_primitives::types::{AccountId, BlockHeight, EpochId, Nonce};
Expand Down Expand Up @@ -299,11 +300,13 @@ fn validate_chunk_authorship(
&epoch_id,
&chunk_header.prev_block_hash(),
)? {
let chunk_producer = epoch_manager.get_chunk_producer(
&epoch_id,
chunk_header.height_created(),
chunk_header.shard_id(),
)?;
let chunk_producer = epoch_manager
.get_chunk_producer_info(&ChunkProductionKey {
epoch_id,
height_created: chunk_header.height_created(),
shard_id: chunk_header.shard_id(),
})?
.take_account_id();
Ok(chunk_producer)
} else {
Err(Error::InvalidChallenge)
Expand Down
62 changes: 40 additions & 22 deletions chain/chunks/src/shards_manager_actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ use near_primitives::sharding::{
PartialEncodedChunkPart, PartialEncodedChunkV2, ShardChunk, ShardChunkHeader,
TransactionReceipt,
};
use near_primitives::stateless_validation::ChunkProductionKey;
use near_primitives::transaction::SignedTransaction;
use near_primitives::types::validator_stake::ValidatorStake;
use near_primitives::types::{
Expand Down Expand Up @@ -440,11 +441,14 @@ impl ShardsManagerActor {
&self.shard_tracker,
);

let chunk_producer_account_id = self.epoch_manager.as_ref().get_chunk_producer(
&self.epoch_manager.get_epoch_id_from_prev_block(ancestor_hash)?,
height,
shard_id,
)?;
let chunk_producer_account_id = self
.epoch_manager
.get_chunk_producer_info(&ChunkProductionKey {
epoch_id: self.epoch_manager.get_epoch_id_from_prev_block(ancestor_hash)?,
height_created: height,
shard_id,
})?
.take_account_id();

// In the following we compute which target accounts we should request parts and receipts from
// First we choose a shard representative target which is either the original chunk producer
Expand Down Expand Up @@ -645,8 +649,14 @@ impl ShardsManagerActor {
return Ok(true);
}
}
let chunk_producer =
self.epoch_manager.get_chunk_producer(&epoch_id, next_chunk_height, shard_id)?;
let chunk_producer = self
.epoch_manager
.get_chunk_producer_info(&ChunkProductionKey {
epoch_id,
height_created: next_chunk_height,
shard_id,
})?
.take_account_id();
if &chunk_producer == me {
return Ok(true);
}
Expand Down Expand Up @@ -1699,11 +1709,14 @@ impl ShardsManagerActor {
let have_all_receipts = self.has_all_receipts(&prev_block_hash, entry, me)?;

let can_reconstruct = entry.parts.len() >= self.epoch_manager.num_data_parts();
let chunk_producer = self.epoch_manager.get_chunk_producer(
&epoch_id,
header.height_created(),
header.shard_id(),
)?;
let chunk_producer = self
.epoch_manager
.get_chunk_producer_info(&ChunkProductionKey {
epoch_id,
height_created: header.height_created(),
shard_id: header.shard_id(),
})?
.take_account_id();

if have_all_parts {
if self.encoded_chunks.mark_chunk_for_inclusion(&chunk_hash) {
Expand Down Expand Up @@ -1863,11 +1876,14 @@ impl ShardsManagerActor {
let shard_id = partial_encoded_chunk.header.shard_id();
let mut accounts_forwarded_to = HashSet::new();
accounts_forwarded_to.insert(me.clone());
let next_chunk_producer = self.epoch_manager.get_chunk_producer(
&epoch_id,
current_chunk_height + 1,
shard_id,
)?;
let next_chunk_producer = self
.epoch_manager
.get_chunk_producer_info(&ChunkProductionKey {
epoch_id: *epoch_id,
height_created: current_chunk_height + 1,
shard_id,
})?
.take_account_id();
for (bp, _) in block_producers {
let bp_account_id = bp.take_account_id();

Expand Down Expand Up @@ -1907,11 +1923,13 @@ impl ShardsManagerActor {
.shard_ids(&epoch_id)?
.into_iter()
.map(|shard_id| {
self.epoch_manager.get_chunk_producer(
&epoch_id,
current_chunk_height + 1,
shard_id,
)
self.epoch_manager
.get_chunk_producer_info(&ChunkProductionKey {
epoch_id: *epoch_id,
height_created: current_chunk_height + 1,
shard_id,
})
.map(|info| info.take_account_id())
})
.collect::<Result<HashSet<_>, _>>()?;
next_chunk_producers.remove(me);
Expand Down
11 changes: 9 additions & 2 deletions chain/chunks/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use near_primitives::sharding::{
EncodedShardChunk, PartialEncodedChunk, PartialEncodedChunkPart, PartialEncodedChunkV2,
ShardChunkHeader,
};
use near_primitives::stateless_validation::ChunkProductionKey;
use near_primitives::test_utils::create_test_signer;
use near_primitives::types::MerkleHash;
use near_primitives::types::{AccountId, EpochId, ShardId};
Expand Down Expand Up @@ -96,8 +97,14 @@ impl ChunkTestFixture {
let mock_shard_id: ShardId = ShardId::new(0);
let mock_epoch_id =
epoch_manager.get_epoch_id_from_prev_block(&mock_ancestor_hash).unwrap();
let mock_chunk_producer =
epoch_manager.get_chunk_producer(&mock_epoch_id, mock_height, mock_shard_id).unwrap();
let mock_chunk_producer = epoch_manager
.get_chunk_producer_info(&ChunkProductionKey {
epoch_id: mock_epoch_id,
height_created: mock_height,
shard_id: mock_shard_id,
})
.unwrap()
.take_account_id();
let signer = create_test_signer(mock_chunk_producer.as_str());
let validators: Vec<_> = epoch_manager
.get_epoch_block_producers_ordered(&EpochId::default(), &CryptoHash::default())
Expand Down
68 changes: 50 additions & 18 deletions chain/client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ use near_primitives::sharding::{
EncodedShardChunk, PartialEncodedChunk, ShardChunk, ShardChunkHeader, StateSyncInfo,
StateSyncInfoV1,
};
use near_primitives::stateless_validation::ChunkProductionKey;
use near_primitives::transaction::SignedTransaction;
use near_primitives::types::chunk_extra::ChunkExtra;
use near_primitives::types::{AccountId, ApprovalStake, BlockHeight, EpochId, NumBlocks, ShardId};
Expand Down Expand Up @@ -833,8 +834,15 @@ impl Client {
Error::ChunkProducer("Called without block producer info.".to_string())
})?;

let chunk_proposer =
self.epoch_manager.get_chunk_producer(epoch_id, next_height, shard_id).unwrap();
let chunk_proposer = self
.epoch_manager
.get_chunk_producer_info(&ChunkProductionKey {
epoch_id: *epoch_id,
height_created: next_height,
shard_id,
})
.unwrap()
.take_account_id();
if signer.validator_id() != &chunk_proposer {
debug!(target: "client",
me = ?signer.as_ref().validator_id(),
Expand Down Expand Up @@ -1429,11 +1437,14 @@ impl Client {
) -> Result<(), Error> {
let epoch_id =
self.epoch_manager.get_epoch_id_from_prev_block(chunk_header.prev_block_hash())?;
let chunk_producer = self.epoch_manager.get_chunk_producer(
&epoch_id,
chunk_header.height_created(),
chunk_header.shard_id(),
)?;
let chunk_producer = self
.epoch_manager
.get_chunk_producer_info(&ChunkProductionKey {
epoch_id,
height_created: chunk_header.height_created(),
shard_id: chunk_header.shard_id(),
})?
.take_account_id();
error!(
target: "client",
?chunk_producer,
Expand Down Expand Up @@ -1816,8 +1827,14 @@ impl Client {
for shard_id in self.epoch_manager.shard_ids(&epoch_id).unwrap() {
let next_height = block.header().height() + 1;
let epoch_manager = self.epoch_manager.as_ref();
let chunk_proposer =
epoch_manager.get_chunk_producer(&epoch_id, next_height, shard_id).unwrap();
let chunk_proposer = epoch_manager
.get_chunk_producer_info(&ChunkProductionKey {
epoch_id,
height_created: next_height,
shard_id,
})
.unwrap()
.take_account_id();
if &chunk_proposer != &validator_id {
continue;
}
Expand Down Expand Up @@ -2201,18 +2218,27 @@ impl Client {
.chain(vec![self.config.tx_routing_height_horizon * 2].into_iter())
{
let target_height = head.height + horizon - 1;
let validator =
self.epoch_manager.get_chunk_producer(epoch_id, target_height, shard_id)?;
let validator = self
.epoch_manager
.get_chunk_producer_info(&ChunkProductionKey {
epoch_id: *epoch_id,
height_created: target_height,
shard_id,
})?
.take_account_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.epoch_manager.get_chunk_producer(
next_epoch_id,
target_height,
next_shard_id,
)?;
let validator = self
.epoch_manager
.get_chunk_producer_info(&ChunkProductionKey {
epoch_id: *next_epoch_id,
height_created: target_height,
shard_id: next_shard_id,
})?
.take_account_id();
validators.insert(validator);
}
}
Expand Down Expand Up @@ -2450,8 +2476,14 @@ impl Client {
};

for i in 1..=self.config.tx_routing_height_horizon {
let chunk_producer =
self.epoch_manager.get_chunk_producer(&epoch_id, head.height + i, shard_id)?;
let chunk_producer = self
.epoch_manager
.get_chunk_producer_info(&ChunkProductionKey {
epoch_id,
height_created: head.height + i,
shard_id,
})?
.take_account_id();
if &chunk_producer == account_id {
return Ok(true);
}
Expand Down
29 changes: 20 additions & 9 deletions chain/client/src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use near_primitives::congestion_info::CongestionControl;
use near_primitives::errors::EpochError;
use near_primitives::state_sync::get_num_state_parts;
use near_primitives::stateless_validation::chunk_endorsement::ChunkEndorsement;
use near_primitives::stateless_validation::ChunkProductionKey;
use near_primitives::types::{
AccountId, BlockHeight, NumShards, ShardId, ShardIndex, ValidatorInfoIdentifier,
};
Expand Down Expand Up @@ -144,8 +145,13 @@ impl BlockProductionTracker {
chunk_included: true,
});
} else {
let chunk_producer =
epoch_manager.get_chunk_producer(epoch_id, block_height, shard_id)?;
let chunk_producer = epoch_manager
.get_chunk_producer_info(&ChunkProductionKey {
epoch_id: *epoch_id,
height_created: block_height,
shard_id,
})?
.take_account_id();
chunk_collection_info.push(ChunkCollection {
chunk_producer,
received_time: None,
Expand Down Expand Up @@ -511,11 +517,12 @@ impl ClientActorInner {
chunk_producer: self
.client
.epoch_manager
.get_chunk_producer(
block_header.epoch_id(),
block_header.height(),
chunk.shard_id(),
)
.get_chunk_producer_info(&ChunkProductionKey {
epoch_id: *block_header.epoch_id(),
height_created: block_header.height(),
shard_id: chunk.shard_id(),
})
.map(|info| info.take_account_id())
.ok(),
gas_used: chunk.prev_gas_used(),
processing_time_ms: CryptoHashTimer::get_timer_value(
Expand Down Expand Up @@ -629,8 +636,12 @@ impl ClientActorInner {
let chunk_producer = self
.client
.epoch_manager
.get_chunk_producer(&epoch_id, height, shard_id)
.map(|f| f.to_string())
.get_chunk_producer_info(&ChunkProductionKey {
epoch_id,
height_created: height,
shard_id,
})
.map(|info| info.take_account_id().to_string())
.unwrap_or_default();
if chunk_producer == validator_id {
production.chunk_production.insert(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -374,8 +374,10 @@ impl PartialWitnessActor {
) -> Result<(), Error> {
let ChunkProductionKey { shard_id, epoch_id, height_created } =
partial_witness.chunk_production_key();
let chunk_producer =
self.epoch_manager.get_chunk_producer(&epoch_id, height_created, shard_id)?;
let chunk_producer = self
.epoch_manager
.get_chunk_producer_info(&ChunkProductionKey { epoch_id, height_created, shard_id })?
.take_account_id();

// Forward witness part to chunk validators except the validator that produced the chunk and witness.
let target_chunk_validators = self
Expand Down
Loading

0 comments on commit 6da4f87

Please sign in to comment.