vm: drop duplicating stackItem structure, build JSON from Parameters

smartcontract.Parameter has everything needed now.
This commit is contained in:
Roman Khimov 2020-03-03 13:05:57 +03:00
parent 9b4fd99fbc
commit 56e37ad6ba
4 changed files with 20 additions and 50 deletions

View file

@ -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 {

View file

@ -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)
}

View file

@ -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())
}

View file

@ -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.