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: proof store indices (2) #327

Merged
merged 12 commits into from
Jan 19, 2024
11 changes: 8 additions & 3 deletions proto/pocket/supplier/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,9 @@ service Query {
option (google.api.http).get = "/pocket/supplier/claims";
}

// TODO_UPNEXT(@Olshansk): Update these endpoints after implementing proof persistence
// Queries a list of Proof items.
rpc Proof (QueryGetProofRequest) returns (QueryGetProofResponse) {
option (google.api.http).get = "/pocket/supplier/proof/{index}";
option (google.api.http).get = "/pocket/supplier/proof/{session_id}/{supplier_address}";
}
rpc AllProofs (QueryAllProofsRequest) returns (QueryAllProofsResponse) {
option (google.api.http).get = "/pocket/supplier/proof";
Expand Down Expand Up @@ -99,7 +98,8 @@ message QueryAllClaimsResponse {
}

message QueryGetProofRequest {
string index = 1;
string session_id = 1;
string supplier_address = 2 [(cosmos_proto.scalar) = "cosmos.AddressString"];
}

message QueryGetProofResponse {
Expand All @@ -108,6 +108,11 @@ message QueryGetProofResponse {

message QueryAllProofsRequest {
cosmos.base.query.v1beta1.PageRequest pagination = 1;
oneof filter {
string supplier_address = 2;
string session_id = 3;
uint64 session_end_height = 4;
}
}

message QueryAllProofsResponse {
Expand Down
7 changes: 7 additions & 0 deletions x/supplier/client/cli/flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package cli

const (
FlagSessionEndHeight = "session-end-height"
FlagSessionId = "session-id"
FlagSupplierAddress = "supplier-address"
)
6 changes: 0 additions & 6 deletions x/supplier/client/cli/query_claim.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@ import (
// Prevent strconv unused error
var _ = strconv.IntSize

const (
FlagSessionEndHeight = "session-end-height"
FlagSessionId = "session-id"
FlagSupplierAddress = "supplier-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")
Expand Down
124 changes: 105 additions & 19 deletions x/supplier/client/cli/query_proof.go
Original file line number Diff line number Diff line change
@@ -1,35 +1,59 @@
package cli

import (
"fmt"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/spf13/cobra"

"github.com/pokt-network/poktroll/x/supplier/types"
)

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

func CmdListProof() *cobra.Command {
cmd := &cobra.Command{
Use: "list-proofs",
Short: "list all proofs",
Long: `List all the proofs that the node being queried has in its state.

The proofs can be optionally filtered by one of --session-end-height --session-id or --supplier-address flags

Example:
$ poktrolld q proof list-proofs --node $(POCKET_NODE) --home=$(POKTROLLD_HOME)
$ poktrolld q proof list-proofs --session-id <session_id> --node $(POCKET_NODE) --home=$(POKTROLLD_HOME)
$ poktrolld q proof list-proofs --session-end-height <session_end_height> --node $(POCKET_NODE) --home=$(POKTROLLD_HOME)
$ poktrolld q proof list-proofs --supplier-address <supplier_address> --node $(POCKET_NODE) --home=$(POKTROLLD_HOME)`,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
pageReq, err := client.ReadPageRequest(cmd.Flags())
if err != nil {
return err
}

pageReq, err := client.ReadPageRequest(cmd.Flags())
if err != nil {
req := &types.QueryAllProofsRequest{
Pagination: pageReq,
}
if err := updateProofsFilter(cmd, req); err != nil {
return err
}
if err := req.ValidateBasic(); err != nil {
return err
}

queryClient := types.NewQueryClient(clientCtx)

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

res, err := queryClient.AllProofs(cmd.Context(), params)
res, err := queryClient.AllProofs(cmd.Context(), req)
if err != nil {
return err
}
Expand All @@ -38,33 +62,44 @@ func CmdListProof() *cobra.Command {
},
}

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

return cmd
}

// TODO_UPNEXT(@Olshansk): Remove the dependency on index which was part of the default scaffolding behaviour
func CmdShowProof() *cobra.Command {
cmd := &cobra.Command{
Use: "show-proof <index>",
Short: "shows a proof",
Args: cobra.ExactArgs(1),
Use: "show-proof <session_id> <supplier_addr>",
Short: "shows a specific proof",
Long: `List a specific proof that the node being queried has access to.

A unique proof can be defined via a session_id that a given supplier participated in.

Example:
$ poktrolld --home=$(POKTROLLD_HOME) q proof show-proofs <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]

getProofRequest := &types.QueryGetProofRequest{
SessionId: sessionId,
SupplierAddress: supplierAddr,
}
if err := getProofRequest.ValidateBasic(); err != nil {
return err
}

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

queryClient := types.NewQueryClient(clientCtx)

argIndex := args[0]

params := &types.QueryGetProofRequest{
Index: argIndex,
}

res, err := queryClient.Proof(cmd.Context(), params)
res, err := queryClient.Proof(cmd.Context(), getProofRequest)
if err != nil {
return err
}
Expand All @@ -77,3 +112,54 @@ func CmdShowProof() *cobra.Command {

return cmd
}

// updateProofsFilter updates the proofs filter request based on the flags set provided
func updateProofsFilter(cmd *cobra.Command, req *types.QueryAllProofsRequest) 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.QueryAllProofsRequest_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.QueryAllProofsRequest_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.QueryAllProofsRequest_SessionEndHeight{
SessionEndHeight: sessionEndHeight,
}
return nil
}

return nil
}
20 changes: 14 additions & 6 deletions x/supplier/client/cli/tx_submit_proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package cli

import (
"encoding/base64"
"encoding/json"
"strconv"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/codec"
cdctypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/spf13/cobra"

sessiontypes "github.com/pokt-network/poktroll/x/session/types"
Expand All @@ -24,12 +25,19 @@ func CmdSubmitProof() *cobra.Command {
Short: "Broadcast message submit-proof",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) (err error) {
argSessionHeader := new(sessiontypes.SessionHeader)
err = json.Unmarshal([]byte(args[0]), argSessionHeader)
sessionHeaderEncodedStr := args[0]
smstProofEncodedStr := args[1]

// Get the session header
sessionHeaderBz, err := base64.StdEncoding.DecodeString(sessionHeaderEncodedStr)
if err != nil {
return err
}
argSmstProof, err := base64.StdEncoding.DecodeString(args[1])
sessionHeader := &sessiontypes.SessionHeader{}
cdc := codec.NewProtoCodec(cdctypes.NewInterfaceRegistry())
cdc.MustUnmarshalJSON(sessionHeaderBz, sessionHeader)

smstProof, err := base64.StdEncoding.DecodeString(smstProofEncodedStr)
if err != nil {
return err
}
Expand All @@ -41,8 +49,8 @@ func CmdSubmitProof() *cobra.Command {

msg := types.NewMsgSubmitProof(
clientCtx.GetFromAddress().String(),
argSessionHeader,
argSmstProof,
sessionHeader,
smstProof,
)
if err := msg.ValidateBasic(); err != nil {
return err
Expand Down
49 changes: 9 additions & 40 deletions x/supplier/keeper/msg_server_create_claim.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

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

sessiontypes "github.com/pokt-network/poktroll/x/session/types"
suppliertypes "github.com/pokt-network/poktroll/x/supplier/types"
)

Expand All @@ -20,51 +19,18 @@ func (k msgServer) CreateClaim(goCtx context.Context, msg *suppliertypes.MsgCrea
return nil, err
}

sessionReq := &sessiontypes.QueryGetSessionRequest{
ApplicationAddress: msg.GetSessionHeader().GetApplicationAddress(),
Service: msg.GetSessionHeader().GetService(),
BlockHeight: msg.GetSessionHeader().GetSessionStartBlockHeight(),
}
sessionRes, err := k.Keeper.sessionKeeper.GetSession(goCtx, sessionReq)
session, err := k.queryAndValidateSessionHeader(
goCtx,
msg.GetSessionHeader(),
msg.GetSupplierAddress(),
)
if err != nil {
return nil, err
}

logger.
With(
"session_id", sessionRes.GetSession().GetSessionId(),
"session_end_height", msg.GetSessionHeader().GetSessionEndBlockHeight(),
"supplier", msg.GetSupplierAddress(),
).
Debug("got sessionId for claim")

if sessionRes.Session.SessionId != msg.SessionHeader.SessionId {
return nil, suppliertypes.ErrSupplierInvalidSessionId.Wrapf(
"claimed sessionRes ID does not match on-chain sessionRes ID; expected %q, got %q",
sessionRes.Session.SessionId,
msg.SessionHeader.SessionId,
)
}

var found bool
for _, supplier := range sessionRes.GetSession().GetSuppliers() {
if supplier.Address == msg.GetSupplierAddress() {
found = true
break
}
}

if !found {
return nil, suppliertypes.ErrSupplierNotFound.Wrapf(
"supplier address %q in session ID %q",
msg.GetSupplierAddress(),
sessionRes.GetSession().GetSessionId(),
)
}

logger.
With(
"session_id", sessionRes.GetSession().GetSessionId(),
"session_id", session.GetSessionId(),
"session_end_height", msg.GetSessionHeader().GetSessionEndBlockHeight(),
"supplier", msg.GetSupplierAddress(),
).
Expand All @@ -88,6 +54,9 @@ func (k msgServer) CreateClaim(goCtx context.Context, msg *suppliertypes.MsgCrea
SessionHeader: msg.GetSessionHeader(),
RootHash: msg.RootHash,
}

// TODO_BLOCKER: check if this claim already exists and return an appropriate error
// in any case where the supplier should no longer be able to update the given proof.
k.Keeper.UpsertClaim(ctx, claim)

logger.
Expand Down
Loading
Loading