Skip to content

Commit

Permalink
Merge pull request #593 from iotaledger/mana-rewards-epoch
Browse files Browse the repository at this point in the history
Fix Mana Rewards Decay Epoch
  • Loading branch information
PhilippGackstatter authored Dec 5, 2023
2 parents 4e3cf41 + 3c9b3e6 commit 52beb5d
Show file tree
Hide file tree
Showing 12 changed files with 32 additions and 40 deletions.
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-20231128121006-331a9e522dfe
github.com/iotaledger/inx-app v1.0.0-rc.3.0.20231201123347-1c44b3f24221
github.com/iotaledger/inx/go v1.0.0-rc.2.0.20231201114846-3bb5c3fd5665
github.com/iotaledger/iota.go/v4 v4.0.0-20231204084048-9c12053adaf6
github.com/iotaledger/iota.go/v4 v4.0.0-20231204142547-416c9a87403d
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 @@ -307,8 +307,8 @@ github.com/iotaledger/inx-app v1.0.0-rc.3.0.20231201123347-1c44b3f24221 h1:+ozra
github.com/iotaledger/inx-app v1.0.0-rc.3.0.20231201123347-1c44b3f24221/go.mod h1:6cLX3gnhP0WL+Q+mf3/rIqfACe5fWKVR8luPXWh2xiY=
github.com/iotaledger/inx/go v1.0.0-rc.2.0.20231201114846-3bb5c3fd5665 h1:XdhojOpZ0t0pJFyNO0zlBogSAUrhEI67eCpTC9H6sGM=
github.com/iotaledger/inx/go v1.0.0-rc.2.0.20231201114846-3bb5c3fd5665/go.mod h1:obK1N42oafGA7EH6zC4VX2fKh7jTa3WnIa9h1azfxq0=
github.com/iotaledger/iota.go/v4 v4.0.0-20231204084048-9c12053adaf6 h1:Wuf8Ps3tzuZ5CCbl7LA5O2qwhfuXWo30RCOT0yhm4pg=
github.com/iotaledger/iota.go/v4 v4.0.0-20231204084048-9c12053adaf6/go.mod h1:lCk9rhP3B5pX9BKhzR+Jobq4xPd+GHlqgF4Ga+eQfWA=
github.com/iotaledger/iota.go/v4 v4.0.0-20231204142547-416c9a87403d h1:MPklxa8jW4/EgDm/LEzf6orxjik7U+vMUW/ToGT1Zqg=
github.com/iotaledger/iota.go/v4 v4.0.0-20231204142547-416c9a87403d/go.mod h1:lCk9rhP3B5pX9BKhzR+Jobq4xPd+GHlqgF4Ga+eQfWA=
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
4 changes: 2 additions & 2 deletions pkg/protocol/engine/accounts/accountsledger/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ func (m *Manager) computeBlockBurnsForSlot(slot iotago.SlotIndex, rmc iotago.Man
return nil, ierrors.Wrapf(err, "cannot compute penalty for over-issuing validator, account %s could not be retrieved", accountID)
}
punishmentEpochs := apiForSlot.ProtocolParameters().PunishmentEpochs()
manaPunishment, err := apiForSlot.ManaDecayProvider().ManaGenerationWithDecay(accountData.ValidatorStake, slot, slot+apiForSlot.TimeProvider().EpochDurationSlots()*iotago.SlotIndex(punishmentEpochs))
manaPunishment, err := apiForSlot.ManaDecayProvider().GenerateManaAndDecayBySlots(accountData.ValidatorStake, slot, slot+apiForSlot.TimeProvider().EpochDurationSlots()*iotago.SlotIndex(punishmentEpochs))
if err != nil {
return nil, ierrors.Wrapf(err, "cannot compute penalty for over-issuing validator with account ID %s due to problem with mana generation", accountID)
}
Expand Down Expand Up @@ -526,7 +526,7 @@ func (m *Manager) commitAccountTree(slot iotago.SlotIndex, accountDiffChanges ma
if diffChange.BICChange != 0 || !exists {
// decay the credits to the current slot if the account exists
if exists {
decayedPreviousCredits, err := m.apiProvider.APIForSlot(slot).ManaDecayProvider().ManaWithDecay(iotago.Mana(accountData.Credits.Value), accountData.Credits.UpdateSlot, slot)
decayedPreviousCredits, err := m.apiProvider.APIForSlot(slot).ManaDecayProvider().DecayManaBySlots(iotago.Mana(accountData.Credits.Value), accountData.Credits.UpdateSlot, slot)
if err != nil {
return ierrors.Wrapf(err, "can't retrieve account, could not decay credits for account (%s) in slot (%d)", accountData.ID, slot)
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/protocol/engine/accounts/mana/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,13 @@ func (m *Manager) GetManaOnAccount(accountID iotago.AccountID, slot iotago.SlotI

// Apply decay to stored, potential and BIC mana that was calculated when adding the entry to cache
// so that it's correct for the requested slot.
manaWithDecay, err := manaDecayProvider.ManaWithDecay(mana.Value(), mana.UpdateTime(), slot)
manaWithDecay, err := manaDecayProvider.DecayManaBySlots(mana.Value(), mana.UpdateTime(), slot)
if err != nil {
return 0, err
}

// Calculate newly generated potential mana since last update time.
manaPotential, err := manaDecayProvider.ManaGenerationWithDecay(mana.ExcessBaseTokens(), mana.UpdateTime(), slot)
manaPotential, err := manaDecayProvider.GenerateManaAndDecayBySlots(mana.ExcessBaseTokens(), mana.UpdateTime(), slot)
if err != nil {
return 0, err
}
Expand Down Expand Up @@ -124,12 +124,12 @@ func (m *Manager) getMana(accountID iotago.AccountID, output *utxoledger.Output,
var manaUpdateTime iotago.SlotIndex
var totalMana iotago.Mana
if bicUpdateTime > output.SlotCreated() {
manaPotential, err := manaDecayProvider.ManaGenerationWithDecay(excessBaseTokens, output.SlotCreated(), bicUpdateTime)
manaPotential, err := manaDecayProvider.GenerateManaAndDecayBySlots(excessBaseTokens, output.SlotCreated(), bicUpdateTime)
if err != nil {
return nil, ierrors.Wrapf(err, "failed to calculate mana generation with decay (excessBaseTokens: %d; outputSlotCreated: %d; targetSlot: %d)", excessBaseTokens, output.SlotCreated(), bicUpdateTime)
}

manaStored, err := manaDecayProvider.ManaWithDecay(output.StoredMana(), output.SlotCreated(), bicUpdateTime)
manaStored, err := manaDecayProvider.DecayManaBySlots(output.StoredMana(), output.SlotCreated(), bicUpdateTime)
if err != nil {
return nil, ierrors.Wrapf(err, "failed to calculate mana with decay (storedMana: %d; outputSlotCreated: %d; targetSlot: %d)", output.StoredMana(), output.SlotCreated(), bicUpdateTime)
}
Expand All @@ -147,7 +147,7 @@ func (m *Manager) getMana(accountID iotago.AccountID, output *utxoledger.Output,
manaUpdateTime = bicUpdateTime
} else if output.SlotCreated() > bicUpdateTime {
// Decay BIC to match the Output creation time.
bicWithDecay, err := manaDecayProvider.ManaWithDecay(bic, bicUpdateTime, output.SlotCreated())
bicWithDecay, err := manaDecayProvider.DecayManaBySlots(bic, bicUpdateTime, output.SlotCreated())
if err != nil {
return nil, err
}
Expand Down
20 changes: 10 additions & 10 deletions pkg/protocol/engine/accounts/mana/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,10 @@ func TestManager_GetManaOnAccountOverflow(t *testing.T) {
mana, err := manager.GetManaOnAccount(accountIDValid, 1)
require.NoError(t, err)

decayedMana, err := manaDecayProvider.ManaWithDecay(iotago.MaxMana/2+iotago.Mana(iotago.MaxBlockIssuanceCredits/2+iotago.MaxBlockIssuanceCredits/4), 0, 1)
decayedMana, err := manaDecayProvider.DecayManaBySlots(iotago.MaxMana/2+iotago.Mana(iotago.MaxBlockIssuanceCredits/2+iotago.MaxBlockIssuanceCredits/4), 0, 1)
require.NoError(t, err)

generatedMana, err := manaDecayProvider.ManaGenerationWithDecay(iotago.MaxBaseToken/2, 0, 1)
generatedMana, err := manaDecayProvider.GenerateManaAndDecayBySlots(iotago.MaxBaseToken/2, 0, 1)
require.NoError(t, err)

require.EqualValues(t, decayedMana+generatedMana, mana)
Expand All @@ -153,17 +153,17 @@ func TestManager_GetManaOnAccountOverflow(t *testing.T) {
mana, err := manager.GetManaOnAccount(accountIDRecentOutput, 2)
require.NoError(t, err)

decayedStoredMana, err := manaDecayProvider.ManaWithDecay(iotago.MaxMana/2, 1, 2)
decayedStoredMana, err := manaDecayProvider.DecayManaBySlots(iotago.MaxMana/2, 1, 2)
require.NoError(t, err)
decayedBIC2Slots, err := manaDecayProvider.ManaWithDecay(iotago.Mana(iotago.MaxBlockIssuanceCredits/2+iotago.MaxBlockIssuanceCredits/4), 0, 2)
decayedBIC2Slots, err := manaDecayProvider.DecayManaBySlots(iotago.Mana(iotago.MaxBlockIssuanceCredits/2+iotago.MaxBlockIssuanceCredits/4), 0, 2)
require.NoError(t, err)

generatedMana, err := manaDecayProvider.ManaGenerationWithDecay(iotago.MaxBaseToken/2, 1, 2)
generatedMana, err := manaDecayProvider.GenerateManaAndDecayBySlots(iotago.MaxBaseToken/2, 1, 2)
require.NoError(t, err)

require.EqualValues(t, decayedBIC2Slots+decayedStoredMana+generatedMana, mana)

decayedBIC1Slot, err := manaDecayProvider.ManaWithDecay(iotago.Mana(iotago.MaxBlockIssuanceCredits/2+iotago.MaxBlockIssuanceCredits/4), 0, 2)
decayedBIC1Slot, err := manaDecayProvider.DecayManaBySlots(iotago.Mana(iotago.MaxBlockIssuanceCredits/2+iotago.MaxBlockIssuanceCredits/4), 0, 2)
require.NoError(t, err)

// Make sure that cache entry is for slot 1.
Expand All @@ -178,20 +178,20 @@ func TestManager_GetManaOnAccountOverflow(t *testing.T) {
mana, err := manager.GetManaOnAccount(accountIDRecentBIC, 2)
require.NoError(t, err)

decayedStoredMana2Slots, err := manaDecayProvider.ManaWithDecay(iotago.MaxMana/2, 0, 0)
decayedStoredMana2Slots, err := manaDecayProvider.DecayManaBySlots(iotago.MaxMana/2, 0, 0)
require.NoError(t, err)

generatedMana1Slot, err := manaDecayProvider.ManaGenerationWithDecay(iotago.MaxBaseToken/2, 0, 1)
generatedMana1Slot, err := manaDecayProvider.GenerateManaAndDecayBySlots(iotago.MaxBaseToken/2, 0, 1)
require.NoError(t, err)

decayedBIC, err := manaDecayProvider.ManaWithDecay(iotago.Mana(iotago.MaxBlockIssuanceCredits/2+iotago.MaxBlockIssuanceCredits/4), 1, 1)
decayedBIC, err := manaDecayProvider.DecayManaBySlots(iotago.Mana(iotago.MaxBlockIssuanceCredits/2+iotago.MaxBlockIssuanceCredits/4), 1, 1)
require.NoError(t, err)

// generatedMana1Slot is multiplied to calculate generation for two slots. It cannot be done in a single step
// because the value does not match due to approximation errors inherent to the underlying calculation method.
require.EqualValues(t, decayedStoredMana2Slots+generatedMana1Slot*2+decayedBIC, mana)

decayedStoredMana1Slot, err := manaDecayProvider.ManaWithDecay(iotago.MaxMana/2, 0, 1)
decayedStoredMana1Slot, err := manaDecayProvider.DecayManaBySlots(iotago.MaxMana/2, 0, 1)
require.NoError(t, err)

// Make sure that cache entry is for slot 1.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (t *Tracker) ValidatorReward(validatorID iotago.AccountID, stakeAmount iota
}

decayProvider := t.apiProvider.APIForEpoch(epoch).ManaDecayProvider()
decayedEpochRewards, err := decayProvider.RewardsWithDecay(iotago.Mana(unDecayedEpochRewards), epoch, epochEnd)
decayedEpochRewards, err := decayProvider.DecayManaByEpochs(iotago.Mana(unDecayedEpochRewards), epoch, epochEnd)
if err != nil {
return 0, 0, 0, ierrors.Wrapf(err, "failed to calculate rewards with decay for epoch %d and validator accountID %s", epoch, validatorID)
}
Expand Down Expand Up @@ -172,7 +172,7 @@ func (t *Tracker) DelegatorReward(validatorID iotago.AccountID, delegatedAmount
}

decayProvider := t.apiProvider.APIForEpoch(epoch).ManaDecayProvider()
decayedEpochRewards, err := decayProvider.RewardsWithDecay(iotago.Mana(unDecayedEpochRewards), epoch, epochEnd)
decayedEpochRewards, err := decayProvider.DecayManaByEpochs(iotago.Mana(unDecayedEpochRewards), epoch, epochEnd)
if err != nil {
return 0, 0, 0, ierrors.Wrapf(err, "failed to calculate rewards with decay for epoch %d and validator accountID %s", epoch, validatorID)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ func (t *TestSuite) validatorReward(alias string, epoch iotago.EpochIndex, profi
residualValidatorFactor := ((profitMarginComplement * poolRewardsNoFixedCost) >> profitMarginExponent) * stakeAmount / poolStake
unDecayedEpochRewards := fixedCost + profitMarginFactor + residualValidatorFactor
decayProvider := t.api.ManaDecayProvider()
decayedEpochRewards, err := decayProvider.RewardsWithDecay(iotago.Mana(unDecayedEpochRewards), epoch, epoch)
decayedEpochRewards, err := decayProvider.DecayManaByEpochs(iotago.Mana(unDecayedEpochRewards), epoch, epoch)
require.NoError(t.T, err)

return decayedEpochRewards
Expand All @@ -250,7 +250,7 @@ func (t *TestSuite) delegatorReward(epoch iotago.EpochIndex, profitMargin, poolR
unDecayedEpochRewards := (((profitMarginComplement * poolRewards) >> profitMarginExponent) * delegatedAmount) / poolStake

decayProvider := t.api.ManaDecayProvider()
decayedEpochRewards, err := decayProvider.RewardsWithDecay(iotago.Mana(unDecayedEpochRewards), epoch, epoch)
decayedEpochRewards, err := decayProvider.DecayManaByEpochs(iotago.Mana(unDecayedEpochRewards), epoch, epoch)
require.NoError(t.T, err)

return decayedEpochRewards
Expand Down
12 changes: 2 additions & 10 deletions pkg/testsuite/mock/wallet_transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -549,20 +549,12 @@ func (w *Wallet) ClaimDelegatorRewards(transactionName string, inputName string)

// Computes the Potential Mana that the output generates until the current slot.
func (w *Wallet) PotentialMana(api iotago.API, input *utxoledger.Output) iotago.Mana {
minDeposit := lo.PanicOnErr(api.StorageScoreStructure().MinDeposit(input.Output()))

if minDeposit > input.BaseTokenAmount() {
return 0
}

excessBaseTokens := input.BaseTokenAmount() - minDeposit

return lo.PanicOnErr(api.ManaDecayProvider().ManaGenerationWithDecay(excessBaseTokens, input.SlotCreated(), w.currentSlot))
return lo.PanicOnErr(iotago.PotentialMana(api.ManaDecayProvider(), api.StorageScoreStructure(), input.Output(), input.SlotCreated(), w.currentSlot))
}

// Computes the decay on stored mana that the output holds until the current slot.
func (w *Wallet) StoredMana(api iotago.API, input *utxoledger.Output) iotago.Mana {
return lo.PanicOnErr(api.ManaDecayProvider().ManaWithDecay(input.StoredMana(), input.SlotCreated(), w.currentSlot))
return lo.PanicOnErr(api.ManaDecayProvider().DecayManaBySlots(input.StoredMana(), input.SlotCreated(), w.currentSlot))
}

func (w *Wallet) AllotManaToWallet(transactionName string, inputName string, recipientWallet *Wallet) *iotago.SignedTransaction {
Expand Down
2 changes: 1 addition & 1 deletion tools/gendoc/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ require (
github.com/iotaledger/hive.go/stringify v0.0.0-20231128121006-331a9e522dfe // indirect
github.com/iotaledger/inx-app v1.0.0-rc.3.0.20231201123347-1c44b3f24221 // indirect
github.com/iotaledger/inx/go v1.0.0-rc.2.0.20231201114846-3bb5c3fd5665 // indirect
github.com/iotaledger/iota.go/v4 v4.0.0-20231204084048-9c12053adaf6 // indirect
github.com/iotaledger/iota.go/v4 v4.0.0-20231204142547-416c9a87403d // 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 @@ -311,8 +311,8 @@ github.com/iotaledger/inx-app v1.0.0-rc.3.0.20231201123347-1c44b3f24221 h1:+ozra
github.com/iotaledger/inx-app v1.0.0-rc.3.0.20231201123347-1c44b3f24221/go.mod h1:6cLX3gnhP0WL+Q+mf3/rIqfACe5fWKVR8luPXWh2xiY=
github.com/iotaledger/inx/go v1.0.0-rc.2.0.20231201114846-3bb5c3fd5665 h1:XdhojOpZ0t0pJFyNO0zlBogSAUrhEI67eCpTC9H6sGM=
github.com/iotaledger/inx/go v1.0.0-rc.2.0.20231201114846-3bb5c3fd5665/go.mod h1:obK1N42oafGA7EH6zC4VX2fKh7jTa3WnIa9h1azfxq0=
github.com/iotaledger/iota.go/v4 v4.0.0-20231204084048-9c12053adaf6 h1:Wuf8Ps3tzuZ5CCbl7LA5O2qwhfuXWo30RCOT0yhm4pg=
github.com/iotaledger/iota.go/v4 v4.0.0-20231204084048-9c12053adaf6/go.mod h1:lCk9rhP3B5pX9BKhzR+Jobq4xPd+GHlqgF4Ga+eQfWA=
github.com/iotaledger/iota.go/v4 v4.0.0-20231204142547-416c9a87403d h1:MPklxa8jW4/EgDm/LEzf6orxjik7U+vMUW/ToGT1Zqg=
github.com/iotaledger/iota.go/v4 v4.0.0-20231204142547-416c9a87403d/go.mod h1:lCk9rhP3B5pX9BKhzR+Jobq4xPd+GHlqgF4Ga+eQfWA=
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-20231128121006-331a9e522dfe
github.com/iotaledger/hive.go/runtime v0.0.0-20231128121006-331a9e522dfe
github.com/iotaledger/iota-core v0.0.0-00010101000000-000000000000
github.com/iotaledger/iota.go/v4 v4.0.0-20231204084048-9c12053adaf6
github.com/iotaledger/iota.go/v4 v4.0.0-20231204142547-416c9a87403d
github.com/mr-tron/base58 v1.2.0
github.com/spf13/pflag v1.0.5
golang.org/x/crypto v0.16.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 @@ -52,8 +52,8 @@ github.com/iotaledger/hive.go/serializer/v2 v2.0.0-rc.1.0.20231127134220-90b88e3
github.com/iotaledger/hive.go/serializer/v2 v2.0.0-rc.1.0.20231127134220-90b88e35bdb2/go.mod h1:FoH3T6yKlZJp8xm8K+zsQiibSynp32v21CpWx8xkek8=
github.com/iotaledger/hive.go/stringify v0.0.0-20231128121006-331a9e522dfe h1:RcFUqhnJ+86+sA0XMrZ0q+086ULrdWQkWrjUt2OnJK4=
github.com/iotaledger/hive.go/stringify v0.0.0-20231128121006-331a9e522dfe/go.mod h1:FTo/UWzNYgnQ082GI9QVM9HFDERqf9rw9RivNpqrnTs=
github.com/iotaledger/iota.go/v4 v4.0.0-20231204084048-9c12053adaf6 h1:Wuf8Ps3tzuZ5CCbl7LA5O2qwhfuXWo30RCOT0yhm4pg=
github.com/iotaledger/iota.go/v4 v4.0.0-20231204084048-9c12053adaf6/go.mod h1:lCk9rhP3B5pX9BKhzR+Jobq4xPd+GHlqgF4Ga+eQfWA=
github.com/iotaledger/iota.go/v4 v4.0.0-20231204142547-416c9a87403d h1:MPklxa8jW4/EgDm/LEzf6orxjik7U+vMUW/ToGT1Zqg=
github.com/iotaledger/iota.go/v4 v4.0.0-20231204142547-416c9a87403d/go.mod h1:lCk9rhP3B5pX9BKhzR+Jobq4xPd+GHlqgF4Ga+eQfWA=
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 52beb5d

Please sign in to comment.