Skip to content
This repository was archived by the owner on Jan 13, 2025. It is now read-only.

Commit

Permalink
Check and avoid duplicates at BitCannaID.Bcnaid
Browse files Browse the repository at this point in the history
  • Loading branch information
RaulBernal committed Apr 21, 2023
1 parent 4702c9d commit 1cbd759
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
18 changes: 18 additions & 0 deletions x/bcna/keeper/bitcannaid.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,24 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
)

// Check if the BitCannaID exist previously
func (k Keeper) HasBitcannaidWithBcnaid(ctx sdk.Context, bcnaid string) bool {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, []byte(types.BitcannaidKey))

defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var bitcannaid types.Bitcannaid
k.cdc.MustUnmarshal(iterator.Value(), &bitcannaid)

if bitcannaid.Bcnaid == bcnaid {
return true
}
}

return false
}

// GetBitcannaidCount get the total number of bitcannaid
func (k Keeper) GetBitcannaidCount(ctx sdk.Context) uint64 {
store := prefix.NewStore(ctx.KVStore(k.storeKey), []byte{})
Expand Down
8 changes: 8 additions & 0 deletions x/bcna/keeper/msg_server_bitcannaid.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import (
func (k msgServer) CreateBitcannaid(goCtx context.Context, msg *types.MsgCreateBitcannaid) (*types.MsgCreateBitcannaidResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

// Check if a BitCannaID with the same Bcnaid already exists
if k.HasBitcannaidWithBcnaid(ctx, msg.Bcnaid) {
return nil, sdkerrors.Wrapf(types.ErrDuplicateBitcannaid, "BitCannaID with Bcnaid %s already exists", msg.Bcnaid)
}
var bitcannaid = types.Bitcannaid{
Creator: msg.Creator,
Bcnaid: msg.Bcnaid,
Expand All @@ -31,6 +35,10 @@ func (k msgServer) CreateBitcannaid(goCtx context.Context, msg *types.MsgCreateB
func (k msgServer) UpdateBitcannaid(goCtx context.Context, msg *types.MsgUpdateBitcannaid) (*types.MsgUpdateBitcannaidResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

// Check if a BitCannaID with the same Bcnaid already exists
if k.HasBitcannaidWithBcnaid(ctx, msg.Bcnaid) {
return nil, sdkerrors.Wrapf(types.ErrDuplicateBitcannaid, "BitCannaID with Bcnaid %s already exists", msg.Bcnaid)
}
var bitcannaid = types.Bitcannaid{
Creator: msg.Creator,
Id: msg.Id,
Expand Down
2 changes: 1 addition & 1 deletion x/bcna/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ import (

// x/bcna module sentinel errors
var (
ErrSample = sdkerrors.Register(ModuleName, 1100, "sample error")
ErrDuplicateBitcannaid = sdkerrors.Register(ModuleName, 1101, "BitCannaID already exists")
)

0 comments on commit 1cbd759

Please sign in to comment.