github.com/amazechain/amc@v0.1.3/internal/avm/rlp/decode_test.go (about)

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