Skip to content

v0.8.0 #14

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 7 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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
[![Run Tests](https://github.com/defiweb/go-eth/actions/workflows/test.yml/badge.svg)](https://github.com/defiweb/go-eth/actions/workflows/test.yml)

**README IS OUTDATED ON THIS BRANCH**

# go-eth

This library is a Go package designed to interact with the Ethereum blockchain. This package provides robust tools for
Expand Down
6 changes: 3 additions & 3 deletions abi/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func encodeInt(v *big.Int, size int) (Words, error) {
if err := x.SetBigInt(v); err != nil {
return nil, err
}
if err := w.SetBytesPadLeft(x.Bytes()); err != nil {
if err := x.FillBytes(w[:]); err != nil {
return nil, err
}
return Words{w}, nil
Expand All @@ -224,7 +224,7 @@ func encodeUint(v *big.Int, size int) (Words, error) {
if err := x.SetBigInt(v); err != nil {
return nil, err
}
if err := w.SetBytesPadLeft(x.Bytes()); err != nil {
if err := x.FillBytes(w[:]); err != nil {
return nil, err
}
return Words{w}, nil
Expand Down Expand Up @@ -259,5 +259,5 @@ func writeInt(w *Word, x int) error {
if err := i32.SetInt(x); err != nil {
return err
}
return w.SetBytesPadLeft(i32.Bytes())
return i32.FillBytes(w[:])
}
2 changes: 1 addition & 1 deletion abi/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func (e *Event) String() string {
}

func (e *Event) calculateTopic0() {
e.topic0 = crypto.Keccak256([]byte(e.signature))
e.topic0 = types.Hash(crypto.Keccak256([]byte(e.signature)))
}

func (e *Event) generateSignature() {
Expand Down
47 changes: 44 additions & 3 deletions abi/num.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,12 @@ func (i *intX) BitSize() int {
return i.size
}

// BitLen returns the number of bits required to represent x.
// BitLen returns the number of bits required to represent the integer.
func (i *intX) BitLen() int {
return signedBitLen(i.val)
}

// IsInt returns true if the integer can be represented as an int.
func (i *intX) IsInt() bool {
if !i.val.IsInt64() {
return false
Expand All @@ -59,6 +60,7 @@ func (i *intX) IsInt() bool {
return true
}

// Int returns the int64 representation of the integer.
func (i *intX) Int() (int, error) {
if !i.val.IsInt64() {
return 0, fmt.Errorf("abi: int overflow")
Expand All @@ -73,14 +75,15 @@ func (i *intX) Int() (int, error) {
return int(i.val.Int64()), nil
}

// Int64 returns the int64 representation of the integer.
func (i *intX) Int64() (int64, error) {
if !i.val.IsInt64() {
return 0, fmt.Errorf("abi: int64 overflow")
}
return i.val.Int64(), nil
}

// BigInt returns the value of the integer as a big integer.
// BigInt returns the *big.Int representation of the integer.
func (i *intX) BigInt() *big.Int {
return i.val
}
Expand All @@ -95,6 +98,24 @@ func (i *intX) Bytes() []byte {
return r
}

// FillBytes fills the byte slice r with the value of the integer as a
// big-endian byte slice. If the byte slice is smaller than the integer,
// an error is returned. The byte slice cannot be larger than 256 bits.
// Negative values are two's complement encoded.
func (i *intX) FillBytes(r []byte) error {
bitLen := len(r) * 8
if bitLen < i.size {
return fmt.Errorf("abi: cannot fill %d-bit integer to %d bytes", i.size, len(r))
}
if bitLen > 256 {
return fmt.Errorf("abi: cannot fill byte slices larger than 256 bits")
}
x := new(big.Int).Set(i.val).And(i.val, MaxUint[bitLen])
x.FillBytes(r)
return nil
}

// SetInt sets the value of the integer to x.
func (i *intX) SetInt(x int) error {
if bits.Len(uint(x)) > i.size {
return fmt.Errorf("abi: cannot set %d-bit integer to %d-bit int", bits.Len(uint(x)), i.size)
Expand All @@ -103,6 +124,7 @@ func (i *intX) SetInt(x int) error {
return nil
}

// SetInt64 sets the value of the integer to x.
func (i *intX) SetInt64(x int64) error {
if bits.Len64(uint64(x)) > i.size {
return fmt.Errorf("abi: cannot set %d-bit integer to %d-bit int64", bits.Len64(uint64(x)), i.size)
Expand Down Expand Up @@ -156,6 +178,7 @@ func newUintX(bitSize int) *uintX {
}
}

// Uint returns the uint representation of the integer.
func (i *uintX) Uint() (int, error) {
if !i.val.IsUint64() {
return 0, fmt.Errorf("abi: uint overflow")
Expand All @@ -167,14 +190,15 @@ func (i *uintX) Uint() (int, error) {
return int(i.val.Uint64()), nil
}

// Uint64 returns the uint64 representation of the integer.
func (i *uintX) Uint64() (uint64, error) {
if !i.val.IsUint64() {
return 0, fmt.Errorf("abi: int64 overflow")
}
return i.val.Uint64(), nil
}

// BigInt returns the value of the integer as a big integer.
// BigInt returns the *big.Int representation of the integer.
func (i *uintX) BigInt() *big.Int {
return i.val
}
Expand All @@ -188,6 +212,22 @@ func (i *uintX) Bytes() []byte {
return r
}

// FillBytes fills the byte slice r with the value of the integer as a
// big-endian byte slice. If the byte slice is smaller than the integer,
// an error is returned. The byte slice cannot be larger than 256 bits.
func (i *uintX) FillBytes(r []byte) error {
bitLen := len(r) * 8
if bitLen < i.size {
return fmt.Errorf("abi: cannot fill %d-bit integer to %d bytes", i.size, len(r))
}
if bitLen > 256 {
return fmt.Errorf("abi: cannot fill byte slices larger than 256 bits")
}
i.val.FillBytes(r)
return nil
}

// SetUint sets the value of the integer to x.
func (i *uintX) SetUint(x uint) error {
if bits.Len(x) > i.size {
return fmt.Errorf("abi: cannot set %d-bit integer to %d-bit int", bits.Len(x), i.size)
Expand All @@ -196,6 +236,7 @@ func (i *uintX) SetUint(x uint) error {
return nil
}

// SetUint64 sets the value of the integer to x.
func (i *uintX) SetUint64(x uint64) error {
if bits.Len64(x) > i.size {
return fmt.Errorf("abi: cannot set %d-bit integer to %d-bit int64", bits.Len64(x), i.size)
Expand Down
118 changes: 95 additions & 23 deletions abi/num_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,29 @@ func TestIntX_Bytes(t *testing.T) {
}
}

func TestIntX_SetIntUint(t *testing.T) {
tests := []struct {
x uint64
bitLen int
want bool
}{
{x: 0, bitLen: 8, want: true},
{x: 1, bitLen: 8, want: true},
{x: 255, bitLen: 8, want: true},
{x: 256, bitLen: 8, want: false},
{x: 0, bitLen: 32, want: true},
{x: 1, bitLen: 32, want: true},
{x: math.MaxUint32, bitLen: 32, want: true},
{x: math.MaxUint32 + 1, bitLen: 32, want: false},
{x: math.MaxUint64, bitLen: 64, want: true},
}
for n, tt := range tests {
t.Run(fmt.Sprintf("case-%d", n+1), func(t *testing.T) {
assert.Equal(t, tt.want, canSetUint(tt.x, tt.bitLen))
})
}
}

func TestIntX_SetBytes(t *testing.T) {
tests := []struct {
val *intX
Expand Down Expand Up @@ -146,6 +169,78 @@ func TestIntX_SetBytes(t *testing.T) {
}
}

func TestIntX_FillBytes(t *testing.T) {
tests := []struct {
val *intX
set *big.Int
want []byte
wantErr bool
}{
{
val: newIntX(8),
set: big.NewInt(0),
want: []byte{0x00},
},
{
val: newIntX(8),
set: big.NewInt(1),
want: []byte{0x01},
},
{
val: newIntX(8),
set: big.NewInt(-1),
want: []byte{0xff},
},
{
val: newIntX(8),
set: big.NewInt(127),
want: []byte{0x7f},
},
{
val: newIntX(8),
set: big.NewInt(-128),
want: []byte{0x80},
},
{
val: newIntX(8),
set: big.NewInt(1),
want: []byte{0x00, 0x01},
},
{
val: newIntX(8),
set: big.NewInt(-1),
want: []byte{0xff, 0xff},
},
// Too small slice
{
val: newIntX(16),
set: big.NewInt(0),
want: []byte{0x00},
wantErr: true,
},
// Too large slice
{
val: newIntX(8),
set: big.NewInt(0),
want: make([]byte, 33),
wantErr: true,
},
}
for n, tt := range tests {
t.Run(fmt.Sprintf("case-%d", n+1), func(t *testing.T) {
require.NoError(t, tt.val.SetBigInt(tt.set))
res := make([]byte, len(tt.want))
err := tt.val.FillBytes(res)
if tt.wantErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.Equal(t, tt.want, res)
}
})
}
}

func Test_signedBitLen(t *testing.T) {
tests := []struct {
arg *big.Int
Expand Down Expand Up @@ -196,29 +291,6 @@ func Test_canSetInt(t *testing.T) {
}
}

func TestIntX_SetIntUint(t *testing.T) {
tests := []struct {
x uint64
bitLen int
want bool
}{
{x: 0, bitLen: 8, want: true},
{x: 1, bitLen: 8, want: true},
{x: 255, bitLen: 8, want: true},
{x: 256, bitLen: 8, want: false},
{x: 0, bitLen: 32, want: true},
{x: 1, bitLen: 32, want: true},
{x: math.MaxUint32, bitLen: 32, want: true},
{x: math.MaxUint32 + 1, bitLen: 32, want: false},
{x: math.MaxUint64, bitLen: 64, want: true},
}
for n, tt := range tests {
t.Run(fmt.Sprintf("case-%d", n+1), func(t *testing.T) {
assert.Equal(t, tt.want, canSetUint(tt.x, tt.bitLen))
})
}
}

func bigIntStr(s string) *big.Int {
i, ok := new(big.Int).SetString(s, 0)
if !ok {
Expand Down
4 changes: 2 additions & 2 deletions abi/value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,13 +278,13 @@ func TestEncodeABI(t *testing.T) {
},
{
name: "int8#127",
val: &IntValue{Size: 256},
val: &IntValue{Size: 8},
arg: big.NewInt(127),
want: Words{padL("7f")},
},
{
name: "int8#-128",
val: &IntValue{Size: 256},
val: &IntValue{Size: 8},
arg: big.NewInt(-128),
want: Words{padR("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80")},
},
Expand Down
Loading
Loading