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: claim & proof protobufs + #263

Merged
merged 17 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
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
37 changes: 1 addition & 36 deletions pkg/relayer/session/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,11 @@ import (
"github.com/pokt-network/poktroll/pkg/observable/channel"
"github.com/pokt-network/poktroll/pkg/polylog/polyzero"
"github.com/pokt-network/poktroll/pkg/relayer"
"github.com/pokt-network/poktroll/pkg/relayer/miner"
"github.com/pokt-network/poktroll/pkg/relayer/session"
"github.com/pokt-network/poktroll/testutil/testclient/testblock"
"github.com/pokt-network/poktroll/testutil/testclient/testsupplier"
"github.com/pokt-network/poktroll/testutil/testpolylog"
"github.com/pokt-network/poktroll/testutil/testrelayer"
servicetypes "github.com/pokt-network/poktroll/x/service/types"
sessiontypes "github.com/pokt-network/poktroll/x/session/types"
)

func TestRelayerSessionsManager_Start(t *testing.T) {
Expand Down Expand Up @@ -54,7 +51,7 @@ func TestRelayerSessionsManager_Start(t *testing.T) {
relayerSessionsManager.Start(ctx)

// Publish a mined relay to the minedRelaysPublishCh to insert into the session tree.
minedRelay := newMinedRelay(t, sessionStartHeight, sessionEndHeight)
minedRelay := testrelayer.NewMinedRelay(t, sessionStartHeight, sessionEndHeight)
minedRelaysPublishCh <- minedRelay

// Wait a tick to allow the relayer sessions manager to process asynchronously.
Expand Down Expand Up @@ -82,35 +79,3 @@ func TestRelayerSessionsManager_Start(t *testing.T) {
// Wait a tick to allow the relayer sessions manager to process asynchronously.
time.Sleep(250 * time.Millisecond)
}

// newMinedRelay returns a new mined relay with the given session start and end
// heights on the session header, and the bytes and hash fields populated.
func newMinedRelay(
t *testing.T,
sessionStartHeight int64,
sessionEndHeight int64,
) *relayer.MinedRelay {
relay := servicetypes.Relay{
Req: &servicetypes.RelayRequest{
Meta: &servicetypes.RelayRequestMetadata{
SessionHeader: &sessiontypes.SessionHeader{
SessionStartBlockHeight: sessionStartHeight,
SessionEndBlockHeight: sessionEndHeight,
},
},
},
Res: &servicetypes.RelayResponse{},
}

// TODO_BLOCKER: use canonical codec to serialize the relay
relayBz, err := relay.Marshal()
require.NoError(t, err)

relayHash := testrelayer.HashBytes(t, miner.DefaultRelayHasher, relayBz)

return &relayer.MinedRelay{
Relay: relay,
Bytes: relayBz,
Hash: relayHash,
}
}
8 changes: 5 additions & 3 deletions proto/pocket/supplier/claim.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ package pocket.supplier;
option go_package = "github.com/pokt-network/poktroll/x/supplier/types";

import "cosmos_proto/cosmos.proto";
import "pocket/session/session.proto";

// Claim is the serialized object stored on-chain for claims pending to be proven
message Claim {
string supplier_address = 1 [(cosmos_proto.scalar) = "cosmos.AddressString"]; // the address of the supplier that submitted this claim
string session_id = 2; // session id from the SessionHeader
uint64 session_end_block_height = 3; // session end block height from the SessionHeader
bytes root_hash = 4; // smt.SMST#Root()
// The session header of the session that this claim is for.
session.SessionHeader session_header = 2;
// Root hash returned from smt.SMST#Root().
bytes root_hash = 3;
}
12 changes: 6 additions & 6 deletions proto/pocket/supplier/proof.proto
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
syntax = "proto3";
package pocket.supplier;

import "cosmos_proto/cosmos.proto";
import "pocket/session/session.proto";

option go_package = "github.com/pokt-network/poktroll/x/supplier/types";

// TODO_UPNEXT(@Olshansk): The structure below is the default (untouched) scaffolded type. Update
// and productionize it for our use case.
message Proof {
string index = 1;
string supplier_address = 2;
string session_id = 3;
string merkle_proof = 4;
string supplier_address = 1 [(cosmos_proto.scalar) = "cosmos.AddressString"];
bryanchriswhite marked this conversation as resolved.
Show resolved Hide resolved
session.SessionHeader session_header = 2; // the session header of the session that this claim is for
bytes merkle_proof = 3; // the serialized SMT proof
bryanchriswhite marked this conversation as resolved.
Show resolved Hide resolved
}

5 changes: 3 additions & 2 deletions proto/pocket/supplier/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ syntax = "proto3";

package pocket.supplier;

import "cosmos_proto/cosmos.proto";
import "cosmos/base/query/v1beta1/pagination.proto";
import "gogoproto/gogo.proto";
import "google/api/annotations.proto";
import "cosmos/base/query/v1beta1/pagination.proto";
import "pocket/supplier/params.proto";
import "pocket/shared/supplier.proto";
import "pocket/supplier/claim.proto";
Expand Down Expand Up @@ -76,7 +77,7 @@ message QueryAllSupplierResponse {

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

message QueryGetClaimResponse {
Expand Down
44 changes: 44 additions & 0 deletions testutil/testrelayer/relays.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package testrelayer

import (
"testing"

"github.com/stretchr/testify/require"

"github.com/pokt-network/poktroll/pkg/relayer"
"github.com/pokt-network/poktroll/pkg/relayer/miner"
servicetypes "github.com/pokt-network/poktroll/x/service/types"
sessiontypes "github.com/pokt-network/poktroll/x/session/types"
)

// NewMinedRelay returns a new mined relay with the given session start and end
// heights on the session header, and the bytes and hash fields populated.
func NewMinedRelay(
t *testing.T,
sessionStartHeight int64,
sessionEndHeight int64,
) *relayer.MinedRelay {
relay := servicetypes.Relay{
Req: &servicetypes.RelayRequest{
Meta: &servicetypes.RelayRequestMetadata{
SessionHeader: &sessiontypes.SessionHeader{
SessionStartBlockHeight: sessionStartHeight,
SessionEndBlockHeight: sessionEndHeight,
},
},
},
Res: &servicetypes.RelayResponse{},
}

// TODO_BLOCKER: use canonical codec to serialize the relay
relayBz, err := relay.Marshal()
require.NoError(t, err)

relayHash := HashBytes(t, miner.DefaultRelayHasher, relayBz)

return &relayer.MinedRelay{
Relay: relay,
Bytes: relayBz,
Hash: relayHash,
}
}
13 changes: 9 additions & 4 deletions x/supplier/client/cli/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,15 @@ func createClaim(

// TODO_TECHDEBT: Forward the actual claim in the response once the response is updated to return it.
return &types.Claim{
SupplierAddress: supplierAddr,
SessionId: sessionId,
SessionEndBlockHeight: uint64(sessionEndHeight),
RootHash: rootHash,
SupplierAddress: supplierAddr,
SessionHeader: &sessiontypes.SessionHeader{
ApplicationAddress: appAddress,
Service: &sharedtypes.Service{Id: testServiceId},
SessionId: sessionId,
SessionStartBlockHeight: sessionStartHeight,
SessionEndBlockHeight: sessionEndHeight,
},
RootHash: rootHash,
}
}

Expand Down
6 changes: 3 additions & 3 deletions x/supplier/client/cli/query_claim.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,14 @@ func CmdShowClaim() *cobra.Command {
cmd := &cobra.Command{
Use: "show-claim <session_id> <supplier_addr>",
Short: "shows a specific claim",
Long: `List a specific claim that the node being queried has access to (if it still exists)
Long: `List a specific claim that the node being queried has access to (if it still exists).
bryanchriswhite marked this conversation as resolved.
Show resolved Hide resolved

A unique claim can be defined via a session_id that a supplier participated in
A unique claim can be defined via a session_id that a given supplier participated in.
bryanchriswhite marked this conversation as resolved.
Show resolved Hide resolved

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) {
RunE: func(cmd *cobra.Command, args []string) error {
sessionId := args[0]
supplierAddr := args[1]

Expand Down
34 changes: 17 additions & 17 deletions x/supplier/client/cli/query_claim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,29 +36,29 @@ func TestClaim_Show(t *testing.T) {
sessionId string
supplierAddr string

args []string
err error
obj types.Claim
args []string
err error
claim types.Claim
}{
{
desc: "claim found",
sessionId: claims[0].SessionId,
supplierAddr: claims[0].SupplierAddress,
sessionId: claims[0].GetSessionHeader().GetSessionId(),
supplierAddr: claims[0].GetSupplierAddress(),

args: common,
obj: claims[0],
args: common,
claim: claims[0],
},
{
desc: "claim not found (wrong session ID)",
sessionId: "wrong_session_id",
supplierAddr: claims[0].SupplierAddress,
supplierAddr: claims[0].GetSupplierAddress(),

args: common,
err: status.Error(codes.NotFound, "not found"),
},
{
desc: "claim not found (wrong supplier address)",
sessionId: claims[0].SessionId,
sessionId: claims[0].GetSessionHeader().GetSessionId(),
supplierAddr: "wrong_supplier_address",

args: common,
Expand All @@ -82,10 +82,10 @@ func TestClaim_Show(t *testing.T) {
var resp types.QueryGetClaimResponse
require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp))
require.NotNil(t, resp.Claim)
require.Equal(t,
nullify.Fill(&tc.obj),
nullify.Fill(&resp.Claim),
)

require.Equal(t, tc.claim.GetSupplierAddress(), resp.Claim.GetSupplierAddress())
require.Equal(t, tc.claim.GetRootHash(), resp.Claim.GetRootHash())
require.Equal(t, tc.claim.GetSessionHeader(), resp.Claim.GetSessionHeader())
}
})
}
Expand Down Expand Up @@ -187,13 +187,13 @@ func TestClaim_List(t *testing.T) {
})

t.Run("BySession", func(t *testing.T) {
sessionId := claims[0].SessionId
sessionId := claims[0].GetSessionHeader().SessionId
args := prepareArgs(nil, 0, uint64(totalClaims), true)
args = append(args, fmt.Sprintf("--%s=%s", cli.FlagSessionId, sessionId))

expectedClaims := make([]types.Claim, 0)
for _, claim := range claims {
if claim.SessionId == sessionId {
if claim.GetSessionHeader().SessionId == sessionId {
expectedClaims = append(expectedClaims, claim)
}
}
Expand All @@ -212,13 +212,13 @@ func TestClaim_List(t *testing.T) {
})

t.Run("ByHeight", func(t *testing.T) {
sessionEndHeight := claims[0].SessionEndBlockHeight
sessionEndHeight := claims[0].GetSessionHeader().GetSessionEndBlockHeight()
args := prepareArgs(nil, 0, uint64(totalClaims), true)
args = append(args, fmt.Sprintf("--%s=%d", cli.FlagSessionEndHeight, sessionEndHeight))

expectedClaims := make([]types.Claim, 0)
for _, claim := range claims {
if claim.SessionEndBlockHeight == sessionEndHeight {
if claim.GetSessionHeader().GetSessionEndBlockHeight() == sessionEndHeight {
expectedClaims = append(expectedClaims, claim)
}
}
Expand Down
19 changes: 11 additions & 8 deletions x/supplier/keeper/claim.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,20 @@ import (
"github.com/pokt-network/poktroll/x/supplier/types"
)

// InsertClaim adds a claim to the store
func (k Keeper) InsertClaim(ctx sdk.Context, claim types.Claim) {
logger := k.Logger(ctx).With("method", "InsertClaim")
// UpsertClaim inserts or updates a specific claim in the store by index.
func (k Keeper) UpsertClaim(ctx sdk.Context, claim types.Claim) {
bryanchriswhite marked this conversation as resolved.
Show resolved Hide resolved
logger := k.Logger(ctx).With("method", "UpsertClaim")

claimBz := k.cdc.MustMarshal(&claim)
parentStore := ctx.KVStore(k.storeKey)

// Update the primary store: ClaimPrimaryKey -> ClaimObject
primaryStore := prefix.NewStore(parentStore, types.KeyPrefix(types.ClaimPrimaryKeyPrefix))
primaryKey := types.ClaimPrimaryKey(claim.SessionId, claim.SupplierAddress)
sessionId := claim.GetSessionHeader().GetSessionId()
primaryKey := types.ClaimPrimaryKey(sessionId, claim.SupplierAddress)
primaryStore.Set(primaryKey, claimBz)

logger.Info("inserted claim for supplier %s with primaryKey %s", claim.SupplierAddress, primaryKey)
logger.Info("upserted claim for supplier %s with primaryKey %s", claim.SupplierAddress, primaryKey)
bryanchriswhite marked this conversation as resolved.
Show resolved Hide resolved

// Update the address index: supplierAddress -> [ClaimPrimaryKey]
addressStoreIndex := prefix.NewStore(parentStore, types.KeyPrefix(types.ClaimSupplierAddressPrefix))
Expand All @@ -32,10 +33,11 @@ func (k Keeper) InsertClaim(ctx sdk.Context, claim types.Claim) {

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

logger.Info("indexed claim for supplier %s at session ending height %d", claim.SupplierAddress, claim.SessionEndBlockHeight)
logger.Info("indexed claim for supplier %s at session ending height %d", claim.SupplierAddress, sessionEndBlockHeight)
}

// RemoveClaim removes a claim from the store
Expand All @@ -58,7 +60,8 @@ func (k Keeper) RemoveClaim(ctx sdk.Context, sessionId, supplierAddr string) {
sessionHeightStoreIndex := prefix.NewStore(parentStore, types.KeyPrefix(types.ClaimSessionEndHeightPrefix))

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

// Delete all the entries (primary store and secondary indices)
primaryStore.Delete(primaryKey)
Expand Down
Loading