Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add bindings for the pinned metrics #526

Merged
merged 20 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions flake.lock

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

36 changes: 36 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
description = "Devshell flake";

inputs = {
flake-utils.url = "github:numtide/flake-utils";
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
rust-overlay = {
inputs = {
flake-utils.follows = "flake-utils";
nixpkgs.follows = "nixpkgs";
};
url = "github:oxalica/rust-overlay";
};
};

outputs = { self, nixpkgs, flake-utils, rust-overlay } @ inputs:
flake-utils.lib.eachDefaultSystem (system:
let
overlays = [ (import rust-overlay) ];
pkgs = import nixpkgs {
inherit overlays system;
};
in
{
formatter = pkgs.nixpkgs-fmt;

devShells.default = pkgs.mkShell {
buildInputs = with pkgs; [
go
gofumpt
golangci-lint
];
aumetra marked this conversation as resolved.
Show resolved Hide resolved
};
}
);
}
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ go 1.21

require (
github.com/google/btree v1.0.0
github.com/shamaton/msgpack/v2 v2.2.0
github.com/stretchr/testify v1.8.1
golang.org/x/sys v0.16.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/kr/pretty v0.1.0 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
15 changes: 10 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/btree v1.0.0 h1:0udJVsspx3VBr5FwtLhQQtuAsVc79tTq0ocGIPAU6qo=
github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/shamaton/msgpack/v2 v2.2.0 h1:IP1m01pHwCrMa6ZccP9B3bqxEMKMSmMVAVKk54g3L/Y=
github.com/shamaton/msgpack/v2 v2.2.0/go.mod h1:6khjYnkx73f7VQU7wjcFS9DFjs+59naVWJv1TB7qdOI=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
Expand Down
2 changes: 2 additions & 0 deletions internal/api/bindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,8 @@ struct AnalysisReport analyze_code(struct cache_t *cache,

struct Metrics get_metrics(struct cache_t *cache, struct UnmanagedVector *error_msg);

struct UnmanagedVector get_pinned_metrics(struct cache_t *cache, struct UnmanagedVector *error_msg);

/**
* frees a cache reference
*
Expand Down
15 changes: 15 additions & 0 deletions internal/api/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,21 @@ func GetMetrics(cache Cache) (*types.Metrics, error) {
}, nil
}

func GetPinnedMetrics(cache Cache) (*types.PinnedMetrics, error) {
errmsg := uninitializedUnmanagedVector()
metrics, err := C.get_pinned_metrics(cache.ptr, &errmsg)
if err != nil {
return nil, errorWithMessage(err, errmsg)
}

var pinnedMetrics types.PinnedMetrics
if err := pinnedMetrics.UnmarshalMessagePack(copyAndDestroyUnmanagedVector(metrics)); err != nil {
return nil, err
}

return &pinnedMetrics, nil
}

func Instantiate(
cache Cache,
checksum []byte,
Expand Down
80 changes: 80 additions & 0 deletions internal/api/lib_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package api

import (
"bytes"
"crypto/sha256"
"encoding/hex"
"encoding/json"
Expand Down Expand Up @@ -382,6 +383,85 @@ func TestGetMetrics(t *testing.T) {
require.InEpsilon(t, 3700000, metrics.SizeMemoryCache, 0.25)
}

func TestGetPinnedMetrics(t *testing.T) {
cache, cleanup := withCache(t)
defer cleanup()

// GetMetrics 1
metrics, err := GetPinnedMetrics(cache)
require.NoError(t, err)
assert.Equal(t, &types.PinnedMetrics{PerModule: make([]types.PerModuleEntry, 0)}, metrics)

// Store contract 1
wasm, err := os.ReadFile("../../testdata/hackatom.wasm")
require.NoError(t, err)
checksum, err := StoreCode(cache, wasm)
require.NoError(t, err)

err = Pin(cache, checksum)
require.NoError(t, err)

// Store contract 2
cyberpunkWasm, err := os.ReadFile("../../testdata/cyberpunk.wasm")
require.NoError(t, err)
cyberpunkChecksum, err := StoreCode(cache, cyberpunkWasm)
require.NoError(t, err)

err = Pin(cache, cyberpunkChecksum)
require.NoError(t, err)

findMetrics := func(list []types.PerModuleEntry, checksum types.Checksum) *types.PerModuleMetrics {
found := (*types.PerModuleMetrics)(nil)

for _, structure := range list {
if bytes.Equal(structure.Checksum, checksum) {
found = &structure.Metrics
break
}
}

return found
}

// GetMetrics 2
metrics, err = GetPinnedMetrics(cache)
require.NoError(t, err)
assert.Equal(t, 2, len(metrics.PerModule))

hackatomMetrics := findMetrics(metrics.PerModule, checksum)
cyberpunkMetrics := findMetrics(metrics.PerModule, cyberpunkChecksum)

assert.Equal(t, uint32(0), hackatomMetrics.Hits)
assert.NotEqual(t, uint32(0), hackatomMetrics.Size)
assert.Equal(t, uint32(0), cyberpunkMetrics.Hits)
assert.NotEqual(t, uint32(0), cyberpunkMetrics.Size)

// Instantiate 1
gasMeter := NewMockGasMeter(TESTING_GAS_LIMIT)
igasMeter := types.GasMeter(gasMeter)
store := NewLookup(gasMeter)
api := NewMockAPI()
querier := DefaultQuerier(MOCK_CONTRACT_ADDR, types.Array[types.Coin]{types.NewCoin(100, "ATOM")})
env := MockEnvBin(t)
info := MockInfoBin(t, "creator")
msg1 := []byte(`{"verifier": "fred", "beneficiary": "bob"}`)
_, _, err = Instantiate(cache, checksum, env, info, msg1, &igasMeter, store, api, &querier, TESTING_GAS_LIMIT, TESTING_PRINT_DEBUG)
require.NoError(t, err)

// GetMetrics 3
metrics, err = GetPinnedMetrics(cache)
require.NoError(t, err)
assert.Equal(t, 2, len(metrics.PerModule))

hackatomMetrics = findMetrics(metrics.PerModule, checksum)
cyberpunkMetrics = findMetrics(metrics.PerModule, cyberpunkChecksum)

assert.Equal(t, uint32(1), hackatomMetrics.Hits)
assert.NotEqual(t, uint32(0), hackatomMetrics.Size)
assert.Equal(t, uint32(0), cyberpunkMetrics.Hits)
assert.NotEqual(t, uint32(0), cyberpunkMetrics.Size)
}

func TestInstantiate(t *testing.T) {
cache, cleanup := withCache(t)
defer cleanup()
Expand Down
29 changes: 26 additions & 3 deletions libwasmvm/Cargo.lock

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

2 changes: 2 additions & 0 deletions libwasmvm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ backtraces = []
cosmwasm-std = { git = "https://github.com/CosmWasm/cosmwasm.git", rev = "v2.1.0-rc.1", features = ["staking", "stargate", "iterator"] }
cosmwasm-vm = { git = "https://github.com/CosmWasm/cosmwasm.git", rev = "v2.1.0-rc.1", features = ["staking", "stargate", "iterator"] }
errno = "0.3.8"
rmp-serde = "1.1.2"
serde = { version = "1.0.103", features = ["derive"] }
serde_json = "1.0.91"
thiserror = "1.0.38"
hex = "0.4.3"
Expand Down
2 changes: 2 additions & 0 deletions libwasmvm/bindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,8 @@ struct AnalysisReport analyze_code(struct cache_t *cache,

struct Metrics get_metrics(struct cache_t *cache, struct UnmanagedVector *error_msg);

struct UnmanagedVector get_pinned_metrics(struct cache_t *cache, struct UnmanagedVector *error_msg);

/**
* frees a cache reference
*
Expand Down
Loading
Loading