-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[TODO] refactor: query expiring claims w/ index #671
Changes from 5 commits
2307ef7
392504e
e636363
5f477b5
47b84bd
a2802a5
4bc2bb6
f488e0e
60bd91b
84226fe
98284fa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ import ( | |
"fmt" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/types/query" | ||
|
||
poktrand "github.com/pokt-network/poktroll/pkg/crypto/rand" | ||
"github.com/pokt-network/poktroll/telemetry" | ||
|
@@ -27,10 +28,10 @@ func (k Keeper) SettlePendingClaims(ctx sdk.Context) ( | |
) { | ||
logger := k.Logger().With("method", "SettlePendingClaims") | ||
|
||
// TODO_BLOCKER(@Olshansk): Optimize this by indexing expiringClaims appropriately | ||
// and only retrieving the expiringClaims that need to be settled rather than all | ||
// of them and iterating through them one by one. | ||
expiringClaims := k.getExpiringClaims(ctx) | ||
expiringClaims, err := k.getExpiringClaims(ctx) | ||
if err != nil { | ||
return 0, 0, relaysPerServiceMap, computeUnitsPerServiceMap, err | ||
} | ||
|
||
blockHeight := ctx.BlockHeight() | ||
|
||
|
@@ -160,25 +161,49 @@ func (k Keeper) SettlePendingClaims(ctx sdk.Context) ( | |
// This is the height at which the proof window closes. | ||
// If the proof window closes and a proof IS NOT required -> settle the claim. | ||
// If the proof window closes and a proof IS required -> only settle it if a proof is available. | ||
func (k Keeper) getExpiringClaims(ctx sdk.Context) (expiringClaims []prooftypes.Claim) { | ||
func (k Keeper) getExpiringClaims(ctx sdk.Context) (expiringClaims []prooftypes.Claim, err error) { | ||
blockHeight := ctx.BlockHeight() | ||
|
||
// TODO_TECHDEBT: Optimize this by indexing claims appropriately | ||
// and only retrieving the claims that need to be settled rather than all | ||
// of them and iterating through them one by one. | ||
claims := k.proofKeeper.GetAllClaims(ctx) | ||
// NB: This error can be safely ignored as on-chain SharedQueryClient implementation cannot return an error. | ||
sharedParams, _ := k.sharedQuerier.GetParams(ctx) | ||
claimWindowSizeBlocks := sharedParams.GetClaimWindowCloseOffsetBlocks() | ||
proofWindowSizeBlocks := sharedParams.GetProofWindowCloseOffsetBlocks() | ||
|
||
previousSessionEndHeight := blockHeight - | ||
bryanchriswhite marked this conversation as resolved.
Show resolved
Hide resolved
|
||
int64(sharedParams.GetGracePeriodEndOffsetBlocks()+ | ||
bryanchriswhite marked this conversation as resolved.
Show resolved
Hide resolved
|
||
claimWindowSizeBlocks+ | ||
proofWindowSizeBlocks+1) | ||
|
||
allClaims := k.proofKeeper.GetAllClaims(ctx) | ||
_ = allClaims | ||
|
||
for { | ||
claimsRes, err := k.proofKeeper.AllClaims(ctx, &prooftypes.QueryAllClaimsRequest{ | ||
Pagination: &query.PageRequest{ | ||
Offset: uint64(len(expiringClaims)), | ||
bryanchriswhite marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
Filter: &prooftypes.QueryAllClaimsRequest_SessionEndHeight{ | ||
SessionEndHeight: uint64(previousSessionEndHeight), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wouldn't it be easier to just use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure but at the moment we don't have any such index; we have: oneof filter {
string supplier_address= 2;
string session_id = 3;
uint64 session_end_height = 4;
} Even if we had a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe. My concern is about the potential case where the session we want to settle is n-2 or older (relative to the current session). If claim and proof window offsets do not allow that, then we should be fine, Otherwise we should consider other means to retrieve the session we want to settle. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method will be called for each block, progressively "draining" all expiring claims as the current height climbs. Since this is part of consensus, all sessions considered here MUST be from the same, most recently expiring session number. |
||
}, | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Loop over all claims we need to check for expiration | ||
for _, claim := range claims { | ||
claimSessionStartHeight := claim.GetSessionHeader().GetSessionStartBlockHeight() | ||
expirationHeight := k.sharedKeeper.GetProofWindowCloseHeight(ctx, claimSessionStartHeight) | ||
if blockHeight >= expirationHeight { | ||
for _, claim := range claimsRes.GetClaims() { | ||
expiringClaims = append(expiringClaims, claim) | ||
} | ||
|
||
// Continue if there are more claims to fetch. | ||
if claimsRes.Pagination.GetNextKey() != nil { | ||
continue | ||
} | ||
|
||
break | ||
} | ||
|
||
// Return the actually expiring claims | ||
return expiringClaims | ||
return expiringClaims, nil | ||
} | ||
|
||
// proofRequirementForClaim checks if a proof is required for a claim. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ensure
relaysPerServiceMap
andcomputeUnitsPerServiceMap
are initialized before use.The variables
relaysPerServiceMap
andcomputeUnitsPerServiceMap
should be initialized before being used in the return statement to avoid potentialnil
map issues.Committable suggestion