neoneo-go/pkg/vm/vm.go
decentralisedkev c1b6738bdb
VM: Add basic vm (#166)
* VM:Add abstract stack item

* VM: Add stackItems; Array, Boolean, Int and ByteArray

* VM: Add tests for stack item

* VM: first pass at Random Access Stack object

* VM: Add Sub, Mul, Mod LSH, RSH

* VM: moved test helper functions into separate file

* VM: removed helper functions from stack_test.go

* Add conversions for bytearray and Int stack items

* Add instructions file for vm

* - Add guide to stack readme
- Add testReadInt64

* Add Builder

* Refactor Int, Boolean, ByteArray conversion

* Add Context stack Item

* Add Invocation stack - convenience RAS

* rename testhelper to test_helper

* Move opcode file

* - Add `Add` OpCode
- Add Opcode Function map

* - Add test for math `Add` opcode
- basic opcode execution

* Add popTwoIntegers convenience func

* Add `SUB` Opcode

* Export Context Read methods
- Return errors where failable

* - Add `Op` to handleOP func signature
- Add PushNBytes OPcode

* remove error on NewBoolean
- Expose underlying with Getter on Boolean StackItem
- Add Equals method for ByteArray

* Make Next() method on Context failable, refactor peekContext and Peek

* Add ExecuteOp, Step and Run methods on the VM

* Add Equal Opcode

* Add THROWIFNOT Opcode

* Add RET Opcode

* Refactor PushNBytes Opcode

* refactor Add, Sub to return VMSTATE
add popTwoByteArrays helper function

* Add basic tests for vm

* clarify vm states

* Add astack

* [VM]

Pass ResultStack to the opcode handlers

* [VM]

refactor handlers to have rstack as argument

* [Stack]

- Change RemoveCurrentContext for PopCurrentContext
- Add CopTo method to stack

* [VM]

Add Result stack Len check in simple run test

* [VM] fix typo

* [Peer/Stall]

Change seconds to milliseconds in test
2019-03-18 21:40:21 +00:00

72 lines
1.9 KiB
Go

package vm
import (
"fmt"
"github.com/CityOfZion/neo-go/pkg/vm/stack"
)
// VM represents an instance of a Neo Virtual Machine
type VM struct {
// ResultStack contains the results of
// the last evaluation stack before the program terminated
ResultStack stack.RandomAccess
// InvocationStack contains all of the contexts
// loaded into the vm
InvocationStack stack.Invocation
state Vmstate
}
// NewVM will:
// Set the state of the VM to NONE
// instantiate a script as a new context
// Push 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
}
// Run loops over the current context by continuously stepping.
// Run breaks; once step returns an error or any state that is not NONE
func (v *VM) Run() (Vmstate, error) {
for {
state, err := v.step()
if err != nil || state != NONE {
return state, err
}
}
}
// step will read `one` opcode from the script in the current context
// Then excute that opcode
func (v *VM) step() (Vmstate, error) {
// Get Current Context
ctx, err := v.InvocationStack.CurrentContext()
if err != nil {
return FAULT, err
}
// Read Opcode from context
op, _ := ctx.Next() // The only error that can occur from this, is if the pointer goes over the pointer
// In the NEO-VM specs, this is ignored and we return the RET opcode
// Execute OpCode
state, err := v.executeOp(stack.Instruction(op), ctx)
if err != nil {
return FAULT, err
}
return state, nil
}
// ExecuteOp will execute one opcode on a given context.
// If the opcode is not registered, then an unknown opcode error will be returned
func (v *VM) executeOp(op stack.Instruction, ctx *stack.Context) (Vmstate, error) {
//Find function which handles that specific opcode
handleOp, ok := opFunc[op]
if !ok {
return FAULT, fmt.Errorf("unknown opcode entered %v", op)
}
return handleOp(op, ctx, &v.InvocationStack, &v.ResultStack)
}