mirror of
https://github.com/ipfs/kubo.git
synced 2025-06-27 16:07:42 +08:00
peer+mocknet: sorting for determinism.
This commit is contained in:
@ -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()) }
|
||||
|
@ -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]) }
|
||||
|
Reference in New Issue
Block a user