diff --git a/internal/api/lib_test.go b/internal/api/lib_test.go index 27a7406e4..d039f60f8 100644 --- a/internal/api/lib_test.go +++ b/internal/api/lib_test.go @@ -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") @@ -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) @@ -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) { diff --git a/libwasmvm/src/cache.rs b/libwasmvm/src/cache.rs index 7f84662b3..5f38affd2 100644 --- a/libwasmvm/src/cache.rs +++ b/libwasmvm/src/cache.rs @@ -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; @@ -433,7 +433,7 @@ impl From for PerModuleMetrics { #[derive(Serialize)] struct PinnedMetrics { - per_module: HashMap, + per_module: Vec<(Checksum, PerModuleMetrics)>, } impl From for PinnedMetrics { @@ -441,8 +441,7 @@ impl From for PinnedMetrics { Self { per_module: value .per_module - .iter() - .cloned() + .into_iter() .map(|(checksum, metrics)| (checksum, metrics.into())) .collect(), } diff --git a/types/checksum.go b/types/checksum.go index bb472e317..2f74224d9 100644 --- a/types/checksum.go +++ b/types/checksum.go @@ -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) } diff --git a/types/types.go b/types/types.go index 4113f2313..904e67b3e 100644 --- a/types/types.go +++ b/types/types.go @@ -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 {