Skip to content

Commit

Permalink
Add sentinel errors and error wrappings
Browse files Browse the repository at this point in the history
  • Loading branch information
qdm12 committed Jul 21, 2022
1 parent fb25185 commit c67a5be
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 20 deletions.
6 changes: 4 additions & 2 deletions dot/rpc/websocket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@ var testCalls = []struct {
expected: []byte(`{"jsonrpc":"2.0","result":3,"id":5}` + "\n")},
{
call: []byte(`{"jsonrpc":"2.0","method":"author_submitAndWatchExtrinsic","params":["0x010203"],"id":6}`),
expected: []byte(`{"jsonrpc":"2.0","error":{"code":null,"message":"Failed to call the ` +
"`" + `TaggedTransactionQueue_validate_transaction` + "`" + ` exported function."},"id":6}` + "\n")},
expected: []byte(`{"jsonrpc":"2.0","error":{"code":null,` +
`"message":"running runtime function: Failed to call the ` +
"`" + `TaggedTransactionQueue_validate_transaction` + "`" +
` exported function."},"id":6}` + "\n")},
{
call: []byte(`{"jsonrpc":"2.0","method":"state_subscribeRuntimeVersion","params":[],"id":7}`),
expected: []byte(`{"jsonrpc":"2.0","result":6,"id":7}` + "\n")},
Expand Down
22 changes: 11 additions & 11 deletions lib/runtime/wasmer/exports_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package wasmer

import (
"encoding/json"
"errors"
"fmt"
"math/big"
"os"
Expand Down Expand Up @@ -1022,15 +1021,14 @@ func TestInstance_DecodeSessionKeys(t *testing.T) {

func TestInstance_PaymentQueryInfo(t *testing.T) {
tests := []struct {
extB []byte
ext string
err error
expect *types.TransactionPaymentQueryInfo
extB []byte
ext string
errMessage string
expect *types.TransactionPaymentQueryInfo
}{
{
// Was made with @polkadot/api on https://github.com/danforbes/polkadot-js-scripts/tree/create-signed-tx
ext: "0xd1018400d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d01bc2b6e35929aabd5b8bc4e5b0168c9bee59e2bb9d6098769f6683ecf73e44c776652d947a270d59f3d37eb9f9c8c17ec1b4cc473f2f9928ffdeef0f3abd43e85d502000000012844616e20466f72626573", //nolint:lll
err: nil,
expect: &types.TransactionPaymentQueryInfo{
Weight: 1973000,
Class: 0,
Expand All @@ -1043,12 +1041,14 @@ func TestInstance_PaymentQueryInfo(t *testing.T) {
{
// incomplete extrinsic
ext: "0x4ccde39a5684e7a56da23b22d4d9fbadb023baa19c56495432884d0640000000000000000000000000000000",
err: errors.New("Failed to call the `TransactionPaymentApi_query_info` exported function."), //nolint:revive
errMessage: "running runtime function: " +
"Failed to call the `TransactionPaymentApi_query_info` exported function.",
},
{
// incomplete extrinsic
extB: nil,
err: errors.New("Failed to call the `TransactionPaymentApi_query_info` exported function."), //nolint:revive
errMessage: "running runtime function: " +
"Failed to call the `TransactionPaymentApi_query_info` exported function.",
},
}

Expand All @@ -1066,11 +1066,11 @@ func TestInstance_PaymentQueryInfo(t *testing.T) {
ins := NewTestInstance(t, runtime.NODE_RUNTIME)
info, err := ins.PaymentQueryInfo(extBytes)

if test.err != nil {
require.Error(t, err)
require.Equal(t, err.Error(), test.err.Error())
if test.errMessage != "" {
assert.EqualError(t, err, test.errMessage)
continue
}
require.NoError(t, err)

fmt.Println(info.PartialFee.String())
fmt.Println(test.expect.PartialFee.String())
Expand Down
7 changes: 4 additions & 3 deletions lib/runtime/wasmer/imports_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -924,15 +924,15 @@ func Test_ext_crypto_ecdsa_verify_version_2_Table(t *testing.T) {
key: []byte{132, 2, 39, 55, 134, 131, 142, 43, 100, 63, 134, 96, 14, 253, 15, 222, 119, 154, 110, 188, 20, 159, 62, 125, 42, 59, 127, 19, 16, 0, 161, 236, 109}, //nolint:lll
err: wasmer.NewExportedFunctionError(
"rtm_ext_crypto_ecdsa_verify_version_2",
"Failed to call the `%s` exported function."),
"running runtime function: Failed to call the `%s` exported function."),
},
"invalid message": {
sig: []byte{5, 1, 187, 179, 88, 183, 46, 115, 242, 32, 9, 54, 141, 207, 44, 15, 238, 42, 217, 196, 111, 173, 239, 204, 128, 93, 49, 179, 137, 150, 162, 125, 226, 225, 28, 145, 122, 127, 15, 154, 185, 11, 3, 66, 27, 187, 204, 242, 107, 68, 26, 111, 245, 30, 115, 141, 85, 74, 158, 211, 161, 217, 43, 151, 120, 125, 1}, //nolint:lll
msg: []byte{48, 72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100},
key: []byte{132, 2, 39, 206, 55, 134, 131, 142, 43, 100, 63, 134, 96, 14, 253, 15, 222, 119, 154, 110, 188, 20, 159, 62, 125, 42, 59, 127, 19, 16, 0, 161, 236, 109}, //nolint:lll
err: wasmer.NewExportedFunctionError(
"rtm_ext_crypto_ecdsa_verify_version_2",
"Failed to call the `%s` exported function."),
"running runtime function: Failed to call the `%s` exported function."),
},
}
for name, tc := range testCases {
Expand Down Expand Up @@ -1595,7 +1595,8 @@ func Test_ext_default_child_storage_storage_kill_version_3(t *testing.T) {
key: []byte(`fakekey`),
limit: optLimit2,
expected: []byte{0, 0, 0, 0, 0},
errMsg: "Failed to call the `rtm_ext_default_child_storage_storage_kill_version_3` exported function.",
errMsg: "running runtime function: " +
"Failed to call the `rtm_ext_default_child_storage_storage_kill_version_3` exported function.",
},
{key: testChildKey, limit: optLimit2, expected: []byte{1, 2, 0, 0, 0}},
{key: testChildKey, limit: nil, expected: []byte{0, 1, 0, 0, 0}},
Expand Down
13 changes: 9 additions & 4 deletions lib/runtime/wasmer/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,19 +264,24 @@ func (in *Instance) Stop() {
}
}

var (
ErrInstanceIsStopped = errors.New("instance is stopped")
ErrExportFunctionNotFound = errors.New("export function not found")
)

// Exec calls the given function with the given data
func (in *Instance) Exec(function string, data []byte) ([]byte, error) {
in.Lock()
defer in.Unlock()

if in.isClosed {
return nil, errors.New("instance is stopped")
return nil, ErrInstanceIsStopped
}

dataLength := uint32(len(data))
inputPtr, err := in.ctx.Allocator.Allocate(dataLength)
if err != nil {
return nil, err
return nil, fmt.Errorf("allocating input memory: %w", err)
}

defer in.ctx.Allocator.Clear()
Expand All @@ -287,12 +292,12 @@ func (in *Instance) Exec(function string, data []byte) ([]byte, error) {

runtimeFunc, ok := in.vm.Exports[function]
if !ok {
return nil, fmt.Errorf("could not find exported function %s", function)
return nil, fmt.Errorf("%w: %s", ErrExportFunctionNotFound, function)
}

wasmValue, err := runtimeFunc(int32(inputPtr), int32(dataLength))
if err != nil {
return nil, err
return nil, fmt.Errorf("running runtime function: %w", err)
}

outputPtr, outputLength := runtime.Int64ToPointerAndSize(wasmValue.ToI64())
Expand Down

0 comments on commit c67a5be

Please sign in to comment.