Skip to content

Commit

Permalink
Implement Stringer interface, skip HashMap on Rust side
Browse files Browse the repository at this point in the history
  • Loading branch information
aumetra committed Jun 26, 2024
1 parent 99cb47c commit f4d0a92
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 17 deletions.
43 changes: 32 additions & 11 deletions internal/api/lib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ func TestGetPinnedMetrics(t *testing.T) {
// GetMetrics 1
metrics, err := GetPinnedMetrics(cache)
require.NoError(t, err)
assert.Equal(t, &types.PinnedMetrics{PerModule: make(map[string]types.PerModuleMetrics, 0)}, metrics)
assert.Equal(t, &types.PinnedMetrics{PerModule: make([]types.PerModuleEntry, 0)}, metrics)

// Store contract 1
wasm, err := os.ReadFile("../../testdata/hackatom.wasm")
Expand All @@ -409,17 +409,34 @@ func TestGetPinnedMetrics(t *testing.T) {
err = Pin(cache, cyberpunkChecksum)
require.NoError(t, err)

checksumStr := types.Checksum(checksum).EncodeHex()
cyberpunkChecksumStr := types.Checksum(cyberpunkChecksum).EncodeHex()
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))
assert.Equal(t, uint32(0), metrics.PerModule[checksumStr].Hits)
assert.NotEqual(t, uint32(0), metrics.PerModule[checksumStr].Size)
assert.Equal(t, uint32(0), metrics.PerModule[cyberpunkChecksumStr].Hits)
assert.NotEqual(t, uint32(0), metrics.PerModule[cyberpunkChecksumStr].Size)

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)
Expand All @@ -437,10 +454,14 @@ func TestGetPinnedMetrics(t *testing.T) {
metrics, err = GetPinnedMetrics(cache)
require.NoError(t, err)
assert.Equal(t, 2, len(metrics.PerModule))
assert.Equal(t, uint32(1), metrics.PerModule[checksumStr].Hits)
assert.NotEqual(t, uint32(0), metrics.PerModule[checksumStr].Size)
assert.Equal(t, uint32(0), metrics.PerModule[cyberpunkChecksumStr].Hits)
assert.NotEqual(t, uint32(0), metrics.PerModule[cyberpunkChecksumStr].Size)

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) {
Expand Down
7 changes: 3 additions & 4 deletions libwasmvm/src/cache.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::{BTreeSet, HashMap};
use std::collections::BTreeSet;
use std::convert::TryInto;
use std::panic::{catch_unwind, AssertUnwindSafe};
use std::str::from_utf8;
Expand Down Expand Up @@ -433,16 +433,15 @@ impl From<cosmwasm_vm::PerModuleMetrics> for PerModuleMetrics {

#[derive(Serialize)]
struct PinnedMetrics {
per_module: HashMap<Checksum, PerModuleMetrics>,
per_module: Vec<(Checksum, PerModuleMetrics)>,
}

impl From<cosmwasm_vm::PinnedMetrics> for PinnedMetrics {
fn from(value: cosmwasm_vm::PinnedMetrics) -> Self {
Self {
per_module: value
.per_module
.iter()
.cloned()
.into_iter()
.map(|(checksum, metrics)| (checksum, metrics.into()))
.collect(),
}
Expand Down
2 changes: 1 addition & 1 deletion types/checksum.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
// The length of a checksum must always be ChecksumLen.
type Checksum []byte

func (cs Checksum) EncodeHex() string {
func (cs Checksum) String() string {
return hex.EncodeToString(cs)
}

Expand Down
7 changes: 6 additions & 1 deletion types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,13 @@ type PerModuleMetrics struct {
Size uint64 `msgpack:"size"`
}

type PerModuleEntry struct {
Name string
Metrics PerModuleMetrics
}

type PinnedMetrics struct {
PerModule map[string]PerModuleMetrics `msgpack:"per_module"`
PerModule []PerModuleEntry `msgpack:"per_module,as_array"`
}

func (pm *PinnedMetrics) UnmarshalMessagePack(data []byte) error {
Expand Down

0 comments on commit f4d0a92

Please sign in to comment.