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 13 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
};
}
);
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ go 1.21
require (
github.com/google/btree v1.0.0
github.com/stretchr/testify v1.8.1
github.com/vmihailenco/msgpack/v5 v5.4.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/pmezard/go-difflib v1.0.0 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/vmihailenco/msgpack/v5 v5.4.1 h1:cQriyiUvjTwOHg8QZaPihLWeRAAVoCpE00IUPn0Bjt8=
github.com/vmihailenco/msgpack/v5 v5.4.1/go.mod h1:GaZTsDaehaPpQVyxrf5mtQlH+pc21PIudVV/E3rRQok=
github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g=
github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds=
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
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
82 changes: 82 additions & 0 deletions internal/api/lib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,88 @@ 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)

checksumStr := types.Checksum(checksum).String()
cyberpunkChecksumStr := types.Checksum(cyberpunkChecksum).String()

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

for _, structure := range list {
if structure.Name == 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, checksumStr)
cyberpunkMetrics := findMetrics(metrics.PerModule, cyberpunkChecksumStr)

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, checksumStr)
cyberpunkMetrics = findMetrics(metrics.PerModule, cyberpunkChecksumStr)

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