-
Notifications
You must be signed in to change notification settings - Fork 3
/
reachable.go
238 lines (210 loc) · 5.71 KB
/
reachable.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
233
234
235
236
237
238
package base
import (
"context"
"fmt"
"io"
"time"
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-routing-language/parse"
xr "github.com/libp2p/go-routing-language/syntax"
ma "github.com/multiformats/go-multiaddr"
"github.com/libp2p/go-smart-record/ir"
meta "github.com/libp2p/go-smart-record/ir/metadata"
)
// Reachable is a smart node. It detects if the multiaddrs is
// connected or dialable.
type Reachable struct {
// Multiaddr checked
addr ma.Multiaddr
// What do we need to verify?
verifyConn bool
verifyDial bool
// Has it been verified?
verifiedConn bool
verifiedDial bool
verifiedFail bool
verifiedFailConn bool
metadataCtx *meta.Meta
}
// Reachable disassembles to reachable xr.Predicate of the form
// dialed(address=MULTIADDRESS:STRING) if dial checks
// connected(address=MULTIADDRESS:STRING) if connected checks.
// notConnected(address=MULTIADDRESS:STRING) if connected fails.
// notDialable(address=MULTIADDRESS:STRING) if dial fails.
func (r Reachable) Disassemble() xr.Node {
var tag string
// Set right tag for predicate
if r.verifiedConn {
tag = "connected"
} else if r.verifiedDial {
tag = "dialed"
} else if r.verifiedFail {
tag = "notDialable"
} else if r.verifiedFailConn {
tag = "notConnected"
} else {
// This means that nothing has been verified
// Disassemble the predicate as-is
if r.verifyConn {
tag = "connectivity"
} else if r.verifyDial {
tag = "dialable"
}
}
return xr.Predicate{
Tag: tag,
Named: xr.Pairs{
xr.Pair{
Key: xr.String{Value: "address"},
Value: xr.String{Value: r.addr.String()},
},
},
}
}
func (r *Reachable) Metadata() meta.MetadataInfo {
return r.metadataCtx.Get()
}
func (r *Reachable) WritePretty(w io.Writer) error {
return r.Disassemble().WritePretty(w)
}
func (r *Reachable) UpdateWith(ctx ir.UpdateContext, with ir.Node) error {
w, ok := with.(*Reachable)
if !ok {
return fmt.Errorf("cannot update with a non-reachable node")
}
// Update value
*r = *w
// Update metadata
r.metadataCtx.Update(w.metadataCtx)
return nil
}
// getNamed returns the xr.Node in a key.
// NOTE: Consider adding this as a function of xr.Predicates
// in the routing-language, and remove it from here.
func getNamed(p xr.Predicate, key xr.Node) xr.Node {
for _, ps := range p.Named {
if xr.IsEqual(ps.Key, key) {
return ps.Value
}
}
return nil
}
type ReachableAssembler struct{}
// Reachable assemble expects a predicate of the form:
// connectivity(address=MULTIADDRESS) or
// dialable(address=MULTIADDRESS)
// See Disassemble() for more info on the resulting predicates after check.
func (ReachableAssembler) Assemble(ctx ir.AssemblerContext, srcNode xr.Node, metadata ...meta.Metadata) (ir.Node, error) {
// Reachable receives a predicate
p, ok := srcNode.(xr.Predicate)
if !ok {
return nil, fmt.Errorf("smart-tags must be predicates")
}
// Get tag and positional arguments.
tag := p.Tag
addr := getNamed(p, xr.String{Value: "address"})
// Check tag
if tag != "connectivity" && tag != "dialable" {
return nil, fmt.Errorf("not a reachable smart tag")
}
// Check multiaddress
maddr, err := parse.ParseMultiaddr(&parse.ParseCtx{}, addr)
if err != nil {
return nil, fmt.Errorf("no valid multiaddr provided")
}
// Assemble metadata
m := meta.New()
if err := m.Apply(metadata...); err != nil {
return nil, err
}
return &Reachable{
addr: maddr,
verifyConn: tag == "connectivity",
verifyDial: tag == "dialable",
metadataCtx: m,
}, nil
}
// TriggerReachable triggers the execution of Reachable verifications
// over a dict and adds the appropiate flag to Nodes that don't pass the verification.
func TriggerReachable(d *ir.Dict, h host.Host) {
// For each pair.
for _, p := range d.Pairs {
triggerReachable(p.Key, h)
triggerReachable(p.Value, h)
}
}
func triggerReachable(n ir.Node, h host.Host) {
switch n1 := n.(type) {
case *Reachable:
n1.verify(h)
case *ir.Dict:
TriggerReachable(n1, h)
case *ir.List:
verifyList(n1, h)
}
}
func verifyList(s *ir.List, h host.Host) {
// For each element
for _, e := range s.Elements {
triggerReachable(e, h)
}
}
// trigger the verification of reachable
// updates flags according to the verification result
func (r *Reachable) verify(h host.Host) {
info, err := peer.AddrInfoFromP2pAddr(r.addr)
// If there is an error, the verification is not successful.
if err != nil {
r.verifiedFail = true
return
}
// If dialable verification enabled and not checked.
if r.verifyDial && !r.verifiedDial {
// Set verifyFail if the verification failed.
if c := checkIfDialable(h, *info); !c {
r.verifiedFail = true
return
}
// Set verified flag
r.verifiedDial = true
}
// If connected verification enabled and not checked.
if r.verifyConn && !r.verifiedConn {
// Set verifyFail if the verification failed.
if c := checkIfConnected(h, *info); !c {
r.verifiedFailConn = true
return
}
// Set verified flag
r.verifiedConn = true
}
}
// CheckIfdialable Checks if peer reachable with 5s timeout.
func checkIfDialable(h host.Host, i peer.AddrInfo) bool {
// If self, consider as reachable and don't try to connect
if h.ID() == i.ID {
return true
}
reachable := false
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
if err := h.Connect(ctx, i); err == nil {
reachable = true
}
cancel()
return reachable
}
// CheckIfConnected checks if we are currently connected to a peer.
func checkIfConnected(h host.Host, i peer.AddrInfo) bool {
// If self, consider as connected and don't try to connect
if h.ID() == i.ID {
return true
}
// Check if we are connected to peer
for _, p := range h.Network().Peers() {
if p == i.ID {
return true
}
}
return false
}