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