Skip to content
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

Refactor "codecForBigInt" into "BigIntValue" and "BigUIntValue" #12

Merged
merged 1 commit into from
May 29, 2024
Merged
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
68 changes: 68 additions & 0 deletions abi/bigIntValue.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package abi

import (
"io"
"math/big"

twos "github.com/multiversx/mx-components-big-int/twos-complement"
)

// BigIntValue is a wrapper for a big integer (signed)
type BigIntValue struct {
Value *big.Int
}

// EncodeNested encodes the value in the nested form
func (value *BigIntValue) EncodeNested(writer io.Writer) error {
data := twos.ToBytes(value.Value)
dataLength := len(data)

// Write the length of the payload
err := encodeLength(writer, uint32(dataLength))
if err != nil {
return err
}

// Write the payload
_, err = writer.Write(data)
if err != nil {
return err
}

return nil
}

// EncodeTopLevel encodes the value in the top-level form
func (value *BigIntValue) EncodeTopLevel(writer io.Writer) error {
data := twos.ToBytes(value.Value)
_, err := writer.Write(data)
if err != nil {
return err
}

return nil
}

// DecodeNested decodes the value from the nested form
func (value *BigIntValue) DecodeNested(reader io.Reader) error {
// Read the length of the payload
length, err := decodeLength(reader)
if err != nil {
return err
}

// Read the payload
data, err := readBytesExactly(reader, int(length))
if err != nil {
return err
}

value.Value = twos.FromBytes(data)
return nil
}

// DecodeTopLevel decodes the value from the top-level form
func (value *BigIntValue) DecodeTopLevel(data []byte) error {
value.Value = twos.FromBytes(data)
return nil
}
54 changes: 54 additions & 0 deletions abi/bigIntValue_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package abi

import (
"math/big"
"testing"
)

func TestBigIntValue(t *testing.T) {
codec := &codec{}

t.Run("should encode nested", func(t *testing.T) {
testEncodeNested(t, codec, &BigIntValue{Value: big.NewInt(0)}, "00000000")
testEncodeNested(t, codec, &BigIntValue{Value: big.NewInt(1)}, "0000000101")
testEncodeNested(t, codec, &BigIntValue{Value: big.NewInt(-1)}, "00000001ff")
testEncodeNested(t, codec, &BigIntValue{Value: big.NewInt(127)}, "000000017f")
testEncodeNested(t, codec, &BigIntValue{Value: big.NewInt(128)}, "000000020080")
testEncodeNested(t, codec, &BigIntValue{Value: big.NewInt(255)}, "0000000200ff")
testEncodeNested(t, codec, &BigIntValue{Value: big.NewInt(256)}, "000000020100")
})

t.Run("should encode top-level", func(t *testing.T) {
testEncodeTopLevel(t, codec, &BigIntValue{Value: big.NewInt(0)}, "")
testEncodeTopLevel(t, codec, &BigIntValue{Value: big.NewInt(1)}, "01")
testEncodeTopLevel(t, codec, &BigIntValue{Value: big.NewInt(-1)}, "ff")
testEncodeTopLevel(t, codec, &BigIntValue{Value: big.NewInt(127)}, "7f")
testEncodeTopLevel(t, codec, &BigIntValue{Value: big.NewInt(128)}, "0080")
testEncodeTopLevel(t, codec, &BigIntValue{Value: big.NewInt(255)}, "00ff")
testEncodeTopLevel(t, codec, &BigIntValue{Value: big.NewInt(256)}, "0100")
})

t.Run("should decode nested", func(t *testing.T) {
testDecodeNested(t, codec, "00000000", &BigIntValue{}, &BigIntValue{Value: big.NewInt(0)})
testDecodeNested(t, codec, "0000000101", &BigIntValue{}, &BigIntValue{Value: big.NewInt(1)})
testDecodeNested(t, codec, "00000001ff", &BigIntValue{}, &BigIntValue{Value: big.NewInt(-1)})
testDecodeNested(t, codec, "000000017f", &BigIntValue{}, &BigIntValue{Value: big.NewInt(127)})
testDecodeNested(t, codec, "000000020080", &BigIntValue{}, &BigIntValue{Value: big.NewInt(128)})
testDecodeNested(t, codec, "0000000200ff", &BigIntValue{}, &BigIntValue{Value: big.NewInt(255)})
testDecodeNested(t, codec, "000000020100", &BigIntValue{}, &BigIntValue{Value: big.NewInt(256)})
})

t.Run("should err on decode nested", func(t *testing.T) {
testDecodeNestedWithError(t, codec, "0000000301", &BigIntValue{}, "cannot decode (nested) *abi.BigIntValue, because of: cannot read exactly 3 bytes")
})

t.Run("should decode top-level", func(t *testing.T) {
testDecodeTopLevel(t, codec, "", &BigIntValue{}, &BigIntValue{Value: big.NewInt(0)})
testDecodeTopLevel(t, codec, "01", &BigIntValue{}, &BigIntValue{Value: big.NewInt(1)})
testDecodeTopLevel(t, codec, "ff", &BigIntValue{}, &BigIntValue{Value: big.NewInt(-1)})
testDecodeTopLevel(t, codec, "7f", &BigIntValue{}, &BigIntValue{Value: big.NewInt(127)})
testDecodeTopLevel(t, codec, "0080", &BigIntValue{}, &BigIntValue{Value: big.NewInt(128)})
testDecodeTopLevel(t, codec, "00ff", &BigIntValue{}, &BigIntValue{Value: big.NewInt(255)})
testDecodeTopLevel(t, codec, "0100", &BigIntValue{}, &BigIntValue{Value: big.NewInt(256)})
})
}
66 changes: 66 additions & 0 deletions abi/bigUIntValue.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package abi

import (
"io"
"math/big"
)

// BigUIntValue is a wrapper for a big integer (unsigned)
type BigUIntValue struct {
Value *big.Int
}

// EncodeNested encodes the value in the nested form
func (value *BigUIntValue) EncodeNested(writer io.Writer) error {
data := value.Value.Bytes()
dataLength := len(data)

// Write the length of the payload
err := encodeLength(writer, uint32(dataLength))
if err != nil {
return err
}

// Write the payload
_, err = writer.Write(data)
if err != nil {
return err
}

return nil
}

// EncodeTopLevel encodes the value in the top-level form
func (value *BigUIntValue) EncodeTopLevel(writer io.Writer) error {
data := value.Value.Bytes()
_, err := writer.Write(data)
if err != nil {
return err
}

return nil
}

// DecodeNested decodes the value from the nested form
func (value *BigUIntValue) DecodeNested(reader io.Reader) error {
// Read the length of the payload
length, err := decodeLength(reader)
if err != nil {
return err
}

// Read the payload
data, err := readBytesExactly(reader, int(length))
if err != nil {
return err
}

value.Value = big.NewInt(0).SetBytes(data)
return nil
}

// DecodeTopLevel decodes the value from the top-level form
func (value *BigUIntValue) DecodeTopLevel(data []byte) error {
value.Value = big.NewInt(0).SetBytes(data)
return nil
}
48 changes: 48 additions & 0 deletions abi/bigUIntValue_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package abi

import (
"math/big"
"testing"
)

func TestBigUIntValue(t *testing.T) {
codec := &codec{}

t.Run("should encode nested", func(t *testing.T) {
testEncodeNested(t, codec, &BigUIntValue{Value: big.NewInt(0)}, "00000000")
testEncodeNested(t, codec, &BigUIntValue{Value: big.NewInt(1)}, "0000000101")
testEncodeNested(t, codec, &BigUIntValue{Value: big.NewInt(127)}, "000000017f")
testEncodeNested(t, codec, &BigUIntValue{Value: big.NewInt(128)}, "0000000180")
testEncodeNested(t, codec, &BigUIntValue{Value: big.NewInt(255)}, "00000001ff")
testEncodeNested(t, codec, &BigUIntValue{Value: big.NewInt(256)}, "000000020100")
})

t.Run("should encode top-level", func(t *testing.T) {
testEncodeTopLevel(t, codec, &BigUIntValue{Value: big.NewInt(0)}, "")
testEncodeTopLevel(t, codec, &BigUIntValue{Value: big.NewInt(1)}, "01")
testEncodeTopLevel(t, codec, &BigUIntValue{Value: big.NewInt(127)}, "7f")
testEncodeTopLevel(t, codec, &BigUIntValue{Value: big.NewInt(128)}, "80")
testEncodeTopLevel(t, codec, &BigUIntValue{Value: big.NewInt(256)}, "0100")
})

t.Run("should decode nested", func(t *testing.T) {
testDecodeNested(t, codec, "00000000", &BigUIntValue{}, &BigUIntValue{Value: big.NewInt(0)})
testDecodeNested(t, codec, "0000000101", &BigUIntValue{}, &BigUIntValue{Value: big.NewInt(1)})
testDecodeNested(t, codec, "000000017f", &BigUIntValue{}, &BigUIntValue{Value: big.NewInt(127)})
testDecodeNested(t, codec, "0000000180", &BigUIntValue{}, &BigUIntValue{Value: big.NewInt(128)})
testDecodeNested(t, codec, "00000001ff", &BigUIntValue{}, &BigUIntValue{Value: big.NewInt(255)})
testDecodeNested(t, codec, "000000020100", &BigUIntValue{}, &BigUIntValue{Value: big.NewInt(256)})
})

t.Run("should err on decode nested", func(t *testing.T) {
testDecodeNestedWithError(t, codec, "0000000301", &BigUIntValue{}, "cannot decode (nested) *abi.BigUIntValue, because of: cannot read exactly 3 bytes")
})

t.Run("should decode top-level", func(t *testing.T) {
testDecodeTopLevel(t, codec, "", &BigUIntValue{}, &BigUIntValue{Value: big.NewInt(0)})
testDecodeTopLevel(t, codec, "01", &BigUIntValue{}, &BigUIntValue{Value: big.NewInt(1)})
testDecodeTopLevel(t, codec, "7f", &BigUIntValue{}, &BigUIntValue{Value: big.NewInt(127)})
testDecodeTopLevel(t, codec, "80", &BigUIntValue{}, &BigUIntValue{Value: big.NewInt(128)})
testDecodeTopLevel(t, codec, "0100", &BigUIntValue{}, &BigUIntValue{Value: big.NewInt(256)})
})
}
109 changes: 0 additions & 109 deletions abi/codecForBigInt.go

This file was deleted.

Loading