Skip to content

Commit

Permalink
chore(pkg/scale): return error if Result already has an assigned va…
Browse files Browse the repository at this point in the history
…lue (#2143)

* fix: return error if result already has an assigned value

* chore: include unit test

* chore: fix typo `ErrResultAlreadySet`

* chore: remove unneeded `require.Error`

* chore: fix undeclared name

* chore: remove packaged scope var to avoid problems with result type

* chore: fix result.Set error at offchain test
  • Loading branch information
EclesioMeloJunior authored and timwu20 committed Jan 12, 2022
1 parent 299bd05 commit 25cd1fc
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 9 deletions.
18 changes: 9 additions & 9 deletions lib/babe/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,6 @@ var (
other Other
invalidCustom InvalidCustom
unknownCustom UnknownCustom

dispatchError = scale.MustNewVaryingDataType(other, CannotLookup{}, BadOrigin{}, Module{})
invalid = scale.MustNewVaryingDataType(Call{}, Payment{}, Future{}, Stale{}, BadProof{}, AncientBirthBlock{},
ExhaustsResources{}, invalidCustom, BadMandatory{}, MandatoryDispatch{})
unknown = scale.MustNewVaryingDataType(ValidityCannotLookup{}, NoUnsignedValidator{}, unknownCustom)

okRes = scale.NewResult(nil, dispatchError)
errRes = scale.NewResult(invalid, unknown)
result = scale.NewResult(okRes, errRes)
)

// A DispatchOutcomeError is outcome of dispatching the extrinsic
Expand Down Expand Up @@ -279,6 +270,15 @@ func determineErrType(vdt scale.VaryingDataType) error {
}

func determineErr(res []byte) error {
dispatchError := scale.MustNewVaryingDataType(other, CannotLookup{}, BadOrigin{}, Module{})
invalid := scale.MustNewVaryingDataType(Call{}, Payment{}, Future{}, Stale{}, BadProof{}, AncientBirthBlock{},
ExhaustsResources{}, invalidCustom, BadMandatory{}, MandatoryDispatch{})
unknown := scale.MustNewVaryingDataType(ValidityCannotLookup{}, NoUnsignedValidator{}, unknownCustom)

okRes := scale.NewResult(nil, dispatchError)
errRes := scale.NewResult(invalid, unknown)
result := scale.NewResult(okRes, errRes)

err := scale.Unmarshal(res, &result)
if err != nil {
return &UnmarshalError{err.Error()}
Expand Down
3 changes: 3 additions & 0 deletions lib/runtime/wasmer/imports_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,8 @@ func Test_ext_offchain_http_request_start_version_1(t *testing.T) {
ret, err = inst.Exec("rtm_ext_offchain_http_request_start_version_1", params)
require.NoError(t, err)

resReqID = scale.NewResult(int16(0), nil)

err = scale.Unmarshal(ret, &resReqID)
require.NoError(t, err)

Expand All @@ -302,6 +304,7 @@ func Test_ext_offchain_http_request_start_version_1(t *testing.T) {
require.Equal(t, int16(2), requestNumber)

// start request number 2
resReqID = scale.NewResult(int16(0), nil)
ret, err = inst.Exec("rtm_ext_offchain_http_request_start_version_1", params)
require.NoError(t, err)

Expand Down
8 changes: 8 additions & 0 deletions pkg/scale/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
package scale

import (
"errors"
"fmt"
"reflect"
)

var ErrResultAlreadySet = errors.New("result already has an assigned value")

// ResultMode is the mode the Result is set to
type ResultMode int

Expand Down Expand Up @@ -46,6 +49,10 @@ func NewResult(okIn, errIn interface{}) (res Result) {

// Set takes in a mode (OK/Err) and the associated interface and sets the Result value
func (r *Result) Set(mode ResultMode, in interface{}) (err error) {
if r.mode != Unset {
return ErrResultAlreadySet
}

switch mode {
case OK:
if reflect.TypeOf(r.ok) == reflect.TypeOf(empty{}) && in == nil {
Expand All @@ -70,6 +77,7 @@ func (r *Result) Set(mode ResultMode, in interface{}) (err error) {
default:
err = fmt.Errorf("invalid ResultMode %v", mode)
}

return
}

Expand Down
12 changes: 12 additions & 0 deletions pkg/scale/result_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ package scale
import (
"reflect"
"testing"

"github.com/stretchr/testify/require"
)

func TestEncodeDecodeResult(t *testing.T) {
Expand Down Expand Up @@ -304,3 +306,13 @@ func TestResult_Set(t *testing.T) {
})
}
}

func TestResult_UseSetMoreThanOnce(t *testing.T) {
result := NewResult(int(0), string(""))

err := result.Set(OK, 10)
require.NoError(t, err)

err = result.Set(Err, "something failed")
require.ErrorIs(t, err, ErrResultAlreadySet)
}

0 comments on commit 25cd1fc

Please sign in to comment.