Skip to content

Commit

Permalink
refactor(p2p/netaddress)!: move to netaddr (cometbft#4303)
Browse files Browse the repository at this point in the history
Refs cometbft#4302

**Important: no logic has been changed; just moving stuff and updating
interfaces.**

## Public API Changes

* Package Renaming: The `netaddress` package is renamed to `netaddr`.
* Removal of Prefix: The `NetAddress` prefix has been removed from
relevant types and methods for better alignment with Go naming
conventions.
  • Loading branch information
melekes authored Oct 24, 2024
1 parent eef3464 commit 3bd747e
Show file tree
Hide file tree
Showing 31 changed files with 394 additions and 394 deletions.
4 changes: 2 additions & 2 deletions cmd/cometbft/commands/testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
cfg "github.com/cometbft/cometbft/config"
cmtrand "github.com/cometbft/cometbft/internal/rand"
"github.com/cometbft/cometbft/libs/bytes"
na "github.com/cometbft/cometbft/p2p/netaddress"
na "github.com/cometbft/cometbft/p2p/netaddr"
"github.com/cometbft/cometbft/p2p/nodekey"
"github.com/cometbft/cometbft/privval"
"github.com/cometbft/cometbft/types"
Expand Down Expand Up @@ -256,7 +256,7 @@ func persistentPeersString(config *cfg.Config) (string, error) {
if err != nil {
return "", err
}
persistentPeers[i] = na.IDAddressString(nk.ID(), fmt.Sprintf("%s:%d", hostnameOrIP(i), p2pPort))
persistentPeers[i] = na.IDAddrString(nk.ID(), fmt.Sprintf("%s:%d", hostnameOrIP(i), p2pPort))
}
return strings.Join(persistentPeers, ","), nil
}
Expand Down
4 changes: 2 additions & 2 deletions docs/explanation/core/running-in-production.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ CometBFT keeps multiple distinct databases in the `$CMTHOME/data`:
used to temporarily store intermediate results during block processing.
- `tx_index.db`: Indexes transactions and by tx hash and height. The tx results are indexed if they are added to the `FinalizeBlock` response in the application.

> By default, CometBFT will only index transactions by their hash and height, if you want the result events to be indexed, see [indexing transactions](../../guides/app-dev/indexing-transactions.md#adding-events)
> By default, CometBFT will only index transactions by their hash and height, if you want the result events to be indexed, see [indexing transactions](../../guides/app-dev/indexing-transactions.md#adding-events)
for details.

Applications can expose block pruning strategies to the node operator.
Expand Down Expand Up @@ -382,7 +382,7 @@ application to process the committed block.

By default, CometBFT checks whenever a peer's address is routable before
saving it to the address book. The address is considered as routable if the IP
is [valid and within allowed ranges](https://github.com/cometbft/cometbft/blob/main/p2p/netaddress.go#L258).
is [valid and within allowed ranges](https://github.com/cometbft/cometbft/blob/main/p2p/netaddr/netaddr.go#L258).
This may not be the case for private or local networks, where your IP range is usually
strictly limited and private. If that case, you need to set `addr_book_strict`
Expand Down
6 changes: 3 additions & 3 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
"github.com/cometbft/cometbft/light"
mempl "github.com/cometbft/cometbft/mempool"
"github.com/cometbft/cometbft/p2p"
na "github.com/cometbft/cometbft/p2p/netaddress"
na "github.com/cometbft/cometbft/p2p/netaddr"
ni "github.com/cometbft/cometbft/p2p/nodeinfo"
"github.com/cometbft/cometbft/p2p/nodekey"
"github.com/cometbft/cometbft/p2p/pex"
Expand Down Expand Up @@ -530,7 +530,7 @@ func NewNodeWithCliParams(ctx context.Context,
//
// We need to set Seeds and PersistentPeers on the switch,
// since it needs to be able to use these (and their DNS names)
// even if the PEX is off. We can include the DNS name in the NetAddress,
// even if the PEX is off. We can include the DNS name in the na.NetAddr,
// but it would still be nice to have a clear list of the current "PersistentPeers"
// somewhere that we can return with net_info.
//
Expand Down Expand Up @@ -613,7 +613,7 @@ func (n *Node) OnStart() error {
}

// Start the transport.
addr, err := na.NewNetAddressString(na.IDAddressString(n.nodeKey.ID(), n.config.P2P.ListenAddress))
addr, err := na.NewFromString(na.IDAddrString(n.nodeKey.ID(), n.config.P2P.ListenAddress))
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions node/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"github.com/cometbft/cometbft/light"
mempl "github.com/cometbft/cometbft/mempool"
"github.com/cometbft/cometbft/p2p"
na "github.com/cometbft/cometbft/p2p/netaddress"
na "github.com/cometbft/cometbft/p2p/netaddr"
ni "github.com/cometbft/cometbft/p2p/nodeinfo"
"github.com/cometbft/cometbft/p2p/nodekey"
"github.com/cometbft/cometbft/p2p/pex"
Expand Down Expand Up @@ -519,14 +519,14 @@ func createAddrBookAndSetOnSwitch(config *cfg.Config, sw *p2p.Switch,

// Add ourselves to addrbook to prevent dialing ourselves
if config.P2P.ExternalAddress != "" {
addr, err := na.NewNetAddressString(na.IDAddressString(nodeKey.ID(), config.P2P.ExternalAddress))
addr, err := na.NewFromString(na.IDAddrString(nodeKey.ID(), config.P2P.ExternalAddress))
if err != nil {
return nil, fmt.Errorf("p2p.external_address is incorrect: %w", err)
}
addrBook.AddOurAddress(addr)
}
if config.P2P.ListenAddress != "" {
addr, err := na.NewNetAddressString(na.IDAddressString(nodeKey.ID(), config.P2P.ListenAddress))
addr, err := na.NewFromString(na.IDAddrString(nodeKey.ID(), config.P2P.ListenAddress))
if err != nil {
return nil, fmt.Errorf("p2p.laddr is incorrect: %w", err)
}
Expand Down
6 changes: 3 additions & 3 deletions p2p/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"net"

na "github.com/cometbft/cometbft/p2p/netaddress"
na "github.com/cometbft/cometbft/p2p/netaddr"
"github.com/cometbft/cometbft/p2p/nodekey"
)

Expand All @@ -30,15 +30,15 @@ func (e ErrSwitchDuplicatePeerIP) Error() string {

// ErrSwitchConnectToSelf to be raised when trying to connect to itself.
type ErrSwitchConnectToSelf struct {
Addr *na.NetAddress
Addr *na.NetAddr
}

func (e ErrSwitchConnectToSelf) Error() string {
return fmt.Sprintf("connect to self: %v", e.Addr)
}

type ErrSwitchAuthenticationFailure struct {
Dialed *na.NetAddress
Dialed *na.NetAddr
Got nodekey.ID
}

Expand Down
10 changes: 5 additions & 5 deletions p2p/handshake.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import (

tmp2p "github.com/cometbft/cometbft/api/cometbft/p2p/v1"
"github.com/cometbft/cometbft/libs/protoio"
na "github.com/cometbft/cometbft/p2p/netaddress"
na "github.com/cometbft/cometbft/p2p/netaddr"
ni "github.com/cometbft/cometbft/p2p/nodeinfo"
"github.com/cometbft/cometbft/p2p/nodekey"
)

// ErrRejected indicates that a Peer was rejected carrying additional
// information as to the reason.
type ErrRejected struct {
addr na.NetAddress
addr na.NetAddr
conn net.Conn
err error
id nodekey.ID
Expand All @@ -27,8 +27,8 @@ type ErrRejected struct {
isSelf bool
}

// Addr returns the NetAddress for the rejected Peer.
func (e ErrRejected) Addr() na.NetAddress {
// Addr returns the network address for the rejected Peer.
func (e ErrRejected) Addr() na.NetAddr {
return e.addr
}

Expand Down Expand Up @@ -136,7 +136,7 @@ func handshake(ourNodeInfo ni.NodeInfo, c net.Conn, handshakeTimeout time.Durati
// Reject self.
if ourNodeInfo.ID() == nodeInfo.ID() {
return nil, ErrRejected{
addr: *na.NewNetAddress(nodeInfo.ID(), c.RemoteAddr()),
addr: *na.New(nodeInfo.ID(), c.RemoteAddr()),
conn: c,
id: nodeInfo.ID(),
isSelf: true,
Expand Down
20 changes: 10 additions & 10 deletions p2p/mock/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/cometbft/cometbft/crypto/ed25519"
"github.com/cometbft/cometbft/libs/service"
"github.com/cometbft/cometbft/p2p"
na "github.com/cometbft/cometbft/p2p/netaddress"
na "github.com/cometbft/cometbft/p2p/netaddr"
ni "github.com/cometbft/cometbft/p2p/nodeinfo"
"github.com/cometbft/cometbft/p2p/nodekey"
"github.com/cometbft/cometbft/p2p/transport/tcp/conn"
Expand All @@ -16,7 +16,7 @@ type Peer struct {
*service.BaseService
ip net.IP
id nodekey.ID
addr *na.NetAddress
addr *na.NetAddr
kv map[string]any
Outbound, Persistent bool
server, client net.Conn
Expand All @@ -25,11 +25,11 @@ type Peer struct {
// NewPeer creates and starts a new mock peer. If the ip
// is nil, random routable address is used.
func NewPeer(ip net.IP) *Peer {
var netAddr *na.NetAddress
var netAddr *na.NetAddr
if ip == nil {
_, netAddr = na.CreateRoutableAddr()
} else {
netAddr = na.NewNetAddressIPPort(ip, 26656)
netAddr = na.NewFromIPPort(ip, 26656)
}
nodeKey := nodekey.NodeKey{PrivKey: ed25519.GenPrivKey()}
netAddr.ID = nodeKey.ID()
Expand Down Expand Up @@ -77,9 +77,9 @@ func (mp *Peer) Get(key string) any {
func (mp *Peer) Set(key string, value any) {
mp.kv[key] = value
}
func (mp *Peer) RemoteIP() net.IP { return mp.ip }
func (mp *Peer) SocketAddr() *na.NetAddress { return mp.addr }
func (mp *Peer) RemoteAddr() net.Addr { return &net.TCPAddr{IP: mp.ip, Port: 8800} }
func (mp *Peer) Conn() net.Conn { return mp.server }
func (*Peer) SetRemovalFailed() {}
func (*Peer) GetRemovalFailed() bool { return false }
func (mp *Peer) RemoteIP() net.IP { return mp.ip }
func (mp *Peer) SocketAddr() *na.NetAddr { return mp.addr }
func (mp *Peer) RemoteAddr() net.Addr { return &net.TCPAddr{IP: mp.ip, Port: 8800} }
func (mp *Peer) Conn() net.Conn { return mp.server }
func (*Peer) SetRemovalFailed() {}
func (*Peer) GetRemovalFailed() bool { return false }
12 changes: 6 additions & 6 deletions p2p/mock_transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import (
"net"
"time"

na "github.com/cometbft/cometbft/p2p/netaddress"
na "github.com/cometbft/cometbft/p2p/netaddr"
)

type mockTransport struct {
ln net.Listener
addr na.NetAddress
addr na.NetAddr
}

func (t *mockTransport) Listen(addr na.NetAddress) error {
func (t *mockTransport) Listen(addr na.NetAddr) error {
ln, err := net.Listen("tcp", addr.DialString())
if err != nil {
return err
Expand All @@ -22,16 +22,16 @@ func (t *mockTransport) Listen(addr na.NetAddress) error {
return nil
}

func (t *mockTransport) NetAddress() na.NetAddress {
func (t *mockTransport) NetAddr() na.NetAddr {
return t.addr
}

func (t *mockTransport) Accept() (net.Conn, *na.NetAddress, error) {
func (t *mockTransport) Accept() (net.Conn, *na.NetAddr, error) {
c, err := t.ln.Accept()
return c, nil, err
}

func (*mockTransport) Dial(addr na.NetAddress) (net.Conn, error) {
func (*mockTransport) Dial(addr na.NetAddr) (net.Conn, error) {
return addr.DialTimeout(time.Second)
}

Expand Down
10 changes: 5 additions & 5 deletions p2p/mocks/peer.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 9 additions & 9 deletions p2p/netaddress/errors.go → p2p/netaddr/errors.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package netaddress
package netaddr

import (
"errors"
Expand All @@ -13,35 +13,35 @@ var (
ErrInvalidIP = errors.New("invalid IP address")
)

type ErrNetAddressNoID struct {
type ErrNoID struct {
Addr string
}

func (e ErrNetAddressNoID) Error() string {
func (e ErrNoID) Error() string {
return fmt.Sprintf("address (%s) does not contain ID", e.Addr)
}

type ErrNetAddressInvalid struct {
type ErrInvalid struct {
Addr string
Err error
}

func (e ErrNetAddressInvalid) Error() string {
func (e ErrInvalid) Error() string {
return fmt.Sprintf("invalid address (%s): %v", e.Addr, e.Err)
}

func (e ErrNetAddressInvalid) Unwrap() error { return e.Err }
func (e ErrInvalid) Unwrap() error { return e.Err }

type ErrNetAddressLookup struct {
type ErrLookup struct {
Addr string
Err error
}

func (e ErrNetAddressLookup) Error() string {
func (e ErrLookup) Error() string {
return fmt.Sprintf("error looking up host (%s): %v", e.Addr, e.Err)
}

func (e ErrNetAddressLookup) Unwrap() error { return e.Err }
func (e ErrLookup) Unwrap() error { return e.Err }

type ErrInvalidPort struct {
Port uint32
Expand Down
Loading

0 comments on commit 3bd747e

Please sign in to comment.