-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathnode.go
More file actions
284 lines (253 loc) · 7.35 KB
/
Copy pathnode.go
File metadata and controls
284 lines (253 loc) · 7.35 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package node
import (
"context"
"net"
"reflect"
"sync"
"time"
"github.com/wavesplatform/gowaves/pkg/logging"
"github.com/wavesplatform/gowaves/pkg/node/network"
"github.com/pkg/errors"
"go.uber.org/zap"
"github.com/wavesplatform/gowaves/pkg/metrics"
"github.com/wavesplatform/gowaves/pkg/node/fsm"
"github.com/wavesplatform/gowaves/pkg/node/fsm/tasks"
"github.com/wavesplatform/gowaves/pkg/node/messages"
"github.com/wavesplatform/gowaves/pkg/node/peers"
"github.com/wavesplatform/gowaves/pkg/p2p/peer"
"github.com/wavesplatform/gowaves/pkg/proto"
"github.com/wavesplatform/gowaves/pkg/services"
"github.com/wavesplatform/gowaves/pkg/state"
"github.com/wavesplatform/gowaves/pkg/types"
)
const (
spawnOutgoingConnectionsInterval = 1 * time.Minute
metricInternalChannelSizeUpdateInterval = 1 * time.Second
)
type Config struct {
AppName string
NodeName string
Listen string
DeclAddr string
}
type Node struct {
peers peers.PeerManager
state state.State
declAddr proto.TCPAddr
bindAddr proto.TCPAddr
scheduler types.Scheduler
utx types.UtxPool
services services.Services
microblockInterval time.Duration
obsolescence time.Duration
enableLightMode bool
}
func NewNode(
services services.Services, declAddr proto.TCPAddr, bindAddr proto.TCPAddr, microblockInterval time.Duration,
enableLightMode bool,
) *Node {
if bindAddr.EmptyNoPort() {
zap.S().Warnf("Bind IP address and port are empty, using declared address %q", declAddr.String())
bindAddr = declAddr
}
return &Node{
state: services.State,
peers: services.Peers,
declAddr: declAddr,
bindAddr: bindAddr,
scheduler: services.Scheduler,
utx: services.UtxPool,
services: services,
microblockInterval: microblockInterval,
enableLightMode: enableLightMode,
}
}
func (a *Node) Close() error {
ch := make(chan struct{})
a.services.InternalChannel <- messages.NewHaltMessage(ch)
<-ch
return nil
}
func (a *Node) SpawnOutgoingConnections(ctx context.Context) {
a.peers.SpawnOutgoingConnections(ctx)
}
func (a *Node) SpawnOutgoingConnection(ctx context.Context, addr proto.TCPAddr) error {
return a.peers.Connect(ctx, addr)
}
func (a *Node) serveIncomingPeers(ctx context.Context) error {
var wg sync.WaitGroup
defer wg.Wait()
// it's important defer wg.Wait before deferring the context cancellation
ctx, cancel := context.WithCancel(ctx)
defer cancel()
// if empty declared address, listen on port doesn't make sense
if a.declAddr.Empty() {
zap.S().Warn("Declared IP address is empty")
return nil
}
if a.bindAddr.EmptyNoPort() {
zap.S().Warn("Bind IP address and port are empty")
return nil
}
zap.S().Infof("Start listening on %s", a.bindAddr.String())
var lc net.ListenConfig
l, err := lc.Listen(ctx, "tcp", a.bindAddr.String())
if err != nil {
return err
}
// Close the listener when the context is done
wg.Add(1)
go func() {
defer wg.Done()
<-ctx.Done()
if clErr := l.Close(); clErr != nil {
zap.S().Errorf("Failed to close %T on addr %q: %v", l, l.Addr().String(), clErr)
}
}()
for {
conn, err := l.Accept()
if err != nil {
if ctx.Err() != nil { // context has been canceled
return nil
}
zap.S().Errorf("Failed to accept new peer: %v", err)
continue
}
go func() {
if err := a.peers.SpawnIncomingConnection(ctx, conn); err != nil {
zap.S().Named(logging.NetworkNamespace).Debugf("Incoming connection failed with addr %q: %v",
conn.RemoteAddr().String(), err)
return
}
}()
}
}
func (a *Node) logErrors(err error) {
var infoMsg *proto.InfoMsg
_ = error(infoMsg) // compile time check
switch {
case errors.As(err, &infoMsg):
zap.S().Named(logging.FSMNamespace).Debugf("Error: %v", infoMsg)
default:
zap.S().Errorf("%v", err)
}
}
func (a *Node) Run(
ctx context.Context, p peer.Parent, internalMessageCh <-chan messages.InternalMessage,
networkMsgCh <-chan network.InfoMessage, syncPeer *network.SyncPeer,
) {
protoMessagesChan := chanLenProvider[peer.ProtoMessage](p.MessageCh)
go a.runOutgoingConnections(ctx)
go a.runInternalMetrics(ctx, protoMessagesChan)
go a.runIncomingConnections(ctx)
tasksCh := make(chan tasks.AsyncTask, 10)
// TODO: Consider using context `ctx` in FSM, for now FSM works in the background context.
m, async, err := fsm.NewFSM(a.services, a.microblockInterval, a.obsolescence, syncPeer, a.enableLightMode)
if err != nil {
zap.S().Errorf("Failed to create FSM: %v", err)
return
}
spawnAsync(ctx, tasksCh, async)
actions := createActions()
for {
select {
case internalMess := <-internalMessageCh:
switch t := internalMess.(type) {
case *messages.MinedBlockInternalMessage:
async, err = m.MinedBlock(t.Block, t.Limits, t.KeyPair, t.Vrf)
case *messages.HaltMessage:
async, err = m.Halt()
t.Complete()
case *messages.BroadcastTransaction:
async, err = m.Transaction(nil, t.Transaction)
select {
case t.Response <- err:
default:
}
default:
zap.S().Errorf("[%s] Unknown internal message '%T'", m.State.State, t)
continue
}
case task := <-tasksCh:
async, err = m.Task(task)
case msg := <-networkMsgCh:
switch t := msg.(type) {
case network.StartMining:
async, err = m.StartMining()
case network.StopSync:
async, err = m.StopSync()
case network.ChangeSyncPeer:
async, err = m.ChangeSyncPeer(t.Peer)
case network.StopMining:
async, err = m.StopMining()
default:
zap.S().Warnf("[%s] Unknown network info message '%T'", m.State.State, msg)
}
case mess := <-p.MessageCh:
zap.S().Named(logging.FSMNamespace).Debugf("[%s] Network message '%T' received from '%s'",
m.State.State, mess.Message, mess.ID.ID())
action, ok := actions[reflect.TypeOf(mess.Message)]
if !ok {
zap.S().Errorf("[%s] Unknown network message '%T' from '%s'",
m.State.State, mess.Message, mess.ID.ID())
continue
}
async, err = action(a.services, mess, m)
}
if err != nil {
a.logErrors(err)
}
spawnAsync(ctx, tasksCh, async)
}
}
func (a *Node) runIncomingConnections(ctx context.Context) {
if err := a.serveIncomingPeers(ctx); err != nil && !errors.Is(err, context.Canceled) {
zap.S().Errorf("Failed to continue serving incoming peers: %v", err)
}
}
type lenProvider interface {
Len() int
}
type chanLenProvider[T any] <-chan T
func (c chanLenProvider[T]) Len() int { return len(c) }
func (a *Node) runInternalMetrics(ctx context.Context, protoMessagesChan lenProvider) {
ticker := time.NewTicker(metricInternalChannelSizeUpdateInterval)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
l := protoMessagesChan.Len()
metrics.FSMChannelLength(l)
metricInternalChannelSize.Set(float64(l))
}
}
}
func (a *Node) runOutgoingConnections(ctx context.Context) {
for {
a.SpawnOutgoingConnections(ctx)
timer := time.NewTimer(spawnOutgoingConnectionsInterval)
select {
case <-ctx.Done():
if !timer.Stop() {
select {
case <-timer.C:
default:
}
}
return
case <-timer.C:
}
}
}
func spawnAsync(ctx context.Context, ch chan tasks.AsyncTask, a fsm.Async) {
for _, t := range a {
go func(t tasks.Task) {
err := t.Run(ctx, ch)
if err != nil && !errors.Is(err, context.Canceled) {
zap.S().Warnf("Async task '%T' finished with error: %q", t, err)
}
}(t)
}
}