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

feat: introduce libp2p QUIC transport #4424

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cmd/bee/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const (
optionNameP2PAddr = "p2p-addr"
optionNameNATAddr = "nat-addr"
optionNameP2PWSEnable = "p2p-ws-enable"
optionNameP2PQUICEnable = "p2p-quic-enable"
optionNameDebugAPIEnable = "debug-api-enable"
optionNameDebugAPIAddr = "debug-api-addr"
optionNameBootnodes = "bootnode"
Expand Down Expand Up @@ -249,6 +250,7 @@ func (c *command) setAllFlags(cmd *cobra.Command) {
cmd.Flags().String(optionNameAPIAddr, ":1633", "HTTP API listen address")
cmd.Flags().String(optionNameP2PAddr, ":1634", "P2P listen address")
cmd.Flags().String(optionNameNATAddr, "", "NAT exposed address")
cmd.Flags().Bool(optionNameP2PQUICEnable, true, "enable P2P QUIC transport")
cmd.Flags().Bool(optionNameP2PWSEnable, false, "enable P2P WebSocket transport")
cmd.Flags().StringSlice(optionNameBootnodes, []string{""}, "initial nodes to connect to")
cmd.Flags().Bool(optionNameDebugAPIEnable, false, "enable debug HTTP API")
Expand Down
1 change: 1 addition & 0 deletions cmd/bee/cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ func buildBeeNode(ctx context.Context, c *command, cmd *cobra.Command, logger lo
DebugAPIAddr: debugAPIAddr,
Addr: c.config.GetString(optionNameP2PAddr),
NATAddr: c.config.GetString(optionNameNATAddr),
EnableQUIC: c.config.GetBool(optionNameP2PQUICEnable),
EnableWS: c.config.GetBool(optionNameP2PWSEnable),
WelcomeMessage: c.config.GetString(optionWelcomeMessage),
Bootnodes: networkConfig.bootNodes,
Expand Down
13 changes: 12 additions & 1 deletion openapi/SwarmCommon.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,18 @@ components:
type: array
nullable: false
items:
$ref: "#/components/schemas/Address"
type: object
properties:
address:
$ref: "#/components/schemas/Address"
fullNode:
type: boolean
underlay:
type: array
nullable: true
items:
$ref: "SwarmCommon.yaml#/components/schemas/MultiAddress"


BlockListedPeers:
type: array
Expand Down
4 changes: 4 additions & 0 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (

"github.com/ethereum/go-ethereum/common"
"github.com/ethersphere/bee/pkg/accounting"
"github.com/ethersphere/bee/pkg/addressbook"
"github.com/ethersphere/bee/pkg/auth"
"github.com/ethersphere/bee/pkg/crypto"
"github.com/ethersphere/bee/pkg/feeds"
Expand Down Expand Up @@ -176,6 +177,7 @@ type Service struct {
chequebook chequebook.Service
pseudosettle settlement.Interface
pingpong pingpong.Interface
addressBook addressbook.Getter

batchStore postage.Storer
stamperStore storage.Store
Expand Down Expand Up @@ -247,6 +249,7 @@ type ExtraOptions struct {
Steward steward.Interface
SyncStatus func() (bool, error)
NodeStatus *status.Service
AddressBook addressbook.Getter
}

func New(
Expand Down Expand Up @@ -318,6 +321,7 @@ func (s *Service) Configure(signer crypto.Signer, auth auth.Authenticator, trace
s.postageContract = e.PostageContract
s.steward = e.Steward
s.stakingContract = e.Staking
s.addressBook = e.AddressBook

s.pingpong = e.Pingpong
s.topologyDriver = e.TopologyDriver
Expand Down
4 changes: 4 additions & 0 deletions pkg/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

"github.com/ethereum/go-ethereum/common"
accountingmock "github.com/ethersphere/bee/pkg/accounting/mock"
"github.com/ethersphere/bee/pkg/addressbook"
"github.com/ethersphere/bee/pkg/api"
"github.com/ethersphere/bee/pkg/auth"
mockauth "github.com/ethersphere/bee/pkg/auth/mock"
Expand Down Expand Up @@ -169,6 +170,8 @@ func newTestServer(t *testing.T, o testServerOptions) (*http.Client, *websocket.
chequebook := chequebookmock.NewChequebook(o.ChequebookOpts...)
ln := lightnode.NewContainer(o.Overlay)

addressBook := addressbook.New(statestore.NewStateStore())

transaction := transactionmock.New(o.TransactionOpts...)

storeRecipient := statestore.NewStateStore()
Expand Down Expand Up @@ -199,6 +202,7 @@ func newTestServer(t *testing.T, o testServerOptions) (*http.Client, *websocket.
SyncStatus: o.SyncStatus,
Staking: o.StakingContract,
NodeStatus: o.NodeStatus,
AddressBook: addressBook,
}

// By default bee mode is set to full mode.
Expand Down
1 change: 1 addition & 0 deletions pkg/api/p2p_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func TestAddresses(t *testing.T) {
addresses := []multiaddr.Multiaddr{
mustMultiaddr(t, "/ip4/127.0.0.1/tcp/7071/p2p/16Uiu2HAmTBuJT9LvNmBiQiNoTsxE5mtNy6YG3paw79m94CRa9sRb"),
mustMultiaddr(t, "/ip4/192.168.0.101/tcp/7071/p2p/16Uiu2HAmTBuJT9LvNmBiQiNoTsxE5mtNy6YG3paw79m94CRa9sRb"),
mustMultiaddr(t, "/ip4/127.0.0.1/udp/7071/quic-v1/p2p/16Uiu2HAmTBuJT9LvNmBiQiNoTsxE5mtNy6YG3paw79m94CRa9sRb"),
}

ethereumAddress := common.HexToAddress("abcd")
Expand Down
27 changes: 19 additions & 8 deletions pkg/api/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,10 @@ func (s *Service) peerDisconnectHandler(w http.ResponseWriter, r *http.Request)

// Peer holds information about a Peer.
type Peer struct {
Address swarm.Address `json:"address"`
FullNode bool `json:"fullNode"`
Address swarm.Address `json:"address"`
Underlay multiaddr.Multiaddr `json:"underlay,omitempty"`
FullNode bool `json:"fullNode"`
Connections []multiaddr.Multiaddr `json:"connections,omitempty"`
}

type BlockListedPeer struct {
Expand All @@ -99,7 +101,7 @@ type blockListedPeersResponse struct {

func (s *Service) peersHandler(w http.ResponseWriter, _ *http.Request) {
jsonhttp.OK(w, peersResponse{
Peers: mapPeers(s.p2p.Peers()),
Peers: s.mapPeers(s.p2p.Peers()),
})
}

Expand All @@ -118,13 +120,22 @@ func (s *Service) blocklistedPeersHandler(w http.ResponseWriter, _ *http.Request
})
}

func mapPeers(peers []p2p.Peer) (out []Peer) {
func (s *Service) mapPeers(peers []p2p.Peer) (out []Peer) {
out = make([]Peer, 0, len(peers))
for _, peer := range peers {
out = append(out, Peer{
Address: peer.Address,
FullNode: peer.FullNode,
})
bzz, err := s.addressBook.Get(peer.Address)
if err != nil {
s.logger.Debug("get bzz address from addressbook", "address", peer.Address, "error", err)
}
p := Peer{
Address: peer.Address,
FullNode: peer.FullNode,
Connections: peer.Connections,
}
if bzz != nil {
p.Underlay = bzz.Underlay
}
out = append(out, p)
}
return
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/hive/hive.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,8 @@ func (s *Service) checkAndAddPeers(ctx context.Context, peers pb.Peers) {
wg.Done()
}()

s.logger.Debug("received peer", "overlay", swarm.NewAddress(newPeer.Overlay), "underlay", multiUnderlay)

ctx, cancel := context.WithTimeout(ctx, pingTimeout)
defer cancel()

Expand Down
3 changes: 3 additions & 0 deletions pkg/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ type Options struct {
DebugAPIAddr string
Addr string
NATAddr string
EnableQUIC bool
EnableWS bool
WelcomeMessage string
Bootnodes []string
Expand Down Expand Up @@ -639,6 +640,7 @@ func NewBee(
p2ps, err := libp2p.New(ctx, signer, networkID, swarmAddress, addr, addressbook, stateStore, lightNodes, logger, tracer, libp2p.Options{
PrivateKey: libp2pPrivateKey,
NATAddr: o.NATAddr,
EnableQUIC: o.EnableQUIC,
EnableWS: o.EnableWS,
WelcomeMessage: o.WelcomeMessage,
FullNode: o.FullNodeMode,
Expand Down Expand Up @@ -1104,6 +1106,7 @@ func NewBee(
Steward: steward,
SyncStatus: syncStatusFn,
NodeStatus: nodeStatus,
AddressBook: addressbook,
}

if o.APIAddr != "" {
Expand Down
9 changes: 6 additions & 3 deletions pkg/p2p/discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import (
"context"
"errors"
"fmt"
"math/rand"
"sort"
"strings"

ma "github.com/multiformats/go-multiaddr"
madns "github.com/multiformats/go-multiaddr-dns"
Expand All @@ -35,9 +36,11 @@ func Discover(ctx context.Context, addr ma.Multiaddr, f func(ma.Multiaddr) (bool
return false, errors.New("non-resolvable API endpoint")
}

rand.Shuffle(len(addrs), func(i, j int) {
addrs[i], addrs[j] = addrs[j], addrs[i]
// Prioritize QUIC transport connections over the others.
sort.Slice(addrs, func(i, j int) bool {
return strings.Contains(addrs[i].String(), "/quic")
})

for _, addr := range addrs {
stopped, err := Discover(ctx, addr, f)
if err != nil {
Expand Down
31 changes: 31 additions & 0 deletions pkg/p2p/libp2p/connections_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1129,6 +1129,37 @@ func TestReachabilityUpdate(t *testing.T) {
}
}

func TestConnectWithEnabledQUICTransports(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

n1, overlay1 := newService(t, 1, libp2pServiceOpts{
libp2pOpts: libp2p.Options{
EnableQUIC: true,
FullNode: true,
},
})

n2, overlay2 := newService(t, 1, libp2pServiceOpts{
libp2pOpts: libp2p.Options{
EnableQUIC: true,
FullNode: true,
},
})

addr1 := serviceUnderlayAddress(t, n1, ma.P_QUIC_V1)
if addr1 == nil {
t.Fatal("QUIC protocol address not found")
}

if _, err := n2.Connect(ctx, addr1); err != nil {
t.Fatal(err)
}

expectPeers(t, n2, overlay1)
expectPeersEventually(t, n1, overlay2)
}

func testUserAgentLogLine(t *testing.T, logs *buffer, substring string) {
t.Helper()

Expand Down
55 changes: 33 additions & 22 deletions pkg/p2p/libp2p/libp2p.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,46 +17,46 @@ import (
"sync"
"time"

ocprom "contrib.go.opencensus.io/exporter/prometheus"
"github.com/ethersphere/bee"
"github.com/ethersphere/bee/pkg/addressbook"
"github.com/ethersphere/bee/pkg/bzz"
beecrypto "github.com/ethersphere/bee/pkg/crypto"
"github.com/ethersphere/bee/pkg/log"
m2 "github.com/ethersphere/bee/pkg/metrics"
"github.com/ethersphere/bee/pkg/p2p"
"github.com/ethersphere/bee/pkg/p2p/libp2p/internal/blocklist"
"github.com/ethersphere/bee/pkg/p2p/libp2p/internal/breaker"
handshake "github.com/ethersphere/bee/pkg/p2p/libp2p/internal/handshake"
"github.com/ethersphere/bee/pkg/p2p/libp2p/internal/handshake"
"github.com/ethersphere/bee/pkg/p2p/libp2p/internal/reacher"
"github.com/ethersphere/bee/pkg/storage"
"github.com/ethersphere/bee/pkg/swarm"
"github.com/ethersphere/bee/pkg/topology"
"github.com/ethersphere/bee/pkg/topology/lightnode"
"github.com/ethersphere/bee/pkg/tracing"
libp2p "github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/event"
"github.com/libp2p/go-libp2p/core/host"
network "github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/network"
libp2ppeer "github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/peerstore"
protocol "github.com/libp2p/go-libp2p/core/protocol"
autonat "github.com/libp2p/go-libp2p/p2p/host/autonat"
"github.com/libp2p/go-libp2p/core/protocol"
"github.com/libp2p/go-libp2p/p2p/host/autonat"
basichost "github.com/libp2p/go-libp2p/p2p/host/basic"
"github.com/libp2p/go-libp2p/p2p/host/peerstore/pstoremem"
rcmgr "github.com/libp2p/go-libp2p/p2p/host/resource-manager"
rcmgrObs "github.com/libp2p/go-libp2p/p2p/host/resource-manager"
lp2pswarm "github.com/libp2p/go-libp2p/p2p/net/swarm"
libp2pping "github.com/libp2p/go-libp2p/p2p/protocol/ping"
libp2pquic "github.com/libp2p/go-libp2p/p2p/transport/quic"
"github.com/libp2p/go-libp2p/p2p/transport/tcp"
ws "github.com/libp2p/go-libp2p/p2p/transport/websocket"

ma "github.com/multiformats/go-multiaddr"
"github.com/multiformats/go-multistream"
"go.uber.org/atomic"

ocprom "contrib.go.opencensus.io/exporter/prometheus"
m2 "github.com/ethersphere/bee/pkg/metrics"
rcmgrObs "github.com/libp2p/go-libp2p/p2p/host/resource-manager"
"github.com/prometheus/client_golang/prometheus"
"go.uber.org/atomic"
"golang.org/x/exp/maps"
)

// loggerName is the tree path name of the logger for this package.
Expand Down Expand Up @@ -122,6 +122,7 @@ type lightnodes interface {
type Options struct {
PrivateKey *ecdsa.PrivateKey
NATAddr string
EnableQUIC bool
EnableWS bool
FullNode bool
LightNodeLimit int
Expand Down Expand Up @@ -156,13 +157,19 @@ func New(ctx context.Context, signer beecrypto.Signer, networkID uint64, overlay
var listenAddrs []string
if ip4Addr != "" {
listenAddrs = append(listenAddrs, fmt.Sprintf("/ip4/%s/tcp/%s", ip4Addr, port))
if o.EnableQUIC {
listenAddrs = append(listenAddrs, fmt.Sprintf("/ip4/%s/udp/%s/quic-v1", ip4Addr, port))
}
if o.EnableWS {
listenAddrs = append(listenAddrs, fmt.Sprintf("/ip4/%s/tcp/%s/ws", ip4Addr, port))
}
}

if ip6Addr != "" {
listenAddrs = append(listenAddrs, fmt.Sprintf("/ip6/%s/tcp/%s", ip6Addr, port))
if o.EnableQUIC {
listenAddrs = append(listenAddrs, fmt.Sprintf("/ip6/%s/udp/%s/quic-v1", ip6Addr, port))
}
if o.EnableWS {
listenAddrs = append(listenAddrs, fmt.Sprintf("/ip6/%s/tcp/%s/ws", ip6Addr, port))
}
Expand Down Expand Up @@ -244,7 +251,9 @@ func New(ctx context.Context, signer beecrypto.Signer, networkID uint64, overlay
transports := []libp2p.Option{
libp2p.Transport(tcp.NewTCPTransport, tcp.DisableReuseport()),
}

if o.EnableQUIC {
transports = append(transports, libp2p.Transport(libp2pquic.NewTransport))
}
if o.EnableWS {
transports = append(transports, libp2p.Transport(ws.New))
}
Expand Down Expand Up @@ -644,24 +653,26 @@ func (s *Service) AddProtocol(p p2p.ProtocolSpec) (err error) {
return nil
}

func (s *Service) Addresses() (addreses []ma.Multiaddr, err error) {
func (s *Service) Addresses() ([]ma.Multiaddr, error) {
addresses := make(map[string]ma.Multiaddr)
for _, addr := range s.host.Addrs() {
a, err := buildUnderlayAddress(addr, s.host.ID())
if err != nil {
return nil, err
}

addreses = append(addreses, a)
addresses[a.String()] = a
}
if s.natAddrResolver != nil && len(addreses) > 0 {
a, err := s.natAddrResolver.Resolve(addreses[0])
if err != nil {
return nil, err
if s.natAddrResolver != nil {
for _, addr := range addresses {
a, err := s.natAddrResolver.Resolve(addr)
if err != nil {
return nil, err
}
addresses[a.String()] = a
}
addreses = append(addreses, a)
}

return addreses, nil
return maps.Values(addresses), nil
}

func (s *Service) NATManager() basichost.NATManager {
Expand Down Expand Up @@ -695,7 +706,7 @@ func (s *Service) Blocklist(overlay swarm.Address, duration time.Duration, reaso
}

func buildHostAddress(peerID libp2ppeer.ID) (ma.Multiaddr, error) {
return ma.NewMultiaddr(fmt.Sprintf("/p2p/%s", peerID.Pretty()))
return ma.NewMultiaddr(fmt.Sprintf("/p2p/%s", peerID))
}

func buildUnderlayAddress(addr ma.Multiaddr, peerID libp2ppeer.ID) (ma.Multiaddr, error) {
Expand Down
Loading
Loading