Skip to content

Commit

Permalink
refactor: supplier module keys
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanchriswhite committed Dec 13, 2023
1 parent ef3cd99 commit f509fbf
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 33 deletions.
4 changes: 2 additions & 2 deletions x/supplier/keeper/claim.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (k Keeper) UpsertClaim(ctx sdk.Context, claim types.Claim) {

// Update the session end height index: sessionEndHeight -> [ClaimPrimaryKey]
sessionHeightStoreIndex := prefix.NewStore(parentStore, types.KeyPrefix(types.ClaimSessionEndHeightPrefix))
sessionEndBlockHeight := uint64(claim.GetSessionHeader().GetSessionEndBlockHeight())
sessionEndBlockHeight := claim.GetSessionHeader().GetSessionEndBlockHeight()
heightKey := types.ClaimSupplierEndSessionHeightKey(sessionEndBlockHeight, primaryKey)
sessionHeightStoreIndex.Set(heightKey, primaryKey)

Expand All @@ -60,7 +60,7 @@ func (k Keeper) RemoveClaim(ctx sdk.Context, sessionId, supplierAddr string) {
sessionHeightStoreIndex := prefix.NewStore(parentStore, types.KeyPrefix(types.ClaimSessionEndHeightPrefix))

addressKey := types.ClaimSupplierAddressKey(claim.SupplierAddress, primaryKey)
sessionEndBlockHeight := uint64(claim.GetSessionHeader().GetSessionEndBlockHeight())
sessionEndBlockHeight := claim.GetSessionHeader().GetSessionEndBlockHeight()
heightKey := types.ClaimSupplierEndSessionHeightKey(sessionEndBlockHeight, primaryKey)

// Delete all the entries (primary store and secondary indices)
Expand Down
37 changes: 8 additions & 29 deletions x/supplier/types/key_claim.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,43 +18,22 @@ const (
ClaimSessionEndHeightPrefix = "Claim/height/"
)

// ClaimPrimaryKey returns the primary store key to retrieve a Claim by creating a composite key of the sessionId and supplierAddr
// ClaimPrimaryKey returns the primary store key used to retrieve a Claim by creating a composite key of the sessionId and supplierAddr.
func ClaimPrimaryKey(sessionId, supplierAddr string) []byte {
var key []byte

// We are guaranteed uniqueness of the primary key if it's a composite of the (sessionId, supplierAddr)
// because every supplier can only have one claim per session.
key = append(key, []byte(sessionId)...)
key = append(key, []byte("/")...)
key = append(key, []byte(supplierAddr)...)
key = append(key, []byte("/")...)

return key
return KeyComposite([]byte(sessionId), []byte(supplierAddr))
}

// ClaimSupplierAddressKey returns the address key to iterate through claims given a supplier Address
// ClaimSupplierAddressKey returns the key used to iterate through claims given a supplier Address.
func ClaimSupplierAddressKey(supplierAddr string, primaryKey []byte) []byte {
var key []byte

key = append(key, []byte(supplierAddr)...)
key = append(key, []byte("/")...)
key = append(key, primaryKey...)
key = append(key, []byte("/")...)

return key
return KeyComposite([]byte(supplierAddr), primaryKey)
}

// ClaimSupplierAddressKey returns the address key to iterate through claims given a supplier Address
func ClaimSupplierEndSessionHeightKey(sessionEndHeight uint64, primaryKey []byte) []byte {
var key []byte

// ClaimSupplierEndSessionHeightKey returns the key used to iterate through claims given a session end height.
func ClaimSupplierEndSessionHeightKey(sessionEndHeight int64, primaryKey []byte) []byte {
heightBz := make([]byte, 8)
binary.BigEndian.PutUint64(heightBz, sessionEndHeight)

key = append(key, []byte(heightBz)...)
key = append(key, []byte("/")...)
key = append(key, primaryKey...)
key = append(key, []byte("/")...)
binary.BigEndian.PutUint64(heightBz, uint64(sessionEndHeight))

return key
return KeyComposite(heightBz, primaryKey)
}
17 changes: 15 additions & 2 deletions x/supplier/types/keys.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package types

import (
"bytes"
)

const (
// ModuleName defines the module name
ModuleName = "supplier"
Expand All @@ -14,6 +18,15 @@ const (
MemStoreKey = "mem_supplier"
)

func KeyPrefix(p string) []byte {
return []byte(p)
// KeyDelimiter is the delimiter for composite keys.
var KeyDelimiter = []byte("/")

// KeyPrefix returns the given prefix as a byte slice for use with the KVStore.
func KeyPrefix(prefix string) []byte {
return []byte(prefix)
}

// KeyComposite combines the given keys into a single key for use with KVStore.
func KeyComposite(keys ...[]byte) []byte {
return bytes.Join(keys, KeyDelimiter)
}

0 comments on commit f509fbf

Please sign in to comment.