Reset stack pointer on run

This commit is contained in:
obscuren 2013-12-28 01:46:18 +01:00
parent 0f656652e6
commit c7dc92e127

23
vm.go

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