diff --git a/pkg/vm/context.go b/pkg/vm/context.go index 462531711..cb540cea2 100644 --- a/pkg/vm/context.go +++ b/pkg/vm/context.go @@ -172,7 +172,10 @@ func (c *Context) Dup() StackItem { // ToContractParameter implements StackItem interface. func (c *Context) ToContractParameter(map[StackItem]bool) smartcontract.Parameter { - panic("Not implemented") + return smartcontract.Parameter{ + Type: smartcontract.StringType, + Value: c.String(), + } } func (c *Context) atBreakPoint() bool { diff --git a/pkg/vm/output.go b/pkg/vm/output.go deleted file mode 100644 index 46c27d3f9..000000000 --- a/pkg/vm/output.go +++ /dev/null @@ -1,47 +0,0 @@ -package vm - -import "encoding/json" - -// StackOutput holds information about the stack, used for pretty printing -// the stack. -type stackItem struct { - Value interface{} `json:"value"` - Type string `json:"type"` -} - -func appendToItems(items *[]stackItem, val StackItem, seen map[StackItem]bool) { - if arr, ok := val.Value().([]StackItem); ok { - if seen[val] { - return - } - seen[val] = true - intItems := make([]stackItem, 0, len(arr)) - for _, v := range arr { - appendToItems(&intItems, v, seen) - } - *items = append(*items, stackItem{ - Value: intItems, - Type: val.String(), - }) - - } else { - *items = append(*items, stackItem{ - Value: val, - Type: val.String(), - }) - } -} - -func stackToArray(s *Stack) []stackItem { - items := make([]stackItem, 0, s.Len()) - seen := make(map[StackItem]bool) - s.IterBack(func(e *Element) { - appendToItems(&items, e.value, seen) - }) - return items -} - -func buildStackOutput(s *Stack) string { - b, _ := json.MarshalIndent(stackToArray(s), "", " ") - return string(b) -} diff --git a/pkg/vm/stack.go b/pkg/vm/stack.go index b968d9cc2..bc3142363 100644 --- a/pkg/vm/stack.go +++ b/pkg/vm/stack.go @@ -6,6 +6,7 @@ import ( "fmt" "math/big" + "github.com/CityOfZion/neo-go/pkg/smartcontract" "github.com/CityOfZion/neo-go/pkg/vm/emit" ) @@ -469,7 +470,18 @@ func (s *Stack) popSigElements() ([][]byte, error) { return elems, nil } +// ToContractParameters converts Stack to slice of smartcontract.Parameter. +func (s *Stack) ToContractParameters() []smartcontract.Parameter { + items := make([]smartcontract.Parameter, 0, s.Len()) + s.IterBack(func(e *Element) { + // Each item is independent. + seen := make(map[StackItem]bool) + items = append(items, e.value.ToContractParameter(seen)) + }) + return items +} + // MarshalJSON implements JSON marshalling interface. func (s *Stack) MarshalJSON() ([]byte, error) { - return json.Marshal(stackToArray(s)) + return json.Marshal(s.ToContractParameters()) } diff --git a/pkg/vm/vm.go b/pkg/vm/vm.go index b08795ef7..e5300377b 100644 --- a/pkg/vm/vm.go +++ b/pkg/vm/vm.go @@ -3,6 +3,7 @@ package vm import ( "crypto/sha1" "encoding/binary" + "encoding/json" "fmt" "io/ioutil" "math/big" @@ -301,7 +302,8 @@ func (v *VM) Stack(n string) string { if n == "estack" { s = v.estack } - return buildStackOutput(s) + b, _ := json.MarshalIndent(s.ToContractParameters(), "", " ") + return string(b) } // State returns string representation of the state for the VM.