mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-18 18:02:24 +00:00
whisper: complete wnode message exchange cycle
This commit is contained in:
parent
8148e9fea4
commit
226eccdab0
2 changed files with 36 additions and 14 deletions
|
|
@ -17,6 +17,7 @@
|
||||||
package whisperv6
|
package whisperv6
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"io"
|
||||||
"github.com/ethereum/go-ethereum/log"
|
"github.com/ethereum/go-ethereum/log"
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
|
|
@ -101,7 +102,7 @@ func (stream *LibP2PStream) WriteMsg(msg p2p.Msg) error {
|
||||||
nbytes, err := msg.Payload.Read(data[codeLength+payloadSizeLength:])
|
nbytes, err := msg.Payload.Read(data[codeLength+payloadSizeLength:])
|
||||||
if nbytes > math.MaxInt32 || uint32(nbytes) != msg.Size {
|
if nbytes > math.MaxInt32 || uint32(nbytes) != msg.Size {
|
||||||
return fmt.Errorf("Invalid size read in libp2p stream: read %d bytes, was expecting %d bytes", nbytes, msg.Size)
|
return fmt.Errorf("Invalid size read in libp2p stream: read %d bytes, was expecting %d bytes", nbytes, msg.Size)
|
||||||
} else if err != nil {
|
} else if err != nil && err != io.EOF {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,7 @@
|
||||||
package whisperv6
|
package whisperv6
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"io"
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
|
|
@ -36,6 +37,21 @@ const (
|
||||||
testProtocolID = "/whispertesting/6.1"
|
testProtocolID = "/whispertesting/6.1"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func newTestMsg(t *testing.T, size int) p2p.Msg {
|
||||||
|
code := rand.Uint64()
|
||||||
|
payload := make([]byte, size)
|
||||||
|
n, err := rand.Read(payload)
|
||||||
|
if err != nil || n != size {
|
||||||
|
t.Fatalf("Read %d random bytes instead of the expected %d, err: %v", n, size, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return p2p.Msg{
|
||||||
|
Code: code,
|
||||||
|
Size: math.MaxInt32 + 1,
|
||||||
|
Payload: bytes.NewReader(payload),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Create a network with n mock hosts. Each host in the array is linked to
|
// Create a network with n mock hosts. Each host in the array is linked to
|
||||||
// all hosts preceding it, and has dialed them.
|
// all hosts preceding it, and has dialed them.
|
||||||
func createTestNetwork(ctx context.Context, t *testing.T, n int) []host.Host {
|
func createTestNetwork(ctx context.Context, t *testing.T, n int) []host.Host {
|
||||||
|
|
@ -236,15 +252,9 @@ func TestMaxWriteSize(t *testing.T) {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
hosts := createTestNetwork(ctx, t, 2)
|
hosts := createTestNetwork(ctx, t, 2)
|
||||||
|
|
||||||
code := rand.Uint64()
|
|
||||||
// This isn't the size that will be reported, but if I actually
|
// This isn't the size that will be reported, but if I actually
|
||||||
// require 2GB or RAM the CI servers will fail.
|
// require 2GB or RAM the CI servers will fail.
|
||||||
size := 10
|
msg := newTestMsg(t, 10)
|
||||||
payload := make([]byte, size)
|
|
||||||
n, err := rand.Read(payload)
|
|
||||||
if err != nil || n != size {
|
|
||||||
t.Fatalf("Read %d random bytes instead of the expected %d, err: %v", n, size, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
hosts[0].SetStreamHandler(testProtocolID, func(s inet.Stream) {
|
hosts[0].SetStreamHandler(testProtocolID, func(s inet.Stream) {
|
||||||
defer s.Close()
|
defer s.Close()
|
||||||
|
|
@ -257,12 +267,6 @@ func TestMaxWriteSize(t *testing.T) {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
msg := p2p.Msg{
|
|
||||||
Code: code,
|
|
||||||
Size: math.MaxInt32 + 1,
|
|
||||||
Payload: bytes.NewReader(payload),
|
|
||||||
}
|
|
||||||
|
|
||||||
lps := LibP2PStream{
|
lps := LibP2PStream{
|
||||||
stream: stream,
|
stream: stream,
|
||||||
}
|
}
|
||||||
|
|
@ -311,3 +315,20 @@ func TestMaxReadSize(t *testing.T) {
|
||||||
|
|
||||||
stream.Close()
|
stream.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestEndOfStreamDoesNotBlockWriteMsg checks that an EOF in the
|
||||||
|
// stream will not return an error.
|
||||||
|
func TestEndOfStreamDoesNotBlockWriteMsg(t *testing.T) {
|
||||||
|
r, w := io.Pipe()
|
||||||
|
|
||||||
|
s := mocknet.NewStream(w, r)
|
||||||
|
|
||||||
|
lps := &LibP2PStream{s}
|
||||||
|
|
||||||
|
msg := newTestMsg(t, 5)
|
||||||
|
|
||||||
|
err := lps.WriteMsg(msg)
|
||||||
|
if err == io.EOF {
|
||||||
|
t.Fatalf("EOF got reported as an error")
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue