forked from notional-labs/tinyseed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
182 lines (154 loc) · 5.64 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
package main
import (
"fmt"
"path/filepath"
"os"
"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"
"github.com/mitchellh/go-homedir"
)
// Config defines the configuration format for TinySeed
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(homeDir string) *Config {
return &Config{
ListenAddress: "tcp://0.0.0.0:36656",
ChainID: "columbus-5",
NodeKeyFile: filepath.Join(homeDir, "config/node_key.json"),
AddrBookFile: filepath.Join(homeDir, "data/addrbook.json"),
AddrBookStrict: true,
MaxNumInboundPeers: 1000,
MaxNumOutboundPeers: 1000,
Seeds: "e999fc20aa5b87c1acef8677cf495ad85061cfb9@seed.terra.delightlabs.io:26656,[email protected]:26656,[email protected]:26656",
}
}
// TinySeed lives here. Smol ting.
func main() {
idOverride := os.Getenv("ID")
seedOverride := os.Getenv("SEEDS")
listenAddressOverride := os.Getenv("LISTENADDRESS")
userHomeDir, err := homedir.Dir()
if err != nil {
panic(err)
}
homeDir := filepath.Join(userHomeDir, ".tinyseed")
configFile := "config/config.toml"
configFilePath := filepath.Join(homeDir, configFile)
MkdirAllPanic(filepath.Dir(configFilePath), os.ModePerm)
SeedConfig := DefaultConfig(homeDir)
if idOverride != "" {
SeedConfig.ChainID = idOverride
}
if seedOverride != "" {
SeedConfig.Seeds = seedOverride
}
if listenAddressOverride != "" {
SeedConfig.ListenAddress = listenAddressOverride
}
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 Tenderseed
func Start(SeedConfig Config) {
logger := log.NewTMLogger(
log.NewSyncWriter(os.Stdout),
)
chainID := SeedConfig.ChainID
nodeKeyFilePath := SeedConfig.NodeKeyFile
addrBookFilePath := SeedConfig.AddrBookFile
MkdirAllPanic(filepath.Dir(nodeKeyFilePath), os.ModePerm)
MkdirAllPanic(filepath.Dir(addrBookFilePath), os.ModePerm)
cfg := config.DefaultP2PConfig()
cfg.AllowDuplicateIP = true
// allow a lot of inbound peers since we disconnect from them quickly in seed mode
cfg.MaxNumInboundPeers = SeedConfig.MaxNumInboundPeers
// keep trying to make outbound connections to exchange peering info
cfg.MaxNumOutboundPeers = SeedConfig.MaxNumOutboundPeers
nodeKey, err := p2p.LoadOrGenNodeKey(nodeKeyFilePath)
if err != nil {
panic(err)
}
logger.Info("tenderseed",
"key", nodeKey.ID(),
"key path", nodeKeyFilePath,
"address book path", addrBookFilePath,
"listen", SeedConfig.ListenAddress,
"chain", chainID,
"strict-routing", SeedConfig.AddrBookStrict,
"max-inbound", SeedConfig.MaxNumInboundPeers,
"max-outbound", SeedConfig.MaxNumOutboundPeers,
)
// TODO(roman) expose per-module log levels in the config
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.5.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)
}
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, ",", " "),
})
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)
}
sw.Wait()
}