Skip to content

Commit

Permalink
Reduce me.as_ref() and prefer &Arc<ValidatorSigner>
Browse files Browse the repository at this point in the history
  • Loading branch information
staffik committed Jun 20, 2024
1 parent b2646ba commit b053500
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 48 deletions.
48 changes: 19 additions & 29 deletions chain/chunks/src/shards_manager_actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2096,9 +2096,10 @@ impl ShardsManagerActor {
)
.entered();
let me = self.validator_signer.get().map(|signer| signer.validator_id().clone());
let me = me.as_ref();
match request {
ShardsManagerRequestFromClient::ProcessChunkHeaderFromBlock(chunk_header) => {
if let Err(e) = self.process_chunk_header_from_block(&chunk_header, me.as_ref()) {
if let Err(e) = self.process_chunk_header_from_block(&chunk_header, me) {
warn!(target: "chunks", "Error processing chunk header from block: {:?}", e);
}
}
Expand All @@ -2116,37 +2117,30 @@ impl ShardsManagerActor {
encoded_chunk,
&merkle_paths,
outgoing_receipts,
me.as_ref(),
me,
) {
warn!(target: "chunks", "Error distributing encoded chunk: {:?}", e);
}
}
ShardsManagerRequestFromClient::RequestChunks { chunks_to_request, prev_hash } => {
self.request_chunks(chunks_to_request, prev_hash, me.as_ref())
self.request_chunks(chunks_to_request, prev_hash, me)
}
ShardsManagerRequestFromClient::RequestChunksForOrphan {
chunks_to_request,
epoch_id,
ancestor_hash,
} => self.request_chunks_for_orphan(
chunks_to_request,
&epoch_id,
ancestor_hash,
me.as_ref(),
),
} => self.request_chunks_for_orphan(chunks_to_request, &epoch_id, ancestor_hash, me),
ShardsManagerRequestFromClient::CheckIncompleteChunks(prev_block_hash) => {
self.check_incomplete_chunks(&prev_block_hash, me.as_ref())
self.check_incomplete_chunks(&prev_block_hash, me)
}
ShardsManagerRequestFromClient::ProcessOrRequestChunk {
candidate_chunk,
request_header,
prev_hash,
} => {
if let Err(err) =
self.process_partial_encoded_chunk(candidate_chunk.into(), me.as_ref())
{
if let Err(err) = self.process_partial_encoded_chunk(candidate_chunk.into(), me) {
warn!(target: "chunks", ?err, "Error processing partial encoded chunk");
self.request_chunk_single(&request_header, prev_hash, false, me.as_ref());
self.request_chunk_single(&request_header, prev_hash, false, me);
}
}
ShardsManagerRequestFromClient::ProcessOrRequestChunkForOrphan {
Expand All @@ -2155,15 +2149,13 @@ impl ShardsManagerActor {
ancestor_hash,
epoch_id,
} => {
if let Err(e) =
self.process_partial_encoded_chunk(candidate_chunk.into(), me.as_ref())
{
if let Err(e) = self.process_partial_encoded_chunk(candidate_chunk.into(), me) {
warn!(target: "chunks", "Error processing partial encoded chunk: {:?}", e);
self.request_chunks_for_orphan(
vec![request_header],
&epoch_id,
ancestor_hash,
me.as_ref(),
me,
);
}
}
Expand All @@ -2178,21 +2170,20 @@ impl ShardsManagerActor {
)
.entered();
let me = self.validator_signer.get().map(|signer| signer.validator_id().clone());
let me = me.as_ref();
match request {
ShardsManagerRequestFromNetwork::ProcessPartialEncodedChunk(partial_encoded_chunk) => {
if let Err(e) =
self.process_partial_encoded_chunk(partial_encoded_chunk.into(), me.as_ref())
if let Err(e) = self.process_partial_encoded_chunk(partial_encoded_chunk.into(), me)
{
warn!(target: "chunks", "Error processing partial encoded chunk: {:?}", e);
}
}
ShardsManagerRequestFromNetwork::ProcessPartialEncodedChunkForward(
partial_encoded_chunk_forward,
) => {
if let Err(e) = self.process_partial_encoded_chunk_forward(
partial_encoded_chunk_forward,
me.as_ref(),
) {
if let Err(e) =
self.process_partial_encoded_chunk_forward(partial_encoded_chunk_forward, me)
{
warn!(target: "chunks", "Error processing partial encoded chunk forward: {:?}", e);
}
}
Expand All @@ -2203,10 +2194,9 @@ impl ShardsManagerActor {
metrics::PARTIAL_ENCODED_CHUNK_RESPONSE_DELAY.observe(
(self.clock.now().signed_duration_since(received_time)).as_seconds_f64(),
);
if let Err(e) = self.process_partial_encoded_chunk_response(
partial_encoded_chunk_response,
me.as_ref(),
) {
if let Err(e) =
self.process_partial_encoded_chunk_response(partial_encoded_chunk_response, me)
{
warn!(target: "chunks", "Error processing partial encoded chunk response: {:?}", e);
}
}
Expand All @@ -2217,7 +2207,7 @@ impl ShardsManagerActor {
self.process_partial_encoded_chunk_request(
partial_encoded_chunk_request,
route_back,
me.as_ref(),
me,
);
}
}
Expand Down
10 changes: 5 additions & 5 deletions chain/client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,7 @@ impl Client {
last_header: ShardChunkHeader,
next_height: BlockHeight,
shard_id: ShardId,
signer: Option<Arc<ValidatorSigner>>,
signer: Option<&Arc<ValidatorSigner>>,
) -> Result<Option<ProduceChunkResult>, Error> {
let signer = signer.ok_or_else(|| {
Error::ChunkProducer("Called without block producer info.".to_string())
Expand Down Expand Up @@ -856,7 +856,7 @@ impl Client {
last_header: ShardChunkHeader,
next_height: BlockHeight,
shard_id: ShardId,
validator_signer: Arc<ValidatorSigner>,
validator_signer: &Arc<ValidatorSigner>,
) -> Result<Option<ProduceChunkResult>, Error> {
let span = tracing::Span::current();
let timer = Instant::now();
Expand Down Expand Up @@ -1676,7 +1676,7 @@ impl Client {
&& !self.sync_status.is_syncing()
&& !skip_produce_chunk
{
self.produce_chunks(&block, signer);
self.produce_chunks(&block, &signer);
} else {
info!(target: "client", "not producing a chunk");
}
Expand Down Expand Up @@ -1771,7 +1771,7 @@ impl Client {
}

// Produce new chunks
fn produce_chunks(&mut self, block: &Block, signer: Arc<ValidatorSigner>) {
fn produce_chunks(&mut self, block: &Block, signer: &Arc<ValidatorSigner>) {
let validator_id = signer.validator_id().clone();
let _span = debug_span!(
target: "client",
Expand Down Expand Up @@ -1820,7 +1820,7 @@ impl Client {
last_header.clone(),
next_height,
shard_id,
Some(signer.clone()),
Some(signer),
) {
Ok(Some(result)) => {
let shard_chunk = self
Expand Down
9 changes: 5 additions & 4 deletions chain/client/src/stateless_validation/chunk_validator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl ChunkValidator {
state_witness: ChunkStateWitness,
chain: &Chain,
processing_done_tracker: Option<ProcessingDoneTracker>,
signer: Arc<ValidatorSigner>,
signer: &Arc<ValidatorSigner>,
) -> Result<(), Error> {
let prev_block_hash = state_witness.chunk_header.prev_block_hash();
let epoch_id = self.epoch_manager.get_epoch_id_from_prev_block(prev_block_hash)?;
Expand Down Expand Up @@ -131,7 +131,7 @@ impl ChunkValidator {
send_chunk_endorsement_to_block_producers(
&chunk_header,
epoch_manager.as_ref(),
signer.as_ref(),
signer,
&network_sender,
chunk_endorsement_tracker.as_ref(),
);
Expand All @@ -153,6 +153,7 @@ impl ChunkValidator {

let runtime_adapter = self.runtime_adapter.clone();
let cache = self.main_state_transition_result_cache.clone();
let signer = signer.clone();
self.validation_spawner.spawn("stateless_validation", move || {
// processing_done_tracker must survive until the processing is finished.
let _processing_done_tracker_capture: Option<ProcessingDoneTracker> =
Expand Down Expand Up @@ -277,7 +278,7 @@ impl Client {
witness,
&block,
processing_done_tracker,
signer,
&signer,
),
Err(Error::DBNotFoundErr(_)) => {
// Previous block isn't available at the moment, add this witness to the orphan pool.
Expand Down Expand Up @@ -314,7 +315,7 @@ impl Client {
witness: ChunkStateWitness,
prev_block: &Block,
processing_done_tracker: Option<ProcessingDoneTracker>,
signer: Arc<ValidatorSigner>,
signer: &Arc<ValidatorSigner>,
) -> Result<(), Error> {
if witness.chunk_header.prev_block_hash() != prev_block.hash() {
return Err(Error::Other(format!(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl Client {
Ok(HandleOrphanWitnessOutcome::SavedToPool)
}

fn process_ready_orphan_witnesses(&mut self, new_block: &Block, signer: Arc<ValidatorSigner>) {
fn process_ready_orphan_witnesses(&mut self, new_block: &Block, signer: &Arc<ValidatorSigner>) {
let ready_witnesses = self
.chunk_validator
.orphan_witness_pool
Expand All @@ -92,12 +92,9 @@ impl Client {
witness_prev_block = ?header.prev_block_hash(),
"Processing an orphaned ChunkStateWitness, its previous block has arrived."
);
if let Err(err) = self.process_chunk_state_witness_with_prev_block(
witness,
new_block,
None,
signer.clone(),
) {
if let Err(err) =
self.process_chunk_state_witness_with_prev_block(witness, new_block, None, signer)
{
tracing::error!(target: "client", ?err, "Error processing orphan chunk state witness");
}
}
Expand All @@ -112,7 +109,7 @@ impl Client {
signer: &Option<Arc<ValidatorSigner>>,
) {
if let Some(signer) = signer {
self.process_ready_orphan_witnesses(new_block, signer.clone());
self.process_ready_orphan_witnesses(new_block, signer);
} else {
tracing::error!(target: "client", new_block=?new_block.hash(), "Cannot process ready orphan witnesses - not a validator");
}
Expand Down
4 changes: 2 additions & 2 deletions chain/client/src/test_utils/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ fn create_chunk_on_height_for_shard(
.unwrap(),
next_height,
shard_id,
signer,
signer.as_ref(),
)
.unwrap()
.unwrap()
Expand Down Expand Up @@ -192,7 +192,7 @@ pub fn create_chunk(
last_block.chunks()[0].clone(),
next_height,
0,
signer.clone(),
signer.as_ref(),
)
.unwrap()
.unwrap();
Expand Down

0 comments on commit b053500

Please sign in to comment.