From ddb350aaac175fd6287b0f8cf9214f289d111dd7 Mon Sep 17 00:00:00 2001 From: Yun Yeo Date: Wed, 11 Aug 2021 10:59:30 +0900 Subject: [PATCH] check attributes length before doing attribute parse --- x/wasm/types/connector.go | 18 ++++++++------- x/wasm/types/connector_test.go | 41 ++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 8 deletions(-) diff --git a/x/wasm/types/connector.go b/x/wasm/types/connector.go index 854c16e3d..4af1e5067 100644 --- a/x/wasm/types/connector.go +++ b/x/wasm/types/connector.go @@ -26,16 +26,18 @@ func ParseEvents( var sdkEvents sdk.Events - sdkEvent, err := buildEvent(EventTypeWasmPrefix, contractAddr, attributes) - if err != nil { - return nil, err - } + if len(attributes) != 0 { + sdkEvent, err := buildEvent(EventTypeWasmPrefix, contractAddr, attributes) + if err != nil { + return nil, err + } - sdkEvents = sdkEvents.AppendEvent(*sdkEvent) + sdkEvents = sdkEvents.AppendEvent(*sdkEvent) - // Deprecated: from_contract - sdkEvent.Type = EventTypeFromContract - sdkEvents = sdkEvents.AppendEvent(*sdkEvent) + // Deprecated: from_contract + sdkEvent.Type = EventTypeFromContract + sdkEvents = sdkEvents.AppendEvent(*sdkEvent) + } // append wasm prefix for the events for _, event := range events { diff --git a/x/wasm/types/connector_test.go b/x/wasm/types/connector_test.go index 28865437b..0ba75b9a3 100644 --- a/x/wasm/types/connector_test.go +++ b/x/wasm/types/connector_test.go @@ -73,6 +73,47 @@ func TestParseEvents(t *testing.T) { )}, events) } +func TestParseEventsWithoutAttributes(t *testing.T) { + _, _, addr := keyPubAddr() + + events, err := ParseEvents(addr, wasmvmtypes.EventAttributes{}, wasmvmtypes.Events{ + { + Type: "type1", + Attributes: wasmvmtypes.EventAttributes{ + {Key: "key1", Value: "value1"}, + {Key: "key2", Value: "value2"}, + {Key: "key3", Value: "value3"}, + }, + }, + { + Type: "type2", + Attributes: wasmvmtypes.EventAttributes{ + {Key: "key1", Value: "value1"}, + {Key: "key2", Value: "value2"}, + {Key: "key3", Value: "value3"}, + }, + }, + }) + require.NoError(t, err) + require.Equal(t, sdk.Events{sdk.NewEvent( + fmt.Sprintf("%s-type1", EventTypeWasmPrefix), + []sdk.Attribute{ + {Key: AttributeKeyContractAddress, Value: addr.String()}, + {Key: "key1", Value: "value1"}, + {Key: "key2", Value: "value2"}, + {Key: "key3", Value: "value3"}, + }..., + ), sdk.NewEvent( + fmt.Sprintf("%s-type2", EventTypeWasmPrefix), + []sdk.Attribute{ + {Key: AttributeKeyContractAddress, Value: addr.String()}, + {Key: "key1", Value: "value1"}, + {Key: "key2", Value: "value2"}, + {Key: "key3", Value: "value3"}, + }..., + )}, events) +} + func TestBuildEvent(t *testing.T) { _, _, addr := keyPubAddr()