github.com/cgcardona/r-subnet-evm@v0.1.5/rpc/types_test.go (about)

     1  // (c) 2019-2020, Ava Labs, Inc.
     2  //
     3  // This file is a derived work, based on the go-ethereum library whose original
     4  // notices appear below.
     5  //
     6  // It is distributed under a license compatible with the licensing terms of the
     7  // original code from which it is derived.
     8  //
     9  // Much love to the original authors for their work.
    10  // **********
    11  // Copyright 2015 The go-ethereum Authors
    12  // This file is part of the go-ethereum library.
    13  //
    14  // The go-ethereum library is free software: you can redistribute it and/or modify
    15  // it under the terms of the GNU Lesser General Public License as published by
    16  // the Free Software Foundation, either version 3 of the License, or
    17  // (at your option) any later version.
    18  //
    19  // The go-ethereum library is distributed in the hope that it will be useful,
    20  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    21  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    22  // GNU Lesser General Public License for more details.
    23  //
    24  // You should have received a copy of the GNU Lesser General Public License
    25  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    26  
    27  package rpc
    28  
    29  import (
    30  	"encoding/json"
    31  	"reflect"
    32  	"testing"
    33  
    34  	"github.com/ethereum/go-ethereum/common"
    35  	"github.com/ethereum/go-ethereum/common/math"
    36  )
    37  
    38  func TestBlockNumberJSONUnmarshal(t *testing.T) {
    39  	tests := []struct {
    40  		input    string
    41  		mustFail bool
    42  		expected BlockNumber
    43  	}{
    44  		0:  {`"0x"`, true, BlockNumber(0)},
    45  		1:  {`"0x0"`, false, BlockNumber(0)},
    46  		2:  {`"0X1"`, false, BlockNumber(1)},
    47  		3:  {`"0x00"`, true, BlockNumber(0)},
    48  		4:  {`"0x01"`, true, BlockNumber(0)},
    49  		5:  {`"0x1"`, false, BlockNumber(1)},
    50  		6:  {`"0x12"`, false, BlockNumber(18)},
    51  		7:  {`"0x7fffffffffffffff"`, false, BlockNumber(math.MaxInt64)},
    52  		8:  {`"0x8000000000000000"`, true, BlockNumber(0)},
    53  		9:  {"0", true, BlockNumber(0)},
    54  		10: {`"ff"`, true, BlockNumber(0)},
    55  		11: {`"pending"`, false, PendingBlockNumber},
    56  		12: {`"latest"`, false, LatestBlockNumber},
    57  		13: {`"earliest"`, false, EarliestBlockNumber},
    58  		14: {`someString`, true, BlockNumber(0)},
    59  		15: {`""`, true, BlockNumber(0)},
    60  		16: {``, true, BlockNumber(0)},
    61  	}
    62  
    63  	for i, test := range tests {
    64  		var num BlockNumber
    65  		err := json.Unmarshal([]byte(test.input), &num)
    66  		if test.mustFail && err == nil {
    67  			t.Errorf("Test %d should fail", i)
    68  			continue
    69  		}
    70  		if !test.mustFail && err != nil {
    71  			t.Errorf("Test %d should pass but got err: %v", i, err)
    72  			continue
    73  		}
    74  		if num != test.expected {
    75  			t.Errorf("Test %d got unexpected value, want %d, got %d", i, test.expected, num)
    76  		}
    77  	}
    78  }
    79  
    80  func TestBlockNumberOrHash_UnmarshalJSON(t *testing.T) {
    81  	tests := []struct {
    82  		input    string
    83  		mustFail bool
    84  		expected BlockNumberOrHash
    85  	}{
    86  		0:  {`"0x"`, true, BlockNumberOrHash{}},
    87  		1:  {`"0x0"`, false, BlockNumberOrHashWithNumber(0)},
    88  		2:  {`"0X1"`, false, BlockNumberOrHashWithNumber(1)},
    89  		3:  {`"0x00"`, true, BlockNumberOrHash{}},
    90  		4:  {`"0x01"`, true, BlockNumberOrHash{}},
    91  		5:  {`"0x1"`, false, BlockNumberOrHashWithNumber(1)},
    92  		6:  {`"0x12"`, false, BlockNumberOrHashWithNumber(18)},
    93  		7:  {`"0x7fffffffffffffff"`, false, BlockNumberOrHashWithNumber(math.MaxInt64)},
    94  		8:  {`"0x8000000000000000"`, true, BlockNumberOrHash{}},
    95  		9:  {"0", true, BlockNumberOrHash{}},
    96  		10: {`"ff"`, true, BlockNumberOrHash{}},
    97  		11: {`"pending"`, false, BlockNumberOrHashWithNumber(PendingBlockNumber)},
    98  		12: {`"latest"`, false, BlockNumberOrHashWithNumber(LatestBlockNumber)},
    99  		13: {`"earliest"`, false, BlockNumberOrHashWithNumber(EarliestBlockNumber)},
   100  		14: {`someString`, true, BlockNumberOrHash{}},
   101  		15: {`""`, true, BlockNumberOrHash{}},
   102  		16: {``, true, BlockNumberOrHash{}},
   103  		17: {`"0x0000000000000000000000000000000000000000000000000000000000000000"`, false, BlockNumberOrHashWithHash(common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"), false)},
   104  		18: {`{"blockHash":"0x0000000000000000000000000000000000000000000000000000000000000000"}`, false, BlockNumberOrHashWithHash(common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"), false)},
   105  		19: {`{"blockHash":"0x0000000000000000000000000000000000000000000000000000000000000000","requireCanonical":false}`, false, BlockNumberOrHashWithHash(common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"), false)},
   106  		20: {`{"blockHash":"0x0000000000000000000000000000000000000000000000000000000000000000","requireCanonical":true}`, false, BlockNumberOrHashWithHash(common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"), true)},
   107  		21: {`{"blockNumber":"0x1"}`, false, BlockNumberOrHashWithNumber(1)},
   108  		22: {`{"blockNumber":"pending"}`, false, BlockNumberOrHashWithNumber(PendingBlockNumber)},
   109  		23: {`{"blockNumber":"latest"}`, false, BlockNumberOrHashWithNumber(LatestBlockNumber)},
   110  		24: {`{"blockNumber":"earliest"}`, false, BlockNumberOrHashWithNumber(EarliestBlockNumber)},
   111  		25: {`{"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  	tests := []struct {
   139  		name   string
   140  		number int64
   141  	}{
   142  		{"max", math.MaxInt64},
   143  		{"pending", int64(PendingBlockNumber)},
   144  		{"latest", int64(LatestBlockNumber)},
   145  		{"earliest", int64(EarliestBlockNumber)},
   146  	}
   147  	for _, test := range tests {
   148  		test := test
   149  		t.Run(test.name, func(t *testing.T) {
   150  			bnh := BlockNumberOrHashWithNumber(BlockNumber(test.number))
   151  			marshalled, err := json.Marshal(bnh)
   152  			if err != nil {
   153  				t.Fatal("cannot marshal:", err)
   154  			}
   155  			var unmarshalled BlockNumberOrHash
   156  			err = json.Unmarshal(marshalled, &unmarshalled)
   157  			if err != nil {
   158  				t.Fatal("cannot unmarshal:", err)
   159  			}
   160  			if !reflect.DeepEqual(bnh, unmarshalled) {
   161  				t.Fatalf("wrong result: expected %v, got %v", bnh, unmarshalled)
   162  			}
   163  		})
   164  	}
   165  }