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] Refactor client and client actor to move the code for block processing to client #7898

Merged
merged 8 commits into from
Oct 25, 2022

Conversation

mzhangmzz
Copy link
Contributor

@mzhangmzz mzhangmzz commented Oct 21, 2022

This PR is a pure refactoring. The context is that any processing details should be put in Client instead of ClientActor. ClientActor should just serve as a coordinator class to handle messages and check triggers and immediately pass it to Client. This is better for testing since we can't write unit test for any logic in ClientActor and also better for code readability as the logic is not scattered in two classes.

This PR only moves the part around block processing. The rest is tracked by #7899

@matklad
Copy link
Contributor

matklad commented Oct 21, 2022

ClientActor should just serve as a coordinator class to handle messages and check triggers and immediately pass it to Client.

Let'ss add this to ClientActor's docstring

chain/client/src/client.rs Outdated Show resolved Hide resolved
chain/client/src/client.rs Outdated Show resolved Hide resolved
chain/client/src/client.rs Outdated Show resolved Hide resolved
chain/client/src/client.rs Outdated Show resolved Hide resolved
Comment on lines 848 to 850
if let near_chain::Error::DBNotFoundErr(msg) = err {
debug_assert!(!msg.starts_with("BLOCK HEIGHT"), "{:?}", err);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a pre-existing sketchiness, iffing on error messages is yuck

chain/client/src/client.rs Outdated Show resolved Hide resolved
block: &Block,
was_requested: bool,
) -> Result<bool, near_chain::Error> {
let head = self.chain.head()?;
Copy link
Contributor

Choose a reason for hiding this comment

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

Aha, so here we replace unwrap_or_returns with just ?, right? Looks correct to me, given that we just log the stuff at the call-side. Also, major kuros for removing unwrap_or_return -- it was making the code quite hard to read!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep!

chain/client/src/client.rs Outdated Show resolved Hide resolved
@@ -1239,10 +1247,20 @@ impl ClientActor {
fn produce_block(&mut self, next_height: BlockHeight) -> Result<(), Error> {
let _span = tracing::debug_span!(target: "client", "produce_block", next_height).entered();
if let Some(block) = self.client.produce_block(next_height)? {
let peer_id = self.node_id.clone();
// We’ve produced the block so that counts as validated block.
Copy link
Contributor

Choose a reason for hiding this comment

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

misplaced commet

self.verify_and_rebroadcast_block(&block, was_requested, &peer_id)?;
let provenance =
if was_requested { near_chain::Provenance::SYNC } else { near_chain::Provenance::NONE };
let res = self.start_process_block(block.into(), provenance, apply_chunks_done_callback);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
let res = self.start_process_block(block.into(), provenance, apply_chunks_done_callback);
let res = self.start_process_block(block, provenance, apply_chunks_done_callback);

);
// If we produced it, we don’t need to validate it. Mark the block
// as valid.
block.mark_as_valid();
Copy link
Contributor

Choose a reason for hiding this comment

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

Note: we lost this statement, but I think this is correct, this seems to be dead code: when we have provenance == Provenance::PRODUCED, we already mark block as valid.

/// propagated in the network fast.
fn verify_and_rebroadcast_block(
&mut self,
block: &MaybeValidated<Block>,
Copy link
Contributor

Choose a reason for hiding this comment

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

Hm, the fact that we statically know that the block is not validated here, but is validatde afterwards, gives me a pause. It seems that, eg, start_process_block always receives a validated block?

Not sure if there's anythnig to fixhere, and that's definietly out of scope for the pr

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes I had the same thoughts too and also agree that it's out of the scope of this PR. I will create a github issue and look more into this.

@near-bulldozer near-bulldozer bot merged commit a5f6c7c into master Oct 25, 2022
@near-bulldozer near-bulldozer bot deleted the client_refactor branch October 25, 2022 18:45
nikurt pushed a commit that referenced this pull request Oct 25, 2022
…k processing to client (#7898)

This PR is a pure refactoring. The context is that any processing details should be put in Client instead of ClientActor. ClientActor should just serve as a coordinator class to handle messages and check triggers and immediately pass it to Client. This is better for testing since we can't write unit test for any logic in ClientActor and also better for code readability as the logic is not scattered in two classes.

This PR only moves the part around block processing. The rest is tracked by #7899
nikurt pushed a commit that referenced this pull request Nov 7, 2022
…k processing to client (#7898)

This PR is a pure refactoring. The context is that any processing details should be put in Client instead of ClientActor. ClientActor should just serve as a coordinator class to handle messages and check triggers and immediately pass it to Client. This is better for testing since we can't write unit test for any logic in ClientActor and also better for code readability as the logic is not scattered in two classes.

This PR only moves the part around block processing. The rest is tracked by #7899
nikurt pushed a commit that referenced this pull request Nov 9, 2022
…k processing to client (#7898)

This PR is a pure refactoring. The context is that any processing details should be put in Client instead of ClientActor. ClientActor should just serve as a coordinator class to handle messages and check triggers and immediately pass it to Client. This is better for testing since we can't write unit test for any logic in ClientActor and also better for code readability as the logic is not scattered in two classes.

This PR only moves the part around block processing. The rest is tracked by #7899
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.

2 participants