-
Notifications
You must be signed in to change notification settings - Fork 404
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
Add msg update contract label #1640
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -653,6 +653,26 @@ func (k Keeper) setContractAdmin(ctx context.Context, contractAddress, caller, n | |
return nil | ||
} | ||
|
||
func (k Keeper) setContractLabel(ctx context.Context, contractAddress, caller sdk.AccAddress, newLabel string, authZ types.AuthorizationPolicy) error { | ||
sdkCtx := sdk.UnwrapSDKContext(ctx) | ||
contractInfo := k.GetContractInfo(sdkCtx, contractAddress) | ||
if contractInfo == nil { | ||
return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "unknown contract") | ||
} | ||
if !authZ.CanModifyContract(contractInfo.AdminAddr(), caller) { | ||
return errorsmod.Wrap(sdkerrors.ErrUnauthorized, "can not modify contract") | ||
} | ||
contractInfo.Label = newLabel | ||
k.mustStoreContractInfo(sdkCtx, contractAddress, contractInfo) | ||
sdkCtx.EventManager().EmitEvent(sdk.NewEvent( | ||
types.EventTypeUpdateContractLabel, | ||
sdk.NewAttribute(types.AttributeKeyContractAddr, contractAddress.String()), | ||
sdk.NewAttribute(types.AttributeKeyNewLabel, newLabel), | ||
)) | ||
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. nice! |
||
|
||
return nil | ||
} | ||
|
||
func (k Keeper) appendToContractHistory(ctx context.Context, contractAddr sdk.AccAddress, newEntries ...types.ContractCodeHistoryEntry) error { | ||
store := k.storeService.OpenKVStore(ctx) | ||
// find last element position | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2489,6 +2489,69 @@ func TestGasConsumed(t *testing.T) { | |
} | ||
} | ||
|
||
func TestSetContractLabel(t *testing.T) { | ||
parentCtx, keepers := CreateTestInput(t, false, AvailableCapabilities) | ||
k := keepers.WasmKeeper | ||
example := InstantiateReflectExampleContract(t, parentCtx, keepers) | ||
|
||
specs := map[string]struct { | ||
newLabel string | ||
caller sdk.AccAddress | ||
policy types.AuthorizationPolicy | ||
contract sdk.AccAddress | ||
expErr bool | ||
}{ | ||
"update label - default policy": { | ||
newLabel: "new label", | ||
caller: example.CreatorAddr, | ||
policy: DefaultAuthorizationPolicy{}, | ||
contract: example.Contract, | ||
}, | ||
"update label - gov policy": { | ||
newLabel: "new label", | ||
policy: GovAuthorizationPolicy{}, | ||
caller: RandomAccountAddress(t), | ||
contract: example.Contract, | ||
}, | ||
"update label - unauthorized": { | ||
newLabel: "new label", | ||
caller: RandomAccountAddress(t), | ||
policy: DefaultAuthorizationPolicy{}, | ||
contract: example.Contract, | ||
expErr: true, | ||
}, | ||
"update label - unknown contract": { | ||
newLabel: "new label", | ||
caller: example.CreatorAddr, | ||
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. When you use a random caller address, then it would ensure the different behaviour of the gov policy |
||
policy: DefaultAuthorizationPolicy{}, | ||
contract: RandomAccountAddress(t), | ||
expErr: true, | ||
}, | ||
} | ||
for name, spec := range specs { | ||
t.Run(name, func(t *testing.T) { | ||
ctx, _ := parentCtx.CacheContext() | ||
em := sdk.NewEventManager() | ||
ctx = ctx.WithEventManager(em) | ||
gotErr := k.setContractLabel(ctx, spec.contract, spec.caller, spec.newLabel, spec.policy) | ||
if spec.expErr { | ||
require.Error(t, gotErr) | ||
return | ||
} | ||
require.NoError(t, gotErr) | ||
assert.Equal(t, spec.newLabel, k.GetContractInfo(ctx, spec.contract).Label) | ||
// and event emitted | ||
require.Len(t, em.Events(), 1) | ||
assert.Equal(t, "update_contract_label", em.Events()[0].Type) | ||
exp := map[string]string{ | ||
"_contract_address": spec.contract.String(), | ||
"new_label": spec.newLabel, | ||
} | ||
assert.Equal(t, exp, attrsToStringMap(em.Events()[0].Attributes)) | ||
}) | ||
} | ||
} | ||
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. Good tests! For bonus points, there is also the unknown contract address scenario that must not panic. |
||
|
||
func attrsToStringMap(attrs []abci.EventAttribute) map[string]string { | ||
r := make(map[string]string, len(attrs)) | ||
for _, v := range attrs { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -462,3 +462,26 @@ | |
Data: data, | ||
}, nil | ||
} | ||
|
||
func (m msgServer) UpdateContractLabel(ctx context.Context, msg *types.MsgUpdateContractLabel) (*types.MsgUpdateContractLabelResponse, error) { | ||
if err := msg.ValidateBasic(); err != nil { | ||
return nil, err | ||
} | ||
|
||
senderAddr, err := sdk.AccAddressFromBech32(msg.Sender) | ||
if err != nil { | ||
return nil, errorsmod.Wrap(err, "sender") | ||
} | ||
contractAddr, err := sdk.AccAddressFromBech32(msg.Contract) | ||
if err != nil { | ||
return nil, errorsmod.Wrap(err, "contract") | ||
} | ||
|
||
policy := m.selectAuthorizationPolicy(ctx, msg.Sender) | ||
|
||
if err := m.keeper.setContractLabel(ctx, contractAddr, senderAddr, msg.NewLabel, policy); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &types.MsgUpdateContractLabelResponse{}, nil | ||
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. straight forward 👍 |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1144,3 +1144,86 @@ func TestStoreAndMigrateContract(t *testing.T) { | |
}) | ||
} | ||
} | ||
|
||
func TestUpdateContractLabel(t *testing.T) { | ||
wasmApp := app.Setup(t) | ||
ctx := wasmApp.BaseApp.NewContextLegacy(false, tmproto.Header{Time: time.Now()}) | ||
|
||
var ( | ||
myAddress sdk.AccAddress = make([]byte, types.ContractAddrLen) | ||
authority = wasmApp.WasmKeeper.GetAuthority() | ||
_, _, otherAddr = testdata.KeyTestPubAddr() | ||
) | ||
|
||
specs := map[string]struct { | ||
addr string | ||
newLabel string | ||
expErr bool | ||
}{ | ||
"authority can update contract label": { | ||
addr: authority, | ||
newLabel: "new label", | ||
expErr: false, | ||
}, | ||
"admin can update contract label": { | ||
addr: myAddress.String(), | ||
newLabel: "new label", | ||
expErr: false, | ||
}, | ||
"other address cannot update contract label": { | ||
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. 🤔 good to have the scenarios covered but there is some overlap with the keeper tests. |
||
addr: otherAddr.String(), | ||
newLabel: "new label", | ||
expErr: true, | ||
}, | ||
"empty new label": { | ||
addr: authority, | ||
expErr: true, | ||
}, | ||
"invalid new label": { | ||
addr: authority, | ||
newLabel: " start with space ", | ||
expErr: true, | ||
}, | ||
} | ||
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. Please check for invalid data |
||
for name, spec := range specs { | ||
t.Run(name, func(t *testing.T) { | ||
// setup | ||
msg := &types.MsgStoreAndInstantiateContract{ | ||
Authority: spec.addr, | ||
WASMByteCode: wasmContract, | ||
InstantiatePermission: &types.AllowEverybody, | ||
Admin: myAddress.String(), | ||
UnpinCode: false, | ||
Label: "old label", | ||
Msg: []byte(`{}`), | ||
Funds: sdk.Coins{}, | ||
} | ||
rsp, err := wasmApp.MsgServiceRouter().Handler(msg)(ctx, msg) | ||
require.NoError(t, err) | ||
var storeAndInstantiateResponse types.MsgStoreAndInstantiateContractResponse | ||
require.NoError(t, wasmApp.AppCodec().Unmarshal(rsp.Data, &storeAndInstantiateResponse)) | ||
|
||
contract := storeAndInstantiateResponse.Address | ||
contractAddr, err := sdk.AccAddressFromBech32(contract) | ||
require.NoError(t, err) | ||
require.Equal(t, "old label", wasmApp.WasmKeeper.GetContractInfo(ctx, contractAddr).Label) | ||
|
||
// when | ||
msgUpdateLabel := &types.MsgUpdateContractLabel{ | ||
Sender: spec.addr, | ||
NewLabel: spec.newLabel, | ||
Contract: storeAndInstantiateResponse.Address, | ||
} | ||
_, err = wasmApp.MsgServiceRouter().Handler(msgUpdateLabel)(ctx, msgUpdateLabel) | ||
|
||
// then | ||
if spec.expErr { | ||
require.Error(t, err) | ||
require.Equal(t, "old label", wasmApp.WasmKeeper.GetContractInfo(ctx, contractAddr).Label) | ||
} else { | ||
require.NoError(t, err) | ||
require.Equal(t, spec.newLabel, wasmApp.WasmKeeper.GetContractInfo(ctx, contractAddr).Label) | ||
} | ||
}) | ||
} | ||
} | ||
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. Nice test! |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,7 @@ | |
cdc.RegisterConcrete(&MsgAddCodeUploadParamsAddresses{}, "wasm/MsgAddCodeUploadParamsAddresses", nil) | ||
cdc.RegisterConcrete(&MsgRemoveCodeUploadParamsAddresses{}, "wasm/MsgRemoveCodeUploadParamsAddresses", nil) | ||
cdc.RegisterConcrete(&MsgStoreAndMigrateContract{}, "wasm/MsgStoreAndMigrateContract", nil) | ||
cdc.RegisterConcrete(&MsgUpdateContractLabel{}, "wasm/MsgUpdateContractLabel", nil) | ||
|
||
cdc.RegisterConcrete(&PinCodesProposal{}, "wasm/PinCodesProposal", nil) | ||
cdc.RegisterConcrete(&UnpinCodesProposal{}, "wasm/UnpinCodesProposal", nil) | ||
|
@@ -79,6 +80,7 @@ | |
&MsgAddCodeUploadParamsAddresses{}, | ||
&MsgRemoveCodeUploadParamsAddresses{}, | ||
&MsgStoreAndMigrateContract{}, | ||
&MsgUpdateContractLabel{}, | ||
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. Important! |
||
) | ||
registry.RegisterImplementations( | ||
(*v1beta1.Content)(nil), | ||
|
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.
👍