Skip to content

Commit

Permalink
Add ibc_source_chain_callback entrypoint
Browse files Browse the repository at this point in the history
  • Loading branch information
chipshort committed Feb 29, 2024
1 parent 82a4a08 commit 6350cbe
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 12 deletions.
12 changes: 12 additions & 0 deletions internal/api/bindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,18 @@ struct UnmanagedVector ibc_packet_timeout(struct cache_t *cache,
struct GasReport *gas_report,
struct UnmanagedVector *error_msg);

struct UnmanagedVector ibc_source_chain_callback(struct cache_t *cache,
struct ByteSliceView checksum,
struct ByteSliceView env,
struct ByteSliceView msg,
struct Db db,
struct GoApi api,
struct GoQuerier querier,
uint64_t gas_limit,
bool print_debug,
struct GasReport *gas_report,
struct UnmanagedVector *error_msg);

struct UnmanagedVector new_unmanaged_vector(bool nil, const uint8_t *ptr, uintptr_t length);

void destroy_unmanaged_vector(struct UnmanagedVector v);
Expand Down
42 changes: 42 additions & 0 deletions internal/api/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,48 @@ func IBCPacketTimeout(
return copyAndDestroyUnmanagedVector(res), convertGasReport(gasReport), nil
}

func IBCSourceChainCallback(
cache Cache,
checksum []byte,
env []byte,
msg []byte,
gasMeter *types.GasMeter,
store types.KVStore,
api *types.GoAPI,
querier *Querier,
gasLimit uint64,
printDebug bool,
) ([]byte, types.GasReport, error) {
cs := makeView(checksum)
defer runtime.KeepAlive(checksum)
e := makeView(env)
defer runtime.KeepAlive(env)
ac := makeView(msg)
defer runtime.KeepAlive(msg)
var pinner runtime.Pinner
pinner.Pin(gasMeter)
checkAndPinAPI(api, pinner)
checkAndPinQuerier(querier, pinner)
defer pinner.Unpin()

callID := startCall()
defer endCall(callID)

dbState := buildDBState(store, callID)
db := buildDB(&dbState, gasMeter)
a := buildAPI(api)
q := buildQuerier(querier)
var gasReport C.GasReport
errmsg := uninitializedUnmanagedVector()

res, err := C.ibc_source_chain_callback(cache.ptr, cs, e, ac, db, a, q, cu64(gasLimit), cbool(printDebug), &gasReport, &errmsg)
if err != nil && err.(syscall.Errno) != C.ErrnoValue_Success {
// Depending on the nature of the error, `gasUsed` will either have a meaningful value, or just 0.
return nil, convertGasReport(gasReport), errorWithMessage(err, errmsg)
}
return copyAndDestroyUnmanagedVector(res), convertGasReport(gasReport), nil
}

func convertGasReport(report C.GasReport) types.GasReport {
return types.GasReport{
Limit: uint64(report.limit),
Expand Down
35 changes: 35 additions & 0 deletions lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,41 @@ func (vm *VM) IBCPacketTimeout(
return &result, gasReport.UsedInternally, nil
}

// IBCSourceChainCallback is available on IBC-enabled contracts with and is called when an
// the response for an outgoing packet (previously sent by this contract)
// is received
func (vm *VM) IBCSourceChainCallback(
checksum Checksum,
env types.Env,
msg types.IBCSourceChainCallbackMsg,
store KVStore,
goapi GoAPI,
querier Querier,
gasMeter GasMeter,
gasLimit uint64,
deserCost types.UFraction,
) (*types.IBCBasicResult, uint64, error) {
envBin, err := json.Marshal(env)
if err != nil {
return nil, 0, err
}
msgBin, err := json.Marshal(msg)
if err != nil {
return nil, 0, err
}
data, gasReport, err := api.IBCSourceChainCallback(vm.cache, checksum, envBin, msgBin, &gasMeter, store, &goapi, &querier, gasLimit, vm.printDebug)
if err != nil {
return nil, gasReport.UsedInternally, err
}

var result types.IBCBasicResult
err = DeserializeResponse(gasLimit, deserCost, &gasReport, data, &result)
if err != nil {
return nil, gasReport.UsedInternally, err
}
return &result, gasReport.UsedInternally, nil
}

func compileCost(code WasmCode) uint64 {
// CostPerByte is how much CosmWasm gas is charged *per byte* for compiling WASM code.
// Benchmarks and numbers (in SDK Gas) were discussed in:
Expand Down
16 changes: 8 additions & 8 deletions libwasmvm/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions libwasmvm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ default = []
backtraces = []

[dependencies]
cosmwasm-std = { git = "https://github.com/CosmWasm/cosmwasm.git", rev = "v2.0.0-rc.0", features = ["staking", "stargate", "iterator"] }
cosmwasm-vm = { git = "https://github.com/CosmWasm/cosmwasm.git", rev = "v2.0.0-rc.0", features = ["staking", "stargate", "iterator"] }
# cosmwasm-std = { git = "https://github.com/CosmWasm/cosmwasm.git", rev = "v2.0.0-rc.1", features = ["staking", "stargate", "iterator"] }
# cosmwasm-vm = { git = "https://github.com/CosmWasm/cosmwasm.git", rev = "v2.0.0-rc.1", features = ["staking", "stargate", "iterator"] }
cosmwasm-std = { git = "https://github.com/CosmWasm/cosmwasm.git", branch = "ibc-callbacks", features = ["staking", "stargate", "iterator"] }
cosmwasm-vm = { git = "https://github.com/CosmWasm/cosmwasm.git", branch = "ibc-callbacks", features = ["staking", "stargate", "iterator"] }
errno = "0.3.8"
serde_json = "1.0.91"
thiserror = "1.0.38"
Expand Down
12 changes: 12 additions & 0 deletions libwasmvm/bindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,18 @@ struct UnmanagedVector ibc_packet_timeout(struct cache_t *cache,
struct GasReport *gas_report,
struct UnmanagedVector *error_msg);

struct UnmanagedVector ibc_source_chain_callback(struct cache_t *cache,
struct ByteSliceView checksum,
struct ByteSliceView env,
struct ByteSliceView msg,
struct Db db,
struct GoApi api,
struct GoQuerier querier,
uint64_t gas_limit,
bool print_debug,
struct GasReport *gas_report,
struct UnmanagedVector *error_msg);

struct UnmanagedVector new_unmanaged_vector(bool nil, const uint8_t *ptr, uintptr_t length);

void destroy_unmanaged_vector(struct UnmanagedVector v);
Expand Down
35 changes: 33 additions & 2 deletions libwasmvm/src/calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ use cosmwasm_std::Checksum;
use cosmwasm_vm::{
call_execute_raw, call_ibc_channel_close_raw, call_ibc_channel_connect_raw,
call_ibc_channel_open_raw, call_ibc_packet_ack_raw, call_ibc_packet_receive_raw,
call_ibc_packet_timeout_raw, call_instantiate_raw, call_migrate_raw, call_query_raw,
call_reply_raw, call_sudo_raw, Backend, Cache, Instance, InstanceOptions, VmResult,
call_ibc_packet_timeout_raw, call_ibc_source_chain_callback_raw, call_instantiate_raw,
call_migrate_raw, call_query_raw, call_reply_raw, call_sudo_raw, Backend, Cache, Instance,
InstanceOptions, VmResult,
};

use crate::api::GoApi;
Expand Down Expand Up @@ -395,6 +396,36 @@ pub extern "C" fn ibc_packet_timeout(
)
}

#[no_mangle]
pub extern "C" fn ibc_source_chain_callback(
cache: *mut cache_t,
checksum: ByteSliceView,
env: ByteSliceView,
msg: ByteSliceView,
db: Db,
api: GoApi,
querier: GoQuerier,
gas_limit: u64,
print_debug: bool,
gas_report: Option<&mut GasReport>,
error_msg: Option<&mut UnmanagedVector>,
) -> UnmanagedVector {
call_2_args(
call_ibc_source_chain_callback_raw,
cache,
checksum,
env,
msg,
db,
api,
querier,
gas_limit,
print_debug,
gas_report,
error_msg,
)
}

type VmFn2Args = fn(
instance: &mut Instance<GoApi, GoStorage, GoQuerier>,
arg1: &[u8],
Expand Down

0 comments on commit 6350cbe

Please sign in to comment.