Skip to content

Commit

Permalink
Merge branch 'master' into feat/relay-api-autoshard
Browse files Browse the repository at this point in the history
  • Loading branch information
chaitanyaprem authored Oct 17, 2023
2 parents d732304 + bfee996 commit 83adec1
Show file tree
Hide file tree
Showing 18 changed files with 59 additions and 68 deletions.
6 changes: 1 addition & 5 deletions .github/workflows/auto_assign_pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,4 @@ jobs:
assign_creator:
runs-on: ubuntu-latest
steps:
- name: Assign PR to author
run: |
gh pr edit ${{ github.event.pull_request.number }} --add-assignee "${{ github.event.pull_request.user.login }}"
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- uses: toshimaru/[email protected]
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# BUILD IMAGE --------------------------------------------------------
FROM golang:1.19 as builder
FROM golang:1.20 as builder

WORKDIR /app
COPY . .
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ A Go implementation of the [Waku v2 protocol](https://rfc.vac.dev/spec/10).
<p align="left">
<a href="https://goreportcard.com/report/github.com/waku-org/go-waku"><img src="https://goreportcard.com/badge/github.com/waku-org/go-waku" /></a>
<a href="https://godoc.org/github.com/waku-org/go-waku"><img src="http://img.shields.io/badge/godoc-reference-5272B4.svg?style=flat-square" /></a>
<a href=""><img src="https://img.shields.io/badge/golang-%3E%3D1.19.0-orange.svg?style=flat-square" /></a>
<a href=""><img src="https://img.shields.io/badge/golang-%3E%3D1.20.0-orange.svg?style=flat-square" /></a>
<a href="https://codeclimate.com/github/waku-org/go-waku/maintainability"><img src="https://api.codeclimate.com/v1/badges/426bdff6a339ff4d536b/maintainability" /></a>
<br>
</p>
Expand Down Expand Up @@ -106,7 +106,7 @@ Thank you for considering to help out with the source code! We welcome contribut
If you'd like to contribute to go-waku, please fork, fix, commit and send a pull request. If you wish to submit more complex changes though, please check up with the core devs first to ensure those changes are in line with the general philosophy of the project and/or get some early feedback which can make both your efforts much lighter as well as our review and merge procedures quick and simple.

To build and test this repository, you need:
- [Go](https://golang.org/) (version 1.19 or 1.20)
- [Go](https://golang.org/) (version 1.20)
- [protoc](https://grpc.io/docs/protoc-installation/)
- [protoc-gen-go](https://protobuf.dev/getting-started/gotutorial/#compiling-protocol-buffers)

Expand Down
2 changes: 1 addition & 1 deletion examples/basic2/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module basic2

go 1.19
go 1.20

replace github.com/waku-org/go-waku => ../..

Expand Down
2 changes: 1 addition & 1 deletion examples/chat2/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module chat2

go 1.19
go 1.20

replace github.com/waku-org/go-waku => ../..

Expand Down
2 changes: 1 addition & 1 deletion examples/filter2/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module filter2

go 1.19
go 1.20

replace github.com/waku-org/go-waku => ../..

Expand Down
2 changes: 1 addition & 1 deletion examples/noise/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module noise

go 1.19
go 1.20

replace github.com/waku-org/go-waku => ../..

Expand Down
2 changes: 1 addition & 1 deletion examples/rln/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module rln

go 1.19
go 1.20

replace github.com/waku-org/go-waku => ../..

Expand Down
2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
pkgs = nixpkgsFor.${system};
commit = builtins.substring 0 7 (self.rev or "dirty");
version = builtins.readFile ./VERSION;
in pkgs.buildGo119Module {
in pkgs.buildGo120Module {
name = "go-waku";
src = self;
inherit subPackages;
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/waku-org/go-waku

go 1.19
go 1.20

replace github.com/ethereum/go-ethereum v1.10.26 => github.com/status-im/go-ethereum v1.10.25-status.4

Expand Down Expand Up @@ -160,7 +160,7 @@ require (
go.uber.org/atomic v1.11.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.12.0 // indirect
golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1 // indirect
golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1
golang.org/x/mod v0.12.0 // indirect
golang.org/x/net v0.14.0 // indirect
golang.org/x/sys v0.11.0
Expand Down
15 changes: 7 additions & 8 deletions waku/v2/node/address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,20 @@ func TestExternalAddressSelection(t *testing.T) {
a9, _ := ma.NewMultiaddr("/dns4/node-02.gc-us-central1-a.status.prod.statusim.net/tcp/443/wss/p2p/16Uiu2HAmDQugwDHM3YeUp86iGjrUvbdw3JPRgikC7YoGBsT2ymMg/p2p-circuit/p2p/16Uiu2HAmUVVrJo1KMw4QwUANYF7Ws4mfcRqf9xHaaGP87GbMuY2f") // VALID
a10, _ := ma.NewMultiaddr("/dns4/node-01.gc-us-central1-a.wakuv2.test.statusim.net/tcp/8000/wss/p2p/16Uiu2HAmJb2e28qLXxT5kZxVUUoJt72EMzNGXB47Rxx5hw3q4YjS/p2p-circuit/p2p/16Uiu2HAmUVVrJo1KMw4QwUANYF7Ws4mfcRqf9xHaaGP87GbMuY2f") // VALID
a11, _ := ma.NewMultiaddr("/dns4/node-01.gc-us-central1-a.wakuv2.test.statusim.net/tcp/30303/p2p/16Uiu2HAmJb2e28qLXxT5kZxVUUoJt72EMzNGXB47Rxx5hw3q4YjS/p2p-circuit/p2p/16Uiu2HAmUVVrJo1KMw4QwUANYF7Ws4mfcRqf9xHaaGP87GbMuY2f") // VALID
a12, _ := ma.NewMultiaddr("/ip4/188.23.1.8/tcp/30303/p2p/16Uiu2HAmUVVrJo1KMw4QwUANYF7Ws4mfcRqf9xHaaGP87GbMuY2f") // VALID

addrs := []ma.Multiaddr{a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11}
addrs := []ma.Multiaddr{a1, a2, a3, a4, a5, a6, a7}

w := &WakuNode{}
extAddr, multiaddr, err := w.getENRAddresses([]ma.Multiaddr{a1, a2, a3, a4, a5, a6, a7, a8})

a5NoP2P, _ := decapsulateP2P(a5)
extAddr, multiaddr, err := w.getENRAddresses([]ma.Multiaddr{a1, a2, a3, a4, a5, a6, a7})
a4NoP2P, _ := decapsulateP2P(a4)
require.NoError(t, err)
require.Equal(t, extAddr.IP, net.IPv4(192, 168, 0, 106))
require.Equal(t, extAddr.Port, 60000)
require.Equal(t, multiaddr[0].String(), a5NoP2P.String())
require.Len(t, multiaddr, 1) // Should only have 1, without circuit relay
require.Equal(t, multiaddr[0].String(), a4NoP2P.String())
require.Len(t, multiaddr, 4)

a12, _ := ma.NewMultiaddr("/ip4/188.23.1.8/tcp/30303/p2p/16Uiu2HAmUVVrJo1KMw4QwUANYF7Ws4mfcRqf9xHaaGP87GbMuY2f") // VALID
addrs = append(addrs, a12)
addrs = append(addrs, a8, a9, a10, a11, a12)
extAddr, _, err = w.getENRAddresses(addrs)
require.NoError(t, err)
require.Equal(t, extAddr.IP, net.IPv4(188, 23, 1, 8))
Expand Down
37 changes: 16 additions & 21 deletions waku/v2/node/localnode.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,13 @@ func (w *WakuNode) updateLocalNode(localnode *enode.LocalNode, multiaddrs []ma.M
ip4 := ipAddr.IP.To4()
ip6 := ipAddr.IP.To16()
if ip4 != nil && !ip4.IsUnspecified() {
localnode.SetFallbackIP(ip4)
localnode.Set(enr.IPv4(ip4))
localnode.Set(enr.TCP(uint16(ipAddr.Port)))
} else {
localnode.Delete(enr.IPv4{})
localnode.Delete(enr.TCP(0))
localnode.SetFallbackIP(net.IP{127, 0, 0, 1})
}

if ip4 == nil && ip6 != nil && !ip6.IsUnspecified() {
Expand All @@ -59,8 +61,6 @@ func (w *WakuNode) updateLocalNode(localnode *enode.LocalNode, multiaddrs []ma.M
}
}

localnode.SetFallbackIP(net.IP{127, 0, 0, 1})

return wenr.Update(localnode, options...)
}

Expand Down Expand Up @@ -191,7 +191,7 @@ func decapsulateCircuitRelayAddr(addr ma.Multiaddr) (ma.Multiaddr, error) {
return addr, nil
}

func selectWSSListenAddresses(addresses []ma.Multiaddr) ([]ma.Multiaddr, error) {
func selectWSListenAddresses(addresses []ma.Multiaddr) ([]ma.Multiaddr, error) {
var result []ma.Multiaddr
for _, addr := range addresses {
// It's a p2p-circuit address. We dont use these at this stage yet
Expand All @@ -200,14 +200,9 @@ func selectWSSListenAddresses(addresses []ma.Multiaddr) ([]ma.Multiaddr, error)
continue
}

// Only WSS with a domain name are allowed
_, err = addr.ValueForProtocol(ma.P_DNS4)
if err != nil {
continue
}

_, err = addr.ValueForProtocol(ma.P_WSS)
if err != nil {
_, noWS := addr.ValueForProtocol(ma.P_WSS)
_, noWSS := addr.ValueForProtocol(ma.P_WS)
if noWS != nil && noWSS != nil { // Neither WS or WSS found
continue
}

Expand Down Expand Up @@ -240,22 +235,22 @@ func (w *WakuNode) getENRAddresses(addrs []ma.Multiaddr) (extAddr *net.TCPAddr,
return nil, nil, err
}

wssAddrs, err := selectWSSListenAddresses(addrs)
wssAddrs, err := selectWSListenAddresses(addrs)
if err != nil {
return nil, nil, err
}

multiaddr = append(multiaddr, wssAddrs...)

// to use WSS, you should have a valid certificate with a domain name.
// that means you're reachable. So circuit relay addresses are ignored
if len(wssAddrs) == 0 {
circuitAddrs, err := selectCircuitRelayListenAddresses(addrs)
if err != nil {
return nil, nil, err
}
circuitAddrs, err := selectCircuitRelayListenAddresses(addrs)
if err != nil {
return nil, nil, err
}

if len(circuitAddrs) != 0 {
// Node is unreachable, hence why we have circuit relay multiaddr
// We prefer these instead of any ws/s address
multiaddr = append(multiaddr, circuitAddrs...)
} else {
multiaddr = append(multiaddr, wssAddrs...)
}

return
Expand Down
26 changes: 9 additions & 17 deletions waku/v2/node/wakunode2.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
golog "github.com/ipfs/go-log/v2"
"github.com/libp2p/go-libp2p"
"go.uber.org/zap"
"golang.org/x/exp/maps"

"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/p2p/enode"
Expand Down Expand Up @@ -306,30 +307,22 @@ func New(opts ...WakuNodeOption) (*WakuNode, error) {
func (w *WakuNode) watchMultiaddressChanges(ctx context.Context) {
defer w.wg.Done()

addrs := w.ListenAddresses()
addrsSet := utils.MultiAddrSet(w.ListenAddresses()...)

first := make(chan struct{}, 1)
first <- struct{}{}
for {
select {
case <-ctx.Done():
return
case <-first:
w.log.Info("listening", logging.MultiAddrs("multiaddr", addrs...))
addr := maps.Keys(addrsSet)
w.log.Info("listening", logging.MultiAddrs("multiaddr", addr...))
case <-w.addressChangesSub.Out():
newAddrs := w.ListenAddresses()
diff := false
if len(addrs) != len(newAddrs) {
diff = true
} else {
for i := range newAddrs {
if addrs[i].String() != newAddrs[i].String() {
diff = true
break
}
}
}
if diff {
addrs = newAddrs
newAddrs := utils.MultiAddrSet(w.ListenAddresses()...)
if !maps.Equal(addrsSet, newAddrs) {
addrsSet = newAddrs
addrs := maps.Keys(addrsSet)
w.log.Info("listening addresses update received", logging.MultiAddrs("multiaddr", addrs...))
err := w.setupENR(ctx, addrs)
if err != nil {
Expand Down Expand Up @@ -889,7 +882,6 @@ func (w *WakuNode) findRelayNodes(ctx context.Context) {
}

// Shuffle peers
rand.Seed(time.Now().UnixNano())
rand.Shuffle(len(peers), func(i, j int) { peers[i], peers[j] = peers[j], peers[i] })

for _, p := range peers {
Expand Down
3 changes: 1 addition & 2 deletions waku/v2/payload/waku_payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
crand "crypto/rand"
"encoding/binary"
"fmt"
mrand "math/rand"

"errors"
"strconv"
Expand Down Expand Up @@ -282,7 +281,7 @@ func generateSecureRandomData(length int) ([]byte, error) {
} else if !validateDataIntegrity(x, length) {
return nil, errors.New("crypto/rand failed to generate secure random data")
}
_, err = mrand.Read(y)
_, err = crand.Read(y)
if err != nil {
return nil, err
} else if !validateDataIntegrity(y, length) {
Expand Down
3 changes: 2 additions & 1 deletion waku/v2/payload/waku_payload_crypto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package payload
import (
"crypto/aes"
"crypto/cipher"
crand "crypto/rand"
mrand "math/rand"
"testing"

Expand Down Expand Up @@ -84,7 +85,7 @@ func singlePaddingTest(t *testing.T, padSize int) {
Key: keyInfo,
}

_, err = mrand.Read(p.Padding) // nolint: gosec
_, err = crand.Read(p.Padding) // nolint: gosec
require.NoError(t, err)

encodedPayload, err := p.Encode(1)
Expand Down
2 changes: 0 additions & 2 deletions waku/v2/protocol/enr/localnode.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"math"
"math/rand"
"net"
"time"

"github.com/ethereum/go-ethereum/p2p/enode"
"github.com/ethereum/go-ethereum/p2p/enr"
Expand All @@ -28,7 +27,6 @@ func WithMultiaddress(multiaddrs ...multiaddr.Multiaddr) ENROption {
return func(localnode *enode.LocalNode) (err error) {

// Randomly shuffle multiaddresses
rand.Seed(time.Now().UnixNano())
rand.Shuffle(len(multiaddrs), func(i, j int) { multiaddrs[i], multiaddrs[j] = multiaddrs[j], multiaddrs[i] })

// Adding extra multiaddresses. Should probably not exceed the enr max size of 300bytes
Expand Down
5 changes: 4 additions & 1 deletion waku/v2/protocol/peer_exchange/enr_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ func newEnrCache(size int) (*enrCache, error) {
func (c *enrCache) updateCache(node *enode.Node) {
c.mu.Lock()
defer c.mu.Unlock()
c.data.Add(node.ID(), node)
currNode, ok := c.data.Get(node.ID())
if !ok || node.Seq() > currNode.(*enode.Node).Seq() {
c.data.Add(node.ID(), node)
}
}

// get `numPeers` records of enr
Expand Down
8 changes: 8 additions & 0 deletions waku/v2/utils/multiaddr.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,11 @@ func EncapsulatePeerID(peerID peer.ID, addrs ...multiaddr.Multiaddr) []multiaddr
}
return result
}

func MultiAddrSet(addr ...multiaddr.Multiaddr) map[multiaddr.Multiaddr]struct{} {
r := make(map[multiaddr.Multiaddr]struct{})
for _, a := range addr {
r[a] = struct{}{}
}
return r
}

0 comments on commit 83adec1

Please sign in to comment.