Skip to content
This repository has been archived by the owner on Nov 16, 2022. It is now read-only.

Commit

Permalink
Merge pull request #2025 from bandprotocol/clean-up-go-owasm-error
Browse files Browse the repository at this point in the history
go-owasm: Cleanup code documentation and error messages
  • Loading branch information
taobun authored Jun 22, 2020
2 parents fe16b73 + 4df0804 commit 38463db
Show file tree
Hide file tree
Showing 19 changed files with 285 additions and 406 deletions.
1 change: 1 addition & 0 deletions CHANGELOG_UNRELEASED.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@

### Owasm

- (chore) [\#2025](https://github.com/bandprotocol/bandchain/pull/2025) Cleanup code documentation and error messages
- (feat) [\#1998](https://github.com/bandprotocol/bandchain/pull/1998) Implement safe guard to check import and export from wasm
- (feat) [\#1994](https://github.com/bandprotocol/bandchain/pull/1994) Implement inject stack height when compile wasm code.
- (feat) [\#1988](https://github.com/bandprotocol/bandchain/pull/1988) Implement safe guard to check memory limit from wasm
Expand Down
18 changes: 9 additions & 9 deletions chain/x/oracle/types/exec_env.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func (env *BaseEnv) GetCalldata() []byte {

// SetReturnData implements Owasm ExecEnv interface.
func (env *BaseEnv) SetReturnData(data []byte) error {
return api.ErrSetReturnDataWrongPeriod
return api.ErrWrongPeriodAction
}

// GetAskCount implements Owasm ExecEnv interface.
Expand All @@ -31,22 +31,22 @@ func (env *BaseEnv) GetMinCount() int64 {

// GetAnsCount implements Owasm ExecEnv interface.
func (env *BaseEnv) GetAnsCount() (int64, error) {
return 0, api.ErrAnsCountWrongPeriod
return 0, api.ErrWrongPeriodAction
}

// AskExternalData implements Owasm ExecEnv interface.
func (env *BaseEnv) AskExternalData(eid int64, did int64, data []byte) error {
return api.ErrAskExternalDataWrongPeriod
return api.ErrWrongPeriodAction
}

// GetExternalDataStatus implements Owasm ExecEnv interface.
func (env *PrepareEnv) GetExternalDataStatus(eid int64, vid int64) (int64, error) {
return 0, api.ErrGetExternalDataStatusWrongPeriod
return 0, api.ErrWrongPeriodAction
}

// GetExternalData implements Owasm ExecEnv interface.
func (env *PrepareEnv) GetExternalData(eid int64, vid int64) ([]byte, error) {
return nil, api.ErrGetExternalDataWrongPeriod
return nil, api.ErrWrongPeriodAction
}

// PrepareEnv implements ExecEnv interface only expected function and panic on non-prepare functions.
Expand All @@ -69,10 +69,10 @@ func NewPrepareEnv(req Request, maxRawRequests int64) *PrepareEnv {
// AskExternalData implements Owasm ExecEnv interface.
func (env *PrepareEnv) AskExternalData(eid int64, did int64, data []byte) error {
if int64(len(data)) > MaxRawRequestDataSize {
return api.ErrSpanExceededCapacity
return api.ErrSpanTooSmall
}
if int64(len(env.rawRequests)) >= env.maxRawRequests {
return api.ErrAskExternalDataExceed
return api.ErrTooManyExternalData
}
env.rawRequests = append(env.rawRequests, NewRawRequest(
ExternalID(eid), DataSourceID(did), data,
Expand Down Expand Up @@ -126,7 +126,7 @@ func (env *ExecuteEnv) SetReturnData(data []byte) error {

func (env *ExecuteEnv) getExternalDataFull(eid int64, valIdx int64) ([]byte, int64, error) {
if valIdx < 0 || valIdx >= int64(len(env.request.RequestedValidators)) {
return nil, 0, api.ErrValidatorOutOfRange
return nil, 0, api.ErrBadValidatorIndex
}
valAddr := env.request.RequestedValidators[valIdx].String()
valReports, ok := env.reports[valAddr]
Expand All @@ -135,7 +135,7 @@ func (env *ExecuteEnv) getExternalDataFull(eid int64, valIdx int64) ([]byte, int
}
valReport, ok := valReports[ExternalID(eid)]
if !ok {
return nil, -1, api.ErrInvalidExternalID
return nil, -1, api.ErrBadExternalID
}
return valReport.Data, int64(valReport.ExitCode), nil
}
Expand Down
14 changes: 7 additions & 7 deletions chain/x/oracle/types/exec_env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func TestSetReturnData(t *testing.T) {

penv := mockFreshPrepareEnv()
err := penv.SetReturnData(result)
require.Equal(t, api.ErrSetReturnDataWrongPeriod, err)
require.Equal(t, api.ErrWrongPeriodAction, err)

eenv := mockExecEnv()
eenv.SetReturnData(result)
Expand Down Expand Up @@ -113,7 +113,7 @@ func TestGetAnsCount(t *testing.T) {
// Should return error if call on prepare environment.
penv := mockFreshPrepareEnv()
_, err := penv.GetAnsCount()
require.Equal(t, api.ErrAnsCountWrongPeriod, err)
require.Equal(t, api.ErrWrongPeriodAction, err)

eenv := mockExecEnv()
v, err := eenv.GetAnsCount()
Expand All @@ -139,9 +139,9 @@ func TestGetExternalData(t *testing.T) {
require.Equal(t, int64(-1), status)

_, err = env.GetExternalData(1, 100)
require.Equal(t, api.ErrValidatorOutOfRange, err)
require.Equal(t, api.ErrBadValidatorIndex, err)
_, err = env.GetExternalDataStatus(1, 100)
require.Equal(t, api.ErrValidatorOutOfRange, err)
require.Equal(t, api.ErrBadValidatorIndex, err)

_, err = env.GetExternalData(1, -1)
require.Error(t, err)
Expand All @@ -158,9 +158,9 @@ func TestFailedGetExternalData(t *testing.T) {
penv := mockAlreadyPreparedEnv()

_, err := penv.GetExternalData(1, 1)
require.Equal(t, api.ErrGetExternalDataWrongPeriod, err)
require.Equal(t, api.ErrWrongPeriodAction, err)
_, err = penv.GetExternalDataStatus(1, 1)
require.Equal(t, api.ErrGetExternalDataStatusWrongPeriod, err)
require.Equal(t, api.ErrWrongPeriodAction, err)
}

func TestAskExternalData(t *testing.T) {
Expand Down Expand Up @@ -203,7 +203,7 @@ func TestAskExternalDataOnExecEnv(t *testing.T) {
env := mockExecEnv()
calldata := []byte("CALLDATA")
err := env.AskExternalData(2, 2, calldata)
require.Equal(t, api.ErrAskExternalDataWrongPeriod, err)
require.Equal(t, api.ErrWrongPeriodAction, err)
}

func TestGetRawRequests(t *testing.T) {
Expand Down
73 changes: 24 additions & 49 deletions go-owasm/api/bindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,53 +11,28 @@

enum Error {
Error_NoError = 0,
Error_CompliationError = 1,
Error_RunError = 2,
Error_ParseError = 3,
Error_WriteBinaryError = 4,
Error_ResolveNamesError = 5,
Error_ValidateError = 6,
Error_SpanExceededCapacityError = 7,
Error_DeserializationError = 8,
Error_GasCounterInjectionError = 9,
Error_SerializationError = 10,
Error_GasLimitExceedError = 11,
Error_NoMemoryWasmError = 12,
Error_MinimumMemoryExceedError = 13,
Error_SetMaximumMemoryError = 14,
Error_StackHeightInstrumentationError = 15,
Error_CheckWasmImportsError = 16,
Error_CheckWasmExportsError = 17,
Error_InvalidSignatureFunctionError = 18,
Error_SetReturnDataWrongPeriodError = 128,
Error_AnsCountWrongPeriodError = 129,
Error_AskExternalDataWrongPeriodError = 130,
Error_AskExternalDataExceedError = 131,
Error_GetExternalDataStatusWrongPeriodError = 132,
Error_GetExternalDataWrongPeriodError = 133,
Error_ValidatorOutOfRangeError = 134,
Error_InvalidExternalIDError = 135,
Error_GetUnreportedDataError = 136,
Error_SpanTooSmallError = 1,
Error_ValidationError = 2,
Error_DeserializationError = 3,
Error_SerializationError = 4,
Error_InvalidImportsError = 5,
Error_InvalidExportsError = 6,
Error_BadMemorySectionError = 7,
Error_GasCounterInjectionError = 8,
Error_StackHeightInjectionError = 9,
Error_InstantiationError = 10,
Error_RuntimeError = 11,
Error_OutOfGasError = 12,
Error_BadEntrySignatureError = 13,
Error_WrongPeriodActionError = 128,
Error_TooManyExternalDataError = 129,
Error_BadValidatorIndexError = 130,
Error_BadExternalIDError = 131,
Error_UnavailableExternalDataError = 132,
Error_UnknownError = 255,
};
typedef int32_t Error;

enum GoResult {
GoResult_Ok = 0,
GoResult_SpanExceededCapacity = 1,
GoResult_SetReturnDataWrongPeriod = 2,
GoResult_AnsCountWrongPeriod = 3,
GoResult_AskExternalDataWrongPeriod = 4,
GoResult_AskExternalDataExceed = 5,
GoResult_GetExternalDataStatusWrongPeriod = 6,
GoResult_GetExternalDataWrongPeriod = 7,
GoResult_ValidatorOutOfRange = 8,
GoResult_InvalidExternalID = 9,
GoResult_GetUnreportedData = 10,
GoResult_Other = 11,
};
typedef int32_t GoResult;

typedef struct Span {
uint8_t *ptr;
uintptr_t len;
Expand All @@ -69,14 +44,14 @@ typedef struct env_t {
} env_t;

typedef struct EnvDispatcher {
GoResult (*get_calldata)(env_t*, Span *calldata);
GoResult (*set_return_data)(env_t*, Span data);
Error (*get_calldata)(env_t*, Span *calldata);
Error (*set_return_data)(env_t*, Span data);
int64_t (*get_ask_count)(env_t*);
int64_t (*get_min_count)(env_t*);
GoResult (*get_ans_count)(env_t*, int64_t*);
GoResult (*ask_external_data)(env_t*, int64_t eid, int64_t did, Span data);
GoResult (*get_external_data_status)(env_t*, int64_t eid, int64_t vid, int64_t *status);
GoResult (*get_external_data)(env_t*, int64_t eid, int64_t vid, Span *data);
Error (*get_ans_count)(env_t*, int64_t*);
Error (*ask_external_data)(env_t*, int64_t eid, int64_t did, Span data);
Error (*get_external_data_status)(env_t*, int64_t eid, int64_t vid, int64_t *status);
Error (*get_external_data)(env_t*, int64_t eid, int64_t vid, Span *data);
} EnvDispatcher;

typedef struct Env {
Expand Down
24 changes: 12 additions & 12 deletions go-owasm/api/cgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ package api

// #include "bindings.h"
//
// GoResult cGetCalldata(env_t *e, Span *calldata);
// GoResult cGetCalldata_cgo(env_t *e, Span *calldata) { return cGetCalldata(e, calldata); }
// GoResult cSetReturnData(env_t *e, Span data);
// GoResult cSetReturnData_cgo(env_t *e, Span data) { return cSetReturnData(e, data); }
// Error cGetCalldata(env_t *e, Span *calldata);
// Error cGetCalldata_cgo(env_t *e, Span *calldata) { return cGetCalldata(e, calldata); }
// Error cSetReturnData(env_t *e, Span data);
// Error cSetReturnData_cgo(env_t *e, Span data) { return cSetReturnData(e, data); }
// int64_t cGetAskCount(env_t *e);
// int64_t cGetAskCount_cgo(env_t *e) { return cGetAskCount(e); }
// int64_t cGetMinCount(env_t *e);
// int64_t cGetMinCount_cgo(env_t *e) { return cGetMinCount(e); }
// GoResult cGetAnsCount(env_t *e, int64_t *val);
// GoResult cGetAnsCount_cgo(env_t *e, int64_t *val) { return cGetAnsCount(e, val); }
// GoResult cAskExternalData(env_t *e, int64_t eid, int64_t did, Span data);
// GoResult cAskExternalData_cgo(env_t *e, int64_t eid, int64_t did, Span data) { return cAskExternalData(e, eid, did, data); }
// GoResult cGetExternalDataStatus(env_t *e, int64_t eid, int64_t vid, int64_t *status);
// GoResult cGetExternalDataStatus_cgo(env_t *e, int64_t eid, int64_t vid, int64_t *status) { return cGetExternalDataStatus(e, eid, vid, status); }
// GoResult cGetExternalData(env_t *e, int64_t eid, int64_t vid, Span *data);
// GoResult cGetExternalData_cgo(env_t *e, int64_t eid, int64_t vid, Span *data) { return cGetExternalData(e, eid, vid, data); }
// Error cGetAnsCount(env_t *e, int64_t *val);
// Error cGetAnsCount_cgo(env_t *e, int64_t *val) { return cGetAnsCount(e, val); }
// Error cAskExternalData(env_t *e, int64_t eid, int64_t did, Span data);
// Error cAskExternalData_cgo(env_t *e, int64_t eid, int64_t did, Span data) { return cAskExternalData(e, eid, did, data); }
// Error cGetExternalDataStatus(env_t *e, int64_t eid, int64_t vid, int64_t *status);
// Error cGetExternalDataStatus_cgo(env_t *e, int64_t eid, int64_t vid, int64_t *status) { return cGetExternalDataStatus(e, eid, vid, status); }
// Error cGetExternalData(env_t *e, int64_t eid, int64_t vid, Span *data);
// Error cGetExternalData_cgo(env_t *e, int64_t eid, int64_t vid, Span *data) { return cGetExternalData(e, eid, vid, data); }
import "C"
71 changes: 23 additions & 48 deletions go-owasm/api/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,47 +18,26 @@ type EnvInterface interface {
}

type envIntl struct {
ext EnvInterface
extData map[[2]int64][]byte
ext EnvInterface
}

func createEnvIntl(ext EnvInterface) *envIntl {
return &envIntl{
ext: ext,
extData: make(map[[2]int64][]byte),
}
}

func parseErrorToC(err error) C.GoResult {
switch err {
case ErrSetReturnDataWrongPeriod:
return C.GoResult_SetReturnDataWrongPeriod
case ErrAnsCountWrongPeriod:
return C.GoResult_AnsCountWrongPeriod
case ErrAskExternalDataWrongPeriod:
return C.GoResult_AskExternalDataWrongPeriod
case ErrGetExternalDataStatusWrongPeriod:
return C.GoResult_GetExternalDataStatusWrongPeriod
case ErrGetExternalDataWrongPeriod:
return C.GoResult_GetExternalDataWrongPeriod
default:
return C.GoResult_Other
}
return &envIntl{ext: ext}
}

//export cGetCalldata
func cGetCalldata(e *C.env_t, calldata *C.Span) C.GoResult {
func cGetCalldata(e *C.env_t, calldata *C.Span) C.Error {
data := (*(*envIntl)(unsafe.Pointer(e))).ext.GetCalldata()
return writeSpan(calldata, data)
}

//export cSetReturnData
func cSetReturnData(e *C.env_t, span C.Span) C.GoResult {
func cSetReturnData(e *C.env_t, span C.Span) C.Error {
err := (*(*envIntl)(unsafe.Pointer(e))).ext.SetReturnData(readSpan(span))
if err != nil {
return parseErrorToC(err)
return toCError(err)
}
return C.GoResult_Ok
return C.Error_NoError
}

//export cGetAskCount
Expand All @@ -72,47 +51,43 @@ func cGetMinCount(e *C.env_t) C.int64_t {
}

//export cGetAnsCount
func cGetAnsCount(e *C.env_t, val *C.int64_t) C.GoResult {
func cGetAnsCount(e *C.env_t, val *C.int64_t) C.Error {
v, err := (*(*envIntl)(unsafe.Pointer(e))).ext.GetAnsCount()
if err != nil {
return parseErrorToC(err)
return toCError(err)
}
*val = C.int64_t(v)
return C.GoResult_Ok
return C.Error_NoError
}

//export cAskExternalData
func cAskExternalData(e *C.env_t, eid C.int64_t, did C.int64_t, span C.Span) C.GoResult {
func cAskExternalData(e *C.env_t, eid C.int64_t, did C.int64_t, span C.Span) C.Error {
err := (*(*envIntl)(unsafe.Pointer(e))).ext.AskExternalData(int64(eid), int64(did), readSpan(span))
if err != nil {
return parseErrorToC(err)
return toCError(err)
}
return C.GoResult_Ok
return C.Error_NoError
}

//export cGetExternalDataStatus
func cGetExternalDataStatus(e *C.env_t, eid C.int64_t, vid C.int64_t, status *C.int64_t) C.GoResult {
func cGetExternalDataStatus(e *C.env_t, eid C.int64_t, vid C.int64_t, status *C.int64_t) C.Error {
s, err := (*(*envIntl)(unsafe.Pointer(e))).ext.GetExternalDataStatus(int64(eid), int64(vid))
if err != nil {
return parseErrorToC(err)
return toCError(err)
}
*status = C.int64_t(s)
return C.GoResult_Ok
return C.Error_NoError
}

//export cGetExternalData
func cGetExternalData(e *C.env_t, eid C.int64_t, vid C.int64_t, data *C.Span) C.GoResult {
key := [2]int64{int64(eid), int64(vid)}
func cGetExternalData(e *C.env_t, eid C.int64_t, vid C.int64_t, data *C.Span) C.Error {
env := (*(*envIntl)(unsafe.Pointer(e)))
if _, ok := env.extData[key]; !ok {
data, err := env.ext.GetExternalData(int64(eid), int64(vid))
if err != nil {
return parseErrorToC(err)
}
if data == nil {
return C.GoResult_GetUnreportedData
}
env.extData[key] = data
extData, err := env.ext.GetExternalData(int64(eid), int64(vid))
if err != nil {
return toCError(err)
}
if extData == nil {
return C.Error_UnavailableExternalDataError
}
return writeSpan(data, env.extData[key])
return writeSpan(data, extData)
}
Loading

0 comments on commit 38463db

Please sign in to comment.