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

[Testing, Tooling] chore: in-memory network interface & config types #289

Merged
merged 60 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
60 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
6817364
refactor: supplier module errors
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
dfb067d
chore: review feedback improvements
bryanchriswhite Dec 22, 2023
0351cc7
Merge branch 'issues/141/refactor/supplier-keys' into issues/141/refa…
bryanchriswhite Dec 22, 2023
b82e49a
fix: pre-generated keyring accounts
bryanchriswhite Dec 19, 2023
2d52838
fix: session hydrator
bryanchriswhite Dec 21, 2023
6a9d6ec
feat: add InMemoryCosmosNetwork interface
bryanchriswhite Dec 21, 2023
5566e5c
chore: add InMemoryNetworkConfig
bryanchriswhite Dec 21, 2023
923907b
chore: add TODO comment
bryanchriswhite Dec 21, 2023
8b57b22
Revert "fix: pre-generated keyring accounts"
bryanchriswhite Dec 21, 2023
93125b0
chore: add godoc comments & simplify
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
8ab13f0
Merge branch 'issues/141/refactor/supplier-keys' into issues/141/refa…
bryanchriswhite Jan 8, 2024
c21071b
chore: review feedback improvements
bryanchriswhite Jan 8, 2024
6515c64
chore: update comment
bryanchriswhite Jan 8, 2024
b4ed47c
Merge branch 'issues/141/refactor/supplier-errors' into issues/141/pr…
bryanchriswhite Jan 8, 2024
7987e14
chore: rename InMemoryCosmosNetwork to InMemoryNetwork
bryanchriswhite Jan 8, 2024
cdcfb7d
chore: add #GetConfig()
bryanchriswhite Jan 8, 2024
92c0a19
chore: fix comment
bryanchriswhite Jan 8, 2024
ff09a42
chore: fix comment typo
bryanchriswhite Jan 8, 2024
9f472f6
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
2b32545
chore: generalize ErrSupplierInvalidAddress and simplify
bryanchriswhite Dec 13, 2023
f41547e
Merge branch 'issues/141/refactor/supplier-keys' into issues/141/refa…
bryanchriswhite Jan 9, 2024
a527f23
Merge branch 'issues/141/refactor/supplier-errors' into issues/141/pr…
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
7bacad9
Merge branch 'issues/141/refactor/supplier-keys' into issues/141/refa…
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
ab3fd1e
Merge branch 'issues/141/refactor/supplier-keys' into issues/141/refa…
bryanchriswhite Jan 10, 2024
a57273c
chore: review feedback improvements
bryanchriswhite Jan 10, 2024
8486a8c
Merge branch 'issues/141/refactor/supplier-errors' into issues/141/pr…
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
6df8994
Merge branch 'issues/141/refactor/supplier-keys' into issues/141/refa…
bryanchriswhite Jan 10, 2024
2f1232c
chore: review feedback improvements
bryanchriswhite Jan 10, 2024
bd60220
Merge branch 'issues/141/refactor/supplier-errors' into issues/141/pr…
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
fea3d27
Merge remote-tracking branch 'pokt/main' into issues/141/refactor/sup…
bryanchriswhite Jan 10, 2024
c00e1b2
Merge branch 'issues/141/refactor/supplier-keys' into issues/141/refa…
bryanchriswhite Jan 10, 2024
7d1b1e0
Merge remote-tracking branch 'pokt/main' into issues/141/refactor/sup…
bryanchriswhite Jan 10, 2024
dd5ea83
Merge branch 'issues/141/refactor/supplier-errors' into issues/141/pr…
bryanchriswhite Jan 10, 2024
afbefe7
trigger CI
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
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;
}
14 changes: 8 additions & 6 deletions proto/pocket/supplier/proof.proto
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
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"];
// The session header of the session that this claim is for.
session.SessionHeader session_header = 2;
// The serialized SMST proof from the `#ClosestProof()` method.
bytes closest_merkle_proof = 3;
}

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
86 changes: 86 additions & 0 deletions testutil/network/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package network

import (
"fmt"
"testing"
"time"

db "github.com/cometbft/cometbft-db"
"github.com/cometbft/cometbft/libs/rand"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
sdkservertypes "github.com/cosmos/cosmos-sdk/server/types"
pruninttypes "github.com/cosmos/cosmos-sdk/store/pruning/types"
bryanchriswhite marked this conversation as resolved.
Show resolved Hide resolved
"github.com/cosmos/cosmos-sdk/testutil/network"
"github.com/cosmos/cosmos-sdk/testutil/sims"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth/types"

"github.com/pokt-network/poktroll/app"
)

// GetNumApplications returns the number of applications to be created in the network at genesis.
func (cfg *InMemoryNetworkConfig) GetNumApplications(t *testing.T) int {
t.Helper()

if cfg.NumApplications > 0 {
return cfg.NumApplications
}

return cfg.AppSupplierPairingRatio * cfg.NumSuppliers
}

// GetNumKeyringAccounts returns the number of keyring accounts needed for the given configuration.
func (cfg *InMemoryNetworkConfig) GetNumKeyringAccounts(t *testing.T) int {
t.Helper()

return cfg.NumGateways + cfg.NumSuppliers + cfg.GetNumApplications(t)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we inconsistent with the usage of accessor functions and state varaibles? Please add a TODO if there's an opportunity/rationale for it.

Copy link
Contributor Author

@bryanchriswhite bryanchriswhite Jan 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TL;DR, re: #GetNumApplications(), is computed here because of the way some network implementations use the network config.

In the case of inMemoryNetworkWithSessions, #NumApplications MUST be 0; therefore, #GetNumApplications() MUST be used to compute the same value using #AppToSupplierRatio instead. #GetNumApplications() will use whichever (#NumApplications / `#AppToSupplierRatio) is populated to return the appropriate value.

This constraint is enforced in the constructor. inMemoryNetworkWithGateways has the opposite constraint. My goal was to minimize complexity by sharing a single config with minimal necessary fields that are common to all network implementations.

Re: #GetNumKeyringAccounts(), I'm assuming that the number of keyring accounts is strictly a function of the number of genesis actors. If we find that we want to create more keyring accounts for a particular network, I would propose adding an #NumAdditionalKeyringAccounts field to the network config which is considered here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Re assumption w.r.t #GetNumKeyringAccounts. 👍 and doesn't even need a comment
  2. Regarding #GetNumApplications(), I think we should add a comment above the return w/ something like "NumApplications is intentionally a computed field" and potentially link to this comment.

Copy link
Contributor Author

@bryanchriswhite bryanchriswhite Jan 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If NumApplications is not populated in the config when it is provided, it is never subsequently populated by network helpers. My intention was for NumApplications and AppToSupplierRatio to be inputs and `#GetNumApplications() to be the output (single source of truth). I'm reluctant to have the helpers mutate the config after it's provided. That just seems like something that would come back around to bite us later.

I will add a comment saying as much, let me know what you think.

}

// DefaultConfig will initialize config for the network with custom application,
// genesis and single validator. All other parameters are inherited from cosmos-sdk/testutil/network.DefaultConfig
bryanchriswhite marked this conversation as resolved.
Show resolved Hide resolved
//
// TODO_UPNEXT(@bryanchriswhite #285): Remove _ prefix after DebugConfig is removed from network.go.
func _DefaultConfig() network.Config {
var (
encoding = app.MakeEncodingConfig()
chainID = "chain-" + rand.NewRand().Str(6)
)
return network.Config{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the answer to #303 is in this struct somewhere.

Codec: encoding.Marshaler,
TxConfig: encoding.TxConfig,
LegacyAmino: encoding.Amino,
InterfaceRegistry: encoding.InterfaceRegistry,
AccountRetriever: types.AccountRetriever{},
AppConstructor: func(val network.ValidatorI) sdkservertypes.Application {
return app.New(
val.GetCtx().Logger,
db.NewMemDB(),
nil,
true,
map[int64]bool{},
val.GetCtx().Config.RootDir,
0,
encoding,
sims.EmptyAppOptions{},
baseapp.SetPruning(pruninttypes.NewPruningOptionsFromString(val.GetAppConfig().Pruning)),
bryanchriswhite marked this conversation as resolved.
Show resolved Hide resolved
baseapp.SetMinGasPrices(val.GetAppConfig().MinGasPrices),
baseapp.SetChainID(chainID),
)
},
GenesisState: app.ModuleBasics.DefaultGenesis(encoding.Marshaler),
TimeoutCommit: 2 * time.Second,
ChainID: chainID,
NumValidators: 1,
BondDenom: sdk.DefaultBondDenom,
MinGasPrices: fmt.Sprintf("0.000006%s", sdk.DefaultBondDenom),
AccountTokens: sdk.TokensFromConsensusPower(1000, sdk.DefaultPowerReduction),
StakingTokens: sdk.TokensFromConsensusPower(500, sdk.DefaultPowerReduction),
BondedTokens: sdk.TokensFromConsensusPower(100, sdk.DefaultPowerReduction),
PruningStrategy: pruninttypes.PruningOptionNothing,
bryanchriswhite marked this conversation as resolved.
Show resolved Hide resolved
CleanupDir: true,
SigningAlgo: string(hd.Secp256k1Type),
KeyringOptions: []keyring.Option{},
}
}
30 changes: 30 additions & 0 deletions testutil/network/interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package network

import (
"context"
"testing"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/testutil/network"
)

// InMemoryCosmosNetwork encapsulates the cosmos-sdk testutil network instance and
// the responsibility of initializing it, along with (optional) additional/ setup,
// in #Start(). It also provides access to additional cosmos-sdk testutil network
// internals via corresponding methods.
type InMemoryCosmosNetwork interface {
// GetClientCtx returns a cosmos-sdk client.Context associated with the
// underlying cosmos-sdk testutil network instance.
GetClientCtx(*testing.T) client.Context

// GetNetworkConfig returns the underlying cosmos-sdk testutil network config.
GetNetworkConfig(*testing.T) *network.Config
bryanchriswhite marked this conversation as resolved.
Show resolved Hide resolved

// GetNetwork returns the underlying cosmos-sdk testutil network instance.
GetNetwork(*testing.T) *network.Network

// Start initializes the in-memory network, performing any setup
// (e.g. preparing on-chain state) for the test scenarios which
// will be exercised afterward.
Start(context.Context, *testing.T)
}
56 changes: 56 additions & 0 deletions testutil/network/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package network

import (
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/testutil/network"
)

// TestMerkleProofPath is intended to be used as a "path" in a merkle tree for
// which to generate a proof.
var TestMerkleProofPath = []byte("test_proof_merkle_path")

// InMemoryNetworkConfig is a **SHARED** config struct for use by InMemoryCosmosNetwork
// implementations to configure themselves, provide the necessary parameters to set-up
// code, and initialize the underlying cosmos-sdk testutil network.
//
// Examples of set-up operations include but are not limited to:
// - Creating accounts in the local keyring.
// - Creating genesis state for (a) module(s).
// - Executing on-chain transactions (i.e. on-chain, non-genesis state).
bryanchriswhite marked this conversation as resolved.
Show resolved Hide resolved
type InMemoryNetworkConfig struct {
bryanchriswhite marked this conversation as resolved.
Show resolved Hide resolved
// NumSessions is the number of sessions (with sequential start heights) for
bryanchriswhite marked this conversation as resolved.
Show resolved Hide resolved
// which the network should generate claims and proofs.
NumSessions int

// NumRelaysPerSession is the number of nodes to be inserted into each claim's
// session tree prior to generating the corresponding proof.
NumRelaysPerSession int
Olshansk marked this conversation as resolved.
Show resolved Hide resolved

// NumSuppliers is the number of suppliers that should be created at genesis.
NumSuppliers int

// NumGateways is the number of gateways that should be created at genesis.
NumGateways int

// NumApplications is the number of applications that should be created at genesis.
// Usage is mutually exclusive with AppSupplierPairingRatio. This is enforced by
Olshansk marked this conversation as resolved.
Show resolved Hide resolved
// InMemoryCosmosNetwork implementations.
NumApplications int

// AppSupplierPairingRatio is the number of applications, per supplier, that
// share a serviceId (i.e. will be in the same session).
// Usage is mutually exclusive with NumApplications. This is enforced by
// InMemoryCosmosNetwork implementations.
AppSupplierPairingRatio int

// CosmosCfg is the configuration for the underlying cosmos-sdk testutil network.
CosmosCfg *network.Config

// Keyring is the keyring to be used by clients of the cosmos-sdk testutil network.
// It is intended to be populated with a sufficient number of accounts for the
// InMemoryCosmosNetwork implementation's use cases. BaseInMemoryCosmosNetwork
// implements a #GetNumKeyringAccounts() for this purpose.
// This keyring is associated with the cosmos client context returned from
// BaseInMemoryCosmosNetwork#GetClientCtx().
Keyring keyring.Keyring
}
2 changes: 1 addition & 1 deletion x/session/keeper/query_get_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (k Keeper) GetSession(goCtx context.Context, req *types.QueryGetSessionRequ
blockHeight = ctx.BlockHeight()
}

sessionHydrator := NewSessionHydrator(req.ApplicationAddress, req.Service.Id, req.BlockHeight)
sessionHydrator := NewSessionHydrator(req.ApplicationAddress, req.Service.Id, blockHeight)
Olshansk marked this conversation as resolved.
Show resolved Hide resolved
session, err := k.HydrateSession(ctx, sessionHydrator)
if err != nil {
return nil, err
Expand Down
4 changes: 3 additions & 1 deletion x/session/keeper/session_hydrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ func (k Keeper) hydrateSessionMetadata(ctx sdk.Context, sh *sessionHydrator) err
}

sh.session.NumBlocksPerSession = NumBlocksPerSession
sh.session.SessionNumber = int64(sh.blockHeight / NumBlocksPerSession)
sh.session.SessionNumber = sh.blockHeight / NumBlocksPerSession

// NB: SessionStartBlockHeight should be aligned to NumBlocksPerSession.
bryanchriswhite marked this conversation as resolved.
Show resolved Hide resolved
sh.sessionHeader.SessionStartBlockHeight = sh.blockHeight - (sh.blockHeight % NumBlocksPerSession)
sh.sessionHeader.SessionEndBlockHeight = sh.sessionHeader.SessionStartBlockHeight + NumBlocksPerSession
return nil
Expand Down
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
8 changes: 5 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,16 @@ 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 the given supplier participated in.
Claims are pruned, according to protocol parameters, some time after their respective proof has been submitted and any dispute window has elapsed.
This is done to minimize the rate state accumulation by effectively eliminating claims as a long-term factor to persistence requirements.

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
Loading