Skip to content

Commit edcb5e4

Browse files
committed
move changes to libevm files
1 parent a98e92b commit edcb5e4

File tree

8 files changed

+298
-212
lines changed

8 files changed

+298
-212
lines changed

common/hexutil/hexutil.go

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ var (
4747
ErrOddLength = &decError{"hex string of odd length"}
4848
ErrEmptyNumber = &decError{"hex string \"0x\""}
4949
ErrLeadingZero = &decError{"hex number with leading zero digits"}
50-
ErrUint16Range = &decError{"hex number > 16 bits"}
5150
ErrUint64Range = &decError{"hex number > 64 bits"}
5251
ErrUintRange = &decError{fmt.Sprintf("hex number > %d bits", uintBits)}
5352
ErrBig256Range = &decError{"hex number > 256 bits"}
@@ -112,37 +111,13 @@ func MustDecodeUint64(input string) uint64 {
112111
return dec
113112
}
114113

115-
// DecodeUint16 decodes a hex string with 0x prefix as a quantity.
116-
func DecodeUint16(input string) (uint16, error) {
117-
raw, err := checkNumber(input)
118-
if err != nil {
119-
return 0, err
120-
}
121-
dec, err := strconv.ParseUint(raw, 16, 16)
122-
if err != nil {
123-
err = mapError(err)
124-
if err == ErrUint64Range {
125-
return 0, ErrUint16Range
126-
}
127-
}
128-
return uint16(dec), err
129-
}
130-
131-
// MustDecodeUint16 decodes a hex string with 0x prefix as a quantity.
132114
// EncodeUint64 encodes i as a hex string with 0x prefix.
133115
func EncodeUint64(i uint64) string {
134116
enc := make([]byte, 2, 10)
135117
copy(enc, "0x")
136118
return string(strconv.AppendUint(enc, i, 16))
137119
}
138120

139-
// EncodeUint16 encodes i as a hex string with 0x prefix.
140-
func EncodeUint16(i uint16) string {
141-
enc := make([]byte, 2, 6)
142-
copy(enc, "0x")
143-
return string(strconv.AppendUint(enc, uint64(i), 16))
144-
}
145-
146121
var bigWordNibbles int
147122

148123
func init() {

common/hexutil/hexutil.libevm.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright 2025 the libevm authors.
2+
//
3+
// The libevm additions to go-ethereum are free software: you can redistribute
4+
// them and/or modify them under the terms of the GNU Lesser General Public License
5+
// as published by the Free Software Foundation, either version 3 of the License,
6+
// or (at your option) any later version.
7+
//
8+
// The libevm additions are distributed in the hope that they will be useful,
9+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
11+
// General Public License for more details.
12+
//
13+
// You should have received a copy of the GNU Lesser General Public License
14+
// along with the go-ethereum library. If not, see
15+
// <http://www.gnu.org/licenses/>.
16+
17+
package hexutil
18+
19+
import "strconv"
20+
21+
var ErrUint16Range = &decError{"hex number > 16 bits"}
22+
23+
// DecodeUint16 decodes a hex string with 0x prefix as a quantity.
24+
func DecodeUint16(input string) (uint16, error) {
25+
raw, err := checkNumber(input)
26+
if err != nil {
27+
return 0, err
28+
}
29+
dec, err := strconv.ParseUint(raw, 16, 16)
30+
if err != nil {
31+
err = mapError(err)
32+
if err == ErrUint64Range {
33+
return 0, ErrUint16Range
34+
}
35+
}
36+
return uint16(dec), err
37+
}
38+
39+
// EncodeUint16 encodes i as a hex string with 0x prefix.
40+
func EncodeUint16(i uint16) string {
41+
enc := make([]byte, 2, 6)
42+
copy(enc, "0x")
43+
return string(strconv.AppendUint(enc, uint64(i), 16))
44+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright 2025 the libevm authors.
2+
//
3+
// The libevm additions to go-ethereum are free software: you can redistribute
4+
// them and/or modify them under the terms of the GNU Lesser General Public License
5+
// as published by the Free Software Foundation, either version 3 of the License,
6+
// or (at your option) any later version.
7+
//
8+
// The libevm additions are distributed in the hope that they will be useful,
9+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
11+
// General Public License for more details.
12+
//
13+
// You should have received a copy of the GNU Lesser General Public License
14+
// along with the go-ethereum library. If not, see
15+
// <http://www.gnu.org/licenses/>.
16+
17+
package hexutil
18+
19+
import "testing"
20+
21+
var (
22+
encodeUint16Tests = []marshalTest{
23+
{uint16(0), "0x0"},
24+
{uint16(1), "0x1"},
25+
{uint16(0xff), "0xff"},
26+
{uint16(0x1122), "0x1122"},
27+
}
28+
29+
decodeUint16Tests = []unmarshalTest{
30+
// invalid
31+
{input: `0`, wantErr: ErrMissingPrefix},
32+
{input: `0x`, wantErr: ErrEmptyNumber},
33+
{input: `0x01`, wantErr: ErrLeadingZero},
34+
{input: `0xfffff`, wantErr: ErrUint16Range},
35+
{input: `0xz1`, wantErr: ErrSyntax},
36+
// valid
37+
{input: `0x0`, want: uint16(0)},
38+
{input: `0x2`, want: uint16(0x2)},
39+
{input: `0x2F2`, want: uint16(0x2f2)},
40+
{input: `0X2F2`, want: uint16(0x2f2)},
41+
{input: `0xff`, want: uint16(0xff)},
42+
{input: `0x12af`, want: uint16(0x12af)},
43+
{input: `0xbbb`, want: uint16(0xbbb)},
44+
{input: `0xffff`, want: uint16(0xffff)},
45+
}
46+
)
47+
48+
func TestEncodeUint16(t *testing.T) {
49+
for _, test := range encodeUint16Tests {
50+
enc := EncodeUint16(test.input.(uint16))
51+
if enc != test.want {
52+
t.Errorf("input %x: wrong encoding %s", test.input, enc)
53+
}
54+
}
55+
}
56+
57+
func TestDecodeUint16(t *testing.T) {
58+
for _, test := range decodeUint16Tests {
59+
dec, err := DecodeUint16(test.input)
60+
if !checkError(t, test.input, err, test.wantErr) {
61+
continue
62+
}
63+
if dec != test.want.(uint16) {
64+
t.Errorf("input %s: value mismatch: got %x, want %x", test.input, dec, test.want)
65+
continue
66+
}
67+
}
68+
}

common/hexutil/hexutil_test.go

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,6 @@ var (
5757
{uint64(0x1122334455667788), "0x1122334455667788"},
5858
}
5959

60-
encodeUint16Tests = []marshalTest{
61-
{uint16(0), "0x0"},
62-
{uint16(1), "0x1"},
63-
{uint16(0xff), "0xff"},
64-
{uint16(0x1122), "0x1122"},
65-
}
66-
6760
encodeUintTests = []marshalTest{
6861
{uint(0), "0x0"},
6962
{uint(1), "0x1"},
@@ -141,24 +134,6 @@ var (
141134
{input: `0xbbb`, want: uint64(0xbbb)},
142135
{input: `0xffffffffffffffff`, want: uint64(0xffffffffffffffff)},
143136
}
144-
145-
decodeUint16Tests = []unmarshalTest{
146-
// invalid
147-
{input: `0`, wantErr: ErrMissingPrefix},
148-
{input: `0x`, wantErr: ErrEmptyNumber},
149-
{input: `0x01`, wantErr: ErrLeadingZero},
150-
{input: `0xfffff`, wantErr: ErrUint16Range},
151-
{input: `0xz1`, wantErr: ErrSyntax},
152-
// valid
153-
{input: `0x0`, want: uint16(0)},
154-
{input: `0x2`, want: uint16(0x2)},
155-
{input: `0x2F2`, want: uint16(0x2f2)},
156-
{input: `0X2F2`, want: uint16(0x2f2)},
157-
{input: `0xff`, want: uint16(0xff)},
158-
{input: `0x12af`, want: uint16(0x12af)},
159-
{input: `0xbbb`, want: uint16(0xbbb)},
160-
{input: `0xffff`, want: uint16(0xffff)},
161-
}
162137
)
163138

164139
func TestEncode(t *testing.T) {
@@ -214,15 +189,6 @@ func TestEncodeUint64(t *testing.T) {
214189
}
215190
}
216191

217-
func TestEncodeUint16(t *testing.T) {
218-
for _, test := range encodeUint16Tests {
219-
enc := EncodeUint16(test.input.(uint16))
220-
if enc != test.want {
221-
t.Errorf("input %x: wrong encoding %s", test.input, enc)
222-
}
223-
}
224-
}
225-
226192
func TestDecodeUint64(t *testing.T) {
227193
for _, test := range decodeUint64Tests {
228194
dec, err := DecodeUint64(test.input)
@@ -236,19 +202,6 @@ func TestDecodeUint64(t *testing.T) {
236202
}
237203
}
238204

239-
func TestDecodeUint16(t *testing.T) {
240-
for _, test := range decodeUint16Tests {
241-
dec, err := DecodeUint16(test.input)
242-
if !checkError(t, test.input, err, test.wantErr) {
243-
continue
244-
}
245-
if dec != test.want.(uint16) {
246-
t.Errorf("input %s: value mismatch: got %x, want %x", test.input, dec, test.want)
247-
continue
248-
}
249-
}
250-
}
251-
252205
func BenchmarkEncodeBig(b *testing.B) {
253206
for _, bench := range encodeBigTests {
254207
b.Run(bench.want, func(b *testing.B) {

common/hexutil/json.go

Lines changed: 0 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ var (
3131
bytesT = reflect.TypeOf(Bytes(nil))
3232
bigT = reflect.TypeOf((*Big)(nil))
3333
uintT = reflect.TypeOf(Uint(0))
34-
uint16T = reflect.TypeOf(Uint16(0))
3534
uint64T = reflect.TypeOf(Uint64(0))
3635
u256T = reflect.TypeOf((*uint256.Int)(nil))
3736
)
@@ -370,71 +369,6 @@ func (b Uint) String() string {
370369
return EncodeUint64(uint64(b))
371370
}
372371

373-
// Uint16 marshals/unmarshals as a JSON string with 0x prefix.
374-
// The zero value marshals as "0x0".
375-
type Uint16 uint16
376-
377-
// MarshalText implements encoding.TextMarshaler.
378-
func (b Uint16) MarshalText() ([]byte, error) {
379-
buf := make([]byte, 2, 6)
380-
copy(buf, `0x`)
381-
buf = strconv.AppendUint(buf, uint64(b), 16)
382-
return buf, nil
383-
}
384-
385-
// UnmarshalJSON implements json.Unmarshaler.
386-
func (b *Uint16) UnmarshalJSON(input []byte) error {
387-
if !isString(input) {
388-
return errNonString(uint16T)
389-
}
390-
return wrapTypeError(b.UnmarshalText(input[1:len(input)-1]), uint16T)
391-
}
392-
393-
// UnmarshalText implements encoding.TextUnmarshaler.
394-
func (b *Uint16) UnmarshalText(input []byte) error {
395-
raw, err := checkNumberText(input)
396-
if err != nil {
397-
return err
398-
}
399-
if len(raw) > 4 {
400-
return ErrUint16Range
401-
}
402-
var dec uint16
403-
for _, byte := range raw {
404-
nib := decodeNibble(byte)
405-
if nib == badNibble {
406-
return ErrSyntax
407-
}
408-
dec *= 16
409-
dec += uint16(nib)
410-
}
411-
412-
*b = Uint16(dec)
413-
return nil
414-
}
415-
416-
// String returns the hex encoding of b.
417-
func (b Uint16) String() string {
418-
return EncodeUint16(uint16(b))
419-
}
420-
421-
// ImplementsGraphQLType returns true if Uint16 implements the provided GraphQL type.
422-
func (b Uint16) ImplementsGraphQLType(name string) bool { return name == "Int" }
423-
424-
// UnmarshalGraphQL unmarshals the provided GraphQL query data.
425-
func (b *Uint16) UnmarshalGraphQL(input interface{}) error {
426-
var err error
427-
switch input := input.(type) {
428-
case string:
429-
return b.UnmarshalText([]byte(input))
430-
case int32:
431-
*b = Uint16(input)
432-
default:
433-
err = fmt.Errorf("unexpected type %T for Int", input)
434-
}
435-
return err
436-
}
437-
438372
func isString(input []byte) bool {
439373
return len(input) >= 2 && input[0] == '"' && input[len(input)-1] == '"'
440374
}

0 commit comments

Comments
 (0)