accounts/abi: move U256Bytes to common/math (#21020)
This commit is contained in:
parent
e872083d44
commit
ab72803e6f
@ -27,6 +27,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
|
"github.com/ethereum/go-ethereum/common/math"
|
||||||
"github.com/ethereum/go-ethereum/crypto"
|
"github.com/ethereum/go-ethereum/crypto"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -601,7 +602,7 @@ func TestInputFixedArrayAndVariableInputLength(t *testing.T) {
|
|||||||
strvalue = common.RightPadBytes([]byte(strin), 32)
|
strvalue = common.RightPadBytes([]byte(strin), 32)
|
||||||
fixedarrin1value1 = common.LeftPadBytes(fixedarrin1[0].Bytes(), 32)
|
fixedarrin1value1 = common.LeftPadBytes(fixedarrin1[0].Bytes(), 32)
|
||||||
fixedarrin1value2 = common.LeftPadBytes(fixedarrin1[1].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 = make([]byte, 32)
|
||||||
dynarrlength[31] = byte(len(dynarrin))
|
dynarrlength[31] = byte(len(dynarrin))
|
||||||
dynarrinvalue1 = common.LeftPadBytes(dynarrin[0].Bytes(), 32)
|
dynarrinvalue1 = common.LeftPadBytes(dynarrin[0].Bytes(), 32)
|
||||||
|
@ -103,7 +103,6 @@ var (
|
|||||||
_ = big.NewInt
|
_ = big.NewInt
|
||||||
_ = strings.NewReader
|
_ = strings.NewReader
|
||||||
_ = ethereum.NotFound
|
_ = ethereum.NotFound
|
||||||
_ = abi.U256
|
|
||||||
_ = bind.Bind
|
_ = bind.Bind
|
||||||
_ = common.Big1
|
_ = common.Big1
|
||||||
_ = types.BloomLookup
|
_ = types.BloomLookup
|
||||||
|
@ -21,7 +21,6 @@ import (
|
|||||||
"reflect"
|
"reflect"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
"github.com/ethereum/go-ethereum/common/math"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -37,9 +36,3 @@ var (
|
|||||||
int64T = reflect.TypeOf(int64(0))
|
int64T = reflect.TypeOf(int64(0))
|
||||||
addressT = reflect.TypeOf(common.Address{})
|
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)
|
|
||||||
}
|
|
||||||
|
@ -1,33 +0,0 @@
|
|||||||
// Copyright 2015 The go-ethereum Authors
|
|
||||||
// This file is part of the go-ethereum library.
|
|
||||||
//
|
|
||||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
||||||
// it under the terms of the GNU Lesser General Public License as published by
|
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
|
||||||
// (at your option) any later version.
|
|
||||||
//
|
|
||||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
||||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
// GNU Lesser General Public License for more details.
|
|
||||||
//
|
|
||||||
// You should have received a copy of the GNU Lesser General Public License
|
|
||||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
|
|
||||||
package abi
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"math/big"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestNumberTypes(t *testing.T) {
|
|
||||||
ubytes := make([]byte, 32)
|
|
||||||
ubytes[31] = 1
|
|
||||||
|
|
||||||
unsigned := U256(big.NewInt(1))
|
|
||||||
if !bytes.Equal(unsigned, ubytes) {
|
|
||||||
t.Errorf("expected %x got %x", ubytes, unsigned)
|
|
||||||
}
|
|
||||||
}
|
|
@ -69,11 +69,11 @@ func packElement(t Type, reflectValue reflect.Value) []byte {
|
|||||||
func packNum(value reflect.Value) []byte {
|
func packNum(value reflect.Value) []byte {
|
||||||
switch kind := value.Kind(); kind {
|
switch kind := value.Kind(); kind {
|
||||||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
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:
|
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:
|
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:
|
default:
|
||||||
panic("abi: fatal error")
|
panic("abi: fatal error")
|
||||||
}
|
}
|
||||||
|
@ -184,6 +184,12 @@ func U256(x *big.Int) *big.Int {
|
|||||||
return x.And(x, tt256m1)
|
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.
|
// 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.
|
// x must not exceed 256 bits (the result is undefined if it does) and is not modified.
|
||||||
//
|
//
|
||||||
|
@ -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) {
|
func TestBigEndianByteAt(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
x string
|
x string
|
||||||
|
@ -20,7 +20,6 @@ var (
|
|||||||
_ = big.NewInt
|
_ = big.NewInt
|
||||||
_ = strings.NewReader
|
_ = strings.NewReader
|
||||||
_ = ethereum.NotFound
|
_ = ethereum.NotFound
|
||||||
_ = abi.U256
|
|
||||||
_ = bind.Bind
|
_ = bind.Bind
|
||||||
_ = common.Big1
|
_ = common.Big1
|
||||||
_ = types.BloomLookup
|
_ = types.BloomLookup
|
||||||
|
@ -31,7 +31,6 @@ import (
|
|||||||
"unicode"
|
"unicode"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/accounts"
|
"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"
|
||||||
"github.com/ethereum/go-ethereum/common/hexutil"
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
||||||
"github.com/ethereum/go-ethereum/common/math"
|
"github.com/ethereum/go-ethereum/common/math"
|
||||||
@ -587,7 +586,7 @@ func (typedData *TypedData) EncodePrimitiveValue(encType string, encValue interf
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return abi.U256(b), nil
|
return math.U256Bytes(b), nil
|
||||||
}
|
}
|
||||||
return nil, fmt.Errorf("unrecognized type '%s'", encType)
|
return nil, fmt.Errorf("unrecognized type '%s'", encType)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user