Skip to content

Commit

Permalink
unwrap: add Nothing function
Browse files Browse the repository at this point in the history
The `Nothing` function expects zero stack items and a successful invocation
(HALT state).

Signed-off-by: Tatiana Nesterenko <[email protected]>
  • Loading branch information
tatiana-nspcc committed Sep 3, 2023
1 parent 0d30c83 commit acd8219
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
12 changes: 12 additions & 0 deletions pkg/rpcclient/unwrap/unwrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
18 changes: 18 additions & 0 deletions pkg/rpcclient/unwrap/unwrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit acd8219

Please sign in to comment.