Skip to content

Commit

Permalink
Differentiate between contract and system error
Browse files Browse the repository at this point in the history
  • Loading branch information
chipshort committed Nov 21, 2023
1 parent 10138ad commit 1dd4fa3
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 70 deletions.
24 changes: 18 additions & 6 deletions ibc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,21 @@ func TestIBCHandshake(t *testing.T) {
init_msg := IBCInstantiateMsg{
ReflectCodeID: REFLECT_ID,
}
ires, _, err := vm.Instantiate(checksum, env, info, toBytes(t, init_msg), store, *goapi, querier, gasMeter1, TESTING_GAS_LIMIT, deserCost)
i, _, err := vm.Instantiate(checksum, env, info, toBytes(t, init_msg), store, *goapi, querier, gasMeter1, TESTING_GAS_LIMIT, deserCost)
require.NoError(t, err)
assert.NotNil(t, i.Ok)
ires := i.Ok
require.Equal(t, 0, len(ires.Messages))

// channel open
gasMeter2 := api.NewMockGasMeter(TESTING_GAS_LIMIT)
store.SetGasMeter(gasMeter2)
env = api.MockEnv()
openMsg := api.MockIBCChannelOpenInit(CHANNEL_ID, types.Ordered, IBC_VERSION)
ores, _, err := vm.IBCChannelOpen(checksum, env, openMsg, store, *goapi, querier, gasMeter2, TESTING_GAS_LIMIT, deserCost)
o, _, err := vm.IBCChannelOpen(checksum, env, openMsg, store, *goapi, querier, gasMeter2, TESTING_GAS_LIMIT, deserCost)
require.NoError(t, err)
require.NotNil(t, o.Ok)
ores := o.Ok
require.Equal(t, &types.IBC3ChannelOpenResponse{Version: "ibc-reflect-v1"}, ores)

// channel connect
Expand All @@ -124,8 +128,10 @@ func TestIBCHandshake(t *testing.T) {
env = api.MockEnv()
// completes and dispatches message to create reflect contract
connectMsg := api.MockIBCChannelConnectAck(CHANNEL_ID, types.Ordered, IBC_VERSION)
res, _, err := vm.IBCChannelConnect(checksum, env, connectMsg, store, *goapi, querier, gasMeter2, TESTING_GAS_LIMIT, deserCost)
conn, _, err := vm.IBCChannelConnect(checksum, env, connectMsg, store, *goapi, querier, gasMeter2, TESTING_GAS_LIMIT, deserCost)
require.NoError(t, err)
require.NotNil(t, conn.Ok)
res := conn.Ok
require.Equal(t, 1, len(res.Messages))

// check for the expected custom event
Expand Down Expand Up @@ -179,17 +185,21 @@ func TestIBCPacketDispatch(t *testing.T) {
gasMeter2 := api.NewMockGasMeter(TESTING_GAS_LIMIT)
store.SetGasMeter(gasMeter2)
openMsg := api.MockIBCChannelOpenInit(CHANNEL_ID, types.Ordered, IBC_VERSION)
ores, _, err := vm.IBCChannelOpen(checksum, env, openMsg, store, *goapi, querier, gasMeter2, TESTING_GAS_LIMIT, deserCost)
o, _, err := vm.IBCChannelOpen(checksum, env, openMsg, store, *goapi, querier, gasMeter2, TESTING_GAS_LIMIT, deserCost)
require.NoError(t, err)
require.NotNil(t, o.Ok)
ores := o.Ok
require.Equal(t, &types.IBC3ChannelOpenResponse{Version: "ibc-reflect-v1"}, ores)

// channel connect
gasMeter3 := api.NewMockGasMeter(TESTING_GAS_LIMIT)
store.SetGasMeter(gasMeter3)
// completes and dispatches message to create reflect contract
connectMsg := api.MockIBCChannelConnectAck(CHANNEL_ID, types.Ordered, IBC_VERSION)
res, _, err := vm.IBCChannelConnect(checksum, env, connectMsg, store, *goapi, querier, gasMeter3, TESTING_GAS_LIMIT, deserCost)
conn, _, err := vm.IBCChannelConnect(checksum, env, connectMsg, store, *goapi, querier, gasMeter3, TESTING_GAS_LIMIT, deserCost)
require.NoError(t, err)
require.NotNil(t, conn.Ok)
res := conn.Ok
require.Equal(t, 1, len(res.Messages))
id := res.Messages[0].ID

Expand Down Expand Up @@ -220,8 +230,10 @@ func TestIBCPacketDispatch(t *testing.T) {
queryMsg := IBCQueryMsg{
ListAccounts: &struct{}{},
}
qres, _, err := vm.Query(checksum, env, toBytes(t, queryMsg), store, *goapi, querier, gasMeter4, TESTING_GAS_LIMIT, deserCost)
q, _, err := vm.Query(checksum, env, toBytes(t, queryMsg), store, *goapi, querier, gasMeter4, TESTING_GAS_LIMIT, deserCost)
require.NoError(t, err)
require.NotNil(t, q.Ok)
qres := q.Ok
var accounts ListAccountsResponse
err = json.Unmarshal(qres, &accounts)
require.NoError(t, err)
Expand Down
77 changes: 22 additions & 55 deletions lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func (vm *VM) Instantiate(
gasMeter GasMeter,
gasLimit uint64,
deserCost types.UFraction,
) (*types.Response, uint64, error) {
) (*types.ContractResult, uint64, error) {
envBin, err := json.Marshal(env)
if err != nil {
return nil, 0, err
Expand All @@ -146,10 +146,7 @@ func (vm *VM) Instantiate(
if err != nil {
return nil, gasReport.UsedInternally, err
}
if result.Err != "" {
return nil, gasReport.UsedInternally, fmt.Errorf("%s", result.Err)
}
return result.Ok, gasReport.UsedInternally, nil
return &result, gasReport.UsedInternally, nil
}

// Execute calls a given contract. Since the only difference between contracts with the same Checksum is the
Expand All @@ -169,7 +166,7 @@ func (vm *VM) Execute(
gasMeter GasMeter,
gasLimit uint64,
deserCost types.UFraction,
) (*types.Response, uint64, error) {
) (*types.ContractResult, uint64, error) {
envBin, err := json.Marshal(env)
if err != nil {
return nil, 0, err
Expand All @@ -188,10 +185,7 @@ func (vm *VM) Execute(
if err != nil {
return nil, gasReport.UsedInternally, err
}
if result.Err != "" {
return nil, gasReport.UsedInternally, fmt.Errorf("%s", result.Err)
}
return result.Ok, gasReport.UsedInternally, nil
return &result, gasReport.UsedInternally, nil
}

// Query allows a client to execute a contract-specific query. If the result is not empty, it should be
Expand All @@ -207,7 +201,7 @@ func (vm *VM) Query(
gasMeter GasMeter,
gasLimit uint64,
deserCost types.UFraction,
) ([]byte, uint64, error) {
) (*types.QueryResponse, uint64, error) {
envBin, err := json.Marshal(env)
if err != nil {
return nil, 0, err
Expand All @@ -222,10 +216,7 @@ func (vm *VM) Query(
if err != nil {
return nil, gasReport.UsedInternally, err
}
if resp.Err != "" {
return nil, gasReport.UsedInternally, fmt.Errorf("%s", resp.Err)
}
return resp.Ok, gasReport.UsedInternally, nil
return &resp, gasReport.UsedInternally, nil
}

// Migrate will migrate an existing contract to a new code binary.
Expand All @@ -244,7 +235,7 @@ func (vm *VM) Migrate(
gasMeter GasMeter,
gasLimit uint64,
deserCost types.UFraction,
) (*types.Response, uint64, error) {
) (*types.ContractResult, uint64, error) {
envBin, err := json.Marshal(env)
if err != nil {
return nil, 0, err
Expand All @@ -259,10 +250,7 @@ func (vm *VM) Migrate(
if err != nil {
return nil, gasReport.UsedInternally, err
}
if resp.Err != "" {
return nil, gasReport.UsedInternally, fmt.Errorf("%s", resp.Err)
}
return resp.Ok, gasReport.UsedInternally, nil
return &resp, gasReport.UsedInternally, nil
}

// Sudo allows native Go modules to make priviledged (sudo) calls on the contract.
Expand All @@ -281,7 +269,7 @@ func (vm *VM) Sudo(
gasMeter GasMeter,
gasLimit uint64,
deserCost types.UFraction,
) (*types.Response, uint64, error) {
) (*types.ContractResult, uint64, error) {
envBin, err := json.Marshal(env)
if err != nil {
return nil, 0, err
Expand All @@ -296,10 +284,7 @@ func (vm *VM) Sudo(
if err != nil {
return nil, gasReport.UsedInternally, err
}
if resp.Err != "" {
return nil, gasReport.UsedInternally, fmt.Errorf("%s", resp.Err)
}
return resp.Ok, gasReport.UsedInternally, nil
return &resp, gasReport.UsedInternally, nil
}

// Reply allows the native Go wasm modules to make a priviledged call to return the result
Expand All @@ -316,7 +301,7 @@ func (vm *VM) Reply(
gasMeter GasMeter,
gasLimit uint64,
deserCost types.UFraction,
) (*types.Response, uint64, error) {
) (*types.ContractResult, uint64, error) {
envBin, err := json.Marshal(env)
if err != nil {
return nil, 0, err
Expand All @@ -335,10 +320,7 @@ func (vm *VM) Reply(
if err != nil {
return nil, gasReport.UsedInternally, err
}
if resp.Err != "" {
return nil, gasReport.UsedInternally, fmt.Errorf("%s", resp.Err)
}
return resp.Ok, gasReport.UsedInternally, nil
return &resp, gasReport.UsedInternally, nil
}

// IBCChannelOpen is available on IBC-enabled contracts and is a hook to call into
Expand All @@ -353,7 +335,7 @@ func (vm *VM) IBCChannelOpen(
gasMeter GasMeter,
gasLimit uint64,
deserCost types.UFraction,
) (*types.IBC3ChannelOpenResponse, uint64, error) {
) (*types.IBCChannelOpenResult, uint64, error) {
envBin, err := json.Marshal(env)
if err != nil {
return nil, 0, err
Expand All @@ -372,10 +354,7 @@ func (vm *VM) IBCChannelOpen(
if err != nil {
return nil, gasReport.UsedInternally, err
}
if resp.Err != "" {
return nil, gasReport.UsedInternally, fmt.Errorf("%s", resp.Err)
}
return resp.Ok, gasReport.UsedInternally, nil
return &resp, gasReport.UsedInternally, nil
}

// IBCChannelConnect is available on IBC-enabled contracts and is a hook to call into
Expand All @@ -390,7 +369,7 @@ func (vm *VM) IBCChannelConnect(
gasMeter GasMeter,
gasLimit uint64,
deserCost types.UFraction,
) (*types.IBCBasicResponse, uint64, error) {
) (*types.IBCBasicResult, uint64, error) {
envBin, err := json.Marshal(env)
if err != nil {
return nil, 0, err
Expand All @@ -409,10 +388,7 @@ func (vm *VM) IBCChannelConnect(
if err != nil {
return nil, gasReport.UsedInternally, err
}
if resp.Err != "" {
return nil, gasReport.UsedInternally, fmt.Errorf("%s", resp.Err)
}
return resp.Ok, gasReport.UsedInternally, nil
return &resp, gasReport.UsedInternally, nil
}

// IBCChannelClose is available on IBC-enabled contracts and is a hook to call into
Expand All @@ -427,7 +403,7 @@ func (vm *VM) IBCChannelClose(
gasMeter GasMeter,
gasLimit uint64,
deserCost types.UFraction,
) (*types.IBCBasicResponse, uint64, error) {
) (*types.IBCBasicResult, uint64, error) {
envBin, err := json.Marshal(env)
if err != nil {
return nil, 0, err
Expand All @@ -446,10 +422,7 @@ func (vm *VM) IBCChannelClose(
if err != nil {
return nil, gasReport.UsedInternally, err
}
if resp.Err != "" {
return nil, gasReport.UsedInternally, fmt.Errorf("%s", resp.Err)
}
return resp.Ok, gasReport.UsedInternally, nil
return &resp, gasReport.UsedInternally, nil
}

// IBCPacketReceive is available on IBC-enabled contracts and is called when an incoming
Expand Down Expand Up @@ -499,7 +472,7 @@ func (vm *VM) IBCPacketAck(
gasMeter GasMeter,
gasLimit uint64,
deserCost types.UFraction,
) (*types.IBCBasicResponse, uint64, error) {
) (*types.IBCBasicResult, uint64, error) {
envBin, err := json.Marshal(env)
if err != nil {
return nil, 0, err
Expand All @@ -518,10 +491,7 @@ func (vm *VM) IBCPacketAck(
if err != nil {
return nil, gasReport.UsedInternally, err
}
if resp.Err != "" {
return nil, gasReport.UsedInternally, fmt.Errorf("%s", resp.Err)
}
return resp.Ok, gasReport.UsedInternally, nil
return &resp, gasReport.UsedInternally, nil
}

// IBCPacketTimeout is available on IBC-enabled contracts and is called when an
Expand All @@ -537,7 +507,7 @@ func (vm *VM) IBCPacketTimeout(
gasMeter GasMeter,
gasLimit uint64,
deserCost types.UFraction,
) (*types.IBCBasicResponse, uint64, error) {
) (*types.IBCBasicResult, uint64, error) {
envBin, err := json.Marshal(env)
if err != nil {
return nil, 0, err
Expand All @@ -556,10 +526,7 @@ func (vm *VM) IBCPacketTimeout(
if err != nil {
return nil, gasReport.UsedInternally, err
}
if resp.Err != "" {
return nil, gasReport.UsedInternally, fmt.Errorf("%s", resp.Err)
}
return resp.Ok, gasReport.UsedInternally, nil
return &resp, gasReport.UsedInternally, nil
}

func DeserializeResponse(gasLimit uint64, deserCost types.UFraction, gasReport *types.GasReport, data []byte, response any) error {
Expand Down
Loading

0 comments on commit 1dd4fa3

Please sign in to comment.