forked from notional-labs/tinyseed
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
190 lines (160 loc) · 5.88 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
package main
import (
"fmt"
"os"
"path/filepath"
"time"
"github.com/mitchellh/go-homedir"
"github.com/tendermint/tendermint/config"
"github.com/tendermint/tendermint/libs/log"
tmos "github.com/tendermint/tendermint/libs/os"
tmstrings "github.com/tendermint/tendermint/libs/strings"
"github.com/tendermint/tendermint/p2p"
"github.com/tendermint/tendermint/p2p/pex"
"github.com/tendermint/tendermint/version"
)
var (
configDir = ".goldenseed"
logger = log.NewTMLogger(log.NewSyncWriter(os.Stdout))
)
// Config defines the configuration format
type Config struct {
ListenAddress string `toml:"laddr" comment:"Address to listen for incoming connections"`
ChainID string `toml:"chain_id" comment:"network identifier (todo move to cli flag argument? keeps the config network agnostic)"`
NodeKeyFile string `toml:"node_key_file" comment:"path to node_key (relative to tendermint-seed home directory or an absolute path)"`
AddrBookFile string `toml:"addr_book_file" comment:"path to address book (relative to tendermint-seed home directory or an absolute path)"`
AddrBookStrict bool `toml:"addr_book_strict" comment:"Set true for strict routability rules\n Set false for private or local networks"`
MaxNumInboundPeers int `toml:"max_num_inbound_peers" comment:"maximum number of inbound connections"`
MaxNumOutboundPeers int `toml:"max_num_outbound_peers" comment:"maximum number of outbound connections"`
Seeds string `toml:"seeds" comment:"seed nodes we can use to discover peers"`
}
// DefaultConfig returns a seed config initialized with default values
func DefaultConfig() *Config {
return &Config{
ListenAddress: "tcp://0.0.0.0:16187",
ChainID: "evmos_9001-2",
NodeKeyFile: "node_key.json",
AddrBookFile: "addrbook.json",
AddrBookStrict: true,
MaxNumInboundPeers: 5000,
MaxNumOutboundPeers: 5000,
Seeds: "[email protected]:26656,[email protected]:26656,[email protected]:26656,[email protected]:26656,588cedb70fa1d98c14a2f2c1456bfa41e1a156a8@evmos-sentry.mercury-nodes.net:29539",
}
}
func main() {
idOverride := os.Getenv("ID")
seedOverride := os.Getenv("SEEDS")
userHomeDir, err := homedir.Dir()
seedConfig := DefaultConfig()
if err != nil {
panic(err)
}
// init config directory & files
homeDir := filepath.Join(userHomeDir, configDir, "config")
configFilePath := filepath.Join(homeDir, "config.toml")
nodeKeyFilePath := filepath.Join(homeDir, seedConfig.NodeKeyFile)
addrBookFilePath := filepath.Join(homeDir, seedConfig.AddrBookFile)
MkdirAllPanic(filepath.Dir(nodeKeyFilePath), os.ModePerm)
MkdirAllPanic(filepath.Dir(addrBookFilePath), os.ModePerm)
MkdirAllPanic(filepath.Dir(configFilePath), os.ModePerm)
if idOverride != "" {
seedConfig.ChainID = idOverride
}
if seedOverride != "" {
seedConfig.Seeds = seedOverride
}
logger.Info("Starting Seed Node...")
Start(*seedConfig)
}
// MkdirAllPanic invokes os.MkdirAll but panics if there is an error
func MkdirAllPanic(path string, perm os.FileMode) {
err := os.MkdirAll(path, perm)
if err != nil {
panic(err)
}
}
// Start starts a goldenseed
func Start(seedConfig Config) {
chainID := seedConfig.ChainID
cfg := config.DefaultP2PConfig()
cfg.AllowDuplicateIP = true
userHomeDir, err := homedir.Dir()
nodeKeyFilePath := filepath.Join(userHomeDir, configDir, "config", seedConfig.NodeKeyFile)
nodeKey, err := p2p.LoadOrGenNodeKey(nodeKeyFilePath)
if err != nil {
panic(err)
}
logger.Info("Configuration",
"key", nodeKey.ID(),
"node listen", seedConfig.ListenAddress,
"chain", chainID,
"strict-routing", seedConfig.AddrBookStrict,
"max-inbound", seedConfig.MaxNumInboundPeers,
"max-outbound", seedConfig.MaxNumOutboundPeers,
)
filteredLogger := log.NewFilter(logger, log.AllowInfo())
protocolVersion :=
p2p.NewProtocolVersion(
version.P2PProtocol,
version.BlockProtocol,
0,
)
// NodeInfo gets info on your node
nodeInfo := p2p.DefaultNodeInfo{
ProtocolVersion: protocolVersion,
DefaultNodeID: nodeKey.ID(),
ListenAddr: seedConfig.ListenAddress,
Network: chainID,
Version: "0.6.9",
Channels: []byte{pex.PexChannel},
Moniker: fmt.Sprintf("%s-seed", chainID),
}
addr, err := p2p.NewNetAddressString(p2p.IDAddressString(nodeInfo.DefaultNodeID, nodeInfo.ListenAddr))
if err != nil {
panic(err)
}
transport := p2p.NewMultiplexTransport(nodeInfo, *nodeKey, p2p.MConnConfig(cfg))
if err := transport.Listen(*addr); err != nil {
panic(err)
}
addrBookFilePath := filepath.Join(userHomeDir, configDir, "config", seedConfig.AddrBookFile)
book := pex.NewAddrBook(addrBookFilePath, seedConfig.AddrBookStrict)
book.SetLogger(filteredLogger.With("module", "book"))
pexReactor := pex.NewReactor(book, &pex.ReactorConfig{
SeedMode: true,
Seeds: tmstrings.SplitAndTrim(seedConfig.Seeds, ",", " "),
SeedDisconnectWaitPeriod: 1 * time.Second, // default is 28 hours, we just want to harvest as many addresses as possible
PersistentPeersMaxDialPeriod: 0, // use exponential back-off
})
pexReactor.SetLogger(filteredLogger.With("module", "pex"))
sw := p2p.NewSwitch(cfg, transport)
sw.SetLogger(filteredLogger.With("module", "switch"))
sw.SetNodeKey(nodeKey)
sw.SetAddrBook(book)
sw.AddReactor("pex", pexReactor)
// last
sw.SetNodeInfo(nodeInfo)
tmos.TrapSignal(logger, func() {
logger.Info("shutting down...")
book.Save()
err := sw.Stop()
if err != nil {
panic(err)
}
})
err = sw.Start()
if err != nil {
panic(err)
}
go func() {
// Fire periodically
ticker := time.NewTicker(5 * time.Second)
for {
select {
case <-ticker.C:
logger.Info("Peers list", "peers", sw.Peers().List())
}
}
}()
sw.Wait()
}