mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-12-23 03:41:34 +00:00
- Add test for math Add
opcode
- basic opcode execution
This commit is contained in:
parent
c7fb4c3bdf
commit
baf9d2b768
3 changed files with 87 additions and 0 deletions
10
pkg/vm/state.go
Normal file
10
pkg/vm/state.go
Normal file
|
@ -0,0 +1,10 @@
|
|||
package vm
|
||||
|
||||
type vmstate byte
|
||||
|
||||
const (
|
||||
NONE = 0
|
||||
HALT = 1 << 0
|
||||
FAULT = 1 << 1
|
||||
BREAK = 1 << 2
|
||||
)
|
38
pkg/vm/vm.go
Normal file
38
pkg/vm/vm.go
Normal file
|
@ -0,0 +1,38 @@
|
|||
package vm
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/CityOfZion/neo-go/pkg/vm/stack"
|
||||
)
|
||||
|
||||
// VM represents an instance of a Neo Virtual Machine
|
||||
type VM struct {
|
||||
InvocationStack stack.Invocation
|
||||
state vmstate
|
||||
}
|
||||
|
||||
//NewVM loads in a script
|
||||
// uses the script to initiate a Context object
|
||||
// pushes the context to the invocation stack
|
||||
func NewVM(script []byte) *VM {
|
||||
ctx := stack.NewContext(script)
|
||||
v := &VM{
|
||||
state: NONE,
|
||||
}
|
||||
v.InvocationStack.Push(ctx)
|
||||
return v
|
||||
}
|
||||
|
||||
// ExecuteOp will execute one opcode for a given context
|
||||
func (v *VM) ExecuteOp(op stack.Instruction, ctx *stack.Context) error {
|
||||
handleOp, ok := opFunc[op]
|
||||
if !ok {
|
||||
return fmt.Errorf("unknown opcode entered %v", op)
|
||||
}
|
||||
err := handleOp(ctx, &v.InvocationStack)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
39
pkg/vm/vm_ops_maths_test.go
Normal file
39
pkg/vm/vm_ops_maths_test.go
Normal file
|
@ -0,0 +1,39 @@
|
|||
package vm
|
||||
|
||||
import (
|
||||
"math/big"
|
||||
"testing"
|
||||
|
||||
"github.com/CityOfZion/neo-go/pkg/vm/stack"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestAddOp(t *testing.T) {
|
||||
|
||||
v := VM{}
|
||||
|
||||
a, err := stack.NewInt(big.NewInt(20))
|
||||
if err != nil {
|
||||
t.Fail()
|
||||
}
|
||||
b, err := stack.NewInt(big.NewInt(23))
|
||||
if err != nil {
|
||||
t.Fail()
|
||||
}
|
||||
|
||||
ctx := stack.NewContext([]byte{})
|
||||
ctx.Estack.Push(a).Push(b)
|
||||
|
||||
v.ExecuteOp(stack.ADD, ctx)
|
||||
|
||||
// Stack should have one item
|
||||
assert.Equal(t, 1, ctx.Estack.Len())
|
||||
|
||||
item, err := ctx.Estack.PopInt()
|
||||
if err != nil {
|
||||
t.Fail()
|
||||
}
|
||||
|
||||
assert.Equal(t, int64(43), item.Value().Int64())
|
||||
|
||||
}
|
Loading…
Reference in a new issue