Skip to content

Commit

Permalink
Compiling with unit tests passing
Browse files Browse the repository at this point in the history
  • Loading branch information
Olshansk committed Nov 10, 2023
1 parent 746d765 commit 3d5581a
Show file tree
Hide file tree
Showing 13 changed files with 228 additions and 38 deletions.
6 changes: 1 addition & 5 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ localnet/*/config/*.json
!localnet/poktrolld/config/client.toml
!localnet/poktrolld/config/config.toml


# Macos
.DS_Store
**/.DS_Store
Expand Down Expand Up @@ -65,8 +64,5 @@ release
# Only keep one go module in our codebase
go.work.sum

# Avoid accidently commiting gomock artifacts
# Avoid accidentally committing gomock artifacts
**/gomock_reflect_*


gomock_reflect
16 changes: 10 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -445,17 +445,21 @@ acc_balance_total_supply: ## Query the total supply of the network
### Claims ###
##############

.PHONY: claim_list
.PHONY: claims_list
claim_list: ## List all the claims
poktrolld --home=$(POCKETD_HOME) q claim list-claims --node $(POCKET_NODE)

.PHONY: claim_list_address
claim_list_address: ## List all the claims for a specific address (make claim_list_address ADDR=pokt...)
poktrolld --home=$(POCKETD_HOME) q claim list-claims $(ADDR) --node $(POCKET_NODE)
.PHONY: claims_list_address
claim_list_address: ## List all the claims for a specific address (specified via ADDR variable)
poktrolld --home=$(POCKETD_HOME) q claim list-claims address $(ADDR) --node $(POCKET_NODE)

.PHONY: claim_list_height
claim_list_height: ## List all the claims for a specific height (make claim_list_height HEIGHT=...)
poktrolld --home=$(POCKETD_HOME) q claim list-claims --height $(HEIGHT) --node $(POCKET_NODE)
claim_list_height: ## List all the claims ending at a specific height (specified via HEIGHT variable)
poktrolld --home=$(POCKETD_HOME) q claim list-claims height $(HEIGHT) --node $(POCKET_NODE)

.PHONY: claim_list_session
claim_list_session: ## List all the claims ending at a specific session (specified via SESSION variable)
poktrolld --home=$(POCKETD_HOME) q claim list-claims session $(SESSION) --node $(POCKET_NODE)

######################
### Ignite Helpers ###
Expand Down
20 changes: 20 additions & 0 deletions docs/static/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47446,6 +47446,10 @@ paths:
session_id:
type: string
title: session id from the SessionHeader
session_end_block_height:
type: string
format: uint64
title: session end block height from the SessionHeader
root_hash:
type: string
format: byte
Expand Down Expand Up @@ -47499,6 +47503,10 @@ paths:
session_id:
type: string
title: session id from the SessionHeader
session_end_block_height:
type: string
format: uint64
title: session end block height from the SessionHeader
root_hash:
type: string
format: byte
Expand Down Expand Up @@ -78036,6 +78044,10 @@ definitions:
session_id:
type: string
title: session id from the SessionHeader
session_end_block_height:
type: string
format: uint64
title: session end block height from the SessionHeader
root_hash:
type: string
format: byte
Expand Down Expand Up @@ -78065,6 +78077,10 @@ definitions:
session_id:
type: string
title: session id from the SessionHeader
session_end_block_height:
type: string
format: uint64
title: session end block height from the SessionHeader
root_hash:
type: string
format: byte
Expand Down Expand Up @@ -78249,6 +78265,10 @@ definitions:
session_id:
type: string
title: session id from the SessionHeader
session_end_block_height:
type: string
format: uint64
title: session end block height from the SessionHeader
root_hash:
type: string
format: byte
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
cosmossdk.io/math v1.0.1
github.com/cometbft/cometbft v0.37.2
github.com/cometbft/cometbft-db v0.8.0
github.com/cosmos/cosmos-proto v1.0.0-beta.2
github.com/cosmos/cosmos-sdk v0.47.3
github.com/cosmos/gogoproto v1.4.10
github.com/cosmos/ibc-go/v7 v7.1.0
Expand All @@ -28,6 +29,7 @@ require (
go.uber.org/multierr v1.11.0
golang.org/x/crypto v0.12.0
golang.org/x/sync v0.3.0
google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1
google.golang.org/grpc v1.56.1
gopkg.in/yaml.v2 v2.4.0
)
Expand Down Expand Up @@ -71,7 +73,6 @@ require (
github.com/containerd/cgroups v1.1.0 // indirect
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
github.com/cosmos/btcutil v1.0.5 // indirect
github.com/cosmos/cosmos-proto v1.0.0-beta.2 // indirect
github.com/cosmos/go-bip39 v1.0.0 // indirect
github.com/cosmos/gogogateway v1.2.0 // indirect
github.com/cosmos/iavl v0.20.0 // indirect
Expand Down Expand Up @@ -267,7 +268,6 @@ require (
gonum.org/v1/gonum v0.11.0 // indirect
google.golang.org/api v0.122.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
3 changes: 2 additions & 1 deletion proto/pocket/supplier/claim.proto
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ import "cosmos_proto/cosmos.proto";
message Claim {
string supplier_address = 1 [(cosmos_proto.scalar) = "cosmos.AddressString"]; // the address of the supplier that submitted this claims
string session_id = 2; // session id from the SessionHeader
bytes root_hash = 3; // smt.SMST#Root()
uint64 session_end_block_height = 3; // session end block height from the SessionHeader
bytes root_hash = 4; // smt.SMST#Root()
}
89 changes: 85 additions & 4 deletions x/supplier/client/cli/query_claim.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cli

import (
"fmt"
"strconv"

"github.com/cosmos/cosmos-sdk/client"
Expand All @@ -13,27 +14,56 @@ import (
// Prevent strconv unused error
var _ = strconv.IntSize

const (
FlagSessionEndHeight = "height"
FlagSessionId = "session"
FlagSupplierAddress = "address"
)

// AddPaginationFlagsToCmd adds common pagination flags to cmd
func AddClaimFilterFlags(cmd *cobra.Command) {
cmd.Flags().Uint64(FlagSessionEndHeight, 0, "claims whose session ends at this height will be returned")
cmd.Flags().String(FlagSessionId, "", "claims matching this session id will be returned")
cmd.Flags().String(FlagSupplierAddress, "", "claims submitted by suppliers matching this address will be returned")
}

func CmdListClaims() *cobra.Command {
cmd := &cobra.Command{
Use: "list-claims",
Short: "list all claims",
Args: cobra.NoArgs,
Long: `List all the claims that the node being queried has in its state.
The claims can be optionally filtered by one of --session --height or --address flags
Example:
$ poktrolld --home=$(POKTROLLD_HOME) q claim list-claims --node $(POCKET_NODE)
$ poktrolld --home=$(POKTROLLD_HOME) q claim list-claims --session <session_id> --node $(POCKET_NODE)
$ poktrolld --home=$(POKTROLLD_HOME) q claim list-claims --height <session_end_height> --node $(POCKET_NODE)
$ poktrolld --home=$(POKTROLLD_HOME) q claim list-claims --address <supplier_address> --node $(POCKET_NODE)`,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
pageReq, err := client.ReadPageRequest(cmd.Flags())
if err != nil {
return err
}
params := &types.QueryAllClaimsRequest{

req := &types.QueryAllClaimsRequest{
Pagination: pageReq,
}
if err := updateClaimsFilter(cmd, req); err != nil {
return err
}
if err := req.ValidateBasic(); err != nil {
return err
}

clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)

res, err := queryClient.AllClaims(cmd.Context(), params)
res, err := queryClient.AllClaims(cmd.Context(), req)
if err != nil {
return err
}
Expand All @@ -42,17 +72,68 @@ func CmdListClaims() *cobra.Command {
},
}

AddClaimFilterFlags(cmd)
flags.AddPaginationFlagsToCmd(cmd, cmd.Use)
flags.AddQueryFlagsToCmd(cmd)

return cmd
}

func updateClaimsFilter(cmd *cobra.Command, req *types.QueryAllClaimsRequest) error {
sessionId, _ := cmd.Flags().GetString(FlagSessionId)
supplierAddr, _ := cmd.Flags().GetString(FlagSupplierAddress)
sessionEndHeight, _ := cmd.Flags().GetUint64(FlagSessionEndHeight)

// Preparing a shared error in case more than one flag was set
err := fmt.Errorf("can only specify one flag filter but got sessionId (%s), supplierAddr (%s) and sessionEngHeight (%d)", sessionId, supplierAddr, sessionEndHeight)

// Use the session id as the filter
if sessionId != "" {
// If the session id is set, then the other flags must not be set
if supplierAddr != "" || sessionEndHeight > 0 {
return err
}
// Set the session id filter
req.Filter.(*types.QueryAllClaimsRequest_SessionId).SessionId = sessionId
return nil
}

// Use the supplier address as the filter
if supplierAddr != "" {
// If the supplier address is set, then the other flags must not be set
if sessionId != "" || sessionEndHeight > 0 {
return err
}
// Set the supplier address filter
req.Filter.(*types.QueryAllClaimsRequest_SupplierAddress).SupplierAddress = supplierAddr
return nil
}

// Use the session end height as the filter
if sessionEndHeight > 0 {
// If the session end height is set, then the other flags must not be set
if sessionId != "" || supplierAddr != "" {
return err
}
// Set the session end height filter
req.Filter.(*types.QueryAllClaimsRequest_SessionEndHeight).SessionEndHeight = sessionEndHeight
return nil
}

return nil
}

func CmdShowClaim() *cobra.Command {
cmd := &cobra.Command{
Use: "show-claim <session_id> <supplier_addr>",
Short: "shows a specific claim",
Args: cobra.ExactArgs(2),
Long: `List a specific claim that the node being queried has access to (if it still exists)
A unique claim can be defined via a session_id that a supplier participated in
Example:
$ poktrolld --home=$(POKTROLLD_HOME) q claim show-claims <session_id> <supplier_address> --node $(POCKET_NODE)`,
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) (err error) {
sessionId := args[0]
supplierAddr := args[1]
Expand Down
3 changes: 2 additions & 1 deletion x/supplier/client/cli/tx_create_claim.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ func CmdCreateClaim() *cobra.Command {
return err
}

supplierAddress := clientCtx.GetFromAddress().String()
msg := types.NewMsgCreateClaim(
clientCtx.GetFromAddress().String(),
supplierAddress,
argSessionHeader,
argRootHash,
)
Expand Down
52 changes: 43 additions & 9 deletions x/supplier/keeper/claim.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package keeper

import (
"encoding/binary"

"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"

Expand All @@ -22,29 +24,40 @@ func (k Keeper) InsertClaim(ctx sdk.Context, claim types.Claim) {
logger.Info("inserted claim with primaryKey %s", primaryKey)

// Update the address index: supplierAddress -> [ClaimPrimaryKey]
addressStoreIndex := prefix.NewStore(parentStore, types.KeyPrefix(types.ClaimAddressPrefix))
addressStoreIndex := prefix.NewStore(parentStore, types.KeyPrefix(types.ClaimSupplierAddressPrefix))
addressKey := types.ClaimSupplierAddressKey(claim.SupplierAddress, primaryKey)
addressStoreIndex.Set(addressKey, primaryKey)

// TODO: Index by sessionId
// TODO: Index by sessionEndHeight
// Update the session end height index: sessionEndHeight -> [ClaimPrimaryKey]
sessionHeightStoreIndex := prefix.NewStore(parentStore, types.KeyPrefix(types.ClaimSessionEndHeightPrefix))
heightKey := types.ClaimSupplierEndSessionHeightKey(claim.SessionEndBlockHeight, primaryKey)
sessionHeightStoreIndex.Set(heightKey, primaryKey)
}

// RemoveClaim removes a claim from the store
func (k Keeper) RemoveClaim(ctx sdk.Context, sessionId, supplierAddr string) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.ClaimPrimaryKeyPrefix))
parentStore := ctx.KVStore(k.storeKey)
store := prefix.NewStore(parentStore, types.KeyPrefix(types.ClaimPrimaryKeyPrefix))

// Check if the claim exists
primaryKey := types.ClaimPrimaryKey(sessionId, supplierAddr)
claim, foundClaim := k.getClaimByPrimaryKey(ctx, primaryKey)
if !foundClaim {
k.Logger(ctx).Error("trying to delete non-existent claim with primary key %s for supplier %s and session %s", primaryKey, supplierAddr, sessionId)
}

addressStore := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.ClaimAddressPrefix))
// Prepare the indices for deletion
addressStoreIndex := prefix.NewStore(parentStore, types.KeyPrefix(types.ClaimSupplierAddressPrefix))
sessionHeightStoreIndex := prefix.NewStore(parentStore, types.KeyPrefix(types.ClaimSessionEndHeightPrefix))

addressKey := types.ClaimSupplierAddressKey(claim.SupplierAddress, primaryKey)
heightKey := types.ClaimSupplierEndSessionHeightKey(claim.SessionEndBlockHeight, primaryKey)

addressStore.Delete(addressKey)
// Delete all the entries
store.Delete(primaryKey)
addressStoreIndex.Delete(addressKey)
sessionHeightStoreIndex.Delete(heightKey)

}

// GetClaim returns a Claim given a SessionId & SupplierAddr
Expand All @@ -68,11 +81,32 @@ func (k Keeper) GetAllClaims(ctx sdk.Context) (claims []types.Claim) {
return
}

// GetClaimsByAddress returns all claims for a given address
// GetClaimsByAddress returns all claims for a given supplier address
func (k Keeper) GetClaimsByAddress(ctx sdk.Context, address sdk.AccAddress) (claims []types.Claim) {
addressStore := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.ClaimAddressPrefix))
addressStoreIndex := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.ClaimSupplierAddressPrefix))

iterator := sdk.KVStorePrefixIterator(addressStoreIndex, []byte(address))
defer iterator.Close()

for ; iterator.Valid(); iterator.Next() {
primaryKey := iterator.Value()
claim, claimFound := k.getClaimByPrimaryKey(ctx, primaryKey)
if claimFound {
claims = append(claims, claim)
}
}

return claims
}

// GetClaimsByAddress returns all claims whose session ended at the given block height
func (k Keeper) GetClaimsByHeight(ctx sdk.Context, height uint64) (claims []types.Claim) {
sessionHeightStoreIndex := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.ClaimSessionEndHeightPrefix))

heightBz := make([]byte, 8)
binary.BigEndian.PutUint64(heightBz, height)

iterator := sdk.KVStorePrefixIterator(addressStore, []byte(address))
iterator := sdk.KVStorePrefixIterator(sessionHeightStoreIndex, heightBz)
defer iterator.Close()

for ; iterator.Valid(); iterator.Next() {
Expand Down
8 changes: 8 additions & 0 deletions x/supplier/keeper/msg_server_create_claim.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ func (k msgServer) CreateClaim(goCtx context.Context, msg *types.MsgCreateClaim)
return nil, err
}

claim := types.Claim{
SupplierAddress: msg.SupplierAddress,
SessionId: msg.SessionHeader.SessionId,
SessionEndBlockHeight: uint64(msg.SessionHeader.SessionEndBlockHeight),
RootHash: msg.RootHash,
}
k.Keeper.InsertClaim(ctx, claim)

/*
INCOMPLETE: Handling the message
Expand Down
Loading

0 comments on commit 3d5581a

Please sign in to comment.