diff --git a/common/hexutil/hexutil.go b/common/hexutil/hexutil.go index a8aa2a6456..ca8a4ae0ef 100644 --- a/common/hexutil/hexutil.go +++ b/common/hexutil/hexutil.go @@ -185,7 +185,6 @@ func EncodeBig(bigint *big.Int) string { } } -// Add more comprehensive check to validate a hex string func IsValidHexString(input string) bool { if !has0xPrefix(input) { return false diff --git a/common/hexutil/hexutil_test.go b/common/hexutil/hexutil_test.go index f2b800d82c..5c89fa8c9d 100644 --- a/common/hexutil/hexutil_test.go +++ b/common/hexutil/hexutil_test.go @@ -22,6 +22,11 @@ import ( "testing" ) +type hexValidityTest struct { + input string + want bool +} + type marshalTest struct { input interface{} want string @@ -134,6 +139,12 @@ var ( {input: `0xbbb`, want: uint64(0xbbb)}, {input: `0xffffffffffffffff`, want: uint64(0xffffffffffffffff)}, } + + hexStringValidityTest = []hexValidityTest{ + {"0x", false}, + {"asdcc", false}, + {"0x00000102", true}, + } ) func TestEncode(t *testing.T) { @@ -202,6 +213,15 @@ func TestDecodeUint64(t *testing.T) { } } +func TestIsValidHexString(t *testing.T) { + for _, test := range hexStringValidityTest { + actual := IsValidHexString(test.input) + if actual != test.want { + t.Errorf("input %s: value mismatch: got %t, want %t", test.input, actual, test.want) + } + } +} + func BenchmarkEncodeBig(b *testing.B) { for _, bench := range encodeBigTests { b.Run(bench.want, func(b *testing.B) { diff --git a/rpc/types_test.go b/rpc/types_test.go index e48db859d4..2a895886b8 100644 --- a/rpc/types_test.go +++ b/rpc/types_test.go @@ -42,16 +42,16 @@ func TestBlockNumberJSONUnmarshal(t *testing.T) { 6: {`"0x12"`, false, BlockNumber(18)}, 7: {`"0x7fffffffffffffff"`, false, BlockNumber(math.MaxInt64)}, 8: {`"0x8000000000000000"`, true, BlockNumber(0)}, - 9: {"0", true, BlockNumber(0)}, - 10: {`"ff"`, true, BlockNumber(0)}, - 11: {`"pending"`, false, PendingBlockNumber}, - 12: {`"latest"`, false, LatestBlockNumber}, - 13: {`"earliest"`, false, EarliestBlockNumber}, - 14: {`"committed"`, false, CommittedBlockNumber}, - 15: {`"finalized"`, false, CommittedBlockNumber}, - 16: {`someString`, true, BlockNumber(0)}, - 17: {`""`, true, BlockNumber(0)}, - 18: {``, true, BlockNumber(0)}, + 9: {`"ff"`, true, BlockNumber(0)}, + 10: {`"pending"`, false, PendingBlockNumber}, + 11: {`"latest"`, false, LatestBlockNumber}, + 12: {`"earliest"`, false, EarliestBlockNumber}, + 13: {`"committed"`, false, CommittedBlockNumber}, + 14: {`"finalized"`, false, CommittedBlockNumber}, + 15: {`someString`, true, BlockNumber(0)}, + 16: {`""`, true, BlockNumber(0)}, + 17: {``, true, BlockNumber(0)}, + 18: {`88439993`, false, BlockNumber(88439993)}, } for i, test := range tests {