-
Notifications
You must be signed in to change notification settings - Fork 201
/
main.go
259 lines (223 loc) · 6.34 KB
/
main.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package main
import (
"context"
"crypto/ecdsa"
"crypto/rand"
"errors"
"fmt"
"io"
"os"
"os/signal"
"sort"
"strings"
"syscall"
"time"
"github.com/ElrondNetwork/elrond-go/config"
"github.com/ElrondNetwork/elrond-go/core"
"github.com/ElrondNetwork/elrond-go/display"
"github.com/ElrondNetwork/elrond-go/hashing/sha256"
"github.com/ElrondNetwork/elrond-go/p2p"
"github.com/ElrondNetwork/elrond-go/p2p/libp2p"
"github.com/ElrondNetwork/elrond-go/p2p/libp2p/discovery"
factoryP2P "github.com/ElrondNetwork/elrond-go/p2p/libp2p/factory"
"github.com/ElrondNetwork/elrond-go/p2p/loadBalancer"
"github.com/btcsuite/btcd/btcec"
libp2pCrypto "github.com/libp2p/go-libp2p-core/crypto"
"github.com/urfave/cli"
)
var (
seedNodeHelpTemplate = `NAME:
{{.Name}} - {{.Usage}}
USAGE:
{{.HelpName}} {{if .VisibleFlags}}[global options]{{end}}
{{if len .Authors}}
AUTHOR:
{{range .Authors}}{{ . }}{{end}}
{{end}}{{if .Commands}}
GLOBAL OPTIONS:
{{range .VisibleFlags}}{{.}}
{{end}}
VERSION:
{{.Version}}
{{end}}
`
// port defines a flag for setting the port on which the node will listen for connections
port = cli.IntFlag{
Name: "port",
Usage: "Port number on which the application will start",
Value: 10000,
}
// p2pSeed defines a flag to be used as a seed when generating P2P credentials. Useful for seed nodes.
p2pSeed = cli.StringFlag{
Name: "p2p-seed",
Usage: "P2P seed will be used when generating credentials for p2p component. Can be any string.",
Value: "seed",
}
p2pConfigurationFile = "./config/p2p.toml"
errNilSeed = errors.New("nil seed")
errEmotySeed = errors.New("empty seed")
errNilBuffer = errors.New("nil buffer")
errEmptyBuffer = errors.New("empty buffer")
errInvalidPort = errors.New("cannot start node on port < 0")
errPeerDiscoveryShouldBeKadDht = errors.New("kad-dht peer discovery should have been enabled")
)
type seedRandReader struct {
index int
seed []byte
}
// NewSeedRandReader will return a new instance of a seed-based reader
func NewSeedRandReader(seed []byte) *seedRandReader {
return &seedRandReader{seed: seed, index: 0}
}
// Read to provided buffer pseudo-random generated bytes
func (srr *seedRandReader) Read(p []byte) (n int, err error) {
if srr.seed == nil {
return 0, errNilSeed
}
if len(srr.seed) == 0 {
return 0, errEmotySeed
}
if p == nil {
return 0, errNilBuffer
}
if len(p) == 0 {
return 0, errEmptyBuffer
}
for i := 0; i < len(p); i++ {
p[i] = srr.seed[srr.index]
srr.index = (srr.index + 1) % len(srr.seed)
}
return len(p), nil
}
func main() {
app := cli.NewApp()
cli.AppHelpTemplate = seedNodeHelpTemplate
app.Name = "SeedNode CLI App"
app.Usage = "This is the entry point for starting a new seed node - the app will help bootnodes connect to the network"
app.Flags = []cli.Flag{port, p2pSeed}
app.Version = "v0.0.1"
app.Authors = []cli.Author{
{
Name: "The Elrond Team",
Email: "contact@elrond.com",
},
}
app.Action = func(c *cli.Context) error {
return startNode(c)
}
err := app.Run(os.Args)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
}
func startNode(ctx *cli.Context) error {
fmt.Println("Starting node...")
stop := make(chan bool, 1)
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
p2pConfig, err := core.LoadP2PConfig(p2pConfigurationFile)
if err != nil {
return err
}
fmt.Printf("Initialized with p2p config from: %s\n", p2pConfigurationFile)
if ctx.IsSet(port.Name) {
p2pConfig.Node.Port = ctx.GlobalInt(port.Name)
}
if ctx.IsSet(p2pSeed.Name) {
p2pConfig.Node.Seed = ctx.GlobalString(p2pSeed.Name)
}
fmt.Println("Seed node....")
messenger, err := createNode(p2pConfig)
if err != nil {
return err
}
err = messenger.Bootstrap()
if err != nil {
return err
}
go func() {
<-sigs
fmt.Println("terminating at user's signal...")
stop <- true
}()
fmt.Println("Application is now running...")
displayMessengerInfo(messenger)
for {
select {
case <-stop:
return nil
case <-time.After(time.Second * 5):
displayMessengerInfo(messenger)
}
}
}
func createNode(p2pConfig *config.P2PConfig) (p2p.Messenger, error) {
var randReader io.Reader
if p2pConfig.Node.Seed != "" {
hasher := sha256.Sha256{}
randReader = NewSeedRandReader(hasher.Compute(p2pConfig.Node.Seed))
} else {
randReader = rand.Reader
}
netMessenger, err := createNetMessenger(p2pConfig, randReader)
if err != nil {
return nil, err
}
return netMessenger, nil
}
func createNetMessenger(
p2pConfig *config.P2PConfig,
randReader io.Reader,
) (p2p.Messenger, error) {
if p2pConfig.Node.Port < 0 {
return nil, errInvalidPort
}
pDiscoveryFactory := factoryP2P.NewPeerDiscovererCreator(*p2pConfig)
pDiscoverer, err := pDiscoveryFactory.CreatePeerDiscoverer()
if err != nil {
return nil, err
}
_, ok := pDiscoverer.(*discovery.KadDhtDiscoverer)
if !ok {
return nil, errPeerDiscoveryShouldBeKadDht
}
fmt.Printf("Starting with peer discovery: %s\n", pDiscoverer.Name())
prvKey, _ := ecdsa.GenerateKey(btcec.S256(), randReader)
sk := (*libp2pCrypto.Secp256k1PrivateKey)(prvKey)
nm, err := libp2p.NewNetworkMessenger(
context.Background(),
p2pConfig.Node.Port,
sk,
nil,
loadBalancer.NewOutgoingChannelLoadBalancer(),
pDiscoverer,
libp2p.ListenAddrWithIp4AndTcp,
p2pConfig.Node.TargetPeerCount,
)
if err != nil {
return nil, err
}
return nm, nil
}
func displayMessengerInfo(messenger p2p.Messenger) {
headerSeedAddresses := []string{"Seednode addresses:"}
addresses := make([]*display.LineData, 0)
for _, address := range messenger.Addresses() {
addresses = append(addresses, display.NewLineData(false, []string{address}))
}
tbl, _ := display.CreateTableString(headerSeedAddresses, addresses)
fmt.Println(tbl)
mesConnectedAddrs := messenger.ConnectedAddresses()
sort.Slice(mesConnectedAddrs, func(i, j int) bool {
return strings.Compare(mesConnectedAddrs[i], mesConnectedAddrs[j]) < 0
})
headerConnectedAddresses := []string{
fmt.Sprintf("Seednode is connected to %d peers:", len(mesConnectedAddrs))}
connAddresses := make([]*display.LineData, len(mesConnectedAddrs))
for idx, address := range mesConnectedAddrs {
connAddresses[idx] = display.NewLineData(false, []string{address})
}
tbl2, _ := display.CreateTableString(headerConnectedAddresses, connAddresses)
fmt.Println(tbl2)
}