github.com/jimmyx0x/go-ethereum@v1.10.28/rpc/types_test.go (about)

     1  // Copyright 2015 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 rpc
    18  
    19  import (
    20  	"encoding/json"
    21  	"reflect"
    22  	"testing"
    23  
    24  	"github.com/ethereum/go-ethereum/common"
    25  	"github.com/ethereum/go-ethereum/common/math"
    26  )
    27  
    28  func TestBlockNumberJSONUnmarshal(t *testing.T) {
    29  	tests := []struct {
    30  		input    string
    31  		mustFail bool
    32  		expected BlockNumber
    33  	}{
    34  		0:  {`"0x"`, true, BlockNumber(0)},
    35  		1:  {`"0x0"`, false, BlockNumber(0)},
    36  		2:  {`"0X1"`, false, BlockNumber(1)},
    37  		3:  {`"0x00"`, true, BlockNumber(0)},
    38  		4:  {`"0x01"`, true, BlockNumber(0)},
    39  		5:  {`"0x1"`, false, BlockNumber(1)},
    40  		6:  {`"0x12"`, false, BlockNumber(18)},
    41  		7:  {`"0x7fffffffffffffff"`, false, BlockNumber(math.MaxInt64)},
    42  		8:  {`"0x8000000000000000"`, true, BlockNumber(0)},
    43  		9:  {"0", true, BlockNumber(0)},
    44  		10: {`"ff"`, true, BlockNumber(0)},
    45  		11: {`"pending"`, false, PendingBlockNumber},
    46  		12: {`"latest"`, false, LatestBlockNumber},
    47  		13: {`"earliest"`, false, EarliestBlockNumber},
    48  		14: {`someString`, true, BlockNumber(0)},
    49  		15: {`""`, true, BlockNumber(0)},
    50  		16: {``, true, BlockNumber(0)},
    51  	}
    52  
    53  	for i, test := range tests {
    54  		var num BlockNumber
    55  		err := json.Unmarshal([]byte(test.input), &num)
    56  		if test.mustFail && err == nil {
    57  			t.Errorf("Test %d should fail", i)
    58  			continue
    59  		}
    60  		if !test.mustFail && err != nil {
    61  			t.Errorf("Test %d should pass but got err: %v", i, err)
    62  			continue
    63  		}
    64  		if num != test.expected {
    65  			t.Errorf("Test %d got unexpected value, want %d, got %d", i, test.expected, num)
    66  		}
    67  	}
    68  }
    69  
    70  func TestBlockNumberOrHash_UnmarshalJSON(t *testing.T) {
    71  	tests := []struct {
    72  		input    string
    73  		mustFail bool
    74  		expected BlockNumberOrHash
    75  	}{
    76  		0:  {`"0x"`, true, BlockNumberOrHash{}},
    77  		1:  {`"0x0"`, false, BlockNumberOrHashWithNumber(0)},
    78  		2:  {`"0X1"`, false, BlockNumberOrHashWithNumber(1)},
    79  		3:  {`"0x00"`, true, BlockNumberOrHash{}},
    80  		4:  {`"0x01"`, true, BlockNumberOrHash{}},
    81  		5:  {`"0x1"`, false, BlockNumberOrHashWithNumber(1)},
    82  		6:  {`"0x12"`, false, BlockNumberOrHashWithNumber(18)},
    83  		7:  {`"0x7fffffffffffffff"`, false, BlockNumberOrHashWithNumber(math.MaxInt64)},
    84  		8:  {`"0x8000000000000000"`, true, BlockNumberOrHash{}},
    85  		9:  {"0", true, BlockNumberOrHash{}},
    86  		10: {`"ff"`, true, BlockNumberOrHash{}},
    87  		11: {`"pending"`, false, BlockNumberOrHashWithNumber(PendingBlockNumber)},
    88  		12: {`"latest"`, false, BlockNumberOrHashWithNumber(LatestBlockNumber)},
    89  		13: {`"earliest"`, false, BlockNumberOrHashWithNumber(EarliestBlockNumber)},
    90  		14: {`someString`, true, BlockNumberOrHash{}},
    91  		15: {`""`, true, BlockNumberOrHash{}},
    92  		16: {``, true, BlockNumberOrHash{}},
    93  		17: {`"0x0000000000000000000000000000000000000000000000000000000000000000"`, false, BlockNumberOrHashWithHash(common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"), false)},
    94  		18: {`{"blockHash":"0x0000000000000000000000000000000000000000000000000000000000000000"}`, false, BlockNumberOrHashWithHash(common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"), false)},
    95  		19: {`{"blockHash":"0x0000000000000000000000000000000000000000000000000000000000000000","requireCanonical":false}`, false, BlockNumberOrHashWithHash(common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"), false)},
    96  		20: {`{"blockHash":"0x0000000000000000000000000000000000000000000000000000000000000000","requireCanonical":true}`, false, BlockNumberOrHashWithHash(common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"), true)},
    97  		21: {`{"blockNumber":"0x1"}`, false, BlockNumberOrHashWithNumber(1)},
    98  		22: {`{"blockNumber":"pending"}`, false, BlockNumberOrHashWithNumber(PendingBlockNumber)},
    99  		23: {`{"blockNumber":"latest"}`, false, BlockNumberOrHashWithNumber(LatestBlockNumber)},
   100  		24: {`{"blockNumber":"earliest"}`, false, BlockNumberOrHashWithNumber(EarliestBlockNumber)},
   101  		25: {`{"blockNumber":"0x1", "blockHash":"0x0000000000000000000000000000000000000000000000000000000000000000"}`, true, BlockNumberOrHash{}},
   102  	}
   103  
   104  	for i, test := range tests {
   105  		var bnh BlockNumberOrHash
   106  		err := json.Unmarshal([]byte(test.input), &bnh)
   107  		if test.mustFail && err == nil {
   108  			t.Errorf("Test %d should fail", i)
   109  			continue
   110  		}
   111  		if !test.mustFail && err != nil {
   112  			t.Errorf("Test %d should pass but got err: %v", i, err)
   113  			continue
   114  		}
   115  		hash, hashOk := bnh.Hash()
   116  		expectedHash, expectedHashOk := test.expected.Hash()
   117  		num, numOk := bnh.Number()
   118  		expectedNum, expectedNumOk := test.expected.Number()
   119  		if bnh.RequireCanonical != test.expected.RequireCanonical ||
   120  			hash != expectedHash || hashOk != expectedHashOk ||
   121  			num != expectedNum || numOk != expectedNumOk {
   122  			t.Errorf("Test %d got unexpected value, want %v, got %v", i, test.expected, bnh)
   123  		}
   124  	}
   125  }
   126  
   127  func TestBlockNumberOrHash_WithNumber_MarshalAndUnmarshal(t *testing.T) {
   128  	tests := []struct {
   129  		name   string
   130  		number int64
   131  	}{
   132  		{"max", math.MaxInt64},
   133  		{"pending", int64(PendingBlockNumber)},
   134  		{"latest", int64(LatestBlockNumber)},
   135  		{"earliest", int64(EarliestBlockNumber)},
   136  	}
   137  	for _, test := range tests {
   138  		test := test
   139  		t.Run(test.name, func(t *testing.T) {
   140  			bnh := BlockNumberOrHashWithNumber(BlockNumber(test.number))
   141  			marshalled, err := json.Marshal(bnh)
   142  			if err != nil {
   143  				t.Fatal("cannot marshal:", err)
   144  			}
   145  			var unmarshalled BlockNumberOrHash
   146  			err = json.Unmarshal(marshalled, &unmarshalled)
   147  			if err != nil {
   148  				t.Fatal("cannot unmarshal:", err)
   149  			}
   150  			if !reflect.DeepEqual(bnh, unmarshalled) {
   151  				t.Fatalf("wrong result: expected %v, got %v", bnh, unmarshalled)
   152  			}
   153  		})
   154  	}
   155  }