Skip to content

Commit

Permalink
sdkerrors.Wrap
Browse files Browse the repository at this point in the history
  • Loading branch information
0xbala-k committed Mar 15, 2023
1 parent b8d6300 commit 2c88328
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 66 deletions.
9 changes: 5 additions & 4 deletions x/registry/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keeper
import (
"context"

errorsmod "cosmossdk.io/errors"
"github.com/cerc-io/laconicd/x/registry/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
Expand Down Expand Up @@ -48,7 +49,7 @@ func (q Querier) GetRecord(c context.Context, req *types.QueryRecordByIDRequest)
ctx := sdk.UnwrapSDKContext(c)
id := req.GetId()
if !q.Keeper.HasRecord(ctx, id) {
return nil, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "Record not found.")
return nil, errorsmod.Wrap(sdkerrors.ErrUnknownRequest, "Record not found.")
}
record := q.Keeper.GetRecord(ctx, id)
return &types.QueryRecordByIDResponse{Record: record}, nil
Expand Down Expand Up @@ -86,11 +87,11 @@ func (q Querier) LookupCrn(c context.Context, req *types.QueryLookupCrn) (*types
ctx := sdk.UnwrapSDKContext(c)
crn := req.GetCrn()
if !q.Keeper.HasNameRecord(ctx, crn) {
return nil, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "CRN not found.")
return nil, errorsmod.Wrap(sdkerrors.ErrUnknownRequest, "CRN not found.")
}
nameRecord := q.Keeper.GetNameRecord(ctx, crn)
if nameRecord == nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "name record not found.")
return nil, errorsmod.Wrap(sdkerrors.ErrUnknownRequest, "name record not found.")
}
return &types.QueryLookupCrnResponse{Name: nameRecord}, nil
}
Expand All @@ -100,7 +101,7 @@ func (q Querier) ResolveCrn(c context.Context, req *types.QueryResolveCrn) (*typ
crn := req.GetCrn()
record := q.Keeper.ResolveCRN(ctx, crn)
if record == nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "record not found.")
return nil, errorsmod.Wrap(sdkerrors.ErrUnknownRequest, "record not found.")
}
return &types.QueryResolveCrnResponse{Record: record}, nil
}
Expand Down
7 changes: 4 additions & 3 deletions x/registry/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"sort"
"time"

errorsmod "cosmossdk.io/errors"
auctionkeeper "github.com/cerc-io/laconicd/x/auction/keeper"
bondkeeper "github.com/cerc-io/laconicd/x/bond/keeper"
"github.com/cerc-io/laconicd/x/registry/helpers"
Expand Down Expand Up @@ -240,7 +241,7 @@ func (k Keeper) ProcessSetRecord(ctx sdk.Context, msg types.MsgSetRecord) (*type
resourceSignBytes, _ := record.GetSignBytes()
cid, err := record.GetCID()
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "Invalid record JSON")
return nil, errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "Invalid record JSON")
}

record.ID = cid
Expand All @@ -255,13 +256,13 @@ func (k Keeper) ProcessSetRecord(ctx sdk.Context, msg types.MsgSetRecord) (*type
pubKey, err := legacy.PubKeyFromBytes(helpers.BytesFromBase64(sig.PubKey))
if err != nil {
fmt.Println("Error decoding pubKey from bytes: ", err)
return nil, sdkerrors.Wrap(sdkerrors.ErrUnauthorized, "Invalid public key.")
return nil, errorsmod.Wrap(sdkerrors.ErrUnauthorized, "Invalid public key.")
}

sigOK := pubKey.VerifySignature(resourceSignBytes, helpers.BytesFromBase64(sig.Sig))
if !sigOK {
fmt.Println("Signature mismatch: ", sig.PubKey)
return nil, sdkerrors.Wrap(sdkerrors.ErrUnauthorized, "Invalid signature.")
return nil, errorsmod.Wrap(sdkerrors.ErrUnauthorized, "Invalid signature.")
}
record.Owners = append(record.Owners, pubKey.Address().String())
}
Expand Down
37 changes: 19 additions & 18 deletions x/registry/keeper/naming_keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"
"time"

errorsmod "cosmossdk.io/errors"
auctiontypes "github.com/cerc-io/laconicd/x/auction/types"
"github.com/cerc-io/laconicd/x/registry/helpers"
"github.com/cerc-io/laconicd/x/registry/types"
Expand Down Expand Up @@ -112,12 +113,12 @@ func (k Keeper) updateBlockChangeSetForName(ctx sdk.Context, crn string) {
func (k Keeper) getAuthority(ctx sdk.Context, crn string) (string, *url.URL, *types.NameAuthority, error) {
parsedCRN, err := url.Parse(crn)
if err != nil {
return "", nil, nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "Invalid CRN.")
return "", nil, nil, errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "Invalid CRN.")
}

name := parsedCRN.Host
if !k.HasNameAuthority(ctx, name) {
return name, nil, nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "Name authority not found.")
return name, nil, nil, errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "Name authority not found.")
}
authority := k.GetNameAuthority(ctx, name)
return name, parsedCRN, &authority, nil
Expand All @@ -131,19 +132,19 @@ func (k Keeper) checkCRNAccess(ctx sdk.Context, signer sdk.AccAddress, crn strin

formattedCRN := fmt.Sprintf("crn://%s%s", name, parsedCRN.RequestURI())
if formattedCRN != crn {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "Invalid CRN.")
return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "Invalid CRN.")
}

if authority.OwnerAddress != signer.String() {
return sdkerrors.Wrap(sdkerrors.ErrUnauthorized, "Access denied.")
return errorsmod.Wrap(sdkerrors.ErrUnauthorized, "Access denied.")
}

if authority.Status != types.AuthorityActive {
return sdkerrors.Wrap(sdkerrors.ErrUnauthorized, "Authority is not active.")
return errorsmod.Wrap(sdkerrors.ErrUnauthorized, "Authority is not active.")
}

if authority.BondId == "" || len(authority.BondId) == 0 {
return sdkerrors.Wrap(sdkerrors.ErrUnauthorized, "Authority bond not found.")
return errorsmod.Wrap(sdkerrors.ErrUnauthorized, "Authority bond not found.")
}

if authority.OwnerPublicKey == "" {
Expand Down Expand Up @@ -323,13 +324,13 @@ func (k Keeper) ProcessReserveSubAuthority(ctx sdk.Context, name string, msg typ

// Check if parent authority exists.
if !k.HasNameAuthority(ctx, parent) {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "Parent authority not found.")
return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "Parent authority not found.")
}
parentAuthority := k.GetNameAuthority(ctx, parent)

// Sub-authority creator needs to be the owner of the parent authority.
if parentAuthority.OwnerAddress != msg.Signer {
return sdkerrors.Wrap(sdkerrors.ErrUnauthorized, "Access denied.")
return errorsmod.Wrap(sdkerrors.ErrUnauthorized, "Access denied.")
}

// Sub-authority owner defaults to parent authority owner.
Expand Down Expand Up @@ -362,17 +363,17 @@ func (k Keeper) createAuthority(ctx sdk.Context, name string, owner string, isRo
if k.HasNameAuthority(ctx, name) {
authority := k.GetNameAuthority(ctx, name)
if authority.Status != types.AuthorityExpired {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "Name already reserved.")
return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "Name already reserved.")
}
}

ownerAddress, err := sdk.AccAddressFromBech32(owner)
if err != nil {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "Invalid owner address.")
return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "Invalid owner address.")
}
ownerAccount := k.accountKeeper.GetAccount(ctx, ownerAddress)
if ownerAccount == nil {
return sdkerrors.Wrap(sdkerrors.ErrUnknownAddress, "Account not found.")
return errorsmod.Wrap(sdkerrors.ErrUnknownAddress, "Account not found.")
}

authority := types.NameAuthority{
Expand Down Expand Up @@ -430,11 +431,11 @@ func (k Keeper) ProcessReserveAuthority(ctx sdk.Context, msg types.MsgReserveAut
crn := fmt.Sprintf("crn://%s", msg.GetName())
parsedCrn, err := url.Parse(crn)
if err != nil {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "Invalid name")
return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "Invalid name")
}
name := parsedCrn.Host
if fmt.Sprintf("crn://%s", name) != crn {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "Invalid name")
return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "Invalid name")
}
if strings.Contains(name, ".") {
return k.ProcessReserveSubAuthority(ctx, name, msg)
Expand All @@ -450,20 +451,20 @@ func (k Keeper) ProcessSetAuthorityBond(ctx sdk.Context, msg types.MsgSetAuthori
name := msg.GetName()
signer := msg.GetSigner()
if !k.HasNameAuthority(ctx, name) {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "Name authority not found.")
return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "Name authority not found.")
}
authority := k.GetNameAuthority(ctx, name)
if authority.OwnerAddress != signer {
return sdkerrors.Wrap(sdkerrors.ErrUnauthorized, "Access denied")
return errorsmod.Wrap(sdkerrors.ErrUnauthorized, "Access denied")
}

if !k.bondKeeper.HasBond(ctx, msg.BondId) {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "Bond not found.")
return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "Bond not found.")
}
//
bond := k.bondKeeper.GetBond(ctx, msg.BondId)
if bond.Owner != signer {
return sdkerrors.Wrap(sdkerrors.ErrUnauthorized, "Bond owner mismatch.")
return errorsmod.Wrap(sdkerrors.ErrUnauthorized, "Bond owner mismatch.")
}

// No-op if bond hasn't changed.
Expand Down Expand Up @@ -496,7 +497,7 @@ func (k Keeper) ProcessDeleteName(ctx sdk.Context, msg types.MsgDeleteNameAuthor
}

if !k.HasNameRecord(ctx, msg.Crn) {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "Name not found.")
return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "Name not found.")
}

// Set CID to empty string.
Expand Down
31 changes: 16 additions & 15 deletions x/registry/keeper/record_keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"time"

errorsmod "cosmossdk.io/errors"
auctionkeeper "github.com/cerc-io/laconicd/x/auction/keeper"
auctiontypes "github.com/cerc-io/laconicd/x/auction/types"
bondtypes "github.com/cerc-io/laconicd/x/bond/types"
Expand Down Expand Up @@ -158,7 +159,7 @@ func (k RecordKeeper) QueryRecordsByBond(ctx sdk.Context, bondID string) []types
// ProcessRenewRecord renews a record.
func (k Keeper) ProcessRenewRecord(ctx sdk.Context, msg types.MsgRenewRecord) error {
if !k.HasRecord(ctx, msg.RecordId) {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "Record not found.")
return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "Record not found.")
}

// Check if renewal is required (i.e. expired record marked as deleted).
Expand All @@ -169,7 +170,7 @@ func (k Keeper) ProcessRenewRecord(ctx sdk.Context, msg types.MsgRenewRecord) er
}

if !record.Deleted || expiryTime.After(ctx.BlockTime()) {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "Renewal not required.")
return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "Renewal not required.")
}

recordType := record.ToRecordType()
Expand All @@ -184,23 +185,23 @@ func (k Keeper) ProcessRenewRecord(ctx sdk.Context, msg types.MsgRenewRecord) er
// ProcessAssociateBond associates a record with a bond.
func (k Keeper) ProcessAssociateBond(ctx sdk.Context, msg types.MsgAssociateBond) error {
if !k.HasRecord(ctx, msg.RecordId) {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "Record not found.")
return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "Record not found.")
}

if !k.bondKeeper.HasBond(ctx, msg.BondId) {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "Bond not found.")
return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "Bond not found.")
}

// Check if already associated with a bond.
record := k.GetRecord(ctx, msg.RecordId)
if record.BondId != "" || len(record.BondId) != 0 {
return sdkerrors.Wrap(sdkerrors.ErrUnauthorized, "Bond already exists.")
return errorsmod.Wrap(sdkerrors.ErrUnauthorized, "Bond already exists.")
}

// Only the bond owner can associate a record with the bond.
bond := k.bondKeeper.GetBond(ctx, msg.BondId)
if msg.Signer != bond.Owner {
return sdkerrors.Wrap(sdkerrors.ErrUnauthorized, "Bond owner mismatch.")
return errorsmod.Wrap(sdkerrors.ErrUnauthorized, "Bond owner mismatch.")
}

record.BondId = msg.BondId
Expand All @@ -218,20 +219,20 @@ func (k Keeper) ProcessAssociateBond(ctx sdk.Context, msg types.MsgAssociateBond
// ProcessDissociateBond dissociates a record from its bond.
func (k Keeper) ProcessDissociateBond(ctx sdk.Context, msg types.MsgDissociateBond) error {
if !k.HasRecord(ctx, msg.RecordId) {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "Record not found.")
return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "Record not found.")
}

// Check if associated with a bond.
record := k.GetRecord(ctx, msg.RecordId)
bondID := record.BondId
if bondID == "" || len(bondID) == 0 {
return sdkerrors.Wrap(sdkerrors.ErrUnauthorized, "Bond not found.")
return errorsmod.Wrap(sdkerrors.ErrUnauthorized, "Bond not found.")
}

// Only the bond owner can dissociate a record from the bond.
bond := k.bondKeeper.GetBond(ctx, bondID)
if msg.Signer != bond.Owner {
return sdkerrors.Wrap(sdkerrors.ErrUnauthorized, "Bond owner mismatch.")
return errorsmod.Wrap(sdkerrors.ErrUnauthorized, "Bond owner mismatch.")
}

// Clear bond ID.
Expand All @@ -245,13 +246,13 @@ func (k Keeper) ProcessDissociateBond(ctx sdk.Context, msg types.MsgDissociateBo
// ProcessDissociateRecords dissociates all records associated with a given bond.
func (k Keeper) ProcessDissociateRecords(ctx sdk.Context, msg types.MsgDissociateRecords) error {
if !k.bondKeeper.HasBond(ctx, msg.BondId) {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "Bond not found.")
return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "Bond not found.")
}

// Only the bond owner can dissociate all records from the bond.
bond := k.bondKeeper.GetBond(ctx, msg.BondId)
if msg.Signer != bond.Owner {
return sdkerrors.Wrap(sdkerrors.ErrUnauthorized, "Bond owner mismatch.")
return errorsmod.Wrap(sdkerrors.ErrUnauthorized, "Bond owner mismatch.")
}

// Dissociate all records from the bond.
Expand All @@ -269,22 +270,22 @@ func (k Keeper) ProcessDissociateRecords(ctx sdk.Context, msg types.MsgDissociat
// ProcessReAssociateRecords switches records from and old to new bond.
func (k Keeper) ProcessReAssociateRecords(ctx sdk.Context, msg types.MsgReAssociateRecords) error {
if !k.bondKeeper.HasBond(ctx, msg.OldBondId) {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "Old bond not found.")
return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "Old bond not found.")
}

if !k.bondKeeper.HasBond(ctx, msg.NewBondId) {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "New bond not found.")
return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "New bond not found.")
}

// Only the bond owner can re-associate all records.
oldBond := k.bondKeeper.GetBond(ctx, msg.OldBondId)
if msg.Signer != oldBond.Owner {
return sdkerrors.Wrap(sdkerrors.ErrUnauthorized, "Old bond owner mismatch.")
return errorsmod.Wrap(sdkerrors.ErrUnauthorized, "Old bond owner mismatch.")
}

newBond := k.bondKeeper.GetBond(ctx, msg.NewBondId)
if msg.Signer != newBond.Owner {
return sdkerrors.Wrap(sdkerrors.ErrUnauthorized, "New bond owner mismatch.")
return errorsmod.Wrap(sdkerrors.ErrUnauthorized, "New bond owner mismatch.")
}

// Re-associate all records.
Expand Down
Loading

0 comments on commit 2c88328

Please sign in to comment.