Skip to content

Commit

Permalink
chore: review improvments
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanchriswhite committed Jun 26, 2024
1 parent cb76263 commit bfa1d34
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 3 deletions.
32 changes: 29 additions & 3 deletions x/shared/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
}
76 changes: 76 additions & 0 deletions x/shared/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
})
}
}

0 comments on commit bfa1d34

Please sign in to comment.