-
Notifications
You must be signed in to change notification settings - Fork 415
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
39 changed files
with
725 additions
and
330 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package keeper | ||
|
||
import ( | ||
"fmt" | ||
"github.com/CosmWasm/wasmd/x/wasm/types" | ||
wasmvmtypes "github.com/CosmWasm/wasmvm/types" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
) | ||
|
||
// newWasmModuleEvent creates with wasm module event for interacting with the given contract. Adds custom attributes | ||
// to this event. | ||
func newWasmModuleEvent(customAttributes []wasmvmtypes.EventAttribute, contractAddr sdk.AccAddress) sdk.Events { | ||
attrs := contractSDKEventAttributes(customAttributes, contractAddr) | ||
|
||
// each wasm invocation always returns one sdk.Event | ||
return sdk.Events{sdk.NewEvent(types.WasmModuleEventType, attrs...)} | ||
} | ||
|
||
// returns true when a wasm module event was emitted for this contract already | ||
func hasWasmModuleEvent(ctx sdk.Context, contractAddr sdk.AccAddress) bool { | ||
for _, e := range ctx.EventManager().Events() { | ||
if e.Type == types.WasmModuleEventType { | ||
for _, a := range e.Attributes { | ||
if string(a.Key) == types.AttributeKeyContractAddr && string(a.Value) == contractAddr.String() { | ||
return true | ||
} | ||
} | ||
} | ||
} | ||
return false | ||
} | ||
|
||
const eventTypeMinLength = 2 | ||
|
||
// newCustomEvents converts wasmvm events from a contract response to sdk type events | ||
func newCustomEvents(evts wasmvmtypes.Events, contractAddr sdk.AccAddress) sdk.Events { | ||
events := make(sdk.Events, 0, len(evts)) | ||
for _, e := range evts { | ||
if len(e.Type) <= eventTypeMinLength { | ||
continue | ||
} | ||
attributes := contractSDKEventAttributes(e.Attributes, contractAddr) | ||
events = append(events, sdk.NewEvent(fmt.Sprintf("%s%s", types.CustomContractEventPrefix, e.Type), attributes...)) | ||
} | ||
return events | ||
} | ||
|
||
// convert and add contract address issuing this event | ||
func contractSDKEventAttributes(customAttributes []wasmvmtypes.EventAttribute, contractAddr sdk.AccAddress) []sdk.Attribute { | ||
attrs := []sdk.Attribute{sdk.NewAttribute(types.AttributeKeyContractAddr, contractAddr.String())} | ||
// append attributes from wasm to the sdk.Event | ||
for _, l := range customAttributes { | ||
// and reserve the contract_address key for our use (not contract) | ||
if l.Key != types.AttributeKeyContractAddr { | ||
attrs = append(attrs, sdk.NewAttribute(l.Key, l.Value)) | ||
} | ||
} | ||
return attrs | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
package keeper | ||
|
||
import ( | ||
"context" | ||
"github.com/CosmWasm/wasmd/x/wasm/types" | ||
wasmvmtypes "github.com/CosmWasm/wasmvm/types" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestHasWasmModuleEvent(t *testing.T) { | ||
myContractAddr := RandomAccountAddress(t) | ||
specs := map[string]struct { | ||
srcEvents []sdk.Event | ||
exp bool | ||
}{ | ||
"event found": { | ||
srcEvents: []sdk.Event{ | ||
sdk.NewEvent(types.WasmModuleEventType, sdk.NewAttribute("contract_address", myContractAddr.String())), | ||
}, | ||
exp: true, | ||
}, | ||
"different event: not found": { | ||
srcEvents: []sdk.Event{ | ||
sdk.NewEvent(types.CustomContractEventPrefix, sdk.NewAttribute("contract_address", myContractAddr.String())), | ||
}, | ||
exp: false, | ||
}, | ||
"event with different address: not found": { | ||
srcEvents: []sdk.Event{ | ||
sdk.NewEvent(types.WasmModuleEventType, sdk.NewAttribute("contract_address", RandomBech32AccountAddress(t))), | ||
}, | ||
exp: false, | ||
}, | ||
"no event": { | ||
srcEvents: []sdk.Event{}, | ||
exp: false, | ||
}, | ||
} | ||
for name, spec := range specs { | ||
t.Run(name, func(t *testing.T) { | ||
em := sdk.NewEventManager() | ||
em.EmitEvents(spec.srcEvents) | ||
ctx := sdk.Context{}.WithContext(context.Background()).WithEventManager(em) | ||
|
||
got := hasWasmModuleEvent(ctx, myContractAddr) | ||
assert.Equal(t, spec.exp, got) | ||
}) | ||
} | ||
} | ||
|
||
func TestNewCustomEvents(t *testing.T) { | ||
myContract := RandomAccountAddress(t) | ||
specs := map[string]struct { | ||
src wasmvmtypes.Events | ||
exp sdk.Events | ||
}{ | ||
"all good": { | ||
src: wasmvmtypes.Events{{ | ||
Type: "foo", | ||
Attributes: []wasmvmtypes.EventAttribute{{Key: "myKey", Value: "myVal"}}, | ||
}}, | ||
exp: sdk.Events{sdk.NewEvent("wasm-foo", | ||
sdk.NewAttribute("contract_address", myContract.String()), | ||
sdk.NewAttribute("myKey", "myVal"))}, | ||
}, | ||
"multiple attributes": { | ||
src: wasmvmtypes.Events{{ | ||
Type: "foo", | ||
Attributes: []wasmvmtypes.EventAttribute{{Key: "myKey", Value: "myVal"}, | ||
{Key: "myOtherKey", Value: "myOtherVal"}}, | ||
}}, | ||
exp: sdk.Events{sdk.NewEvent("wasm-foo", | ||
sdk.NewAttribute("contract_address", myContract.String()), | ||
sdk.NewAttribute("myKey", "myVal"), | ||
sdk.NewAttribute("myOtherKey", "myOtherVal"))}, | ||
}, | ||
"multiple events": { | ||
src: wasmvmtypes.Events{{ | ||
Type: "foo", | ||
Attributes: []wasmvmtypes.EventAttribute{{Key: "myKey", Value: "myVal"}}, | ||
}, { | ||
Type: "bar", | ||
Attributes: []wasmvmtypes.EventAttribute{{Key: "otherKey", Value: "otherVal"}}, | ||
}}, | ||
exp: sdk.Events{sdk.NewEvent("wasm-foo", | ||
sdk.NewAttribute("contract_address", myContract.String()), | ||
sdk.NewAttribute("myKey", "myVal")), | ||
sdk.NewEvent("wasm-bar", | ||
sdk.NewAttribute("contract_address", myContract.String()), | ||
sdk.NewAttribute("otherKey", "otherVal"))}, | ||
}, | ||
"without attributes": { | ||
src: wasmvmtypes.Events{{ | ||
Type: "foo", | ||
}}, | ||
exp: sdk.Events{sdk.NewEvent("wasm-foo", | ||
sdk.NewAttribute("contract_address", myContract.String()))}, | ||
}, | ||
"min length not reached": { | ||
src: wasmvmtypes.Events{{ | ||
Type: "f", | ||
}}, | ||
exp: sdk.Events{}, | ||
}, | ||
"overwrite contract_address": { | ||
src: wasmvmtypes.Events{{ | ||
Type: "foo", | ||
Attributes: []wasmvmtypes.EventAttribute{{Key: "contract_address", Value: RandomBech32AccountAddress(t)}}, | ||
}}, | ||
exp: sdk.Events{sdk.NewEvent("wasm-foo", | ||
sdk.NewAttribute("contract_address", myContract.String()))}, | ||
}, | ||
} | ||
for name, spec := range specs { | ||
t.Run(name, func(t *testing.T) { | ||
gotEvent := newCustomEvents(spec.src, myContract) | ||
assert.Equal(t, spec.exp, gotEvent) | ||
}) | ||
} | ||
} | ||
|
||
func TestNewWasmModuleEvent(t *testing.T) { | ||
myContract := RandomAccountAddress(t) | ||
specs := map[string]struct { | ||
src []wasmvmtypes.EventAttribute | ||
exp sdk.Events | ||
}{ | ||
"all good": { | ||
src: []wasmvmtypes.EventAttribute{{Key: "myKey", Value: "myVal"}}, | ||
exp: sdk.Events{sdk.NewEvent("wasm", | ||
sdk.NewAttribute("contract_address", myContract.String()), | ||
sdk.NewAttribute("myKey", "myVal"))}, | ||
}, | ||
"multiple attributes": { | ||
src: []wasmvmtypes.EventAttribute{{Key: "myKey", Value: "myVal"}, | ||
{Key: "myOtherKey", Value: "myOtherVal"}}, | ||
exp: sdk.Events{sdk.NewEvent("wasm", | ||
sdk.NewAttribute("contract_address", myContract.String()), | ||
sdk.NewAttribute("myKey", "myVal"), | ||
sdk.NewAttribute("myOtherKey", "myOtherVal"))}, | ||
}, | ||
"without attributes": { | ||
exp: sdk.Events{sdk.NewEvent("wasm", | ||
sdk.NewAttribute("contract_address", myContract.String()))}, | ||
}, | ||
"overwrite contract_address": { | ||
src: []wasmvmtypes.EventAttribute{{Key: "contract_address", Value: RandomBech32AccountAddress(t)}}, | ||
exp: sdk.Events{sdk.NewEvent("wasm", | ||
sdk.NewAttribute("contract_address", myContract.String()))}, | ||
}, | ||
} | ||
for name, spec := range specs { | ||
t.Run(name, func(t *testing.T) { | ||
gotEvent := newWasmModuleEvent(spec.src, myContract) | ||
assert.Equal(t, spec.exp, gotEvent) | ||
}) | ||
} | ||
} |
Oops, something went wrong.