Skip to content

Commit 5a5560f

Browse files
committed
rlp: add Stream.Reset and accept any reader (for p2p)
1 parent 59b63ca commit 5a5560f

2 files changed

Lines changed: 66 additions & 7 deletions

File tree

‎rlp/decode.go‎

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package rlp
22

33
import (
4+
"bufio"
45
"encoding/binary"
56
"errors"
67
"fmt"
@@ -24,8 +25,9 @@ type Decoder interface {
2425
DecodeRLP(*Stream) error
2526
}
2627

27-
// Decode parses RLP-encoded data from r and stores the result
28-
// in the value pointed to by val. Val must be a non-nil pointer.
28+
// Decode parses RLP-encoded data from r and stores the result in the
29+
// value pointed to by val. Val must be a non-nil pointer. If r does
30+
// not implement ByteReader, Decode will do its own buffering.
2931
//
3032
// Decode uses the following type-dependent decoding rules:
3133
//
@@ -66,7 +68,7 @@ type Decoder interface {
6668
//
6769
// Non-empty interface types are not supported, nor are bool, float32,
6870
// float64, maps, channel types and functions.
69-
func Decode(r ByteReader, val interface{}) error {
71+
func Decode(r io.Reader, val interface{}) error {
7072
return NewStream(r).Decode(val)
7173
}
7274

@@ -432,8 +434,14 @@ type Stream struct {
432434

433435
type listpos struct{ pos, size uint64 }
434436

435-
func NewStream(r ByteReader) *Stream {
436-
return &Stream{r: r, uintbuf: make([]byte, 8), kind: -1}
437+
// NewStream creates a new stream reading from r.
438+
// If r does not implement ByteReader, the Stream will
439+
// introduce its own buffering.
440+
func NewStream(r io.Reader) *Stream {
441+
s := new(Stream)
442+
s.Reset(r)
443+
return s
444+
}
437445
}
438446

439447
// Bytes reads an RLP string and returns its contents as a byte slice.
@@ -543,6 +551,23 @@ func (s *Stream) Decode(val interface{}) error {
543551
return info.decoder(s, rval.Elem())
544552
}
545553

554+
// Reset discards any information about the current decoding context
555+
// and starts reading from r. If r does not also implement ByteReader,
556+
// Stream will do its own buffering.
557+
func (s *Stream) Reset(r io.Reader) {
558+
bufr, ok := r.(ByteReader)
559+
if !ok {
560+
bufr = bufio.NewReader(r)
561+
}
562+
s.r = bufr
563+
s.stack = s.stack[:0]
564+
s.size = 0
565+
s.kind = -1
566+
if s.uintbuf == nil {
567+
s.uintbuf = make([]byte, 8)
568+
}
569+
}
570+
546571
// Kind returns the kind and size of the next value in the
547572
// input stream.
548573
//

‎rlp/decode_test.go‎

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,14 +286,14 @@ var decodeTests = []decodeTest{
286286

287287
func intp(i int) *int { return &i }
288288

289-
func TestDecode(t *testing.T) {
289+
func runTests(t *testing.T, decode func([]byte, interface{}) error) {
290290
for i, test := range decodeTests {
291291
input, err := hex.DecodeString(test.input)
292292
if err != nil {
293293
t.Errorf("test %d: invalid hex input %q", i, test.input)
294294
continue
295295
}
296-
err = Decode(bytes.NewReader(input), test.ptr)
296+
err = decode(input, test.ptr)
297297
if err != nil && test.error == nil {
298298
t.Errorf("test %d: unexpected Decode error: %v\ndecoding into %T\ninput %q",
299299
i, err, test.ptr, test.input)
@@ -312,6 +312,40 @@ func TestDecode(t *testing.T) {
312312
}
313313
}
314314

315+
func TestDecodeWithByteReader(t *testing.T) {
316+
runTests(t, func(input []byte, into interface{}) error {
317+
return Decode(bytes.NewReader(input), into)
318+
})
319+
}
320+
321+
// dumbReader reads from a byte slice but does not
322+
// implement ReadByte.
323+
type dumbReader []byte
324+
325+
func (r *dumbReader) Read(buf []byte) (n int, err error) {
326+
if len(*r) == 0 {
327+
return 0, io.EOF
328+
}
329+
n = copy(buf, *r)
330+
*r = (*r)[n:]
331+
return n, nil
332+
}
333+
334+
func TestDecodeWithNonByteReader(t *testing.T) {
335+
runTests(t, func(input []byte, into interface{}) error {
336+
r := dumbReader(input)
337+
return Decode(&r, into)
338+
})
339+
}
340+
341+
func TestDecodeStreamReset(t *testing.T) {
342+
s := NewStream(nil)
343+
runTests(t, func(input []byte, into interface{}) error {
344+
s.Reset(bytes.NewReader(input))
345+
return s.Decode(into)
346+
})
347+
}
348+
315349
type testDecoder struct{ called bool }
316350

317351
func (t *testDecoder) DecodeRLP(s *Stream) error {

0 commit comments

Comments
 (0)