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

[P2P] Refactor P2P interfaces #558

Merged
merged 54 commits into from
Mar 21, 2023
Merged

[P2P] Refactor P2P interfaces #558

merged 54 commits into from
Mar 21, 2023

Conversation

bryanchriswhite
Copy link
Contributor

@bryanchriswhite bryanchriswhite commented Mar 2, 2023

@Reviewer

This PR may be more digestible / reviewable on a commit-by-commit basis. Commits are organized logically and any given line is only modified in a single commit, with few exceptions.


Description

Redifines P2P module packages in terms of new interfaces:

  • Peer / PeerList
  • Peerstore
  • PeerManager / PeersView

This has the effect of decoupling the Network implementation (e.g. rainTreeNetwork) from the peer representation and storage/lookup. This will be necessary for use with the libp2p based P2P module as it uses different implementations for these concerns.

Organization

This PR adds a temporary shared/p2p package to facilitate the migration to the new libp2p based P2P module implementation. This package will be re-consolidated into the remaining P2P module as part of #554.

├── p2p
│   └── * addrbook_delta_test.go -> peer_test.go
└── shared
    └── p2p
        ├── + peer.go
        ├── + peer_manager.go
        └── + peerstore.go

peer.go

peerstore.go

  • Peerstore interface plus a simple map-based implementation, PeerAddrMap

peer_manager.go

  • PeerManager and PeerView interfaces
  • SortedPeerManager implementation factored out of the same, raintree.peersManager
    • decouples sorting and "view construction" (networkView) behavior from any conception of raintree specific network features (e.g. "levels")

Nomenclature changes

AddrBook --> Peerstore: The word "address" is becoming ambiguous as libp2p nomenclature uses it to refer to the network address (e.g. IP / hostname). As it's currently used, this object holds more than just the both identity and network address so regardless of interpretation, I would argue that AddrBook doesn't accurately convey its responsibility. Peerstore also better aligns with other libp2p nomenclature and should result it in more consistent and coherent integration and refactoring going forward.

Potential next steps

  1. Decouple peer connection management from identity/address storage and lookup.
    • Add a ConnManager interface to be responsible for this
      • Move Peer#GetStream() to it
      • Move PeerManager#GetSelfAddr() to it
      • Refactor accordingly

Issue

Fixes #552

Type of change

Please mark the relevant option(s):

  • New feature, functionality or library
  • Bug fix
  • Code health or cleanup
  • Major breaking change
  • Documentation
  • Other

List of changes

  • Add Peer and Peerstore interfaces
  • Refactor AddrBook to PeerList type
  • Refactor getAddrBookDelta to be a member of PeerList
  • Move typesP2P.AddrBookMap to sharedP2P.PeerAddrMap and refactor to implement the new Peerstore interface
  • Factor SortedPeerManager out of raintree.peersManager and add peerManager interface
  • Refactor raintree.peersManager to use SortedPeerManager and implement PeerManager interface
  • Refactor stdnetwork.Network implementation to use P2P interfaces
  • Refactor AddrBookProvider to use new P2P interfaces
  • Refactor typesP2P.Network to use new P2P interfaces
  • Refactor typesP2P.Transport to embed io.ReadWriteCloser
  • Rename NetworkPeer#Dialer to NetworkPeer#Transportfor readability and consistency
  • Refactor typesP2P.NetworkPeer to implement the new Peer interface
  • Refactor debug CLI to use new P2P interfaces

Testing

  • make develop_test
  • LocalNet w/ all of the steps outlined in the README

Required Checklist

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have tested my changes using the available tooling
  • I have updated the corresponding CHANGELOG

If Applicable Checklist

  • I have updated the corresponding README(s); local and/or global
  • I have added tests that prove my fix is effective or that my feature works
  • I have added, or updated, mermaid.js diagrams in the corresponding README(s)
  • I have added, or updated, documentation and mermaid.js diagrams in shared/docs/* if I updated shared/*README(s)

@bryanchriswhite bryanchriswhite added the p2p P2P specific changes label Mar 2, 2023
@bryanchriswhite bryanchriswhite self-assigned this Mar 2, 2023
@bryanchriswhite bryanchriswhite changed the title Chore/p2p interfaces [P2P] Refactor P2P interfaces Mar 2, 2023
@bryanchriswhite bryanchriswhite force-pushed the chore/p2p-interfaces branch 7 times, most recently from 4ad4965 to 278558e Compare March 6, 2023 13:17
@bryanchriswhite bryanchriswhite marked this pull request as ready for review March 6, 2023 13:59
@bryanchriswhite bryanchriswhite force-pushed the chore/p2p-interfaces branch 2 times, most recently from 0343d5e to ec7dbe8 Compare March 6, 2023 21:04
@bryanchriswhite bryanchriswhite marked this pull request as draft March 8, 2023 10:42
The new `Peer` interface defines a `GetStream()` method, which returns `io.ReadWriteCloser`.
Currently, this must be exposed via the `Transport` interface.
This change:
  - renames `Transport#Read()` to `Transrport#ReadAll()`
  - embeds `io.ReadWriteCloser` in the `Transport` interface
  - implements `io.ReadWriteCloser` in the `tcpConn` implementation
Copy link
Member

@Olshansk Olshansk left a comment

Choose a reason for hiding this comment

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

@bryanchriswhite I didn't do a super deep dive, but mostly looks good.

Overall, the PR got pretty big with lots of renaming and things moving around, so it is getting a little hard to maintain overall context. No actionable feedback but just FYI that I don't think my review on this one is as best as it could be.

@bryanchriswhite bryanchriswhite force-pushed the chore/p2p-interfaces branch 2 times, most recently from 98b4761 to 1665e5b Compare March 17, 2023 13:55
* pokt/main:
  [Libp2p] post-merge review improvements (#590)
  Add call # to devlog4.md title
  chore: add `godoc` comment requirement (#587)
)

func TestSortedPeersView_Remove(t *testing.T) {
t.Skip("TECHDEBT(554): test that this method works as expected when target peer/addr is not in the list!")
Copy link
Member

Choose a reason for hiding this comment

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

Nice

Copy link
Member

@Olshansk Olshansk left a comment

Choose a reason for hiding this comment

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

@bryanchriswhite I left a couple replies regarding that we could/should potentially add here. Use your best judgment to do what you deem is appropriate. Approving since it is not a blocker, since you will likely revisit a lot of it in the followup PR and because I'm getting a bit of "PR fatigue" from this one :)

* pokt/main:
  [Tooling] Output changelog validation check into the PR automatically (#596)
  [debug client] Limit key import concurrency to 4 (#591)
  [SHARED][CLI] adds message routing type field to debug messages and CLI commands (#561)
@bryanchriswhite
Copy link
Contributor Author

Thanks for bearing with me @Olshansk 🙏 I think we're almost done with the necessarily large PRs (after #576 😅).

FWIW, after reflecting on my most recent experience, I've noticed one heuristic I seemed to be using when thinking about if a PR like this can be split seems to boil down the atomicity of commits with respect to the test suite. I.e., can the commits be organized such that there is a commit at which the tests are passing prior to the conclusion of the main change? If the answer is yes, then you have a decent fist estimation for how/where to make a split.

@bryanchriswhite bryanchriswhite merged commit ce4ec9e into main Mar 21, 2023
@Olshansk Olshansk deleted the chore/p2p-interfaces branch March 21, 2023 17:33
@Olshansk
Copy link
Member

can the commits be organized such that there is a commit at which the tests are passing prior to the conclusion of the main change? If the answer is yes, then you have a decent fist estimation for how/where to make a split.

giphy

dylanlott pushed a commit that referenced this pull request Mar 24, 2023
## @Reviewer

This PR may be more digestible / reviewable on a commit-by-commit basis.
Commits are organized logically and any given line is only modified in a
single commit, with few exceptions.

---

## Description

Redifines P2P module packages in terms of new interfaces:
- `Peer` / `PeerList`
- `Peerstore`
- `PeerManager` / `PeersView`

This has the effect of decoupling the `Network` implementation (e.g.
`rainTreeNetwork`) from the peer representation and storage/lookup. This
will be necessary for use with the libp2p based P2P module as it uses
different implementations for these concerns.

### Organization

This PR adds a *temporary* `shared/p2p` package to facilitate the
migration to the new libp2p based P2P module implementation. This
package will be re-consolidated into the remaining P2P module as part of
#554.

```
├── p2p
│   └── * addrbook_delta_test.go -> peer_test.go
└── shared
    └── p2p
        ├── + peer.go
        ├── + peer_manager.go
        └── + peerstore.go
```

#### peer.go

- `Peer` interface
- abstracted from
[`typesP2P.NetworkPeer`](https://github.com/pokt-network/pocket/blob/main/p2p/types/network_peer.go#L7)
and its usage
- `PeerList` concrete type (`[]Peer`)
  - implements a `#Delta()` method
- ported from
[`GetAddrBookDelta`](https://github.com/pokt-network/pocket/blob/main/p2p/addrbook_delta.go#L6)


#### peerstore.go

- `Peerstore` interface plus a simple map-based implementation,
`PeerAddrMap`

#### peer_manager.go

- `PeerManager` and `PeerView` interfaces
- abstracted from
[`raintree.peersManager`](https://github.com/pokt-network/pocket/blob/main/p2p/raintree/peers_manager.go#L15)
and its usage
- `SortedPeerManager` implementation factored out of the same,
`raintree.peersManager`
- decouples sorting and "view construction"
([`networkView`](https://github.com/pokt-network/pocket/blob/main/p2p/raintree/peers_manager.go#L181))
behavior from any conception of raintree specific network features (e.g.
"levels")

### Nomenclature changes

`AddrBook` --> `Peerstore`: The word "address" is becoming ambiguous as
libp2p nomenclature uses it to refer to the network address (e.g. IP /
hostname). As it's currently used, this object holds more than just the
both identity and network address so regardless of interpretation, I
would argue that `AddrBook` doesn't accurately convey its
responsibility. `Peerstore` also better aligns with other libp2p
nomenclature and should result it in more consistent and coherent
integration and refactoring going forward.

### Potential next steps 

1. Decouple peer connection management from identity/address storage and
lookup.
    - Add a `ConnManager` interface to be responsible for this
      - Move `Peer#GetStream()` to it
      - Move `PeerManager#GetSelfAddr()` to it
      - Refactor accordingly


## Issue

Fixes #552 

## Type of change

Please mark the relevant option(s):

- [ ] New feature, functionality or library
- [ ] Bug fix
- [x] Code health or cleanup
- [ ] Major breaking change
- [ ] Documentation
- [ ] Other <!-- add details here if it a different type of change -->

## List of changes

- Add `Peer` and `Peerstore` interfaces
- Refactor `AddrBook` to `PeerList` type
- Refactor `getAddrBookDelta` to be a member of `PeerList`
- Move `typesP2P.AddrBookMap` to `sharedP2P.PeerAddrMap` and refactor to
implement the new `Peerstore` interface
- Factor `SortedPeerManager` out of `raintree.peersManager` and add
`peerManager` interface
- Refactor `raintree.peersManager` to use `SortedPeerManager` and
implement `PeerManager` interface
- Refactor `stdnetwork.Network` implementation to use P2P interfaces
- Refactor `AddrBookProvider` to use new P2P interfaces
- Refactor `typesP2P.Network` to use new P2P interfaces
- Refactor `typesP2P.Transport` to embed `io.ReadWriteCloser`
- Rename `NetworkPeer#Dialer` to `NetworkPeer#Transport`for readability
and consistency
- Refactor `typesP2P.NetworkPeer` to implement the new `Peer` interface
- Refactor debug CLI to use new P2P interfaces


## Testing

- [x] `make develop_test`
- [x]
[LocalNet](https://github.com/pokt-network/pocket/blob/main/docs/development/README.md)
w/ all of the steps outlined in the `README`

<!-- REMOVE this comment block after following the instructions
 If you added additional tests or infrastructure, describe it here.
 Bonus points for images and videos or gifs.
-->

## Required Checklist

- [x] I have performed a self-review of my own code
- [x] I have commented my code, particularly in hard-to-understand areas
- [x] I have tested my changes using the available tooling
- [x] I have updated the corresponding CHANGELOG

### If Applicable Checklist

- [ ] I have updated the corresponding README(s); local and/or global
- [x] I have added tests that prove my fix is effective or that my
feature works
- [ ] I have added, or updated,
[mermaid.js](https://mermaid-js.github.io) diagrams in the corresponding
README(s)
- [ ] I have added, or updated, documentation and
[mermaid.js](https://mermaid-js.github.io) diagrams in `shared/docs/*`
if I updated `shared/*`README(s)
dylanlott pushed a commit to dylanlott/pocket that referenced this pull request Mar 24, 2023
This PR may be more digestible / reviewable on a commit-by-commit basis.
Commits are organized logically and any given line is only modified in a
single commit, with few exceptions.

---

Redifines P2P module packages in terms of new interfaces:
- `Peer` / `PeerList`
- `Peerstore`
- `PeerManager` / `PeersView`

This has the effect of decoupling the `Network` implementation (e.g.
`rainTreeNetwork`) from the peer representation and storage/lookup. This
will be necessary for use with the libp2p based P2P module as it uses
different implementations for these concerns.

This PR adds a *temporary* `shared/p2p` package to facilitate the
migration to the new libp2p based P2P module implementation. This
package will be re-consolidated into the remaining P2P module as part of

```
├── p2p
│   └── * addrbook_delta_test.go -> peer_test.go
└── shared
    └── p2p
        ├── + peer.go
        ├── + peer_manager.go
        └── + peerstore.go
```

- `Peer` interface
- abstracted from
[`typesP2P.NetworkPeer`](https://github.com/pokt-network/pocket/blob/main/p2p/types/network_peer.go#L7)
and its usage
- `PeerList` concrete type (`[]Peer`)
  - implements a `#Delta()` method
- ported from
[`GetAddrBookDelta`](https://github.com/pokt-network/pocket/blob/main/p2p/addrbook_delta.go#L6)

- `Peerstore` interface plus a simple map-based implementation,
`PeerAddrMap`

- `PeerManager` and `PeerView` interfaces
- abstracted from
[`raintree.peersManager`](https://github.com/pokt-network/pocket/blob/main/p2p/raintree/peers_manager.go#L15)
and its usage
- `SortedPeerManager` implementation factored out of the same,
`raintree.peersManager`
- decouples sorting and "view construction"
([`networkView`](https://github.com/pokt-network/pocket/blob/main/p2p/raintree/peers_manager.go#L181))
behavior from any conception of raintree specific network features (e.g.
"levels")

`AddrBook` --> `Peerstore`: The word "address" is becoming ambiguous as
libp2p nomenclature uses it to refer to the network address (e.g. IP /
hostname). As it's currently used, this object holds more than just the
both identity and network address so regardless of interpretation, I
would argue that `AddrBook` doesn't accurately convey its
responsibility. `Peerstore` also better aligns with other libp2p
nomenclature and should result it in more consistent and coherent
integration and refactoring going forward.

1. Decouple peer connection management from identity/address storage and
lookup.
    - Add a `ConnManager` interface to be responsible for this
      - Move `Peer#GetStream()` to it
      - Move `PeerManager#GetSelfAddr()` to it
      - Refactor accordingly

Fixes pokt-network#552

Please mark the relevant option(s):

- [ ] New feature, functionality or library
- [ ] Bug fix
- [x] Code health or cleanup
- [ ] Major breaking change
- [ ] Documentation
- [ ] Other <!-- add details here if it a different type of change -->

- Add `Peer` and `Peerstore` interfaces
- Refactor `AddrBook` to `PeerList` type
- Refactor `getAddrBookDelta` to be a member of `PeerList`
- Move `typesP2P.AddrBookMap` to `sharedP2P.PeerAddrMap` and refactor to
implement the new `Peerstore` interface
- Factor `SortedPeerManager` out of `raintree.peersManager` and add
`peerManager` interface
- Refactor `raintree.peersManager` to use `SortedPeerManager` and
implement `PeerManager` interface
- Refactor `stdnetwork.Network` implementation to use P2P interfaces
- Refactor `AddrBookProvider` to use new P2P interfaces
- Refactor `typesP2P.Network` to use new P2P interfaces
- Refactor `typesP2P.Transport` to embed `io.ReadWriteCloser`
- Rename `NetworkPeer#Dialer` to `NetworkPeer#Transport`for readability
and consistency
- Refactor `typesP2P.NetworkPeer` to implement the new `Peer` interface
- Refactor debug CLI to use new P2P interfaces

- [x] `make develop_test`
- [x]
[LocalNet](https://github.com/pokt-network/pocket/blob/main/docs/development/README.md)
w/ all of the steps outlined in the `README`

<!-- REMOVE this comment block after following the instructions
 If you added additional tests or infrastructure, describe it here.
 Bonus points for images and videos or gifs.
-->

- [x] I have performed a self-review of my own code
- [x] I have commented my code, particularly in hard-to-understand areas
- [x] I have tested my changes using the available tooling
- [x] I have updated the corresponding CHANGELOG

- [ ] I have updated the corresponding README(s); local and/or global
- [x] I have added tests that prove my fix is effective or that my
feature works
- [ ] I have added, or updated,
[mermaid.js](https://mermaid-js.github.io) diagrams in the corresponding
README(s)
- [ ] I have added, or updated, documentation and
[mermaid.js](https://mermaid-js.github.io) diagrams in `shared/docs/*`
if I updated `shared/*`README(s)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p2p P2P specific changes
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

[P2P] Redefine P2P concrete types in terms of interfaces
2 participants