Skip to content
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

Minor chore #1522

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 13 additions & 19 deletions x/wasm/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,22 +375,13 @@ func (m msgServer) AddCodeUploadParamsAddresses(goCtx context.Context, req *type
}

addresses := params.CodeUploadAccess.Addresses
size := len(addresses)
for _, newAddr := range req.Addresses {
exists := false
for i := 0; i < size; i++ {
if addresses[i] == newAddr {
exists = true
break
}
}
if !exists {
if !contains(addresses, newAddr) {
addresses = append(addresses, newAddr)
}
}

params.CodeUploadAccess.Addresses = addresses

if err := m.keeper.SetParams(ctx, params); err != nil {
return nil, err
}
Expand All @@ -417,16 +408,10 @@ func (m msgServer) RemoveCodeUploadParamsAddresses(goCtx context.Context, req *t
addresses := params.CodeUploadAccess.Addresses
newAddresses := make([]string, 0)
for _, addr := range addresses {
reject := false
for i := 0; i < len(req.Addresses); i++ {
if req.Addresses[i] == addr {
reject = true
break
}
}
if !reject {
newAddresses = append(newAddresses, addr)
if contains(req.Addresses, addr) {
continue
}
newAddresses = append(newAddresses, addr)
}

params.CodeUploadAccess.Addresses = newAddresses
Expand All @@ -438,6 +423,15 @@ func (m msgServer) RemoveCodeUploadParamsAddresses(goCtx context.Context, req *t
return &types.MsgRemoveCodeUploadParamsAddressesResponse{}, nil
}

func contains[T comparable](src []T, o T) bool {
alpe marked this conversation as resolved.
Show resolved Hide resolved
for _, v := range src {
if v == o {
return true
}
}
return false
}

func (m msgServer) selectAuthorizationPolicy(ctx sdk.Context, actor string) types.AuthorizationPolicy {
if actor == m.keeper.GetAuthority() {
return newGovAuthorizationPolicy(m.keeper.propagateGovAuthorization)
Expand Down
46 changes: 31 additions & 15 deletions x/wasm/keeper/msg_server_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,14 @@ func TestAddCodeUploadParamsAddresses(t *testing.T) {
},
uploadConfig: types.AccessTypeAnyOfAddresses.With(myAddress),
expUploadConfig: types.AccessTypeAnyOfAddresses.With(myAddress, otherAddr),
expErr: false,
},
"add existing address": {
alpe marked this conversation as resolved.
Show resolved Hide resolved
src: types.MsgAddCodeUploadParamsAddresses{
Authority: govAuthority,
Addresses: []string{myAddress.String()},
},
uploadConfig: types.AccessTypeAnyOfAddresses.With(myAddress),
expUploadConfig: types.AccessTypeAnyOfAddresses.With(myAddress),
},
"authority cannot add addresses when permission is allow everybody": {
src: types.MsgAddCodeUploadParamsAddresses{
Expand Down Expand Up @@ -229,15 +236,16 @@ func TestAddCodeUploadParamsAddresses(t *testing.T) {
if spec.expErr {
require.Error(t, err)
require.Nil(t, rsp)
} else {
require.NoError(t, err)
var result types.MsgAddCodeUploadParamsAddressesResponse
require.NoError(t, wasmApp.AppCodec().Unmarshal(rsp.Data, &result))
return
}
require.NoError(t, err)
require.Len(t, rsp.MsgResponses, 1)
assert.IsType(t, rsp.MsgResponses[0].GetCachedValue(), &types.MsgAddCodeUploadParamsAddressesResponse{})

// then
assert.True(t, spec.expUploadConfig.Equals(wasmApp.WasmKeeper.GetParams(ctx).CodeUploadAccess),
"got %#v not %#v", wasmApp.WasmKeeper.GetParams(ctx).CodeUploadAccess, spec.expUploadConfig)
gotUploadConfig := wasmApp.WasmKeeper.GetParams(ctx).CodeUploadAccess
assert.True(t, spec.expUploadConfig.Equals(gotUploadConfig),
"got %#v not %#v", gotUploadConfig, spec.expUploadConfig)
})
}
}
Expand Down Expand Up @@ -265,7 +273,14 @@ func TestRemoveCodeUploadParamsAddresses(t *testing.T) {
},
uploadConfig: types.AccessTypeAnyOfAddresses.With(myAddress, otherAddr),
expUploadConfig: types.AccessTypeAnyOfAddresses.With(myAddress),
expErr: false,
},
"remove unknown address does not fail": {
alpe marked this conversation as resolved.
Show resolved Hide resolved
src: types.MsgRemoveCodeUploadParamsAddresses{
Authority: govAuthority,
Addresses: []string{otherAddr.String()},
},
uploadConfig: types.AccessTypeAnyOfAddresses.With(myAddress),
expUploadConfig: types.AccessTypeAnyOfAddresses.With(myAddress),
},
"authority cannot remove addresses when permission is allow everybody": {
src: types.MsgRemoveCodeUploadParamsAddresses{
Expand Down Expand Up @@ -309,7 +324,7 @@ func TestRemoveCodeUploadParamsAddresses(t *testing.T) {
Addresses: []string{otherAddr.String()},
},
uploadConfig: types.AccessTypeAnyOfAddresses.With(myAddress, otherAddr),
expUploadConfig: types.AccessTypeAnyOfAddresses.With(myAddress),
expUploadConfig: types.AccessTypeAnyOfAddresses.With(myAddress, otherAddr),
alpe marked this conversation as resolved.
Show resolved Hide resolved
expErr: true,
},
}
Expand All @@ -326,15 +341,16 @@ func TestRemoveCodeUploadParamsAddresses(t *testing.T) {
if spec.expErr {
require.Error(t, err)
require.Nil(t, rsp)
} else {
require.NoError(t, err)
var result types.MsgAddCodeUploadParamsAddressesResponse
require.NoError(t, wasmApp.AppCodec().Unmarshal(rsp.Data, &result))
return
}
require.NoError(t, err)
require.Len(t, rsp.MsgResponses, 1)
assert.IsType(t, rsp.MsgResponses[0].GetCachedValue(), &types.MsgRemoveCodeUploadParamsAddressesResponse{})

// then
assert.True(t, spec.expUploadConfig.Equals(wasmApp.WasmKeeper.GetParams(ctx).CodeUploadAccess),
"got %#v not %#v", wasmApp.WasmKeeper.GetParams(ctx).CodeUploadAccess, spec.expUploadConfig)
gotUploadConfig := wasmApp.WasmKeeper.GetParams(ctx).CodeUploadAccess
assert.True(t, spec.expUploadConfig.Equals(gotUploadConfig),
"got %#v not %#v", gotUploadConfig, spec.expUploadConfig)
})
}
}
Expand Down