vm: allow to convert stack to a slice

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgenii Stratonikov 2020-07-29 12:59:19 +03:00
parent 805f746f15
commit 7b4acd5a7f
2 changed files with 25 additions and 0 deletions

View file

@ -396,6 +396,15 @@ func (s *Stack) PopSigElements() ([][]byte, error) {
return elems, nil return elems, nil
} }
// ToArray converts stack to an array of stackitems with top item being the last.
func (s *Stack) ToArray() []stackitem.Item {
items := make([]stackitem.Item, 0, s.len)
s.IterBack(func(e *Element) {
items = append(items, e.Item())
})
return items
}
// ToContractParameters converts Stack to slice of smartcontract.Parameter. // ToContractParameters converts Stack to slice of smartcontract.Parameter.
func (s *Stack) ToContractParameters() []smartcontract.Parameter { func (s *Stack) ToContractParameters() []smartcontract.Parameter {
items := make([]smartcontract.Parameter, 0, s.Len()) items := make([]smartcontract.Parameter, 0, s.Len())

View file

@ -237,6 +237,22 @@ func TestPushVal(t *testing.T) {
assert.IsType(t, elem.value, &stackitem.Array{}) assert.IsType(t, elem.value, &stackitem.Array{})
} }
func TestStack_ToArray(t *testing.T) {
t.Run("Empty", func(t *testing.T) {
s := NewStack("test")
items := s.ToArray()
require.Equal(t, 0, len(items))
})
t.Run("NonEmpty", func(t *testing.T) {
s := NewStack("test")
expected := []stackitem.Item{stackitem.Make(1), stackitem.Make(true)}
for i := range expected {
s.PushVal(expected[i])
}
require.Equal(t, expected, s.ToArray())
})
}
func TestSwapElemValues(t *testing.T) { func TestSwapElemValues(t *testing.T) {
s := NewStack("test") s := NewStack("test")