Skip to content

Commit

Permalink
[Application] Refactor app module to fetch on-chain params (#555)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel Olshansky <[email protected]>
  • Loading branch information
bryanchriswhite and Olshansk authored May 30, 2024
1 parent 5a3e81e commit df338b8
Show file tree
Hide file tree
Showing 22 changed files with 204 additions and 55 deletions.
10 changes: 5 additions & 5 deletions load-testing/tests/relays_stress_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,9 @@ func (s *relaysSuite) mapSessionInfoForLoadTestDurationFn(

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

infoLogger := logger.Info().
Expand Down Expand Up @@ -720,7 +720,7 @@ func (plan *actorLoadTestIncrementPlan) shouldIncrementActorCount(
return false
}

initialSessionNumber := shared.GetSessionNumber(startBlockHeight)
initialSessionNumber := shared.GetSessionNumberWithDefaultParams(startBlockHeight)
// TODO_TECHDEBT(#21): replace with gov param query when available.
actorSessionIncRate := plan.blocksPerIncrement / shared.NumBlocksPerSession
nextSessionNumber := sessionInfo.sessionNumber + 1 - initialSessionNumber
Expand All @@ -746,7 +746,7 @@ func (plan *actorLoadTestIncrementPlan) shouldIncrementSupplierCount(
return false
}

initialSessionNumber := shared.GetSessionNumber(startBlockHeight)
initialSessionNumber := shared.GetSessionNumberWithDefaultParams(startBlockHeight)
// TODO_TECHDEBT(#21): replace with gov param query when available.
supplierSessionIncRate := plan.blocksPerIncrement / shared.NumBlocksPerSession
nextSessionNumber := sessionInfo.sessionNumber + 1 - initialSessionNumber
Expand Down
2 changes: 1 addition & 1 deletion pkg/crypto/rings/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ func (rc *ringClient) getRingPointsForAddressAtHeight(
// gateways that have been undelegated after the target session end height.
func GetRingAddressesAtBlock(app *apptypes.Application, blockHeight int64) []string {
// Get the target session end height at which we want to get the active delegations.
targetSessionEndHeight := uint64(shared.GetSessionEndBlockHeight(blockHeight))
targetSessionEndHeight := uint64(shared.GetSessionEndHeightWithDefaultParams(blockHeight))
// Get the current active delegations for the application and use them as a base.
activeDelegationsAtHeight := app.DelegateeGatewayAddresses

Expand Down
17 changes: 16 additions & 1 deletion testutil/keeper/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ import (
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"

mocks "github.com/pokt-network/poktroll/testutil/application/mocks"
"github.com/pokt-network/poktroll/testutil/application/mocks"
"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"
)

// stakedGatewayMap is used to mock whether a gateway is staked or not for use
Expand Down Expand Up @@ -67,6 +69,18 @@ func ApplicationKeeper(t testing.TB) (keeper.Keeper, context.Context) {
},
).AnyTimes()

mockSharedKeeper := mocks.NewMockSharedKeeper(ctrl)
mockSharedKeeper.EXPECT().GetParams(gomock.Any()).
DoAndReturn(func(_ context.Context) sharedtypes.Params {
return sharedtypes.DefaultParams()
}).
AnyTimes()
mockSharedKeeper.EXPECT().GetSessionEndHeight(gomock.Any(), gomock.Any()).
DoAndReturn(func(_ context.Context, queryHeight int64) int64 {
return shared.GetSessionEndHeightWithDefaultParams(queryHeight)
}).
AnyTimes()

k := keeper.NewKeeper(
cdc,
runtime.NewKVStoreService(storeKey),
Expand All @@ -75,6 +89,7 @@ func ApplicationKeeper(t testing.TB) (keeper.Keeper, context.Context) {
mockBankKeeper,
mockAccountKeeper,
mockGatewayKeeper,
mockSharedKeeper,
)

ctx := sdk.NewContext(stateStore, cmtproto.Header{}, false, log.NewNopLogger())
Expand Down
10 changes: 10 additions & 0 deletions testutil/keeper/proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
prooftypes "github.com/pokt-network/poktroll/x/proof/types"
sessionkeeper "github.com/pokt-network/poktroll/x/session/keeper"
sessiontypes "github.com/pokt-network/poktroll/x/session/types"
sharedkeeper "github.com/pokt-network/poktroll/x/shared/keeper"
sharedtypes "github.com/pokt-network/poktroll/x/shared/types"
supplierkeeper "github.com/pokt-network/poktroll/x/supplier/keeper"
suppliertypes "github.com/pokt-network/poktroll/x/supplier/types"
Expand Down Expand Up @@ -142,6 +143,14 @@ func NewProofModuleKeepers(t testing.TB, opts ...ProofKeepersOpt) (_ *ProofModul
authority.String(),
)

// Construct a real shared keeper.
sharedKeeper := sharedkeeper.NewKeeper(
cdc,
runtime.NewKVStoreService(keys[sharedtypes.StoreKey]),
logger,
authority.String(),
)

// Construct gateway keeper with a mocked bank keeper.
gatewayKeeper := gatewaykeeper.NewKeeper(
cdc,
Expand All @@ -161,6 +170,7 @@ func NewProofModuleKeepers(t testing.TB, opts ...ProofKeepersOpt) (_ *ProofModul
applicationmocks.NewMockBankKeeper(ctrl),
accountKeeper,
gatewayKeeper,
sharedKeeper,
)
require.NoError(t, appKeeper.SetParams(ctx, apptypes.DefaultParams()))

Expand Down
12 changes: 11 additions & 1 deletion testutil/keeper/tokenomics.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"testing"

"cosmossdk.io/log"
math "cosmossdk.io/math"
"cosmossdk.io/math"
"cosmossdk.io/store"
"cosmossdk.io/store/metrics"
storetypes "cosmossdk.io/store/types"
Expand Down Expand Up @@ -38,6 +38,7 @@ import (
prooftypes "github.com/pokt-network/poktroll/x/proof/types"
sessionkeeper "github.com/pokt-network/poktroll/x/session/keeper"
sessiontypes "github.com/pokt-network/poktroll/x/session/types"
sharedkeeper "github.com/pokt-network/poktroll/x/shared/keeper"
sharedtypes "github.com/pokt-network/poktroll/x/shared/types"
supplierkeeper "github.com/pokt-network/poktroll/x/supplier/keeper"
suppliertypes "github.com/pokt-network/poktroll/x/supplier/types"
Expand Down Expand Up @@ -239,6 +240,14 @@ func NewTokenomicsModuleKeepers(
bankKeeper.MintCoins(ctx, suppliertypes.ModuleName, sdk.NewCoins(sdk.NewCoin("upokt", math.NewInt(1000000000000))))
bankKeeper.MintCoins(ctx, apptypes.ModuleName, sdk.NewCoins(sdk.NewCoin("upokt", math.NewInt(1000000000000))))

// Construct a real shared keeper.
sharedKeeper := sharedkeeper.NewKeeper(
cdc,
runtime.NewKVStoreService(keys[sharedtypes.StoreKey]),
logger,
authority.String(),
)

// Construct gateway keeper with a mocked bank keeper.
gatewayKeeper := gatewaykeeper.NewKeeper(
cdc,
Expand All @@ -258,6 +267,7 @@ func NewTokenomicsModuleKeepers(
bankKeeper,
accountKeeper,
gatewayKeeper,
sharedKeeper,
)
require.NoError(t, appKeeper.SetParams(ctx, apptypes.DefaultParams()))

Expand Down
6 changes: 3 additions & 3 deletions testutil/testclient/testqueryclients/sessionquerier.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@ func AddToExistingSessions(
Service: &sharedtypes.Service{Id: serviceId},
ApplicationAddress: appAddress,
SessionId: sessionId,
SessionStartBlockHeight: shared.GetSessionStartBlockHeight(blockHeight),
SessionEndBlockHeight: shared.GetSessionEndBlockHeight(blockHeight),
SessionStartBlockHeight: shared.GetSessionStartHeightWithDefaultParams(blockHeight),
SessionEndBlockHeight: shared.GetSessionEndHeightWithDefaultParams(blockHeight),
},
NumBlocksPerSession: shared.NumBlocksPerSession,
SessionNumber: shared.GetSessionNumber(blockHeight),
SessionNumber: shared.GetSessionNumberWithDefaultParams(blockHeight),
SessionId: sessionId,
Suppliers: []*sharedtypes.Supplier{},
}
Expand Down
4 changes: 2 additions & 2 deletions testutil/testproxy/relayerproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,8 +409,8 @@ func GenerateRelayRequest(
ApplicationAddress: appAddress,
SessionId: string(sessionId[:]),
Service: &sharedtypes.Service{Id: serviceId},
SessionStartBlockHeight: shared.GetSessionStartBlockHeight(blockHeight),
SessionEndBlockHeight: shared.GetSessionEndBlockHeight(blockHeight),
SessionStartBlockHeight: shared.GetSessionStartHeightWithDefaultParams(blockHeight),
SessionEndBlockHeight: shared.GetSessionEndHeightWithDefaultParams(blockHeight),
},
// The returned relay is unsigned and must be signed elsewhere for functionality
Signature: []byte(""),
Expand Down
3 changes: 3 additions & 0 deletions x/application/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type (
bankKeeper types.BankKeeper
accountKeeper types.AccountKeeper
gatewayKeeper types.GatewayKeeper
sharedKeeper types.SharedKeeper
}
)

Expand All @@ -36,6 +37,7 @@ func NewKeeper(
bankKeeper types.BankKeeper,
accountKeeper types.AccountKeeper,
gatewayKeeper types.GatewayKeeper,
sharedKeeper types.SharedKeeper,
) Keeper {
if _, err := sdk.AccAddressFromBech32(authority); err != nil {
panic(fmt.Sprintf("invalid authority address: %s", authority))
Expand All @@ -50,6 +52,7 @@ func NewKeeper(
bankKeeper: bankKeeper,
accountKeeper: accountKeeper,
gatewayKeeper: gatewayKeeper,
sharedKeeper: sharedKeeper,
}
}

Expand Down
6 changes: 3 additions & 3 deletions x/application/keeper/msg_server_undelegate_from_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/pokt-network/poktroll/telemetry"
"github.com/pokt-network/poktroll/x/application/types"
"github.com/pokt-network/poktroll/x/shared"
)

func (k msgServer) UndelegateFromGateway(ctx context.Context, msg *types.MsgUndelegateFromGateway) (*types.MsgUndelegateFromGatewayResponse, error) {
Expand Down Expand Up @@ -55,7 +54,7 @@ func (k msgServer) UndelegateFromGateway(ctx context.Context, msg *types.MsgUnde
sdkCtx := sdk.UnwrapSDKContext(ctx)
currentBlock := sdkCtx.BlockHeight()

k.recordPendingUndelegation(&foundApp, msg.GatewayAddress, currentBlock)
k.recordPendingUndelegation(ctx, &foundApp, msg.GatewayAddress, currentBlock)

// Update the application store with the new delegation
k.SetApplication(ctx, foundApp)
Expand All @@ -76,11 +75,12 @@ func (k msgServer) UndelegateFromGateway(ctx context.Context, msg *types.MsgUnde
// recordPendingUndelegation adds the given gateway address to the application's
// pending undelegations list.
func (k Keeper) recordPendingUndelegation(
ctx context.Context,
app *types.Application,
gatewayAddress string,
currentBlockHeight int64,
) {
sessionEndHeight := uint64(shared.GetSessionEndBlockHeight(currentBlockHeight))
sessionEndHeight := uint64(k.sharedKeeper.GetSessionEndHeight(ctx, currentBlockHeight))
undelegatingGatewayListAtBlock := app.PendingUndelegations[sessionEndHeight]

// Add the gateway address to the undelegated gateways list if it's not already there.
Expand Down
18 changes: 13 additions & 5 deletions x/application/keeper/msg_server_undelegate_from_gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ func TestMsgServer_UndelegateFromGateway_DelegationIsActiveUntilNextSession(t *t

// Verify that the gateway is added to the pending undelegation list with the
// right sessionEndHeight as the map key.
sessionEndHeight := uint64(shared.GetSessionEndBlockHeight(undelegationHeight))
sessionEndHeight := uint64(shared.GetSessionEndHeightWithDefaultParams(undelegationHeight))
require.Contains(t,
app.PendingUndelegations[sessionEndHeight].GatewayAddresses,
pendingUndelegateFromAddr,
Expand Down Expand Up @@ -384,7 +384,7 @@ func TestMsgServer_UndelegateFromGateway_DelegationIsPrunedAfterRetentionPeriod(

// Verify that the the pending undelegation map no longer contains the
// sessionEndHeight key.
sessionEndHeight := uint64(shared.GetSessionEndBlockHeight(undelegationHeight))
sessionEndHeight := uint64(shared.GetSessionEndHeightWithDefaultParams(undelegationHeight))
require.Empty(t, app.PendingUndelegations[sessionEndHeight])

// Verify that the reconstructed delegatee gateway list can no longer include
Expand Down Expand Up @@ -423,7 +423,7 @@ func TestMsgServer_UndelegateFromGateway_RedelegationAfterUndelegationAtTheSameS

// Verify that the gateway is also present in the pending undelegation list with the
// right sessionEndHeight as the map key.
sessionEndHeight := uint64(shared.GetSessionEndBlockHeight(undelegationHeight))
sessionEndHeight := uint64(shared.GetSessionEndHeightWithDefaultParams(undelegationHeight))
require.Contains(t,
app.PendingUndelegations[sessionEndHeight].GatewayAddresses,
gatewayAddrToRedelegate,
Expand Down Expand Up @@ -533,7 +533,15 @@ func createAppStakeDelegateAndUndelegate(
// getUndelegationPruningBlockHeight returns the block height at which undelegations
// should be pruned for a given undlegation block height.
func getUndelegationPruningBlockHeight(blockHeight int64) (pruningHeihgt int64) {
nextSessionStartHeight := shared.GetSessionEndBlockHeight(blockHeight) + 1
nextSessionStartHeight := shared.GetSessionEndHeightWithDefaultParams(blockHeight) + 1

return nextSessionStartHeight + keeper.GetNumBlocksUndelegationRetention()
return nextSessionStartHeight + getNumBlocksUndelegationRetentionWithDefaultParams()
}

// getNumBlocksUndelegationRetentionWithDefaultParams returns the number of blocks for
// which undelegations should be kept before being pruned, given the default shared
// module parameters.
func getNumBlocksUndelegationRetentionWithDefaultParams() int64 {
sharedParams := sharedtypes.DefaultParams()
return keeper.GetNumBlocksUndelegationRetention(&sharedParams)
}
22 changes: 18 additions & 4 deletions x/application/keeper/prune_undelegations.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package keeper

import (
"context"

sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/pokt-network/poktroll/x/shared"
sharedtypes "github.com/pokt-network/poktroll/x/shared/types"
)

// NumSessionsAppToGatewayUndelegationRetention is the number of sessions for which
Expand All @@ -20,7 +23,7 @@ func (k Keeper) EndBlockerPruneAppToGatewayPendingUndelegation(ctx sdk.Context)
currentHeight := ctx.BlockHeight()

// Calculate the block height at which undelegations should be pruned
numBlocksUndelegationRetention := GetNumBlocksUndelegationRetention()
numBlocksUndelegationRetention := k.GetNumBlocksUndelegationRetention(ctx)
if currentHeight <= numBlocksUndelegationRetention {
return nil
}
Expand All @@ -43,8 +46,19 @@ func (k Keeper) EndBlockerPruneAppToGatewayPendingUndelegation(ctx sdk.Context)
}

// GetNumBlocksUndelegationRetention returns the number of blocks for which
// undelegations should be kept before being pruned.
func GetNumBlocksUndelegationRetention() int64 {
// undelegations should be kept before being pruned, given the current on-chain
// shared module parameters.
func (k Keeper) GetNumBlocksUndelegationRetention(ctx context.Context) int64 {
sharedParams := k.sharedKeeper.GetParams(ctx)
return GetNumBlocksUndelegationRetention(&sharedParams)
}

// GetNumBlocksUndelegationRetention returns the number of blocks for which
// undelegations should be kept before being pruned, given the passed shared
// module parameters.
func GetNumBlocksUndelegationRetention(sharedParams *sharedtypes.Params) int64 {
numBlocksPerSession := int64(sharedParams.GetNumBlocksPerSession())

return shared.SessionGracePeriodBlocks +
(shared.NumBlocksPerSession * NumSessionsAppToGatewayUndelegationRetention)
(numBlocksPerSession * NumSessionsAppToGatewayUndelegationRetention)
}
2 changes: 2 additions & 0 deletions x/application/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ type ModuleInputs struct {
AccountKeeper types.AccountKeeper
BankKeeper types.BankKeeper
GatewayKeeper types.GatewayKeeper
SharedKeeper types.SharedKeeper
}

type ModuleOutputs struct {
Expand All @@ -204,6 +205,7 @@ func ProvideModule(in ModuleInputs) ModuleOutputs {
in.BankKeeper,
in.AccountKeeper,
in.GatewayKeeper,
in.SharedKeeper,
)
m := NewAppModule(
in.Cdc,
Expand Down
9 changes: 8 additions & 1 deletion x/application/types/expected_keepers.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:generate mockgen -destination ../../../testutil/application/mocks/expected_keepers_mock.go -package mocks . AccountKeeper,BankKeeper,GatewayKeeper
//go:generate mockgen -destination ../../../testutil/application/mocks/expected_keepers_mock.go -package mocks . AccountKeeper,BankKeeper,GatewayKeeper,SharedKeeper

package types

Expand All @@ -8,6 +8,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"

gatewaytypes "github.com/pokt-network/poktroll/x/gateway/types"
sharedtypes "github.com/pokt-network/poktroll/x/shared/types"
)

// AccountKeeper defines the expected interface for the Account module.
Expand All @@ -30,3 +31,9 @@ type BankKeeper interface {
type GatewayKeeper interface {
GetGateway(ctx context.Context, addr string) (gatewaytypes.Gateway, bool)
}

// SharedKeeper defines the expected interface needed to retrieve shared information.
type SharedKeeper interface {
GetParams(ctx context.Context) sharedtypes.Params
GetSessionEndHeight(ctx context.Context, queryHeight int64) int64
}
2 changes: 1 addition & 1 deletion x/proof/keeper/msg_server_create_claim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ func newTestClaimMsg(
Service: service,
SessionId: sessionId,
SessionStartBlockHeight: sessionStartHeight,
SessionEndBlockHeight: shared.GetSessionEndBlockHeight(sessionStartHeight),
SessionEndBlockHeight: shared.GetSessionEndHeightWithDefaultParams(sessionStartHeight),
},
merkleRoot,
)
Expand Down
2 changes: 1 addition & 1 deletion x/proof/keeper/proof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func createNProofs(keeper keeper.Keeper, ctx context.Context, n int) []types.Pro
Service: &sharedtypes.Service{Id: testServiceId},
SessionId: fmt.Sprintf("session-%d", i),
SessionStartBlockHeight: 1,
SessionEndBlockHeight: shared.GetSessionEndBlockHeight(1),
SessionEndBlockHeight: shared.GetSessionEndHeightWithDefaultParams(1),
},
ClosestMerkleProof: nil,
}
Expand Down
Loading

0 comments on commit df338b8

Please sign in to comment.