Skip to content

Commit 37db84d

Browse files
fix: replace DHT with private peer discovery (#34)
1 parent f9becbf commit 37db84d

File tree

17 files changed

+376
-125
lines changed

17 files changed

+376
-125
lines changed

cmd/tss/main.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ func main() {
5858
p2pConf.BootstrapPeers,
5959
p2pConf.Port,
6060
priKey,
61-
p2pConf.RendezvousString,
6261
baseFolder,
6362
tssConf,
6463
nil,

cmd/tss/mock_tss_server.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ package main
33
import (
44
"errors"
55

6+
"github.com/libp2p/go-libp2p/core/peer"
67
"gitlab.com/thorchain/tss/go-tss/blame"
78
"gitlab.com/thorchain/tss/go-tss/common"
89
"gitlab.com/thorchain/tss/go-tss/conversion"
910
"gitlab.com/thorchain/tss/go-tss/keygen"
1011
"gitlab.com/thorchain/tss/go-tss/keysign"
11-
"gitlab.com/thorchain/tss/go-tss/tss"
1212
)
1313

1414
type MockTssServer struct {
@@ -31,8 +31,8 @@ func (mts *MockTssServer) GetLocalPeerID() string {
3131
return conversion.GetRandomPeerID().String()
3232
}
3333

34-
func (mts *MockTssServer) GetKnownPeers() []tss.PeerInfo {
35-
return []tss.PeerInfo{}
34+
func (mts *MockTssServer) GetKnownPeers() []peer.AddrInfo {
35+
return []peer.AddrInfo{}
3636
}
3737

3838
func (mts *MockTssServer) Keygen(req keygen.Request) (keygen.Response, error) {

go.mod

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ require (
1414
github.com/gorilla/mux v1.8.0
1515
github.com/ipfs/go-log v1.0.5
1616
github.com/libp2p/go-libp2p v0.25.1
17-
github.com/libp2p/go-libp2p-kad-dht v0.21.0
1817
github.com/libp2p/go-libp2p-testing v0.12.0
1918
github.com/magiconair/properties v1.8.6
2019
github.com/multiformats/go-multiaddr v0.8.0
@@ -97,7 +96,6 @@ require (
9796
github.com/google/btree v1.1.2 // indirect
9897
github.com/google/go-cmp v0.6.0 // indirect
9998
github.com/google/gopacket v1.1.19 // indirect
100-
github.com/google/uuid v1.4.0 // indirect
10199
github.com/gorilla/websocket v1.5.0 // indirect
102100
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
103101
github.com/grpc-ecosystem/grpc-gateway v1.16.0 // indirect
@@ -114,14 +112,9 @@ require (
114112
github.com/huin/goupnp v1.0.3 // indirect
115113
github.com/inconshreveable/mousetrap v1.0.1 // indirect
116114
github.com/ipfs/go-cid v0.3.2 // indirect
117-
github.com/ipfs/go-datastore v0.6.0 // indirect
118-
github.com/ipfs/go-ipfs-util v0.0.2 // indirect
119-
github.com/ipfs/go-ipns v0.2.0 // indirect
120115
github.com/ipfs/go-log/v2 v2.5.1 // indirect
121-
github.com/ipld/go-ipld-prime v0.9.0 // indirect
122116
github.com/jackpal/go-nat-pmp v1.0.2 // indirect
123117
github.com/jbenet/go-temp-err-catcher v0.1.0 // indirect
124-
github.com/jbenet/goprocess v0.1.4 // indirect
125118
github.com/jmhodges/levigo v1.0.0 // indirect
126119
github.com/klauspost/compress v1.16.7 // indirect
127120
github.com/klauspost/cpuid/v2 v2.2.1 // indirect
@@ -132,8 +125,6 @@ require (
132125
github.com/libp2p/go-cidranger v1.1.0 // indirect
133126
github.com/libp2p/go-flow-metrics v0.1.0 // indirect
134127
github.com/libp2p/go-libp2p-asn-util v0.2.0 // indirect
135-
github.com/libp2p/go-libp2p-kbucket v0.5.0 // indirect
136-
github.com/libp2p/go-libp2p-record v0.2.0 // indirect
137128
github.com/libp2p/go-msgio v0.3.0 // indirect
138129
github.com/libp2p/go-nat v0.1.0 // indirect
139130
github.com/libp2p/go-netroute v0.2.1 // indirect
@@ -170,7 +161,6 @@ require (
170161
github.com/pelletier/go-toml/v2 v2.0.7 // indirect
171162
github.com/petermattis/goid v0.0.0-20230317030725-371a4b8eda08 // indirect
172163
github.com/pmezard/go-difflib v1.0.0 // indirect
173-
github.com/polydawn/refmt v0.0.0-20190807091052-3d65705ee9f1 // indirect
174164
github.com/prometheus/common v0.42.0 // indirect
175165
github.com/prometheus/procfs v0.9.0 // indirect
176166
github.com/raulk/go-watchdog v1.3.0 // indirect
@@ -189,11 +179,9 @@ require (
189179
github.com/tecbot/gorocksdb v0.0.0-20191217155057-f0fad39f321c // indirect
190180
github.com/tendermint/go-amino v0.16.0 // indirect
191181
github.com/tidwall/btree v1.6.0 // indirect
192-
github.com/whyrusleeping/go-keyspace v0.0.0-20160322163242-5b898ac5add1 // indirect
193182
github.com/zondax/hid v0.9.2 // indirect
194183
github.com/zondax/ledger-go v0.14.3 // indirect
195184
go.etcd.io/bbolt v1.3.7 // indirect
196-
go.opencensus.io v0.24.0 // indirect
197185
go.uber.org/dig v1.15.0 // indirect
198186
go.uber.org/fx v1.18.2 // indirect
199187
go.uber.org/multierr v1.8.0 // indirect

go.sum

Lines changed: 0 additions & 48 deletions
Large diffs are not rendered by default.

keygen/ecdsa/keygen_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,13 @@ func (s *TssECDSAKeygenTestSuite) SetUpTest(c *C) {
124124
buf, err := base64.StdEncoding.DecodeString(testPriKeyArr[i])
125125
c.Assert(err, IsNil)
126126
if i == 0 {
127-
comm, err := p2p.NewCommunication("asgard", nil, ports[i], "", whitelistedPeers)
127+
comm, err := p2p.NewCommunication(nil, ports[i], "", whitelistedPeers)
128128
c.Assert(err, IsNil)
129129
c.Assert(comm.Start(buf[:]), IsNil)
130130
s.comms[i] = comm
131131
continue
132132
}
133-
comm, err := p2p.NewCommunication("asgard", []maddr.Multiaddr{multiAddr}, ports[i], "", whitelistedPeers)
133+
comm, err := p2p.NewCommunication([]maddr.Multiaddr{multiAddr}, ports[i], "", whitelistedPeers)
134134
c.Assert(err, IsNil)
135135
c.Assert(comm.Start(buf[:]), IsNil)
136136
s.comms[i] = comm

keygen/eddsa/keygen_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,13 @@ func (s *EddsaKeygenTestSuite) SetUpTest(c *C) {
110110
buf, err := base64.StdEncoding.DecodeString(testPriKeyArr[i])
111111
c.Assert(err, IsNil)
112112
if i == 0 {
113-
comm, err := p2p.NewCommunication("asgard", nil, ports[i], "", whitelistedPeers)
113+
comm, err := p2p.NewCommunication(nil, ports[i], "", whitelistedPeers)
114114
c.Assert(err, IsNil)
115115
c.Assert(comm.Start(buf), IsNil)
116116
s.comms[i] = comm
117117
continue
118118
}
119-
comm, err := p2p.NewCommunication("asgard", []maddr.Multiaddr{multiAddr}, ports[i], "", whitelistedPeers)
119+
comm, err := p2p.NewCommunication([]maddr.Multiaddr{multiAddr}, ports[i], "", whitelistedPeers)
120120
c.Assert(err, IsNil)
121121
c.Assert(comm.Start(buf), IsNil)
122122
s.comms[i] = comm

keysign/ecdsa/keysign_old_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,13 @@ func (s *TssECDSAKeysignOldTestSuite) SetUpTest(c *C) {
136136
buf, err := base64.StdEncoding.DecodeString(testPriKeyArr[i])
137137
c.Assert(err, IsNil)
138138
if i == 0 {
139-
comm, err := p2p.NewCommunication("asgard", nil, ports[i], "", whitelistedPeers)
139+
comm, err := p2p.NewCommunication(nil, ports[i], "", whitelistedPeers)
140140
c.Assert(err, IsNil)
141141
c.Assert(comm.Start(buf), IsNil)
142142
s.comms[i] = comm
143143
continue
144144
}
145-
comm, err := p2p.NewCommunication("asgard", []maddr.Multiaddr{multiAddr}, ports[i], "", whitelistedPeers)
145+
comm, err := p2p.NewCommunication([]maddr.Multiaddr{multiAddr}, ports[i], "", whitelistedPeers)
146146
c.Assert(err, IsNil)
147147
c.Assert(comm.Start(buf), IsNil)
148148
s.comms[i] = comm

keysign/ecdsa/keysign_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,13 @@ func (s *TssECDSAKeysignTestSuite) SetUpTest(c *C) {
149149
buf, err := base64.StdEncoding.DecodeString(testPriKeyArr[i])
150150
c.Assert(err, IsNil)
151151
if i == 0 {
152-
comm, err := p2p.NewCommunication("asgard", nil, ports[i], "", whitelistedPeers)
152+
comm, err := p2p.NewCommunication(nil, ports[i], "", whitelistedPeers)
153153
c.Assert(err, IsNil)
154154
c.Assert(comm.Start(buf), IsNil)
155155
s.comms[i] = comm
156156
continue
157157
}
158-
comm, err := p2p.NewCommunication("asgard", []maddr.Multiaddr{multiAddr}, ports[i], "", whitelistedPeers)
158+
comm, err := p2p.NewCommunication([]maddr.Multiaddr{multiAddr}, ports[i], "", whitelistedPeers)
159159
c.Assert(err, IsNil)
160160
c.Assert(comm.Start(buf), IsNil)
161161
s.comms[i] = comm

keysign/eddsa/keysign_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,13 @@ func (s *EddsaKeysignTestSuite) SetUpTest(c *C) {
146146
buf, err := base64.StdEncoding.DecodeString(testPriKeyArr[i])
147147
c.Assert(err, IsNil)
148148
if i == 0 {
149-
comm, err := p2p.NewCommunication("asgard", nil, ports[i], "", whitelistedPeers)
149+
comm, err := p2p.NewCommunication(nil, ports[i], "", whitelistedPeers)
150150
c.Assert(err, IsNil)
151151
c.Assert(comm.Start(buf), IsNil)
152152
s.comms[i] = comm
153153
continue
154154
}
155-
comm, err := p2p.NewCommunication("asgard", []maddr.Multiaddr{multiAddr}, ports[i], "", whitelistedPeers)
155+
comm, err := p2p.NewCommunication([]maddr.Multiaddr{multiAddr}, ports[i], "", whitelistedPeers)
156156
c.Assert(err, IsNil)
157157
c.Assert(comm.Start(buf), IsNil)
158158
s.comms[i] = comm

p2p/communication.go

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,11 @@ import (
1010
"time"
1111

1212
libp2p "github.com/libp2p/go-libp2p"
13-
dht "github.com/libp2p/go-libp2p-kad-dht"
1413
"github.com/libp2p/go-libp2p/core/crypto"
1514
"github.com/libp2p/go-libp2p/core/host"
1615
"github.com/libp2p/go-libp2p/core/network"
1716
"github.com/libp2p/go-libp2p/core/peer"
1817
"github.com/libp2p/go-libp2p/core/protocol"
19-
discovery_routing "github.com/libp2p/go-libp2p/p2p/discovery/routing"
20-
discovery_util "github.com/libp2p/go-libp2p/p2p/discovery/util"
2118
rcmgr "github.com/libp2p/go-libp2p/p2p/host/resource-manager"
2219
"github.com/libp2p/go-libp2p/p2p/net/connmgr"
2320
"github.com/libp2p/go-libp2p/p2p/protocol/ping"
@@ -49,7 +46,6 @@ type Message struct {
4946

5047
// Communication use p2p to broadcast messages among all the TSS nodes
5148
type Communication struct {
52-
rendezvous string // based on group
5349
bootstrapPeers []maddr.Multiaddr
5450
logger zerolog.Logger
5551
listenAddr maddr.Multiaddr
@@ -63,11 +59,11 @@ type Communication struct {
6359
externalAddr maddr.Multiaddr
6460
streamMgr *StreamMgr
6561
whitelistedPeers []peer.ID
62+
discovery *PeerDiscovery
6663
}
6764

6865
// NewCommunication create a new instance of Communication
6966
func NewCommunication(
70-
rendezvous string,
7167
bootstrapPeers []maddr.Multiaddr,
7268
port int,
7369
externalIP string,
@@ -85,7 +81,7 @@ func NewCommunication(
8581
}
8682
}
8783
return &Communication{
88-
rendezvous: rendezvous,
84+
8985
bootstrapPeers: bootstrapPeers,
9086
logger: log.With().Str("module", "communication").Logger(),
9187
listenAddr: addr,
@@ -252,7 +248,7 @@ func (c *Communication) bootStrapConnectivityCheck() error {
252248
}
253249

254250
func (c *Communication) startChannel(privKeyBytes []byte) error {
255-
ctx := context.Background()
251+
c.logger.Info().Msgf("No DHT enabled; use private gossip instead.")
256252
p2pPriKey, err := crypto.UnmarshalSecp256k1PrivateKey(privKeyBytes)
257253
if err != nil {
258254
c.logger.Error().Msgf("error is %f", err)
@@ -310,6 +306,7 @@ func (c *Communication) startChannel(privKeyBytes []byte) error {
310306
libp2p.ResourceManager(m),
311307
libp2p.ConnectionManager(cmgr),
312308
libp2p.ConnectionGater(NewWhitelistConnectionGater(c.whitelistedPeers, c.logger)),
309+
libp2p.DisableRelay(),
313310
)
314311
if err != nil {
315312
return fmt.Errorf("fail to create p2p host: %w", err)
@@ -321,14 +318,6 @@ func (c *Communication) startChannel(privKeyBytes []byte) error {
321318
// client because we want each peer to maintain its own local copy of the
322319
// DHT, so that the bootstrapping node of the DHT can go down without
323320
// inhibiting future peer discovery.
324-
kademliaDHT, err := dht.New(ctx, h, dht.Mode(dht.ModeServer))
325-
if err != nil {
326-
return fmt.Errorf("fail to create DHT: %w", err)
327-
}
328-
c.logger.Debug().Msg("Bootstrapping the DHT")
329-
if err = kademliaDHT.Bootstrap(ctx); err != nil {
330-
return fmt.Errorf("fail to bootstrap DHT: %w", err)
331-
}
332321

333322
var connectionErr error
334323
for i := 0; i < 5; i++ {
@@ -343,30 +332,28 @@ func (c *Communication) startChannel(privKeyBytes []byte) error {
343332
return fmt.Errorf("fail to connect to bootstrap peer: %w", connectionErr)
344333
}
345334

346-
// We use a rendezvous point "meet me here" to announce our location.
347-
// This is like telling your friends to meet you at the Eiffel Tower.
348-
routingDiscovery := discovery_routing.NewRoutingDiscovery(kademliaDHT)
349-
discovery_util.Advertise(ctx, routingDiscovery, c.rendezvous)
350-
351-
// Create a goroutine to shut down the DHT after 5 minutes
352-
go func() {
353-
select {
354-
case <-time.After(5 * time.Minute):
355-
c.logger.Info().Msg("Closing Kademlia DHT after 5 minutes")
356-
if err := kademliaDHT.Close(); err != nil {
357-
c.logger.Error().Err(err).Msg("Failed to close Kademlia DHT")
358-
}
359-
case <-ctx.Done():
360-
c.logger.Info().Msg("Context done, not waiting for 5 minutes to close DHT")
361-
}
362-
}()
363-
364335
err = c.bootStrapConnectivityCheck()
365336
if err != nil {
366337
return err
367338
}
368339

369340
c.logger.Info().Msg("Successfully announced!")
341+
342+
c.logger.Info().Msg("Start peer discovery/gossip...")
343+
//c.bootstrapPeers
344+
bootstrapPeerAddrInfos := make([]peer.AddrInfo, 0, len(c.bootstrapPeers))
345+
for _, addr := range c.bootstrapPeers {
346+
peerInfo, err := peer.AddrInfoFromP2pAddr(addr)
347+
if err != nil {
348+
c.logger.Error().Err(err).Msgf("fail to convert multiaddr to peer info: %s", addr)
349+
continue
350+
}
351+
bootstrapPeerAddrInfos = append(bootstrapPeerAddrInfos, *peerInfo)
352+
}
353+
discovery := NewPeerDiscovery(c.host, bootstrapPeerAddrInfos)
354+
c.discovery = discovery
355+
discovery.Start(context.Background())
356+
370357
return nil
371358
}
372359

@@ -435,6 +422,9 @@ func (c *Communication) Start(priKeyBytes []byte) error {
435422

436423
// Stop communication
437424
func (c *Communication) Stop() error {
425+
if c.discovery != nil {
426+
c.discovery.Stop()
427+
}
438428
// we need to stop the handler and the p2p services firstly, then terminate the our communication threads
439429
if err := c.host.Close(); err != nil {
440430
c.logger.Err(err).Msg("fail to close host network")

0 commit comments

Comments
 (0)