diff --git a/x/shared/session.go b/x/shared/session.go index 1ab6dc396..deba0806b 100644 --- a/x/shared/session.go +++ b/x/shared/session.go @@ -5,6 +5,11 @@ import ( sharedtypes "github.com/pokt-network/poktroll/x/shared/types" ) +const ( + minimumClaimWindowSizeBlocks = 1 + minimumProofWindowSizeBlocks = 1 +) + // TODO_DOCUMENT(@bryanchriswhite): Move this into the documentation: https://github.com/pokt-network/poktroll/pull/571#discussion_r1630923625 // SessionGracePeriodBlocks is the number of blocks after the session ends before the @@ -132,8 +137,15 @@ func GetEarliestClaimCommitHeight( // GetClaimWindowSizeBlocks returns the number of blocks between the opening and closing // of the claim window, given the passed sharedParams. func GetClaimWindowSizeBlocks(sharedParams *sharedtypes.Params) uint64 { - return sharedParams.ClaimWindowCloseOffsetBlocks - - sharedParams.ClaimWindowOpenOffsetBlocks + windowSizeBlocks := sharedParams.ClaimWindowCloseOffsetBlocks - + sharedParams.ClaimWindowOpenOffsetBlocks - + minimumClaimWindowSizeBlocks + + if windowSizeBlocks < 1 { + return 1 + } + + return windowSizeBlocks } // GetEarliestProofCommitHeight returns the earliest block height at which a proof @@ -151,8 +163,22 @@ func GetEarliestProofCommitHeight( // window open block hash and the supplier address. randomNumber := poktrand.SeededInt63(proofWindowOpenBlockHash, []byte(supplierAddr)) - distributionWindowSizeBlocks := sharedParams.ProofWindowCloseOffsetBlocks - sharedParams.ProofWindowOpenOffsetBlocks + distributionWindowSizeBlocks := GetProofWindowSizeBlocks(sharedParams) randCreateProofHeightOffset := randomNumber % int64(distributionWindowSizeBlocks) return proofWindowOpenHeight + randCreateProofHeightOffset } + +// GetProofWindowSizeBlocks returns the number of blocks between the opening and closing +// of the proof window, given the passed sharedParams. +func GetProofWindowSizeBlocks(sharedParams *sharedtypes.Params) uint64 { + windowSizeBlocks := sharedParams.ProofWindowCloseOffsetBlocks - + sharedParams.ProofWindowOpenOffsetBlocks - + minimumProofWindowSizeBlocks + + if windowSizeBlocks < 1 { + return 1 + } + + return windowSizeBlocks +} diff --git a/x/shared/session_test.go b/x/shared/session_test.go index 65c511843..0ff52ffa1 100644 --- a/x/shared/session_test.go +++ b/x/shared/session_test.go @@ -79,3 +79,79 @@ func TestGetEarliestProofCommitHeight_IsDeterministic(t *testing.T) { } } } + +func TestClaimProofWindows(t *testing.T) { + var blockHash []byte + + // NB: arbitrary sample size intended to be large enough to + sampleSize := 15000 + + tests := []struct { + desc string + sharedParams sharedtypes.Params + queryHeight int64 + }{ + { + desc: "default params", + sharedParams: sharedtypes.DefaultParams(), + queryHeight: int64(1), + }, + { + desc: "minimal windows", + sharedParams: sharedtypes.Params{ + NumBlocksPerSession: 1, + ClaimWindowOpenOffsetBlocks: 0, + ClaimWindowCloseOffsetBlocks: 1, + ProofWindowOpenOffsetBlocks: 0, + ProofWindowCloseOffsetBlocks: 1, + }, + queryHeight: int64(1), + }, + } + + for _, test := range tests { + t.Run(test.desc, func(t *testing.T) { + for i := 0; i < sampleSize; i++ { + // Randomize the supplier address for each sample. + // This will produce different randomized earliest claim & proof offsets. + supplierAddr := sample.AccAddress() + + claimWindowOpenHeight := GetClaimWindowOpenHeight(&test.sharedParams, test.queryHeight) + claimWindowCloseHeight := GetClaimWindowCloseHeight(&test.sharedParams, test.queryHeight) + + require.Greater(t, claimWindowCloseHeight, claimWindowOpenHeight) + + proofWindowOpenHeight := GetProofWindowOpenHeight(&test.sharedParams, test.queryHeight) + proofWindowCloseHeight := GetProofWindowCloseHeight(&test.sharedParams, test.queryHeight) + + require.GreaterOrEqual(t, proofWindowOpenHeight, claimWindowCloseHeight) + require.Greater(t, proofWindowCloseHeight, proofWindowOpenHeight) + + earliestClaimCommitHeight := GetEarliestClaimCommitHeight( + &test.sharedParams, + test.queryHeight, + blockHash, + supplierAddr, + ) + + require.Greater(t, claimWindowCloseHeight, earliestClaimCommitHeight) + + earliestProofCommitHeight := GetEarliestProofCommitHeight( + &test.sharedParams, + test.queryHeight, + blockHash, + supplierAddr, + ) + + require.GreaterOrEqual(t, earliestProofCommitHeight, claimWindowCloseHeight) + require.Greater(t, proofWindowCloseHeight, earliestProofCommitHeight) + + claimWindowSizeBlocks := GetClaimWindowSizeBlocks(&test.sharedParams) + require.Greater(t, claimWindowSizeBlocks, uint64(0)) + + proofWindowSizeBlocks := GetProofWindowSizeBlocks(&test.sharedParams) + require.Greater(t, proofWindowSizeBlocks, uint64(0)) + } + }) + } +}