/
node.go
177 lines (157 loc) · 4.12 KB
/
node.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
package p2p
import (
cfg "github.com/tendermint/go-config"
"github.com/tendermint/go-crypto"
gop2p "github.com/tendermint/go-p2p"
"github.com/tendermint/go-wire"
"github.com/zbo14/pos/crypto/tndr"
. "github.com/zbo14/pos/util"
)
// From github.com/jaekwon/twirl/node/node
const (
DEFAULT_PROTO = "tcp"
NETWORK = "SPACEMINT"
PART_SIZE = 1024 * 16
SKIP_UPNP = false
VERSION = "0.0.0"
)
// TODO: add logger
type Node struct {
config cfg.Config
dataReactor *DataReactor
pexReactor *gop2p.PEXReactor
priv crypto.PrivKeyEd25519
sw *gop2p.Switch
}
func NewConfig(bookPath, configPath, input, laddr, output string, priv crypto.PrivKeyEd25519, seeds string, seedsLimit int) cfg.Config {
data := make(map[string]interface{})
data["book_path"] = bookPath
data["input"] = input
data["laddr"] = laddr
data["output"] = output
data["priv_key"] = tndr.PrivKeyToB58(priv)
data["seeds"] = seeds
data["seeds_limit"] = seedsLimit
file := MustOpenFile(configPath)
MustWriteJSON(file, data)
return cfg.NewMapConfig(data)
}
func ReadConfig(configPath string) cfg.Config {
file := MustOpenFile(configPath)
var data map[string]interface{}
MustReadJSON(file, &data)
return cfg.NewMapConfig(data)
}
func NewNode(config cfg.Config) *Node {
bookPath := config.GetString("book_path")
book := gop2p.NewAddrBook(bookPath)
pexReactor := gop2p.NewPEXReactor(book)
dataReactor := NewDataReactor()
input := config.GetString("input")
output := config.GetString("output")
b58 := config.GetString("priv_key")
priv := tndr.PrivKeyFromB58(b58)
if input != "" {
bytes := MustReadFile(input)
parts := NewPartSetFromData(bytes, PART_SIZE)
dataReactor.SetParts(parts)
}
if output != "" {
dataReactor.SetOutputPath(output)
}
sw := gop2p.NewSwitch(config)
sw.AddReactor("PEX", pexReactor)
sw.AddReactor("DATA", dataReactor)
return &Node{
dataReactor: dataReactor,
pexReactor: pexReactor,
priv: priv,
sw: sw,
}
}
func (nd *Node) Start() error {
ndInfo := newNodeInfo(nd.config, nd.sw, nd.priv)
nd.sw.SetNodeInfo(ndInfo)
nd.sw.SetNodePrivKey(nd.priv)
_, err := nd.sw.Start()
return err
}
func (nd *Node) Stop() {
// TODO: gracefully disconnect from peers
nd.sw.Stop()
}
func (nd *Node) AddListener(lis gop2p.Listener) {
nd.sw.AddListener(lis)
}
func (nd *Node) DataReactor() *DataReactor {
return nd.dataReactor
}
func (nd *Node) PexReactor() *gop2p.PEXReactor {
return nd.pexReactor
}
func (nd *Node) Switch() *gop2p.Switch {
return nd.sw
}
func newNodeInfo(config cfg.Config, sw *gop2p.Switch, priv crypto.PrivKeyEd25519) *gop2p.NodeInfo {
pub := tndr.PubKey(priv)
ndInfo := &gop2p.NodeInfo{
Network: NETWORK,
PubKey: pub,
Version: VERSION,
Other: []string{
Sprintf("wire_version=%v", wire.Version),
Sprintf("gop2p_version=%v", gop2p.Version),
},
}
if !sw.IsListening() {
return ndInfo
}
lis := sw.Listeners()[0]
addr := lis.ExternalAddress()
host := addr.IP.String()
port := addr.Port
ndInfo.ListenAddr = Sprintf("%v:%v", host, port)
return ndInfo
}
func RunNode(configPath string) *Node {
config := ReadConfig(configPath)
nd := NewNode(config)
laddr := config.GetString("laddr")
proto, addr := ProtocolAndAddress(laddr)
lis := gop2p.NewDefaultListener(proto, addr, SKIP_UPNP)
nd.AddListener(lis)
if err := nd.Start(); err != nil {
//..
}
if seedString := config.GetString("seeds"); seedString != "" {
seeds := Split(seedString, ",")
if lmt := config.GetInt("seeds_limit"); lmt != 0 && len(seeds) > lmt {
perm := RandPerm(len(seeds))
_seeds := make([]string, lmt)
for i, j := range perm[:lmt] {
_seeds[i] = seeds[j]
}
seeds = _seeds
}
nd.DialSeeds(seeds)
}
return nd
}
func (nd *Node) NodeInfo() *gop2p.NodeInfo {
return nd.sw.NodeInfo()
}
func (nd *Node) DialSeeds(seeds []string) {
nd.sw.DialSeeds(seeds)
}
func (nd *Node) ShutdownPeers() {
msg := new(ShutdownMessage)
nd.sw.Broadcast(DATA_CHANNEL, struct{ DataMessage }{msg})
}
func ProtocolAndAddress(laddr string) (string, string) {
proto, addr := DEFAULT_PROTO, laddr
part1, part2, err := Split2(laddr, "://")
if err == nil {
proto, addr = part1, part2
}
return proto, addr
}