Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
adam.huang
go-libp2p
Commits
d8565dd1
Commit
d8565dd1
authored
8 years ago
by
Jakub Sztandera
Committed by
GitHub
8 years ago
Browse files
Options
Download
Plain Diff
Merge pull request #191 from libp2p/feat/identify/smarter-obsaddrs
Improve ObservedAddrSet behaviour
parents
c5aa669d
ed98e371
master
2018-Q4-OKR
feat/protobuf
feat/udp
fix/473
fix/no-custom-field
fix/reset-ping-stream
fix/revert-correct-external-addr
gx/update-jccl6u
gx/update-nza0mn
jenkinsfile
kevina/fix-go-vet
multistream-ping
punching
revert-276-update-go-detect-race
v6.0.23
v6.0.22
v6.0.21
v6.0.20
v6.0.19
v6.0.18
v6.0.17
v6.0.16
v6.0.15
v6.0.14
v6.0.13
v6.0.12
v6.0.11
v6.0.10
v6.0.9
v6.0.8
v6.0.7
v6.0.6
v6.0.5
v6.0.4
v6.0.3
v6.0.2
v6.0.1
v6.0.0
v5.0.21
v5.0.20
v5.0.19
v5.0.18
v5.0.17
v5.0.16
v5.0.15
v5.0.14
v5.0.13
v5.0.12
v5.0.11
v5.0.10
v5.0.9
v5.0.8
v5.0.7
v5.0.6
v5.0.5
v5.0.4
v5.0.3
v5.0.2
v5.0.1
v5.0.0
v4.5.5
v4.5.4
v4.5.3
v4.5.2
v4.5.1
v4.5.0
v4.4.5
v4.4.4
v4.4.3
v4.4.2
v4.4.1
v4.4.0
v4.3.12
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
p2p/protocol/identify/obsaddr.go
+26
-17
p2p/protocol/identify/obsaddr.go
p2p/protocol/identify/obsaddr_test.go
+22
-10
p2p/protocol/identify/obsaddr_test.go
with
48 additions
and
27 deletions
+48
-27
p2p/protocol/identify/obsaddr.go
View file @
d8565dd1
...
...
@@ -8,15 +8,32 @@ import (
ma
"github.com/multiformats/go-multiaddr"
)
const
ActivationThresh
=
4
// ObservedAddr is an entry for an address reported by our peers.
// We only use addresses that:
// - have been observed
more than once
. (counter symmetric nats)
// - have been observed recently (1
0min
), because our position in the
// - have been observed
at least 4 times in last 1h
. (counter symmetric nats)
// - have been observed
at least once
recently (1
h
), because our position in the
// network, or network port mapppings, may have changed.
type
ObservedAddr
struct
{
Addr
ma
.
Multiaddr
SeenBy
map
[
string
]
struct
{}
LastSeen
time
.
Time
Addr
ma
.
Multiaddr
SeenBy
map
[
string
]
time
.
Time
LastSeen
time
.
Time
Activated
bool
}
func
(
oa
*
ObservedAddr
)
TryActivate
(
ttl
time
.
Duration
)
bool
{
// cleanup SeenBy set
now
:=
time
.
Now
()
for
k
,
t
:=
range
oa
.
SeenBy
{
if
now
.
Sub
(
t
)
>
ttl
*
ActivationThresh
{
delete
(
oa
.
SeenBy
,
k
)
}
}
// We only activate if in the TTL other peers observed the same address
// of ours at least 4 times.
return
len
(
oa
.
SeenBy
)
>=
ActivationThresh
}
// ObservedAddrSet keeps track of a set of ObservedAddrs
...
...
@@ -46,16 +63,7 @@ func (oas *ObservedAddrSet) Addrs() []ma.Multiaddr {
continue
}
// we only use an address if we've seen it more than once
// because symmetric nats may cause all our peers to see
// different port numbers and thus report always different
// addresses (different ports) for us. These wouldn't be
// very useful. We make the assumption that if we've
// connected to two different peers, and they both have
// reported seeing the same address, it is probably useful.
//
// Note: make sure not to double count observers.
if
len
(
a
.
SeenBy
)
>
1
{
if
a
.
Activated
||
a
.
TryActivate
(
oas
.
ttl
)
{
addrs
=
append
(
addrs
,
a
.
Addr
)
}
}
...
...
@@ -79,13 +87,13 @@ func (oas *ObservedAddrSet) Add(addr ma.Multiaddr, observer ma.Multiaddr) {
if
!
found
{
oa
=
&
ObservedAddr
{
Addr
:
addr
,
SeenBy
:
make
(
map
[
string
]
struct
{}
),
SeenBy
:
make
(
map
[
string
]
time
.
Time
),
}
oas
.
addrs
[
s
]
=
oa
}
// mark the observer
oa
.
SeenBy
[
observerGroup
(
observer
)]
=
struct
{}{}
oa
.
SeenBy
[
observerGroup
(
observer
)]
=
time
.
Now
()
oa
.
LastSeen
=
time
.
Now
()
}
...
...
@@ -100,6 +108,7 @@ func (oas *ObservedAddrSet) Add(addr ma.Multiaddr, observer ma.Multiaddr) {
// Here, we use the root multiaddr address. This is mostly
// IP addresses. In practice, this is what we want.
func
observerGroup
(
m
ma
.
Multiaddr
)
string
{
//TODO: If IPv6 rolls out we should mark /64 routing zones as one group
return
ma
.
Split
(
m
)[
0
]
.
String
()
}
...
...
This diff is collapsed.
Click to expand it.
p2p/protocol/identify/obsaddr_test.go
View file @
d8565dd1
...
...
@@ -18,6 +18,10 @@ func TestObsAddrSet(t *testing.T) {
}
addrsMarch
:=
func
(
a
,
b
[]
ma
.
Multiaddr
)
bool
{
if
len
(
a
)
!=
len
(
b
)
{
return
false
}
for
_
,
aa
:=
range
a
{
found
:=
false
for
_
,
bb
:=
range
b
{
...
...
@@ -38,8 +42,12 @@ func TestObsAddrSet(t *testing.T) {
a3
:=
m
(
"/ip4/1.2.3.4/tcp/1233"
)
a4
:=
m
(
"/ip4/1.2.3.4/tcp/1234"
)
a5
:=
m
(
"/ip4/1.2.3.4/tcp/1235"
)
a6
:=
m
(
"/ip4/1.2.3.6/tcp/1236"
)
a7
:=
m
(
"/ip4/1.2.3.7/tcp/1237"
)
b1
:=
m
(
"/ip4/1.2.3.6/tcp/1236"
)
b2
:=
m
(
"/ip4/1.2.3.7/tcp/1237"
)
b3
:=
m
(
"/ip4/1.2.3.8/tcp/1237"
)
b4
:=
m
(
"/ip4/1.2.3.9/tcp/1237"
)
b5
:=
m
(
"/ip4/1.2.3.10/tcp/1237"
)
oas
:=
ObservedAddrSet
{}
...
...
@@ -72,7 +80,9 @@ func TestObsAddrSet(t *testing.T) {
t
.
Error
(
"addrs should _still_ be empty (same obs group)"
)
}
oas
.
Add
(
a1
,
a6
)
oas
.
Add
(
a1
,
b1
)
oas
.
Add
(
a1
,
b2
)
oas
.
Add
(
a1
,
b3
)
if
!
addrsMarch
(
oas
.
Addrs
(),
[]
ma
.
Multiaddr
{
a1
})
{
t
.
Error
(
"addrs should only have a1"
)
}
...
...
@@ -80,12 +90,14 @@ func TestObsAddrSet(t *testing.T) {
oas
.
Add
(
a2
,
a5
)
oas
.
Add
(
a1
,
a5
)
oas
.
Add
(
a1
,
a5
)
oas
.
Add
(
a2
,
a6
)
oas
.
Add
(
a1
,
a6
)
oas
.
Add
(
a1
,
a6
)
oas
.
Add
(
a2
,
a7
)
oas
.
Add
(
a1
,
a7
)
oas
.
Add
(
a1
,
a7
)
oas
.
Add
(
a2
,
b1
)
oas
.
Add
(
a1
,
b1
)
oas
.
Add
(
a1
,
b1
)
oas
.
Add
(
a2
,
b2
)
oas
.
Add
(
a1
,
b2
)
oas
.
Add
(
a1
,
b2
)
oas
.
Add
(
a2
,
b4
)
oas
.
Add
(
a2
,
b5
)
if
!
addrsMarch
(
oas
.
Addrs
(),
[]
ma
.
Multiaddr
{
a1
,
a2
})
{
t
.
Error
(
"addrs should only have a1, a2"
)
}
...
...
@@ -93,7 +105,7 @@ func TestObsAddrSet(t *testing.T) {
// change the timeout constant so we can time it out.
oas
.
SetTTL
(
time
.
Millisecond
*
200
)
<-
time
.
After
(
time
.
Millisecond
*
210
)
if
!
addrsMarch
(
oas
.
Addrs
(),
[]
ma
.
Multiaddr
{
nil
}
)
{
if
!
addrsMarch
(
oas
.
Addrs
(),
nil
)
{
t
.
Error
(
"addrs should have timed out"
)
}
}
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment
Menu
Projects
Groups
Snippets
Help