From e3c060b7392618c814ed9c338cd72f993fbc02ed Mon Sep 17 00:00:00 2001 From: Alex Vanin Date: Wed, 28 Oct 2020 15:59:40 +0300 Subject: [PATCH] [#122] Add BigIntFromStackItem Neo-go can return big.Int values on stack after contract execution. Signed-off-by: Alex Vanin --- pkg/morph/client/util.go | 6 ++++++ pkg/morph/client/util_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/pkg/morph/client/util.go b/pkg/morph/client/util.go index 59f60830..4066472e 100644 --- a/pkg/morph/client/util.go +++ b/pkg/morph/client/util.go @@ -2,6 +2,7 @@ package client import ( "encoding/binary" + "math/big" sc "github.com/nspcc-dev/neo-go/pkg/smartcontract" "github.com/nspcc-dev/neo-go/pkg/vm/stackitem" @@ -157,6 +158,11 @@ func IntFromStackItem(param stackitem.Item) (int64, error) { } } +// BigIntFromStackItem receives numerical value from the value of a smart contract parameter. +func BigIntFromStackItem(param stackitem.Item) (*big.Int, error) { + return param.TryInteger() +} + // BytesFromStackItem receives binary value from the value of a smart contract parameter. func BytesFromStackItem(param stackitem.Item) ([]byte, error) { switch param.Type() { diff --git a/pkg/morph/client/util_test.go b/pkg/morph/client/util_test.go index 8b0eff8f..9d73376a 100644 --- a/pkg/morph/client/util_test.go +++ b/pkg/morph/client/util_test.go @@ -1,9 +1,11 @@ package client import ( + "math" "math/big" "testing" + "github.com/nspcc-dev/neo-go/pkg/encoding/bigint" sc "github.com/nspcc-dev/neo-go/pkg/smartcontract" "github.com/nspcc-dev/neo-go/pkg/vm/stackitem" "github.com/stretchr/testify/require" @@ -50,9 +52,13 @@ var ( Value: []sc.Parameter{intParam, byteArrayParam}, } + bigIntValue = new(big.Int).Mul(big.NewInt(math.MaxInt64), big.NewInt(10)) + stringByteItem = stackitem.NewByteArray([]byte("Hello World")) intItem = stackitem.NewBigInteger(new(big.Int).SetInt64(1)) + bigIntItem = stackitem.NewBigInteger(bigIntValue) byteWithIntItem = stackitem.NewByteArray([]byte{0x0a}) + byteWithBigIntItem = stackitem.NewByteArray(bigint.ToBytes(bigIntValue)) emptyByteArrayItem = stackitem.NewByteArray([]byte{}) trueBoolItem = stackitem.NewBool(true) falseBoolItem = stackitem.NewBool(false) @@ -233,6 +239,27 @@ func TestIntFromStackItem(t *testing.T) { }) } +func TestBigIntFromStackItem(t *testing.T) { + t.Run("correct assert", func(t *testing.T) { + val, err := BigIntFromStackItem(bigIntItem) + require.NoError(t, err) + require.Equal(t, bigIntValue, val) + + val, err = BigIntFromStackItem(byteWithBigIntItem) + require.NoError(t, err) + require.Equal(t, bigIntValue, val) + + val, err = BigIntFromStackItem(emptyByteArrayItem) + require.NoError(t, err) + require.Equal(t, big.NewInt(0), val) + }) + + t.Run("incorrect assert", func(t *testing.T) { + _, err := BigIntFromStackItem(arrayItem) + require.Error(t, err) + }) +} + func TestStringFromStackItem(t *testing.T) { t.Run("correct assert", func(t *testing.T) { val, err := StringFromStackItem(stringByteItem)