-
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
Merged
Merged
Changes from 10 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
b620034
add permission param `ContractStatusAccess`
90863fe
add `ContractStatus` type and field
d766bfd
add msg `MsgUpdateContractStatus`
1eca526
reject inactive smart contract call
c6c6868
add changlog entry to `CHANGELOG_PENDING.md`
7206053
use spaces rather than tabs for json tamplate
bb367d3
Add comments for GovAuthorizationPolicy
b115e2c
Add gov proposal for update contract status
397865b
Add proposal handler for updating contract status
3d284fb
Merge branch 'v2/develop' into whylee/v2/feat/contract-access-control
b6a7f1a
Merge branch 'v2/develop' into whylee/v2/feat/contract-access-control
6620bc5
Merge branch 'v2/develop' into whylee/v2/feat/contract-access-control
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -127,6 +127,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) | ||
|
@@ -289,7 +295,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) | ||
|
@@ -324,6 +330,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(k.getInstanceCost(ctx), "Loading CosmWasm module: execute") | ||
|
@@ -377,6 +386,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") | ||
} | ||
|
@@ -534,11 +546,35 @@ 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 { | ||
return k.updateContractStatus(ctx, contractAddress, caller, status, k.authZPolicy) | ||
} | ||
|
||
func (k Keeper) updateContractStatus(ctx sdk.Context, contractAddress sdk.AccAddress, caller sdk.AccAddress, status types.ContractStatus, authZ AuthorizationPolicy) error { | ||
if !authZ.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") | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.