forked from binaryholdings/tenderseed
-
Notifications
You must be signed in to change notification settings - Fork 16
/
main.go
188 lines (153 loc) · 4.83 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
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"time"
"github.com/mitchellh/go-homedir"
"github.com/tendermint/tendermint/config"
"github.com/tendermint/tendermint/libs/log"
"github.com/tendermint/tendermint/p2p"
"github.com/tendermint/tendermint/p2p/pex"
"github.com/tendermint/tendermint/version"
)
var (
configDir = ".tinyseed"
logger = log.NewTMLogger(log.NewSyncWriter(os.Stdout))
)
func main() {
seedConfig := DefaultConfig()
chains := GetChains()
var allchains []Chain
// Get all chains that seeds
for _, chain := range chains.Chains {
current := GetChain(chain)
allchains = append(allchains, current)
}
// Seed each chain
for i, chain := range allchains {
// increment the port number
address := "tcp://0.0.0.0:" + fmt.Sprint(9000+i)
// make folders and files
nodeKey := MakeFolders(chain, seedConfig)
// allpeers is a slice of peers
var allpeers []string
// make the struct of peers into a string
for _, peer := range chain.Peers.PersistentPeers {
thispeer := peer.ID + "@" + peer.Address
// check to make sure there's a colon in the address
if !strings.Contains(thispeer, ":") {
continue
}
// ensure that there is only one ampersand in the address
if strings.Count(thispeer, "@") != 1 {
continue
}
allpeers = append(allpeers, thispeer) //nolint:staticcheck
}
// set the configuration
seedConfig.ChainID = chain.ChainID
seedConfig.Seeds = allpeers
seedConfig.ListenAddress = address
// give the user addresses where we are seeding
logger.Info("Starting Seed Node for " + chain.ChainID + " on " + string(nodeKey.ID()) + "@0.0.0.0:" + fmt.Sprint(9000+i))
go Start(seedConfig, &nodeKey)
time.Sleep(1 * time.Second)
}
}
// make folders and files
func MakeFolders(chain Chain, seedConfig *Config) (nodeKey p2p.NodeKey) {
userHomeDir, err := homedir.Dir()
if err != nil {
panic(err)
}
// init config directory & files
homeDir := filepath.Join(userHomeDir, configDir+"/"+chain.ChainID)
configFilePath := filepath.Join(homeDir, "config.toml")
addrBookFilePath := filepath.Join(homeDir, "addrbook.json")
nodeKeyFilePath := filepath.Join(homeDir, "node_key.json")
// Make folders
for _, path := range []string{configFilePath, addrBookFilePath, nodeKeyFilePath} {
err := os.MkdirAll(filepath.Dir(path), 0700)
if err != nil {
panic(err)
}
}
nk, err := p2p.LoadOrGenNodeKey(nodeKeyFilePath)
if err != nil {
panic(err)
}
return *nk
}
// Start starts a Tenderseed
func Start(seedConfig *Config, nodeKey *p2p.NodeKey) {
chainID := seedConfig.ChainID
cfg := config.DefaultP2PConfig()
userHomeDir, err := homedir.Dir()
if err != nil {
panic(err)
}
filteredLogger := log.NewFilter(logger, log.AllowInfo())
logger.Info("Configuration",
"chain", chainID,
"key", nodeKey.ID(),
"node listen", seedConfig.ListenAddress,
"max-inbound", seedConfig.MaxNumInboundPeers,
"max-outbound", seedConfig.MaxNumOutboundPeers,
)
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, seedConfig.AddrBookFile)
book := pex.NewAddrBook(addrBookFilePath, seedConfig.AddrBookStrict)
// book.SetLogger(filteredLogger.With("module", "book"))
pexReactor := pex.NewReactor(book, &pex.ReactorConfig{
SeedMode: true,
Seeds: seedConfig.Seeds,
SeedDisconnectWaitPeriod: 5 * 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)
err = sw.Start()
if err != nil {
panic(err)
}
go func() {
// Fire periodically
ticker := time.NewTicker(5 * time.Second)
for range ticker.C {
peersout, peersin, dialing := sw.NumPeers()
fmt.Println(seedConfig.ChainID, peersout, " outbound peers, ", peersin, " inbound peers, and ", dialing, " dialing peers")
}
}()
sw.Wait()
// if we block here, we just get the first chain.
}