Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Supplier] refactor: supplier module keys #264

Merged
merged 26 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
4bcd8de
refactor: `NewMinedRelay` to shared testutil
bryanchriswhite Dec 12, 2023
673911c
refactor: claim & proof protobuf types
bryanchriswhite Dec 12, 2023
fd55896
refactor: rename supplier keeper `UpsertClaim` & `UpsertProof`
bryanchriswhite Dec 7, 2023
43cbf0e
refactor: misc. claim-side improvements
bryanchriswhite Dec 12, 2023
ef3cd99
chore: add TODOs
bryanchriswhite Dec 13, 2023
f509fbf
refactor: supplier module keys
bryanchriswhite Dec 12, 2023
6e60952
chore: review feedback improvements
bryanchriswhite Dec 22, 2023
edc4e41
chore: review feedback improvements
bryanchriswhite Dec 22, 2023
7994ca2
Merge remote-tracking branch 'pokt/main' into issues/141/refactor/cla…
bryanchriswhite Dec 22, 2023
6a5b3e6
Merge branch 'issues/141/refactor/claim-proof' into issues/141/refact…
bryanchriswhite Dec 22, 2023
b11bbe3
chore: add TODOs
bryanchriswhite Jan 8, 2024
f3e7866
Merge remote-tracking branch 'pokt/main' into issues/141/refactor/cla…
bryanchriswhite Jan 8, 2024
df9e3c2
Merge branch 'issues/141/refactor/claim-proof' into issues/141/refact…
bryanchriswhite Jan 8, 2024
bca059c
trigger CI
bryanchriswhite Jan 8, 2024
3de1971
chore: add TODO
bryanchriswhite Jan 9, 2024
800b53b
Merge remote-tracking branch 'pokt/main' into issues/141/refactor/cla…
bryanchriswhite Jan 9, 2024
8e0f4e9
Merge branch 'issues/141/refactor/claim-proof' into issues/141/refact…
bryanchriswhite Jan 9, 2024
5905d20
Merge branch 'main' into issues/141/refactor/claim-proof
bryanchriswhite Jan 10, 2024
4248b0b
chore: review feedback improvements
bryanchriswhite Jan 10, 2024
f064432
Merge branch 'issues/141/refactor/claim-proof' into issues/141/refact…
bryanchriswhite Jan 10, 2024
6d5826f
fix: usage raw string literal
bryanchriswhite Jan 10, 2024
92970b9
Merge branch 'issues/141/refactor/claim-proof' into issues/141/refact…
bryanchriswhite Jan 10, 2024
53faaac
Merge branch 'main' into issues/141/refactor/claim-proof
bryanchriswhite Jan 10, 2024
0ada011
Merge branch 'issues/141/refactor/claim-proof' into issues/141/refact…
bryanchriswhite Jan 10, 2024
c648d4f
Merge remote-tracking branch 'pokt/main' into issues/141/refactor/cla…
bryanchriswhite Jan 10, 2024
dd53efc
Merge branch 'issues/141/refactor/claim-proof' into issues/141/refact…
bryanchriswhite Jan 10, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions x/supplier/keeper/claim.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,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 @@ -61,7 +61,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 {
Olshansk marked this conversation as resolved.
Show resolved Hide resolved
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)
}