Skip to content

Commit 698cee4

Browse files
committed
Add ibc_source_chain_callback entrypoint
1 parent 3391e19 commit 698cee4

File tree

5 files changed

+134
-2
lines changed

5 files changed

+134
-2
lines changed

internal/api/bindings.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,18 @@ struct UnmanagedVector ibc_packet_timeout(struct cache_t *cache,
597597
struct GasReport *gas_report,
598598
struct UnmanagedVector *error_msg);
599599

600+
struct UnmanagedVector ibc_source_chain_callback(struct cache_t *cache,
601+
struct ByteSliceView checksum,
602+
struct ByteSliceView env,
603+
struct ByteSliceView msg,
604+
struct Db db,
605+
struct GoApi api,
606+
struct GoQuerier querier,
607+
uint64_t gas_limit,
608+
bool print_debug,
609+
struct GasReport *gas_report,
610+
struct UnmanagedVector *error_msg);
611+
600612
struct UnmanagedVector new_unmanaged_vector(bool nil, const uint8_t *ptr, uintptr_t length);
601613

602614
void destroy_unmanaged_vector(struct UnmanagedVector v);

internal/api/lib.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,48 @@ func IBCPacketTimeout(
700700
return copyAndDestroyUnmanagedVector(res), convertGasReport(gasReport), nil
701701
}
702702

703+
func IBCSourceChainCallback(
704+
cache Cache,
705+
checksum []byte,
706+
env []byte,
707+
msg []byte,
708+
gasMeter *types.GasMeter,
709+
store types.KVStore,
710+
api *types.GoAPI,
711+
querier *Querier,
712+
gasLimit uint64,
713+
printDebug bool,
714+
) ([]byte, types.GasReport, error) {
715+
cs := makeView(checksum)
716+
defer runtime.KeepAlive(checksum)
717+
e := makeView(env)
718+
defer runtime.KeepAlive(env)
719+
ac := makeView(msg)
720+
defer runtime.KeepAlive(msg)
721+
var pinner runtime.Pinner
722+
pinner.Pin(gasMeter)
723+
checkAndPinAPI(api, pinner)
724+
checkAndPinQuerier(querier, pinner)
725+
defer pinner.Unpin()
726+
727+
callID := startCall()
728+
defer endCall(callID)
729+
730+
dbState := buildDBState(store, callID)
731+
db := buildDB(&dbState, gasMeter)
732+
a := buildAPI(api)
733+
q := buildQuerier(querier)
734+
var gasReport C.GasReport
735+
errmsg := uninitializedUnmanagedVector()
736+
737+
res, err := C.ibc_source_chain_callback(cache.ptr, cs, e, ac, db, a, q, cu64(gasLimit), cbool(printDebug), &gasReport, &errmsg)
738+
if err != nil && err.(syscall.Errno) != C.ErrnoValue_Success {
739+
// Depending on the nature of the error, `gasUsed` will either have a meaningful value, or just 0.
740+
return nil, convertGasReport(gasReport), errorWithMessage(err, errmsg)
741+
}
742+
return copyAndDestroyUnmanagedVector(res), convertGasReport(gasReport), nil
743+
}
744+
703745
func convertGasReport(report C.GasReport) types.GasReport {
704746
return types.GasReport{
705747
Limit: uint64(report.limit),

lib_libwasmvm.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,41 @@ func (vm *VM) IBCPacketTimeout(
532532
return &result, gasReport.UsedInternally, nil
533533
}
534534

535+
// IBCSourceChainCallback is available on IBC-enabled contracts with and is called when an
536+
// the response for an outgoing packet (previously sent by this contract)
537+
// is received
538+
func (vm *VM) IBCSourceChainCallback(
539+
checksum Checksum,
540+
env types.Env,
541+
msg types.IBCSourceChainCallbackMsg,
542+
store KVStore,
543+
goapi GoAPI,
544+
querier Querier,
545+
gasMeter GasMeter,
546+
gasLimit uint64,
547+
deserCost types.UFraction,
548+
) (*types.IBCBasicResult, uint64, error) {
549+
envBin, err := json.Marshal(env)
550+
if err != nil {
551+
return nil, 0, err
552+
}
553+
msgBin, err := json.Marshal(msg)
554+
if err != nil {
555+
return nil, 0, err
556+
}
557+
data, gasReport, err := api.IBCSourceChainCallback(vm.cache, checksum, envBin, msgBin, &gasMeter, store, &goapi, &querier, gasLimit, vm.printDebug)
558+
if err != nil {
559+
return nil, gasReport.UsedInternally, err
560+
}
561+
562+
var result types.IBCBasicResult
563+
err = DeserializeResponse(gasLimit, deserCost, &gasReport, data, &result)
564+
if err != nil {
565+
return nil, gasReport.UsedInternally, err
566+
}
567+
return &result, gasReport.UsedInternally, nil
568+
}
569+
535570
func compileCost(code WasmCode) uint64 {
536571
// CostPerByte is how much CosmWasm gas is charged *per byte* for compiling WASM code.
537572
// Benchmarks and numbers (in SDK Gas) were discussed in:

libwasmvm/bindings.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,18 @@ struct UnmanagedVector ibc_packet_timeout(struct cache_t *cache,
597597
struct GasReport *gas_report,
598598
struct UnmanagedVector *error_msg);
599599

600+
struct UnmanagedVector ibc_source_chain_callback(struct cache_t *cache,
601+
struct ByteSliceView checksum,
602+
struct ByteSliceView env,
603+
struct ByteSliceView msg,
604+
struct Db db,
605+
struct GoApi api,
606+
struct GoQuerier querier,
607+
uint64_t gas_limit,
608+
bool print_debug,
609+
struct GasReport *gas_report,
610+
struct UnmanagedVector *error_msg);
611+
600612
struct UnmanagedVector new_unmanaged_vector(bool nil, const uint8_t *ptr, uintptr_t length);
601613

602614
void destroy_unmanaged_vector(struct UnmanagedVector v);

libwasmvm/src/calls.rs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ use cosmwasm_std::Checksum;
99
use cosmwasm_vm::{
1010
call_execute_raw, call_ibc_channel_close_raw, call_ibc_channel_connect_raw,
1111
call_ibc_channel_open_raw, call_ibc_packet_ack_raw, call_ibc_packet_receive_raw,
12-
call_ibc_packet_timeout_raw, call_instantiate_raw, call_migrate_raw, call_query_raw,
13-
call_reply_raw, call_sudo_raw, Backend, Cache, Instance, InstanceOptions, VmResult,
12+
call_ibc_packet_timeout_raw, call_ibc_source_chain_callback_raw, call_instantiate_raw,
13+
call_migrate_raw, call_query_raw, call_reply_raw, call_sudo_raw, Backend, Cache, Instance,
14+
InstanceOptions, VmResult,
1415
};
1516

1617
use crate::api::GoApi;
@@ -395,6 +396,36 @@ pub extern "C" fn ibc_packet_timeout(
395396
)
396397
}
397398

399+
#[no_mangle]
400+
pub extern "C" fn ibc_source_chain_callback(
401+
cache: *mut cache_t,
402+
checksum: ByteSliceView,
403+
env: ByteSliceView,
404+
msg: ByteSliceView,
405+
db: Db,
406+
api: GoApi,
407+
querier: GoQuerier,
408+
gas_limit: u64,
409+
print_debug: bool,
410+
gas_report: Option<&mut GasReport>,
411+
error_msg: Option<&mut UnmanagedVector>,
412+
) -> UnmanagedVector {
413+
call_2_args(
414+
call_ibc_source_chain_callback_raw,
415+
cache,
416+
checksum,
417+
env,
418+
msg,
419+
db,
420+
api,
421+
querier,
422+
gas_limit,
423+
print_debug,
424+
gas_report,
425+
error_msg,
426+
)
427+
}
428+
398429
type VmFn2Args = fn(
399430
instance: &mut Instance<GoApi, GoStorage, GoQuerier>,
400431
arg1: &[u8],

0 commit comments

Comments
 (0)