Skip to content

Commit 260e8e1

Browse files
committed
Add Go test for pinned metrics
1 parent 94cb09d commit 260e8e1

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

internal/api/lib_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,63 @@ func TestGetMetrics(t *testing.T) {
382382
require.InEpsilon(t, 3700000, metrics.SizeMemoryCache, 0.25)
383383
}
384384

385+
func TestGetPinnedMetrics(t *testing.T) {
386+
cache, cleanup := withCache(t)
387+
defer cleanup()
388+
389+
// GetMetrics 1
390+
metrics, err := GetPinnedMetrics(cache)
391+
require.NoError(t, err)
392+
assert.Equal(t, &types.PinnedMetrics{PerModule: make(map[string]types.PerModuleMetrics, 0)}, metrics)
393+
394+
// Store contract 1
395+
wasm, err := os.ReadFile("../../testdata/hackatom.wasm")
396+
require.NoError(t, err)
397+
checksum, err := StoreCode(cache, wasm)
398+
require.NoError(t, err)
399+
400+
err = Pin(cache, checksum)
401+
require.NoError(t, err)
402+
403+
// Store contract 2
404+
cyberpunkWasm, err := os.ReadFile("../../testdata/cyberpunk.wasm")
405+
require.NoError(t, err)
406+
cyberpunkChecksum, err := StoreCode(cache, cyberpunkWasm)
407+
require.NoError(t, err)
408+
409+
err = Pin(cache, cyberpunkChecksum)
410+
require.NoError(t, err)
411+
412+
checksumStr := types.Checksum(checksum).EncodeHex()
413+
cyberpunkChecksumStr := types.Checksum(cyberpunkChecksum).EncodeHex()
414+
415+
// GetMetrics 2
416+
metrics, err = GetPinnedMetrics(cache)
417+
require.NoError(t, err)
418+
assert.Equal(t, len(metrics.PerModule), 2)
419+
assert.Equal(t, metrics.PerModule[checksumStr].Hits, 0)
420+
assert.Equal(t, metrics.PerModule[cyberpunkChecksumStr].Hits, 0)
421+
422+
// Instantiate 1
423+
gasMeter := NewMockGasMeter(TESTING_GAS_LIMIT)
424+
igasMeter := types.GasMeter(gasMeter)
425+
store := NewLookup(gasMeter)
426+
api := NewMockAPI()
427+
querier := DefaultQuerier(MOCK_CONTRACT_ADDR, types.Array[types.Coin]{types.NewCoin(100, "ATOM")})
428+
env := MockEnvBin(t)
429+
info := MockInfoBin(t, "creator")
430+
msg1 := []byte(`{"verifier": "fred", "beneficiary": "bob"}`)
431+
_, _, err = Instantiate(cache, checksum, env, info, msg1, &igasMeter, store, api, &querier, TESTING_GAS_LIMIT, TESTING_PRINT_DEBUG)
432+
require.NoError(t, err)
433+
434+
// GetMetrics 3
435+
metrics, err = GetPinnedMetrics(cache)
436+
require.NoError(t, err)
437+
assert.Equal(t, len(metrics.PerModule), 1)
438+
assert.Equal(t, metrics.PerModule[checksumStr].Hits, 1)
439+
assert.Equal(t, metrics.PerModule[cyberpunkChecksumStr].Hits, 0)
440+
}
441+
385442
func TestInstantiate(t *testing.T) {
386443
cache, cleanup := withCache(t)
387444
defer cleanup()

types/checksum.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ import (
1010
// The length of a checksum must always be ChecksumLen.
1111
type Checksum []byte
1212

13+
func (cs Checksum) EncodeHex() string {
14+
return hex.EncodeToString(cs)
15+
}
16+
1317
func (cs Checksum) MarshalJSON() ([]byte, error) {
1418
return json.Marshal(hex.EncodeToString(cs))
1519
}

types/types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ type PerModuleMetrics struct {
197197
}
198198

199199
type PinnedMetrics struct {
200-
PerModule PerModuleMetrics `json:"per_module"`
200+
PerModule map[string]PerModuleMetrics `json:"per_module"`
201201
}
202202

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

0 commit comments

Comments
 (0)