vm: make NewBigInteger accept *big.Int

It creates big.Int internally anyway, so this is the most flexible way.
This commit is contained in:
Evgenii Stratonikov 2020-04-16 15:28:34 +03:00
parent 9586af32f2
commit 3831aec53f
7 changed files with 45 additions and 43 deletions

View file

@ -34,7 +34,7 @@ func TestEntryPointWithArgs(t *testing.T) {
return 2 + args[1].(int)
}
`
args := []vm.StackItem{vm.NewBigIntegerItem(0), vm.NewBigIntegerItem(1)}
args := []vm.StackItem{vm.NewBigIntegerItem(big.NewInt(0)), vm.NewBigIntegerItem(big.NewInt(1))}
evalWithArgs(t, src, nil, args, big.NewInt(3))
}
@ -49,7 +49,7 @@ func TestEntryPointWithMethodAndArgs(t *testing.T) {
return 0
}
`
args := []vm.StackItem{vm.NewBigIntegerItem(0), vm.NewBigIntegerItem(1)}
args := []vm.StackItem{vm.NewBigIntegerItem(big.NewInt(0)), vm.NewBigIntegerItem(big.NewInt(1))}
evalWithArgs(t, src, []byte("foobar"), args, big.NewInt(3))
}
@ -154,9 +154,9 @@ func TestIntArray(t *testing.T) {
}
`
eval(t, src, []vm.StackItem{
vm.NewBigIntegerItem(1),
vm.NewBigIntegerItem(2),
vm.NewBigIntegerItem(3),
vm.NewBigIntegerItem(big.NewInt(1)),
vm.NewBigIntegerItem(big.NewInt(2)),
vm.NewBigIntegerItem(big.NewInt(3)),
})
}

View file

@ -271,8 +271,8 @@ var structTestCases = []testCase{
}
`,
[]vm.StackItem{
vm.NewBigIntegerItem(1),
vm.NewBigIntegerItem(2),
vm.NewBigIntegerItem(big.NewInt(1)),
vm.NewBigIntegerItem(big.NewInt(2)),
vm.NewByteArrayItem([]byte("hello")),
vm.NewByteArrayItem([]byte{}),
},

View file

@ -1,6 +1,7 @@
package compiler_test
import (
"math/big"
"testing"
"github.com/nspcc-dev/neo-go/pkg/vm"
@ -40,7 +41,7 @@ func TestNotify(t *testing.T) {
require.NoError(t, v.Run())
require.Equal(t, 3, len(s.events))
exp0 := []vm.StackItem{vm.NewBigIntegerItem(11), vm.NewByteArrayItem([]byte("sum")), vm.NewBigIntegerItem(12)}
exp0 := []vm.StackItem{vm.NewBigIntegerItem(big.NewInt(11)), vm.NewByteArrayItem([]byte("sum")), vm.NewBigIntegerItem(big.NewInt(12))}
assert.Equal(t, exp0, s.events[0].Value())
assert.Equal(t, []vm.StackItem{}, s.events[1].Value())
assert.Equal(t, []vm.StackItem{vm.NewByteArrayItem([]byte("single"))}, s.events[2].Value())

View file

@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"io/ioutil"
"math/big"
"os"
"strconv"
"strings"
@ -438,7 +439,7 @@ func parseArgs(args []string) ([]vm.StackItem, error) {
if err != nil {
return nil, err
}
items[i] = vm.NewBigIntegerItem(val)
items[i] = vm.NewBigIntegerItem(big.NewInt(val))
case stringType:
items[i] = vm.NewByteArrayItem([]byte(value))
}

View file

@ -241,9 +241,9 @@ type BigIntegerItem struct {
}
// NewBigIntegerItem returns an new BigIntegerItem object.
func NewBigIntegerItem(value int64) *BigIntegerItem {
func NewBigIntegerItem(value *big.Int) *BigIntegerItem {
return &BigIntegerItem{
value: big.NewInt(value),
value: value,
}
}

View file

@ -104,7 +104,7 @@ var stringerTestCases = []struct {
result: "Struct",
},
{
input: NewBigIntegerItem(3),
input: NewBigIntegerItem(big.NewInt(3)),
result: "BigInteger",
},
{
@ -148,48 +148,48 @@ var equalsTestCases = map[string][]struct {
},
{
item1: NewStructItem(nil),
item2: NewBigIntegerItem(1),
item2: NewBigIntegerItem(big.NewInt(1)),
result: false,
},
{
item1: NewStructItem(nil),
item2: NewStructItem([]StackItem{NewBigIntegerItem(1)}),
item2: NewStructItem([]StackItem{NewBigIntegerItem(big.NewInt(1))}),
result: false,
},
{
item1: NewStructItem([]StackItem{NewBigIntegerItem(1)}),
item2: NewStructItem([]StackItem{NewBigIntegerItem(2)}),
item1: NewStructItem([]StackItem{NewBigIntegerItem(big.NewInt(1))}),
item2: NewStructItem([]StackItem{NewBigIntegerItem(big.NewInt(2))}),
result: false,
},
{
item1: NewStructItem([]StackItem{NewBigIntegerItem(1)}),
item2: NewStructItem([]StackItem{NewBigIntegerItem(1)}),
item1: NewStructItem([]StackItem{NewBigIntegerItem(big.NewInt(1))}),
item2: NewStructItem([]StackItem{NewBigIntegerItem(big.NewInt(1))}),
result: true,
},
},
"bigint": {
{
item1: NewBigIntegerItem(2),
item1: NewBigIntegerItem(big.NewInt(2)),
item2: nil,
result: false,
},
{
item1: NewBigIntegerItem(2),
item2: NewBigIntegerItem(2),
item1: NewBigIntegerItem(big.NewInt(2)),
item2: NewBigIntegerItem(big.NewInt(2)),
result: true,
},
{
item1: NewBigIntegerItem(2),
item1: NewBigIntegerItem(big.NewInt(2)),
item2: NewBoolItem(false),
result: false,
},
{
item1: NewBigIntegerItem(0),
item1: NewBigIntegerItem(big.NewInt(0)),
item2: NewBoolItem(false),
result: false,
},
{
item1: NewBigIntegerItem(2),
item1: NewBigIntegerItem(big.NewInt(2)),
item2: makeStackItem(int32(2)),
result: true,
},
@ -207,7 +207,7 @@ var equalsTestCases = map[string][]struct {
},
{
item1: NewBoolItem(true),
item2: NewBigIntegerItem(1),
item2: NewBigIntegerItem(big.NewInt(1)),
result: true,
},
{
@ -234,7 +234,7 @@ var equalsTestCases = map[string][]struct {
},
{
item1: NewByteArrayItem([]byte{1}),
item2: NewBigIntegerItem(1),
item2: NewBigIntegerItem(big.NewInt(1)),
result: true,
},
{
@ -261,7 +261,7 @@ var equalsTestCases = map[string][]struct {
},
{
item1: NewArrayItem([]StackItem{&BigIntegerItem{big.NewInt(1)}}),
item2: NewBigIntegerItem(1),
item2: NewBigIntegerItem(big.NewInt(1)),
result: false,
},
{
@ -282,13 +282,13 @@ var equalsTestCases = map[string][]struct {
result: false,
},
{
item1: &MapItem{value: []MapElement{{NewByteArrayItem([]byte("first")), NewBigIntegerItem(1)}, {NewBoolItem(true), NewByteArrayItem([]byte{2})}}},
item2: &MapItem{value: []MapElement{{NewByteArrayItem([]byte("first")), NewBigIntegerItem(1)}, {NewBoolItem(true), NewByteArrayItem([]byte{2})}}},
item1: &MapItem{value: []MapElement{{NewByteArrayItem([]byte("first")), NewBigIntegerItem(big.NewInt(1))}, {NewBoolItem(true), NewByteArrayItem([]byte{2})}}},
item2: &MapItem{value: []MapElement{{NewByteArrayItem([]byte("first")), NewBigIntegerItem(big.NewInt(1))}, {NewBoolItem(true), NewByteArrayItem([]byte{2})}}},
result: false,
},
{
item1: &MapItem{value: []MapElement{{NewByteArrayItem([]byte("first")), NewBigIntegerItem(1)}, {NewBoolItem(true), NewByteArrayItem([]byte{2})}}},
item2: &MapItem{value: []MapElement{{NewByteArrayItem([]byte("first")), NewBigIntegerItem(1)}, {NewBoolItem(true), NewByteArrayItem([]byte{3})}}},
item1: &MapItem{value: []MapElement{{NewByteArrayItem([]byte("first")), NewBigIntegerItem(big.NewInt(1))}, {NewBoolItem(true), NewByteArrayItem([]byte{2})}}},
item2: &MapItem{value: []MapElement{{NewByteArrayItem([]byte("first")), NewBigIntegerItem(big.NewInt(1))}, {NewBoolItem(true), NewByteArrayItem([]byte{3})}}},
result: false,
},
},
@ -333,7 +333,7 @@ var marshalJSONTestCases = []struct {
result []byte
}{
{
input: NewBigIntegerItem(2),
input: NewBigIntegerItem(big.NewInt(2)),
result: []byte(`2`),
},
{
@ -386,7 +386,7 @@ var toContractParameterTestCases = []struct {
}{
{
input: NewStructItem([]StackItem{
NewBigIntegerItem(1),
NewBigIntegerItem(big.NewInt(1)),
NewBoolItem(true),
}),
result: smartcontract.Parameter{Type: smartcontract.ArrayType, Value: []smartcontract.Parameter{
@ -403,7 +403,7 @@ var toContractParameterTestCases = []struct {
result: smartcontract.Parameter{Type: smartcontract.ByteArrayType, Value: []byte{0x01, 0x02, 0x03}},
},
{
input: NewArrayItem([]StackItem{NewBigIntegerItem(2), NewBoolItem(true)}),
input: NewArrayItem([]StackItem{NewBigIntegerItem(big.NewInt(2)), NewBoolItem(true)}),
result: smartcontract.Parameter{Type: smartcontract.ArrayType, Value: []smartcontract.Parameter{
{Type: smartcontract.IntegerType, Value: int64(2)},
{Type: smartcontract.BoolType, Value: true},
@ -415,8 +415,8 @@ var toContractParameterTestCases = []struct {
},
{
input: &MapItem{value: []MapElement{
{NewBigIntegerItem(1), NewBoolItem(true)},
{NewByteArrayItem([]byte("qwerty")), NewBigIntegerItem(3)},
{NewBigIntegerItem(big.NewInt(1)), NewBoolItem(true)},
{NewByteArrayItem([]byte("qwerty")), NewBigIntegerItem(big.NewInt(3))},
{NewBoolItem(true), NewBoolItem(false)},
}},
result: smartcontract.Parameter{

View file

@ -441,7 +441,7 @@ func testIterableCreate(t *testing.T, typ string) {
vm := load(prog)
arr := []StackItem{
NewBigIntegerItem(42),
NewBigIntegerItem(big.NewInt(42)),
NewByteArrayItem([]byte{3, 2, 1}),
}
vm.estack.Push(&Element{value: NewArrayItem(arr)})
@ -479,7 +479,7 @@ func testIterableConcat(t *testing.T, typ string) {
arr := []StackItem{
NewBoolItem(false),
NewBigIntegerItem(123),
NewBigIntegerItem(big.NewInt(123)),
NewMapItem(),
}
vm.estack.Push(&Element{value: NewArrayItem(arr[:1])})
@ -521,15 +521,15 @@ func TestIteratorKeys(t *testing.T) {
v := load(prog)
arr := NewArrayItem([]StackItem{
NewBoolItem(false),
NewBigIntegerItem(42),
NewBigIntegerItem(big.NewInt(42)),
})
v.estack.PushVal(arr)
runVM(t, v)
checkEnumeratorStack(t, v, []StackItem{
NewBigIntegerItem(1), NewBoolItem(true),
NewBigIntegerItem(0), NewBoolItem(true),
NewBigIntegerItem(big.NewInt(1)), NewBoolItem(true),
NewBigIntegerItem(big.NewInt(0)), NewBoolItem(true),
})
}
@ -541,7 +541,7 @@ func TestIteratorValues(t *testing.T) {
v := load(prog)
m := NewMapItem()
m.Add(NewBigIntegerItem(1), NewBoolItem(false))
m.Add(NewBigIntegerItem(big.NewInt(1)), NewBoolItem(false))
m.Add(NewByteArrayItem([]byte{32}), NewByteArrayItem([]byte{7}))
v.estack.PushVal(m)
@ -680,7 +680,7 @@ func TestDeserializeUnknown(t *testing.T) {
prog := append(getSyscallProg("Neo.Runtime.Deserialize"), byte(opcode.RET))
vm := load(prog)
data, err := SerializeItem(NewBigIntegerItem(123))
data, err := SerializeItem(NewBigIntegerItem(big.NewInt(123)))
require.NoError(t, err)
data[0] = 0xFF