Skip to content

Commit

Permalink
Merge pull request #639 from iotaledger/expose-pool-reward
Browse files Browse the repository at this point in the history
Expose latest committed epoch pool reward
  • Loading branch information
PhilippGackstatter authored Dec 21, 2023
2 parents 0ff4e9e + e488e74 commit 3563539
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 17 deletions.
29 changes: 22 additions & 7 deletions components/restapi/core/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,12 @@ func rewardsByOutputID(c echo.Context) (*api.ManaRewardsResponse, error) {
return nil, ierrors.Wrapf(echo.ErrInternalServerError, "failed to get output %s from ledger: %s", outputID.ToHex(), err)
}

var stakingPoolValidatorAccountID iotago.AccountID
var reward iotago.Mana
var firstRewardEpoch, lastRewardEpoch iotago.EpochIndex

apiForSlot := deps.Protocol.APIForSlot(slotIndex)

switch utxoOutput.OutputType() {
case iotago.OutputAccount:
//nolint:forcetypeassert
Expand All @@ -198,13 +202,13 @@ func rewardsByOutputID(c echo.Context) (*api.ManaRewardsResponse, error) {
//nolint:forcetypeassert
stakingFeature := feature.(*iotago.StakingFeature)

apiForSlot := deps.Protocol.APIForSlot(slotIndex)
futureBoundedSlotIndex := slotIndex + apiForSlot.ProtocolParameters().MinCommittableAge()
claimingEpoch := apiForSlot.TimeProvider().EpochFromSlot(futureBoundedSlotIndex)

stakingPoolValidatorAccountID = accountOutput.AccountID
// check if the account is a validator
reward, firstRewardEpoch, lastRewardEpoch, err = deps.Protocol.Engines.Main.Get().SybilProtection.ValidatorReward(
accountOutput.AccountID,
stakingPoolValidatorAccountID,
stakingFeature,
claimingEpoch,
)
Expand All @@ -213,7 +217,6 @@ func rewardsByOutputID(c echo.Context) (*api.ManaRewardsResponse, error) {
//nolint:forcetypeassert
delegationOutput := utxoOutput.Output().(*iotago.DelegationOutput)
delegationEnd := delegationOutput.EndEpoch
apiForSlot := deps.Protocol.APIForSlot(slotIndex)
futureBoundedSlotIndex := slotIndex + apiForSlot.ProtocolParameters().MinCommittableAge()
claimingEpoch := apiForSlot.TimeProvider().EpochFromSlot(futureBoundedSlotIndex)
// If Delegation ID is zeroed, the output is in delegating state, which means its End Epoch is not set and we must use the
Expand All @@ -224,8 +227,10 @@ func rewardsByOutputID(c echo.Context) (*api.ManaRewardsResponse, error) {
delegationEnd = claimingEpoch - iotago.EpochIndex(1)
}

stakingPoolValidatorAccountID = delegationOutput.ValidatorAddress.AccountID()

reward, firstRewardEpoch, lastRewardEpoch, err = deps.Protocol.Engines.Main.Get().SybilProtection.DelegatorReward(
delegationOutput.ValidatorAddress.AccountID(),
stakingPoolValidatorAccountID,
delegationOutput.DelegatedAmount,
delegationOutput.StartEpoch,
delegationEnd,
Expand All @@ -236,10 +241,20 @@ func rewardsByOutputID(c echo.Context) (*api.ManaRewardsResponse, error) {
return nil, ierrors.Wrapf(echo.ErrInternalServerError, "failed to calculate reward for output %s: %s", outputID.ToHex(), err)
}

latestCommittedEpochPoolRewards, poolRewardExists, err := deps.Protocol.Engines.Main.Get().SybilProtection.PoolRewardsForAccount(stakingPoolValidatorAccountID)

if err != nil {
return nil, ierrors.Wrapf(echo.ErrInternalServerError, "failed to retrieve pool rewards for account %s: %s", stakingPoolValidatorAccountID.ToHex(), err)
}
if !poolRewardExists {
latestCommittedEpochPoolRewards = 0
}

return &api.ManaRewardsResponse{
StartEpoch: firstRewardEpoch,
EndEpoch: lastRewardEpoch,
Rewards: reward,
StartEpoch: firstRewardEpoch,
EndEpoch: lastRewardEpoch,
Rewards: reward,
LatestCommittedEpochPoolRewards: latestCommittedEpochPoolRewards,
}, nil
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ require (
github.com/iotaledger/hive.go/stringify v0.0.0-20231219105941-542b1b724494
github.com/iotaledger/inx-app v1.0.0-rc.3.0.20231219130724-cb37c927be6d
github.com/iotaledger/inx/go v1.0.0-rc.2.0.20231219130433-ad0971fe6306
github.com/iotaledger/iota.go/v4 v4.0.0-20231219130133-667f32aa821d
github.com/iotaledger/iota.go/v4 v4.0.0-20231220145409-bad3962d0185
github.com/labstack/echo/v4 v4.11.3
github.com/labstack/gommon v0.4.1
github.com/libp2p/go-libp2p v0.32.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,8 @@ github.com/iotaledger/inx/go v1.0.0-rc.2.0.20231219130433-ad0971fe6306 h1:8JAa8D
github.com/iotaledger/inx/go v1.0.0-rc.2.0.20231219130433-ad0971fe6306/go.mod h1:KcwBYlfpd7w1SmAY75GBO5Ce7k/RyeCXA4TRQcOwTdQ=
github.com/iotaledger/iota-crypto-demo v0.0.0-20231208171603-786bb32fdb00 h1:j5udgLtSN6wQgFI9vnhkdJsqsVdJmwtoc0yOmT/Ila4=
github.com/iotaledger/iota-crypto-demo v0.0.0-20231208171603-786bb32fdb00/go.mod h1:gt+URx7DZu414nZME7jtGgxR4DVTSnNa1jF2trTUTZ0=
github.com/iotaledger/iota.go/v4 v4.0.0-20231219130133-667f32aa821d h1:47rt2taNoMPgMF0BixlEd5q9RHEtvsGtIIuOkKsa2k4=
github.com/iotaledger/iota.go/v4 v4.0.0-20231219130133-667f32aa821d/go.mod h1:66w9NjF5IDk4amUu+i54yPfSi3hLqpAOefotC0kZahs=
github.com/iotaledger/iota.go/v4 v4.0.0-20231220145409-bad3962d0185 h1:VRxOxhIuOGHyfMSOgXLSvObJfL/YIMm/lSdTK7XbGXQ=
github.com/iotaledger/iota.go/v4 v4.0.0-20231220145409-bad3962d0185/go.mod h1:66w9NjF5IDk4amUu+i54yPfSi3hLqpAOefotC0kZahs=
github.com/ipfs/boxo v0.13.1 h1:nQ5oQzcMZR3oL41REJDcTbrvDvuZh3J9ckc9+ILeRQI=
github.com/ipfs/boxo v0.13.1/go.mod h1:btrtHy0lmO1ODMECbbEY1pxNtrLilvKSYLoGQt1yYCk=
github.com/ipfs/go-cid v0.4.1 h1:A/T3qGvxi4kpKWWcPC/PgbvDA2bjVLO7n4UeVwnbs/s=
Expand Down
3 changes: 3 additions & 0 deletions pkg/protocol/sybilprotection/sybilprotection.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ type SybilProtection interface {
// Since the Delegation Output's EndEpoch might be unset due to an ongoing delegation, the epoch until which rewards were calculated is also returned (lastRewardEpoch).
// The rewards are decayed until claimingEpoch, which should be set to the epoch in which the rewards would be claimed.
DelegatorReward(validatorID iotago.AccountID, delegatedAmount iotago.BaseToken, epochStart iotago.EpochIndex, epochEnd iotago.EpochIndex, claimingEpoch iotago.EpochIndex) (delegatorReward iotago.Mana, firstRewardEpoch iotago.EpochIndex, lastRewardEpoch iotago.EpochIndex, err error)
// PoolRewardsForAccount returns the pool rewards of the account identified by accountID in the last committed epoch.
PoolRewardsForAccount(accountID iotago.AccountID) (
poolRewardsForAccount iotago.Mana, exists bool, err error)
SeatManager() seatmanager.SeatManager
CommitSlot(iotago.SlotIndex) (iotago.Identifier, iotago.Identifier, error)
Import(io.ReadSeeker) error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ func (t *Tracker) ApplyEpoch(epoch iotago.EpochIndex, committee *account.Account
panic(ierrors.Wrapf(err, "failed to calculate pool rewards for account %s", accountID))
}

t.LogDebug("PerformanceFactor", "accountID", accountID, "epochPerformanceFactor", epochPerformanceFactor, "poolReward", poolReward)
t.LogInfo("PerformanceApplyEpoch", "accountID", accountID, "epochPerformanceFactor", epochPerformanceFactor, "poolReward", poolReward)

if err = rewardsMap.Set(accountID, &model.PoolRewards{
PoolStake: pool.PoolStake,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,19 @@ func (t *Tracker) DelegatorReward(validatorID iotago.AccountID, delegatedAmount
return delegatorsReward, firstRewardEpoch, lastRewardEpoch, nil
}

func (t *Tracker) PoolRewardsForAccount(accountID iotago.AccountID) (
poolRewardsForAccount iotago.Mana,
exists bool,
err error,
) {
rewards, exists, err := t.rewardsForAccount(accountID, t.latestAppliedEpoch)
if err != nil || !exists {
return 0, exists, err
}

return rewards.PoolRewards, exists, err
}

// Returns the epoch until which rewards are decayed.
//
// When claiming rewards in epoch X for epoch X-1, decay of X-(X-1) = 1 would be applied. Since epoch X is the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,14 @@ func (o *SybilProtection) DelegatorReward(validatorID iotago.AccountID, delegate
return o.performanceTracker.DelegatorReward(validatorID, delegatedAmount, epochStart, epochEnd, claimingEpoch)
}

func (o *SybilProtection) PoolRewardsForAccount(accountID iotago.AccountID) (
poolRewardsForAccount iotago.Mana,
exists bool,
err error,
) {
return o.performanceTracker.PoolRewardsForAccount(accountID)
}

func (o *SybilProtection) Import(reader io.ReadSeeker) error {
return o.performanceTracker.Import(reader)
}
Expand Down
2 changes: 1 addition & 1 deletion tools/gendoc/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ require (
github.com/iotaledger/inx-app v1.0.0-rc.3.0.20231219130724-cb37c927be6d // indirect
github.com/iotaledger/inx/go v1.0.0-rc.2.0.20231219130433-ad0971fe6306 // indirect
github.com/iotaledger/iota-crypto-demo v0.0.0-20231208171603-786bb32fdb00 // indirect
github.com/iotaledger/iota.go/v4 v4.0.0-20231219130133-667f32aa821d // indirect
github.com/iotaledger/iota.go/v4 v4.0.0-20231220145409-bad3962d0185 // indirect
github.com/ipfs/boxo v0.13.1 // indirect
github.com/ipfs/go-cid v0.4.1 // indirect
github.com/ipfs/go-datastore v0.6.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions tools/gendoc/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -313,8 +313,8 @@ github.com/iotaledger/inx/go v1.0.0-rc.2.0.20231219130433-ad0971fe6306 h1:8JAa8D
github.com/iotaledger/inx/go v1.0.0-rc.2.0.20231219130433-ad0971fe6306/go.mod h1:KcwBYlfpd7w1SmAY75GBO5Ce7k/RyeCXA4TRQcOwTdQ=
github.com/iotaledger/iota-crypto-demo v0.0.0-20231208171603-786bb32fdb00 h1:j5udgLtSN6wQgFI9vnhkdJsqsVdJmwtoc0yOmT/Ila4=
github.com/iotaledger/iota-crypto-demo v0.0.0-20231208171603-786bb32fdb00/go.mod h1:gt+URx7DZu414nZME7jtGgxR4DVTSnNa1jF2trTUTZ0=
github.com/iotaledger/iota.go/v4 v4.0.0-20231219130133-667f32aa821d h1:47rt2taNoMPgMF0BixlEd5q9RHEtvsGtIIuOkKsa2k4=
github.com/iotaledger/iota.go/v4 v4.0.0-20231219130133-667f32aa821d/go.mod h1:66w9NjF5IDk4amUu+i54yPfSi3hLqpAOefotC0kZahs=
github.com/iotaledger/iota.go/v4 v4.0.0-20231220145409-bad3962d0185 h1:VRxOxhIuOGHyfMSOgXLSvObJfL/YIMm/lSdTK7XbGXQ=
github.com/iotaledger/iota.go/v4 v4.0.0-20231220145409-bad3962d0185/go.mod h1:66w9NjF5IDk4amUu+i54yPfSi3hLqpAOefotC0kZahs=
github.com/ipfs/boxo v0.13.1 h1:nQ5oQzcMZR3oL41REJDcTbrvDvuZh3J9ckc9+ILeRQI=
github.com/ipfs/boxo v0.13.1/go.mod h1:btrtHy0lmO1ODMECbbEY1pxNtrLilvKSYLoGQt1yYCk=
github.com/ipfs/go-cid v0.4.1 h1:A/T3qGvxi4kpKWWcPC/PgbvDA2bjVLO7n4UeVwnbs/s=
Expand Down
2 changes: 1 addition & 1 deletion tools/genesis-snapshot/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/iotaledger/hive.go/lo v0.0.0-20231219105941-542b1b724494
github.com/iotaledger/hive.go/runtime v0.0.0-20231219105941-542b1b724494
github.com/iotaledger/iota-core v0.0.0-00010101000000-000000000000
github.com/iotaledger/iota.go/v4 v4.0.0-20231219130133-667f32aa821d
github.com/iotaledger/iota.go/v4 v4.0.0-20231220145409-bad3962d0185
github.com/mr-tron/base58 v1.2.0
github.com/spf13/pflag v1.0.5
golang.org/x/crypto v0.17.0
Expand Down
4 changes: 2 additions & 2 deletions tools/genesis-snapshot/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ github.com/iotaledger/hive.go/stringify v0.0.0-20231219105941-542b1b724494 h1:aa
github.com/iotaledger/hive.go/stringify v0.0.0-20231219105941-542b1b724494/go.mod h1:FTo/UWzNYgnQ082GI9QVM9HFDERqf9rw9RivNpqrnTs=
github.com/iotaledger/iota-crypto-demo v0.0.0-20231208171603-786bb32fdb00 h1:j5udgLtSN6wQgFI9vnhkdJsqsVdJmwtoc0yOmT/Ila4=
github.com/iotaledger/iota-crypto-demo v0.0.0-20231208171603-786bb32fdb00/go.mod h1:gt+URx7DZu414nZME7jtGgxR4DVTSnNa1jF2trTUTZ0=
github.com/iotaledger/iota.go/v4 v4.0.0-20231219130133-667f32aa821d h1:47rt2taNoMPgMF0BixlEd5q9RHEtvsGtIIuOkKsa2k4=
github.com/iotaledger/iota.go/v4 v4.0.0-20231219130133-667f32aa821d/go.mod h1:66w9NjF5IDk4amUu+i54yPfSi3hLqpAOefotC0kZahs=
github.com/iotaledger/iota.go/v4 v4.0.0-20231220145409-bad3962d0185 h1:VRxOxhIuOGHyfMSOgXLSvObJfL/YIMm/lSdTK7XbGXQ=
github.com/iotaledger/iota.go/v4 v4.0.0-20231220145409-bad3962d0185/go.mod h1:66w9NjF5IDk4amUu+i54yPfSi3hLqpAOefotC0kZahs=
github.com/ipfs/go-cid v0.4.1 h1:A/T3qGvxi4kpKWWcPC/PgbvDA2bjVLO7n4UeVwnbs/s=
github.com/ipfs/go-cid v0.4.1/go.mod h1:uQHwDeX4c6CtyrFwdqyhpNcxVewur1M7l7fNU7LKwZk=
github.com/klauspost/cpuid/v2 v2.2.5 h1:0E5MSMDEoAulmXNFquVs//DdoomxaoTY1kUhbc/qbZg=
Expand Down

0 comments on commit 3563539

Please sign in to comment.