unwrap: add Nothing function

The `Nothing` function expects zero stack items and a successful invocation
(HALT state).

Signed-off-by: Tatiana Nesterenko <tatiana@nspcc.io>
This commit is contained in:
Tatiana Nesterenko 2023-09-03 16:54:50 +01:00
parent 0d30c834d5
commit acd821948f
2 changed files with 30 additions and 0 deletions

View file

@ -374,3 +374,15 @@ func Item(r *result.Invoke, err error) (stackitem.Item, error) {
}
return r.Stack[0], nil
}
// Nothing expects zero stack items and a successful invocation (HALT state).
func Nothing(r *result.Invoke, err error) error {
err = checkResOK(r, err)
if err != nil {
return err
}
if len(r.Stack) != 0 {
return errors.New("result stack is not empty")
}
return nil
}

View file

@ -123,6 +123,24 @@ func TestBool(t *testing.T) {
require.True(t, b)
}
func TestNothing(t *testing.T) {
// Error on input.
err := Nothing(&result.Invoke{State: "HALT", Stack: []stackitem.Item{}}, errors.New("some"))
require.Error(t, err)
// Nonempty stack.
err = Nothing(&result.Invoke{State: "HALT", Stack: []stackitem.Item{stackitem.Make(42)}}, nil)
require.Error(t, err)
// FAULT state.
err = Nothing(&result.Invoke{State: "FAULT", Stack: []stackitem.Item{}}, nil)
require.Error(t, err)
// Positive.
err = Nothing(&result.Invoke{State: "HALT", Stack: []stackitem.Item{}}, nil)
require.NoError(t, err)
}
func TestInt64(t *testing.T) {
_, err := Int64(&result.Invoke{State: "HALT", Stack: []stackitem.Item{stackitem.Make("0x03c564ed28ba3d50beb1a52dcb751b929e1d747281566bd510363470be186bc0")}}, nil)
require.Error(t, err)