-
Notifications
You must be signed in to change notification settings - Fork 31
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
feat(wasm): add contract access control #151
Changes from 5 commits
b620034
90863fe
d766bfd
1eca526
c6c6868
7206053
bb367d3
b115e2c
397865b
3d284fb
b6a7f1a
6620bc5
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 |
---|---|---|
|
@@ -444,7 +444,10 @@ func TestImportContractWithCodeHistoryReset(t *testing.T) { | |
"permission": "Everybody" | ||
}, | ||
"instantiate_default_permission": "Everybody", | ||
"max_wasm_code_size": 500000 | ||
"max_wasm_code_size": 500000, | ||
"contract_status_access": { | ||
"permission": "Nobody" | ||
} | ||
}, | ||
"codes": [ | ||
{ | ||
|
@@ -469,7 +472,8 @@ func TestImportContractWithCodeHistoryReset(t *testing.T) { | |
"code_id": "1", | ||
"creator": "link1p0yx9c9q4xsnedlcn24gqfry5dcu6e9xkhv9aj", | ||
"admin": "link1qyqszqgpqyqszqgpqyqszqgpqyqszqgp8apuk5", | ||
"label": "ȀĴnZV芢毤" | ||
"label": "ȀĴnZV芢毤", | ||
"status": "Active" | ||
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. Mixed Tab vs Space in these templates. Want to unify. 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 have fixed it to use spaces. |
||
} | ||
} | ||
], | ||
|
@@ -534,6 +538,7 @@ func TestImportContractWithCodeHistoryReset(t *testing.T) { | |
Admin: adminAddr, | ||
Label: "ȀĴnZV芢毤", | ||
Created: &types.AbsoluteTxPosition{BlockHeight: 0, TxIndex: 0}, | ||
Status: wasmTypes.ContractStatusActive, | ||
} | ||
assert.Equal(t, expContractInfo, *gotContractInfo) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -142,6 +142,12 @@ func (k Keeper) getInstantiateAccessConfig(ctx sdk.Context) types.AccessType { | |
return a | ||
} | ||
|
||
func (k Keeper) getContractStatusAccessConfig(ctx sdk.Context) types.AccessConfig { | ||
var a types.AccessConfig | ||
k.paramSpace.Get(ctx, types.ParamStoreKeyContractStatusAccess, &a) | ||
return a | ||
} | ||
|
||
func (k Keeper) GetMaxWasmCodeSize(ctx sdk.Context) uint64 { | ||
var a uint64 | ||
k.paramSpace.Get(ctx, types.ParamStoreKeyMaxWasmCodeSize, &a) | ||
|
@@ -286,7 +292,7 @@ func (k Keeper) instantiate(ctx sdk.Context, codeID uint64, creator, admin sdk.A | |
|
||
// persist instance first | ||
createdAt := types.NewAbsoluteTxPosition(ctx) | ||
contractInfo := types.NewContractInfo(codeID, creator, admin, label, createdAt) | ||
contractInfo := types.NewContractInfo(codeID, creator, admin, label, createdAt, types.ContractStatusActive) | ||
|
||
// check for IBC flag | ||
report, err := k.wasmer.AnalyzeCode(codeInfo.CodeHash) | ||
|
@@ -321,6 +327,9 @@ func (k Keeper) Execute(ctx sdk.Context, contractAddress sdk.AccAddress, caller | |
if err != nil { | ||
return nil, err | ||
} | ||
if contractInfo.Status != types.ContractStatusActive { | ||
return nil, sdkerrors.Wrap(types.ErrInvalid, "inactive contract") | ||
} | ||
|
||
if !k.IsPinnedCode(ctx, contractInfo.CodeID) { | ||
ctx.GasMeter().ConsumeGas(InstanceCost, "Loading CosmWasm module: execute") | ||
|
@@ -374,6 +383,9 @@ func (k Keeper) migrate(ctx sdk.Context, contractAddress sdk.AccAddress, caller | |
if contractInfo == nil { | ||
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "unknown contract") | ||
} | ||
if contractInfo.Status != types.ContractStatusActive { | ||
return nil, sdkerrors.Wrap(types.ErrInvalid, "inactive contract") | ||
} | ||
if !authZ.CanModifyContract(contractInfo.AdminAddr(), caller) { | ||
return nil, sdkerrors.Wrap(sdkerrors.ErrUnauthorized, "can not migrate") | ||
} | ||
|
@@ -530,11 +542,31 @@ func (k Keeper) ClearContractAdmin(ctx sdk.Context, contractAddress sdk.AccAddre | |
return k.setContractAdmin(ctx, contractAddress, caller, nil, k.authZPolicy) | ||
} | ||
|
||
// UpdateContractStatus sets a new status of the contract on the ContractInfo. | ||
func (k Keeper) UpdateContractStatus(ctx sdk.Context, contractAddress sdk.AccAddress, caller sdk.AccAddress, status types.ContractStatus) error { | ||
if !k.authZPolicy.CanUpdateContractStatus(k.getContractStatusAccessConfig(ctx), caller) { | ||
return sdkerrors.Wrap(sdkerrors.ErrUnauthorized, "can not update contract status") | ||
} | ||
|
||
contractInfo := k.GetContractInfo(ctx, contractAddress) | ||
if contractInfo == nil { | ||
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "unknown contract") | ||
} | ||
if contractInfo.Status != status { | ||
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. Can status be 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. No, it is a value type. |
||
contractInfo.Status = status | ||
k.storeContractInfo(ctx, contractAddress, contractInfo) | ||
} | ||
return nil | ||
} | ||
|
||
func (k Keeper) setContractAdmin(ctx sdk.Context, contractAddress, caller, newAdmin sdk.AccAddress, authZ AuthorizationPolicy) error { | ||
contractInfo := k.GetContractInfo(ctx, contractAddress) | ||
if contractInfo == nil { | ||
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "unknown contract") | ||
} | ||
if contractInfo.Status != types.ContractStatusActive { | ||
return sdkerrors.Wrap(types.ErrInvalid, "inactive contract") | ||
} | ||
if !authZ.CanModifyContract(contractInfo.AdminAddr(), caller) { | ||
return sdkerrors.Wrap(sdkerrors.ErrUnauthorized, "can not modify contract") | ||
} | ||
|
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.
When is this (and above 3 functions) called and what is this for? Want comment doc.
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.
I have added the comment to the code.