Skip to content

Commit

Permalink
[On-Chain] Refactor grace_period_end_offset_blocks as a shared mo…
Browse files Browse the repository at this point in the history
…dule param (#628)
  • Loading branch information
bryanchriswhite authored Jun 28, 2024
1 parent d1f4f42 commit e208b42
Show file tree
Hide file tree
Showing 29 changed files with 491 additions and 181 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,10 @@ params_update_shared_all: ## Update the session module params
params_update_shared_num_blocks_per_session: ## Update the shared module num_blocks_per_session param
poktrolld tx authz exec ./tools/scripts/params/shared_num_blocks_per_session.json $(PARAM_FLAGS)

.PHONY: params_update_shared_grace_period_end_offset_blocks
params_update_shared_grace_period_end_offset_blocks: ## Update the shared module grace_period_end_offset_blocks param
poktrolld tx authz exec ./tools/scripts/params/shared_grace_period_end_offset_blocks.json $(PARAM_FLAGS)

.PHONY: params_update_shared_claim_window_open_offset_blocks
params_update_shared_claim_window_open_offset_blocks: ## Update the shared module claim_window_open_offset_blocks param
poktrolld tx authz exec ./tools/scripts/params/shared_claim_window_open_offset_blocks.json $(PARAM_FLAGS)
Expand Down
170 changes: 118 additions & 52 deletions api/poktroll/shared/params.pulsar.go

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,8 @@ genesis:
shared:
params:
num_blocks_per_session: 4
claim_window_open_offset_blocks: 0
grace_period_end_offset_blocks: 1
claim_window_open_offset_blocks: 1
claim_window_close_offset_blocks: 4
proof_window_open_offset_blocks: 0
proof_window_close_offset_blocks: 4
2 changes: 2 additions & 0 deletions e2e/tests/parse_params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ func (s *suite) newSharedMsgUpdateParams(params paramsMap) cosmostypes.Msg {
switch paramName {
case sharedtypes.ParamNumBlocksPerSession:
msgUpdateParams.Params.NumBlocksPerSession = uint64(paramValue.value.(int64))
case sharedtypes.ParamGracePeriodEndOffsetBlocks:
msgUpdateParams.Params.GracePeriodEndOffsetBlocks = uint64(paramValue.value.(int64))
case sharedtypes.ParamClaimWindowOpenOffsetBlocks:
msgUpdateParams.Params.ClaimWindowOpenOffsetBlocks = uint64(paramValue.value.(int64))
case sharedtypes.ParamClaimWindowCloseOffsetBlocks:
Expand Down
2 changes: 2 additions & 0 deletions e2e/tests/update_params.feature
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ Feature: Params Namespace
When the "pnf" account sends an authz exec message to update all "shared" module params
| name | value | type |
| num_blocks_per_session | 8 | int64 |
| grace_period_end_offset_blocks | 3 | int64 |
| claim_window_open_offset_blocks | 8 | int64 |
| claim_window_close_offset_blocks | 8 | int64 |
| proof_window_open_offset_blocks | 8 | int64 |
Expand All @@ -78,6 +79,7 @@ Feature: Params Namespace
| proof | /poktroll.proof.MsgUpdateParam | proof_requirement_threshold | 100 | int64 |
| proof | /poktroll.proof.MsgUpdateParam | proof_missing_penalty | 500 | coin |
| shared | /poktroll.shared.MsgUpdateParam | num_blocks_per_session | 8 | int64 |
| shared | /poktroll.shared.MsgUpdateParam | grace_period_end_offset_blocks | 3 | int64 |
| shared | /poktroll.shared.MsgUpdateParam | claim_window_open_offset_blocks | 8 | int64 |
| shared | /poktroll.shared.MsgUpdateParam | claim_window_close_offset_blocks | 8 | int64 |
| shared | /poktroll.shared.MsgUpdateParam | proof_window_open_offset_blocks | 8 | int64 |
Expand Down
5 changes: 5 additions & 0 deletions e2e/tests/update_params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,11 @@ func (s *suite) assertExpectedModuleParamsUpdated(moduleName string) {
params.NumBlocksPerSession = uint64(numBlocksPerSessionParam.value.(int64))
}

gracePeriodEndOffsetBlocksParam, ok := paramsMap[sharedtypes.ParamGracePeriodEndOffsetBlocks]
if ok {
params.GracePeriodEndOffsetBlocks = uint64(gracePeriodEndOffsetBlocksParam.value.(int64))
}

claimWindowOpenOffsetBlocksParam, ok := paramsMap[sharedtypes.ParamClaimWindowOpenOffsetBlocks]
if ok {
params.ClaimWindowOpenOffsetBlocks = uint64(claimWindowOpenOffsetBlocksParam.value.(int64))
Expand Down
3 changes: 1 addition & 2 deletions load-testing/tests/relays_stress_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ import (
"github.com/pokt-network/poktroll/testutil/testclient/testeventsquery"
apptypes "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"
suppliertypes "github.com/pokt-network/poktroll/x/supplier/types"
)
Expand Down Expand Up @@ -377,7 +376,7 @@ func (plans *actorLoadTestIncrementPlans) totalDurationBlocks(sharedParams *shar
// last increment duration (i.e. **after** maxActorCount actors are activated).
blocksToLastSessionEnd := plans.maxActorBlocksToFinalIncrementEnd()

blocksToLastProofWindowEnd := blocksToLastSessionEnd + shared.SessionGracePeriodBlocks
blocksToLastProofWindowEnd := blocksToLastSessionEnd + int64(sharedParams.GetGracePeriodEndOffsetBlocks())

// Add one session length so that the duration is inclusive of the block which
// commits the last session's proof.
Expand Down
2 changes: 2 additions & 0 deletions pkg/client/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,8 @@ type SharedQueryClient interface {
GetParams(ctx context.Context) (*sharedtypes.Params, error)
// GetSessionGracePeriodEndHeight returns the block height at which the grace period
// for the session that includes queryHeight elapses.
// The grace period is the number of blocks after the session ends during which relays
// SHOULD be included in the session which most recently ended.
GetSessionGracePeriodEndHeight(ctx context.Context, queryHeight int64) (int64, error)
// GetClaimWindowOpenHeight returns the block height at which the claim window of
// the session that includes queryHeight opens.
Expand Down
2 changes: 2 additions & 0 deletions pkg/client/query/sharedquerier.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ func (sq *sharedQuerier) GetProofWindowOpenHeight(ctx context.Context, queryHeig

// GetSessionGracePeriodEndHeight returns the block height at which the grace period
// for the session which includes queryHeight elapses.
// The grace period is the number of blocks after the session ends during which relays
// SHOULD be included in the session which most recently ended.
//
// TODO_TECHDEBT(#543): We don't really want to have to query the params for every method call.
// Once `ModuleParamsClient` is implemented, use its replay observable's `#Last()` method
Expand Down
13 changes: 8 additions & 5 deletions pkg/relayer/proxy/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,17 +346,20 @@ func TestRelayerProxy_NonConfiguredSupplierServices(t *testing.T) {

// Test different RelayRequest scenarios
func TestRelayerProxy_Relays(t *testing.T) {

sharedParams := sharedtypes.DefaultParams()

sessionTwoStartHeight := shared.GetSessionEndHeight(&sharedParams, blockHeight) + 1

// blockOutsideSessionGracePeriod is the block height that is after the first
// 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 +
sharedtypes.DefaultNumBlocksPerSession +
shared.SessionGracePeriodBlocks)
// meaning a relay should be handled as part of the session two AND NOT session one.
blockOutsideSessionGracePeriod := shared.GetSessionGracePeriodEndHeight(&sharedParams, sessionTwoStartHeight)

// blockWithinSessionGracePeriod is the block height that is after the first
// session but within its session's grace period, meaning a relay should be
// handled at this block height.
blockWithinSessionGracePeriod := int64(blockHeight + shared.SessionGracePeriodBlocks)
blockWithinSessionGracePeriod := shared.GetSessionGracePeriodEndHeight(&sharedParams, blockHeight) - 1

tests := []struct {
desc string
Expand Down
13 changes: 9 additions & 4 deletions proto/poktroll/shared/params.proto
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,21 @@ message Params {

// num_blocks_per_session is the number of blocks between the session start & end heights.
uint64 num_blocks_per_session = 1 [(gogoproto.jsontag) = "num_blocks_per_session"];
// grace_period_end_offset_blocks is the number of blocks after the session end height,
// at which the grace period ends.
// Suppliers will get paid for relays serviced during the grace period but will need to recreate a claim if
// a previous one was already submitted.
uint64 grace_period_end_offset_blocks = 2 [(gogoproto.jsontag) = "grace_period_end_offset_blocks"];
// claim_window_open_offset_blocks is the number of blocks after the session grace
// period height, at which the claim window opens.
uint64 claim_window_open_offset_blocks = 2 [(gogoproto.jsontag) = "claim_window_open_offset_blocks"];
uint64 claim_window_open_offset_blocks = 3 [(gogoproto.jsontag) = "claim_window_open_offset_blocks"];
// claim_window_close_offset_blocks is the number of blocks after the claim window
// open height, at which the claim window closes.
uint64 claim_window_close_offset_blocks = 3 [(gogoproto.jsontag) = "claim_window_close_offset_blocks"];
uint64 claim_window_close_offset_blocks = 4 [(gogoproto.jsontag) = "claim_window_close_offset_blocks"];
// proof_window_open_offset_blocks is the number of blocks after the claim window
// close height, at which the proof window opens.
uint64 proof_window_open_offset_blocks = 4 [(gogoproto.jsontag) = "proof_window_open_offset_blocks"];
uint64 proof_window_open_offset_blocks = 5 [(gogoproto.jsontag) = "proof_window_open_offset_blocks"];
// proof_window_close_offset_blocks is the number of blocks after the proof window
// open height, at which the proof window closes.
uint64 proof_window_close_offset_blocks = 5 [(gogoproto.jsontag) = "proof_window_close_offset_blocks"];
uint64 proof_window_close_offset_blocks = 6 [(gogoproto.jsontag) = "proof_window_close_offset_blocks"];
}
22 changes: 17 additions & 5 deletions tests/integration/tokenomics/relay_mining_difficulty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

"github.com/pokt-network/poktroll/cmd/poktrolld/cmd"
testutilevents "github.com/pokt-network/poktroll/testutil/events"
integration "github.com/pokt-network/poktroll/testutil/integration"
"github.com/pokt-network/poktroll/testutil/integration"
testutil "github.com/pokt-network/poktroll/testutil/integration"
"github.com/pokt-network/poktroll/testutil/testrelayer"
prooftypes "github.com/pokt-network/poktroll/x/proof/types"
Expand All @@ -28,6 +28,8 @@ func init() {
}

func TestUpdateRelayMiningDifficulty_NewServiceSeenForTheFirstTime(t *testing.T) {
var claimWindowOpenBlockHash, proofWindowOpenBlockHash []byte

// Create a new integration app
integrationApp := integration.NewCompleteIntegrationApp(t)

Expand All @@ -44,13 +46,23 @@ func TestUpdateRelayMiningDifficulty_NewServiceSeenForTheFirstTime(t *testing.T)
// Compute the number of blocks to wait between different events
// TODO_BLOCKER(@bryanchriswhite): See this comment: https://github.com/pokt-network/poktroll/pull/610#discussion_r1645777322
sessionEndHeight := session.Header.SessionEndBlockHeight
claimWindowOpenHeight := shared.GetClaimWindowOpenHeight(&sharedParams, sessionEndHeight)
proofWindowOpenHeight := shared.GetProofWindowOpenHeight(&sharedParams, sessionEndHeight)
earliestSupplierClaimCommitHeight := shared.GetEarliestSupplierClaimCommitHeight(
&sharedParams,
sessionEndHeight,
claimWindowOpenBlockHash,
integrationApp.DefaultSupplier.GetAddress(),
)
earliestSupplierProofCommitHeight := shared.GetEarliestSupplierProofCommitHeight(
&sharedParams,
sessionEndHeight,
proofWindowOpenBlockHash,
integrationApp.DefaultSupplier.GetAddress(),
)
proofWindowCloseHeight := shared.GetProofWindowCloseHeight(&sharedParams, sessionEndHeight)

// Wait until the earliest claim commit height.
currentBlockHeight := integrationApp.GetSdkCtx().BlockHeight()
numBlocksUntilClaimWindowOpenHeight := claimWindowOpenHeight - currentBlockHeight
numBlocksUntilClaimWindowOpenHeight := earliestSupplierClaimCommitHeight - currentBlockHeight
require.Greater(t, numBlocksUntilClaimWindowOpenHeight, int64(0), "unexpected non-positive number of blocks until the earliest claim commit height")
integrationApp.NextBlocks(t, int(numBlocksUntilClaimWindowOpenHeight))

Expand All @@ -69,7 +81,7 @@ func TestUpdateRelayMiningDifficulty_NewServiceSeenForTheFirstTime(t *testing.T)

// Wait until the proof window is open
currentBlockHeight = integrationApp.GetSdkCtx().BlockHeight()
numBlocksUntilProofWindowOpenHeight := proofWindowOpenHeight - currentBlockHeight
numBlocksUntilProofWindowOpenHeight := earliestSupplierProofCommitHeight - currentBlockHeight
require.Greater(t, numBlocksUntilProofWindowOpenHeight, int64(0), "unexpected non-positive number of blocks until the earliest proof commit height")
integrationApp.NextBlocks(t, int(numBlocksUntilProofWindowOpenHeight))

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

import (
"reflect"
"testing"

"github.com/stretchr/testify/require"
)

// AssertDefaultParamsEqualExceptFields asserts that the expectedParams and
// actualParams are equal except for the fields specified in exceptFields.
// expectedParams and actualParams MUST be reference types (e.g. pionters).
func AssertDefaultParamsEqualExceptFields[P any](
t *testing.T,
expectedParams P,
actualParams P,
exceptFields ...string,
) {
expectedParamsValue := reflect.ValueOf(expectedParams).Elem()
actualParamsValue := reflect.ValueOf(actualParams).Elem()

for fieldIdx := 0; fieldIdx < expectedParamsValue.NumField(); fieldIdx++ {
fieldName := expectedParamsValue.Type().Field(fieldIdx).Name
// Skip all fields in the exceptFields list.
if isFieldException(fieldName, exceptFields) {
continue
}

require.Equal(t,
expectedParamsValue.FieldByName(fieldName).Interface(),
actualParamsValue.FieldByName(fieldName).Interface(),
)
}
}

func isFieldException(fieldName string, exceptFields []string) bool {
for _, exceptField := range exceptFields {
if exceptField == fieldName {
return true
}
}
return false
}
7 changes: 5 additions & 2 deletions tools/scripts/params/shared_all.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
"authority": "pokt10d07y265gmmuvt4z0w9aw880jnsr700j8yv32t",
"params": {
"num_blocks_per_session": "4",
"claim_window_open_offset_blocks": "0",
"claim_window_close_offset_blocks": "4"
"grace_period_end_offset_blocks": "1",
"claim_window_open_offset_blocks": "1",
"claim_window_close_offset_blocks": "4",
"proof_window_open_offset_blocks": "0",
"proof_window_close_offset_blocks": "4"
}
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"@type": "/poktroll.shared.MsgUpdateParam",
"authority": "pokt10d07y265gmmuvt4z0w9aw880jnsr700j8yv32t",
"name": "claim_window_open_offset_blocks",
"as_int64": "0"
"as_int64": "1"
}
]
}
Expand Down
12 changes: 12 additions & 0 deletions tools/scripts/params/shared_grace_period_end_offset_blocks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"body": {
"messages": [
{
"@type": "/poktroll.shared.MsgUpdateParam",
"authority": "pokt10d07y265gmmuvt4z0w9aw880jnsr700j8yv32t",
"name": "grace_period_end_offset_blocks",
"as_int64": "1"
}
]
}
}
13 changes: 7 additions & 6 deletions x/application/keeper/msg_server_undelegate_from_gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,9 +318,9 @@ 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(testsession.GetSessionEndHeightWithDefaultParams(undelegationHeight))
sessionEndHeight := testsession.GetSessionEndHeightWithDefaultParams(undelegationHeight)
require.Contains(t,
app.PendingUndelegations[sessionEndHeight].GatewayAddresses,
app.PendingUndelegations[uint64(sessionEndHeight)].GatewayAddresses,
pendingUndelegateFromAddr,
)

Expand All @@ -333,7 +333,7 @@ func TestMsgServer_UndelegateFromGateway_DelegationIsActiveUntilNextSession(t *t

// Increment the block height to the next session and run the pruning
// undelegations logic again.
nextSessionStartHeight := int64(sessionEndHeight + 1)
nextSessionStartHeight := sessionEndHeight + 1
sdkCtx = sdkCtx.WithBlockHeight(nextSessionStartHeight)
k.EndBlockerPruneAppToGatewayPendingUndelegation(sdkCtx)

Expand All @@ -349,13 +349,14 @@ func TestMsgServer_UndelegateFromGateway_DelegationIsActiveUntilNextSession(t *t

// Increment the block height past the tested session's grace period and run
// the pruning undelegations logic again.
afterSessionGracePeriodHeight := int64(sessionEndHeight + shared.SessionGracePeriodBlocks + 1)
sdkCtx = sdkCtx.WithBlockHeight(afterSessionGracePeriodHeight)
sharedParams := sharedtypes.DefaultParams()
afterSessionGracePeriodEndHeight := shared.GetSessionGracePeriodEndHeight(&sharedParams, sessionEndHeight) + 1
sdkCtx = sdkCtx.WithBlockHeight(afterSessionGracePeriodEndHeight)
k.EndBlockerPruneAppToGatewayPendingUndelegation(sdkCtx)

// Verify that when queried for a block height past the tested session's grace period,
// the reconstructed delegatee gateway list does not include the undelegated gateway.
pastGracePeriodGatewayAddresses := getRingAddressesAtBlockWithDefaultParams(&app, afterSessionGracePeriodHeight)
pastGracePeriodGatewayAddresses := getRingAddressesAtBlockWithDefaultParams(&app, afterSessionGracePeriodEndHeight)
require.NotContains(t, pastGracePeriodGatewayAddresses, pendingUndelegateFromAddr)

// Ensure that when queried for the block height corresponding to the session
Expand Down
3 changes: 1 addition & 2 deletions x/application/keeper/prune_undelegations.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

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

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

Expand Down Expand Up @@ -59,6 +58,6 @@ func (k Keeper) GetNumBlocksUndelegationRetention(ctx context.Context) int64 {
func GetNumBlocksUndelegationRetention(sharedParams *sharedtypes.Params) int64 {
numBlocksPerSession := int64(sharedParams.GetNumBlocksPerSession())

return shared.SessionGracePeriodBlocks +
return int64(sharedParams.GetGracePeriodEndOffsetBlocks()) +
(numBlocksPerSession * NumSessionsAppToGatewayUndelegationRetention)
}
25 changes: 18 additions & 7 deletions x/proof/keeper/msg_server_submit_proof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1321,17 +1321,28 @@ func createClaimAndStoreBlockHash(
_, err = msgServer.CreateClaim(ctx, claimMsg)
require.NoError(t, err)

// TODO_TECHDEBT(@red-0ne): Centralize the business logic that involves taking
// into account the heights, windows and grace periods into helper functions.
proofSubmissionHeight :=
claimMsg.GetSessionHeader().GetSessionEndBlockHeight() +
shared.SessionGracePeriodBlocks
sharedParams := keepers.SharedKeeper.GetParams(ctx)

claimWindowOpenHeight := shared.GetClaimWindowOpenHeight(
&sharedParams,
sessionStartHeight,
)

ctx = keepertest.SetBlockHeight(ctx, claimWindowOpenHeight)
sdkCtx := cosmostypes.UnwrapSDKContext(ctx)

earliestSupplierClaimCommitHeight := shared.GetEarliestSupplierClaimCommitHeight(
&sharedParams,
sessionStartHeight,
sdkCtx.HeaderHash(),
supplierAddr,
)

// Set block height to be after the session grace period.
blockHeightCtx := keepertest.SetBlockHeight(ctx, proofSubmissionHeight)
earliestSupplierClaimCommitCtx := keepertest.SetBlockHeight(ctx, earliestSupplierClaimCommitHeight)

// Store the current context's block hash for future height, which is currently an EndBlocker operation.
keepers.StoreBlockHash(blockHeightCtx)
keepers.StoreBlockHash(earliestSupplierClaimCommitCtx)
}

// getClosestRelayDifficultyBits returns the number of leading 0s (i.e. relay
Expand Down
Loading

0 comments on commit e208b42

Please sign in to comment.