Skip to content

Commit

Permalink
Merge pull request #478 from CosmWasm/GoIter-creation
Browse files Browse the repository at this point in the history
Improve clarity around GoIter creation; bump min Go version to 1.21
  • Loading branch information
webmaster128 authored Nov 29, 2023
2 parents ea6914a + f722521 commit 589ea5a
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 14 deletions.
14 changes: 7 additions & 7 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ jobs:

format-go:
docker:
- image: cimg/go:1.19.7
- image: cimg/go:1.21.4
steps:
- run:
name: Install gofumpt
Expand All @@ -186,7 +186,7 @@ jobs:
# Build types and cosmwam package without cgo
wasmvm_no_cgo:
docker:
- image: cimg/go:1.19.7
- image: cimg/go:1.21.4
steps:
- checkout
- run:
Expand All @@ -204,12 +204,12 @@ jobs:

tidy-go:
docker:
- image: cimg/go:1.19.7
- image: cimg/go:1.21.4
steps:
- checkout
- run:
name: Check go mod tidy
# Use --check or --exit-code when available (Go 1.19?)
# Use --check or --exit-code when available (Go 1.22?)
# https://github.com/golang/go/issues/27005
command: |
go mod tidy
Expand All @@ -222,7 +222,7 @@ jobs:
format-scripts:
docker:
- image: cimg/go:1.21.3
- image: cimg/go:1.21.4
steps:
- run:
name: Install shfmt
Expand Down Expand Up @@ -279,7 +279,7 @@ jobs:
# Test the Go project
wasmvm_test:
docker:
- image: cimg/go:1.19.7
- image: cimg/go:1.21.4
environment:
GORACE: "halt_on_error=1"
BUILD_VERSION: $(echo ${CIRCLE_SHA1} | cut -c 1-10)
Expand Down Expand Up @@ -458,7 +458,7 @@ workflows:
# long living branches
- main
# Development
- no-go-in-builder
- GoIter-creation
# Run only on main, not on tags (auto-build on merge PR)
- deploy_to_git:
requires:
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ test:

test-safety:
# Use package list mode to include all subdirectores. The -count=1 turns off caching.
GODEBUG=cgocheck=2 go test -race -v -count=1 ./...
GOEXPERIMENT=cgocheck2 go test -race -v -count=1 ./...

# Creates a release build in a containerized build environment of the static library for Alpine Linux (.a)
release-build-alpine:
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/CosmWasm/wasmvm

go 1.18
go 1.21

require (
github.com/google/btree v1.0.0
Expand Down
8 changes: 6 additions & 2 deletions internal/api/callbacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,12 @@ func cScan(ptr *C.db_t, gasMeter *C.gas_meter_t, usedGas *cu64, start C.U8SliceV
return C.GoError_User
}

out.state = cIterator
out.vtable = iterator_vtable
*out = C.GoIter{
gas_meter: gasMeter,
state: cIterator,
vtable: iterator_vtable,
}

return C.GoError_None
}

Expand Down
36 changes: 36 additions & 0 deletions internal/api/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,9 @@ func Instantiate(
defer runtime.KeepAlive(info)
m := makeView(msg)
defer runtime.KeepAlive(msg)
var pinner runtime.Pinner
pinner.Pin(gasMeter)
defer pinner.Unpin()

callID := startCall()
defer endCall(callID)
Expand Down Expand Up @@ -221,6 +224,9 @@ func Execute(
defer runtime.KeepAlive(info)
m := makeView(msg)
defer runtime.KeepAlive(msg)
var pinner runtime.Pinner
pinner.Pin(gasMeter)
defer pinner.Unpin()

callID := startCall()
defer endCall(callID)
Expand Down Expand Up @@ -258,6 +264,9 @@ func Migrate(
defer runtime.KeepAlive(env)
m := makeView(msg)
defer runtime.KeepAlive(msg)
var pinner runtime.Pinner
pinner.Pin(gasMeter)
defer pinner.Unpin()

callID := startCall()
defer endCall(callID)
Expand Down Expand Up @@ -295,6 +304,9 @@ func Sudo(
defer runtime.KeepAlive(env)
m := makeView(msg)
defer runtime.KeepAlive(msg)
var pinner runtime.Pinner
pinner.Pin(gasMeter)
defer pinner.Unpin()

callID := startCall()
defer endCall(callID)
Expand Down Expand Up @@ -332,6 +344,9 @@ func Reply(
defer runtime.KeepAlive(env)
r := makeView(reply)
defer runtime.KeepAlive(reply)
var pinner runtime.Pinner
pinner.Pin(gasMeter)
defer pinner.Unpin()

callID := startCall()
defer endCall(callID)
Expand Down Expand Up @@ -369,6 +384,9 @@ func Query(
defer runtime.KeepAlive(env)
m := makeView(msg)
defer runtime.KeepAlive(msg)
var pinner runtime.Pinner
pinner.Pin(gasMeter)
defer pinner.Unpin()

callID := startCall()
defer endCall(callID)
Expand Down Expand Up @@ -406,6 +424,9 @@ func IBCChannelOpen(
defer runtime.KeepAlive(env)
m := makeView(msg)
defer runtime.KeepAlive(msg)
var pinner runtime.Pinner
pinner.Pin(gasMeter)
defer pinner.Unpin()

callID := startCall()
defer endCall(callID)
Expand Down Expand Up @@ -443,6 +464,9 @@ func IBCChannelConnect(
defer runtime.KeepAlive(env)
m := makeView(msg)
defer runtime.KeepAlive(msg)
var pinner runtime.Pinner
pinner.Pin(gasMeter)
defer pinner.Unpin()

callID := startCall()
defer endCall(callID)
Expand Down Expand Up @@ -480,6 +504,9 @@ func IBCChannelClose(
defer runtime.KeepAlive(env)
m := makeView(msg)
defer runtime.KeepAlive(msg)
var pinner runtime.Pinner
pinner.Pin(gasMeter)
defer pinner.Unpin()

callID := startCall()
defer endCall(callID)
Expand Down Expand Up @@ -517,6 +544,9 @@ func IBCPacketReceive(
defer runtime.KeepAlive(env)
pa := makeView(packet)
defer runtime.KeepAlive(packet)
var pinner runtime.Pinner
pinner.Pin(gasMeter)
defer pinner.Unpin()

callID := startCall()
defer endCall(callID)
Expand Down Expand Up @@ -554,6 +584,9 @@ func IBCPacketAck(
defer runtime.KeepAlive(env)
ac := makeView(ack)
defer runtime.KeepAlive(ack)
var pinner runtime.Pinner
pinner.Pin(gasMeter)
defer pinner.Unpin()

callID := startCall()
defer endCall(callID)
Expand Down Expand Up @@ -591,6 +624,9 @@ func IBCPacketTimeout(
defer runtime.KeepAlive(env)
pa := makeView(packet)
defer runtime.KeepAlive(packet)
var pinner runtime.Pinner
pinner.Pin(gasMeter)
defer pinner.Unpin()

callID := startCall()
defer endCall(callID)
Expand Down
31 changes: 29 additions & 2 deletions libwasmvm/src/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,14 @@ pub struct GoIter {
}

impl GoIter {
pub fn new(gas_meter: *mut gas_meter_t) -> Self {
/// Creates an incomplete GoIter with unset fields.
/// This is not ready to be used until those fields are set.
///
/// This is needed to create a correct instance in Rust
/// which is then filled in Go (see `fn scan`).
pub fn stub() -> Self {
GoIter {
gas_meter,
gas_meter: std::ptr::null_mut(),
state: iterator_t::default(),
vtable: IteratorVtable::default(),
}
Expand Down Expand Up @@ -170,3 +175,25 @@ impl GoIter {
(Ok(output), gas_info)
}
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn goiter_stub_works() {
// can be created and dropped
{
let _iter = GoIter::stub();
}

// creates an all null-instance
let iter = GoIter::stub();
assert!(iter.gas_meter.is_null());
assert_eq!(iter.state.call_id, 0);
assert_eq!(iter.state.iterator_index, 0);
assert!(iter.vtable.next.is_none());
assert!(iter.vtable.next_key.is_none());
assert!(iter.vtable.next_value.is_none());
}
}
2 changes: 1 addition & 1 deletion libwasmvm/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl Storage for GoStorage {
order: Order,
) -> BackendResult<u32> {
let mut error_msg = UnmanagedVector::default();
let mut iter = GoIter::new(self.db.gas_meter);
let mut iter = GoIter::stub();
let mut used_gas = 0_u64;
let scan_db = self
.db
Expand Down

0 comments on commit 589ea5a

Please sign in to comment.