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

fix: replace DHT with private peer discovery #34

Merged
merged 22 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
7d2b4f3
removing dht code from communication constructor add custom peer disc…
brewmaster012 Oct 23, 2024
0c977fc
fix linter complaints
brewmaster012 Oct 25, 2024
064fcc6
remove unnecessary arguments
brewmaster012 Oct 25, 2024
7e55943
add unit test
brewmaster012 Oct 25, 2024
50a27bd
remove rendezvous; no longer needed as DHT is removed
brewmaster012 Oct 25, 2024
d3ee7da
disable relay protocol
brewmaster012 Oct 27, 2024
c18043d
cleanup go.mod
brewmaster012 Oct 28, 2024
243c7de
merged with tip with #31, connection gater PR
brewmaster012 Oct 28, 2024
01921b3
WIP: fix some unit tests
brewmaster012 Oct 29, 2024
dbaee46
fix keygen/keysign unit tests
brewmaster012 Oct 29, 2024
13177a3
limit read size in stream
brewmaster012 Oct 29, 2024
ef1479e
make gossip connections concurrent
brewmaster012 Oct 29, 2024
3537fb6
increase timeout in gossip connections
brewmaster012 Oct 29, 2024
f87f1a1
add missing continue/return in gossip
brewmaster012 Oct 29, 2024
1d6e788
fix a bug of capturing loop variable in closure
brewmaster012 Oct 29, 2024
4441df3
make linter happy by renaming
brewmaster012 Oct 29, 2024
a9757e6
make linter happy again
brewmaster012 Oct 29, 2024
dae1f21
allow multiple addrs in gossip for the same peer
brewmaster012 Oct 29, 2024
694f4b4
use structured log if appropriate
brewmaster012 Oct 29, 2024
ba90276
fix unlimited growing of addrs in peer info
brewmaster012 Oct 29, 2024
e53a71f
Merge remote-tracking branch 'origin/master' into dht-removal
brewmaster012 Oct 29, 2024
d3ccaf1
review comments
brewmaster012 Oct 31, 2024
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
1 change: 0 additions & 1 deletion cmd/tss/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ func main() {
p2pConf.BootstrapPeers,
p2pConf.Port,
priKey,
p2pConf.RendezvousString,
baseFolder,
tssConf,
nil,
Expand Down
12 changes: 0 additions & 12 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ require (
github.com/gorilla/mux v1.8.0
github.com/ipfs/go-log v1.0.5
github.com/libp2p/go-libp2p v0.25.1
github.com/libp2p/go-libp2p-kad-dht v0.21.0
github.com/libp2p/go-libp2p-testing v0.12.0
github.com/magiconair/properties v1.8.6
github.com/multiformats/go-multiaddr v0.8.0
Expand Down Expand Up @@ -97,7 +96,6 @@ require (
github.com/google/btree v1.1.2 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/gopacket v1.1.19 // indirect
github.com/google/uuid v1.4.0 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
github.com/grpc-ecosystem/grpc-gateway v1.16.0 // indirect
Expand All @@ -114,14 +112,9 @@ require (
github.com/huin/goupnp v1.0.3 // indirect
github.com/inconshreveable/mousetrap v1.0.1 // indirect
github.com/ipfs/go-cid v0.3.2 // indirect
github.com/ipfs/go-datastore v0.6.0 // indirect
github.com/ipfs/go-ipfs-util v0.0.2 // indirect
github.com/ipfs/go-ipns v0.2.0 // indirect
github.com/ipfs/go-log/v2 v2.5.1 // indirect
github.com/ipld/go-ipld-prime v0.9.0 // indirect
github.com/jackpal/go-nat-pmp v1.0.2 // indirect
github.com/jbenet/go-temp-err-catcher v0.1.0 // indirect
github.com/jbenet/goprocess v0.1.4 // indirect
github.com/jmhodges/levigo v1.0.0 // indirect
github.com/klauspost/compress v1.16.7 // indirect
github.com/klauspost/cpuid/v2 v2.2.1 // indirect
Expand All @@ -132,8 +125,6 @@ require (
github.com/libp2p/go-cidranger v1.1.0 // indirect
github.com/libp2p/go-flow-metrics v0.1.0 // indirect
github.com/libp2p/go-libp2p-asn-util v0.2.0 // indirect
github.com/libp2p/go-libp2p-kbucket v0.5.0 // indirect
github.com/libp2p/go-libp2p-record v0.2.0 // indirect
github.com/libp2p/go-msgio v0.3.0 // indirect
github.com/libp2p/go-nat v0.1.0 // indirect
github.com/libp2p/go-netroute v0.2.1 // indirect
Expand Down Expand Up @@ -170,7 +161,6 @@ require (
github.com/pelletier/go-toml/v2 v2.0.7 // indirect
github.com/petermattis/goid v0.0.0-20230317030725-371a4b8eda08 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/polydawn/refmt v0.0.0-20190807091052-3d65705ee9f1 // indirect
github.com/prometheus/common v0.42.0 // indirect
github.com/prometheus/procfs v0.9.0 // indirect
github.com/raulk/go-watchdog v1.3.0 // indirect
Expand All @@ -189,11 +179,9 @@ require (
github.com/tecbot/gorocksdb v0.0.0-20191217155057-f0fad39f321c // indirect
github.com/tendermint/go-amino v0.16.0 // indirect
github.com/tidwall/btree v1.6.0 // indirect
github.com/whyrusleeping/go-keyspace v0.0.0-20160322163242-5b898ac5add1 // indirect
github.com/zondax/hid v0.9.2 // indirect
github.com/zondax/ledger-go v0.14.3 // indirect
go.etcd.io/bbolt v1.3.7 // indirect
go.opencensus.io v0.24.0 // indirect
go.uber.org/dig v1.15.0 // indirect
go.uber.org/fx v1.18.2 // indirect
go.uber.org/multierr v1.8.0 // indirect
Expand Down
48 changes: 0 additions & 48 deletions go.sum

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions keygen/ecdsa/keygen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,13 @@ func (s *TssECDSAKeygenTestSuite) SetUpTest(c *C) {
buf, err := base64.StdEncoding.DecodeString(testPriKeyArr[i])
c.Assert(err, IsNil)
if i == 0 {
comm, err := p2p.NewCommunication("asgard", nil, ports[i], "", whitelistedPeers)
comm, err := p2p.NewCommunication(nil, ports[i], "", whitelistedPeers)
c.Assert(err, IsNil)
c.Assert(comm.Start(buf[:]), IsNil)
s.comms[i] = comm
continue
}
comm, err := p2p.NewCommunication("asgard", []maddr.Multiaddr{multiAddr}, ports[i], "", whitelistedPeers)
comm, err := p2p.NewCommunication([]maddr.Multiaddr{multiAddr}, ports[i], "", whitelistedPeers)
c.Assert(err, IsNil)
c.Assert(comm.Start(buf[:]), IsNil)
s.comms[i] = comm
Expand Down
4 changes: 2 additions & 2 deletions keygen/eddsa/keygen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,13 @@ func (s *EddsaKeygenTestSuite) SetUpTest(c *C) {
buf, err := base64.StdEncoding.DecodeString(testPriKeyArr[i])
c.Assert(err, IsNil)
if i == 0 {
comm, err := p2p.NewCommunication("asgard", nil, ports[i], "", whitelistedPeers)
comm, err := p2p.NewCommunication(nil, ports[i], "", whitelistedPeers)
c.Assert(err, IsNil)
c.Assert(comm.Start(buf), IsNil)
s.comms[i] = comm
continue
}
comm, err := p2p.NewCommunication("asgard", []maddr.Multiaddr{multiAddr}, ports[i], "", whitelistedPeers)
comm, err := p2p.NewCommunication([]maddr.Multiaddr{multiAddr}, ports[i], "", whitelistedPeers)
c.Assert(err, IsNil)
c.Assert(comm.Start(buf), IsNil)
s.comms[i] = comm
Expand Down
4 changes: 2 additions & 2 deletions keysign/ecdsa/keysign_old_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,13 @@ func (s *TssECDSAKeysignOldTestSuite) SetUpTest(c *C) {
buf, err := base64.StdEncoding.DecodeString(testPriKeyArr[i])
c.Assert(err, IsNil)
if i == 0 {
comm, err := p2p.NewCommunication("asgard", nil, ports[i], "", whitelistedPeers)
comm, err := p2p.NewCommunication(nil, ports[i], "", whitelistedPeers)
c.Assert(err, IsNil)
c.Assert(comm.Start(buf), IsNil)
s.comms[i] = comm
continue
}
comm, err := p2p.NewCommunication("asgard", []maddr.Multiaddr{multiAddr}, ports[i], "", whitelistedPeers)
comm, err := p2p.NewCommunication([]maddr.Multiaddr{multiAddr}, ports[i], "", whitelistedPeers)
c.Assert(err, IsNil)
c.Assert(comm.Start(buf), IsNil)
s.comms[i] = comm
Expand Down
4 changes: 2 additions & 2 deletions keysign/ecdsa/keysign_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,13 @@ func (s *TssECDSAKeysignTestSuite) SetUpTest(c *C) {
buf, err := base64.StdEncoding.DecodeString(testPriKeyArr[i])
c.Assert(err, IsNil)
if i == 0 {
comm, err := p2p.NewCommunication("asgard", nil, ports[i], "", whitelistedPeers)
comm, err := p2p.NewCommunication(nil, ports[i], "", whitelistedPeers)
c.Assert(err, IsNil)
c.Assert(comm.Start(buf), IsNil)
s.comms[i] = comm
continue
}
comm, err := p2p.NewCommunication("asgard", []maddr.Multiaddr{multiAddr}, ports[i], "", whitelistedPeers)
comm, err := p2p.NewCommunication([]maddr.Multiaddr{multiAddr}, ports[i], "", whitelistedPeers)
c.Assert(err, IsNil)
c.Assert(comm.Start(buf), IsNil)
s.comms[i] = comm
Expand Down
4 changes: 2 additions & 2 deletions keysign/eddsa/keysign_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,13 @@ func (s *EddsaKeysignTestSuite) SetUpTest(c *C) {
buf, err := base64.StdEncoding.DecodeString(testPriKeyArr[i])
c.Assert(err, IsNil)
if i == 0 {
comm, err := p2p.NewCommunication("asgard", nil, ports[i], "", whitelistedPeers)
comm, err := p2p.NewCommunication(nil, ports[i], "", whitelistedPeers)
c.Assert(err, IsNil)
c.Assert(comm.Start(buf), IsNil)
s.comms[i] = comm
continue
}
comm, err := p2p.NewCommunication("asgard", []maddr.Multiaddr{multiAddr}, ports[i], "", whitelistedPeers)
comm, err := p2p.NewCommunication([]maddr.Multiaddr{multiAddr}, ports[i], "", whitelistedPeers)
c.Assert(err, IsNil)
c.Assert(comm.Start(buf), IsNil)
s.comms[i] = comm
Expand Down
51 changes: 18 additions & 33 deletions p2p/communication.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,11 @@ import (
"time"

libp2p "github.com/libp2p/go-libp2p"
dht "github.com/libp2p/go-libp2p-kad-dht"
"github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/protocol"
discovery_routing "github.com/libp2p/go-libp2p/p2p/discovery/routing"
discovery_util "github.com/libp2p/go-libp2p/p2p/discovery/util"
rcmgr "github.com/libp2p/go-libp2p/p2p/host/resource-manager"
"github.com/libp2p/go-libp2p/p2p/net/connmgr"
"github.com/libp2p/go-libp2p/p2p/protocol/ping"
Expand Down Expand Up @@ -49,7 +46,6 @@ type Message struct {

// Communication use p2p to broadcast messages among all the TSS nodes
type Communication struct {
rendezvous string // based on group
bootstrapPeers []maddr.Multiaddr
logger zerolog.Logger
listenAddr maddr.Multiaddr
Expand All @@ -67,7 +63,6 @@ type Communication struct {

// NewCommunication create a new instance of Communication
func NewCommunication(
rendezvous string,
bootstrapPeers []maddr.Multiaddr,
port int,
externalIP string,
Expand All @@ -85,7 +80,7 @@ func NewCommunication(
}
}
return &Communication{
rendezvous: rendezvous,

bootstrapPeers: bootstrapPeers,
logger: log.With().Str("module", "communication").Logger(),
listenAddr: addr,
Expand Down Expand Up @@ -252,7 +247,7 @@ func (c *Communication) bootStrapConnectivityCheck() error {
}

func (c *Communication) startChannel(privKeyBytes []byte) error {
ctx := context.Background()
c.logger.Warn().Msgf("No DHT enabled")
brewmaster012 marked this conversation as resolved.
Show resolved Hide resolved
p2pPriKey, err := crypto.UnmarshalSecp256k1PrivateKey(privKeyBytes)
if err != nil {
c.logger.Error().Msgf("error is %f", err)
Expand Down Expand Up @@ -310,6 +305,7 @@ func (c *Communication) startChannel(privKeyBytes []byte) error {
libp2p.ResourceManager(m),
libp2p.ConnectionManager(cmgr),
libp2p.ConnectionGater(NewWhitelistConnectionGater(c.whitelistedPeers, c.logger)),
libp2p.DisableRelay(),
)
if err != nil {
return fmt.Errorf("fail to create p2p host: %w", err)
Expand All @@ -321,14 +317,6 @@ func (c *Communication) startChannel(privKeyBytes []byte) error {
// client because we want each peer to maintain its own local copy of the
// DHT, so that the bootstrapping node of the DHT can go down without
// inhibiting future peer discovery.
kademliaDHT, err := dht.New(ctx, h, dht.Mode(dht.ModeServer))
if err != nil {
return fmt.Errorf("fail to create DHT: %w", err)
}
c.logger.Debug().Msg("Bootstrapping the DHT")
if err = kademliaDHT.Bootstrap(ctx); err != nil {
return fmt.Errorf("fail to bootstrap DHT: %w", err)
}

var connectionErr error
for i := 0; i < 5; i++ {
Expand All @@ -343,30 +331,27 @@ func (c *Communication) startChannel(privKeyBytes []byte) error {
return fmt.Errorf("fail to connect to bootstrap peer: %w", connectionErr)
}

// We use a rendezvous point "meet me here" to announce our location.
// This is like telling your friends to meet you at the Eiffel Tower.
routingDiscovery := discovery_routing.NewRoutingDiscovery(kademliaDHT)
discovery_util.Advertise(ctx, routingDiscovery, c.rendezvous)

// Create a goroutine to shut down the DHT after 5 minutes
go func() {
select {
case <-time.After(5 * time.Minute):
c.logger.Info().Msg("Closing Kademlia DHT after 5 minutes")
if err := kademliaDHT.Close(); err != nil {
c.logger.Error().Err(err).Msg("Failed to close Kademlia DHT")
}
case <-ctx.Done():
c.logger.Info().Msg("Context done, not waiting for 5 minutes to close DHT")
}
}()

err = c.bootStrapConnectivityCheck()
if err != nil {
return err
}

c.logger.Info().Msg("Successfully announced!")

c.logger.Info().Msg("Start peer discovery/gossip...")
//c.bootstrapPeers
bootstrapPeerAddrInfos := make([]peer.AddrInfo, 0, len(c.bootstrapPeers))
for _, addr := range c.bootstrapPeers {
peerInfo, err := peer.AddrInfoFromP2pAddr(addr)
if err != nil {
c.logger.Error().Err(err).Msgf("fail to convert multiaddr to peer info: %s", addr)
continue
}
bootstrapPeerAddrInfos = append(bootstrapPeerAddrInfos, *peerInfo)
}
discovery := NewPeerDiscovery(c.host, bootstrapPeerAddrInfos)
discovery.Start(context.Background())
brewmaster012 marked this conversation as resolved.
Show resolved Hide resolved

brewmaster012 marked this conversation as resolved.
Show resolved Hide resolved
return nil
}

Expand Down
10 changes: 5 additions & 5 deletions p2p/communication_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
var _ = Suite(&CommunicationTestSuite{})

func (CommunicationTestSuite) TestBasicCommunication(c *C) {
comm, err := NewCommunication("rendezvous", nil, 6668, "", []peer.ID{})
comm, err := NewCommunication(nil, 6668, "", []peer.ID{})
c.Assert(err, IsNil)
c.Assert(comm, NotNil)
comm.SetSubscribe(messages.TSSKeyGenMsg, "hello", make(chan *Message))
Expand Down Expand Up @@ -60,13 +60,13 @@
c.Assert(err, IsNil)
privKey, err := base64.StdEncoding.DecodeString(bootstrapPrivKey)
c.Assert(err, IsNil)
comm, err := NewCommunication("commTest", nil, 2220, fakeExternalIP, whitelistedPeers)
comm, err := NewCommunication(nil, 2220, fakeExternalIP, whitelistedPeers)
c.Assert(err, IsNil)
c.Assert(comm.Start(privKey), IsNil)

defer comm.Stop()
c.Assert(err, IsNil)
comm2, err := NewCommunication("commTest", []maddr.Multiaddr{validMultiAddr}, 2221, "", whitelistedPeers)
comm2, err := NewCommunication([]maddr.Multiaddr{validMultiAddr}, 2221, "", whitelistedPeers)
c.Assert(err, IsNil)
err = comm2.Start(sk1raw)
c.Assert(err, IsNil)
Expand All @@ -76,14 +76,14 @@
invalidAddr := "/ip4/127.0.0.1/tcp/2220/p2p/" + id2.String()
invalidMultiAddr, err := maddr.NewMultiaddr(invalidAddr)
c.Assert(err, IsNil)
comm3, err := NewCommunication("commTest", []maddr.Multiaddr{invalidMultiAddr}, 2222, "", whitelistedPeers)
comm3, err := NewCommunication([]maddr.Multiaddr{invalidMultiAddr}, 2222, "", whitelistedPeers)
c.Assert(err, IsNil)
err = comm3.Start(sk1raw)
c.Assert(err, ErrorMatches, "fail to connect to bootstrap peer: fail to connect to any peer")
defer comm3.Stop()

// we connect to one invalid and one valid address
comm4, err := NewCommunication("commTest", []maddr.Multiaddr{invalidMultiAddr, validMultiAddr}, 2223, "", whitelistedPeers)
comm4, err := NewCommunication([]maddr.Multiaddr{invalidMultiAddr, validMultiAddr}, 2223, "", whitelistedPeers)
c.Assert(err, IsNil)
err = comm4.Start(sk1raw)
c.Assert(err, IsNil)
Expand All @@ -97,7 +97,7 @@
c.Assert(checkExist(ps.Addrs(comm.host.ID()), fakeExternalMultiAddr), Equals, true)

// same as above, just without whitelisted peers
comm5, err := NewCommunication("commTest", []maddr.Multiaddr{invalidMultiAddr, validMultiAddr}, 2224, "", []peer.ID{})

Check failure on line 100 in p2p/communication_test.go

View workflow job for this annotation

GitHub Actions / test go 1.20

too many arguments in call to NewCommunication

Check failure on line 100 in p2p/communication_test.go

View workflow job for this annotation

GitHub Actions / test go 1.22

too many arguments in call to NewCommunication
c.Assert(err, IsNil)
err = comm5.Start(sk1raw)
c.Assert(err, ErrorMatches, "fail to connect to bootstrap peer: fail to connect to any peer")
Expand Down
Loading
Loading