forked from TrueCloudLab/frostfs-node
[#122] Add BigIntFromStackItem
Neo-go can return big.Int values on stack after contract execution. Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
parent
330c4b42d9
commit
e3c060b739
2 changed files with 33 additions and 0 deletions
|
@ -2,6 +2,7 @@ package client
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
|
"math/big"
|
||||||
|
|
||||||
sc "github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
sc "github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
"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.
|
// BytesFromStackItem receives binary value from the value of a smart contract parameter.
|
||||||
func BytesFromStackItem(param stackitem.Item) ([]byte, error) {
|
func BytesFromStackItem(param stackitem.Item) ([]byte, error) {
|
||||||
switch param.Type() {
|
switch param.Type() {
|
||||||
|
|
|
@ -1,9 +1,11 @@
|
||||||
package client
|
package client
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"math"
|
||||||
"math/big"
|
"math/big"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/nspcc-dev/neo-go/pkg/encoding/bigint"
|
||||||
sc "github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
sc "github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
@ -50,9 +52,13 @@ var (
|
||||||
Value: []sc.Parameter{intParam, byteArrayParam},
|
Value: []sc.Parameter{intParam, byteArrayParam},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bigIntValue = new(big.Int).Mul(big.NewInt(math.MaxInt64), big.NewInt(10))
|
||||||
|
|
||||||
stringByteItem = stackitem.NewByteArray([]byte("Hello World"))
|
stringByteItem = stackitem.NewByteArray([]byte("Hello World"))
|
||||||
intItem = stackitem.NewBigInteger(new(big.Int).SetInt64(1))
|
intItem = stackitem.NewBigInteger(new(big.Int).SetInt64(1))
|
||||||
|
bigIntItem = stackitem.NewBigInteger(bigIntValue)
|
||||||
byteWithIntItem = stackitem.NewByteArray([]byte{0x0a})
|
byteWithIntItem = stackitem.NewByteArray([]byte{0x0a})
|
||||||
|
byteWithBigIntItem = stackitem.NewByteArray(bigint.ToBytes(bigIntValue))
|
||||||
emptyByteArrayItem = stackitem.NewByteArray([]byte{})
|
emptyByteArrayItem = stackitem.NewByteArray([]byte{})
|
||||||
trueBoolItem = stackitem.NewBool(true)
|
trueBoolItem = stackitem.NewBool(true)
|
||||||
falseBoolItem = stackitem.NewBool(false)
|
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) {
|
func TestStringFromStackItem(t *testing.T) {
|
||||||
t.Run("correct assert", func(t *testing.T) {
|
t.Run("correct assert", func(t *testing.T) {
|
||||||
val, err := StringFromStackItem(stringByteItem)
|
val, err := StringFromStackItem(stringByteItem)
|
||||||
|
|
Loading…
Reference in a new issue