-
Notifications
You must be signed in to change notification settings - Fork 147
/
access_manager.go
232 lines (205 loc) · 7.26 KB
/
access_manager.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
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
// Copyright 2020 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0
package access_mgr
import (
"context"
"time"
"github.com/iotaledger/hive.go/logger"
"github.com/iotaledger/wasp/packages/chains/access_mgr/am_dist"
"github.com/iotaledger/wasp/packages/cryptolib"
"github.com/iotaledger/wasp/packages/gpa"
"github.com/iotaledger/wasp/packages/isc"
"github.com/iotaledger/wasp/packages/peering"
"github.com/iotaledger/wasp/packages/util"
"github.com/iotaledger/wasp/packages/util/pipe"
)
type AccessMgr interface {
TrustedNodes(trusted []*cryptolib.PublicKey)
ChainAccessNodes(chainID isc.ChainID, accessNodes []*cryptolib.PublicKey)
ChainDismissed(chainID isc.ChainID)
}
type accessMgrImpl struct {
dist gpa.AckHandler
dismissPeerBuf []*cryptolib.PublicKey
reqTrustedNodesPipe pipe.Pipe[*reqTrustedNodes]
reqChainAccessNodesPipe pipe.Pipe[*reqChainAccessNodes]
reqChainDismissedPipe pipe.Pipe[*reqChainDismissed]
netRecvPipe pipe.Pipe[*peering.PeerMessageIn]
netPeeringID peering.PeeringID
netPeerPubs map[gpa.NodeID]*cryptolib.PublicKey
net peering.NetworkProvider
log *logger.Logger
}
type reqTrustedNodes struct {
trusted []*cryptolib.PublicKey
}
type reqChainAccessNodes struct {
chainID isc.ChainID
accessNodes []*cryptolib.PublicKey
}
type reqChainDismissed struct {
chainID isc.ChainID
}
var _ AccessMgr = &accessMgrImpl{}
const (
msgTypeAccessMgr byte = iota
)
const (
resendPeriod = 3 * time.Second
distDebugTick = 10 * time.Second
distTimeTick = 1 * time.Second
)
func New(
ctx context.Context,
serversUpdatedCB func(chainID isc.ChainID, servers []*cryptolib.PublicKey),
nodeIdentity *cryptolib.KeyPair,
net peering.NetworkProvider,
log *logger.Logger,
) AccessMgr {
// there is only one AccessMgr per Wasp node, so the identifier is a constant.
netPeeringID := peering.HashPeeringIDFromBytes([]byte("AccessManager")) // AccessManager
ami := &accessMgrImpl{
dismissPeerBuf: []*cryptolib.PublicKey{},
reqTrustedNodesPipe: pipe.NewInfinitePipe[*reqTrustedNodes](),
reqChainAccessNodesPipe: pipe.NewInfinitePipe[*reqChainAccessNodes](),
reqChainDismissedPipe: pipe.NewInfinitePipe[*reqChainDismissed](),
netRecvPipe: pipe.NewInfinitePipe[*peering.PeerMessageIn](),
netPeeringID: netPeeringID,
netPeerPubs: map[gpa.NodeID]*cryptolib.PublicKey{},
net: net,
log: log,
}
me := ami.pubKeyAsNodeID(nodeIdentity.GetPublicKey())
ami.dist = gpa.NewAckHandler(me, gpa.NewOwnHandler(
me,
am_dist.NewAccessMgr(ami.pubKeyAsNodeID, serversUpdatedCB, ami.dismissPeerCB, log).AsGPA(),
), resendPeriod)
netRecvPipeInCh := ami.netRecvPipe.In()
unhook := net.Attach(&netPeeringID, peering.ReceiverAccessMgr, func(recv *peering.PeerMessageIn) {
if recv.MsgType != msgTypeAccessMgr {
ami.log.Warnf("Unexpected message, type=%v", recv.MsgType)
return
}
netRecvPipeInCh <- recv
})
go ami.run(ctx, unhook)
return ami
}
// Implements the AccessMgr interface.
func (ami *accessMgrImpl) TrustedNodes(trusted []*cryptolib.PublicKey) {
ami.reqTrustedNodesPipe.In() <- &reqTrustedNodes{trusted: trusted}
}
// Implements the AccessMgr interface.
func (ami *accessMgrImpl) ChainAccessNodes(chainID isc.ChainID, accessNodes []*cryptolib.PublicKey) {
ami.reqChainAccessNodesPipe.In() <- &reqChainAccessNodes{chainID: chainID, accessNodes: accessNodes}
}
// Implements the AccessMgr interface.
func (ami *accessMgrImpl) ChainDismissed(chainID isc.ChainID) {
ami.reqChainDismissedPipe.In() <- &reqChainDismissed{chainID: chainID}
}
// A callback for amDist.
func (ami *accessMgrImpl) dismissPeerCB(peerPubKey *cryptolib.PublicKey) {
// Dismiss them after the messages are sent.
ami.dismissPeerBuf = append(ami.dismissPeerBuf, peerPubKey)
}
func (ami *accessMgrImpl) run(ctx context.Context, cleanupFunc context.CancelFunc) {
reqTrustedNodesOutCh := ami.reqTrustedNodesPipe.Out()
reqChainAccessNodesPipeOutCh := ami.reqChainAccessNodesPipe.Out()
reqChainDismissedPipeOutCh := ami.reqChainDismissedPipe.Out()
netRecvPipeOutCh := ami.netRecvPipe.Out()
debugTicker := time.NewTicker(distDebugTick)
timeTicker := time.NewTicker(distTimeTick)
for {
select {
case recv, ok := <-reqTrustedNodesOutCh:
if !ok {
reqTrustedNodesOutCh = nil
continue
}
ami.handleReqTrustedNodes(recv)
case recv, ok := <-reqChainAccessNodesPipeOutCh:
if !ok {
reqChainAccessNodesPipeOutCh = nil
continue
}
ami.handleReqChainAccessNodes(recv)
case recv, ok := <-reqChainDismissedPipeOutCh:
if !ok {
reqChainDismissedPipeOutCh = nil
continue
}
ami.handleReqChainDismissed(recv)
case recv, ok := <-netRecvPipeOutCh:
if !ok {
netRecvPipeOutCh = nil
continue
}
ami.handleNetMessage(recv)
case <-debugTicker.C:
ami.handleDistDebugTick()
case timestamp := <-timeTicker.C:
ami.handleDistTimeTick(timestamp)
case <-ctx.Done():
// close(reqTrustedNodesOutCh) // TODO: Causes panic: send on closed channel
// close(reqChainAccessNodesOutCh)
// close(reqChainDismissedOutCh)
debugTicker.Stop()
timeTicker.Stop()
util.ExecuteIfNotNil(cleanupFunc)
return
}
}
}
func (ami *accessMgrImpl) handleReqTrustedNodes(recv *reqTrustedNodes) {
ami.log.Debugf("handleReqTrustedNodes: trusted=%v", recv.trusted)
ami.sendMessages(ami.dist.Input(am_dist.NewInputTrustedNodes(recv.trusted)))
}
func (ami *accessMgrImpl) handleReqChainAccessNodes(recv *reqChainAccessNodes) {
ami.log.Debugf("handleReqChainAccessNodes: chainID=%v, access=%v", recv.chainID, recv.accessNodes)
ami.sendMessages(ami.dist.Input(am_dist.NewInputAccessNodes(recv.chainID, recv.accessNodes)))
}
func (ami *accessMgrImpl) handleReqChainDismissed(recv *reqChainDismissed) {
ami.log.Debugf("handleReqChainDismissed: chainID=%v", recv.chainID)
ami.sendMessages(ami.dist.Input(am_dist.NewInputChainDisabled(recv.chainID)))
}
func (ami *accessMgrImpl) handleDistDebugTick() {
ami.log.Debugf(
"AccessMgr, dist=%v",
ami.dist.StatusString(),
)
}
func (ami *accessMgrImpl) handleDistTimeTick(timestamp time.Time) {
ami.sendMessages(ami.dist.Input(ami.dist.MakeTickInput(timestamp)))
}
func (ami *accessMgrImpl) handleNetMessage(recv *peering.PeerMessageIn) {
msg, err := ami.dist.UnmarshalMessage(recv.MsgData)
if err != nil {
ami.log.Warnf("cannot parse message: %v", err)
return
}
msg.SetSender(ami.pubKeyAsNodeID(recv.SenderPubKey))
outMsgs := ami.dist.Message(msg) // Output is handled via callbacks in this case.
ami.sendMessages(outMsgs)
}
func (ami *accessMgrImpl) sendMessages(outMsgs gpa.OutMessages) {
if len(ami.dismissPeerBuf) != 0 {
for _, dismissPeerPub := range ami.dismissPeerBuf {
ami.dist.DismissPeer(ami.pubKeyAsNodeID(dismissPeerPub))
}
ami.dismissPeerBuf = []*cryptolib.PublicKey{}
}
if outMsgs == nil {
return
}
outMsgs.MustIterate(func(msg gpa.Message) {
pm := peering.NewPeerMessageData(ami.netPeeringID, peering.ReceiverAccessMgr, msgTypeAccessMgr, msg)
ami.net.SendMsgByPubKey(ami.netPeerPubs[msg.Recipient()], pm)
})
}
func (ami *accessMgrImpl) pubKeyAsNodeID(pubKey *cryptolib.PublicKey) gpa.NodeID {
nodeID := gpa.NodeIDFromPublicKey(pubKey)
if _, ok := ami.netPeerPubs[nodeID]; !ok {
ami.netPeerPubs[nodeID] = pubKey
}
return nodeID
}