Skip to content
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

refactor: extract chunk producer logic from Client #12852

Merged
merged 15 commits into from
Feb 4, 2025

Conversation

Longarithm
Copy link
Member

Reducing number of fields in Client from 36 to 31 and number of lines in client.rs from 2913 to 2624.
This is done by separating logic exclusive to chunk production, thus new struct is named ChunkProducer. It is almost as simple as moving some methods from Client to ChunkProducer.

I had to pass chain_validate: &dyn Fn(&SignedTransaction) -> bool, through which is not great but already used in other places. Also, I remove mut keywords because the closure doesn't mutate neither environment nor variable.

Later, we can also hide produce_chunks and state witness production logic. This process is a bit heavy because currently access to the whole Client is used everywhere - which is not really needed.
Another next step can be separating BlockProducer logic.

@Longarithm Longarithm requested a review from a team as a code owner January 31, 2025 13:35
Copy link

codecov bot commented Jan 31, 2025

Codecov Report

Attention: Patch coverage is 80.68670% with 45 lines in your changes missing coverage. Please review.

Project coverage is 70.37%. Comparing base (c4c7f6d) to head (2c4a41c).
Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
chain/client/src/chunk_producer.rs 75.30% 32 Missing and 8 partials ⚠️
chain/chain/src/store/mod.rs 93.75% 0 Missing and 2 partials ⚠️
chain/chain/src/chain.rs 50.00% 0 Missing and 1 partial ⚠️
chain/client/src/client_actor.rs 0.00% 1 Missing ⚠️
chain/client/src/debug.rs 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master   #12852      +/-   ##
==========================================
+ Coverage   70.35%   70.37%   +0.01%     
==========================================
  Files         853      854       +1     
  Lines      174013   174086      +73     
  Branches   174013   174086      +73     
==========================================
+ Hits       122428   122514      +86     
+ Misses      46364    46359       -5     
+ Partials     5221     5213       -8     
Flag Coverage Δ
backward-compatibility 0.35% <0.00%> (-0.01%) ⬇️
db-migration 0.35% <0.00%> (-0.01%) ⬇️
genesis-check 1.41% <0.00%> (-0.01%) ⬇️
linux 70.06% <86.05%> (+0.03%) ⬆️
linux-nightly 69.99% <80.25%> (+<0.01%) ⬆️
pytests 1.72% <0.00%> (-0.01%) ⬇️
sanity-checks 1.52% <0.00%> (-0.01%) ⬇️
unittests 70.20% <80.68%> (+0.01%) ⬆️
upgradability 0.35% <0.00%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@shreyan-gupta shreyan-gupta left a comment

Choose a reason for hiding this comment

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

Looks good!

@@ -352,8 +368,8 @@ impl ChainStore {
/// But we need to implement a more theoretically correct algorithm if shard layouts will change
/// more often in the future
/// <https://github.com/near/nearcore/issues/4877>
pub fn get_outgoing_receipts_for_shard(
&self,
pub fn get_outgoing_receipts_for_shard_from_store(
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice! Does it make sense to push this in the same helper module we had created last time?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes. I just don't want to move too much code at once.

@@ -8,13 +8,16 @@ pub use near_client_primitives::types::{
QueryError, Status, StatusResponse, SyncStatus, TxStatus, TxStatusError,
};

pub use crate::client::{Client, ProduceChunkResult};
#[cfg(feature = "test_features")]
pub use crate::chunk_producer::AdvProduceChunksMode;
Copy link
Contributor

Choose a reason for hiding this comment

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

Checking, do we need to expose this outside the module?

@@ -6,7 +6,8 @@

use near_chain_configs::Genesis;
use near_client::test_utils::{create_chunk_on_height, TestEnv};
use near_client::{ProcessTxResponse, ProduceChunkResult};
use near_client::ProcessTxResponse;
use near_client::ProduceChunkResult;
Copy link
Contributor

Choose a reason for hiding this comment

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

mini nit: Can revert to use near_client::{ProcessTxResponse, ProduceChunkResult};? Fine to leave as is.

@@ -1966,14 +1671,16 @@ impl Client {
.with_label_values(&[&shard_id.to_string()])
.start_timer();
let last_header = Chain::get_prev_chunk_header(epoch_manager, block, shard_id).unwrap();
match self.try_produce_chunk(
let result = self.chunk_producer.produce_chunk(
Copy link
Contributor

@shreyan-gupta shreyan-gupta Jan 31, 2025

Choose a reason for hiding this comment

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

How did we determine if the parent function produce_chunks was supposed to be a part of client.rs or chunk_producer.rs module?

Copy link
Contributor

Choose a reason for hiding this comment

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

Line 1625

Copy link
Member Author

Choose a reason for hiding this comment

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

Again, I didn't want to touch it yet. The main reason is persist_and_distribute_encoded_chunk using many client fields

Copy link
Contributor

@Trisfald Trisfald left a comment

Choose a reason for hiding this comment

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

👍

let prev_prev_hash = *self.chain.get_block_header(&prev_block_hash)?.prev_hash();
if !ChainStore::prev_block_is_caught_up(&self.chain, &prev_prev_hash, &prev_block_hash)?
{
// See comment in similar snipped in `produce_block`
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: this comment is not very helpful IMO, is it possible to copy paste what is said in produce_block?

@Longarithm Longarithm enabled auto-merge February 4, 2025 15:53
@Longarithm Longarithm added this pull request to the merge queue Feb 4, 2025
Merged via the queue into near:master with commit f527efe Feb 4, 2025
29 checks passed
@Longarithm Longarithm deleted the chunk-producer branch February 4, 2025 16:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants