-
Notifications
You must be signed in to change notification settings - Fork 1
/
bootstrap.go
67 lines (60 loc) · 1.65 KB
/
bootstrap.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package p2p
import (
"context"
"github.com/Futuremine-chain/future/tools/crypto/ecc/secp256k1"
log "github.com/Futuremine-chain/future/tools/log/log15"
"github.com/libp2p/go-libp2p-core/peer"
dht "github.com/libp2p/go-libp2p-kad-dht"
"github.com/multiformats/go-multiaddr"
"strings"
)
// Default boot node list
var DefaultBootPeers []multiaddr.Multiaddr
// Custom boot node list
var CustomBootPeers []multiaddr.Multiaddr
func init() {
// FMgzPJfbVqY1dWWXcguqJas9VvDKQZHYoE1
for _, s := range []string{
//"/ip4/18.163.21.69/tcp/19100/ipfs/16Uiu2HAmLxC6SFTQfoy68wGGfU8rt7joSVPBab6jQ4kN2kZuNfc4",
"/ip4/18.163.21.69/tcp/19100/ipfs/16Uiu2HAmCqGE6sNZC9FHEkJ2YbkBGQmEsmgHomVa2TSuMTQZ67T7",
} {
ma, err := multiaddr.NewMultiaddr(s)
if err != nil {
panic(err)
}
DefaultBootPeers = append(DefaultBootPeers, ma)
}
}
func IsBootPeers(id peer.ID) bool {
bootstrap := DefaultBootPeers
if len(CustomBootPeers) > 0 {
bootstrap = CustomBootPeers
}
for _, bootstrap := range bootstrap {
if id.String() == strings.Split(bootstrap.String(), "/")[6] {
return true
}
}
return false
}
func NewBoot(ip, port, external string, private *secp256k1.PrivateKey) (*P2p, error) {
host, err := NewP2PHost(private, ip, port, external)
if err != nil {
return nil, err
}
p2p := &P2p{host: host}
log.Info("Host created", "id", p2p.host.ID(), "address", p2p.host.Addrs())
return p2p, nil
}
func (p *P2p) StartBoot() error {
var err error
p.dht, err = dht.New(context.Background(), p.host)
if err != nil {
return err
}
log.Info("Start the boot node", "module", module)
if err = p.dht.Bootstrap(context.Background()); err != nil {
return err
}
return nil
}