-
Notifications
You must be signed in to change notification settings - Fork 417
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Upgrade to WasmVM v0.15.1 #548
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't like silent failures/filters. I would prefer returning an error (even panic-ing in Go), Devs learn quick when things panic. They ignore errors for months if they are silently ignored |
||
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice to avoid duplicates with reply, etc