-
Notifications
You must be signed in to change notification settings - Fork 208
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
feat: CNS-daily restaking credit #1794
Open
omerlavanet
wants to merge
20
commits into
main
Choose a base branch
from
CNS-daily-delegation-limit
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+836
−74
Open
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
8c31c08
added credit and credit timestamp to delegations
omerlavanet 8ba7e34
fix nil deref and edge cases where delegation is in the future
omerlavanet 7c3f1a7
added delegation credit to rewards distribution
omerlavanet 2b56061
fix delegate set to account for existing delegation
omerlavanet c2fc7bc
wip on tests
omerlavanet 212c6c4
unitests wip
omerlavanet f986516
normalized provider credit too if he wasnt staked for a month, finish…
omerlavanet 00dae9f
lint
omerlavanet 3bee5ed
adding unitests
omerlavanet fe504f3
fix ctx
omerlavanet fa2c139
finished delegation set with credit unitests
omerlavanet 610fe47
wip fixing tests
omerlavanet 3b4d5b0
fix a test
omerlavanet 71c8ae2
fix last test
omerlavanet 5a2803e
add unitest - wip
omerlavanet 88b0444
push unitests wip
omerlavanet 8ad8b1a
finished delegation partial test
omerlavanet 131ea6c
adapted new sub tests to trigger only on the new flow
omerlavanet b90718b
Merge remote-tracking branch 'origin/main' into CNS-daily-delegation-…
omerlavanet 8f3ee81
fix date add bug using months instead of days
omerlavanet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package keeper | ||
|
||
// Delegation allows securing funds for a specific provider to effectively increase | ||
// its stake so it will be paired with consumers more often. The delegators do not | ||
// transfer the funds to the provider but only bestow the funds with it. In return | ||
// to locking the funds there, delegators get some of the provider’s profit (after | ||
// commission deduction). | ||
// | ||
// The delegated funds are stored in the module's BondedPoolName account. On request | ||
// to terminate the delegation, they are then moved to the modules NotBondedPoolName | ||
// account, and remain locked there for staking.UnbondingTime() witholding period | ||
// before finally released back to the delegator. The timers for bonded funds are | ||
// tracked are indexed by the delegator, provider, and chainID. | ||
// | ||
// The delegation state is stores with fixation using two maps: one for delegations | ||
// indexed by the combination <provider,chainD,delegator>, used to track delegations | ||
// and find/access delegations by provider (and chainID); and another for delegators | ||
// tracking the list of providers for a delegator, indexed by the delegator. | ||
|
||
import ( | ||
"time" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/lavanet/lava/v4/x/dualstaking/types" | ||
) | ||
|
||
const ( | ||
monthHours = 720 // 30 days * 24 hours | ||
hourSeconds = 3600 | ||
) | ||
|
||
// calculate the delegation credit based on the timestamps, and the amounts of delegations | ||
// amounts and credits represent daily value, rounded down | ||
// can be used to calculate the credit for distribution or update the credit fields in the delegation | ||
func (k Keeper) CalculateCredit(ctx sdk.Context, delegation types.Delegation) (credit sdk.Coin, creditTimestampRet int64) { | ||
// Calculate the credit for the delegation | ||
currentAmount := delegation.Amount | ||
creditAmount := delegation.Credit | ||
// handle uninitialized amounts | ||
if creditAmount.IsNil() { | ||
creditAmount = sdk.NewCoin(k.stakingKeeper.BondDenom(ctx), sdk.ZeroInt()) | ||
} | ||
if currentAmount.IsNil() { | ||
// this should never happen, but we handle it just in case | ||
currentAmount = sdk.NewCoin(k.stakingKeeper.BondDenom(ctx), sdk.ZeroInt()) | ||
} | ||
currentTimestamp := ctx.BlockTime().UTC() | ||
delegationTimestamp := time.Unix(delegation.Timestamp, 0) | ||
creditTimestamp := time.Unix(delegation.CreditTimestamp, 0) | ||
// we normalize dates before we start the calculation | ||
// maximum scope is 30 days, we start with the delegation truncation then the credit | ||
monthAgo := currentTimestamp.AddDate(0, 0, -30) // we are doing 30 days not a month a month can be a different amount of days | ||
if monthAgo.After(delegationTimestamp) { | ||
// in the case the delegation wasn't changed for 30 days or more we truncate the timestamp to 30 days ago | ||
// and disable the credit for older dates since they are irrelevant | ||
delegationTimestamp = monthAgo | ||
creditTimestamp = delegationTimestamp | ||
creditAmount = sdk.NewCoin(k.stakingKeeper.BondDenom(ctx), sdk.ZeroInt()) | ||
} else if monthAgo.After(creditTimestamp) { | ||
// delegation is less than 30 days, but credit might be older, so truncate it to 30 days | ||
creditTimestamp = monthAgo | ||
Yaroms marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
creditDelta := int64(0) // hours | ||
if delegation.CreditTimestamp == 0 || creditAmount.IsZero() { | ||
Yaroms marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// in case credit was never set, we set it to the delegation timestamp | ||
creditTimestamp = delegationTimestamp | ||
} else if creditTimestamp.Before(delegationTimestamp) { | ||
// calculate the credit delta in hours | ||
creditDelta = (delegationTimestamp.Unix() - creditTimestamp.Unix()) / hourSeconds | ||
} | ||
|
||
amountDelta := int64(0) // hours | ||
if !currentAmount.IsZero() && delegationTimestamp.Before(currentTimestamp) { | ||
amountDelta = (currentTimestamp.Unix() - delegationTimestamp.Unix()) / hourSeconds | ||
} | ||
|
||
// creditDelta is the weight of the history and amountDelta is the weight of the current amount | ||
// we need to average them and store it in the credit | ||
totalDelta := creditDelta + amountDelta | ||
if totalDelta == 0 { | ||
return sdk.NewCoin(k.stakingKeeper.BondDenom(ctx), sdk.ZeroInt()), currentTimestamp.Unix() | ||
} | ||
credit = sdk.NewCoin(k.stakingKeeper.BondDenom(ctx), currentAmount.Amount.MulRaw(amountDelta).Add(creditAmount.Amount.MulRaw(creditDelta)).QuoRaw(totalDelta)) | ||
return credit, creditTimestamp.Unix() | ||
} | ||
|
||
// this function takes the delegation and returns it's credit within the last 30 days | ||
func (k Keeper) CalculateMonthlyCredit(ctx sdk.Context, delegation types.Delegation) (credit sdk.Coin) { | ||
credit, creditTimeEpoch := k.CalculateCredit(ctx, delegation) | ||
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. why is it called "creditTimeEpoch"? |
||
if credit.IsNil() || credit.IsZero() || creditTimeEpoch <= 0 { | ||
return sdk.NewCoin(k.stakingKeeper.BondDenom(ctx), sdk.ZeroInt()) | ||
} | ||
creditTimestamp := time.Unix(creditTimeEpoch, 0) | ||
timeStampDiff := (ctx.BlockTime().UTC().Unix() - creditTimestamp.Unix()) / hourSeconds | ||
if timeStampDiff <= 0 { | ||
// no positive credit | ||
return sdk.NewCoin(k.stakingKeeper.BondDenom(ctx), sdk.ZeroInt()) | ||
} | ||
// make sure we never increase the credit | ||
if timeStampDiff > monthHours { | ||
timeStampDiff = monthHours | ||
} | ||
// normalize credit to 30 days | ||
credit.Amount = credit.Amount.MulRaw(timeStampDiff).QuoRaw(monthHours) | ||
return credit | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
fix comment, current time, not +month
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.
this is the last change timestamp