From d548efc537d496592510f1c7c4e94c7c071251f3 Mon Sep 17 00:00:00 2001 From: Christoph Otter Date: Thu, 17 Aug 2023 13:38:03 +0200 Subject: [PATCH] Adjust go side to new libwasmvm interface --- internal/api/bindings.h | 44 ++++++++++++----- internal/api/lib.go | 101 +++++++++++++++++++++------------------- 2 files changed, 85 insertions(+), 60 deletions(-) diff --git a/internal/api/bindings.h b/internal/api/bindings.h index 60f301408..1f097dcec 100644 --- a/internal/api/bindings.h +++ b/internal/api/bindings.h @@ -342,6 +342,26 @@ typedef struct GoQuerier { struct Querier_vtable vtable; } GoQuerier; +typedef struct GasReport { + /** + * The original limit the instance was created with + */ + uint64_t limit; + /** + * The remaining gas that can be spend + */ + uint64_t remaining; + /** + * The amount of gas that was spend and metered externally in operations triggered by this instance + */ + uint64_t used_externally; + /** + * The amount of gas that was spend and metered internally (i.e. by executing Wasm and calling + * API methods which are not metered externally) + */ + uint64_t used_internally; +} GasReport; + struct cache_t *init_cache(struct ByteSliceView data_dir, struct ByteSliceView available_capabilities, uint32_t cache_size, @@ -391,7 +411,7 @@ struct UnmanagedVector instantiate(struct cache_t *cache, struct GoQuerier querier, uint64_t gas_limit, bool print_debug, - uint64_t *gas_used, + struct GasReport *gas_used, struct UnmanagedVector *error_msg); struct UnmanagedVector execute(struct cache_t *cache, @@ -404,7 +424,7 @@ struct UnmanagedVector execute(struct cache_t *cache, struct GoQuerier querier, uint64_t gas_limit, bool print_debug, - uint64_t *gas_used, + struct GasReport *gas_used, struct UnmanagedVector *error_msg); struct UnmanagedVector migrate(struct cache_t *cache, @@ -416,7 +436,7 @@ struct UnmanagedVector migrate(struct cache_t *cache, struct GoQuerier querier, uint64_t gas_limit, bool print_debug, - uint64_t *gas_used, + struct GasReport *gas_used, struct UnmanagedVector *error_msg); struct UnmanagedVector sudo(struct cache_t *cache, @@ -428,7 +448,7 @@ struct UnmanagedVector sudo(struct cache_t *cache, struct GoQuerier querier, uint64_t gas_limit, bool print_debug, - uint64_t *gas_used, + struct GasReport *gas_used, struct UnmanagedVector *error_msg); struct UnmanagedVector reply(struct cache_t *cache, @@ -440,7 +460,7 @@ struct UnmanagedVector reply(struct cache_t *cache, struct GoQuerier querier, uint64_t gas_limit, bool print_debug, - uint64_t *gas_used, + struct GasReport *gas_used, struct UnmanagedVector *error_msg); struct UnmanagedVector query(struct cache_t *cache, @@ -452,7 +472,7 @@ struct UnmanagedVector query(struct cache_t *cache, struct GoQuerier querier, uint64_t gas_limit, bool print_debug, - uint64_t *gas_used, + struct GasReport *gas_used, struct UnmanagedVector *error_msg); struct UnmanagedVector ibc_channel_open(struct cache_t *cache, @@ -464,7 +484,7 @@ struct UnmanagedVector ibc_channel_open(struct cache_t *cache, struct GoQuerier querier, uint64_t gas_limit, bool print_debug, - uint64_t *gas_used, + struct GasReport *gas_used, struct UnmanagedVector *error_msg); struct UnmanagedVector ibc_channel_connect(struct cache_t *cache, @@ -476,7 +496,7 @@ struct UnmanagedVector ibc_channel_connect(struct cache_t *cache, struct GoQuerier querier, uint64_t gas_limit, bool print_debug, - uint64_t *gas_used, + struct GasReport *gas_used, struct UnmanagedVector *error_msg); struct UnmanagedVector ibc_channel_close(struct cache_t *cache, @@ -488,7 +508,7 @@ struct UnmanagedVector ibc_channel_close(struct cache_t *cache, struct GoQuerier querier, uint64_t gas_limit, bool print_debug, - uint64_t *gas_used, + struct GasReport *gas_used, struct UnmanagedVector *error_msg); struct UnmanagedVector ibc_packet_receive(struct cache_t *cache, @@ -500,7 +520,7 @@ struct UnmanagedVector ibc_packet_receive(struct cache_t *cache, struct GoQuerier querier, uint64_t gas_limit, bool print_debug, - uint64_t *gas_used, + struct GasReport *gas_used, struct UnmanagedVector *error_msg); struct UnmanagedVector ibc_packet_ack(struct cache_t *cache, @@ -512,7 +532,7 @@ struct UnmanagedVector ibc_packet_ack(struct cache_t *cache, struct GoQuerier querier, uint64_t gas_limit, bool print_debug, - uint64_t *gas_used, + struct GasReport *gas_used, struct UnmanagedVector *error_msg); struct UnmanagedVector ibc_packet_timeout(struct cache_t *cache, @@ -524,7 +544,7 @@ struct UnmanagedVector ibc_packet_timeout(struct cache_t *cache, struct GoQuerier querier, uint64_t gas_limit, bool print_debug, - uint64_t *gas_used, + struct GasReport *gas_used, struct UnmanagedVector *error_msg); struct UnmanagedVector new_unmanaged_vector(bool nil, const uint8_t *ptr, uintptr_t length); diff --git a/internal/api/lib.go b/internal/api/lib.go index 4f4f07236..1e4240d01 100644 --- a/internal/api/lib.go +++ b/internal/api/lib.go @@ -189,15 +189,20 @@ func Instantiate( db := buildDB(&dbState, gasMeter) a := buildAPI(api) q := buildQuerier(querier) - var gasUsed cu64 + var gasReport C.GasReport errmsg := uninitializedUnmanagedVector() - res, err := C.instantiate(cache.ptr, cs, e, i, m, db, a, q, cu64(gasLimit), cbool(printDebug), &gasUsed, &errmsg) + res, err := C.instantiate(cache.ptr, cs, e, i, m, db, a, q, cu64(gasLimit), cbool(printDebug), &gasReport, &errmsg) + + test := uint64(gasReport.used_internally) + if false { + panic(test) + } 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, uint64(gasUsed), errorWithMessage(err, errmsg) + return nil, uint64(gasReport.used_internally), errorWithMessage(err, errmsg) } - return copyAndDestroyUnmanagedVector(res), uint64(gasUsed), nil + return copyAndDestroyUnmanagedVector(res), uint64(gasReport.used_internally), nil } func Execute( @@ -229,15 +234,15 @@ func Execute( db := buildDB(&dbState, gasMeter) a := buildAPI(api) q := buildQuerier(querier) - var gasUsed cu64 + var gasReport C.GasReport errmsg := uninitializedUnmanagedVector() - res, err := C.execute(cache.ptr, cs, e, i, m, db, a, q, cu64(gasLimit), cbool(printDebug), &gasUsed, &errmsg) + res, err := C.execute(cache.ptr, cs, e, i, m, 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, uint64(gasUsed), errorWithMessage(err, errmsg) + return nil, uint64(gasReport.used_internally), errorWithMessage(err, errmsg) } - return copyAndDestroyUnmanagedVector(res), uint64(gasUsed), nil + return copyAndDestroyUnmanagedVector(res), uint64(gasReport.used_internally), nil } func Migrate( @@ -266,15 +271,15 @@ func Migrate( db := buildDB(&dbState, gasMeter) a := buildAPI(api) q := buildQuerier(querier) - var gasUsed cu64 + var gasReport C.GasReport errmsg := uninitializedUnmanagedVector() - res, err := C.migrate(cache.ptr, cs, e, m, db, a, q, cu64(gasLimit), cbool(printDebug), &gasUsed, &errmsg) + res, err := C.migrate(cache.ptr, cs, e, m, 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, uint64(gasUsed), errorWithMessage(err, errmsg) + return nil, uint64(gasReport.used_internally), errorWithMessage(err, errmsg) } - return copyAndDestroyUnmanagedVector(res), uint64(gasUsed), nil + return copyAndDestroyUnmanagedVector(res), uint64(gasReport.used_internally), nil } func Sudo( @@ -303,15 +308,15 @@ func Sudo( db := buildDB(&dbState, gasMeter) a := buildAPI(api) q := buildQuerier(querier) - var gasUsed cu64 + var gasReport C.GasReport errmsg := uninitializedUnmanagedVector() - res, err := C.sudo(cache.ptr, cs, e, m, db, a, q, cu64(gasLimit), cbool(printDebug), &gasUsed, &errmsg) + res, err := C.sudo(cache.ptr, cs, e, m, 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, uint64(gasUsed), errorWithMessage(err, errmsg) + return nil, uint64(gasReport.used_internally), errorWithMessage(err, errmsg) } - return copyAndDestroyUnmanagedVector(res), uint64(gasUsed), nil + return copyAndDestroyUnmanagedVector(res), uint64(gasReport.used_internally), nil } func Reply( @@ -340,15 +345,15 @@ func Reply( db := buildDB(&dbState, gasMeter) a := buildAPI(api) q := buildQuerier(querier) - var gasUsed cu64 + var gasReport C.GasReport errmsg := uninitializedUnmanagedVector() - res, err := C.reply(cache.ptr, cs, e, r, db, a, q, cu64(gasLimit), cbool(printDebug), &gasUsed, &errmsg) + res, err := C.reply(cache.ptr, cs, e, r, 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, uint64(gasUsed), errorWithMessage(err, errmsg) + return nil, uint64(gasReport.used_internally), errorWithMessage(err, errmsg) } - return copyAndDestroyUnmanagedVector(res), uint64(gasUsed), nil + return copyAndDestroyUnmanagedVector(res), uint64(gasReport.used_internally), nil } func Query( @@ -377,15 +382,15 @@ func Query( db := buildDB(&dbState, gasMeter) a := buildAPI(api) q := buildQuerier(querier) - var gasUsed cu64 + var gasReport C.GasReport errmsg := uninitializedUnmanagedVector() - res, err := C.query(cache.ptr, cs, e, m, db, a, q, cu64(gasLimit), cbool(printDebug), &gasUsed, &errmsg) + res, err := C.query(cache.ptr, cs, e, m, 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, uint64(gasUsed), errorWithMessage(err, errmsg) + return nil, uint64(gasReport.used_internally), errorWithMessage(err, errmsg) } - return copyAndDestroyUnmanagedVector(res), uint64(gasUsed), nil + return copyAndDestroyUnmanagedVector(res), uint64(gasReport.used_internally), nil } func IBCChannelOpen( @@ -414,15 +419,15 @@ func IBCChannelOpen( db := buildDB(&dbState, gasMeter) a := buildAPI(api) q := buildQuerier(querier) - var gasUsed cu64 + var gasReport C.GasReport errmsg := uninitializedUnmanagedVector() - res, err := C.ibc_channel_open(cache.ptr, cs, e, m, db, a, q, cu64(gasLimit), cbool(printDebug), &gasUsed, &errmsg) + res, err := C.ibc_channel_open(cache.ptr, cs, e, m, 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, uint64(gasUsed), errorWithMessage(err, errmsg) + return nil, uint64(gasReport.used_internally), errorWithMessage(err, errmsg) } - return copyAndDestroyUnmanagedVector(res), uint64(gasUsed), nil + return copyAndDestroyUnmanagedVector(res), uint64(gasReport.used_internally), nil } func IBCChannelConnect( @@ -451,15 +456,15 @@ func IBCChannelConnect( db := buildDB(&dbState, gasMeter) a := buildAPI(api) q := buildQuerier(querier) - var gasUsed cu64 + var gasReport C.GasReport errmsg := uninitializedUnmanagedVector() - res, err := C.ibc_channel_connect(cache.ptr, cs, e, m, db, a, q, cu64(gasLimit), cbool(printDebug), &gasUsed, &errmsg) + res, err := C.ibc_channel_connect(cache.ptr, cs, e, m, 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, uint64(gasUsed), errorWithMessage(err, errmsg) + return nil, uint64(gasReport.used_internally), errorWithMessage(err, errmsg) } - return copyAndDestroyUnmanagedVector(res), uint64(gasUsed), nil + return copyAndDestroyUnmanagedVector(res), uint64(gasReport.used_internally), nil } func IBCChannelClose( @@ -488,15 +493,15 @@ func IBCChannelClose( db := buildDB(&dbState, gasMeter) a := buildAPI(api) q := buildQuerier(querier) - var gasUsed cu64 + var gasReport C.GasReport errmsg := uninitializedUnmanagedVector() - res, err := C.ibc_channel_close(cache.ptr, cs, e, m, db, a, q, cu64(gasLimit), cbool(printDebug), &gasUsed, &errmsg) + res, err := C.ibc_channel_close(cache.ptr, cs, e, m, 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, uint64(gasUsed), errorWithMessage(err, errmsg) + return nil, uint64(gasReport.used_internally), errorWithMessage(err, errmsg) } - return copyAndDestroyUnmanagedVector(res), uint64(gasUsed), nil + return copyAndDestroyUnmanagedVector(res), uint64(gasReport.used_internally), nil } func IBCPacketReceive( @@ -525,15 +530,15 @@ func IBCPacketReceive( db := buildDB(&dbState, gasMeter) a := buildAPI(api) q := buildQuerier(querier) - var gasUsed cu64 + var gasReport C.GasReport errmsg := uninitializedUnmanagedVector() - res, err := C.ibc_packet_receive(cache.ptr, cs, e, pa, db, a, q, cu64(gasLimit), cbool(printDebug), &gasUsed, &errmsg) + res, err := C.ibc_packet_receive(cache.ptr, cs, e, pa, 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, uint64(gasUsed), errorWithMessage(err, errmsg) + return nil, uint64(gasReport.used_internally), errorWithMessage(err, errmsg) } - return copyAndDestroyUnmanagedVector(res), uint64(gasUsed), nil + return copyAndDestroyUnmanagedVector(res), uint64(gasReport.used_internally), nil } func IBCPacketAck( @@ -562,15 +567,15 @@ func IBCPacketAck( db := buildDB(&dbState, gasMeter) a := buildAPI(api) q := buildQuerier(querier) - var gasUsed cu64 + var gasReport C.GasReport errmsg := uninitializedUnmanagedVector() - res, err := C.ibc_packet_ack(cache.ptr, cs, e, ac, db, a, q, cu64(gasLimit), cbool(printDebug), &gasUsed, &errmsg) + res, err := C.ibc_packet_ack(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, uint64(gasUsed), errorWithMessage(err, errmsg) + return nil, uint64(gasReport.used_internally), errorWithMessage(err, errmsg) } - return copyAndDestroyUnmanagedVector(res), uint64(gasUsed), nil + return copyAndDestroyUnmanagedVector(res), uint64(gasReport.used_internally), nil } func IBCPacketTimeout( @@ -599,15 +604,15 @@ func IBCPacketTimeout( db := buildDB(&dbState, gasMeter) a := buildAPI(api) q := buildQuerier(querier) - var gasUsed cu64 + var gasReport C.GasReport errmsg := uninitializedUnmanagedVector() - res, err := C.ibc_packet_timeout(cache.ptr, cs, e, pa, db, a, q, cu64(gasLimit), cbool(printDebug), &gasUsed, &errmsg) + res, err := C.ibc_packet_timeout(cache.ptr, cs, e, pa, 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, uint64(gasUsed), errorWithMessage(err, errmsg) + return nil, uint64(gasReport.used_internally), errorWithMessage(err, errmsg) } - return copyAndDestroyUnmanagedVector(res), uint64(gasUsed), nil + return copyAndDestroyUnmanagedVector(res), uint64(gasReport.used_internally), nil } /**** To error module ***/