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] Updating the scaffolded Claim type #151

Merged
merged 55 commits into from
Nov 17, 2023
Merged
Changes from 1 commit
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
9117197
Ran ignite scaffold map claim --module supplier supplier_address sess…
Olshansk Nov 6, 2023
1d31b94
WIP - updating saffolded claim
Olshansk Nov 6, 2023
21b55c6
Merge branch 'main' into issues/140/scaffold_claim
Olshansk Nov 7, 2023
978161a
Fixed inconsistencies due to the new import
Olshansk Nov 7, 2023
1ee4487
make go_develop_and_test compiles again
Olshansk Nov 7, 2023
025419a
Merge branch 'main' into issues/140/update_scaffold_claim
Olshansk Nov 7, 2023
fd21f19
Merge with issues/140/scaffold_claim
Olshansk Nov 7, 2023
4973689
Updates in progress
Olshansk Nov 7, 2023
70ce89f
Ran go_imports
Olshansk Nov 7, 2023
ba81c59
Merge with issues/140/scaffold_claim
Olshansk Nov 7, 2023
1d5dfdc
Fix some linter errors
Olshansk Nov 7, 2023
99631c5
Merge with issues/140/scaffold_claim
Olshansk Nov 7, 2023
f8f11cf
temp
Olshansk Nov 7, 2023
872bb6f
Snapshotting some super rough WIP
Olshansk Nov 7, 2023
b1b514a
Update x/supplier/client/cli/query_claim.go
Olshansk Nov 7, 2023
2562c90
Update x/supplier/client/cli/query_claim.go
Olshansk Nov 7, 2023
369db38
Update x/supplier/client/cli/query_claim.go
Olshansk Nov 7, 2023
3f6e68a
[Miner] feat: add `TxClient` (#94)
bryanchriswhite Nov 7, 2023
3c4401c
[Off-chain] refactor: keyring errors & helpers (#131)
bryanchriswhite Nov 7, 2023
d67edee
[Miner] feat: add supplier client (#42)
bryanchriswhite Nov 7, 2023
13479cf
[Testing, Docs] refactor: move /internal/... to /testutil/... for god…
bryanchriswhite Nov 7, 2023
afa624f
Tend to review comments
Olshansk Nov 7, 2023
fef7af6
Merge branch 'main' into issues/140/scaffold_claim
Olshansk Nov 7, 2023
ca62bb2
Fixing whitespace
Olshansk Nov 7, 2023
15b5fb3
Fixed the last broken test
Olshansk Nov 7, 2023
ec80770
Merge with issues/140/scaffold_claim
Olshansk Nov 7, 2023
f488ec5
Cleaned up the code enough so it compiles w/ a secondary index but st…
Olshansk Nov 8, 2023
55182aa
Merge with main
Olshansk Nov 8, 2023
5c6be4d
WIP - merge with main
Olshansk Nov 8, 2023
9fb9031
Merge branch 'main' into issues/140/update_scaffold_claim
Olshansk Nov 9, 2023
bc33dda
Non-funtional snapshot
Olshansk Nov 9, 2023
489fde9
Merge branch 'main' into issues/140/update_scaffold_claim
Olshansk Nov 9, 2023
746d765
Unit tests are passing but more work ahead
Olshansk Nov 10, 2023
3d5581a
Compiling with unit tests passing
Olshansk Nov 10, 2023
bea6106
Self reviewed all the files except for claims_test
Olshansk Nov 10, 2023
4ee7b24
Merge branch 'main' into issues/140/update_scaffold_claim
Olshansk Nov 10, 2023
58b524b
Working on localnet
Olshansk Nov 10, 2023
ddc8e3d
Merge branch 'main' into issues/140/update_scaffold_claim
Olshansk Nov 13, 2023
5384236
Merge branch 'main' into issues/140/update_scaffold_claim
Olshansk Nov 14, 2023
6f38d15
Working on the create claim tests
Olshansk Nov 15, 2023
ac99066
Updated x/supplier/keeper/claim_test.go
Olshansk Nov 15, 2023
a26e9c8
Fixed TestClaim_QuerySingle
Olshansk Nov 15, 2023
6c5161d
Implemented filtered paginated claim queries
Olshansk Nov 15, 2023
58ab0d2
Left 4 TODO_IN_THIS_PR after some debugging but still hitting issues …
Olshansk Nov 15, 2023
4b3b912
Merge with main
Olshansk Nov 16, 2023
114a5d2
This test passes: go test -v -run TestClaim_Show ./x/supplier/client…
Olshansk Nov 16, 2023
6756c14
TestClaim_List works but need to clean up the tests
Olshansk Nov 16, 2023
d5d3cb5
Checkpoint where tests pass
Olshansk Nov 16, 2023
4ace899
Add TECHDEBT TODO
Olshansk Nov 16, 2023
4f08f7c
Make claim_create_dummy works
Olshansk Nov 16, 2023
0fccf81
Confirmed that make claim list works
Olshansk Nov 16, 2023
cec2da0
Make go_test finally works again
Olshansk Nov 17, 2023
a45148e
Remove TODO_IN_THIS comment
Olshansk Nov 17, 2023
c096808
s/store/primaryStore
Olshansk Nov 17, 2023
d3b0c5a
Added back commented out tests
Olshansk Nov 17, 2023
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
Prev Previous commit
Next Next commit
temp
Olshansk committed Nov 7, 2023
commit f8f11cf039566d30df87be660056a620eda47f00
4 changes: 2 additions & 2 deletions x/supplier/client/cli/query_claim.go
Original file line number Diff line number Diff line change
@@ -46,9 +46,9 @@ func CmdListClaim() *cobra.Command {

func CmdShowClaim() *cobra.Command {
cmd := &cobra.Command{
Use: "show-claim [index]",
Use: "show-claim <session_id> <supplier_addr>",
Short: "shows a claim",
Args: cobra.ExactArgs(1),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) (err error) {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
110 changes: 89 additions & 21 deletions x/supplier/keeper/claim.go
Original file line number Diff line number Diff line change
@@ -7,17 +7,37 @@ import (
"github.com/pokt-network/poktroll/x/supplier/types"
)

// SetClaim set a specific claim in the store from its index
func (k Keeper) SetClaim(ctx sdk.Context, claim types.Claim) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.ClaimKeyPrefix))
b := k.cdc.MustMarshal(&claim)
store.Set(types.ClaimKey(
claim.SessionId,
claim.SupplierAddress,
), b)
// InsertClaim set a specific a claim given a sessionId & supplierAddr
func (k Keeper) InsertClaim(ctx sdk.Context, claim types.Claim) {
claimBz := k.cdc.MustMarshal(&claim)
parentStore := ctx.KVStore(k.storeKey)

// Store the whole claim in the primary key store
primaryStore := prefix.NewStore(parentStore, types.KeyPrefix(types.ClaimPrimaryKeyPrefix))
primaryKey := types.ClaimPrimaryKey(claim)
primaryStore.Set(primaryKey, claimBz)

// Save the claim in the main claim store with primary key
// primaryStore := prefix.NewStore(ctx.KVStore(k.storeKey), []byte("claim"))
// primaryStore.Set(claim.PrimaryKey(), claim.Marshal())

// Store the param
// heightStore := prefix.NewStore(parentStore, types.KeyPrefix(types.ClaimHeightPrefix))
// heightKey := HeightKey(claim.Height) // Serialize height into a byte slice if needed
// heightStore.Set(heightKey, claim.PrimaryKey())

// Index by address
addressStoreIndex := prefix.NewStore(parentStore, types.KeyPrefix(types.ClaimHeightPrefix))
addressKey := types.ClaimSupplierAddressKey(claim.SupplierAddress)
addressStoreIndex.Set(addressKey, primaryKey)

// ClaimHeightPrefix
// ClaimAddressPrefix
// ClaimSessionIdPrefix

}

// GetClaim returns a claim from its index
// GetClaim returns a claim given a sessionId & supplierAddr
func (k Keeper) GetClaim(
ctx sdk.Context,
sessionId, supplierAddr string,
@@ -37,20 +57,21 @@ func (k Keeper) GetClaim(
}

// RemoveClaim removes a claim from the store
func (k Keeper) RemoveClaim(
ctx sdk.Context,
sessionId, supplierAddr string,
// func (k Keeper) RemoveClaim(
// ctx sdk.Context,
// supplierAddr string,
// sessionId,

) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.ClaimKeyPrefix))
store.Delete(types.ClaimKey(
sessionId, supplierAddr,
))
}
// ) {
// store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.ClaimPrimaryKeyPrefix))
// store.Delete(types.ClaimKey(
// sessionId, supplierAddr,
// ))
// }

// GetAllClaim returns all claim
func (k Keeper) GetAllClaim(ctx sdk.Context) (list []types.Claim) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.ClaimKeyPrefix))
// GetAllClaims returns all claim
func (k Keeper) GetAllClaims(ctx sdk.Context) (list []types.Claim) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.ClaimPrimaryKeyPrefix))
iterator := sdk.KVStorePrefixIterator(store, []byte{})

defer iterator.Close()
@@ -63,3 +84,50 @@ func (k Keeper) GetAllClaim(ctx sdk.Context) (list []types.Claim) {

return
}

// When retrieving by height:
// func (k Keeper) GetClaimsByHeight(ctx sdk.Context, height int64) []Claim {
// heightStore := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.ClaimHeightPrefix))
// heightKey := HeightKey(height) // Serialize height into a byte slice if needed

// // Iterate over the height index store using heightKey
// iterator := sdk.KVStorePrefixIterator(heightStore, heightKey)
// defer iterator.Close()

// var claims []Claim
// for ; iterator.Valid(); iterator.Next() {
// primaryKey := iterator.Value()
// claim := GetClaimByPrimaryKey(ctx, claimStoreKey, primaryKey)
// claims = append(claims, claim)
// }

// return claims
// }

// When retrieving by address:
func (k Keeper) GetClaimsByAddress(ctx sdk.Context, address sdk.AccAddress) []Claim {
addressStore := prefix.NewStore(ctx.KVStore(addressIndexStoreKey), types.KeyPrefix(types.ClaimAddressPrefix))
addressKey := AddressKey(address) // Serialize address into a byte slice if needed

// Iterate over the address index store using addressKey
iterator := sdk.KVStorePrefixIterator(addressStore, addressKey)
defer iterator.Close()

var claims []Claim
for ; iterator.Valid(); iterator.Next() {
primaryKey := iterator.Value()
claim := GetClaimByPrimaryKey(ctx, claimStoreKey, primaryKey)
claims = append(claims, claim)
}

return claims
}

// // Helper function to get a claim by primary key:
func GetClaimByPrimaryKey(ctx sdk.Context, claimStoreKey sdk.StoreKey, primaryKey []byte) Claim {
primaryStore := prefix.NewStore(ctx.KVStore(claimStoreKey), []byte("claim"))
byteClaim := primaryStore.Get(primaryKey)
var claim Claim
claim.Unmarshal(byteClaim) // Unmarshal byte slice into Claim object
return claim
}
38 changes: 21 additions & 17 deletions x/supplier/keeper/claim_test.go
Original file line number Diff line number Diff line change
@@ -5,59 +5,63 @@ import (
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"

keepertest "github.com/pokt-network/poktroll/testutil/keeper"
"github.com/pokt-network/poktroll/testutil/nullify"
"github.com/pokt-network/poktroll/testutil/sample"
"github.com/pokt-network/poktroll/x/supplier/keeper"
"github.com/pokt-network/poktroll/x/supplier/types"
"github.com/stretchr/testify/require"
)

// Prevent strconv unused error
var _ = strconv.IntSize

func createNClaim(keeper *keeper.Keeper, ctx sdk.Context, n int) []types.Claim {
items := make([]types.Claim, n)
for i := range items {
items[i].Index = strconv.Itoa(i)
claims := make([]types.Claim, n)
for i := range claims {
claims[i].SupplierAddress = sample.AccAddress()

keeper.SetClaim(ctx, items[i])
keeper.InsertClaim(ctx, claims[i])
}
return items
return claims
}

func TestClaimGet(t *testing.T) {
keeper, ctx := keepertest.SupplierKeeper(t)
items := createNClaim(keeper, ctx, 10)
for _, item := range items {
claims := createNClaim(keeper, ctx, 10)
for _, claim := range claims {
rst, found := keeper.GetClaim(ctx,
item.Index,
claim.Index,
)
require.True(t, found)
require.Equal(t,
nullify.Fill(&item),
nullify.Fill(&claim),
nullify.Fill(&rst),
)
}
}
func TestClaimRemove(t *testing.T) {
keeper, ctx := keepertest.SupplierKeeper(t)
items := createNClaim(keeper, ctx, 10)
for _, item := range items {
claims := createNClaim(keeper, ctx, 10)
for _, claim := range claims {
keeper.RemoveClaim(ctx,
item.Index,
claim.SessionId,
claim.SupplierAddress,
)
_, found := keeper.GetClaim(ctx,
item.Index,
claim.SessionId,
claim.SupplierAddress,
)
require.False(t, found)
}
}

func TestClaimGetAll(t *testing.T) {
keeper, ctx := keepertest.SupplierKeeper(t)
items := createNClaim(keeper, ctx, 10)
claims := createNClaim(keeper, ctx, 10)
require.ElementsMatch(t,
nullify.Fill(items),
nullify.Fill(keeper.GetAllClaim(ctx)),
nullify.Fill(claims),
nullify.Fill(keeper.GetAllClaims(ctx)),
)
}
2 changes: 1 addition & 1 deletion x/supplier/keeper/query_claim.go
Original file line number Diff line number Diff line change
@@ -21,7 +21,7 @@ func (k Keeper) ClaimAll(goCtx context.Context, req *types.QueryAllClaimRequest)
ctx := sdk.UnwrapSDKContext(goCtx)

store := ctx.KVStore(k.storeKey)
claimStore := prefix.NewStore(store, types.KeyPrefix(types.ClaimKeyPrefix))
claimStore := prefix.NewStore(store, types.KeyPrefix(types.ClaimPrimaryKeyPrefix))

pageRes, err := query.Paginate(claimStore, req.Pagination, func(key []byte, value []byte) error {
var claim types.Claim
37 changes: 28 additions & 9 deletions x/supplier/types/key_claim.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,43 @@
package types

import "encoding/binary"
import (
"encoding/binary"
)

var _ binary.ByteOrder

const (
// ClaimKeyPrefix is the prefix to retrieve all Claim
ClaimKeyPrefix = "Claim/value/"
// ClaimPrimaryKeyPrefix is the prefix to retrieve all Claim (the primary store)
ClaimPrimaryKeyPrefix = "Claim/value/"

// ClaimHeightPrefix is the key to retrieve a Claim's Primary Key from the Height index
ClaimHeightPrefix = "Claim/height/"

// ClaimAddressPrefix is the key to retrieve a Claim's Primary Key from the Address index
ClaimAddressPrefix = "Claim/address/"

// ClaimSessionIdPrefix is the key to retrieve a Claim's Primary Key from the SessionId index
ClaimSessionIdPrefix = "Claim/sessionId/"
)

// ClaimKey returns the store key to retrieve a Claim
func ClaimKey(sessionId, supplierAddr string) []byte {
// ClaimPrimaryKey returns the primary store key to retrieve a Claim
func ClaimPrimaryKey(claim Claim) []byte {
var key []byte

sessionBz := []byte(sessionId)
key = append(key, sessionBz...)
// We are guaranteed uniqueness of the primary key if it's a composite of the (sessionId, supplierAddr)
key = append(key, []byte(claim.SessionId)...)
key = append(key, []byte("/")...)
key = append(key, []byte(claim.SupplierAddress)...)
key = append(key, []byte("/")...)

supplierAddrBz := []byte(supplierAddr)
key = append(key, supplierAddrBz...)
return key
}

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

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

return key