Skip to content

Commit

Permalink
accounts/abi: move U256Bytes to common/math (ethereum#21020)
Browse files Browse the repository at this point in the history
  • Loading branch information
MariusVanDerWijden authored May 4, 2020
1 parent e872083 commit ab72803
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 48 deletions.
3 changes: 2 additions & 1 deletion accounts/abi/abi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"testing"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/crypto"
)

Expand Down Expand Up @@ -601,7 +602,7 @@ func TestInputFixedArrayAndVariableInputLength(t *testing.T) {
strvalue = common.RightPadBytes([]byte(strin), 32)
fixedarrin1value1 = common.LeftPadBytes(fixedarrin1[0].Bytes(), 32)
fixedarrin1value2 = common.LeftPadBytes(fixedarrin1[1].Bytes(), 32)
dynarroffset = U256(big.NewInt(int64(256 + ((len(strin)/32)+1)*32)))
dynarroffset = math.U256Bytes(big.NewInt(int64(256 + ((len(strin)/32)+1)*32)))
dynarrlength = make([]byte, 32)
dynarrlength[31] = byte(len(dynarrin))
dynarrinvalue1 = common.LeftPadBytes(dynarrin[0].Bytes(), 32)
Expand Down
1 change: 0 additions & 1 deletion accounts/abi/bind/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ var (
_ = big.NewInt
_ = strings.NewReader
_ = ethereum.NotFound
_ = abi.U256
_ = bind.Bind
_ = common.Big1
_ = types.BloomLookup
Expand Down
7 changes: 0 additions & 7 deletions accounts/abi/numbers.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"reflect"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/math"
)

var (
Expand All @@ -37,9 +36,3 @@ var (
int64T = reflect.TypeOf(int64(0))
addressT = reflect.TypeOf(common.Address{})
)

// U256 converts a big Int into a 256bit EVM number.
// This operation is destructive.
func U256(n *big.Int) []byte {
return math.PaddedBigBytes(math.U256(n), 32)
}
33 changes: 0 additions & 33 deletions accounts/abi/numbers_test.go

This file was deleted.

6 changes: 3 additions & 3 deletions accounts/abi/pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ func packElement(t Type, reflectValue reflect.Value) []byte {
func packNum(value reflect.Value) []byte {
switch kind := value.Kind(); kind {
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return U256(new(big.Int).SetUint64(value.Uint()))
return math.U256Bytes(new(big.Int).SetUint64(value.Uint()))
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return U256(big.NewInt(value.Int()))
return math.U256Bytes(big.NewInt(value.Int()))
case reflect.Ptr:
return U256(new(big.Int).Set(value.Interface().(*big.Int)))
return math.U256Bytes(new(big.Int).Set(value.Interface().(*big.Int)))
default:
panic("abi: fatal error")
}
Expand Down
6 changes: 6 additions & 0 deletions common/math/big.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,12 @@ func U256(x *big.Int) *big.Int {
return x.And(x, tt256m1)
}

// U256Bytes converts a big Int into a 256bit EVM number.
// This operation is destructive.
func U256Bytes(n *big.Int) []byte {
return PaddedBigBytes(U256(n), 32)
}

// S256 interprets x as a two's complement number.
// x must not exceed 256 bits (the result is undefined if it does) and is not modified.
//
Expand Down
10 changes: 10 additions & 0 deletions common/math/big_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,16 @@ func TestU256(t *testing.T) {
}
}

func TestU256Bytes(t *testing.T) {
ubytes := make([]byte, 32)
ubytes[31] = 1

unsigned := U256Bytes(big.NewInt(1))
if !bytes.Equal(unsigned, ubytes) {
t.Errorf("expected %x got %x", ubytes, unsigned)
}
}

func TestBigEndianByteAt(t *testing.T) {
tests := []struct {
x string
Expand Down
1 change: 0 additions & 1 deletion contracts/checkpointoracle/contract/oracle.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions signer/core/signed_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
"unicode"

"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/common/math"
Expand Down Expand Up @@ -587,7 +586,7 @@ func (typedData *TypedData) EncodePrimitiveValue(encType string, encValue interf
if err != nil {
return nil, err
}
return abi.U256(b), nil
return math.U256Bytes(b), nil
}
return nil, fmt.Errorf("unrecognized type '%s'", encType)

Expand Down

0 comments on commit ab72803

Please sign in to comment.