Skip to content

Commit

Permalink
[Code Health] chore: resolve & cleanup outstanding `TODO_TECHDEBT(#517)…
Browse files Browse the repository at this point in the history
…` comments (#559)

Co-authored-by: Daniel Olshansky <[email protected]>
  • Loading branch information
bryanchriswhite and Olshansk authored May 30, 2024
1 parent 72d3e47 commit 862b793
Show file tree
Hide file tree
Showing 20 changed files with 150 additions and 119 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -390,8 +390,8 @@ test_load_relays_stress: ## Run the stress test for E2E relays on non-ephemeral
-tags=load,test -run LoadRelays --log-level=debug --timeout=30m \
--manifest ./load-testing/loadtest_manifest.yaml

.PHONY: test_localnet_load_relays_stress
test_localnet_load_relays_stress: test_e2e_env ## Run the stress test for E2E relays.
.PHONY: test_load_relays_stress_localnet
test_load_relays_stress_localnet: test_e2e_env ## Run the stress test for E2E relays.
go test -v -count=1 ./load-testing/tests/... \
-tags=load,test -run LoadRelays --log-level=debug --timeout=30m \
--manifest ./load-testing/localnet_loadtest_manifest.yaml
Expand Down
60 changes: 42 additions & 18 deletions load-testing/tests/relays_stress_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ import (
"github.com/pokt-network/poktroll/load-testing/config"
"github.com/pokt-network/poktroll/pkg/client"
"github.com/pokt-network/poktroll/pkg/client/events"
"github.com/pokt-network/poktroll/pkg/client/query"
"github.com/pokt-network/poktroll/pkg/client/tx"
"github.com/pokt-network/poktroll/pkg/observable/channel"
"github.com/pokt-network/poktroll/pkg/sync2"
testsession "github.com/pokt-network/poktroll/testutil/session"
"github.com/pokt-network/poktroll/testutil/testclient"
"github.com/pokt-network/poktroll/testutil/testclient/testeventsquery"
apptypes "github.com/pokt-network/poktroll/x/application/types"
Expand Down Expand Up @@ -173,9 +175,9 @@ func (s *relaysSuite) mapSessionInfoForLoadTestDurationFn(

sessionInfo := &sessionInfoNotif{
blockHeight: blockHeight,
sessionNumber: shared.GetSessionNumberWithDefaultParams(blockHeight),
sessionStartBlockHeight: shared.GetSessionStartHeightWithDefaultParams(blockHeight),
sessionEndBlockHeight: shared.GetSessionEndHeightWithDefaultParams(blockHeight),
sessionNumber: testsession.GetSessionNumberWithDefaultParams(blockHeight),
sessionStartBlockHeight: testsession.GetSessionStartHeightWithDefaultParams(blockHeight),
sessionEndBlockHeight: testsession.GetSessionEndHeightWithDefaultParams(blockHeight),
}

infoLogger := logger.Info().
Expand All @@ -187,7 +189,7 @@ func (s *relaysSuite) mapSessionInfoForLoadTestDurationFn(
if waitingForFirstSession && blockHeight != sessionInfo.sessionStartBlockHeight {
countDownToTestStart := sessionInfo.sessionEndBlockHeight - blockHeight + 1
infoLogger.Msgf(
"waiting for next session to start: in %d blocks",
"waiting for next testsession to start: in %d blocks",
countDownToTestStart,
)

Expand Down Expand Up @@ -269,7 +271,7 @@ func (s *relaysSuite) validateActorLoadTestIncrementPlans(plans *actorLoadTestIn
}

plans.validateAppSupplierPermutations(s)
plans.validateIncrementRates(s)
plans.validateIncrementRates(s, s.sharedParams)
plans.validateMaxAmounts(s)

require.Truef(s,
Expand Down Expand Up @@ -328,17 +330,22 @@ func (plans *actorLoadTestIncrementPlans) validateAppSupplierPermutations(t gocu

// validateIncrementRates ensures that the increment rates are multiples of the session length.
// Otherwise, the expected baseline for several metrics will be periodically skewed.
func (plans *actorLoadTestIncrementPlans) validateIncrementRates(t gocuke.TestingT) {
func (plans *actorLoadTestIncrementPlans) validateIncrementRates(
t gocuke.TestingT,
sharedParams *sharedtypes.Params,
) {
numBlocksPerSession := int64(sharedParams.GetNumBlocksPerSession())

require.Truef(t,
plans.gateways.blocksPerIncrement%shared.NumBlocksPerSession == 0,
plans.gateways.blocksPerIncrement%numBlocksPerSession == 0,
"gateway increment rate must be a multiple of the session length",
)
require.Truef(t,
plans.suppliers.blocksPerIncrement%shared.NumBlocksPerSession == 0,
plans.suppliers.blocksPerIncrement%numBlocksPerSession == 0,
"supplier increment rate must be a multiple of the session length",
)
require.Truef(t,
plans.apps.blocksPerIncrement%shared.NumBlocksPerSession == 0,
plans.apps.blocksPerIncrement%numBlocksPerSession == 0,
"app increment rate must be a multiple of the session length",
)
}
Expand All @@ -364,7 +371,7 @@ func (plans *actorLoadTestIncrementPlans) validateMaxAmounts(t gocuke.TestingT)
// totalDurationBlocks returns the number of blocks which will have elapsed when the
// proof corresponding to the session in which the maxActorCount for the given actor
// has been committed.
func (plans *actorLoadTestIncrementPlans) totalDurationBlocks() int64 {
func (plans *actorLoadTestIncrementPlans) totalDurationBlocks(sharedParams *sharedtypes.Params) int64 {
// The last block of the last session SHOULD align with the last block of the
// last increment duration (i.e. **after** maxActorCount actors are activated).
blocksToLastSessionEnd := plans.maxActorBlocksToFinalIncrementEnd()
Expand All @@ -373,7 +380,7 @@ func (plans *actorLoadTestIncrementPlans) totalDurationBlocks() int64 {

// Add one session length so that the duration is inclusive of the block which
// commits the last session's proof.
return blocksToLastProofWindowEnd + shared.NumBlocksPerSession
return blocksToLastProofWindowEnd + int64(sharedParams.GetNumBlocksPerSession())
}

// blocksToFinalIncrementStart returns the number of blocks that will have
Expand Down Expand Up @@ -416,19 +423,19 @@ func (s *relaysSuite) mapSessionInfoWhenStakingNewSuppliersAndGatewaysFn(
// available for the beginning of the next one.
// This is because the suppliers involvement is out of control of the test
// suite and is driven by the AppGateServer's supplier endpoint selection.
if suppliersPlan.shouldIncrementSupplierCount(notif, activeSuppliers, s.testStartHeight) {
if suppliersPlan.shouldIncrementSupplierCount(s.sharedParams, notif, activeSuppliers, s.testStartHeight) {
newSuppliers = s.sendStakeSuppliersTxs(notif, &suppliersPlan)
}

var newGateways []*accountInfo
activeGateways := int64(len(s.activeGateways))
if gatewaysPlan.shouldIncrementActorCount(notif, activeGateways, s.testStartHeight) {
if gatewaysPlan.shouldIncrementActorCount(s.sharedParams, notif, activeGateways, s.testStartHeight) {
newGateways = s.sendStakeGatewaysTxs(notif, &gatewaysPlan)
}

var newApps []*accountInfo
activeApps := int64(len(s.activeApplications))
if appsPlan.shouldIncrementActorCount(notif, activeApps, s.testStartHeight) {
if appsPlan.shouldIncrementActorCount(s.sharedParams, notif, activeApps, s.testStartHeight) {
newApps = s.sendFundNewAppsTx(notif, &appsPlan)
}

Expand Down Expand Up @@ -711,6 +718,7 @@ func (s *relaysSuite) sendDelegateInitialAppsTxs(apps, gateways []*accountInfo)
//
// TODO_UPNEXT(@red-One, @bryanchriswhite) move to a new file.
func (plan *actorLoadTestIncrementPlan) shouldIncrementActorCount(
sharedParams *sharedtypes.Params,
sessionInfo *sessionInfoNotif,
actorCount int64,
startBlockHeight int64,
Expand All @@ -720,9 +728,9 @@ func (plan *actorLoadTestIncrementPlan) shouldIncrementActorCount(
return false
}

initialSessionNumber := shared.GetSessionNumberWithDefaultParams(startBlockHeight)
initialSessionNumber := testsession.GetSessionNumberWithDefaultParams(startBlockHeight)
// TODO_TECHDEBT(#21): replace with gov param query when available.
actorSessionIncRate := plan.blocksPerIncrement / shared.NumBlocksPerSession
actorSessionIncRate := plan.blocksPerIncrement / int64(sharedParams.GetNumBlocksPerSession())
nextSessionNumber := sessionInfo.sessionNumber + 1 - initialSessionNumber
isSessionStartHeight := sessionInfo.blockHeight == sessionInfo.sessionStartBlockHeight
isActorIncrementHeight := nextSessionNumber%actorSessionIncRate == 0
Expand All @@ -737,6 +745,7 @@ func (plan *actorLoadTestIncrementPlan) shouldIncrementActorCount(
// Suppliers stake transactions are sent at the end of the session so they are
// available for the beginning of the next one.
func (plan *actorLoadTestIncrementPlan) shouldIncrementSupplierCount(
sharedParams *sharedtypes.Params,
sessionInfo *sessionInfoNotif,
actorCount int64,
startBlockHeight int64,
Expand All @@ -746,9 +755,9 @@ func (plan *actorLoadTestIncrementPlan) shouldIncrementSupplierCount(
return false
}

initialSessionNumber := shared.GetSessionNumberWithDefaultParams(startBlockHeight)
initialSessionNumber := testsession.GetSessionNumberWithDefaultParams(startBlockHeight)
// TODO_TECHDEBT(#21): replace with gov param query when available.
supplierSessionIncRate := plan.blocksPerIncrement / shared.NumBlocksPerSession
supplierSessionIncRate := plan.blocksPerIncrement / int64(sharedParams.GetNumBlocksPerSession())
nextSessionNumber := sessionInfo.sessionNumber + 1 - initialSessionNumber
isSessionEndHeight := sessionInfo.blockHeight == sessionInfo.sessionEndBlockHeight
isActorIncrementHeight := nextSessionNumber%supplierSessionIncRate == 0
Expand Down Expand Up @@ -1352,6 +1361,21 @@ func (s *relaysSuite) countClaimAndProofs() {
)
}

// querySharedParams queries the current on-chain shared module parameters for use
// over the duration of the test.
func (s *relaysSuite) querySharedParams() {
s.Helper()

deps := depinject.Supply(s.txContext.GetClientCtx())
sharedQueryClient, err := query.NewSharedQuerier(deps)
require.NoError(s, err)

sharedParams, err := sharedQueryClient.GetParams(s.ctx)
require.NoError(s, err)

s.sharedParams = sharedParams
}

// forEachStakedAndDelegatedAppPrepareApp is a ForEachFn that waits for txs which
// were broadcast in previous pipeline stages have been committed. It ensures that
// new applications were successfully staked and all application actors are delegated
Expand Down
11 changes: 9 additions & 2 deletions load-testing/tests/relays_stress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ import (
"cosmossdk.io/math"
"github.com/cometbft/cometbft/abci/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/pokt-network/poktroll/testutil/testclient"
"github.com/regen-network/gocuke"
"github.com/stretchr/testify/require"

"github.com/pokt-network/poktroll/testutil/testclient"

"github.com/pokt-network/poktroll/cmd/signals"
"github.com/pokt-network/poktroll/pkg/client"
"github.com/pokt-network/poktroll/pkg/observable"
Expand Down Expand Up @@ -133,6 +134,9 @@ type relaysSuite struct {
newTxEventsObs observable.Observable[*types.TxResult]
// txContext is the transaction context used to sign and send transactions.
txContext client.TxContext
// sharedParams is the shared on-chain parameters used in the test.
// It is queried at the beginning of the test.
sharedParams *sharedtypes.Params

// numRelaysSent is the number of relay requests sent during the test.
numRelaysSent atomic.Uint64
Expand Down Expand Up @@ -354,6 +358,9 @@ func (s *relaysSuite) LocalnetIsRunning() {
// Initialize the on-chain claims and proofs counter.
s.countClaimAndProofs()

// Query for the current shared on-chain params.
s.querySharedParams()

// Some suppliers may already be staked at genesis, ensure that staking during
// this test succeeds by increasing the sake amount.
minStakeAmount := s.getProvisionedActorsCurrentStakedAmount()
Expand Down Expand Up @@ -395,7 +402,7 @@ func (s *relaysSuite) MoreActorsAreStakedAsFollows(table gocuke.DataTable) {
// The test duration indicates when the test is complete.
// It is calculated as the relay load duration plus the time it takes to
// submit all claims and proofs.
s.testDurationBlocks = plans.totalDurationBlocks()
s.testDurationBlocks = plans.totalDurationBlocks(s.sharedParams)

if s.isEphemeralChain {
// Adjust the max delegations parameter to the max gateways to permit all
Expand Down
3 changes: 3 additions & 0 deletions pkg/client/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ type TxContext interface {
txHash []byte,
prove bool,
) (*cometrpctypes.ResultTx, error)

// GetClientCtx returns the cosmos-sdk client context associated with the transaction context.
GetClientCtx() cosmosclient.Context
}

// Block is an interface which abstracts the details of a block to its minimal
Expand Down
5 changes: 5 additions & 0 deletions pkg/client/tx/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,8 @@ func (txCtx cosmosTxContext) QueryTx(
) (*cometrpctypes.ResultTx, error) {
return txCtx.clientCtx.Client.Tx(ctx, txHash, prove)
}

// GetClientCtx returns the cosmos-sdk client context associated with the transaction context.
func (txCtx cosmosTxContext) GetClientCtx() cosmosclient.Context {
return cosmosclient.Context(txCtx.clientCtx)
}
2 changes: 1 addition & 1 deletion pkg/relayer/proxy/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ func TestRelayerProxy_Relays(t *testing.T) {
// session's grace period and within the second session's grace period,
// meaning a relay should not be handled at this block height.
blockOutsideSessionGracePeriod := int64(blockHeight +
shared.NumBlocksPerSession +
sharedtypes.DefaultNumBlocksPerSession +
shared.SessionGracePeriodBlocks)

// blockWithinSessionGracePeriod is the block height that is after the first
Expand Down
4 changes: 2 additions & 2 deletions testutil/keeper/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ import (
"github.com/stretchr/testify/require"

"github.com/pokt-network/poktroll/testutil/application/mocks"
testsession "github.com/pokt-network/poktroll/testutil/session"
"github.com/pokt-network/poktroll/x/application/keeper"
"github.com/pokt-network/poktroll/x/application/types"
gatewaytypes "github.com/pokt-network/poktroll/x/gateway/types"
"github.com/pokt-network/poktroll/x/shared"
sharedtypes "github.com/pokt-network/poktroll/x/shared/types"
)

Expand Down Expand Up @@ -77,7 +77,7 @@ func ApplicationKeeper(t testing.TB) (keeper.Keeper, context.Context) {
AnyTimes()
mockSharedKeeper.EXPECT().GetSessionEndHeight(gomock.Any(), gomock.Any()).
DoAndReturn(func(_ context.Context, queryHeight int64) int64 {
return shared.GetSessionEndHeightWithDefaultParams(queryHeight)
return testsession.GetSessionEndHeightWithDefaultParams(queryHeight)
}).
AnyTimes()

Expand Down
45 changes: 45 additions & 0 deletions testutil/session/session.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package session

import (
"github.com/pokt-network/poktroll/x/session/keeper"
"github.com/pokt-network/poktroll/x/shared"
"github.com/pokt-network/poktroll/x/shared/types"
)

// GetSessionIdWithDefaultParams returns the string and bytes representation of the
// sessionId for the session containing blockHeight, given the default shared on-chain
// parameters, application public key, service ID, and block hash.
func GetSessionIdWithDefaultParams(
appPubKey,
serviceId string,
blockHashBz []byte,
blockHeight int64,
) (sessionId string, sessionIdBz []byte) {
sharedParams := types.DefaultParams()
return keeper.GetSessionId(&sharedParams, appPubKey, serviceId, blockHashBz, blockHeight)
}

// GetSessionStartHeightWithDefaultParams returns the block height at which the
// session containing queryHeight starts, given the default shared on-chain
// parameters.
// See shared.GetSessionStartHeight for more details.
func GetSessionStartHeightWithDefaultParams(queryHeight int64) int64 {
sharedParams := types.DefaultParams()
return shared.GetSessionStartHeight(&sharedParams, queryHeight)
}

// GetSessionEndHeightWithDefaultParams returns the block height at which the session
// containing queryHeight ends, given the default shared on-chain parameters.
// See shared.GetSessionEndHeight for more details.
func GetSessionEndHeightWithDefaultParams(queryHeight int64) int64 {
sharedParams := types.DefaultParams()
return shared.GetSessionEndHeight(&sharedParams, queryHeight)
}

// GetSessionNumberWithDefaultParams returns the session number of the session
// containing queryHeight, given the default on-chain shared parameters.
// See shared.GetSessionNumber for more details.
func GetSessionNumberWithDefaultParams(queryHeight int64) int64 {
sharedParams := types.DefaultParams()
return shared.GetSessionNumber(&sharedParams, queryHeight)
}
15 changes: 7 additions & 8 deletions testutil/testclient/testqueryclients/sessionquerier.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ import (
"github.com/golang/mock/gomock"

"github.com/pokt-network/poktroll/testutil/mockclient"
sessionkeeper "github.com/pokt-network/poktroll/x/session/keeper"
testsession "github.com/pokt-network/poktroll/testutil/session"
sessiontypes "github.com/pokt-network/poktroll/x/session/types"
"github.com/pokt-network/poktroll/x/shared"
sharedtypes "github.com/pokt-network/poktroll/x/shared/types"
)

Expand Down Expand Up @@ -48,7 +47,7 @@ func NewTestSessionQueryClient(
serviceId string,
blockHeight int64,
) (session *sessiontypes.Session, err error) {
sessionId, _ := sessionkeeper.GetSessionIdWithDefaultParams(address, serviceId, blockHashBz, blockHeight)
sessionId, _ := testsession.GetSessionIdWithDefaultParams(address, serviceId, blockHashBz, blockHeight)

session, ok := sessionsMap[sessionId]
if !ok {
Expand All @@ -73,18 +72,18 @@ func AddToExistingSessions(
) {
t.Helper()

sessionId, _ := sessionkeeper.GetSessionIdWithDefaultParams(appAddress, serviceId, blockHashBz, blockHeight)
sessionId, _ := testsession.GetSessionIdWithDefaultParams(appAddress, serviceId, blockHashBz, blockHeight)

session := sessiontypes.Session{
Header: &sessiontypes.SessionHeader{
Service: &sharedtypes.Service{Id: serviceId},
ApplicationAddress: appAddress,
SessionId: sessionId,
SessionStartBlockHeight: shared.GetSessionStartHeightWithDefaultParams(blockHeight),
SessionEndBlockHeight: shared.GetSessionEndHeightWithDefaultParams(blockHeight),
SessionStartBlockHeight: testsession.GetSessionStartHeightWithDefaultParams(blockHeight),
SessionEndBlockHeight: testsession.GetSessionEndHeightWithDefaultParams(blockHeight),
},
NumBlocksPerSession: shared.NumBlocksPerSession,
SessionNumber: shared.GetSessionNumberWithDefaultParams(blockHeight),
NumBlocksPerSession: sharedtypes.DefaultNumBlocksPerSession,
SessionNumber: testsession.GetSessionNumberWithDefaultParams(blockHeight),
SessionId: sessionId,
Suppliers: []*sharedtypes.Supplier{},
}
Expand Down
Loading

0 comments on commit 862b793

Please sign in to comment.