Skip to content

Commit

Permalink
Merge pull request #39 from mirrorworld-universe/wsw
Browse files Browse the repository at this point in the history
feat: Append gridBlockFee hash to ensure uniqueness
  • Loading branch information
ZeneDeLuca authored Sep 15, 2024
2 parents 0f5d345 + 58a5507 commit ca9335a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
32 changes: 32 additions & 0 deletions x/hypergridssn/keeper/grid_block_fee.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ func (k Keeper) AppendGridBlockFee(
appendedValue := k.cdc.MustMarshal(&gridBlockFee)
store.Set(GetGridBlockFeeIDBytes(gridBlockFee.Id), appendedValue)

// Save the hash of the gridBlockFee to make blockhash unique.
store2 := prefix.NewStore(storeAdapter, types.KeyPrefix(types.GridBlockhashKey))
hashBytes := []byte(gridBlockFee.Blockhash)
store2.Set(GetGridBlockFeeHashBytes(gridBlockFee.Blockhash), hashBytes)

// Update gridBlockFee count
k.SetGridBlockFeeCount(ctx, count+1)

Expand Down Expand Up @@ -103,10 +108,37 @@ func (k Keeper) GetAllGridBlockFee(ctx context.Context) (list []types.GridBlockF
return
}

// / Check if the key exists in the store
func (k Keeper) HasGridBlockFeeHash(
ctx context.Context,
hash string,

) bool {
storeAdapter := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
store := prefix.NewStore(storeAdapter, types.KeyPrefix(types.GridBlockhashKey))

b := store.Get(GetGridBlockFeeHashBytes(
hash,
))
return b != nil
}

// GetGridBlockFeeIDBytes returns the byte representation of the ID
func GetGridBlockFeeIDBytes(id uint64) []byte {
bz := types.KeyPrefix(types.GridBlockFeeKey)
bz = append(bz, []byte("/")...)
bz = binary.BigEndian.AppendUint64(bz, id)
return bz
}

// GetGridBlockFeeIDBytes returns the byte representation of the ID
func GetGridBlockFeeHashBytes(
hash string,
) []byte {
var key []byte
hashBytes := []byte(hash)
key = append(key, hashBytes...)
key = append(key, []byte("/")...)

return key
}
15 changes: 13 additions & 2 deletions x/hypergridssn/keeper/msg_server_grid_block_fee.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,21 @@ import (

"hypergrid-ssn/x/hypergridssn/types"

errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)

func (k msgServer) CreateGridBlockFee(goCtx context.Context, msg *types.MsgCreateGridBlockFee) (*types.MsgCreateGridBlockFeeResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
var n = len(msg.Items)
if n == 0 {
return nil, errorsmod.Wrap(types.ErrInvalidLengthGridBlockFee, "no GridBlockFeeItem")
} else if n > 10000 {
return nil, errorsmod.Wrap(types.ErrIntOverflowGridBlockFee, "too many GridBlockFeeItem")
}

ids := make([]uint64, len(msg.Items))
ids := make([]uint64, 0, len(msg.Items))
for _, item := range msg.Items {
var gridBlockFee = types.GridBlockFee{
Creator: msg.Creator,
Expand All @@ -22,7 +30,10 @@ func (k msgServer) CreateGridBlockFee(goCtx context.Context, msg *types.MsgCreat
Fee: item.Fee,
}

//todo: make blockhash unique
//make blockhash unique
if k.HasGridBlockFeeHash(ctx, gridBlockFee.Blockhash) {
return nil, errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "blockhash already")
}

id := k.AppendGridBlockFee(
ctx,
Expand Down
1 change: 1 addition & 0 deletions x/hypergridssn/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const (
const (
GridBlockFeeKey = "GridBlockFee/value/"
GridBlockFeeCountKey = "GridBlockFee/count/"
GridBlockhashKey = "GridBlockFee/hash/"
)

const (
Expand Down

0 comments on commit ca9335a

Please sign in to comment.