Skip to content

Commit

Permalink
Merge pull request #799 from CosmWasm/empty_events
Browse files Browse the repository at this point in the history
Tests some event edge cases
  • Loading branch information
alpe authored Apr 6, 2022
2 parents 574e30a + 1dd874a commit 64d39c3
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
16 changes: 16 additions & 0 deletions x/wasm/keeper/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
47 changes: 47 additions & 0 deletions x/wasm/keeper/gas_register_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand Down

0 comments on commit 64d39c3

Please sign in to comment.