github.com/theQRL/go-zond@v0.1.1/rlp/decode_test.go (about)

     1  // Copyright 2014 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package rlp
    18  
    19  import (
    20  	"bytes"
    21  	"encoding/hex"
    22  	"errors"
    23  	"fmt"
    24  	"io"
    25  	"math/big"
    26  	"reflect"
    27  	"strings"
    28  	"testing"
    29  
    30  	"github.com/holiman/uint256"
    31  	"github.com/theQRL/go-zond/common/math"
    32  )
    33  
    34  func TestStreamKind(t *testing.T) {
    35  	tests := []struct {
    36  		input    string
    37  		wantKind Kind
    38  		wantLen  uint64
    39  	}{
    40  		{"00", Byte, 0},
    41  		{"01", Byte, 0},
    42  		{"7F", Byte, 0},
    43  		{"80", String, 0},
    44  		{"B7", String, 55},
    45  		{"B90400", String, 1024},
    46  		{"BFFFFFFFFFFFFFFFFF", String, ^uint64(0)},
    47  		{"C0", List, 0},
    48  		{"C8", List, 8},
    49  		{"F7", List, 55},
    50  		{"F90400", List, 1024},
    51  		{"FFFFFFFFFFFFFFFFFF", List, ^uint64(0)},
    52  	}
    53  
    54  	for i, test := range tests {
    55  		// using plainReader to inhibit input limit errors.
    56  		s := NewStream(newPlainReader(unhex(test.input)), 0)
    57  		kind, len, err := s.Kind()
    58  		if err != nil {
    59  			t.Errorf("test %d: Kind returned error: %v", i, err)
    60  			continue
    61  		}
    62  		if kind != test.wantKind {
    63  			t.Errorf("test %d: kind mismatch: got %d, want %d", i, kind, test.wantKind)
    64  		}
    65  		if len != test.wantLen {
    66  			t.Errorf("test %d: len mismatch: got %d, want %d", i, len, test.wantLen)
    67  		}
    68  	}
    69  }
    70  
    71  func TestNewListStream(t *testing.T) {
    72  	ls := NewListStream(bytes.NewReader(unhex("0101010101")), 3)
    73  	if k, size, err := ls.Kind(); k != List || size != 3 || err != nil {
    74  		t.Errorf("Kind() returned (%v, %d, %v), expected (List, 3, nil)", k, size, err)
    75  	}
    76  	if size, err := ls.List(); size != 3 || err != nil {
    77  		t.Errorf("List() returned (%d, %v), expected (3, nil)", size, err)
    78  	}
    79  	for i := 0; i < 3; i++ {
    80  		if val, err := ls.Uint(); val != 1 || err != nil {
    81  			t.Errorf("Uint() returned (%d, %v), expected (1, nil)", val, err)
    82  		}
    83  	}
    84  	if err := ls.ListEnd(); err != nil {
    85  		t.Errorf("ListEnd() returned %v, expected (3, nil)", err)
    86  	}
    87  }
    88  
    89  func TestStreamErrors(t *testing.T) {
    90  	withoutInputLimit := func(b []byte) *Stream {
    91  		return NewStream(newPlainReader(b), 0)
    92  	}
    93  	withCustomInputLimit := func(limit uint64) func([]byte) *Stream {
    94  		return func(b []byte) *Stream {
    95  			return NewStream(bytes.NewReader(b), limit)
    96  		}
    97  	}
    98  
    99  	type calls []string
   100  	tests := []struct {
   101  		string
   102  		calls
   103  		newStream func([]byte) *Stream // uses bytes.Reader if nil
   104  		error     error
   105  	}{
   106  		{"C0", calls{"Bytes"}, nil, ErrExpectedString},
   107  		{"C0", calls{"Uint"}, nil, ErrExpectedString},
   108  		{"89000000000000000001", calls{"Uint"}, nil, errUintOverflow},
   109  		{"00", calls{"List"}, nil, ErrExpectedList},
   110  		{"80", calls{"List"}, nil, ErrExpectedList},
   111  		{"C0", calls{"List", "Uint"}, nil, EOL},
   112  		{"C8C9010101010101010101", calls{"List", "Kind"}, nil, ErrElemTooLarge},
   113  		{"C3C2010201", calls{"List", "List", "Uint", "Uint", "ListEnd", "Uint"}, nil, EOL},
   114  		{"00", calls{"ListEnd"}, nil, errNotInList},
   115  		{"C401020304", calls{"List", "Uint", "ListEnd"}, nil, errNotAtEOL},
   116  
   117  		// Non-canonical integers (e.g. leading zero bytes).
   118  		{"00", calls{"Uint"}, nil, ErrCanonInt},
   119  		{"820002", calls{"Uint"}, nil, ErrCanonInt},
   120  		{"8133", calls{"Uint"}, nil, ErrCanonSize},
   121  		{"817F", calls{"Uint"}, nil, ErrCanonSize},
   122  		{"8180", calls{"Uint"}, nil, nil},
   123  
   124  		// Non-valid boolean
   125  		{"02", calls{"Bool"}, nil, errors.New("rlp: invalid boolean value: 2")},
   126  
   127  		// Size tags must use the smallest possible encoding.
   128  		// Leading zero bytes in the size tag are also rejected.
   129  		{"8100", calls{"Uint"}, nil, ErrCanonSize},
   130  		{"8100", calls{"Bytes"}, nil, ErrCanonSize},
   131  		{"8101", calls{"Bytes"}, nil, ErrCanonSize},
   132  		{"817F", calls{"Bytes"}, nil, ErrCanonSize},
   133  		{"8180", calls{"Bytes"}, nil, nil},
   134  		{"B800", calls{"Kind"}, withoutInputLimit, ErrCanonSize},
   135  		{"B90000", calls{"Kind"}, withoutInputLimit, ErrCanonSize},
   136  		{"B90055", calls{"Kind"}, withoutInputLimit, ErrCanonSize},
   137  		{"BA0002FFFF", calls{"Bytes"}, withoutInputLimit, ErrCanonSize},
   138  		{"F800", calls{"Kind"}, withoutInputLimit, ErrCanonSize},
   139  		{"F90000", calls{"Kind"}, withoutInputLimit, ErrCanonSize},
   140  		{"F90055", calls{"Kind"}, withoutInputLimit, ErrCanonSize},
   141  		{"FA0002FFFF", calls{"List"}, withoutInputLimit, ErrCanonSize},
   142  
   143  		// Expected EOF
   144  		{"", calls{"Kind"}, nil, io.EOF},
   145  		{"", calls{"Uint"}, nil, io.EOF},
   146  		{"", calls{"List"}, nil, io.EOF},
   147  		{"8180", calls{"Uint", "Uint"}, nil, io.EOF},
   148  		{"C0", calls{"List", "ListEnd", "List"}, nil, io.EOF},
   149  
   150  		{"", calls{"List"}, withoutInputLimit, io.EOF},
   151  		{"8180", calls{"Uint", "Uint"}, withoutInputLimit, io.EOF},
   152  		{"C0", calls{"List", "ListEnd", "List"}, withoutInputLimit, io.EOF},
   153  
   154  		// Input limit errors.
   155  		{"81", calls{"Bytes"}, nil, ErrValueTooLarge},
   156  		{"81", calls{"Uint"}, nil, ErrValueTooLarge},
   157  		{"81", calls{"Raw"}, nil, ErrValueTooLarge},
   158  		{"BFFFFFFFFFFFFFFFFFFF", calls{"Bytes"}, nil, ErrValueTooLarge},
   159  		{"C801", calls{"List"}, nil, ErrValueTooLarge},
   160  
   161  		// Test for list element size check overflow.
   162  		{"CD04040404FFFFFFFFFFFFFFFFFF0303", calls{"List", "Uint", "Uint", "Uint", "Uint", "List"}, nil, ErrElemTooLarge},
   163  
   164  		// Test for input limit overflow. Since we are counting the limit
   165  		// down toward zero in Stream.remaining, reading too far can overflow
   166  		// remaining to a large value, effectively disabling the limit.
   167  		{"C40102030401", calls{"Raw", "Uint"}, withCustomInputLimit(5), io.EOF},
   168  		{"C4010203048180", calls{"Raw", "Uint"}, withCustomInputLimit(6), ErrValueTooLarge},
   169  
   170  		// Check that the same calls are fine without a limit.
   171  		{"C40102030401", calls{"Raw", "Uint"}, withoutInputLimit, nil},
   172  		{"C4010203048180", calls{"Raw", "Uint"}, withoutInputLimit, nil},
   173  
   174  		// Unexpected EOF. This only happens when there is
   175  		// no input limit, so the reader needs to be 'dumbed down'.
   176  		{"81", calls{"Bytes"}, withoutInputLimit, io.ErrUnexpectedEOF},
   177  		{"81", calls{"Uint"}, withoutInputLimit, io.ErrUnexpectedEOF},
   178  		{"BFFFFFFFFFFFFFFF", calls{"Bytes"}, withoutInputLimit, io.ErrUnexpectedEOF},
   179  		{"C801", calls{"List", "Uint", "Uint"}, withoutInputLimit, io.ErrUnexpectedEOF},
   180  
   181  		// This test verifies that the input position is advanced
   182  		// correctly when calling Bytes for empty strings. Kind can be called
   183  		// any number of times in between and doesn't advance.
   184  		{"C3808080", calls{
   185  			"List",  // enter the list
   186  			"Bytes", // past first element
   187  
   188  			"Kind", "Kind", "Kind", // this shouldn't advance
   189  
   190  			"Bytes", // past second element
   191  
   192  			"Kind", "Kind", // can't hurt to try
   193  
   194  			"Bytes", // past final element
   195  			"Bytes", // this one should fail
   196  		}, nil, EOL},
   197  	}
   198  
   199  testfor:
   200  	for i, test := range tests {
   201  		if test.newStream == nil {
   202  			test.newStream = func(b []byte) *Stream { return NewStream(bytes.NewReader(b), 0) }
   203  		}
   204  		s := test.newStream(unhex(test.string))
   205  		rs := reflect.ValueOf(s)
   206  		for j, call := range test.calls {
   207  			fval := rs.MethodByName(call)
   208  			ret := fval.Call(nil)
   209  			err := "<nil>"
   210  			if lastret := ret[len(ret)-1].Interface(); lastret != nil {
   211  				err = lastret.(error).Error()
   212  			}
   213  			if j == len(test.calls)-1 {
   214  				want := "<nil>"
   215  				if test.error != nil {
   216  					want = test.error.Error()
   217  				}
   218  				if err != want {
   219  					t.Log(test)
   220  					t.Errorf("test %d: last call (%s) error mismatch\ngot:  %s\nwant: %s",
   221  						i, call, err, test.error)
   222  				}
   223  			} else if err != "<nil>" {
   224  				t.Log(test)
   225  				t.Errorf("test %d: call %d (%s) unexpected error: %q", i, j, call, err)
   226  				continue testfor
   227  			}
   228  		}
   229  	}
   230  }
   231  
   232  func TestStreamList(t *testing.T) {
   233  	s := NewStream(bytes.NewReader(unhex("C80102030405060708")), 0)
   234  
   235  	len, err := s.List()
   236  	if err != nil {
   237  		t.Fatalf("List error: %v", err)
   238  	}
   239  	if len != 8 {
   240  		t.Fatalf("List returned invalid length, got %d, want 8", len)
   241  	}
   242  
   243  	for i := uint64(1); i <= 8; i++ {
   244  		v, err := s.Uint()
   245  		if err != nil {
   246  			t.Fatalf("Uint error: %v", err)
   247  		}
   248  		if i != v {
   249  			t.Errorf("Uint returned wrong value, got %d, want %d", v, i)
   250  		}
   251  	}
   252  
   253  	if _, err := s.Uint(); err != EOL {
   254  		t.Errorf("Uint error mismatch, got %v, want %v", err, EOL)
   255  	}
   256  	if err = s.ListEnd(); err != nil {
   257  		t.Fatalf("ListEnd error: %v", err)
   258  	}
   259  }
   260  
   261  func TestStreamRaw(t *testing.T) {
   262  	tests := []struct {
   263  		input  string
   264  		output string
   265  	}{
   266  		{
   267  			"C58401010101",
   268  			"8401010101",
   269  		},
   270  		{
   271  			"F842B84001010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101",
   272  			"B84001010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101",
   273  		},
   274  	}
   275  	for i, tt := range tests {
   276  		s := NewStream(bytes.NewReader(unhex(tt.input)), 0)
   277  		s.List()
   278  
   279  		want := unhex(tt.output)
   280  		raw, err := s.Raw()
   281  		if err != nil {
   282  			t.Fatal(err)
   283  		}
   284  		if !bytes.Equal(want, raw) {
   285  			t.Errorf("test %d: raw mismatch: got %x, want %x", i, raw, want)
   286  		}
   287  	}
   288  }
   289  
   290  func TestStreamReadBytes(t *testing.T) {
   291  	tests := []struct {
   292  		input string
   293  		size  int
   294  		err   string
   295  	}{
   296  		// kind List
   297  		{input: "C0", size: 1, err: "rlp: expected String or Byte"},
   298  		// kind Byte
   299  		{input: "04", size: 0, err: "input value has wrong size 1, want 0"},
   300  		{input: "04", size: 1},
   301  		{input: "04", size: 2, err: "input value has wrong size 1, want 2"},
   302  		// kind String
   303  		{input: "820102", size: 0, err: "input value has wrong size 2, want 0"},
   304  		{input: "820102", size: 1, err: "input value has wrong size 2, want 1"},
   305  		{input: "820102", size: 2},
   306  		{input: "820102", size: 3, err: "input value has wrong size 2, want 3"},
   307  	}
   308  
   309  	for _, test := range tests {
   310  		test := test
   311  		name := fmt.Sprintf("input_%s/size_%d", test.input, test.size)
   312  		t.Run(name, func(t *testing.T) {
   313  			s := NewStream(bytes.NewReader(unhex(test.input)), 0)
   314  			b := make([]byte, test.size)
   315  			err := s.ReadBytes(b)
   316  			if test.err == "" {
   317  				if err != nil {
   318  					t.Errorf("unexpected error %q", err)
   319  				}
   320  			} else {
   321  				if err == nil {
   322  					t.Errorf("expected error, got nil")
   323  				} else if err.Error() != test.err {
   324  					t.Errorf("wrong error %q", err)
   325  				}
   326  			}
   327  		})
   328  	}
   329  }
   330  
   331  func TestDecodeErrors(t *testing.T) {
   332  	r := bytes.NewReader(nil)
   333  
   334  	if err := Decode(r, nil); err != errDecodeIntoNil {
   335  		t.Errorf("Decode(r, nil) error mismatch, got %q, want %q", err, errDecodeIntoNil)
   336  	}
   337  
   338  	var nilptr *struct{}
   339  	if err := Decode(r, nilptr); err != errDecodeIntoNil {
   340  		t.Errorf("Decode(r, nilptr) error mismatch, got %q, want %q", err, errDecodeIntoNil)
   341  	}
   342  
   343  	if err := Decode(r, struct{}{}); err != errNoPointer {
   344  		t.Errorf("Decode(r, struct{}{}) error mismatch, got %q, want %q", err, errNoPointer)
   345  	}
   346  
   347  	expectErr := "rlp: type chan bool is not RLP-serializable"
   348  	if err := Decode(r, new(chan bool)); err == nil || err.Error() != expectErr {
   349  		t.Errorf("Decode(r, new(chan bool)) error mismatch, got %q, want %q", err, expectErr)
   350  	}
   351  
   352  	if err := Decode(r, new(uint)); err != io.EOF {
   353  		t.Errorf("Decode(r, new(int)) error mismatch, got %q, want %q", err, io.EOF)
   354  	}
   355  }
   356  
   357  type decodeTest struct {
   358  	input string
   359  	ptr   interface{}
   360  	value interface{}
   361  	error string
   362  }
   363  
   364  type simplestruct struct {
   365  	A uint
   366  	B string
   367  }
   368  
   369  type recstruct struct {
   370  	I     uint
   371  	Child *recstruct `rlp:"nil"`
   372  }
   373  
   374  type bigIntStruct struct {
   375  	I *big.Int
   376  	B string
   377  }
   378  
   379  type invalidNilTag struct {
   380  	X []byte `rlp:"nil"`
   381  }
   382  
   383  type invalidTail1 struct {
   384  	A uint `rlp:"tail"`
   385  	B string
   386  }
   387  
   388  type invalidTail2 struct {
   389  	A uint
   390  	B string `rlp:"tail"`
   391  }
   392  
   393  type tailRaw struct {
   394  	A    uint
   395  	Tail []RawValue `rlp:"tail"`
   396  }
   397  
   398  type tailUint struct {
   399  	A    uint
   400  	Tail []uint `rlp:"tail"`
   401  }
   402  
   403  type tailPrivateFields struct {
   404  	A    uint
   405  	Tail []uint `rlp:"tail"`
   406  	x, y bool   //lint:ignore U1000 unused fields required for testing purposes.
   407  }
   408  
   409  type nilListUint struct {
   410  	X *uint `rlp:"nilList"`
   411  }
   412  
   413  type nilStringSlice struct {
   414  	X *[]uint `rlp:"nilString"`
   415  }
   416  
   417  type intField struct {
   418  	X int
   419  }
   420  
   421  type optionalFields struct {
   422  	A uint
   423  	B uint `rlp:"optional"`
   424  	C uint `rlp:"optional"`
   425  }
   426  
   427  type optionalAndTailField struct {
   428  	A    uint
   429  	B    uint   `rlp:"optional"`
   430  	Tail []uint `rlp:"tail"`
   431  }
   432  
   433  type optionalBigIntField struct {
   434  	A uint
   435  	B *big.Int `rlp:"optional"`
   436  }
   437  
   438  type optionalPtrField struct {
   439  	A uint
   440  	B *[3]byte `rlp:"optional"`
   441  }
   442  
   443  type nonOptionalPtrField struct {
   444  	A uint
   445  	B *[3]byte
   446  }
   447  
   448  type multipleOptionalFields struct {
   449  	A *[3]byte `rlp:"optional"`
   450  	B *[3]byte `rlp:"optional"`
   451  }
   452  
   453  type optionalPtrFieldNil struct {
   454  	A uint
   455  	B *[3]byte `rlp:"optional,nil"`
   456  }
   457  
   458  type ignoredField struct {
   459  	A uint
   460  	B uint `rlp:"-"`
   461  	C uint
   462  }
   463  
   464  var (
   465  	veryBigInt = new(big.Int).Add(
   466  		new(big.Int).Lsh(big.NewInt(0xFFFFFFFFFFFFFF), 16),
   467  		big.NewInt(0xFFFF),
   468  	)
   469  	veryVeryBigInt = new(big.Int).Exp(veryBigInt, big.NewInt(8), nil)
   470  )
   471  
   472  var (
   473  	veryBigInt256, _ = uint256.FromBig(veryBigInt)
   474  )
   475  
   476  var decodeTests = []decodeTest{
   477  	// booleans
   478  	{input: "01", ptr: new(bool), value: true},
   479  	{input: "80", ptr: new(bool), value: false},
   480  	{input: "02", ptr: new(bool), error: "rlp: invalid boolean value: 2"},
   481  
   482  	// integers
   483  	{input: "05", ptr: new(uint32), value: uint32(5)},
   484  	{input: "80", ptr: new(uint32), value: uint32(0)},
   485  	{input: "820505", ptr: new(uint32), value: uint32(0x0505)},
   486  	{input: "83050505", ptr: new(uint32), value: uint32(0x050505)},
   487  	{input: "8405050505", ptr: new(uint32), value: uint32(0x05050505)},
   488  	{input: "850505050505", ptr: new(uint32), error: "rlp: input string too long for uint32"},
   489  	{input: "C0", ptr: new(uint32), error: "rlp: expected input string or byte for uint32"},
   490  	{input: "00", ptr: new(uint32), error: "rlp: non-canonical integer (leading zero bytes) for uint32"},
   491  	{input: "8105", ptr: new(uint32), error: "rlp: non-canonical size information for uint32"},
   492  	{input: "820004", ptr: new(uint32), error: "rlp: non-canonical integer (leading zero bytes) for uint32"},
   493  	{input: "B8020004", ptr: new(uint32), error: "rlp: non-canonical size information for uint32"},
   494  
   495  	// slices
   496  	{input: "C0", ptr: new([]uint), value: []uint{}},
   497  	{input: "C80102030405060708", ptr: new([]uint), value: []uint{1, 2, 3, 4, 5, 6, 7, 8}},
   498  	{input: "F8020004", ptr: new([]uint), error: "rlp: non-canonical size information for []uint"},
   499  
   500  	// arrays
   501  	{input: "C50102030405", ptr: new([5]uint), value: [5]uint{1, 2, 3, 4, 5}},
   502  	{input: "C0", ptr: new([5]uint), error: "rlp: input list has too few elements for [5]uint"},
   503  	{input: "C102", ptr: new([5]uint), error: "rlp: input list has too few elements for [5]uint"},
   504  	{input: "C6010203040506", ptr: new([5]uint), error: "rlp: input list has too many elements for [5]uint"},
   505  	{input: "F8020004", ptr: new([5]uint), error: "rlp: non-canonical size information for [5]uint"},
   506  
   507  	// zero sized arrays
   508  	{input: "C0", ptr: new([0]uint), value: [0]uint{}},
   509  	{input: "C101", ptr: new([0]uint), error: "rlp: input list has too many elements for [0]uint"},
   510  
   511  	// byte slices
   512  	{input: "01", ptr: new([]byte), value: []byte{1}},
   513  	{input: "80", ptr: new([]byte), value: []byte{}},
   514  	{input: "8D6162636465666768696A6B6C6D", ptr: new([]byte), value: []byte("abcdefghijklm")},
   515  	{input: "C0", ptr: new([]byte), error: "rlp: expected input string or byte for []uint8"},
   516  	{input: "8105", ptr: new([]byte), error: "rlp: non-canonical size information for []uint8"},
   517  
   518  	// byte arrays
   519  	{input: "02", ptr: new([1]byte), value: [1]byte{2}},
   520  	{input: "8180", ptr: new([1]byte), value: [1]byte{128}},
   521  	{input: "850102030405", ptr: new([5]byte), value: [5]byte{1, 2, 3, 4, 5}},
   522  
   523  	// byte array errors
   524  	{input: "02", ptr: new([5]byte), error: "rlp: input string too short for [5]uint8"},
   525  	{input: "80", ptr: new([5]byte), error: "rlp: input string too short for [5]uint8"},
   526  	{input: "820000", ptr: new([5]byte), error: "rlp: input string too short for [5]uint8"},
   527  	{input: "C0", ptr: new([5]byte), error: "rlp: expected input string or byte for [5]uint8"},
   528  	{input: "C3010203", ptr: new([5]byte), error: "rlp: expected input string or byte for [5]uint8"},
   529  	{input: "86010203040506", ptr: new([5]byte), error: "rlp: input string too long for [5]uint8"},
   530  	{input: "8105", ptr: new([1]byte), error: "rlp: non-canonical size information for [1]uint8"},
   531  	{input: "817F", ptr: new([1]byte), error: "rlp: non-canonical size information for [1]uint8"},
   532  
   533  	// zero sized byte arrays
   534  	{input: "80", ptr: new([0]byte), value: [0]byte{}},
   535  	{input: "01", ptr: new([0]byte), error: "rlp: input string too long for [0]uint8"},
   536  	{input: "8101", ptr: new([0]byte), error: "rlp: input string too long for [0]uint8"},
   537  
   538  	// strings
   539  	{input: "00", ptr: new(string), value: "\000"},
   540  	{input: "8D6162636465666768696A6B6C6D", ptr: new(string), value: "abcdefghijklm"},
   541  	{input: "C0", ptr: new(string), error: "rlp: expected input string or byte for string"},
   542  
   543  	// big ints
   544  	{input: "80", ptr: new(*big.Int), value: big.NewInt(0)},
   545  	{input: "01", ptr: new(*big.Int), value: big.NewInt(1)},
   546  	{input: "89FFFFFFFFFFFFFFFFFF", ptr: new(*big.Int), value: veryBigInt},
   547  	{input: "B848FFFFFFFFFFFFFFFFF800000000000000001BFFFFFFFFFFFFFFFFC8000000000000000045FFFFFFFFFFFFFFFFC800000000000000001BFFFFFFFFFFFFFFFFF8000000000000000001", ptr: new(*big.Int), value: veryVeryBigInt},
   548  	{input: "10", ptr: new(big.Int), value: *big.NewInt(16)}, // non-pointer also works
   549  
   550  	// big int errors
   551  	{input: "C0", ptr: new(*big.Int), error: "rlp: expected input string or byte for *big.Int"},
   552  	{input: "00", ptr: new(*big.Int), error: "rlp: non-canonical integer (leading zero bytes) for *big.Int"},
   553  	{input: "820001", ptr: new(*big.Int), error: "rlp: non-canonical integer (leading zero bytes) for *big.Int"},
   554  	{input: "8105", ptr: new(*big.Int), error: "rlp: non-canonical size information for *big.Int"},
   555  
   556  	// uint256
   557  	{input: "80", ptr: new(*uint256.Int), value: uint256.NewInt(0)},
   558  	{input: "01", ptr: new(*uint256.Int), value: uint256.NewInt(1)},
   559  	{input: "88FFFFFFFFFFFFFFFF", ptr: new(*uint256.Int), value: uint256.NewInt(math.MaxUint64)},
   560  	{input: "89FFFFFFFFFFFFFFFFFF", ptr: new(*uint256.Int), value: veryBigInt256},
   561  	{input: "10", ptr: new(uint256.Int), value: *uint256.NewInt(16)}, // non-pointer also works
   562  
   563  	// uint256 errors
   564  	{input: "C0", ptr: new(*uint256.Int), error: "rlp: expected input string or byte for *uint256.Int"},
   565  	{input: "00", ptr: new(*uint256.Int), error: "rlp: non-canonical integer (leading zero bytes) for *uint256.Int"},
   566  	{input: "820001", ptr: new(*uint256.Int), error: "rlp: non-canonical integer (leading zero bytes) for *uint256.Int"},
   567  	{input: "8105", ptr: new(*uint256.Int), error: "rlp: non-canonical size information for *uint256.Int"},
   568  	{input: "A1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00", ptr: new(*uint256.Int), error: "rlp: value too large for uint256"},
   569  
   570  	// structs
   571  	{
   572  		input: "C50583343434",
   573  		ptr:   new(simplestruct),
   574  		value: simplestruct{5, "444"},
   575  	},
   576  	{
   577  		input: "C601C402C203C0",
   578  		ptr:   new(recstruct),
   579  		value: recstruct{1, &recstruct{2, &recstruct{3, nil}}},
   580  	},
   581  	{
   582  		// This checks that empty big.Int works correctly in struct context. It's easy to
   583  		// miss the update of s.kind for this case, so it needs its own test.
   584  		input: "C58083343434",
   585  		ptr:   new(bigIntStruct),
   586  		value: bigIntStruct{new(big.Int), "444"},
   587  	},
   588  
   589  	// struct errors
   590  	{
   591  		input: "C0",
   592  		ptr:   new(simplestruct),
   593  		error: "rlp: too few elements for rlp.simplestruct",
   594  	},
   595  	{
   596  		input: "C105",
   597  		ptr:   new(simplestruct),
   598  		error: "rlp: too few elements for rlp.simplestruct",
   599  	},
   600  	{
   601  		input: "C7C50583343434C0",
   602  		ptr:   new([]*simplestruct),
   603  		error: "rlp: too few elements for rlp.simplestruct, decoding into ([]*rlp.simplestruct)[1]",
   604  	},
   605  	{
   606  		input: "83222222",
   607  		ptr:   new(simplestruct),
   608  		error: "rlp: expected input list for rlp.simplestruct",
   609  	},
   610  	{
   611  		input: "C3010101",
   612  		ptr:   new(simplestruct),
   613  		error: "rlp: input list has too many elements for rlp.simplestruct",
   614  	},
   615  	{
   616  		input: "C501C3C00000",
   617  		ptr:   new(recstruct),
   618  		error: "rlp: expected input string or byte for uint, decoding into (rlp.recstruct).Child.I",
   619  	},
   620  	{
   621  		input: "C103",
   622  		ptr:   new(intField),
   623  		error: "rlp: type int is not RLP-serializable (struct field rlp.intField.X)",
   624  	},
   625  	{
   626  		input: "C50102C20102",
   627  		ptr:   new(tailUint),
   628  		error: "rlp: expected input string or byte for uint, decoding into (rlp.tailUint).Tail[1]",
   629  	},
   630  	{
   631  		input: "C0",
   632  		ptr:   new(invalidNilTag),
   633  		error: `rlp: invalid struct tag "nil" for rlp.invalidNilTag.X (field is not a pointer)`,
   634  	},
   635  
   636  	// struct tag "tail"
   637  	{
   638  		input: "C3010203",
   639  		ptr:   new(tailRaw),
   640  		value: tailRaw{A: 1, Tail: []RawValue{unhex("02"), unhex("03")}},
   641  	},
   642  	{
   643  		input: "C20102",
   644  		ptr:   new(tailRaw),
   645  		value: tailRaw{A: 1, Tail: []RawValue{unhex("02")}},
   646  	},
   647  	{
   648  		input: "C101",
   649  		ptr:   new(tailRaw),
   650  		value: tailRaw{A: 1, Tail: []RawValue{}},
   651  	},
   652  	{
   653  		input: "C3010203",
   654  		ptr:   new(tailPrivateFields),
   655  		value: tailPrivateFields{A: 1, Tail: []uint{2, 3}},
   656  	},
   657  	{
   658  		input: "C0",
   659  		ptr:   new(invalidTail1),
   660  		error: `rlp: invalid struct tag "tail" for rlp.invalidTail1.A (must be on last field)`,
   661  	},
   662  	{
   663  		input: "C0",
   664  		ptr:   new(invalidTail2),
   665  		error: `rlp: invalid struct tag "tail" for rlp.invalidTail2.B (field type is not slice)`,
   666  	},
   667  
   668  	// struct tag "-"
   669  	{
   670  		input: "C20102",
   671  		ptr:   new(ignoredField),
   672  		value: ignoredField{A: 1, C: 2},
   673  	},
   674  
   675  	// struct tag "nilList"
   676  	{
   677  		input: "C180",
   678  		ptr:   new(nilListUint),
   679  		error: "rlp: wrong kind of empty value (got String, want List) for *uint, decoding into (rlp.nilListUint).X",
   680  	},
   681  	{
   682  		input: "C1C0",
   683  		ptr:   new(nilListUint),
   684  		value: nilListUint{},
   685  	},
   686  	{
   687  		input: "C103",
   688  		ptr:   new(nilListUint),
   689  		value: func() interface{} {
   690  			v := uint(3)
   691  			return nilListUint{X: &v}
   692  		}(),
   693  	},
   694  
   695  	// struct tag "nilString"
   696  	{
   697  		input: "C1C0",
   698  		ptr:   new(nilStringSlice),
   699  		error: "rlp: wrong kind of empty value (got List, want String) for *[]uint, decoding into (rlp.nilStringSlice).X",
   700  	},
   701  	{
   702  		input: "C180",
   703  		ptr:   new(nilStringSlice),
   704  		value: nilStringSlice{},
   705  	},
   706  	{
   707  		input: "C2C103",
   708  		ptr:   new(nilStringSlice),
   709  		value: nilStringSlice{X: &[]uint{3}},
   710  	},
   711  
   712  	// struct tag "optional"
   713  	{
   714  		input: "C101",
   715  		ptr:   new(optionalFields),
   716  		value: optionalFields{1, 0, 0},
   717  	},
   718  	{
   719  		input: "C20102",
   720  		ptr:   new(optionalFields),
   721  		value: optionalFields{1, 2, 0},
   722  	},
   723  	{
   724  		input: "C3010203",
   725  		ptr:   new(optionalFields),
   726  		value: optionalFields{1, 2, 3},
   727  	},
   728  	{
   729  		input: "C401020304",
   730  		ptr:   new(optionalFields),
   731  		error: "rlp: input list has too many elements for rlp.optionalFields",
   732  	},
   733  	{
   734  		input: "C101",
   735  		ptr:   new(optionalAndTailField),
   736  		value: optionalAndTailField{A: 1},
   737  	},
   738  	{
   739  		input: "C20102",
   740  		ptr:   new(optionalAndTailField),
   741  		value: optionalAndTailField{A: 1, B: 2, Tail: []uint{}},
   742  	},
   743  	{
   744  		input: "C401020304",
   745  		ptr:   new(optionalAndTailField),
   746  		value: optionalAndTailField{A: 1, B: 2, Tail: []uint{3, 4}},
   747  	},
   748  	{
   749  		input: "C101",
   750  		ptr:   new(optionalBigIntField),
   751  		value: optionalBigIntField{A: 1, B: nil},
   752  	},
   753  	{
   754  		input: "C20102",
   755  		ptr:   new(optionalBigIntField),
   756  		value: optionalBigIntField{A: 1, B: big.NewInt(2)},
   757  	},
   758  	{
   759  		input: "C101",
   760  		ptr:   new(optionalPtrField),
   761  		value: optionalPtrField{A: 1},
   762  	},
   763  	{
   764  		input: "C20180", // not accepted because "optional" doesn't enable "nil"
   765  		ptr:   new(optionalPtrField),
   766  		error: "rlp: input string too short for [3]uint8, decoding into (rlp.optionalPtrField).B",
   767  	},
   768  	{
   769  		input: "C20102",
   770  		ptr:   new(optionalPtrField),
   771  		error: "rlp: input string too short for [3]uint8, decoding into (rlp.optionalPtrField).B",
   772  	},
   773  	{
   774  		input: "C50183010203",
   775  		ptr:   new(optionalPtrField),
   776  		value: optionalPtrField{A: 1, B: &[3]byte{1, 2, 3}},
   777  	},
   778  	{
   779  		// all optional fields nil
   780  		input: "C0",
   781  		ptr:   new(multipleOptionalFields),
   782  		value: multipleOptionalFields{A: nil, B: nil},
   783  	},
   784  	{
   785  		// all optional fields set
   786  		input: "C88301020383010203",
   787  		ptr:   new(multipleOptionalFields),
   788  		value: multipleOptionalFields{A: &[3]byte{1, 2, 3}, B: &[3]byte{1, 2, 3}},
   789  	},
   790  	{
   791  		// nil optional field appears before a non-nil one
   792  		input: "C58083010203",
   793  		ptr:   new(multipleOptionalFields),
   794  		error: "rlp: input string too short for [3]uint8, decoding into (rlp.multipleOptionalFields).A",
   795  	},
   796  	{
   797  		// decode a nil ptr into a ptr that is not nil or not optional
   798  		input: "C20180",
   799  		ptr:   new(nonOptionalPtrField),
   800  		error: "rlp: input string too short for [3]uint8, decoding into (rlp.nonOptionalPtrField).B",
   801  	},
   802  	{
   803  		input: "C101",
   804  		ptr:   new(optionalPtrFieldNil),
   805  		value: optionalPtrFieldNil{A: 1},
   806  	},
   807  	{
   808  		input: "C20180", // accepted because "nil" tag allows empty input
   809  		ptr:   new(optionalPtrFieldNil),
   810  		value: optionalPtrFieldNil{A: 1},
   811  	},
   812  	{
   813  		input: "C20102",
   814  		ptr:   new(optionalPtrFieldNil),
   815  		error: "rlp: input string too short for [3]uint8, decoding into (rlp.optionalPtrFieldNil).B",
   816  	},
   817  
   818  	// struct tag "optional" field clearing
   819  	{
   820  		input: "C101",
   821  		ptr:   &optionalFields{A: 9, B: 8, C: 7},
   822  		value: optionalFields{A: 1, B: 0, C: 0},
   823  	},
   824  	{
   825  		input: "C20102",
   826  		ptr:   &optionalFields{A: 9, B: 8, C: 7},
   827  		value: optionalFields{A: 1, B: 2, C: 0},
   828  	},
   829  	{
   830  		input: "C20102",
   831  		ptr:   &optionalAndTailField{A: 9, B: 8, Tail: []uint{7, 6, 5}},
   832  		value: optionalAndTailField{A: 1, B: 2, Tail: []uint{}},
   833  	},
   834  	{
   835  		input: "C101",
   836  		ptr:   &optionalPtrField{A: 9, B: &[3]byte{8, 7, 6}},
   837  		value: optionalPtrField{A: 1},
   838  	},
   839  
   840  	// RawValue
   841  	{input: "01", ptr: new(RawValue), value: RawValue(unhex("01"))},
   842  	{input: "82FFFF", ptr: new(RawValue), value: RawValue(unhex("82FFFF"))},
   843  	{input: "C20102", ptr: new([]RawValue), value: []RawValue{unhex("01"), unhex("02")}},
   844  
   845  	// pointers
   846  	{input: "00", ptr: new(*[]byte), value: &[]byte{0}},
   847  	{input: "80", ptr: new(*uint), value: uintp(0)},
   848  	{input: "C0", ptr: new(*uint), error: "rlp: expected input string or byte for uint"},
   849  	{input: "07", ptr: new(*uint), value: uintp(7)},
   850  	{input: "817F", ptr: new(*uint), error: "rlp: non-canonical size information for uint"},
   851  	{input: "8180", ptr: new(*uint), value: uintp(0x80)},
   852  	{input: "C109", ptr: new(*[]uint), value: &[]uint{9}},
   853  	{input: "C58403030303", ptr: new(*[][]byte), value: &[][]byte{{3, 3, 3, 3}}},
   854  
   855  	// check that input position is advanced also for empty values.
   856  	{input: "C3808005", ptr: new([]*uint), value: []*uint{uintp(0), uintp(0), uintp(5)}},
   857  
   858  	// interface{}
   859  	{input: "00", ptr: new(interface{}), value: []byte{0}},
   860  	{input: "01", ptr: new(interface{}), value: []byte{1}},
   861  	{input: "80", ptr: new(interface{}), value: []byte{}},
   862  	{input: "850505050505", ptr: new(interface{}), value: []byte{5, 5, 5, 5, 5}},
   863  	{input: "C0", ptr: new(interface{}), value: []interface{}{}},
   864  	{input: "C50183040404", ptr: new(interface{}), value: []interface{}{[]byte{1}, []byte{4, 4, 4}}},
   865  	{
   866  		input: "C3010203",
   867  		ptr:   new([]io.Reader),
   868  		error: "rlp: type io.Reader is not RLP-serializable",
   869  	},
   870  
   871  	// fuzzer crashes
   872  	{
   873  		input: "c330f9c030f93030ce3030303030303030bd303030303030",
   874  		ptr:   new(interface{}),
   875  		error: "rlp: element is larger than containing list",
   876  	},
   877  }
   878  
   879  func uintp(i uint) *uint { return &i }
   880  
   881  func runTests(t *testing.T, decode func([]byte, interface{}) error) {
   882  	for i, test := range decodeTests {
   883  		input, err := hex.DecodeString(test.input)
   884  		if err != nil {
   885  			t.Errorf("test %d: invalid hex input %q", i, test.input)
   886  			continue
   887  		}
   888  		err = decode(input, test.ptr)
   889  		if err != nil && test.error == "" {
   890  			t.Errorf("test %d: unexpected Decode error: %v\ndecoding into %T\ninput %q",
   891  				i, err, test.ptr, test.input)
   892  			continue
   893  		}
   894  		if test.error != "" && fmt.Sprint(err) != test.error {
   895  			t.Errorf("test %d: Decode error mismatch\ngot  %v\nwant %v\ndecoding into %T\ninput %q",
   896  				i, err, test.error, test.ptr, test.input)
   897  			continue
   898  		}
   899  		deref := reflect.ValueOf(test.ptr).Elem().Interface()
   900  		if err == nil && !reflect.DeepEqual(deref, test.value) {
   901  			t.Errorf("test %d: value mismatch\ngot  %#v\nwant %#v\ndecoding into %T\ninput %q",
   902  				i, deref, test.value, test.ptr, test.input)
   903  		}
   904  	}
   905  }
   906  
   907  func TestDecodeWithByteReader(t *testing.T) {
   908  	runTests(t, func(input []byte, into interface{}) error {
   909  		return Decode(bytes.NewReader(input), into)
   910  	})
   911  }
   912  
   913  func testDecodeWithEncReader(t *testing.T, n int) {
   914  	s := strings.Repeat("0", n)
   915  	_, r, _ := EncodeToReader(s)
   916  	var decoded string
   917  	err := Decode(r, &decoded)
   918  	if err != nil {
   919  		t.Errorf("Unexpected decode error with n=%v: %v", n, err)
   920  	}
   921  	if decoded != s {
   922  		t.Errorf("Decode mismatch with n=%v", n)
   923  	}
   924  }
   925  
   926  // This is a regression test checking that decoding from encReader
   927  // works for RLP values of size 8192 bytes or more.
   928  func TestDecodeWithEncReader(t *testing.T) {
   929  	testDecodeWithEncReader(t, 8188) // length with header is 8191
   930  	testDecodeWithEncReader(t, 8189) // length with header is 8192
   931  }
   932  
   933  // plainReader reads from a byte slice but does not
   934  // implement ReadByte. It is also not recognized by the
   935  // size validation. This is useful to test how the decoder
   936  // behaves on a non-buffered input stream.
   937  type plainReader []byte
   938  
   939  func newPlainReader(b []byte) io.Reader {
   940  	return (*plainReader)(&b)
   941  }
   942  
   943  func (r *plainReader) Read(buf []byte) (n int, err error) {
   944  	if len(*r) == 0 {
   945  		return 0, io.EOF
   946  	}
   947  	n = copy(buf, *r)
   948  	*r = (*r)[n:]
   949  	return n, nil
   950  }
   951  
   952  func TestDecodeWithNonByteReader(t *testing.T) {
   953  	runTests(t, func(input []byte, into interface{}) error {
   954  		return Decode(newPlainReader(input), into)
   955  	})
   956  }
   957  
   958  func TestDecodeStreamReset(t *testing.T) {
   959  	s := NewStream(nil, 0)
   960  	runTests(t, func(input []byte, into interface{}) error {
   961  		s.Reset(bytes.NewReader(input), 0)
   962  		return s.Decode(into)
   963  	})
   964  }
   965  
   966  type testDecoder struct{ called bool }
   967  
   968  func (t *testDecoder) DecodeRLP(s *Stream) error {
   969  	if _, err := s.Uint(); err != nil {
   970  		return err
   971  	}
   972  	t.called = true
   973  	return nil
   974  }
   975  
   976  func TestDecodeDecoder(t *testing.T) {
   977  	var s struct {
   978  		T1 testDecoder
   979  		T2 *testDecoder
   980  		T3 **testDecoder
   981  	}
   982  	if err := Decode(bytes.NewReader(unhex("C3010203")), &s); err != nil {
   983  		t.Fatalf("Decode error: %v", err)
   984  	}
   985  
   986  	if !s.T1.called {
   987  		t.Errorf("DecodeRLP was not called for (non-pointer) testDecoder")
   988  	}
   989  
   990  	if s.T2 == nil {
   991  		t.Errorf("*testDecoder has not been allocated")
   992  	} else if !s.T2.called {
   993  		t.Errorf("DecodeRLP was not called for *testDecoder")
   994  	}
   995  
   996  	if s.T3 == nil || *s.T3 == nil {
   997  		t.Errorf("**testDecoder has not been allocated")
   998  	} else if !(*s.T3).called {
   999  		t.Errorf("DecodeRLP was not called for **testDecoder")
  1000  	}
  1001  }
  1002  
  1003  func TestDecodeDecoderNilPointer(t *testing.T) {
  1004  	var s struct {
  1005  		T1 *testDecoder `rlp:"nil"`
  1006  		T2 *testDecoder
  1007  	}
  1008  	if err := Decode(bytes.NewReader(unhex("C2C002")), &s); err != nil {
  1009  		t.Fatalf("Decode error: %v", err)
  1010  	}
  1011  	if s.T1 != nil {
  1012  		t.Errorf("decoder T1 allocated for empty input (called: %v)", s.T1.called)
  1013  	}
  1014  	if s.T2 == nil || !s.T2.called {
  1015  		t.Errorf("decoder T2 not allocated/called")
  1016  	}
  1017  }
  1018  
  1019  type byteDecoder byte
  1020  
  1021  func (bd *byteDecoder) DecodeRLP(s *Stream) error {
  1022  	_, err := s.Uint()
  1023  	*bd = 255
  1024  	return err
  1025  }
  1026  
  1027  func (bd byteDecoder) called() bool {
  1028  	return bd == 255
  1029  }
  1030  
  1031  // This test verifies that the byte slice/byte array logic
  1032  // does not kick in for element types implementing Decoder.
  1033  func TestDecoderInByteSlice(t *testing.T) {
  1034  	var slice []byteDecoder
  1035  	if err := Decode(bytes.NewReader(unhex("C101")), &slice); err != nil {
  1036  		t.Errorf("unexpected Decode error %v", err)
  1037  	} else if !slice[0].called() {
  1038  		t.Errorf("DecodeRLP not called for slice element")
  1039  	}
  1040  
  1041  	var array [1]byteDecoder
  1042  	if err := Decode(bytes.NewReader(unhex("C101")), &array); err != nil {
  1043  		t.Errorf("unexpected Decode error %v", err)
  1044  	} else if !array[0].called() {
  1045  		t.Errorf("DecodeRLP not called for array element")
  1046  	}
  1047  }
  1048  
  1049  type unencodableDecoder func()
  1050  
  1051  func (f *unencodableDecoder) DecodeRLP(s *Stream) error {
  1052  	if _, err := s.List(); err != nil {
  1053  		return err
  1054  	}
  1055  	if err := s.ListEnd(); err != nil {
  1056  		return err
  1057  	}
  1058  	*f = func() {}
  1059  	return nil
  1060  }
  1061  
  1062  func TestDecoderFunc(t *testing.T) {
  1063  	var x func()
  1064  	if err := DecodeBytes([]byte{0xC0}, (*unencodableDecoder)(&x)); err != nil {
  1065  		t.Fatal(err)
  1066  	}
  1067  	x()
  1068  }
  1069  
  1070  // This tests the validity checks for fields with struct tag "optional".
  1071  func TestInvalidOptionalField(t *testing.T) {
  1072  	type (
  1073  		invalid1 struct {
  1074  			A uint `rlp:"optional"`
  1075  			B uint
  1076  		}
  1077  		invalid2 struct {
  1078  			T []uint `rlp:"tail,optional"`
  1079  		}
  1080  		invalid3 struct {
  1081  			T []uint `rlp:"optional,tail"`
  1082  		}
  1083  	)
  1084  
  1085  	tests := []struct {
  1086  		v   interface{}
  1087  		err string
  1088  	}{
  1089  		{v: new(invalid1), err: `rlp: invalid struct tag "" for rlp.invalid1.B (must be optional because preceding field "A" is optional)`},
  1090  		{v: new(invalid2), err: `rlp: invalid struct tag "optional" for rlp.invalid2.T (also has "tail" tag)`},
  1091  		{v: new(invalid3), err: `rlp: invalid struct tag "tail" for rlp.invalid3.T (also has "optional" tag)`},
  1092  	}
  1093  	for _, test := range tests {
  1094  		err := DecodeBytes(unhex("C20102"), test.v)
  1095  		if err == nil {
  1096  			t.Errorf("no error for %T", test.v)
  1097  		} else if err.Error() != test.err {
  1098  			t.Errorf("wrong error for %T: %v", test.v, err.Error())
  1099  		}
  1100  	}
  1101  }
  1102  
  1103  func ExampleDecode() {
  1104  	input, _ := hex.DecodeString("C90A1486666F6F626172")
  1105  
  1106  	type example struct {
  1107  		A, B   uint
  1108  		String string
  1109  	}
  1110  
  1111  	var s example
  1112  	err := Decode(bytes.NewReader(input), &s)
  1113  	if err != nil {
  1114  		fmt.Printf("Error: %v\n", err)
  1115  	} else {
  1116  		fmt.Printf("Decoded value: %#v\n", s)
  1117  	}
  1118  	// Output:
  1119  	// Decoded value: rlp.example{A:0xa, B:0x14, String:"foobar"}
  1120  }
  1121  
  1122  func ExampleDecode_structTagNil() {
  1123  	// In this example, we'll use the "nil" struct tag to change
  1124  	// how a pointer-typed field is decoded. The input contains an RLP
  1125  	// list of one element, an empty string.
  1126  	input := []byte{0xC1, 0x80}
  1127  
  1128  	// This type uses the normal rules.
  1129  	// The empty input string is decoded as a pointer to an empty Go string.
  1130  	var normalRules struct {
  1131  		String *string
  1132  	}
  1133  	Decode(bytes.NewReader(input), &normalRules)
  1134  	fmt.Printf("normal: String = %q\n", *normalRules.String)
  1135  
  1136  	// This type uses the struct tag.
  1137  	// The empty input string is decoded as a nil pointer.
  1138  	var withEmptyOK struct {
  1139  		String *string `rlp:"nil"`
  1140  	}
  1141  	Decode(bytes.NewReader(input), &withEmptyOK)
  1142  	fmt.Printf("with nil tag: String = %v\n", withEmptyOK.String)
  1143  
  1144  	// Output:
  1145  	// normal: String = ""
  1146  	// with nil tag: String = <nil>
  1147  }
  1148  
  1149  func ExampleStream() {
  1150  	input, _ := hex.DecodeString("C90A1486666F6F626172")
  1151  	s := NewStream(bytes.NewReader(input), 0)
  1152  
  1153  	// Check what kind of value lies ahead
  1154  	kind, size, _ := s.Kind()
  1155  	fmt.Printf("Kind: %v size:%d\n", kind, size)
  1156  
  1157  	// Enter the list
  1158  	if _, err := s.List(); err != nil {
  1159  		fmt.Printf("List error: %v\n", err)
  1160  		return
  1161  	}
  1162  
  1163  	// Decode elements
  1164  	fmt.Println(s.Uint())
  1165  	fmt.Println(s.Uint())
  1166  	fmt.Println(s.Bytes())
  1167  
  1168  	// Acknowledge end of list
  1169  	if err := s.ListEnd(); err != nil {
  1170  		fmt.Printf("ListEnd error: %v\n", err)
  1171  	}
  1172  	// Output:
  1173  	// Kind: List size:9
  1174  	// 10 <nil>
  1175  	// 20 <nil>
  1176  	// [102 111 111 98 97 114] <nil>
  1177  }
  1178  
  1179  func BenchmarkDecodeUints(b *testing.B) {
  1180  	enc := encodeTestSlice(90000)
  1181  	b.SetBytes(int64(len(enc)))
  1182  	b.ReportAllocs()
  1183  	b.ResetTimer()
  1184  
  1185  	for i := 0; i < b.N; i++ {
  1186  		var s []uint
  1187  		r := bytes.NewReader(enc)
  1188  		if err := Decode(r, &s); err != nil {
  1189  			b.Fatalf("Decode error: %v", err)
  1190  		}
  1191  	}
  1192  }
  1193  
  1194  func BenchmarkDecodeUintsReused(b *testing.B) {
  1195  	enc := encodeTestSlice(100000)
  1196  	b.SetBytes(int64(len(enc)))
  1197  	b.ReportAllocs()
  1198  	b.ResetTimer()
  1199  
  1200  	var s []uint
  1201  	for i := 0; i < b.N; i++ {
  1202  		r := bytes.NewReader(enc)
  1203  		if err := Decode(r, &s); err != nil {
  1204  			b.Fatalf("Decode error: %v", err)
  1205  		}
  1206  	}
  1207  }
  1208  
  1209  func BenchmarkDecodeByteArrayStruct(b *testing.B) {
  1210  	enc, err := EncodeToBytes(&byteArrayStruct{})
  1211  	if err != nil {
  1212  		b.Fatal(err)
  1213  	}
  1214  	b.SetBytes(int64(len(enc)))
  1215  	b.ReportAllocs()
  1216  	b.ResetTimer()
  1217  
  1218  	var out byteArrayStruct
  1219  	for i := 0; i < b.N; i++ {
  1220  		if err := DecodeBytes(enc, &out); err != nil {
  1221  			b.Fatal(err)
  1222  		}
  1223  	}
  1224  }
  1225  
  1226  func BenchmarkDecodeBigInts(b *testing.B) {
  1227  	ints := make([]*big.Int, 200)
  1228  	for i := range ints {
  1229  		ints[i] = math.BigPow(2, int64(i))
  1230  	}
  1231  	enc, err := EncodeToBytes(ints)
  1232  	if err != nil {
  1233  		b.Fatal(err)
  1234  	}
  1235  	b.SetBytes(int64(len(enc)))
  1236  	b.ReportAllocs()
  1237  	b.ResetTimer()
  1238  
  1239  	var out []*big.Int
  1240  	for i := 0; i < b.N; i++ {
  1241  		if err := DecodeBytes(enc, &out); err != nil {
  1242  			b.Fatal(err)
  1243  		}
  1244  	}
  1245  }
  1246  
  1247  func BenchmarkDecodeU256Ints(b *testing.B) {
  1248  	ints := make([]*uint256.Int, 200)
  1249  	for i := range ints {
  1250  		ints[i], _ = uint256.FromBig(math.BigPow(2, int64(i)))
  1251  	}
  1252  	enc, err := EncodeToBytes(ints)
  1253  	if err != nil {
  1254  		b.Fatal(err)
  1255  	}
  1256  	b.SetBytes(int64(len(enc)))
  1257  	b.ReportAllocs()
  1258  	b.ResetTimer()
  1259  
  1260  	var out []*uint256.Int
  1261  	for i := 0; i < b.N; i++ {
  1262  		if err := DecodeBytes(enc, &out); err != nil {
  1263  			b.Fatal(err)
  1264  		}
  1265  	}
  1266  }
  1267  
  1268  func encodeTestSlice(n uint) []byte {
  1269  	s := make([]uint, n)
  1270  	for i := uint(0); i < n; i++ {
  1271  		s[i] = i
  1272  	}
  1273  	b, err := EncodeToBytes(s)
  1274  	if err != nil {
  1275  		panic(fmt.Sprintf("encode error: %v", err))
  1276  	}
  1277  	return b
  1278  }
  1279  
  1280  func unhex(str string) []byte {
  1281  	b, err := hex.DecodeString(strings.ReplaceAll(str, " ", ""))
  1282  	if err != nil {
  1283  		panic(fmt.Sprintf("invalid hex string: %q", str))
  1284  	}
  1285  	return b
  1286  }