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

Save last seen peers and use them as bootstrap upon restart #1877

Closed
wants to merge 18 commits into from
Closed
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
20 changes: 18 additions & 2 deletions cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"fmt"
"time"
"strings"
"io/ioutil"

"github.com/ipfs/go-log"
"github.com/keep-network/keep-common/pkg/chain/ethereum/ethutil"
Expand Down Expand Up @@ -111,11 +113,25 @@ func Start(c *cli.Context) error {
"contract has been authorized to operate on the stake",
)
}

ctx := context.Background()
//Past the last seen peers file path to context for use in keep-core/libp2p.go
ctx := context.WithValue(context.Background(), "peersPath", c.GlobalString("peers"))
networkPrivateKey, _ := key.OperatorKeyToNetworkKey(
operatorPrivateKey, operatorPublicKey,
)

//Read last seen peers file as a bytearray and convert to a slice of strings
s, err := ioutil.ReadFile(c.GlobalString("peers"))
if err != nil {
logger.Debugf("Error reading last seen peers file")
}
lastSeenPeersString := string(s[:])
lastSeenPeers := strings.Split(lastSeenPeersString, ",")

//If the slice of last seen peers slice is non empty, use it as bootstrap peers list
if len(lastSeenPeers) != 0 {
config.LibP2P.Peers = lastSeenPeers
}

netProvider, err := libp2p.Connect(
ctx,
config.LibP2P,
Expand Down
15 changes: 14 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,19 @@ import (
"github.com/urfave/cli"
)

const defaultConfigPath = "./config.toml"
const (
defaultConfigPath = "./config.toml"
//Default path to last seen peers file
defaultPeersListPath = ".peers.dat"
)

var (
version string
revision string

configPath string
//String for last seen peers path
peersListPath string

logger = log.Logger("keep-main")
)
Expand Down Expand Up @@ -56,6 +62,13 @@ func main() {
Destination: &configPath,
Usage: "full path to the configuration file",
},
//Define new flag to specify location of file where to store last seen peers
cli.StringFlag{
Name: "peers",
Value: defaultPeersListPath,
Destination: &peersListPath,
Usage: "full path to the last connected peers list file"
},
}
app.Commands = []cli.Command{
cmd.StartCommand,
Expand Down
28 changes: 28 additions & 0 deletions pkg/net/libp2p/libp2p.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"fmt"
"sync"
"time"
"io/ioutil"
"strings"

"github.com/ipfs/go-log"

Expand Down Expand Up @@ -219,6 +221,32 @@ func (cm *connectionManager) monitorConnectedPeers(ctx context.Context) {
case <-ticker.C:
connectedPeers := cm.ConnectedPeers()

//Slice for connected peers' addresses strings
var addrStrings []string
//Loop over connected peers' IDs
for _, peerID := range cm.Network().Peers() {
//PeerInfo gets a struc containing ID + array of multiaddresses for a given peer
info := cm.Peerstore().PeerInfo(peerID)
//Get multiaddresses for the current peer
peerAddrStrings := make([]string, 0, len(info.Addrs))
//For the set of multiaddresses of the current peer, construct an array of valid transport addresses strings
for _, multiaddr := range info.Addrs {
peerAddrStrings = append(peerAddrStrings, multiaddressWithIdentity(multiaddr, peerID))
}
//Append to slice of addresses of all connected peers
addrStrings = append(addrStrings, peerAddrStrings...)
}

//Get string of file path to write to
peersPath := fmt.Sprintf("%v", ctx.Value("peersPath"))
//Convert addresses slice to splitted string
addresses := strings.Join(addrStrings, ",")
//Write addresses string in specified file
err := ioutil.WriteFile(peersPath, []byte(addresses), 0644)
if err != nil {
logger.Debugf("Error writing peers addresses to file")
}

logger.Infof("number of connected peers: [%v]", len(connectedPeers))
logger.Debugf("connected peers: [%v]", connectedPeers)
case <-ctx.Done():
Expand Down