Skip to content

Commit

Permalink
[fix] Handle negative big ints correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
cedric-cordenier committed Jun 6, 2024
1 parent e746659 commit 1f37dda
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 54 deletions.
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;
BigInt bigint_value = 9;
}
}

message BigInt {
bytes abs_val = 1;
int64 sign = 2;
}

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

0 comments on commit 1f37dda

Please sign in to comment.