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: remove circular dependencies through net_processing (2/N) #5792

Merged
merged 8 commits into from
Feb 14, 2024

Conversation

knst
Copy link
Collaborator

@knst knst commented Dec 28, 2023

Issue being fixed or feature implemented

The architecture of bitcoin assumes that there's no any external class that processes network messages and knows anything about PeerManager from net_processing; no any external call for PeerManager::Misbehaving in bitcoin. All logic related to processing messages are located in net_processing.

Dash has many many extra types of network messages and many of them processed by external components such as llmq/signing or coinjoin/client. Current architecture creates multiple circular dependency.

What was done?

That's part II of refactorings.
This PR removes PeerManager from several constructor and let LLMQContext to forget about PeerManager.
Prior work in this PR: #5782

What else to do?

Some network messages are processed asynchronously in external components such as llmq/signing, llmq/instantsend, llmq/dkgsessionhandler. It doesn't let to refactor them easily, because they can't just simple return status of processing; status of processing would be available sometime later and there's need callback or other way to pass result code without spreading PeerManager over codebase.

How Has This Been Tested?

  • Run unit/functional tests
  • run a linter test/lint/lint-circular-dependencies.sh

Breaking Changes

N/A

Checklist:

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have added or updated relevant unit/integration/functional/e2e tests
  • I have made corresponding changes to the documentation
  • I have assigned this pull request to a milestone

@knst knst added this to the 20.1 milestone Dec 28, 2023
@knst knst marked this pull request as draft December 28, 2023 13:48
Copy link

github-actions bot commented Jan 2, 2024

This pull request has conflicts, please rebase.

@knst knst force-pushed the fix-circular-net-processing-p2 branch from 97053d5 to 5d3d3a8 Compare January 11, 2024 13:41
@knst knst marked this pull request as ready for review January 11, 2024 15:46
UdjinM6
UdjinM6 previously approved these changes Jan 14, 2024
Copy link

@UdjinM6 UdjinM6 left a comment

Choose a reason for hiding this comment

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

LGTM, one small suggestion which can be ignored :)

utACK

src/llmq/instantsend.cpp Outdated Show resolved Hide resolved
@UdjinM6 UdjinM6 changed the title refactor: remove circular dependencies through net_processing (2/N) refactor: remove circular dependencies through net_processing (2/N) Jan 14, 2024
@knst knst force-pushed the fix-circular-net-processing-p2 branch 2 times, most recently from d23f495 to 95972ed Compare January 15, 2024 13:23
@knst knst requested a review from UdjinM6 January 15, 2024 13:23
UdjinM6
UdjinM6 previously approved these changes Jan 15, 2024
Copy link

@UdjinM6 UdjinM6 left a comment

Choose a reason for hiding this comment

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

utACK

Copy link

This pull request has conflicts, please rebase.

@knst knst force-pushed the fix-circular-net-processing-p2 branch from b42f0e3 to afe08c0 Compare January 19, 2024 10:43
@knst knst requested a review from UdjinM6 January 19, 2024 13:32
UdjinM6
UdjinM6 previously approved these changes Jan 19, 2024
Copy link

@UdjinM6 UdjinM6 left a comment

Choose a reason for hiding this comment

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

utACK

Comment on lines +757 to +761
if (m_peerman == nullptr) {
m_peerman = peerman;
}
// we should never use one CInstantSendManager with different PeerManager
assert(m_peerman == peerman);
Copy link
Member

Choose a reason for hiding this comment

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

this scares me; why can this not be initialized in the CInstantSendManager constructor?

This feels like it introduces a data race

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

There's is already race but in constructor.

https://github.com/dashpay/dash/pull/5792/files#diff-3ce892d5580c8044e0b4ebc4d1fe596d919788682fc38ccd66913508ff8fc43dR22

Due to that LLMQContext accept an unique_ptr as an argument because PeerManager would be initialized after initializing of these classes (but required by them):

  • CDKGSessionManager (eliminated by this PR)
  • CSigningManager (eliminated by this PR)
  • CSigSharesManager (still here)
  • CInstantSendManager (eliminated by this PR)

When ProcessMessage is called first time, PeerManager is initialized; before any call of Process Message -> there's no any messages -> m_peerman is not needed because we can't ban anyone if no one send messages yet.

Copy link
Member

@PastaPastaPasta PastaPastaPasta Jan 19, 2024

Choose a reason for hiding this comment

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

Well; there's definitely still a race here if two threads call this ProcessMessage function while m_peerman is null (same in CDKGPendingMessages::PushPendingMessage)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

there's no actually race because network messages are accepted in one thread.

But it may change in future. Here's a fix for it: 7286643

That race is not happens currently because net_processing processes
incomming messages in one thread at the moment, but that can be an issue
some day.
@PastaPastaPasta
Copy link
Member

Thoughts @UdjinM6 ?

Copy link

@UdjinM6 UdjinM6 left a comment

Choose a reason for hiding this comment

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

LGTM, utACK

Copy link
Member

@PastaPastaPasta PastaPastaPasta left a comment

Choose a reason for hiding this comment

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

utACK for squash merge

@PastaPastaPasta PastaPastaPasta merged commit 4becf98 into dashpay:develop Feb 14, 2024
5 of 10 checks passed
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