From 1ada14ed52eebaab501a6793f4166a3692c8825d Mon Sep 17 00:00:00 2001 From: Alexander Peters Date: Mon, 14 Jun 2021 08:57:04 +0200 Subject: [PATCH] Store contract response in event (#535) * Store contract response in event * Review feedback --- x/wasm/keeper/msg_server.go | 5 +++++ x/wasm/keeper/proposal_handler.go | 8 ++++++-- x/wasm/keeper/proposal_integration_test.go | 14 +++++++++++--- x/wasm/types/events.go | 7 ++++--- 4 files changed, 26 insertions(+), 8 deletions(-) diff --git a/x/wasm/keeper/msg_server.go b/x/wasm/keeper/msg_server.go index 93e80043f2..b6f2109f12 100644 --- a/x/wasm/keeper/msg_server.go +++ b/x/wasm/keeper/msg_server.go @@ -2,6 +2,7 @@ package keeper import ( "context" + "encoding/hex" "fmt" "github.com/CosmWasm/wasmd/x/wasm/types" sdk "github.com/cosmos/cosmos-sdk/types" @@ -66,6 +67,7 @@ func (m msgServer) InstantiateContract(goCtx context.Context, msg *types.MsgInst sdk.NewAttribute(types.AttributeKeySigner, msg.Sender), sdk.NewAttribute(types.AttributeKeyCodeID, fmt.Sprintf("%d", msg.CodeID)), sdk.NewAttribute(types.AttributeKeyContract, contractAddr.String()), + sdk.NewAttribute(types.AttributeResultDataHex, hex.EncodeToString(data)), )) return &types.MsgInstantiateContractResponse{ @@ -95,6 +97,7 @@ func (m msgServer) ExecuteContract(goCtx context.Context, msg *types.MsgExecuteC sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName), sdk.NewAttribute(types.AttributeKeySigner, msg.Sender), sdk.NewAttribute(types.AttributeKeyContract, msg.Contract), + sdk.NewAttribute(types.AttributeResultDataHex, hex.EncodeToString(data)), )) return &types.MsgExecuteContractResponse{ @@ -122,7 +125,9 @@ func (m msgServer) MigrateContract(goCtx context.Context, msg *types.MsgMigrateC sdk.EventTypeMessage, sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName), sdk.NewAttribute(types.AttributeKeySigner, msg.Sender), + sdk.NewAttribute(types.AttributeKeyCodeID, fmt.Sprintf("%d", msg.CodeID)), sdk.NewAttribute(types.AttributeKeyContract, msg.Contract), + sdk.NewAttribute(types.AttributeResultDataHex, hex.EncodeToString(data)), )) return &types.MsgMigrateContractResponse{ diff --git a/x/wasm/keeper/proposal_handler.go b/x/wasm/keeper/proposal_handler.go index 77c29e9ba7..a2a224c265 100644 --- a/x/wasm/keeper/proposal_handler.go +++ b/x/wasm/keeper/proposal_handler.go @@ -1,6 +1,7 @@ package keeper import ( + "encoding/hex" "fmt" "github.com/CosmWasm/wasmd/x/wasm/types" sdk "github.com/cosmos/cosmos-sdk/types" @@ -84,7 +85,7 @@ func handleInstantiateProposal(ctx sdk.Context, k types.ContractOpsKeeper, p typ return sdkerrors.Wrap(err, "admin") } - contractAddr, _, err := k.Instantiate(ctx, p.CodeID, runAsAddr, adminAddr, p.InitMsg, p.Label, p.Funds) + contractAddr, data, err := k.Instantiate(ctx, p.CodeID, runAsAddr, adminAddr, p.InitMsg, p.Label, p.Funds) if err != nil { return err } @@ -94,6 +95,7 @@ func handleInstantiateProposal(ctx sdk.Context, k types.ContractOpsKeeper, p typ sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName), sdk.NewAttribute(types.AttributeKeyCodeID, fmt.Sprintf("%d", p.CodeID)), sdk.NewAttribute(types.AttributeKeyContract, contractAddr.String()), + sdk.NewAttribute(types.AttributeResultDataHex, hex.EncodeToString(data)), ) ctx.EventManager().EmitEvent(ourEvent) return nil @@ -112,7 +114,7 @@ func handleMigrateProposal(ctx sdk.Context, k types.ContractOpsKeeper, p types.M if err != nil { return sdkerrors.Wrap(err, "run as address") } - _, err = k.Migrate(ctx, contractAddr, runAsAddr, p.CodeID, p.MigrateMsg) + data, err := k.Migrate(ctx, contractAddr, runAsAddr, p.CodeID, p.MigrateMsg) if err != nil { return err } @@ -120,7 +122,9 @@ func handleMigrateProposal(ctx sdk.Context, k types.ContractOpsKeeper, p types.M ourEvent := sdk.NewEvent( sdk.EventTypeMessage, sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName), + sdk.NewAttribute(types.AttributeKeyCodeID, fmt.Sprintf("%d", p.CodeID)), sdk.NewAttribute(types.AttributeKeyContract, p.Contract), + sdk.NewAttribute(types.AttributeResultDataHex, hex.EncodeToString(data)), ) ctx.EventManager().EmitEvent(ourEvent) return nil diff --git a/x/wasm/keeper/proposal_integration_test.go b/x/wasm/keeper/proposal_integration_test.go index 376c5476a8..6c02b57b1b 100644 --- a/x/wasm/keeper/proposal_integration_test.go +++ b/x/wasm/keeper/proposal_integration_test.go @@ -86,6 +86,7 @@ func TestInstantiateProposal(t *testing.T) { p.Admin = otherAddress.String() p.Label = "testing" }) + em := sdk.NewEventManager() // when stored storedProposal, err := govKeeper.SubmitProposal(ctx, src) @@ -93,7 +94,7 @@ func TestInstantiateProposal(t *testing.T) { // and proposal execute handler := govKeeper.Router().GetRoute(storedProposal.ProposalRoute()) - err = handler(ctx, storedProposal.GetContent()) + err = handler(ctx.WithEventManager(em), storedProposal.GetContent()) require.NoError(t, err) // then @@ -113,6 +114,9 @@ func TestInstantiateProposal(t *testing.T) { Msg: src.InitMsg, }} assert.Equal(t, expHistory, wasmKeeper.GetContractHistory(ctx, contractAddr)) + // and event + require.Len(t, em.Events(), 2, "%#v", em.Events()) + require.Len(t, em.Events()[1].Attributes, 4) } func TestMigrateProposal(t *testing.T) { @@ -161,13 +165,15 @@ func TestMigrateProposal(t *testing.T) { RunAs: otherAddress.String(), } + em := sdk.NewEventManager() + // when stored storedProposal, err := govKeeper.SubmitProposal(ctx, &src) require.NoError(t, err) // and proposal execute handler := govKeeper.Router().GetRoute(storedProposal.ProposalRoute()) - err = handler(ctx, storedProposal.GetContent()) + err = handler(ctx.WithEventManager(em), storedProposal.GetContent()) require.NoError(t, err) // then @@ -188,7 +194,9 @@ func TestMigrateProposal(t *testing.T) { Msg: src.MigrateMsg, }} assert.Equal(t, expHistory, wasmKeeper.GetContractHistory(ctx, contractAddr)) - + // and events emitted + require.Len(t, em.Events(), 2) + require.Len(t, em.Events()[1].Attributes, 4) } func TestAdminProposals(t *testing.T) { diff --git a/x/wasm/types/events.go b/x/wasm/types/events.go index 2ca36b9a72..f7e051f498 100644 --- a/x/wasm/types/events.go +++ b/x/wasm/types/events.go @@ -5,7 +5,8 @@ const ( EventTypeUnpinCode = "unpin_code" ) const ( // event attributes - AttributeKeyContract = "contract_address" - AttributeKeyCodeID = "code_id" - AttributeKeySigner = "signer" + AttributeKeyContract = "contract_address" + AttributeKeyCodeID = "code_id" + AttributeKeySigner = "signer" + AttributeResultDataHex = "result" )