Skip to content

Commit

Permalink
Remove all dynamic peer discovery (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
gartnera authored Dec 16, 2024
1 parent 8535262 commit be92b20
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 399 deletions.
19 changes: 19 additions & 0 deletions conversion/tss_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package conversion

import (
"errors"
"fmt"
"math/rand"

"github.com/blang/semver"
"github.com/cometbft/cometbft/crypto/secp256k1"
sdk "github.com/cosmos/cosmos-sdk/types/bech32/legacybech32"
atypes "github.com/cosmos/cosmos-sdk/x/auth/vesting/types"
"github.com/libp2p/go-libp2p/core/peer"
maddr "github.com/multiformats/go-multiaddr"
)

// GetRandomPubKey for test
Expand Down Expand Up @@ -55,3 +57,20 @@ func VersionLTCheck(currentVer, expectedVer string) (bool, error) {
}
return v.LT(c), nil
}

// TestBootstrapPeers generates a list of bootstrap addresses for local tests
func TestBootstrapAddrs(ports []int, testPubKeys []string) ([]maddr.Multiaddr, error) {
res := make([]maddr.Multiaddr, len(ports))
for i := 0; i < len(ports); i++ {
peerId, err := Bech32PubkeyToPeerID(testPubKeys[i])
if err != nil {
return nil, fmt.Errorf("pubkey for peer %d is not valid %w", i, err)
}
peerAddr, err := maddr.NewMultiaddr(fmt.Sprintf("/ip4/127.0.0.1/tcp/%d/p2p/%s", ports[i], peerId.String()))
if err != nil {
return nil, fmt.Errorf("peer addr %d is not valid %w", i, err)
}
res[i] = peerAddr
}
return res, nil
}
8 changes: 3 additions & 5 deletions keygen/ecdsa/keygen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (

btsskeygen "github.com/bnb-chain/tss-lib/ecdsa/keygen"
btss "github.com/bnb-chain/tss-lib/tss"
maddr "github.com/multiformats/go-multiaddr"
. "gopkg.in/check.v1"

"gitlab.com/thorchain/tss/go-tss/common"
Expand Down Expand Up @@ -110,16 +109,15 @@ func (s *TssECDSAKeygenTestSuite) SetUpTest(c *C) {
s.partyNum = 4
s.comms = make([]*p2p.Communication, s.partyNum)
s.stateMgrs = make([]storage.LocalStateManager, s.partyNum)
bootstrapPeer := "/ip4/127.0.0.1/tcp/18666/p2p/16Uiu2HAm4TmEzUqy3q3Dv7HvdoSboHk5sFj2FH3npiN5vDbJC6gh"
multiAddr, err := maddr.NewMultiaddr(bootstrapPeer)
c.Assert(err, IsNil)
s.preParams = getPreparams(c)
whitelistedPeers := []peer.ID{}
for _, pk := range testPubKeys {
peer, err := conversion.Bech32PubkeyToPeerID(pk)
c.Assert(err, IsNil)
whitelistedPeers = append(whitelistedPeers, peer)
}
bootstrapPeers, err := conversion.TestBootstrapAddrs(ports, testPubKeys)
c.Assert(err, IsNil)
for i := 0; i < s.partyNum; i++ {
buf, err := base64.StdEncoding.DecodeString(testPriKeyArr[i])
c.Assert(err, IsNil)
Expand All @@ -130,7 +128,7 @@ func (s *TssECDSAKeygenTestSuite) SetUpTest(c *C) {
s.comms[i] = comm
continue
}
comm, err := p2p.NewCommunication([]maddr.Multiaddr{multiAddr}, ports[i], "", whitelistedPeers)
comm, err := p2p.NewCommunication(bootstrapPeers, ports[i], "", whitelistedPeers)
c.Assert(err, IsNil)
c.Assert(comm.Start(buf[:]), IsNil)
s.comms[i] = comm
Expand Down
8 changes: 3 additions & 5 deletions keygen/eddsa/keygen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"github.com/libp2p/go-libp2p/core/peer"

btss "github.com/bnb-chain/tss-lib/tss"
maddr "github.com/multiformats/go-multiaddr"
. "gopkg.in/check.v1"

"gitlab.com/thorchain/tss/go-tss/common"
Expand Down Expand Up @@ -97,15 +96,14 @@ func (s *EddsaKeygenTestSuite) SetUpTest(c *C) {
s.partyNum = 4
s.comms = make([]*p2p.Communication, s.partyNum)
s.stateMgrs = make([]storage.LocalStateManager, s.partyNum)
bootstrapPeer := "/ip4/127.0.0.1/tcp/19666/p2p/16Uiu2HAm4TmEzUqy3q3Dv7HvdoSboHk5sFj2FH3npiN5vDbJC6gh"
multiAddr, err := maddr.NewMultiaddr(bootstrapPeer)
c.Assert(err, IsNil)
whitelistedPeers := []peer.ID{}
for _, pk := range testPubKeys {
peer, err := conversion.Bech32PubkeyToPeerID(pk)
c.Assert(err, IsNil)
whitelistedPeers = append(whitelistedPeers, peer)
}
bootstrapPeers, err := conversion.TestBootstrapAddrs(ports, testPubKeys)
c.Assert(err, IsNil)
for i := 0; i < s.partyNum; i++ {
buf, err := base64.StdEncoding.DecodeString(testPriKeyArr[i])
c.Assert(err, IsNil)
Expand All @@ -116,7 +114,7 @@ func (s *EddsaKeygenTestSuite) SetUpTest(c *C) {
s.comms[i] = comm
continue
}
comm, err := p2p.NewCommunication([]maddr.Multiaddr{multiAddr}, ports[i], "", whitelistedPeers)
comm, err := p2p.NewCommunication(bootstrapPeers, ports[i], "", whitelistedPeers)
c.Assert(err, IsNil)
c.Assert(comm.Start(buf), IsNil)
s.comms[i] = comm
Expand Down
5 changes: 2 additions & 3 deletions keysign/ecdsa/keysign_old_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,7 @@ func (s *TssECDSAKeysignOldTestSuite) SetUpTest(c *C) {
s.partyNum = 4
s.comms = make([]*p2p.Communication, s.partyNum)
s.stateMgrs = make([]storage.LocalStateManager, s.partyNum)
bootstrapPeer := "/ip4/127.0.0.1/tcp/17666/p2p/16Uiu2HAm4TmEzUqy3q3Dv7HvdoSboHk5sFj2FH3npiN5vDbJC6gh"
multiAddr, err := maddr.NewMultiaddr(bootstrapPeer)
bootstrapPeers, err := conversion.TestBootstrapAddrs(ports, testPubKeys)
c.Assert(err, IsNil)
whitelistedPeers := []peer.ID{}
for _, pk := range testPubKeys {
Expand All @@ -142,7 +141,7 @@ func (s *TssECDSAKeysignOldTestSuite) SetUpTest(c *C) {
s.comms[i] = comm
continue
}
comm, err := p2p.NewCommunication([]maddr.Multiaddr{multiAddr}, ports[i], "", whitelistedPeers)
comm, err := p2p.NewCommunication(bootstrapPeers, ports[i], "", whitelistedPeers)
c.Assert(err, IsNil)
c.Assert(comm.Start(buf), IsNil)
s.comms[i] = comm
Expand Down
7 changes: 3 additions & 4 deletions keysign/ecdsa/keysign_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,16 +135,15 @@ func (s *TssECDSAKeysignTestSuite) SetUpTest(c *C) {
s.partyNum = 4
s.comms = make([]*p2p.Communication, s.partyNum)
s.stateMgrs = make([]storage.LocalStateManager, s.partyNum)
bootstrapPeer := "/ip4/127.0.0.1/tcp/17666/p2p/16Uiu2HAm4TmEzUqy3q3Dv7HvdoSboHk5sFj2FH3npiN5vDbJC6gh"
multiAddr, err := maddr.NewMultiaddr(bootstrapPeer)
c.Assert(err, IsNil)

whitelistedPeers := []peer.ID{}
for _, pk := range testPubKeys {
peer, err := conversion.Bech32PubkeyToPeerID(pk)
c.Assert(err, IsNil)
whitelistedPeers = append(whitelistedPeers, peer)
}
bootstrapPeers, err := conversion.TestBootstrapAddrs(ports, testPubKeys)
c.Assert(err, IsNil)
for i := 0; i < s.partyNum; i++ {
buf, err := base64.StdEncoding.DecodeString(testPriKeyArr[i])
c.Assert(err, IsNil)
Expand All @@ -155,7 +154,7 @@ func (s *TssECDSAKeysignTestSuite) SetUpTest(c *C) {
s.comms[i] = comm
continue
}
comm, err := p2p.NewCommunication([]maddr.Multiaddr{multiAddr}, ports[i], "", whitelistedPeers)
comm, err := p2p.NewCommunication(bootstrapPeers, ports[i], "", whitelistedPeers)
c.Assert(err, IsNil)
c.Assert(comm.Start(buf), IsNil)
s.comms[i] = comm
Expand Down
7 changes: 3 additions & 4 deletions keysign/eddsa/keysign_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,15 +133,14 @@ func (s *EddsaKeysignTestSuite) SetUpTest(c *C) {
s.partyNum = 4
s.comms = make([]*p2p.Communication, s.partyNum)
s.stateMgrs = make([]storage.LocalStateManager, s.partyNum)
bootstrapPeer := "/ip4/127.0.0.1/tcp/15666/p2p/16Uiu2HAm4TmEzUqy3q3Dv7HvdoSboHk5sFj2FH3npiN5vDbJC6gh"
multiAddr, err := maddr.NewMultiaddr(bootstrapPeer)
c.Assert(err, IsNil)
whitelistedPeers := []peer.ID{}
for _, pk := range testPubKeys {
peer, err := conversion.Bech32PubkeyToPeerID(pk)
c.Assert(err, IsNil)
whitelistedPeers = append(whitelistedPeers, peer)
}
bootstrapPeers, err := conversion.TestBootstrapAddrs(ports, testPubKeys)
c.Assert(err, IsNil)
for i := 0; i < s.partyNum; i++ {
buf, err := base64.StdEncoding.DecodeString(testPriKeyArr[i])
c.Assert(err, IsNil)
Expand All @@ -152,7 +151,7 @@ func (s *EddsaKeysignTestSuite) SetUpTest(c *C) {
s.comms[i] = comm
continue
}
comm, err := p2p.NewCommunication([]maddr.Multiaddr{multiAddr}, ports[i], "", whitelistedPeers)
comm, err := p2p.NewCommunication(bootstrapPeers, ports[i], "", whitelistedPeers)
c.Assert(err, IsNil)
c.Assert(comm.Start(buf), IsNil)
s.comms[i] = comm
Expand Down
8 changes: 0 additions & 8 deletions p2p/communication.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ type Communication struct {
externalAddr maddr.Multiaddr
streamMgr *StreamMgr
whitelistedPeers []peer.ID
discovery *PeerDiscovery
}

// NewCommunication create a new instance of Communication
Expand Down Expand Up @@ -248,7 +247,6 @@ func (c *Communication) bootStrapConnectivityCheck() error {
}

func (c *Communication) startChannel(privKeyBytes []byte) error {
c.logger.Info().Msgf("No DHT enabled; use private gossip instead.")
p2pPriKey, err := crypto.UnmarshalSecp256k1PrivateKey(privKeyBytes)
if err != nil {
c.logger.Error().Msgf("error is %f", err)
Expand Down Expand Up @@ -350,9 +348,6 @@ func (c *Communication) startChannel(privKeyBytes []byte) error {
}
bootstrapPeerAddrInfos = append(bootstrapPeerAddrInfos, *peerInfo)
}
discovery := NewPeerDiscovery(c.host, bootstrapPeerAddrInfos)
c.discovery = discovery
discovery.Start(context.Background())

return nil
}
Expand Down Expand Up @@ -422,9 +417,6 @@ func (c *Communication) Start(priKeyBytes []byte) error {

// Stop communication
func (c *Communication) Stop() error {
if c.discovery != nil {
c.discovery.Stop()
}
// we need to stop the handler and the p2p services firstly, then terminate the our communication threads
if err := c.host.Close(); err != nil {
c.logger.Err(err).Msg("fail to close host network")
Expand Down
Loading

0 comments on commit be92b20

Please sign in to comment.