Skip to content

Commit

Permalink
refactor: ensure different owner (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnletey authored Jun 18, 2024
1 parent f553945 commit 44c811a
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .github/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@

This repository includes the `x/aura` Cosmos SDK module implementation.

For more information, refer to the modules [spec](../x/aura/spec) files.
For more information, refer to the module's [spec](../x/aura/spec) files.
6 changes: 5 additions & 1 deletion utils/mocks/aura.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@ func AuraKeeperWithBank(_ testing.TB, bank BankKeeper) (*keeper.Keeper, sdk.Cont
key := storetypes.NewKVStoreKey(types.ModuleName)
tkey := storetypes.NewTransientStoreKey("transient_aura")

reg := codectypes.NewInterfaceRegistry()
types.RegisterInterfaces(reg)
cdc := codec.NewProtoCodec(reg)

k := keeper.NewKeeper(
codec.NewProtoCodec(codectypes.NewInterfaceRegistry()),
cdc,
key,
"ausdy",
nil,
Expand Down
4 changes: 4 additions & 0 deletions x/aura/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ func (k msgServer) TransferOwnership(goCtx context.Context, msg *types.MsgTransf
return nil, sdkerrors.Wrapf(types.ErrInvalidOwner, "expected %s, got %s", owner, msg.Signer)
}

if msg.NewOwner == owner {
return nil, types.ErrSameOwner
}

k.SetPendingOwner(ctx, msg.NewOwner)

return &types.MsgTransferOwnershipResponse{}, ctx.EventManager().EmitTypedEvent(&types.OwnershipTransferStarted{
Expand Down
4 changes: 4 additions & 0 deletions x/aura/keeper/msg_server_blocklist.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ func (k blocklistMsgServer) TransferOwnership(goCtx context.Context, msg *blockl
return nil, errors.Wrapf(blocklist.ErrInvalidOwner, "expected %s, got %s", owner, msg.Signer)
}

if msg.NewOwner == owner {
return nil, blocklist.ErrSameOwner
}

k.SetBlocklistPendingOwner(ctx, msg.NewOwner)

return &blocklist.MsgTransferOwnershipResponse{}, ctx.EventManager().EmitTypedEvent(&blocklist.OwnershipTransferStarted{
Expand Down
8 changes: 8 additions & 0 deletions x/aura/keeper/msg_server_blocklist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ func TestBlocklistTransferOwnership(t *testing.T) {
// ASSERT: The action should've failed due to invalid signer.
require.ErrorContains(t, err, blocklist.ErrInvalidOwner.Error())

// ACT: Attempt to transfer ownership to same owner.
_, err = server.TransferOwnership(goCtx, &blocklist.MsgTransferOwnership{
Signer: owner.Address,
NewOwner: owner.Address,
})
// ASSERT: The action should've failed due to same owner.
require.ErrorContains(t, err, blocklist.ErrSameOwner.Error())

// ARRANGE: Generate a pending owner account.
pendingOwner := utils.TestAccount()

Expand Down
8 changes: 8 additions & 0 deletions x/aura/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,14 @@ func TestTransferOwnership(t *testing.T) {
// ASSERT: The action should've failed due to invalid signer.
require.ErrorContains(t, err, types.ErrInvalidOwner.Error())

// ACT: Attempt to transfer ownership to same owner.
_, err = server.TransferOwnership(goCtx, &types.MsgTransferOwnership{
Signer: owner.Address,
NewOwner: owner.Address,
})
// ASSERT: The action should've failed due to same owner.
require.ErrorContains(t, err, types.ErrSameOwner.Error())

// ARRANGE: Generate a pending owner account.
pendingOwner := utils.TestAccount()

Expand Down
2 changes: 1 addition & 1 deletion x/aura/keeper/query_server_blocklist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func TestBlocklistAddressQuery(t *testing.T) {
_, err = server.Address(goCtx, &blocklist.QueryAddress{
Address: "cosmos10d07y265gmmuvt4z0w9aw880jnsr700j6zn9kn",
})
// ASSERT: The action should've failed due to invalid address.
// ASSERT: The query should've failed due to invalid address.
require.ErrorContains(t, err, "unable to decode address")

// ACT: Attempt to query blocked state of unblocked address.
Expand Down
1 change: 1 addition & 0 deletions x/aura/spec/02_messages.md
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ A message that initiates an ownership transfer to a provided address.
### Requirements

- Signer must be the current [`owner`](./01_state.md#owner).
- `new_owner` must not be the current [`owner`](./01_state.md#owner).

### State Changes

Expand Down
1 change: 1 addition & 0 deletions x/aura/spec/02_messages_blocklist.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ A message that initiates an ownership transfer to a provided address.
### Requirements

- Signer must be the current [`owner`](./01_state_blocklist.md#owner).
- `new_owner` must not be the current [`owner`](./01_state_blocklist.md#owner).

### State Changes

Expand Down
7 changes: 4 additions & 3 deletions x/aura/types/blocklist/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ var (
Codespace = "aura/blocklist"

ErrNoOwner = errors.Register(Codespace, 1, "there is no blocklist owner")
ErrInvalidOwner = errors.Register(Codespace, 2, "signer is not blocklist owner")
ErrNoPendingOwner = errors.Register(Codespace, 3, "there is no pending blocklist owner")
ErrInvalidPendingOwner = errors.Register(Codespace, 4, "signer is not blocklist pending owner")
ErrSameOwner = errors.Register(Codespace, 2, "provided owner is the current owner")
ErrInvalidOwner = errors.Register(Codespace, 3, "signer is not blocklist owner")
ErrNoPendingOwner = errors.Register(Codespace, 4, "there is no pending blocklist owner")
ErrInvalidPendingOwner = errors.Register(Codespace, 5, "signer is not blocklist pending owner")
)
15 changes: 8 additions & 7 deletions x/aura/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import "cosmossdk.io/errors"

var (
ErrNoOwner = errors.Register(ModuleName, 1, "there is no owner")
ErrInvalidOwner = errors.Register(ModuleName, 2, "signer is not owner")
ErrNoPendingOwner = errors.Register(ModuleName, 3, "there is no pending owner")
ErrInvalidPendingOwner = errors.Register(ModuleName, 4, "signer is not pending owner")
ErrInvalidBurner = errors.Register(ModuleName, 5, "signer is not a burner")
ErrInvalidMinter = errors.Register(ModuleName, 6, "signer is not a minter")
ErrInvalidPauser = errors.Register(ModuleName, 7, "signer is not a pauser")
ErrInsufficientAllowance = errors.Register(ModuleName, 8, "insufficient allowance")
ErrSameOwner = errors.Register(ModuleName, 2, "provided owner is the current owner")
ErrInvalidOwner = errors.Register(ModuleName, 3, "signer is not owner")
ErrNoPendingOwner = errors.Register(ModuleName, 4, "there is no pending owner")
ErrInvalidPendingOwner = errors.Register(ModuleName, 5, "signer is not pending owner")
ErrInvalidBurner = errors.Register(ModuleName, 6, "signer is not a burner")
ErrInvalidMinter = errors.Register(ModuleName, 7, "signer is not a minter")
ErrInvalidPauser = errors.Register(ModuleName, 8, "signer is not a pauser")
ErrInsufficientAllowance = errors.Register(ModuleName, 9, "insufficient allowance")
)

0 comments on commit 44c811a

Please sign in to comment.