diff --git a/load-testing/tests/relays_stress_helpers_test.go b/load-testing/tests/relays_stress_helpers_test.go index a78d42ac7..8acb4dc42 100644 --- a/load-testing/tests/relays_stress_helpers_test.go +++ b/load-testing/tests/relays_stress_helpers_test.go @@ -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(). @@ -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 @@ -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 diff --git a/pkg/crypto/rings/client.go b/pkg/crypto/rings/client.go index 9e8560833..b494c4892 100644 --- a/pkg/crypto/rings/client.go +++ b/pkg/crypto/rings/client.go @@ -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 diff --git a/testutil/keeper/application.go b/testutil/keeper/application.go index f7c5a165a..6680497d3 100644 --- a/testutil/keeper/application.go +++ b/testutil/keeper/application.go @@ -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 @@ -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), @@ -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()) diff --git a/testutil/keeper/proof.go b/testutil/keeper/proof.go index 3a6e8b68c..7de11974e 100644 --- a/testutil/keeper/proof.go +++ b/testutil/keeper/proof.go @@ -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" @@ -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, @@ -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())) diff --git a/testutil/keeper/tokenomics.go b/testutil/keeper/tokenomics.go index da1ebd131..f02b0b262 100644 --- a/testutil/keeper/tokenomics.go +++ b/testutil/keeper/tokenomics.go @@ -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" @@ -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" @@ -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, @@ -258,6 +267,7 @@ func NewTokenomicsModuleKeepers( bankKeeper, accountKeeper, gatewayKeeper, + sharedKeeper, ) require.NoError(t, appKeeper.SetParams(ctx, apptypes.DefaultParams())) diff --git a/testutil/testclient/testqueryclients/sessionquerier.go b/testutil/testclient/testqueryclients/sessionquerier.go index 552e66969..1b0c3c992 100644 --- a/testutil/testclient/testqueryclients/sessionquerier.go +++ b/testutil/testclient/testqueryclients/sessionquerier.go @@ -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{}, } diff --git a/testutil/testproxy/relayerproxy.go b/testutil/testproxy/relayerproxy.go index 1e1363063..19eaa905d 100644 --- a/testutil/testproxy/relayerproxy.go +++ b/testutil/testproxy/relayerproxy.go @@ -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(""), diff --git a/x/application/keeper/keeper.go b/x/application/keeper/keeper.go index a621a1dcf..f26ef8d62 100644 --- a/x/application/keeper/keeper.go +++ b/x/application/keeper/keeper.go @@ -24,6 +24,7 @@ type ( bankKeeper types.BankKeeper accountKeeper types.AccountKeeper gatewayKeeper types.GatewayKeeper + sharedKeeper types.SharedKeeper } ) @@ -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)) @@ -50,6 +52,7 @@ func NewKeeper( bankKeeper: bankKeeper, accountKeeper: accountKeeper, gatewayKeeper: gatewayKeeper, + sharedKeeper: sharedKeeper, } } diff --git a/x/application/keeper/msg_server_undelegate_from_gateway.go b/x/application/keeper/msg_server_undelegate_from_gateway.go index f5a0345da..99728994a 100644 --- a/x/application/keeper/msg_server_undelegate_from_gateway.go +++ b/x/application/keeper/msg_server_undelegate_from_gateway.go @@ -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) { @@ -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) @@ -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. diff --git a/x/application/keeper/msg_server_undelegate_from_gateway_test.go b/x/application/keeper/msg_server_undelegate_from_gateway_test.go index c5d80977d..224b8f3c9 100644 --- a/x/application/keeper/msg_server_undelegate_from_gateway_test.go +++ b/x/application/keeper/msg_server_undelegate_from_gateway_test.go @@ -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, @@ -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 @@ -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, @@ -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) } diff --git a/x/application/keeper/prune_undelegations.go b/x/application/keeper/prune_undelegations.go index 2cf10aa47..eb2e276e9 100644 --- a/x/application/keeper/prune_undelegations.go +++ b/x/application/keeper/prune_undelegations.go @@ -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 @@ -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 } @@ -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) } diff --git a/x/application/module/module.go b/x/application/module/module.go index 72d0e86ac..050332a61 100644 --- a/x/application/module/module.go +++ b/x/application/module/module.go @@ -181,6 +181,7 @@ type ModuleInputs struct { AccountKeeper types.AccountKeeper BankKeeper types.BankKeeper GatewayKeeper types.GatewayKeeper + SharedKeeper types.SharedKeeper } type ModuleOutputs struct { @@ -204,6 +205,7 @@ func ProvideModule(in ModuleInputs) ModuleOutputs { in.BankKeeper, in.AccountKeeper, in.GatewayKeeper, + in.SharedKeeper, ) m := NewAppModule( in.Cdc, diff --git a/x/application/types/expected_keepers.go b/x/application/types/expected_keepers.go index cc74e6f89..3c908df63 100644 --- a/x/application/types/expected_keepers.go +++ b/x/application/types/expected_keepers.go @@ -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 @@ -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. @@ -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 +} diff --git a/x/proof/keeper/msg_server_create_claim_test.go b/x/proof/keeper/msg_server_create_claim_test.go index 5f7da9c31..170f6cbd1 100644 --- a/x/proof/keeper/msg_server_create_claim_test.go +++ b/x/proof/keeper/msg_server_create_claim_test.go @@ -300,7 +300,7 @@ func newTestClaimMsg( Service: service, SessionId: sessionId, SessionStartBlockHeight: sessionStartHeight, - SessionEndBlockHeight: shared.GetSessionEndBlockHeight(sessionStartHeight), + SessionEndBlockHeight: shared.GetSessionEndHeightWithDefaultParams(sessionStartHeight), }, merkleRoot, ) diff --git a/x/proof/keeper/proof_test.go b/x/proof/keeper/proof_test.go index c72f32bbe..13c578f82 100644 --- a/x/proof/keeper/proof_test.go +++ b/x/proof/keeper/proof_test.go @@ -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, } diff --git a/x/proof/module/helpers_test.go b/x/proof/module/helpers_test.go index b8588e554..3e2dd6045 100644 --- a/x/proof/module/helpers_test.go +++ b/x/proof/module/helpers_test.go @@ -130,7 +130,7 @@ func networkWithClaimObjects( claim := createClaim( t, net, ctx, supplierAcct.Address.String(), - shared.GetSessionStartBlockHeight(blockHeight), + shared.GetSessionStartHeightWithDefaultParams(blockHeight), appAcct.Address.String(), ) claims = append(claims, *claim) @@ -160,7 +160,7 @@ func encodeSessionHeader( Service: &sharedtypes.Service{Id: testServiceId}, SessionId: sessionId, SessionStartBlockHeight: sessionStartHeight, - SessionEndBlockHeight: shared.GetSessionEndBlockHeight(sessionStartHeight), + SessionEndBlockHeight: shared.GetSessionEndHeightWithDefaultParams(sessionStartHeight), } cdc := codec.NewProtoCodec(codectypes.NewInterfaceRegistry()) sessionHeaderBz := cdc.MustMarshalJSON(sessionHeader) @@ -210,7 +210,7 @@ func createClaim( Service: &sharedtypes.Service{Id: testServiceId}, SessionId: sessionId, SessionStartBlockHeight: sessionStartHeight, - SessionEndBlockHeight: shared.GetSessionEndBlockHeight(sessionStartHeight), + SessionEndBlockHeight: shared.GetSessionEndHeightWithDefaultParams(sessionStartHeight), }, RootHash: rootHash, } diff --git a/x/proof/types/message_submit_proof_test.go b/x/proof/types/message_submit_proof_test.go index d05419e38..00558bb93 100644 --- a/x/proof/types/message_submit_proof_test.go +++ b/x/proof/types/message_submit_proof_test.go @@ -30,7 +30,7 @@ func TestMsgSubmitProof_ValidateBasic(t *testing.T) { Service: testService, SessionId: "mock_session_id", SessionStartBlockHeight: 1, - SessionEndBlockHeight: shared.GetSessionEndBlockHeight(1), + SessionEndBlockHeight: shared.GetSessionEndHeightWithDefaultParams(1), }, Proof: testClosestMerkleProof, }, @@ -49,7 +49,7 @@ func TestMsgSubmitProof_ValidateBasic(t *testing.T) { Service: testService, SessionId: "mock_session_id", SessionStartBlockHeight: 1, - SessionEndBlockHeight: shared.GetSessionEndBlockHeight(1), + SessionEndBlockHeight: shared.GetSessionEndHeightWithDefaultParams(1), }, Proof: testClosestMerkleProof, }, @@ -68,7 +68,7 @@ func TestMsgSubmitProof_ValidateBasic(t *testing.T) { Service: &sharedtypes.Service{Id: ""}, SessionId: "mock_session_id", SessionStartBlockHeight: 1, - SessionEndBlockHeight: shared.GetSessionEndBlockHeight(1), + SessionEndBlockHeight: shared.GetSessionEndHeightWithDefaultParams(1), }, Proof: testClosestMerkleProof, }, @@ -83,7 +83,7 @@ func TestMsgSubmitProof_ValidateBasic(t *testing.T) { Service: testService, SessionId: "mock_session_id", SessionStartBlockHeight: 1, - SessionEndBlockHeight: shared.GetSessionEndBlockHeight(1), + SessionEndBlockHeight: shared.GetSessionEndHeightWithDefaultParams(1), }, Proof: testClosestMerkleProof, }, diff --git a/x/session/keeper/session_hydrator.go b/x/session/keeper/session_hydrator.go index cba24a8e0..0e397ccd8 100644 --- a/x/session/keeper/session_hydrator.go +++ b/x/session/keeper/session_hydrator.go @@ -99,11 +99,14 @@ func (k Keeper) hydrateSessionMetadata(ctx context.Context, sh *sessionHydrator) ) } + // TODO_UPNEXT(#517): Refactor session module to use current on-chain shared + // parameters instead of their corresponding constant stand-ins. + sh.session.NumBlocksPerSession = shared.NumBlocksPerSession - sh.session.SessionNumber = shared.GetSessionNumber(sh.blockHeight) + sh.session.SessionNumber = shared.GetSessionNumberWithDefaultParams(sh.blockHeight) - sh.sessionHeader.SessionStartBlockHeight = shared.GetSessionStartBlockHeight(sh.blockHeight) - sh.sessionHeader.SessionEndBlockHeight = shared.GetSessionEndBlockHeight(sh.blockHeight) + sh.sessionHeader.SessionStartBlockHeight = shared.GetSessionStartHeightWithDefaultParams(sh.blockHeight) + sh.sessionHeader.SessionEndBlockHeight = shared.GetSessionEndHeightWithDefaultParams(sh.blockHeight) return nil } @@ -291,7 +294,7 @@ func GetSessionId( // getSessionStartBlockHeightBz returns the bytes representation of the session // start block height given the block height. func getSessionStartBlockHeightBz(blockHeight int64) []byte { - sessionStartBlockHeight := shared.GetSessionStartBlockHeight(blockHeight) + sessionStartBlockHeight := shared.GetSessionStartHeightWithDefaultParams(blockHeight) sessionStartBlockHeightBz := make([]byte, 8) binary.LittleEndian.PutUint64(sessionStartBlockHeightBz, uint64(sessionStartBlockHeight)) return sessionStartBlockHeightBz diff --git a/x/shared/keeper/session.go b/x/shared/keeper/session.go new file mode 100644 index 000000000..c0d414c5f --- /dev/null +++ b/x/shared/keeper/session.go @@ -0,0 +1,35 @@ +package keeper + +import ( + "context" + + "github.com/pokt-network/poktroll/x/shared" +) + +// GetSessionStartHeight returns the block height at which the session containing +// queryHeight starts, given the current shared on-chain parameters. +// Returns 0 if the block height is not a consensus produced block. +// Example: If NumBlocksPerSession == 4, sessions start at blocks 1, 5, 9, etc. +func (k Keeper) GetSessionStartHeight(ctx context.Context, queryHeight int64) int64 { + sharedParams := k.GetParams(ctx) + return shared.GetSessionStartHeight(&sharedParams, queryHeight) +} + +// GetSessionEndHeight returns the block height at which the session containing +// queryHeight ends, given the current shared on-chain parameters. +// Returns 0 if the block height is not a consensus produced block. +// Example: If NumBlocksPerSession == 4, sessions end at blocks 4, 8, 11, etc. +func (k Keeper) GetSessionEndHeight(ctx context.Context, queryHeight int64) int64 { + sharedParams := k.GetParams(ctx) + return shared.GetSessionEndHeight(&sharedParams, queryHeight) +} + +// GetSessionNumber returns the session number for the session containing queryHeight, +// given the current shared on-chain parameters. +// Returns session number 0 if the block height is not a consensus produced block. +// Returns session number 1 for block 1 to block NumBlocksPerSession - 1 (inclusive). +// i.e. If NubBlocksPerSession == 4, session == 1 for [1, 4], session == 2 for [5, 8], etc. +func (k Keeper) GetSessionNumber(ctx context.Context, queryHeight int64) int64 { + sharedParams := k.GetParams(ctx) + return shared.GetSessionNumber(&sharedParams, queryHeight) +} diff --git a/x/shared/session.go b/x/shared/session.go index 5240059a5..3b3430cf3 100644 --- a/x/shared/session.go +++ b/x/shared/session.go @@ -1,5 +1,7 @@ package shared +import sharedtypes "github.com/pokt-network/poktroll/x/shared/types" + // NumBlocksPerSession is a place-holder that will be removed once the respective governance // parameter is implemented. // @@ -14,42 +16,82 @@ const NumBlocksPerSession = 4 // governance parameter is implemented. const SessionGracePeriodBlocks = 4 -// GetSessionStartBlockHeight returns the block height at which the session starts +// GetSessionStartHeightWithDefaultParams returns the block height at which the +// session containing queryHeight starts, given the default shared on-chain +// parameters. +// See GetSessionStartHeight for more details. +// +// TODO_TECHDEBT(#517): Move this function to shared testutils. +func GetSessionStartHeightWithDefaultParams(queryHeight int64) int64 { + sharedParams := sharedtypes.DefaultParams() + return GetSessionStartHeight(&sharedParams, queryHeight) +} + +// GetSessionStartHeight returns the block height at which the session containing +// queryHeight starts, given the passed shared on-chain parameters. // Returns 0 if the block height is not a consensus produced block. // Example: If NumBlocksPerSession == 4, sessions start at blocks 1, 5, 9, etc. -func GetSessionStartBlockHeight(blockHeight int64) int64 { - if blockHeight <= 0 { +func GetSessionStartHeight(sharedParams *sharedtypes.Params, queryHeight int64) int64 { + if queryHeight <= 0 { return 0 } + numBlocksPerSession := int64(sharedParams.GetNumBlocksPerSession()) + // TODO_BLOCKER(#543): If the num_blocks_per_session param has ever been changed, // this function may cause unexpected behavior. - return blockHeight - ((blockHeight - 1) % NumBlocksPerSession) + return queryHeight - ((queryHeight - 1) % numBlocksPerSession) } -// GetSessionEndBlockHeight returns the block height at which the session ends +// GetSessionEndHeightWithDefaultParams returns the block height at which the session +// containing queryHeight ends, given the default shared on-chain parameters. +// See GetSessionEndHeight for more details. +// +// TODO_TECHDEBT(#517): Move this function to shared testutils. +func GetSessionEndHeightWithDefaultParams(queryHeight int64) int64 { + sharedParams := sharedtypes.DefaultParams() + return GetSessionEndHeight(&sharedParams, queryHeight) +} + +// GetSessionEndHeight returns the block height at which the session containing +// queryHeight ends, given the passed shared on-chain parameters. // Returns 0 if the block height is not a consensus produced block. // Example: If NumBlocksPerSession == 4, sessions end at blocks 4, 8, 11, etc. -func GetSessionEndBlockHeight(blockHeight int64) int64 { - if blockHeight <= 0 { +func GetSessionEndHeight(sharedParams *sharedtypes.Params, queryHeight int64) int64 { + if queryHeight <= 0 { return 0 } - return GetSessionStartBlockHeight(blockHeight) + NumBlocksPerSession - 1 + numBlocksPerSession := int64(sharedParams.GetNumBlocksPerSession()) + + return GetSessionStartHeight(sharedParams, queryHeight) + numBlocksPerSession - 1 +} + +// GetSessionNumberWithDefaultParams returns the session number of the session +// containing queryHeight, given the default on-chain shared parameters. +// See GetSessionNumber for more details. +// +// TODO_TECHDEBT(#517): Move this function to shared testutils. +func GetSessionNumberWithDefaultParams(queryHeight int64) int64 { + sharedParams := sharedtypes.DefaultParams() + return GetSessionNumber(&sharedParams, queryHeight) } -// GetSessionNumber returns the session number given the block height. +// GetSessionNumber returns the session number of the session containing queryHeight, +// given the passed on-chain shared parameters. // Returns session number 0 if the block height is not a consensus produced block. // Returns session number 1 for block 1 to block NumBlocksPerSession - 1 (inclusive). // i.e. If NubBlocksPerSession == 4, session == 1 for [1, 4], session == 2 for [5, 8], etc. -func GetSessionNumber(blockHeight int64) int64 { - if blockHeight <= 0 { +func GetSessionNumber(sharedParams *sharedtypes.Params, queryHeight int64) int64 { + if queryHeight <= 0 { return 0 } + numBlocksPerSession := int64(sharedParams.GetNumBlocksPerSession()) + // TODO_BLOCKER(#543): If the num_blocks_per_session param has ever been changed, // this function may cause unexpected behavior. - return ((blockHeight - 1) / NumBlocksPerSession) + 1 + return ((queryHeight - 1) / numBlocksPerSession) + 1 } // GetSessionGracePeriodEndHeight returns the block height at which the grace period diff --git a/x/tokenomics/keeper/keeper_test.go b/x/tokenomics/keeper/keeper_test.go index 3fdd0dd88..6103694a6 100644 --- a/x/tokenomics/keeper/keeper_test.go +++ b/x/tokenomics/keeper/keeper_test.go @@ -60,7 +60,7 @@ func (s *TestSuite) SetupTest() { Service: &sharedtypes.Service{Id: testServiceId}, SessionId: "session_id", SessionStartBlockHeight: 1, - SessionEndBlockHeight: shared.GetSessionEndBlockHeight(1), + SessionEndBlockHeight: shared.GetSessionEndHeightWithDefaultParams(1), }, RootHash: smstRootWithSum(69), } diff --git a/x/tokenomics/keeper/settle_session_accounting_test.go b/x/tokenomics/keeper/settle_session_accounting_test.go index 528250aa5..e1f65471b 100644 --- a/x/tokenomics/keeper/settle_session_accounting_test.go +++ b/x/tokenomics/keeper/settle_session_accounting_test.go @@ -52,7 +52,7 @@ func TestSettleSessionAccounting_HandleAppGoingIntoDebt(t *testing.T) { }, SessionId: "session_id", SessionStartBlockHeight: 1, - SessionEndBlockHeight: shared.GetSessionEndBlockHeight(1), + SessionEndBlockHeight: shared.GetSessionEndHeightWithDefaultParams(1), }, RootHash: smstRootWithSum(appStake.Amount.Uint64() + 1), // More than the app stake } @@ -93,7 +93,7 @@ func TestSettleSessionAccounting_AppNotFound(t *testing.T) { }, SessionId: "session_id", SessionStartBlockHeight: 1, - SessionEndBlockHeight: shared.GetSessionEndBlockHeight(1), + SessionEndBlockHeight: shared.GetSessionEndHeightWithDefaultParams(1), }, RootHash: smstRootWithSum(42), } @@ -297,7 +297,7 @@ func baseClaim(appAddr, supplierAddr string, sum uint64) prooftypes.Claim { }, SessionId: "session_id", SessionStartBlockHeight: 1, - SessionEndBlockHeight: shared.GetSessionEndBlockHeight(1), + SessionEndBlockHeight: shared.GetSessionEndHeightWithDefaultParams(1), }, RootHash: smstRootWithSum(sum), }