forked from harmony-one/harmony
/
main.go
141 lines (115 loc) · 4.24 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
// bootnode provides peer discovery service to new node to connect to the p2p network
package main
import (
"flag"
"fmt"
"os"
"path"
"github.com/ethereum/go-ethereum/log"
"github.com/harmony-one/harmony/internal/utils"
"github.com/harmony-one/harmony/p2p"
net "github.com/libp2p/go-libp2p-core/network"
ma "github.com/multiformats/go-multiaddr"
)
// ConnLogger ..
type ConnLogger struct {
l log.Logger
}
func netLogger(n net.Network, l log.Logger) log.Logger {
return l.New(
"netLocalPeer", n.LocalPeer(),
"netListenAddresses", n.ListenAddresses())
}
func connLogger(c net.Conn, l log.Logger) log.Logger {
return l.New(
"connLocalPeer", c.LocalPeer(),
"connLocalAddr", c.LocalMultiaddr(),
"connRemotePeer", c.RemotePeer(),
"connRemoteAddr", c.RemoteMultiaddr())
}
func streamLogger(s net.Stream, l log.Logger) log.Logger {
return connLogger(s.Conn(), l).New("streamProtocolID", s.Protocol())
}
// Listen logs a listener starting listening on an address.
func (cl ConnLogger) Listen(n net.Network, ma ma.Multiaddr) {
utils.WithCaller(netLogger(n, cl.l)).
Debug("listener starting", "listenAddress", ma)
}
// ListenClose logs a listener stopping listening on an address.
func (cl ConnLogger) ListenClose(n net.Network, ma ma.Multiaddr) {
utils.WithCaller(netLogger(n, cl.l)).
Debug("listener closing", "listenAddress", ma)
}
// Connected logs a connection getting opened.
func (cl ConnLogger) Connected(n net.Network, c net.Conn) {
utils.WithCaller(connLogger(c, netLogger(n, cl.l))).Debug("connected")
}
// Disconnected logs a connection getting closed.
func (cl ConnLogger) Disconnected(n net.Network, c net.Conn) {
utils.WithCaller(connLogger(c, netLogger(n, cl.l))).Debug("disconnected")
}
// OpenedStream logs a new stream getting opened.
func (cl ConnLogger) OpenedStream(n net.Network, s net.Stream) {
utils.WithCaller(streamLogger(s, netLogger(n, cl.l))).Debug("stream opened")
}
// ClosedStream logs a stream getting closed.
func (cl ConnLogger) ClosedStream(n net.Network, s net.Stream) {
utils.WithCaller(streamLogger(s, netLogger(n, cl.l))).Debug("stream closed")
}
// NewConnLogger returns a new connection logger that uses the given
// Ethereum logger. See ConnLogger for usage.
func NewConnLogger(l log.Logger) *ConnLogger {
return &ConnLogger{l: l}
}
var (
version string
builtBy string
builtAt string
commit string
)
func printVersion(me string) {
fmt.Fprintf(os.Stderr, "Harmony (C) 2019. %v, version %v-%v (%v %v)\n", path.Base(me), version, commit, builtBy, builtAt)
os.Exit(0)
}
func main() {
ip := flag.String("ip", "127.0.0.1", "IP of the node")
port := flag.String("port", "9876", "port of the node.")
logFolder := flag.String("log_folder", "latest", "the folder collecting the logs of this execution")
logMaxSize := flag.Int("log_max_size", 100, "the max size in megabytes of the log file before it gets rotated")
keyFile := flag.String("key", "./.bnkey", "the private key file of the bootnode")
versionFlag := flag.Bool("version", false, "Output version info")
verbosity := flag.Int("verbosity", 5, "Logging verbosity: 0=silent, 1=error, 2=warn, 3=info, 4=debug, 5=detail (default: 5)")
logConn := flag.Bool("log_conn", false, "log incoming/outgoing connections")
flag.Parse()
if *versionFlag {
printVersion(os.Args[0])
}
// Logging setup
utils.SetLogContext(*port, *ip)
utils.SetLogVerbosity(log.Lvl(*verbosity))
utils.AddLogFile(fmt.Sprintf("%v/bootnode-%v-%v.log", *logFolder, *ip, *port), *logMaxSize)
privKey, _, err := utils.LoadKeyFromFile(*keyFile)
if err != nil {
utils.FatalErrMsg(err, "cannot load key from %s", *keyFile)
}
// For bootstrap nodes, we shall keep .dht file.
dataStorePath := fmt.Sprintf(".dht-%s-%s", *ip, *port)
selfPeer := p2p.Peer{IP: *ip, Port: *port}
host, err := p2p.NewHost(p2p.HostConfig{
Self: &selfPeer,
BLSKey: privKey,
BootNodes: nil, // Boot nodes have no boot nodes :) Will be connected when other nodes joined
DataStoreFile: &dataStorePath,
})
if err != nil {
utils.FatalErrMsg(err, "cannot initialize network")
}
fmt.Printf("bootnode BN_MA=%s",
fmt.Sprintf("/ip4/%s/tcp/%s/p2p/%s", *ip, *port, host.GetID().Pretty()),
)
host.Start()
if *logConn {
host.GetP2PHost().Network().Notify(NewConnLogger(utils.GetLogInstance()))
}
select {}
}