forked from mysteriumnetwork/node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiscovery.go
199 lines (172 loc) · 5.11 KB
/
discovery.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
/*
* Copyright (C) 2018 The "MysteriumNetwork/node" Authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package discovery
import (
"time"
log "github.com/cihub/seelog"
"github.com/mysteriumnetwork/node/identity"
identity_registry "github.com/mysteriumnetwork/node/identity/registry"
dto_discovery "github.com/mysteriumnetwork/node/service_discovery/dto"
)
// Status describes stage of proposal registration
type Status int
// Proposal registration stages
const (
IdentityUnregistered Status = iota
WaitingForRegistration
IdentityRegisterFailed
RegisterProposal
PingProposal
UnregisterProposal
UnregisterProposalFailed
ProposalUnregistered
StatusUndefined
)
const logPrefix = "[discovery] "
// Start launches discovery service
func (d *Discovery) Start(ownIdentity identity.Identity, proposal dto_discovery.ServiceProposal) {
d.RLock()
defer d.RUnlock()
d.ownIdentity = ownIdentity
d.signer = d.signerCreate(ownIdentity)
d.proposal = proposal
stopLoop := make(chan bool)
d.stop = func() {
// cancel (stop) discovery loop
stopLoop <- true
}
d.proposalAnnouncementStopped.Add(1)
go d.checkRegistration()
go d.mainDiscoveryLoop(stopLoop)
}
// Wait wait for proposal announcements to stop / unregister
func (d *Discovery) Wait() {
d.proposalAnnouncementStopped.Wait()
}
// Stop stops discovery loop
func (d *Discovery) Stop() {
d.stop()
}
func (d *Discovery) mainDiscoveryLoop(stopLoop chan bool) {
for {
select {
case <-stopLoop:
d.stopLoop()
case event := <-d.statusChan:
switch event {
case IdentityUnregistered:
d.registerIdentity()
case RegisterProposal:
go d.registerProposal()
case PingProposal:
go d.pingProposal()
case UnregisterProposal:
go d.unregisterProposal()
case IdentityRegisterFailed, ProposalUnregistered, UnregisterProposalFailed:
d.proposalAnnouncementStopped.Done()
return
}
}
}
}
func (d *Discovery) stopLoop() {
log.Info(logPrefix, "stopping discovery loop..")
d.RLock()
if d.status == WaitingForRegistration {
d.RUnlock()
d.unsubscribe()
d.RLock()
}
if d.status == RegisterProposal || d.status == PingProposal {
d.RUnlock()
d.changeStatus(UnregisterProposal)
return
}
d.RUnlock()
}
func (d *Discovery) registerIdentity() {
registerEventChan, unsubscribe := d.identityRegistry.SubscribeToRegistrationEvent(d.ownIdentity)
d.unsubscribe = unsubscribe
d.changeStatus(WaitingForRegistration)
go func() {
registerEvent := <-registerEventChan
switch registerEvent {
case identity_registry.Registered:
log.Info(logPrefix, "identity registered, proceeding with proposal registration")
d.changeStatus(RegisterProposal)
case identity_registry.Cancelled:
log.Info(logPrefix, "cancelled identity registration")
d.changeStatus(IdentityRegisterFailed)
}
}()
}
func (d *Discovery) registerProposal() {
err := d.mysteriumClient.RegisterProposal(d.proposal, d.signer)
if err != nil {
log.Errorf("%s Failed to register proposal, retrying after 1 min. %s", logPrefix, err.Error())
time.Sleep(1 * time.Minute)
d.changeStatus(RegisterProposal)
return
}
d.changeStatus(PingProposal)
}
func (d *Discovery) pingProposal() {
time.Sleep(1 * time.Minute)
err := d.mysteriumClient.PingProposal(d.proposal, d.signer)
if err != nil {
log.Error(logPrefix, "Failed to ping proposal: ", err)
}
d.changeStatus(PingProposal)
}
func (d *Discovery) unregisterProposal() {
err := d.mysteriumClient.UnregisterProposal(d.proposal, d.signer)
if err != nil {
log.Error(logPrefix, "Failed to unregister proposal: ", err)
d.changeStatus(UnregisterProposalFailed)
}
log.Info(logPrefix, "Proposal unregistered")
d.changeStatus(ProposalUnregistered)
}
func (d *Discovery) checkRegistration() {
// check if node's identity is registered
registered, err := d.identityRegistry.IsRegistered(d.ownIdentity)
if err != nil {
d.changeStatus(IdentityRegisterFailed)
return
}
if !registered {
// if not registered - wait indefinitely for identity registration event
registrationData, err := d.identityRegistration.ProvideRegistrationData(d.ownIdentity)
if err != nil {
d.changeStatus(IdentityRegisterFailed)
return
}
identity_registry.PrintRegistrationData(registrationData)
log.Infof("%s identity %s not registered, delaying proposal registration until identity is registered", logPrefix, d.ownIdentity.Address)
d.changeStatus(IdentityUnregistered)
return
}
d.changeStatus(RegisterProposal)
}
func (d *Discovery) changeStatus(status Status) {
d.Lock()
defer d.Unlock()
d.status = status
go func() {
d.statusChan <- status
}()
}