|
|
@ -22,7 +22,9 @@ package les |
|
|
|
import ( |
|
|
|
import ( |
|
|
|
"context" |
|
|
|
"context" |
|
|
|
"crypto/rand" |
|
|
|
"crypto/rand" |
|
|
|
|
|
|
|
"fmt" |
|
|
|
"math/big" |
|
|
|
"math/big" |
|
|
|
|
|
|
|
"sync/atomic" |
|
|
|
"testing" |
|
|
|
"testing" |
|
|
|
"time" |
|
|
|
"time" |
|
|
|
|
|
|
|
|
|
|
@ -347,7 +349,7 @@ func (p *testPeer) close() { |
|
|
|
p.app.Close() |
|
|
|
p.app.Close() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func newTestPeerPair(name string, version int, server *serverHandler, client *clientHandler) (*testPeer, <-chan error, *testPeer, <-chan error) { |
|
|
|
func newTestPeerPair(name string, version int, server *serverHandler, client *clientHandler) (*testPeer, *testPeer, error) { |
|
|
|
// Create a message pipe to communicate through
|
|
|
|
// Create a message pipe to communicate through
|
|
|
|
app, net := p2p.MsgPipe() |
|
|
|
app, net := p2p.MsgPipe() |
|
|
|
|
|
|
|
|
|
|
@ -371,11 +373,25 @@ func newTestPeerPair(name string, version int, server *serverHandler, client *cl |
|
|
|
go func() { |
|
|
|
go func() { |
|
|
|
select { |
|
|
|
select { |
|
|
|
case <-client.closeCh: |
|
|
|
case <-client.closeCh: |
|
|
|
errc1 <- p2p.DiscQuitting |
|
|
|
errc2 <- p2p.DiscQuitting |
|
|
|
case errc1 <- client.handle(peer2): |
|
|
|
case errc2 <- client.handle(peer2): |
|
|
|
} |
|
|
|
} |
|
|
|
}() |
|
|
|
}() |
|
|
|
return &testPeer{cpeer: peer1, net: net, app: app}, errc1, &testPeer{speer: peer2, net: app, app: net}, errc2 |
|
|
|
// Ensure the connection is established or exits when any error occurs
|
|
|
|
|
|
|
|
for { |
|
|
|
|
|
|
|
select { |
|
|
|
|
|
|
|
case err := <-errc1: |
|
|
|
|
|
|
|
return nil, nil, fmt.Errorf("Failed to establish protocol connection %v", err) |
|
|
|
|
|
|
|
case err := <-errc2: |
|
|
|
|
|
|
|
return nil, nil, fmt.Errorf("Failed to establish protocol connection %v", err) |
|
|
|
|
|
|
|
default: |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if atomic.LoadUint32(&peer1.serving) == 1 && atomic.LoadUint32(&peer2.serving) == 1 { |
|
|
|
|
|
|
|
break |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
time.Sleep(50 * time.Millisecond) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return &testPeer{cpeer: peer1, net: net, app: app}, &testPeer{speer: peer2, net: app, app: net}, nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// handshake simulates a trivial handshake that expects the same state from the
|
|
|
|
// handshake simulates a trivial handshake that expects the same state from the
|
|
|
@ -514,17 +530,20 @@ func newClientServerEnv(t *testing.T, blocks int, protocol int, callback indexer |
|
|
|
callback(scIndexer, sbIndexer, sbtIndexer) |
|
|
|
callback(scIndexer, sbIndexer, sbtIndexer) |
|
|
|
} |
|
|
|
} |
|
|
|
var ( |
|
|
|
var ( |
|
|
|
|
|
|
|
err error |
|
|
|
speer, cpeer *testPeer |
|
|
|
speer, cpeer *testPeer |
|
|
|
err1, err2 <-chan error |
|
|
|
|
|
|
|
) |
|
|
|
) |
|
|
|
if connect { |
|
|
|
if connect { |
|
|
|
cpeer, err1, speer, err2 = newTestPeerPair("peer", protocol, server, client) |
|
|
|
done := make(chan struct{}) |
|
|
|
|
|
|
|
client.syncDone = func() { close(done) } |
|
|
|
|
|
|
|
cpeer, speer, err = newTestPeerPair("peer", protocol, server, client) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
t.Fatalf("Failed to connect testing peers %v", err) |
|
|
|
|
|
|
|
} |
|
|
|
select { |
|
|
|
select { |
|
|
|
case <-time.After(time.Millisecond * 300): |
|
|
|
case <-done: |
|
|
|
case err := <-err1: |
|
|
|
case <-time.After(3 * time.Second): |
|
|
|
t.Fatalf("peer 1 handshake error: %v", err) |
|
|
|
t.Fatal("test peer did not connect and sync within 3s") |
|
|
|
case err := <-err2: |
|
|
|
|
|
|
|
t.Fatalf("peer 2 handshake error: %v", err) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
s := &testServer{ |
|
|
|
s := &testServer{ |
|
|
|