Skip to content

Commit

Permalink
feat: add admin-related events (CosmWasm#46)
Browse files Browse the repository at this point in the history
* add admin-related events in docs
* fix ClearAdmin event
* cherry-pick upstream admin-related events update
  • Loading branch information
tkxkd0159 authored May 16, 2023
1 parent 8518b2f commit bffff77
Show file tree
Hide file tree
Showing 7 changed files with 196 additions and 26 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## [Unreleased](https://github.com/Finschia/wasmd/compare/v0.1.3...HEAD)

### Features
* [\#46](https://github.com/Finschia/wasmd/pull/46) add admin-related events

### Improvements
* [\#43](https://github.com/Finschia/wasmd/pull/43) delete unnecessary test
Expand Down
35 changes: 26 additions & 9 deletions x/wasm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -293,16 +293,20 @@ find out the entire path for the events.
| wasm-{customEventType} | {customContractAttributeKey} | {customContractAttributeKey} | (optional) Defined by wasm contract developer |

#### MsgUpdateAdmin
| Type | Attribute Key | Attribute Value | Note |
|---------|-------------------|-------------------|---------------------------|
| message | module | wasm | |
| message | sender | {senderAddress} | |
| Type | Attribute Key | Attribute Value | Note |
|-----------------------|-------------------|--------------------|------|
| message | module | wasm | |
| message | sender | {senderAddress} | |
| update_contract_admin | _contract_address | {contract_address} | |
| update_contract_admin | new_admin_address | {adminAddress} | |

#### MsgClearAdmin
| Type | Attribute Key | Attribute Value | Note |
|---------|-------------------|-------------------|---------------------------|
| message | module | wasm | |
| message | sender | {senderAddress} | |
| Type | Attribute Key | Attribute Value | Note |
|-----------------------|-------------------|--------------------|--------------|
| message | module | wasm | |
| message | sender | {senderAddress} | |
| update_contract_admin | _contract_address | {contract_address} | |
| update_contract_admin | new_admin_address | "" | empty string |

### Keeper Events
In addition to message events, the wasm keeper will produce events when the following methods are called (or any method which ends up calling them)
Expand Down Expand Up @@ -334,14 +338,27 @@ In addition to message events, the wasm keeper will produce events when the foll
|------------|---------------|-----------------|------|
| unpin_code | code_id | {codeID} | |

#### SetContractAdmin
| Type | Attribute Key | Attribute Value | Note |
|-----------------------|-------------------|--------------------|------|
| update_contract_admin | _contract_address | {contract_address} | |
| update_contract_admin | new_admin_address | {adminAddress} | |

#### SetAccessConfig
By governance

| Type | Attribute Key | Attribute Value | Note |
|---------------------------|-----------------|-----------------|------|
| update_code_access_config | code_permission | {String} | |
| update_code_access_config | code_id | {String} | |

### Proposal Events
If you use wasm proposal, it makes common event like below.

| Type | Attribute Key | Attribute Value | Note |
|---------------------|---------------|--------------------|------|
| gov_contract_result | result | {resultOfProposal} | |


## Messages

TODO
Expand Down
19 changes: 18 additions & 1 deletion x/wasm/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -619,8 +619,15 @@ func (k Keeper) setContractAdmin(ctx sdk.Context, contractAddress, caller, newAd
if !authZ.CanModifyContract(contractInfo.AdminAddr(), caller) {
return sdkerrors.Wrap(sdkerrors.ErrUnauthorized, "can not modify contract")
}
contractInfo.Admin = newAdmin.String()
newAdminStr := newAdmin.String()
contractInfo.Admin = newAdminStr
k.storeContractInfo(ctx, contractAddress, contractInfo)
ctx.EventManager().EmitEvent(sdk.NewEvent(
types.EventTypeUpdateContractAdmin,
sdk.NewAttribute(types.AttributeKeyContractAddr, contractAddress.String()),
sdk.NewAttribute(types.AttributeKeyNewAdmin, newAdminStr),
))

return nil
}

Expand Down Expand Up @@ -958,6 +965,16 @@ func (k Keeper) setAccessConfig(ctx sdk.Context, codeID uint64, caller sdk.AccAd

info.InstantiateConfig = newConfig
k.storeCodeInfo(ctx, codeID, *info)
evt := sdk.NewEvent(
types.EventTypeUpdateCodeAccessConfig,
sdk.NewAttribute(types.AttributeKeyCodePermission, newConfig.Permission.String()),
sdk.NewAttribute(types.AttributeKeyCodeID, strconv.FormatUint(codeID, 10)),
)
if addrs := newConfig.AllAuthorizedAddresses(); len(addrs) != 0 {
attr := sdk.NewAttribute(types.AttributeKeyAuthorizedAddresses, strings.Join(addrs, ","))
evt.Attributes = append(evt.Attributes, attr.ToKVPair())
}
ctx.EventManager().EmitEvent(evt)
return nil
}

Expand Down
121 changes: 120 additions & 1 deletion x/wasm/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
fuzz "github.com/google/gofuzz"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
abci "github.com/tendermint/tendermint/abci/types"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"

"github.com/Finschia/finschia-sdk/baseapp"
Expand Down Expand Up @@ -2041,25 +2042,35 @@ func TestSetAccessConfig(t *testing.T) {
k := keepers.WasmKeeper
creatorAddr := RandomAccountAddress(t)
nonCreatorAddr := RandomAccountAddress(t)
const codeID = 1

specs := map[string]struct {
authz AuthorizationPolicy
chainPermission types.AccessType
newConfig types.AccessConfig
caller sdk.AccAddress
expErr bool
expEvts map[string]string
}{
"user with new permissions == chain permissions": {
authz: DefaultAuthorizationPolicy{},
chainPermission: types.AccessTypeEverybody,
newConfig: types.AllowEverybody,
caller: creatorAddr,
expEvts: map[string]string{
"code_id": "1",
"code_permission": "Everybody",
},
},
"user with new permissions < chain permissions": {
authz: DefaultAuthorizationPolicy{},
chainPermission: types.AccessTypeEverybody,
newConfig: types.AllowNobody,
caller: creatorAddr,
expEvts: map[string]string{
"code_id": "1",
"code_permission": "Nobody",
},
},
"user with new permissions > chain permissions": {
authz: DefaultAuthorizationPolicy{},
Expand All @@ -2080,29 +2091,59 @@ func TestSetAccessConfig(t *testing.T) {
chainPermission: types.AccessTypeEverybody,
newConfig: types.AllowEverybody,
caller: creatorAddr,
expEvts: map[string]string{
"code_id": "1",
"code_permission": "Everybody",
},
},
"gov with new permissions < chain permissions": {
authz: GovAuthorizationPolicy{},
chainPermission: types.AccessTypeEverybody,
newConfig: types.AllowNobody,
caller: creatorAddr,
expEvts: map[string]string{
"code_id": "1",
"code_permission": "Nobody",
},
},
"gov with new permissions > chain permissions": {
authz: GovAuthorizationPolicy{},
chainPermission: types.AccessTypeNobody,
newConfig: types.AccessTypeOnlyAddress.With(creatorAddr),
caller: creatorAddr,
expEvts: map[string]string{
"code_id": "1",
"code_permission": "OnlyAddress",
"authorized_addresses": creatorAddr.String(),
},
},
"gov with new permissions > chain permissions - multiple addresses": {
authz: GovAuthorizationPolicy{},
chainPermission: types.AccessTypeNobody,
newConfig: types.AccessTypeAnyOfAddresses.With(creatorAddr, nonCreatorAddr),
caller: creatorAddr,
expEvts: map[string]string{
"code_id": "1",
"code_permission": "AnyOfAddresses",
"authorized_addresses": creatorAddr.String() + "," + nonCreatorAddr.String(),
},
},
"gov without actor": {
authz: GovAuthorizationPolicy{},
chainPermission: types.AccessTypeEverybody,
newConfig: types.AllowEverybody,
expEvts: map[string]string{
"code_id": "1",
"code_permission": "Everybody",
},
},
}
const codeID = 1
for name, spec := range specs {
t.Run(name, func(t *testing.T) {
ctx, _ := parentCtx.CacheContext()
em := sdk.NewEventManager()
ctx = ctx.WithEventManager(em)

newParams := types.DefaultParams()
newParams.InstantiateDefaultPermission = spec.chainPermission
k.SetParams(ctx, newParams)
Expand All @@ -2115,6 +2156,10 @@ func TestSetAccessConfig(t *testing.T) {
return
}
require.NoError(t, gotErr)
// and event emitted
require.Len(t, em.Events(), 1)
assert.Equal(t, "update_code_access_config", em.Events()[0].Type)
assert.Equal(t, spec.expEvts, attrsToStringMap(em.Events()[0].Attributes))
})
}
}
Expand Down Expand Up @@ -2233,3 +2278,77 @@ func TestKeeper_GetByteCode(t *testing.T) {
})
}
}

func TestSetContractAdmin(t *testing.T) {
parentCtx, keepers := CreateTestInput(t, false, AvailableCapabilities)
k := keepers.WasmKeeper
myAddr := RandomAccountAddress(t)
example := InstantiateReflectExampleContract(t, parentCtx, keepers)
specs := map[string]struct {
newAdmin sdk.AccAddress
caller sdk.AccAddress
policy AuthorizationPolicy
expAdmin string
expErr bool
}{
"update admin": {
newAdmin: myAddr,
caller: example.CreatorAddr,
policy: DefaultAuthorizationPolicy{},
expAdmin: myAddr.String(),
},
"update admin - unauthorized": {
newAdmin: myAddr,
caller: RandomAccountAddress(t),
policy: DefaultAuthorizationPolicy{},
expErr: true,
},
"clear admin - default policy": {
caller: example.CreatorAddr,
policy: DefaultAuthorizationPolicy{},
expAdmin: "",
},
"clear admin - unauthorized": {
expAdmin: "",
policy: DefaultAuthorizationPolicy{},
caller: RandomAccountAddress(t),
expErr: true,
},
"clear admin - gov policy": {
newAdmin: nil,
policy: GovAuthorizationPolicy{},
caller: example.CreatorAddr,
expAdmin: "",
},
}
for name, spec := range specs {
t.Run(name, func(t *testing.T) {
ctx, _ := parentCtx.CacheContext()
em := sdk.NewEventManager()
ctx = ctx.WithEventManager(em)
gotErr := k.setContractAdmin(ctx, example.Contract, spec.caller, spec.newAdmin, spec.policy)
if spec.expErr {
require.Error(t, gotErr)
return
}
require.NoError(t, gotErr)
assert.Equal(t, spec.expAdmin, k.GetContractInfo(ctx, example.Contract).Admin)
// and event emitted
require.Len(t, em.Events(), 1)
assert.Equal(t, "update_contract_admin", em.Events()[0].Type)
exp := map[string]string{
"_contract_address": example.Contract.String(),
"new_admin_address": spec.expAdmin,
}
assert.Equal(t, exp, attrsToStringMap(em.Events()[0].Attributes))
})
}
}

func attrsToStringMap(attrs []abci.EventAttribute) map[string]string {
r := make(map[string]string, len(attrs))
for _, v := range attrs {
r[string(v.Key)] = string(v.Value)
}
return r
}
2 changes: 1 addition & 1 deletion x/wasm/keeper/test_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ func InstantiateReflectExampleContract(t testing.TB, ctx sdk.Context, keepers Te
example := StoreReflectContract(t, ctx, keepers)
initialAmount := sdk.NewCoins(sdk.NewInt64Coin("denom", 100))
label := "demo contract to query"
contractAddr, _, err := keepers.ContractKeeper.Instantiate(ctx, example.CodeID, example.CreatorAddr, nil, []byte("{}"), label, initialAmount)
contractAddr, _, err := keepers.ContractKeeper.Instantiate(ctx, example.CodeID, example.CreatorAddr, example.CreatorAddr, []byte("{}"), label, initialAmount)

require.NoError(t, err)
return ExampleInstance{
Expand Down
33 changes: 19 additions & 14 deletions x/wasm/types/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,29 @@ const (
// CustomContractEventPrefix contracts can create custom events. To not mix them with other system events they got the `wasm-` prefix.
CustomContractEventPrefix = "wasm-"

EventTypeStoreCode = "store_code"
EventTypeInstantiate = "instantiate"
EventTypeExecute = "execute"
EventTypeMigrate = "migrate"
EventTypePinCode = "pin_code"
EventTypeUnpinCode = "unpin_code"
EventTypeSudo = "sudo"
EventTypeReply = "reply"
EventTypeGovContractResult = "gov_contract_result"
EventTypeStoreCode = "store_code"
EventTypeInstantiate = "instantiate"
EventTypeExecute = "execute"
EventTypeMigrate = "migrate"
EventTypePinCode = "pin_code"
EventTypeUnpinCode = "unpin_code"
EventTypeSudo = "sudo"
EventTypeReply = "reply"
EventTypeGovContractResult = "gov_contract_result"
EventTypeUpdateContractAdmin = "update_contract_admin"
EventTypeUpdateCodeAccessConfig = "update_code_access_config"
)

// event attributes returned from contract execution
const (
AttributeReservedPrefix = "_"

AttributeKeyContractAddr = "_contract_address"
AttributeKeyCodeID = "code_id"
AttributeKeyChecksum = "code_checksum"
AttributeKeyResultDataHex = "result"
AttributeKeyRequiredCapability = "required_capability"
AttributeKeyContractAddr = "_contract_address"
AttributeKeyCodeID = "code_id"
AttributeKeyChecksum = "code_checksum"
AttributeKeyResultDataHex = "result"
AttributeKeyRequiredCapability = "required_capability"
AttributeKeyNewAdmin = "new_admin_address"
AttributeKeyCodePermission = "code_permission"
AttributeKeyAuthorizedAddresses = "authorized_addresses"
)
11 changes: 11 additions & 0 deletions x/wasm/types/types.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit bffff77

Please sign in to comment.