Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
p2p
go-p2p-peerstore
Commits
b4ad10c6
Unverified
Commit
b4ad10c6
authored
Apr 29, 2021
by
Steven Allen
Committed by
GitHub
Apr 29, 2021
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #157 from libp2p/fix/delete-addrs-update-zero
fix: delete addrs when "updating" them to zero
parents
74ed1a99
cce0adbd
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
22 additions
and
4 deletions
+22
-4
pstoremem/addr_book.go
pstoremem/addr_book.go
+10
-4
test/addr_book_suite.go
test/addr_book_suite.go
+12
-0
No files found.
pstoremem/addr_book.go
View file @
b4ad10c6
...
@@ -25,7 +25,7 @@ type expiringAddr struct {
...
@@ -25,7 +25,7 @@ type expiringAddr struct {
}
}
func
(
e
*
expiringAddr
)
ExpiredBy
(
t
time
.
Time
)
bool
{
func
(
e
*
expiringAddr
)
ExpiredBy
(
t
time
.
Time
)
bool
{
return
t
.
After
(
e
.
Expires
)
return
!
t
.
Before
(
e
.
Expires
)
}
}
type
peerRecordState
struct
{
type
peerRecordState
struct
{
...
@@ -315,9 +315,15 @@ func (mab *memoryAddrBook) UpdateAddrs(p peer.ID, oldTTL time.Duration, newTTL t
...
@@ -315,9 +315,15 @@ func (mab *memoryAddrBook) UpdateAddrs(p peer.ID, oldTTL time.Duration, newTTL t
defer
s
.
Unlock
()
defer
s
.
Unlock
()
exp
:=
time
.
Now
()
.
Add
(
newTTL
)
exp
:=
time
.
Now
()
.
Add
(
newTTL
)
amap
,
found
:=
s
.
addrs
[
p
]
amap
,
found
:=
s
.
addrs
[
p
]
if
found
{
if
!
found
{
for
k
,
a
:=
range
amap
{
return
if
oldTTL
==
a
.
TTL
{
}
for
k
,
a
:=
range
amap
{
if
oldTTL
==
a
.
TTL
{
if
newTTL
==
0
{
delete
(
amap
,
k
)
}
else
{
a
.
TTL
=
newTTL
a
.
TTL
=
newTTL
a
.
Expires
=
exp
a
.
Expires
=
exp
amap
[
k
]
=
a
amap
[
k
]
=
a
...
...
test/addr_book_suite.go
View file @
b4ad10c6
...
@@ -210,6 +210,18 @@ func testUpdateTTLs(m pstore.AddrBook) func(t *testing.T) {
...
@@ -210,6 +210,18 @@ func testUpdateTTLs(m pstore.AddrBook) func(t *testing.T) {
m
.
UpdateAddrs
(
id
,
time
.
Hour
,
time
.
Minute
)
m
.
UpdateAddrs
(
id
,
time
.
Hour
,
time
.
Minute
)
})
})
t
.
Run
(
"update to 0 clears addrs"
,
func
(
t
*
testing
.
T
)
{
id
:=
GeneratePeerIDs
(
1
)[
0
]
addrs
:=
GenerateAddrs
(
1
)
// Shouldn't panic.
m
.
SetAddrs
(
id
,
addrs
,
time
.
Hour
)
m
.
UpdateAddrs
(
id
,
time
.
Hour
,
0
)
if
len
(
m
.
Addrs
(
id
))
!=
0
{
t
.
Error
(
"expected no addresses"
)
}
})
t
.
Run
(
"update ttls successfully"
,
func
(
t
*
testing
.
T
)
{
t
.
Run
(
"update ttls successfully"
,
func
(
t
*
testing
.
T
)
{
ids
:=
GeneratePeerIDs
(
2
)
ids
:=
GeneratePeerIDs
(
2
)
addrs1
,
addrs2
:=
GenerateAddrs
(
2
),
GenerateAddrs
(
2
)
addrs1
,
addrs2
:=
GenerateAddrs
(
2
),
GenerateAddrs
(
2
)
...
...
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