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

[fix] Handle negative big ints correctly #560

Merged
merged 1 commit into from
Jun 6, 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
5 changes: 4 additions & 1 deletion pkg/values/big_int.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ func NewBigInt(b *big.Int) *BigInt {
}

func (b *BigInt) proto() *pb.Value {
return pb.NewBigIntValue(b.Underlying.Bytes())
return pb.NewBigIntValue(
b.Underlying.Sign(),
b.Underlying.Bytes(),
)
}

func (b *BigInt) Unwrap() (any, error) {
Expand Down
31 changes: 31 additions & 0 deletions pkg/values/big_int_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,34 @@ func Test_BigIntUnwrapTo(t *testing.T) {
err = v.UnwrapTo(&varStr)
assert.ErrorContains(t, err, "cannot unwrap to value of type: *string")
}

func Test_BigInt(t *testing.T) {
testCases := []struct {
name string
bi *big.Int
}{
{
name: "positive",
bi: big.NewInt(100),
},
{
name: "0",
bi: big.NewInt(0),
},
{
name: "negative",
bi: big.NewInt(-1),
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
v := NewBigInt(tc.bi)

vp := Proto(v)
got := FromProto(vp)

assert.Equal(t, tc.bi, got.(*BigInt).Underlying)
})
}
}
7 changes: 5 additions & 2 deletions pkg/values/pb/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,13 @@ func NewInt64Value(i int64) *Value {
}
}

func NewBigIntValue(bib []byte) *Value {
func NewBigIntValue(sign int, bib []byte) *Value {
return &Value{
Value: &Value_BigintValue{
BigintValue: bib,
BigintValue: &BigInt{
AbsVal: bib,
Sign: int64(sign),
},
},
}
}
166 changes: 120 additions & 46 deletions pkg/values/pb/values.pb.go

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

8 changes: 7 additions & 1 deletion pkg/values/pb/values.proto
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ option go_package = "github.com/smartcontractkit/chainlink-common/pkg/values/pb"
package values;

message Value {
reserved 8;
oneof value {
string string_value = 1;
bool bool_value = 2;
Expand All @@ -13,10 +14,15 @@ message Value {
List list_value = 5;
string decimal_value = 6;
int64 int64_value = 7;
bytes bigint_value = 8;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there is a formal way to reserve or deprecate this field

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added that on line 8 or did you have something else in mind?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I just expected it to be inline here inside of the oneof - does it matter?

Copy link
Collaborator

@jmank88 jmank88 Jun 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These is also this option:

bytes bigint_value = 8 [deprecated = true];

BigInt bigint_value = 9;
}
}

message BigInt {
bytes abs_val = 1;
int64 sign = 2;
}
jmank88 marked this conversation as resolved.
Show resolved Hide resolved

message Map {
map<string, Value> fields = 1;
}
Expand Down
13 changes: 9 additions & 4 deletions pkg/values/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,15 @@ func fromDecimalValueProto(decStr string) *Decimal {
return NewDecimal(dec)
}

func fromBigIntValueProto(b []byte) *BigInt {
i := big.Int{}
bi := i.SetBytes(b)
return NewBigInt(bi)
func fromBigIntValueProto(biv *pb.BigInt) *BigInt {
av := &big.Int{}
av = av.SetBytes(biv.AbsVal)

if biv.Sign < 0 {
av.Neg(av)
}

return NewBigInt(av)
}

func createMapFromStruct(v any) (Value, error) {
Expand Down
Loading