aboutsummaryrefslogtreecommitdiffstats
path: root/rlp
diff options
context:
space:
mode:
authorFelix Lange <fjl@twurst.com>2014-11-25 02:02:04 +0800
committerFelix Lange <fjl@twurst.com>2014-11-25 02:03:17 +0800
commit205af02a1f13f6712a8f30538ddf31cf0544c8d9 (patch)
treeff4d337e35488f4ab159aac20edacc2f4cd72e7c /rlp
parent5a5560f1051b51fae34e799ee8d2dfd8d1094e09 (diff)
downloaddexon-205af02a1f13f6712a8f30538ddf31cf0544c8d9.tar
dexon-205af02a1f13f6712a8f30538ddf31cf0544c8d9.tar.gz
dexon-205af02a1f13f6712a8f30538ddf31cf0544c8d9.tar.bz2
dexon-205af02a1f13f6712a8f30538ddf31cf0544c8d9.tar.lz
dexon-205af02a1f13f6712a8f30538ddf31cf0544c8d9.tar.xz
dexon-205af02a1f13f6712a8f30538ddf31cf0544c8d9.tar.zst
dexon-205af02a1f13f6712a8f30538ddf31cf0544c8d9.zip
rlp: add NewListStream (for p2p)
Diffstat (limited to 'rlp')
-rw-r--r--rlp/decode.go9
-rw-r--r--rlp/decode_test.go18
2 files changed, 27 insertions, 0 deletions
diff --git a/rlp/decode.go b/rlp/decode.go
index 565c84790..3546f6106 100644
--- a/rlp/decode.go
+++ b/rlp/decode.go
@@ -442,6 +442,15 @@ func NewStream(r io.Reader) *Stream {
s.Reset(r)
return s
}
+
+// NewListStream creates a new stream that pretends to be positioned
+// at an encoded list of the given length.
+func NewListStream(r io.Reader, len uint64) *Stream {
+ s := new(Stream)
+ s.Reset(r)
+ s.kind = List
+ s.size = len
+ return s
}
// Bytes reads an RLP string and returns its contents as a byte slice.
diff --git a/rlp/decode_test.go b/rlp/decode_test.go
index 9d320564b..d82ccbd6a 100644
--- a/rlp/decode_test.go
+++ b/rlp/decode_test.go
@@ -54,6 +54,24 @@ func TestStreamKind(t *testing.T) {
}
}
+func TestNewListStream(t *testing.T) {
+ ls := NewListStream(bytes.NewReader(unhex("0101010101")), 3)
+ if k, size, err := ls.Kind(); k != List || size != 3 || err != nil {
+ t.Errorf("Kind() returned (%v, %d, %v), expected (List, 3, nil)", k, size, err)
+ }
+ if size, err := ls.List(); size != 3 || err != nil {
+ t.Errorf("List() returned (%d, %v), expected (3, nil)", size, err)
+ }
+ for i := 0; i < 3; i++ {
+ if val, err := ls.Uint(); val != 1 || err != nil {
+ t.Errorf("Uint() returned (%d, %v), expected (1, nil)", val, err)
+ }
+ }
+ if err := ls.ListEnd(); err != nil {
+ t.Errorf("ListEnd() returned %v, expected (3, nil)", err)
+ }
+}
+
func TestStreamErrors(t *testing.T) {
type calls []string
tests := []struct {