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

Reusable Diffusion - Make diffusion fully polymorphic #5016

Open
wants to merge 17 commits into
base: main
Choose a base branch
from

Conversation

bolt12
Copy link
Contributor

@bolt12 bolt12 commented Nov 27, 2024

Description

This PR completely refactors ouroboros-network in order to make diffusion layer fully general/polymorphic and extensible. This will allow 3rd party users to instantiate their own diffusion and their own diffusion parameters, creating their own overlay network.

More details about this refactor refer to https://github.com/IntersectMBO/ouroboros-network/wiki/Reusable-Diffusion-Investigation

TODOs:

  • Better document PRP invariants and refactor/cleanup insertion functions
  • remove daBlockFetchMode
  • Add tests for the general components: Churn, OG, etc..
  • Generalise peerChurnGovernor to only have one implementation and not 2
  • Consensus uses a entirely different project for Cardano Node consensus layer instantiation, decide what's the best approach for the networking code.

@bolt12 bolt12 added the diffusion Issues / PRs related to diffusion layer label Nov 27, 2024
@bolt12 bolt12 self-assigned this Nov 27, 2024
@bolt12 bolt12 requested a review from a team as a code owner November 27, 2024 13:10
@bolt12 bolt12 force-pushed the bolt12/reusable-diffusion-3 branch 6 times, most recently from 3cdb9a3 to f00952f Compare November 27, 2024 16:53
Comment on lines 26 to 30
Cardano.Node.ConsensusMode
Cardano.Node.PeerSelection.Bootstrap
Cardano.Node.PeerSelection.LocalRootPeers
Cardano.Node.PeerSelection.PeerTrustable
Cardano.Node.Types
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Check what consensus uses.

Copy link
Contributor

Choose a reason for hiding this comment

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

What did you decide to use?

ouroboros-network/ouroboros-network.cabal Outdated Show resolved Hide resolved
ouroboros-network/src/Cardano/Node/PublicRootPeers.hs Outdated Show resolved Hide resolved
ouroboros-network/src/Ouroboros/Network/Diffusion/P2P.hs Outdated Show resolved Hide resolved
ouroboros-network/src/Cardano/PeerSelection/Churn.hs Outdated Show resolved Hide resolved
@bolt12 bolt12 force-pushed the bolt12/reusable-diffusion-3 branch from c14d5cb to 3b30f43 Compare December 2, 2024 16:42
@yihuang
Copy link

yihuang commented Dec 4, 2024

a step toward haskell blockchain sdk? 😄

@bolt12
Copy link
Contributor Author

bolt12 commented Dec 5, 2024

@yihuang as you can see from the PR description this is some interesting step towards a fully polymorphic diffusion that might be used by others to create their own overlay network! Thank you for noticing 😃

@bolt12 bolt12 force-pushed the bolt12/reusable-diffusion-3 branch 6 times, most recently from f922210 to 3cdf98d Compare December 9, 2024 10:09
bolt12 added 5 commits January 6, 2025 13:56
There's a cyclic dependency issue in this commit that is fixed in the
next one. The problem is that I pulled out `ConsensusModePeerTargets`
which depend on `PeerSelectionTargets` and then the module that defines
`PeerSelectionTargets` requires `ConsensusModePeerTargets`. This is
fixed by abstracting over Cardano Node specific types such as
`ConsensusModePeerTargets`.
Moved Diffusion Cardano specifics to their own folder, separating
utilities and functions accordingly.

Changed Diffusion selection by splitting P2P in two: P2PCardano and P2P.
The former instantiates diffusion using Cardano Node specifics. The
latter is able to accommodate with other use cases.

Added a Minimal/Node.hs that is incomplete but will hold the minimal
Node diffusion instantiation example that highlights how one can use
their own custom types.
- Removed PeerSelectionActionsArgs: This data type was bit redundant.
- Reorganized DNSActions and LedgerPeersArgs
- Improved API for PeerSelectionActions
@bolt12 bolt12 force-pushed the bolt12/reusable-diffusion-3 branch 3 times, most recently from fb13f01 to bd855c2 Compare January 7, 2025 11:30
@bolt12 bolt12 force-pushed the bolt12/reusable-diffusion-3 branch from bd855c2 to ce99e1d Compare January 7, 2025 11:39
bolt12 added 12 commits January 7, 2025 12:07
Make PeerSelection{Views, Counters} extensible by third party users.

Generalised PublicRootPeers

First refactor of Outbound Governor
- Generalize MinimalP2P Module
  - `MinimalP2P` is _the_ module for diffusion initialization.
  - Updated parameter plumbing to support generalization and
    polymorphism.

- Enhance ArgumentsExtra
  - Added additional parameters to `ArgumentsExtra` to facilitate a more
    general and polymorphic diffusion setup. This includes
    `requestPublicRootPeers`, `peerChurnGovernor`, etc..

- Refactor PeerSelection.Governor.Monitor
  - Moved Cardano-specific monitoring actions to a separate module.
  - Implemented general `localRoots` and `targetPeers` monitoring
    actions.
  - Pulled out Cardano specific implementation details that allowed to
    generalise the minimal set of monitoring actions. Added fields to
    `ExtraGuardedDecisions` so that 3rd party users can pass their own
    `localRoots` and `targetPeers` actions and also backdoors/callbacks
    like `abortGovernor`, `updateWithState` to not only support Cardano
    specific needs but to give more power/flexibility to 3rd party
    users.

- Polymorphize Diffusion Module
  - Removed `CardanoP2P` dependencies from the `Diffusion` module.
  - Ensured the `Diffusion` module remains fully polymorphic and
    adaptable to different use cases.
  - Refactored test suite to compile with new changes.
Make addresses polymorphic
@bolt12 bolt12 force-pushed the bolt12/reusable-diffusion-3 branch from ce99e1d to 5400a7c Compare January 7, 2025 12:07
Comment on lines +194 to +196
instance ( Ord peeraddr
, extraPeers ~ CardanoPublicRootPeers peeraddr
) => Semigroup (PublicRootPeers extraPeers peeraddr) where
Copy link
Contributor

@crocodile-dentist crocodile-dentist Jan 7, 2025

Choose a reason for hiding this comment

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

Suggested change
instance ( Ord peeraddr
, extraPeers ~ CardanoPublicRootPeers peeraddr
) => Semigroup (PublicRootPeers extraPeers peeraddr) where
instance ( Ord peeraddr
) => Semigroup (PublicRootPeers (CardanoPublicRootPeers peeraddr) peeraddr) where

Copy link
Contributor

Choose a reason for hiding this comment

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

ref: cb0da21

Comment on lines +199 to +201
instance ( Ord peeraddr
, extraPeers ~ CardanoPublicRootPeers peeraddr
) => Monoid (PublicRootPeers extraPeers peeraddr) where
Copy link
Contributor

Choose a reason for hiding this comment

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

same suggestion as above.

Copy link
Contributor

@coot coot left a comment

Choose a reason for hiding this comment

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

I reviewed the first two patches:

git hash commit title
bae4f75 Pull out Cardano specifics to different modules
cb0da21 Add extension points to Diffusion data structures

Comment on lines 26 to 30
Cardano.Node.ConsensusMode
Cardano.Node.PeerSelection.Bootstrap
Cardano.Node.PeerSelection.LocalRootPeers
Cardano.Node.PeerSelection.PeerTrustable
Cardano.Node.Types
Copy link
Contributor

Choose a reason for hiding this comment

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

What did you decide to use?

Comment on lines 25 to 26
newtype MinBigLedgerPeersForTrustedState =
MinBigLedgerPeersForTrustedState { getMinBigLedgerPeersForTrustedState :: Int }
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
newtype MinBigLedgerPeersForTrustedState =
MinBigLedgerPeersForTrustedState { getMinBigLedgerPeersForTrustedState :: Int }
newtype NumberOfBigLedgerPeers =
NumberOfBigLedgerPeers { getNumberOfBigLedgerPeers :: Int }

the rest is explained in the haddock.

targetNumberOfRootPeers = 1,
targetNumberOfKnownPeers = 1,
targetNumberOfEstablishedPeers = 1,
targetNumberOfActivePeers = 1,
Copy link
Contributor

Choose a reason for hiding this comment

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

why the targets are changed?

ref: cb0da21

Comment on lines 1677 to 1680
targetNumberOfRootPeers = 0,
targetNumberOfKnownPeers = 0,
targetNumberOfEstablishedPeers = 0,
targetNumberOfActivePeers = 0,
Copy link
Contributor

Choose a reason for hiding this comment

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

Why the targets are changed?

ref: cb0da21

Comment on lines 423 to 424
, caeConsensusMode = aConsensusMode na
, caePeerTargets = aPeerTargets na
Copy link
Contributor

Choose a reason for hiding this comment

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

we should drop the a prefix

Suggested change
, caeConsensusMode = aConsensusMode na
, caePeerTargets = aPeerTargets na
, caeConsensusMode = consensusMode na
, caePeerTargets = peerTargets na

ref: cb0da21

=> Set peeraddr
-> PublicRootPeers extraPeers peeraddr
fromLedgerPeers lp =
(empty mempty) { getLedgerPeers = lp }
Copy link
Contributor

Choose a reason for hiding this comment

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

empty mempty sounds a bit like Humpty Dumpty 😁

Comment on lines +194 to +196
instance ( Ord peeraddr
, extraPeers ~ CardanoPublicRootPeers peeraddr
) => Semigroup (PublicRootPeers extraPeers peeraddr) where
Copy link
Contributor

Choose a reason for hiding this comment

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

ref: cb0da21

Comment on lines 12 to 17
data CardanoPublicRootPeers peeraddr =
CardanoPublicRootPeers
{ cprpGetPublicConfigPeers :: !(Map peeraddr PeerAdvertise)
, cprpGetBootstrapPeers :: !(Set peeraddr)
}
deriving (Eq, Show, Typeable)
Copy link
Contributor

Choose a reason for hiding this comment

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

This type should be called CardanoExtraPeers, and then we could provide & use the following:

type CardanoPublicRootPeers peeraddr = PublicRootPeers (CardanoExtraPeers peeraddr) peeraddr 

ref: bae4f75

Comment on lines 13 to 14
data CardanoPeerSelectionState =
CardanoPeerSelectionState {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd call this CardanoExtraState and then we can have & use

type CardanoPeerSelectionState = PeerSelectionState CardanoExtraState ...

ref: bae4f75

Comment on lines 101 to 106
-> PeerSelectionActionsArgs (CardanoPeerSelectionActions m) (CardanoLedgerPeersConsensusInterface m) (CardanoPublicRootPeers peeraddr) PeerTrustable peeraddr peerconn exception m
-> WithLedgerPeersArgs (CardanoLedgerPeersConsensusInterface m) m
-> PeerSelectionActionsDiffusionMode peeraddr peerconn m
-> ( (Async m Void, Async m Void)
-> PeerSelectionActions peeraddr peerconn m
-> PeerSelectionActions (CardanoPeerSelectionActions m) (CardanoPublicRootPeers peeraddr) PeerTrustable (CardanoLedgerPeersConsensusInterface m) peeraddr peerconn m
-> m a)
Copy link
Contributor

Choose a reason for hiding this comment

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

With some type aliases this could look simpler:

  -> CardanoPeerSelectionActionsArgs ...
  -> WithLedgerPeersArgs (CardanoLedgerPeersConsensusInterface m) m
  -> PeerSelectionActionsDiffusionMode peeraddr peerconn m
  -> (   (Async m Void, Async m Void)
      -> CardanoPeerSelectionActions ...
      -> m a)

ref: cb0da21

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
diffusion Issues / PRs related to diffusion layer
Projects
Status: In Review
Development

Successfully merging this pull request may close these issues.

4 participants