Skip to content

Commit

Permalink
Adjust go side to new libwasmvm interface
Browse files Browse the repository at this point in the history
  • Loading branch information
chipshort committed Aug 17, 2023
1 parent b267953 commit d548efc
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 60 deletions.
44 changes: 32 additions & 12 deletions internal/api/bindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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);
Expand Down
101 changes: 53 additions & 48 deletions internal/api/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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 ***/
Expand Down

0 comments on commit d548efc

Please sign in to comment.