Skip to content

Commit

Permalink
check attributes length before doing attribute parse (#531)
Browse files Browse the repository at this point in the history
  • Loading branch information
yys committed Aug 11, 2021
1 parent 4a29019 commit 837c6bd
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 8 deletions.
18 changes: 10 additions & 8 deletions x/wasm/types/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
41 changes: 41 additions & 0 deletions x/wasm/types/connector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down

0 comments on commit 837c6bd

Please sign in to comment.