Skip to content

Conversation

knst
Copy link
Collaborator

@knst knst commented Dec 21, 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?

This PR introduces several key changes:

  • new helper tl::expected that can be widely used in dash code and it has very similar implementation with std::expected and can be replaced to std::expected since C++23 is used by default. Please notice that this PR will superseed refactor: convert maybe_error into generic Result template class #5109
  • new helper PeerMsgRet that is defined as tl::expected<void, MisbehavingError> inside net_types. That lets external modules that process network messages to don't depend directly on net_processing.
  • removed dependency of many classes on net_processing thanks to new PeerMsgRet type: coinjoin/client, coinjoin/server, evo/mnauth, governance/governance, llmq/blockprocessor, llmq/chainlocks, llmq/quorums, spork

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 21, 2023
@knst knst force-pushed the fix-circular-net-processing branch 2 times, most recently from 92c7b34 to 90aa991 Compare December 21, 2023 21:39
src/net_types.h Outdated
int score;
std::string message;

MisbehavingError(int score, std::string message = "") :
Copy link
Member

Choose a reason for hiding this comment

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

I think I would propose dropping the = "" and requiring all invocations provide a message; maybe a future refac however

Copy link
Collaborator Author

@knst knst Dec 22, 2023

Choose a reason for hiding this comment

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

Most of places doesn't have a text message.
Also for case of text message it's little too verbose: need to write tl::expected(MisbehavingError>{...})

For other usages (not net_processing) I'd agree if error usually provides text description

src/net_types.h Outdated
Comment on lines 23 to 25
MisbehavingError(int score, std::string message = "") :
score{score},
message{std::move(message)}
Copy link
Member

Choose a reason for hiding this comment

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

What if we made this a std::string&& to ensure that this is (probably) a compile time constant? Thoughts? Would also need to make std::move into std::forward

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

updated, please check

@PastaPastaPasta
Copy link
Member

Otherwise LGTM

Copy link

This pull request has conflicts, please rebase.

1 similar comment
Copy link

This pull request has conflicts, please rebase.

PastaPastaPasta

This comment was marked as duplicate.

PastaPastaPasta
PastaPastaPasta previously approved these changes Jan 6, 2024
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

Copy link

This pull request has conflicts, please rebase.

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.

re-utACK; please state preference for merge commit vs squash

@knst
Copy link
Collaborator Author

knst commented Jan 10, 2024

please state preference for merge commit vs squash

either way works, commits are each atomic and complete.

One things that may worth an own commit: tl::expected but I am good with squashed

expected_storage_base() : m_has_val(true) {}

Copy link

Choose a reason for hiding this comment

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

3064790: should ignore src/util/expected.h in linters instead - 7515f9a

Copy link

This pull request has conflicts, please rebase.

@knst
Copy link
Collaborator Author

knst commented Jan 10, 2024

resolved conflicts and added expections for linter.

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
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 merging via merge commit

@PastaPastaPasta PastaPastaPasta force-pushed the fix-circular-net-processing branch from bbb342e to a9401cc Compare January 10, 2024 21:12
@PastaPastaPasta PastaPastaPasta merged commit 95fad52 into dashpay:develop Jan 10, 2024
PastaPastaPasta pushed a commit that referenced this pull request Feb 14, 2024
…5792)

## 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:
- [x] I have performed a self-review of my own code
- [x] 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
- [x] I have assigned this pull request to a milestone
DashCoreAutoGuix pushed a commit to DashCoreAutoGuix/dash that referenced this pull request Jul 7, 2025
…ction

c7c7ee9 test: Check max transaction weight in CoinSelection (Aurèle Oulès)
6b563ca wallet: Check max tx weight in coin selector (Aurèle Oulès)

Pull request description:

  This PR is an attempt to fix dashpay#5782.

  I have added 4 test scenarios, 3 of them provided here bitcoin#5782 (comment) (slightly modified to use a segwit wallet).

  Here are my benchmarks :
  ## PR
  |               ns/op |                op/s |    err% |          ins/op |          cyc/op |    IPC |         bra/op |   miss% |     total | benchmark
  |--------------------:|--------------------:|--------:|----------------:|----------------:|-------:|---------------:|--------:|----------:|:----------
  |        1,466,341.00 |              681.97 |    0.6% |   11,176,762.00 |    3,358,752.00 |  3.328 |   1,897,839.00 |    0.3% |      0.02 | `CoinSelection`

  ## Master

  |               ns/op |                op/s |    err% |          ins/op |          cyc/op |    IPC |         bra/op |   miss% |     total | benchmark
  |--------------------:|--------------------:|--------:|----------------:|----------------:|-------:|---------------:|--------:|----------:|:----------
  |        1,526,029.00 |              655.30 |    0.5% |   11,142,188.00 |    3,499,200.00 |  3.184 |   1,994,156.00 |    0.2% |      0.02 | `CoinSelection`

ACKs for top commit:
  achow101:
    reACK c7c7ee9
  w0xlt:
    ACK bitcoin@c7c7ee9
  furszy:
    diff ACK c7c7ee9

Tree-SHA512: ef0b28576ff845174651ba494aa9adee234c96e6f886d0e032eceb7050296e45b099dda0039d1dfb9944469f067627b2101f3ff855c70353cf39d1fc7ee81828
PastaPastaPasta added a commit that referenced this pull request Aug 18, 2025
…geProcessingResult`, drop `PeerMsgRet`

491ae50 revert: new util class `expected` for return errors by more convenient way (Kittywhiskers Van Gogh)
2020757 trivial: remove `PeerMsgRet` handling logic (Kittywhiskers Van Gogh)
5d1222e chore: drop govobj/govobjvote counts, use `ret.m_inventory.size()` (UdjinM6)
4822b93 refactor: allow submitting multiple `CInv`s to `MessageProcessingResult` (Kittywhiskers Van Gogh)
0517aff refactor: mark `RelayInv{,Filtered}`'s `CInv` argument as const (Kittywhiskers Van Gogh)
9084530 refactor: migrate `CGovernanceManager::ProcessMessage()` and friends (Kittywhiskers Van Gogh)
8b9bf7c refactor: migrate `CCoinJoinServer::ProcessMessage()` (Kittywhiskers Van Gogh)
1d50a1a refactor: migrate `CCoinJoinClientQueueManager::ProcessMessage()` (Kittywhiskers Van Gogh)
6352baf refactor: migrate `CMNAuth::ProcessMessage()` (Kittywhiskers Van Gogh)
9f85bd0 refactor: migrate `CInstantSendManager::ProcessMessage()` (Kittywhiskers Van Gogh)
ef4a0bb refactor: migrate `CDKGSessionManager::ProcessMessage()` and friends (Kittywhiskers Van Gogh)
93d68b8 refactor: migrate `CQuorumManager::ProcessMessage()` (Kittywhiskers Van Gogh)
1bbcb95 refactor: migrate `CSporkManager::ProcessMessage()` and friends (Kittywhiskers Van Gogh)
e7b23a2 refactor: migrate `CSigningManager::ProcessMessage()` (Kittywhiskers Van Gogh)
f3b98ce chore: add redefinition of `NodeId` to avoid repetitive redefinition (Kittywhiskers Van Gogh)
f341ef5 chore: add `nodiscard` attrib to `MessageProcessingResult` ret functions (Kittywhiskers Van Gogh)

Pull request description:

  ## Additional Information

  * Dependency for #6821

  * To avoid having to redeclare `NodeId` repeatedly to avoid circular dependencies from including `net.h` ([source](https://github.com/dashpay/dash/blob/fb87a5a937dd232d4f2ae60b5b8c5c29d49cb92f/src/net.h#L121)), a redeclaration is made in the relatively lightweight `net_types.h` that as of `develop` (fb87a5a), doesn't have includes outside the standard library ([source](https://github.com/dashpay/dash/blob/fb87a5a937dd232d4f2ae60b5b8c5c29d49cb92f/src/net_types.h#L8-L10)).

    Redeclarations were flagged during review (see [comment](#6820 (comment)), [comment](#6820 (comment))).

  * As `tl::expected` was introduced alongside `PeerMsgRet` in [dash#5782](#5782) and has not been used elsewhere, we can safely remove it as we've finished migrating all usage to `MessageProcessingResult`.

  ## Breaking Changes

  None expected.

  ## Checklist

  - [x] I have performed a self-review of my own code
  - [x] I have commented my code, particularly in hard-to-understand areas **(note: N/A)**
  - [x] I have added or updated relevant unit/integration/functional/e2e tests
  - [x] I have made corresponding changes to the documentation **(note: N/A)**
  - [x] I have assigned this pull request to a milestone _(for repository code-owners and collaborators only)_

ACKs for top commit:
  knst:
    utACK 491ae50
  UdjinM6:
    utACK 491ae50

Tree-SHA512: c37fe6370555f1c33877fa5a335356e2febf25c2d0d954fa44623f49994647ea2a10aec5ceb9e788bb6fa9315dc00b3e54ca8e0da96c7899fb31c30b888eb732
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