diff --git a/vm.go b/vm.go index 353f4f39e..dc026f318 100644 --- a/vm.go +++ b/vm.go @@ -60,8 +60,6 @@ type TxCallback func(opType OpType) bool type Vm struct { // Memory stack stack map[string]string - // Index ptr - iptr int memory map[string]map[string]string } @@ -71,7 +69,10 @@ func NewVm() *Vm { stackSize := uint(256) fmt.Println("stack size =", stackSize) - return &Vm{make(map[string]string), 0, make(map[string]map[string]string)} + return &Vm{ + stack: make(map[string]string), + memory: make(map[string]map[string]string), + } } func (vm *Vm) RunTransaction(tx *Transaction, cb TxCallback) { @@ -84,6 +85,8 @@ func (vm *Vm) RunTransaction(tx *Transaction, cb TxCallback) { vm.stack["0"] = tx.sender vm.stack["1"] = "100" //int(tx.value) vm.stack["1"] = "1000" //int(tx.fee) + // Stack pointer + stPtr := 0 //vm.memory[tx.addr] = make([]int, 256) vm.memory[tx.addr] = make(map[string]string) @@ -93,13 +96,13 @@ func (vm *Vm) RunTransaction(tx *Transaction, cb TxCallback) { // Instructions are shorthanded as Ix/y/z x := 0; y := 1; z := 2; //a := 3; b := 4; c := 5 out: - for vm.iptr < len(tx.data) { + for stPtr < len(tx.data) { // The base big int for all calculations. Use this for any results. base := new(big.Int) // XXX Should Instr return big int slice instead of string slice? - op, args, _ := Instr(tx.data[vm.iptr]) + op, args, _ := Instr(tx.data[stPtr]) - fmt.Printf("%-3d %d %v\n", vm.iptr, op, args) + fmt.Printf("%-3d %d %v\n", stPtr, op, args) opType := OpType(tNorm) // Determine the op type (used for calculating fees by the block manager) @@ -113,7 +116,7 @@ out: // If the callback yielded a negative result abort execution if !cb(opType) { break out } - nptr := vm.iptr + nptr := stPtr switch op { case oSTOP: fmt.Println("exiting (oSTOP), idx =", nptr) @@ -171,10 +174,10 @@ out: break } - if vm.iptr == nptr { - vm.iptr++ + if stPtr == nptr { + stPtr++ } else { - vm.iptr = nptr + stPtr = nptr fmt.Println("... JMP", nptr, "...") } }