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 6 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
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"];
session.SessionHeader session_header = 2; // the session header of the session that this claim is for
bytes merkle_proof = 3; // the serialized SMT proof
}

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).

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.

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) {
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)

// 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 := 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 := claim.GetSessionHeader().GetSessionEndBlockHeight()
heightKey := types.ClaimSupplierEndSessionHeightKey(sessionEndBlockHeight, primaryKey)

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