mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-19 18:32:23 +00:00
swarm/network: Rework Health test to strict
Pending add test for saturation And add test for as many as possible up to saturation
This commit is contained in:
parent
89e7259149
commit
bdcca4cdf4
2 changed files with 83 additions and 31 deletions
|
|
@ -471,7 +471,6 @@ func depthForPot(p *pot.Pot, minProxBinSize int, pivotAddr []byte) (depth int) {
|
|||
p.EachNeighbour(pivotAddr, Pof, f)
|
||||
|
||||
p.EachBin(pivotAddr, Pof, 0, func(po int, _ int, f func(func(pot.Val, int) bool) bool) bool {
|
||||
log.Trace("eachbin", "addr", pivotAddr, "po", po)
|
||||
if po == depth {
|
||||
if maxDepth == depth {
|
||||
return false
|
||||
|
|
@ -639,7 +638,6 @@ func NewPeerPotMap(minProxBinSize int, addrs [][]byte) map[string]*PeerPot {
|
|||
if po == 256 {
|
||||
return true
|
||||
}
|
||||
|
||||
// append any neighbors found
|
||||
// a neighbor is any peer in or deeper than the depth
|
||||
if po >= depth {
|
||||
|
|
|
|||
|
|
@ -162,43 +162,96 @@ func TestNeighbourhoodDepth(t *testing.T) {
|
|||
testNum++
|
||||
}
|
||||
|
||||
func TestHealth(t *testing.T) {
|
||||
t.Skip("foo")
|
||||
k := newTestKademlia("00000000")
|
||||
assertHealth(t, k, false)
|
||||
Register(k, "00001000")
|
||||
log.Trace(k.String())
|
||||
assertHealth(t, k, false)
|
||||
On(k, "00001000")
|
||||
assertHealth(t, k, true)
|
||||
Register(k, "00000100")
|
||||
log.Trace(k.String())
|
||||
assertHealth(t, k, false)
|
||||
On(k, "00000100")
|
||||
assertHealth(t, k, true)
|
||||
Register(k, "10000000")
|
||||
log.Trace(k.String())
|
||||
assertHealth(t, k, false)
|
||||
On(k, "10000000")
|
||||
assertHealth(t, k, true)
|
||||
Register(k, "00100000")
|
||||
log.Trace(k.String())
|
||||
assertHealth(t, k, false)
|
||||
On(k, "00100000")
|
||||
assertHealth(t, k, true)
|
||||
Register(k, "01000000")
|
||||
log.Trace(k.String())
|
||||
assertHealth(t, k, false)
|
||||
On(k, "01000000")
|
||||
assertHealth(t, k, true)
|
||||
func TestHealthStrict(t *testing.T) {
|
||||
|
||||
// base address is all zeros
|
||||
// no peers
|
||||
// unhealthy (and lonely)
|
||||
k := newTestKademlia("11111111")
|
||||
if err := assertHealth(t, k, false, false); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
func assertHealth(t *testing.T, k *Kademlia, expectHealthy bool) {
|
||||
// know one peer but not connected
|
||||
// unhealthy
|
||||
Register(k, "11100000")
|
||||
log.Trace(k.String())
|
||||
if err := assertHealth(t, k, false, false); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// know one peer and connected
|
||||
// healthy
|
||||
On(k, "11100000")
|
||||
if err := assertHealth(t, k, true, false); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// know two peers, only one connected
|
||||
// unhealthy
|
||||
Register(k, "11111100")
|
||||
log.Trace(k.String())
|
||||
if err := assertHealth(t, k, false, false); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// know two peers and connected to both
|
||||
// healthy
|
||||
On(k, "11111100")
|
||||
if err := assertHealth(t, k, true, false); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// know three peers, connected to the two deepest
|
||||
// healthy
|
||||
Register(k, "00000000")
|
||||
log.Trace(k.String())
|
||||
if err := assertHealth(t, k, true, false); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// know three peers, connected to all three
|
||||
// healthy
|
||||
On(k, "00000000")
|
||||
if err := assertHealth(t, k, true, false); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// add fourth peer deeper than current depth
|
||||
// unhealthy
|
||||
Register(k, "11110000")
|
||||
log.Trace(k.String())
|
||||
if err := assertHealth(t, k, false, false); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// connected to three deepest peers
|
||||
// healthy
|
||||
On(k, "11110000")
|
||||
if err := assertHealth(t, k, true, false); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// add additional peer in same bin as deepest peer
|
||||
// unhealthy
|
||||
Register(k, "11111101")
|
||||
log.Trace(k.String())
|
||||
if err := assertHealth(t, k, false, false); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// four deepest of five peers connected
|
||||
// healthy
|
||||
On(k, "11111101")
|
||||
if err := assertHealth(t, k, true, false); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func assertHealth(t *testing.T, k *Kademlia, expectHealthy bool, expectSaturation bool) error {
|
||||
kid := common.Bytes2Hex(k.BaseAddr())
|
||||
kads := []*Kademlia{k}
|
||||
var addrs [][]byte
|
||||
addrs := [][]byte{k.BaseAddr()}
|
||||
k.EachAddr(nil, 255, func(addr *BzzAddr, po int, _ bool) bool {
|
||||
kads = append(kads, NewKademlia(addr.Address(), newTestKademliaParams()))
|
||||
addrs = append(addrs, addr.Address())
|
||||
return true
|
||||
})
|
||||
|
|
@ -212,8 +265,9 @@ func assertHealth(t *testing.T, k *Kademlia, expectHealthy bool) {
|
|||
// - we are connected to all known neighbors
|
||||
health := healthParams.KnowNN && healthParams.GotNN && healthParams.CountKnowNN > 0
|
||||
if expectHealthy != health {
|
||||
t.Fatalf("expected kademlia health %v, is %v\n%v", expectHealthy, health, k.String())
|
||||
return fmt.Errorf("expected kademlia health %v, is %v\n%v", expectHealthy, health, k.String())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func testSuggestPeer(k *Kademlia, expAddr string, expPo int, expWant bool) error {
|
||||
|
|
|
|||
Loading…
Reference in a new issue