Skip to content

Commit

Permalink
vm: move StackItem to a separate package
Browse files Browse the repository at this point in the history
closes #912
  • Loading branch information
AnnaShaleva committed Jun 3, 2020
1 parent 46cd14a commit bc907d7
Show file tree
Hide file tree
Showing 48 changed files with 1,118 additions and 1,029 deletions.
16 changes: 9 additions & 7 deletions pkg/compiler/codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"sort"
"strings"

"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"

"github.com/nspcc-dev/neo-go/pkg/encoding/address"
"github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/vm"
Expand Down Expand Up @@ -720,7 +722,7 @@ func (c *codegen) Visit(node ast.Node) ast.Visitor {
// For now we will assume that there are only byte slice conversions.
// E.g. []byte("foobar") or []byte(scriptHash).
ast.Walk(c, n.Args[0])
c.emitConvert(vm.BufferT)
c.emitConvert(stackitem.BufferT)
return nil
}

Expand Down Expand Up @@ -1091,12 +1093,12 @@ func (c *codegen) convertBuiltin(expr *ast.CallExpr) {
c.prog.Err = errors.New("panic should have string or nil argument")
}
case "ToInteger", "ToByteArray", "ToBool":
typ := vm.IntegerT
typ := stackitem.IntegerT
switch name {
case "ToByteArray":
typ = vm.ByteArrayT
typ = stackitem.ByteArrayT
case "ToBool":
typ = vm.BooleanT
typ = stackitem.BooleanT
}
c.emitConvert(typ)
case "SHA256":
Expand All @@ -1123,7 +1125,7 @@ func (c *codegen) convertBuiltin(expr *ast.CallExpr) {
}
bytes := uint160.BytesBE()
emit.Bytes(c.prog.BinWriter, bytes)
c.emitConvert(vm.BufferT)
c.emitConvert(stackitem.BufferT)
}
}

Expand All @@ -1150,7 +1152,7 @@ func transformArgs(fun ast.Expr, args []ast.Expr) []ast.Expr {
}

// emitConvert converts top stack item to the specified type.
func (c *codegen) emitConvert(typ vm.StackItemType) {
func (c *codegen) emitConvert(typ stackitem.StackItemType) {
emit.Instruction(c.prog.BinWriter, opcode.CONVERT, []byte{byte(typ)})
}

Expand All @@ -1162,7 +1164,7 @@ func (c *codegen) convertByteArray(lit *ast.CompositeLit) {
buf[i] = byte(val)
}
emit.Bytes(c.prog.BinWriter, buf)
c.emitConvert(vm.BufferT)
c.emitConvert(stackitem.BufferT)
}

func (c *codegen) convertMap(lit *ast.CompositeLit) {
Expand Down
26 changes: 13 additions & 13 deletions pkg/compiler/for_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"math/big"
"testing"

"github.com/nspcc-dev/neo-go/pkg/vm"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
)

func TestEntryPointWithMethod(t *testing.T) {
Expand All @@ -30,7 +30,7 @@ func TestEntryPointWithArgs(t *testing.T) {
return 2 + args[1].(int)
}
`
args := []vm.StackItem{vm.NewBigIntegerItem(big.NewInt(0)), vm.NewBigIntegerItem(big.NewInt(1))}
args := []stackitem.StackItem{stackitem.NewBigIntegerItem(big.NewInt(0)), stackitem.NewBigIntegerItem(big.NewInt(1))}
evalWithArgs(t, src, nil, args, big.NewInt(3))
}

Expand All @@ -45,7 +45,7 @@ func TestEntryPointWithMethodAndArgs(t *testing.T) {
return 0
}
`
args := []vm.StackItem{vm.NewBigIntegerItem(big.NewInt(0)), vm.NewBigIntegerItem(big.NewInt(1))}
args := []stackitem.StackItem{stackitem.NewBigIntegerItem(big.NewInt(0)), stackitem.NewBigIntegerItem(big.NewInt(1))}
evalWithArgs(t, src, []byte("foobar"), args, big.NewInt(3))
}

Expand Down Expand Up @@ -134,10 +134,10 @@ func TestStringArray(t *testing.T) {
return x
}
`
eval(t, src, []vm.StackItem{
vm.NewByteArrayItem([]byte("foo")),
vm.NewByteArrayItem([]byte("bar")),
vm.NewByteArrayItem([]byte("foobar")),
eval(t, src, []stackitem.StackItem{
stackitem.NewByteArrayItem([]byte("foo")),
stackitem.NewByteArrayItem([]byte("bar")),
stackitem.NewByteArrayItem([]byte("foobar")),
})
}

Expand All @@ -149,10 +149,10 @@ func TestIntArray(t *testing.T) {
return arr
}
`
eval(t, src, []vm.StackItem{
vm.NewBigIntegerItem(big.NewInt(1)),
vm.NewBigIntegerItem(big.NewInt(2)),
vm.NewBigIntegerItem(big.NewInt(3)),
eval(t, src, []stackitem.StackItem{
stackitem.NewBigIntegerItem(big.NewInt(1)),
stackitem.NewBigIntegerItem(big.NewInt(2)),
stackitem.NewBigIntegerItem(big.NewInt(3)),
})
}

Expand Down Expand Up @@ -198,7 +198,7 @@ func TestSimpleString(t *testing.T) {
return x
}
`
eval(t, src, vm.NewByteArrayItem([]byte("NEO")).Value())
eval(t, src, stackitem.NewByteArrayItem([]byte("NEO")).Value())
}

func TestBoolAssign(t *testing.T) {
Expand Down Expand Up @@ -315,7 +315,7 @@ func TestAppendString(t *testing.T) {
return arr[3]
}
`
eval(t, src, vm.NewByteArrayItem([]byte("d")).Value())
eval(t, src, stackitem.NewByteArrayItem([]byte("d")).Value())
}

func TestAppendInt(t *testing.T) {
Expand Down
14 changes: 7 additions & 7 deletions pkg/compiler/slice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"math/big"
"testing"

"github.com/nspcc-dev/neo-go/pkg/vm"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
)

var sliceTestCases = []testCase{
Expand Down Expand Up @@ -160,9 +160,9 @@ var sliceTestCases = []testCase{
a = append(a, "b")
return a
}`,
[]vm.StackItem{
vm.NewByteArrayItem([]byte("a")),
vm.NewByteArrayItem([]byte("b")),
[]stackitem.StackItem{
stackitem.NewByteArrayItem([]byte("a")),
stackitem.NewByteArrayItem([]byte("b")),
},
},
{
Expand All @@ -175,9 +175,9 @@ var sliceTestCases = []testCase{
a = append(a, "b")
return a
}`,
[]vm.StackItem{
vm.NewByteArrayItem([]byte("a")),
vm.NewByteArrayItem([]byte("b")),
[]stackitem.StackItem{
stackitem.NewByteArrayItem([]byte("a")),
stackitem.NewByteArrayItem([]byte("b")),
},
},
{
Expand Down
12 changes: 6 additions & 6 deletions pkg/compiler/struct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"math/big"
"testing"

"github.com/nspcc-dev/neo-go/pkg/vm"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
)

var structTestCases = []testCase{
Expand Down Expand Up @@ -281,11 +281,11 @@ var structTestCases = []testCase{
return newToken()
}
`,
[]vm.StackItem{
vm.NewBigIntegerItem(big.NewInt(1)),
vm.NewBigIntegerItem(big.NewInt(2)),
vm.NewByteArrayItem([]byte("hello")),
vm.NewBoolItem(false),
[]stackitem.StackItem{
stackitem.NewBigIntegerItem(big.NewInt(1)),
stackitem.NewBigIntegerItem(big.NewInt(2)),
stackitem.NewByteArrayItem([]byte("hello")),
stackitem.NewBoolItem(false),
},
},
{
Expand Down
9 changes: 5 additions & 4 deletions pkg/compiler/syscall_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import (
"math/big"
"testing"

"github.com/nspcc-dev/neo-go/pkg/vm"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -41,8 +42,8 @@ func TestNotify(t *testing.T) {
require.NoError(t, v.Run())
require.Equal(t, 3, len(s.events))

exp0 := []vm.StackItem{vm.NewBigIntegerItem(big.NewInt(11)), vm.NewByteArrayItem([]byte("sum")), vm.NewBigIntegerItem(big.NewInt(12))}
exp0 := []stackitem.StackItem{stackitem.NewBigIntegerItem(big.NewInt(11)), stackitem.NewByteArrayItem([]byte("sum")), stackitem.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())
assert.Equal(t, []stackitem.StackItem{}, s.events[1].Value())
assert.Equal(t, []stackitem.StackItem{stackitem.NewByteArrayItem([]byte("single"))}, s.events[2].Value())
}
6 changes: 4 additions & 2 deletions pkg/compiler/vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"strings"
"testing"

"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"

"github.com/nspcc-dev/neo-go/pkg/compiler"
"github.com/nspcc-dev/neo-go/pkg/vm"
"github.com/nspcc-dev/neo-go/pkg/vm/emit"
Expand Down Expand Up @@ -38,7 +40,7 @@ func eval(t *testing.T, src string, result interface{}) {
assertResult(t, vm, result)
}

func evalWithArgs(t *testing.T, src string, op []byte, args []vm.StackItem, result interface{}) {
func evalWithArgs(t *testing.T, src string, op []byte, args []stackitem.StackItem, result interface{}) {
vm := vmAndCompile(t, src)
vm.LoadArgs(op, args)
err := vm.Run()
Expand Down Expand Up @@ -73,7 +75,7 @@ func vmAndCompileInterop(t *testing.T, src string) (*vm.VM, *storagePlugin) {
type storagePlugin struct {
mem map[string][]byte
interops map[uint32]vm.InteropFunc
events []vm.StackItem
events []stackitem.StackItem
}

func newStoragePlugin() *storagePlugin {
Expand Down
8 changes: 6 additions & 2 deletions pkg/core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import (
"sync/atomic"
"time"

"github.com/nspcc-dev/neo-go/pkg/vm/bigint"

"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"

"github.com/nspcc-dev/neo-go/pkg/config"
"github.com/nspcc-dev/neo-go/pkg/core/block"
"github.com/nspcc-dev/neo-go/pkg/core/dao"
Expand Down Expand Up @@ -745,7 +749,7 @@ func (bc *Blockchain) storeBlock(block *block.Block) error {
return errors.Wrap(err, "failed to persist invocation results")
}
for _, note := range systemInterop.Notifications {
arr, ok := note.Item.Value().([]vm.StackItem)
arr, ok := note.Item.Value().([]stackitem.StackItem)
if !ok || len(arr) != 4 {
continue
}
Expand Down Expand Up @@ -777,7 +781,7 @@ func (bc *Blockchain) storeBlock(block *block.Block) error {
if !ok {
continue
}
amount = emit.BytesToInt(bs)
amount = bigint.BytesToInt(bs)
}
bc.processNEP5Transfer(cache, tx, block, note.ScriptHash, from, to, amount.Int64())
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/core/interop/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neo-go/pkg/vm"
"github.com/nspcc-dev/neo-go/pkg/vm/emit"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -60,7 +61,7 @@ type Function struct {
}

// Method is a signature for a native method.
type Method = func(ic *Context, args []vm.StackItem) vm.StackItem
type Method = func(ic *Context, args []stackitem.StackItem) stackitem.StackItem

// MethodAndPrice is a native-contract method descriptor.
type MethodAndPrice struct {
Expand Down
8 changes: 5 additions & 3 deletions pkg/core/interop/crypto/ecdsa.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"errors"
"fmt"

"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"

"github.com/nspcc-dev/neo-go/pkg/core/interop"
"github.com/nspcc-dev/neo-go/pkg/crypto"
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
Expand Down Expand Up @@ -48,12 +50,12 @@ func ECDSACheckMultisig(ic *interop.Context, v *vm.VM) error {
return nil
}

func getMessage(ic *interop.Context, item vm.StackItem) []byte {
func getMessage(ic *interop.Context, item stackitem.StackItem) []byte {
var msg []byte
switch val := item.(type) {
case *vm.InteropItem:
case *stackitem.InteropItem:
msg = val.Value().(crypto.Verifiable).GetSignedPart()
case vm.NullItem:
case stackitem.NullItem:
msg = ic.Container.GetSignedPart()
default:
var err error
Expand Down
16 changes: 9 additions & 7 deletions pkg/core/interop/crypto/ecdsa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"encoding/binary"
"testing"

"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"

"github.com/nspcc-dev/neo-go/pkg/core/interop"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/trigger"
Expand All @@ -13,12 +15,12 @@ import (
"github.com/stretchr/testify/require"
)

func initCHECKMULTISIG(msg []byte, n int) ([]vm.StackItem, []vm.StackItem, map[string]*keys.PublicKey, error) {
func initCHECKMULTISIG(msg []byte, n int) ([]stackitem.StackItem, []stackitem.StackItem, map[string]*keys.PublicKey, error) {
var err error

keyMap := make(map[string]*keys.PublicKey)
pkeys := make([]*keys.PrivateKey, n)
pubs := make([]vm.StackItem, n)
pubs := make([]stackitem.StackItem, n)
for i := range pubs {
pkeys[i], err = keys.NewPrivateKey()
if err != nil {
Expand All @@ -27,25 +29,25 @@ func initCHECKMULTISIG(msg []byte, n int) ([]vm.StackItem, []vm.StackItem, map[s

pk := pkeys[i].PublicKey()
data := pk.Bytes()
pubs[i] = vm.NewByteArrayItem(data)
pubs[i] = stackitem.NewByteArrayItem(data)
keyMap[string(data)] = pk
}

sigs := make([]vm.StackItem, n)
sigs := make([]stackitem.StackItem, n)
for i := range sigs {
sig := pkeys[i].Sign(msg)
sigs[i] = vm.NewByteArrayItem(sig)
sigs[i] = stackitem.NewByteArrayItem(sig)
}

return pubs, sigs, keyMap, nil
}

func subSlice(arr []vm.StackItem, indices []int) []vm.StackItem {
func subSlice(arr []stackitem.StackItem, indices []int) []stackitem.StackItem {
if indices == nil {
return arr
}

result := make([]vm.StackItem, len(indices))
result := make([]stackitem.StackItem, len(indices))
for i, j := range indices {
result[i] = arr[j]
}
Expand Down
Loading

0 comments on commit bc907d7

Please sign in to comment.