From 1dd874afdcb32da535ade6498b531466efd4916f Mon Sep 17 00:00:00 2001 From: Alex Peters Date: Wed, 6 Apr 2022 11:19:44 +0200 Subject: [PATCH] Add more tests for event edge cases --- x/wasm/keeper/events_test.go | 16 ++++++++++ x/wasm/keeper/gas_register_test.go | 47 ++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/x/wasm/keeper/events_test.go b/x/wasm/keeper/events_test.go index 8c59516343..e10074a0c2 100644 --- a/x/wasm/keeper/events_test.go +++ b/x/wasm/keeper/events_test.go @@ -183,6 +183,13 @@ func TestNewCustomEvents(t *testing.T) { sdk.NewAttribute("_contract_address", myContract.String()), sdk.NewAttribute("my Key", "myVal"))}, }, + "empty event elements": { + src: make(wasmvmtypes.Events, 10), + isError: true, + }, + "nil": { + exp: sdk.Events{}, + }, } for name, spec := range specs { t.Run(name, func(t *testing.T) { @@ -240,6 +247,15 @@ func TestNewWasmModuleEvent(t *testing.T) { sdk.NewAttribute("_contract_address", myContract.String()), sdk.NewAttribute("my-real-key", "some-val"))}, }, + "empty elements": { + src: make([]wasmvmtypes.EventAttribute, 10), + isError: true, + }, + "nil": { + exp: sdk.Events{sdk.NewEvent("wasm", + sdk.NewAttribute("_contract_address", myContract.String()), + )}, + }, } for name, spec := range specs { t.Run(name, func(t *testing.T) { diff --git a/x/wasm/keeper/gas_register_test.go b/x/wasm/keeper/gas_register_test.go index 1ecabc5fba..b3ca5b9c86 100644 --- a/x/wasm/keeper/gas_register_test.go +++ b/x/wasm/keeper/gas_register_test.go @@ -283,6 +283,26 @@ func TestReplyCost(t *testing.T) { srcConfig: DefaultGasRegisterConfig(), exp: sdk.Gas(DefaultInstanceCost + 3*DefaultContractMessageDataCost), }, + "subcall response with empty events": { + src: wasmvmtypes.Reply{ + Result: wasmvmtypes.SubcallResult{ + Ok: &wasmvmtypes.SubcallResponse{ + Events: make([]wasmvmtypes.Event, 10), + }, + }, + }, + srcConfig: DefaultGasRegisterConfig(), + exp: DefaultInstanceCost, + }, + "subcall response with events unset": { + src: wasmvmtypes.Reply{ + Result: wasmvmtypes.SubcallResult{ + Ok: &wasmvmtypes.SubcallResponse{}, + }, + }, + srcConfig: DefaultGasRegisterConfig(), + exp: DefaultInstanceCost, + }, } for name, spec := range specs { t.Run(name, func(t *testing.T) { @@ -298,6 +318,33 @@ func TestReplyCost(t *testing.T) { } } +func TestEventCosts(t *testing.T) { + // most cases are covered in TestReplyCost already. This ensures some edge cases + specs := map[string]struct { + srcAttrs []wasmvmtypes.EventAttribute + srcEvents wasmvmtypes.Events + expGas sdk.Gas + }{ + "empty events": { + srcEvents: make([]wasmvmtypes.Event, 1), + expGas: DefaultPerCustomEventCost, + }, + "empty attributes": { + srcAttrs: make([]wasmvmtypes.EventAttribute, 1), + expGas: DefaultPerAttributeCost, + }, + "both nil": { + expGas: 0, + }, + } + for name, spec := range specs { + t.Run(name, func(t *testing.T) { + gotGas := NewDefaultWasmGasRegister().EventCosts(spec.srcAttrs, spec.srcEvents) + assert.Equal(t, spec.expGas, gotGas) + }) + } +} + func TestToWasmVMGasConversion(t *testing.T) { specs := map[string]struct { src storetypes.Gas