Skip to content
GitLab
Menu
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
f3c490a4
Commit
f3c490a4
authored
Jan 05, 2015
by
Juan Batiz-Benet
Browse files
peer+mocknet: sorting for determinism.
parent
e4f1221b
Changes
2
Hide whitespace changes
Inline
Side-by-side
net/mock/mock_net.go
View file @
f3c490a4
...
...
@@ -2,6 +2,7 @@ package mocknet
import
(
"fmt"
"sort"
"sync"
"time"
...
...
@@ -90,6 +91,7 @@ func (mn *mocknet) Peers() []peer.ID {
for
_
,
n
:=
range
mn
.
nets
{
cp
=
append
(
cp
,
n
.
peer
)
}
sort
.
Sort
(
peer
.
IDSlice
(
cp
))
return
cp
}
...
...
@@ -115,6 +117,8 @@ func (mn *mocknet) Hosts() []host.Host {
for
_
,
h
:=
range
mn
.
hosts
{
cp
=
append
(
cp
,
h
)
}
sort
.
Sort
(
hostSlice
(
cp
))
return
cp
}
...
...
@@ -126,6 +130,7 @@ func (mn *mocknet) Nets() []inet.Network {
for
_
,
n
:=
range
mn
.
nets
{
cp
=
append
(
cp
,
n
)
}
sort
.
Sort
(
netSlice
(
cp
))
return
cp
}
...
...
@@ -339,3 +344,17 @@ func (mn *mocknet) LinkDefaults() LinkOptions {
defer
mn
.
RUnlock
()
return
mn
.
linkDefaults
}
// netSlice for sorting by peer
type
netSlice
[]
inet
.
Network
func
(
es
netSlice
)
Len
()
int
{
return
len
(
es
)
}
func
(
es
netSlice
)
Swap
(
i
,
j
int
)
{
es
[
i
],
es
[
j
]
=
es
[
j
],
es
[
i
]
}
func
(
es
netSlice
)
Less
(
i
,
j
int
)
bool
{
return
string
(
es
[
i
]
.
LocalPeer
())
<
string
(
es
[
j
]
.
LocalPeer
())
}
// hostSlice for sorting by peer
type
hostSlice
[]
host
.
Host
func
(
es
hostSlice
)
Len
()
int
{
return
len
(
es
)
}
func
(
es
hostSlice
)
Swap
(
i
,
j
int
)
{
es
[
i
],
es
[
j
]
=
es
[
j
],
es
[
i
]
}
func
(
es
hostSlice
)
Less
(
i
,
j
int
)
bool
{
return
string
(
es
[
i
]
.
ID
())
<
string
(
es
[
j
]
.
ID
())
}
peer/peer.go
View file @
f3c490a4
...
...
@@ -130,3 +130,10 @@ type PeerInfo struct {
ID
ID
Addrs
[]
ma
.
Multiaddr
}
// IDSlice for sorting peers
type
IDSlice
[]
ID
func
(
es
IDSlice
)
Len
()
int
{
return
len
(
es
)
}
func
(
es
IDSlice
)
Swap
(
i
,
j
int
)
{
es
[
i
],
es
[
j
]
=
es
[
j
],
es
[
i
]
}
func
(
es
IDSlice
)
Less
(
i
,
j
int
)
bool
{
return
string
(
es
[
i
])
<
string
(
es
[
j
])
}
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