Skip to content

Commit

Permalink
[Testing, Tooling] chore: in-memory network interface & config types (#…
Browse files Browse the repository at this point in the history
…289)

* refactor: `NewMinedRelay` to shared testutil

* refactor: claim & proof protobuf types

* refactor: rename supplier keeper `UpsertClaim` & `UpsertProof`

* refactor: misc. claim-side improvements

* chore: add TODOs

* refactor: supplier module keys

* refactor: supplier module errors

* chore: review feedback improvements

Co-authored-by: harry <[email protected]>
Co-authored-by: Daniel Olshansky <[email protected]>

* chore: review feedback improvements

Co-authored-by: Daniel Olshansky <[email protected]>
Co-authored-by: harry <[email protected]>

* chore: review feedback improvements

Co-authored-by: Daniel Olshansky <[email protected]>

* fix: pre-generated keyring accounts

* fix: session hydrator

* feat: add InMemoryCosmosNetwork interface

* chore: add InMemoryNetworkConfig

* chore: add TODO comment

* Revert "fix: pre-generated keyring accounts"

This reverts commit af531b0.

* chore: add godoc comments & simplify

* chore: add TODOs

* trigger CI

* chore: review feedback improvements

Co-authored-by: Redouane Lakrache <[email protected]>
Co-authored-by: Daniel Olshansky <[email protected]>

* chore: update comment

* chore: rename InMemoryCosmosNetwork to InMemoryNetwork

* chore: add #GetConfig()

* chore: fix comment

* chore: fix comment typo

* trigger CI

* chore: add TODO

* chore: generalize ErrSupplierInvalidAddress and simplify

* chore: review feedback improvements

Co-authored-by: Daniel Olshansky <[email protected]>

* fix: usage raw string literal

* chore: review feedback improvements

Co-authored-by: h5law <[email protected]>
Co-authored-by: Daniel Olshansky <[email protected]>

* chore: review feedback improvements

* trigger CI

---------

Co-authored-by: harry <[email protected]>
Co-authored-by: Daniel Olshansky <[email protected]>
Co-authored-by: Redouane Lakrache <[email protected]>
Co-authored-by: h5law <[email protected]>
  • Loading branch information
5 people authored Jan 10, 2024
1 parent e39ba19 commit db80345
Show file tree
Hide file tree
Showing 5 changed files with 194 additions and 2 deletions.
90 changes: 90 additions & 0 deletions testutil/network/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
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"
pruningtypes "github.com/cosmos/cosmos-sdk/store/pruning/types"
"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. NOTE: This method is intended to compute the correct value,
// regardless of how the configuration is configured (i.e. the expectations/usage
// of it in any given in-memory network implementation).
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)
}

// 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.
//
// 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{
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(pruningtypes.NewPruningOptionsFromString(val.GetAppConfig().Pruning)),
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: pruningtypes.PruningOptionNothing,
CleanupDir: true,
SigningAlgo: string(hd.Secp256k1Type),
KeyringOptions: []keyring.Option{},
}
}
34 changes: 34 additions & 0 deletions testutil/network/interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package network

import (
"context"
"testing"

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

// InMemoryNetwork 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 InMemoryNetwork interface {
// GetConfig returns the InMemoryNetworkConfig which associated with a given
// InMemoryNetwork instance.
GetConfig(*testing.T) *InMemoryNetworkConfig

// GetClientCtx returns a cosmos-sdk client.Context associated with the
// underlying cosmos-sdk testutil network instance.
GetClientCtx(*testing.T) client.Context

// GetCosmosNetworkConfig returns the underlying cosmos-sdk testutil network config.
GetCosmosNetworkConfig(*testing.T) *network.Config

// 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)
}
66 changes: 66 additions & 0 deletions testutil/network/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
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 InMemoryNetwork
// 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).
// - Governance parameter configuration
// - Etc...
type InMemoryNetworkConfig struct {
// NumSessions is the number of sessions (with increasing start heights) for
// 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

// 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
// InMemoryNetwork implementations.
//
// NOTE: Use #GetNumApplications() to compute the correct value, regardless of
// which field is used; in-memory network implementations SHOULD NOT modify config
// fields. #GetNumApplications() is intended to be the single source of truth.
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
// InMemoryNetwork implementations.
//
// NOTE: Use #GetNumApplications() to compute the correct value, regardless of
// which field is used; in-memory network implementations SHOULD NOT modify config
// fields. #GetNumApplications() is intended to be the single source of truth.
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
// InMemoryNetwork implementation's use cases. BaseInMemoryNetwork
// implements a #GetNumKeyringAccounts() for this purpose.
// This keyring is associated with the cosmos client context returned from
// BaseInMemoryNetwork#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)
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

// TODO_BLOCKER: SessionStartBlockHeight should be aligned to NumBlocksPerSession.
sh.sessionHeader.SessionStartBlockHeight = sh.blockHeight - (sh.blockHeight % NumBlocksPerSession)
sh.sessionHeader.SessionEndBlockHeight = sh.sessionHeader.SessionStartBlockHeight + NumBlocksPerSession
return nil
Expand Down

0 comments on commit db80345

Please sign in to comment.