Skip to content

Commit ab72803

Browse files
accounts/abi: move U256Bytes to common/math (#21020)
1 parent e872083 commit ab72803

9 files changed

Lines changed: 22 additions & 48 deletions

File tree

‎accounts/abi/abi_test.go‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"testing"
2828

2929
"github.com/ethereum/go-ethereum/common"
30+
"github.com/ethereum/go-ethereum/common/math"
3031
"github.com/ethereum/go-ethereum/crypto"
3132
)
3233

@@ -601,7 +602,7 @@ func TestInputFixedArrayAndVariableInputLength(t *testing.T) {
601602
strvalue = common.RightPadBytes([]byte(strin), 32)
602603
fixedarrin1value1 = common.LeftPadBytes(fixedarrin1[0].Bytes(), 32)
603604
fixedarrin1value2 = common.LeftPadBytes(fixedarrin1[1].Bytes(), 32)
604-
dynarroffset = U256(big.NewInt(int64(256 + ((len(strin)/32)+1)*32)))
605+
dynarroffset = math.U256Bytes(big.NewInt(int64(256 + ((len(strin)/32)+1)*32)))
605606
dynarrlength = make([]byte, 32)
606607
dynarrlength[31] = byte(len(dynarrin))
607608
dynarrinvalue1 = common.LeftPadBytes(dynarrin[0].Bytes(), 32)

‎accounts/abi/bind/template.go‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ var (
103103
_ = big.NewInt
104104
_ = strings.NewReader
105105
_ = ethereum.NotFound
106-
_ = abi.U256
107106
_ = bind.Bind
108107
_ = common.Big1
109108
_ = types.BloomLookup

‎accounts/abi/numbers.go‎

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"reflect"
2222

2323
"github.com/ethereum/go-ethereum/common"
24-
"github.com/ethereum/go-ethereum/common/math"
2524
)
2625

2726
var (
@@ -37,9 +36,3 @@ var (
3736
int64T = reflect.TypeOf(int64(0))
3837
addressT = reflect.TypeOf(common.Address{})
3938
)
40-
41-
// U256 converts a big Int into a 256bit EVM number.
42-
// This operation is destructive.
43-
func U256(n *big.Int) []byte {
44-
return math.PaddedBigBytes(math.U256(n), 32)
45-
}

‎accounts/abi/numbers_test.go‎

Lines changed: 0 additions & 33 deletions
This file was deleted.

‎accounts/abi/pack.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,11 @@ func packElement(t Type, reflectValue reflect.Value) []byte {
6969
func packNum(value reflect.Value) []byte {
7070
switch kind := value.Kind(); kind {
7171
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
72-
return U256(new(big.Int).SetUint64(value.Uint()))
72+
return math.U256Bytes(new(big.Int).SetUint64(value.Uint()))
7373
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
74-
return U256(big.NewInt(value.Int()))
74+
return math.U256Bytes(big.NewInt(value.Int()))
7575
case reflect.Ptr:
76-
return U256(new(big.Int).Set(value.Interface().(*big.Int)))
76+
return math.U256Bytes(new(big.Int).Set(value.Interface().(*big.Int)))
7777
default:
7878
panic("abi: fatal error")
7979
}

‎common/math/big.go‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,12 @@ func U256(x *big.Int) *big.Int {
184184
return x.And(x, tt256m1)
185185
}
186186

187+
// U256Bytes converts a big Int into a 256bit EVM number.
188+
// This operation is destructive.
189+
func U256Bytes(n *big.Int) []byte {
190+
return PaddedBigBytes(U256(n), 32)
191+
}
192+
187193
// S256 interprets x as a two's complement number.
188194
// x must not exceed 256 bits (the result is undefined if it does) and is not modified.
189195
//

‎common/math/big_test.go‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,16 @@ func TestU256(t *testing.T) {
212212
}
213213
}
214214

215+
func TestU256Bytes(t *testing.T) {
216+
ubytes := make([]byte, 32)
217+
ubytes[31] = 1
218+
219+
unsigned := U256Bytes(big.NewInt(1))
220+
if !bytes.Equal(unsigned, ubytes) {
221+
t.Errorf("expected %x got %x", ubytes, unsigned)
222+
}
223+
}
224+
215225
func TestBigEndianByteAt(t *testing.T) {
216226
tests := []struct {
217227
x string

‎contracts/checkpointoracle/contract/oracle.go‎

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎signer/core/signed_data.go‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import (
3131
"unicode"
3232

3333
"github.com/ethereum/go-ethereum/accounts"
34-
"github.com/ethereum/go-ethereum/accounts/abi"
3534
"github.com/ethereum/go-ethereum/common"
3635
"github.com/ethereum/go-ethereum/common/hexutil"
3736
"github.com/ethereum/go-ethereum/common/math"
@@ -587,7 +586,7 @@ func (typedData *TypedData) EncodePrimitiveValue(encType string, encValue interf
587586
if err != nil {
588587
return nil, err
589588
}
590-
return abi.U256(b), nil
589+
return math.U256Bytes(b), nil
591590
}
592591
return nil, fmt.Errorf("unrecognized type '%s'", encType)
593592

0 commit comments

Comments
 (0)