Skip to content

accounts/abi/abigen: use golang uint256.Int for solidity uint256 params #31607

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions accounts/abi/abi.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ import (
"errors"
"fmt"
"io"
"math/big"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/holiman/uint256"
)

// The ABI holds information about a contract's context and available
Expand Down Expand Up @@ -299,7 +299,7 @@ func UnpackRevert(data []byte) (string, error) {
if err != nil {
return "", err
}
pCode := unpacked[0].(*big.Int)
pCode := unpacked[0].(*uint256.Int)
// uint64 safety check for future
// but the code is not bigger than MAX(uint64) now
if pCode.IsUint64() {
Expand Down
23 changes: 12 additions & 11 deletions accounts/abi/abi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/internal/testrand"
"github.com/holiman/uint256"
)

const jsondata = `
Expand Down Expand Up @@ -185,7 +186,7 @@ func TestConstructor(t *testing.T) {
t.Error("Missing expected constructor")
}
// Test pack/unpack
packed, err := abi.Pack("", big.NewInt(1), big.NewInt(2))
packed, err := abi.Pack("", uint256.NewInt(1), uint256.NewInt(2))
if err != nil {
t.Error(err)
}
Expand All @@ -194,10 +195,10 @@ func TestConstructor(t *testing.T) {
t.Error(err)
}

if !reflect.DeepEqual(unpacked[0], big.NewInt(1)) {
if !reflect.DeepEqual(unpacked[0], uint256.NewInt(1)) {
t.Error("Unable to pack/unpack from constructor")
}
if !reflect.DeepEqual(unpacked[1], big.NewInt(2)) {
if !reflect.DeepEqual(unpacked[1], uint256.NewInt(2)) {
t.Error("Unable to pack/unpack from constructor")
}
}
Expand Down Expand Up @@ -328,11 +329,11 @@ func TestCustomErrorUnpackIntoInterface(t *testing.T) {
}
type MyError struct {
Sender common.Address
Balance *big.Int
Balance *uint256.Int
}

sender := testrand.Address()
balance := new(big.Int).SetBytes(testrand.Bytes(8))
balance := new(uint256.Int).SetBytes(testrand.Bytes(8))
encoded, err := abi.Errors[errorName].Inputs.Pack(sender, balance)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -802,7 +803,7 @@ func TestUnpackEvent(t *testing.T) {

type ReceivedEvent struct {
Sender common.Address
Amount *big.Int
Amount *uint256.Int
Memo []byte
}
var ev ReceivedEvent
Expand Down Expand Up @@ -842,7 +843,7 @@ func TestUnpackEventIntoMap(t *testing.T) {
receivedMap := map[string]interface{}{}
expectedReceivedMap := map[string]interface{}{
"sender": common.HexToAddress("0x376c47978271565f56DEB45495afa69E59c16Ab2"),
"amount": big.NewInt(1),
"amount": uint256.NewInt(1),
"memo": []byte{88},
}
if err := abi.UnpackIntoMap(receivedMap, "received", data); err != nil {
Expand All @@ -854,7 +855,7 @@ func TestUnpackEventIntoMap(t *testing.T) {
if receivedMap["sender"] != expectedReceivedMap["sender"] {
t.Error("unpacked `received` map does not match expected map")
}
if receivedMap["amount"].(*big.Int).Cmp(expectedReceivedMap["amount"].(*big.Int)) != 0 {
if receivedMap["amount"].(*uint256.Int).Cmp(expectedReceivedMap["amount"].(*uint256.Int)) != 0 {
t.Error("unpacked `received` map does not match expected map")
}
if !bytes.Equal(receivedMap["memo"].([]byte), expectedReceivedMap["memo"].([]byte)) {
Expand Down Expand Up @@ -906,7 +907,7 @@ func TestUnpackMethodIntoMap(t *testing.T) {
if len(sendMap) != 1 {
t.Error("unpacked `send` map expected to have length 1")
}
if sendMap["amount"].(*big.Int).Cmp(big.NewInt(1)) != 0 {
if sendMap["amount"].(*uint256.Int).Cmp(uint256.NewInt(1)) != 0 {
t.Error("unpacked `send` map expected `amount` value of 1")
}

Expand Down Expand Up @@ -988,7 +989,7 @@ func TestUnpackIntoMapNamingConflict(t *testing.T) {
}
expectedReceivedMap := map[string]interface{}{
"sender": common.HexToAddress("0x376c47978271565f56DEB45495afa69E59c16Ab2"),
"amount": big.NewInt(1),
"amount": uint256.NewInt(1),
"memo": []byte{88},
}
if err = abi.UnpackIntoMap(receivedMap, "Received", data); err != nil {
Expand All @@ -1000,7 +1001,7 @@ func TestUnpackIntoMapNamingConflict(t *testing.T) {
if receivedMap["sender"] != expectedReceivedMap["sender"] {
t.Error("unpacked `received` map does not match expected map")
}
if receivedMap["amount"].(*big.Int).Cmp(expectedReceivedMap["amount"].(*big.Int)) != 0 {
if receivedMap["amount"].(*uint256.Int).Cmp(expectedReceivedMap["amount"].(*uint256.Int)) != 0 {
t.Error("unpacked `received` map does not match expected map")
}
if !bytes.Equal(receivedMap["memo"].([]byte), expectedReceivedMap["memo"].([]byte)) {
Expand Down
3 changes: 3 additions & 0 deletions accounts/abi/abigen/bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,9 @@ func bindBasicType(kind abi.Type) string {
case "8", "16", "32", "64":
return fmt.Sprintf("%sint%s", parts[1], parts[2])
}
if parts[1] == "u" {
return "*uint256.Int"
}
return "*big.Int"
case abi.FixedBytesTy:
return fmt.Sprintf("[%d]byte", kind.Size)
Expand Down
Loading