Skip to content

Commit

Permalink
Merge pull request #3065 from OnedgeLee/feature/reward-base
Browse files Browse the repository at this point in the history
Introduce RewardBase
  • Loading branch information
OnedgeLee authored Dec 12, 2024
2 parents 9649a61 + 9e8d87c commit eef7e54
Show file tree
Hide file tree
Showing 25 changed files with 1,590 additions and 541 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ var expectedProposerReward

var validatorAddress = vote.ValidatorPublicKey.Address;
var actualDelegatee = actualRepository.GetValidatorDelegatee(validatorAddress);
var validatorRewardAddress = actualDelegatee.CurrentLumpSumRewardsRecordAddress();
var validatorRewardAddress = actualDelegatee.DistributionPoolAddress();
var actualDelegationBalance = world.GetBalance(validatorAddress, DelegationCurrency);
var actualCommission = world.GetBalance(validatorAddress, GuildAllocateRewardCurrency);
var actualUnclaimedReward = world.GetBalance(validatorRewardAddress, GuildAllocateRewardCurrency);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ namespace Lib9c.Tests.Action.ValidatorDelegation;
using Nekoyume.ValidatorDelegation;
using Xunit;
using Nekoyume.Action.Guild.Migration.LegacyModels;
using Nekoyume.Delegation;

public class ValidatorDelegationTestBase
{
Expand Down Expand Up @@ -592,7 +593,10 @@ protected static FungibleAssetValue CalculateBonusPropserReward(
}

protected static FungibleAssetValue CalculateClaim(BigInteger share, BigInteger totalShare, FungibleAssetValue totalClaim)
=> (totalClaim * share).DivRem(totalShare).Quotient;
{
var multiplier = BigInteger.Pow(10, (int)Math.Floor(BigInteger.Log10(totalShare)) + RewardBase.Margin);
return ((totalClaim * multiplier).DivRem(totalShare).Quotient * share).DivRem(multiplier).Quotient;
}

protected static FungibleAssetValue CalculateCommunityFund(ImmutableArray<Vote> votes, FungibleAssetValue reward)
{
Expand Down
6 changes: 6 additions & 0 deletions .Lib9c.Tests/Delegation/DelegationFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,18 @@ public DelegationFixture()
new Address("0x67A44E11506b8f0Bb625fEECccb205b33265Bb48"), TestRepository.DelegateeAccountAddress, TestRepository);
TestDelegatee2 = new TestDelegatee(
new Address("0xea1C4eedEfC99691DEfc6eF2753FAfa8C17F4584"), TestRepository.DelegateeAccountAddress, TestRepository);
TestRepository.SetDelegator(TestDelegator1);
TestRepository.SetDelegator(TestDelegator2);
TestRepository.SetDelegatee(TestDelegatee1);
TestRepository.SetDelegatee(TestDelegatee2);

DummyRepository = new DummyRepository(world, context);
DummyDelegatee1 = new DummyDelegatee(
new Address("0x67A44E11506b8f0Bb625fEECccb205b33265Bb48"), DummyRepository.DelegateeAccountAddress, DummyRepository);
DummyDelegator1 = new DummyDelegator(
new Address("0x0054E98312C47E7Fa0ABed45C23Fa187e31C373a"), DummyRepository.DelegateeAccountAddress, DummyRepository);
DummyRepository.SetDelegator(DummyDelegator1);
DummyRepository.SetDelegatee(DummyDelegatee1);
}

public TestRepository TestRepository { get; }
Expand Down
65 changes: 45 additions & 20 deletions .Lib9c.Tests/Delegation/DelegatorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -328,21 +328,26 @@ public void RewardOnDelegate()
delegator2Balance = repo.World.GetBalance(delegator2.Address, delegatee.DelegationCurrency);
var delegator1RewardBalances = delegatee.RewardCurrencies.Select(
c => repo.World.GetBalance(delegator1.Address, c));
var collectedRewards = DelegationFixture.TotalRewardsOfRecords(delegatee, repo);
var collectedRewards = delegatee.RewardCurrencies.Select(
c => repo.World.GetBalance(delegatee.DistributionPoolAddress(), c));
var legacyRewards = DelegationFixture.TotalRewardsOfRecords(delegatee, repo);

var rewards1 = rewards.Select(r => (r * share1).DivRem(totalShares, out _));
Assert.Equal(delegatorInitialBalance - delegatingFAV1 * 2, delegator1Balance);
Assert.Equal(delegatorInitialBalance - delegatingFAV2, delegator2Balance);
Assert.Equal(rewards1, delegator1RewardBalances);
Assert.Equal(rewards.Zip(rewards1, (f, s) => f * 2 - s), collectedRewards);
Assert.Equal(rewards.Zip(rewards1, (f, s) => f - s), collectedRewards);
Assert.Equal(rewards, legacyRewards);

delegator2.Delegate(delegatee, delegatingFAV2, 11L);
delegator2Balance = repo.World.GetBalance(delegator2.Address, delegatee.DelegationCurrency);
delegator1RewardBalances = delegatee.RewardCurrencies.Select(
c => repo.World.GetBalance(delegator1.Address, c));
var delegator2RewardBalances = delegatee.RewardCurrencies.Select(
c => repo.World.GetBalance(delegator2.Address, c));
collectedRewards = DelegationFixture.TotalRewardsOfRecords(delegatee, repo);
collectedRewards = delegatee.RewardCurrencies.Select(
c => repo.World.GetBalance(delegatee.DistributionPoolAddress(), c));
legacyRewards = DelegationFixture.TotalRewardsOfRecords(delegatee, repo);

var rewards2 = rewards.Select(r => (r * share2).DivRem(totalShares, out _));
Assert.Equal(delegatorInitialBalance - delegatingFAV1 * 2, delegator1Balance);
Expand All @@ -353,8 +358,9 @@ public void RewardOnDelegate()
// Flushing to remainder pool is now inactive.
// Assert.Equal(delegatee.RewardCurrencies.Select(c => c * 0), collectedRewards);
Assert.Equal(
rewards.Select(r => r * 2).Zip(rewards1.Zip(rewards2, (f, s) => f + s), (f, s) => f - s).ToArray(),
rewards.Zip(rewards1.Zip(rewards2, (f, s) => f + s), (f, s) => f - s).ToArray(),
collectedRewards);
Assert.Equal(rewards, legacyRewards);
}

[Fact]
Expand All @@ -374,7 +380,7 @@ public void RewardOnUndelegate()
repo.MintAsset(delegatee.RewardPoolAddress, reward);
}

// EndBlock after delegatee's reward
// BeginBlock after delegatee's reward
delegatee.CollectRewards(10L);

var delegatingFAV1 = delegatee.DelegationCurrency * 10;
Expand All @@ -398,21 +404,24 @@ public void RewardOnUndelegate()
repo.MintAsset(delegatee.RewardPoolAddress, reward);
}

// EndBlock after delegatee's reward
delegatee.CollectRewards(10L);
// BeginBlock after delegatee's reward
delegatee.CollectRewards(11L);

var shareToUndelegate = repo.GetBond(delegatee, delegator1.Address).Share / 3;
delegator1.Undelegate(delegatee, shareToUndelegate, 11L);
delegator1Balance = repo.World.GetBalance(delegator1.Address, delegatee.DelegationCurrency);
var delegator1RewardBalances = delegatee.RewardCurrencies.Select(
c => repo.World.GetBalance(delegator1.Address, c));
var collectedRewards = DelegationFixture.TotalRewardsOfRecords(delegatee, repo);
var collectedRewards = delegatee.RewardCurrencies.Select(
c => repo.World.GetBalance(delegatee.DistributionPoolAddress(), c));
var legacyRewards = DelegationFixture.TotalRewardsOfRecords(delegatee, repo);

var rewards1 = rewards.Select(r => (r * share1).DivRem(totalShares, out _));
Assert.Equal(delegatorInitialBalance - delegatingFAV1, delegator1Balance);
Assert.Equal(delegatorInitialBalance - delegatingFAV2, delegator2Balance);
Assert.Equal(rewards1, delegator1RewardBalances);
Assert.Equal(rewards.Zip(rewards1, (f, s) => f * 2 - s), collectedRewards);
Assert.Equal(rewards.Zip(rewards1, (f, s) => f - s), collectedRewards);
Assert.Equal(rewards, legacyRewards);

shareToUndelegate = repo.GetBond(delegatee, delegator2.Address).Share / 2;
delegator2.Undelegate(delegatee, shareToUndelegate, 11L);
Expand All @@ -421,7 +430,9 @@ public void RewardOnUndelegate()
c => repo.World.GetBalance(delegator1.Address, c));
var delegator2RewardBalances = delegatee.RewardCurrencies.Select(
c => repo.World.GetBalance(delegator2.Address, c));
collectedRewards = DelegationFixture.TotalRewardsOfRecords(delegatee, repo);
collectedRewards = delegatee.RewardCurrencies.Select(
c => repo.World.GetBalance(delegatee.DistributionPoolAddress(), c));
legacyRewards = DelegationFixture.TotalRewardsOfRecords(delegatee, repo);

var rewards2 = rewards.Select(r => (r * share2).DivRem(totalShares, out _));
Assert.Equal(delegatorInitialBalance - delegatingFAV1, delegator1Balance);
Expand All @@ -430,10 +441,12 @@ public void RewardOnUndelegate()
Assert.Equal(rewards2, delegator2RewardBalances);

// Flushing to remainder pool is now inactive.
// Assert.Equal(delegatee.RewardCurrencies.Select(c => c * 0), collectedRewards);
Assert.Equal(
rewards.Select(r => r * 2).Zip(rewards1.Zip(rewards2, (f, s) => f + s), (f, s) => f - s).ToArray(),
rewards.Zip(rewards1.Zip(rewards2, (f, s) => f + s), (f, s) => f - s).ToArray(),
collectedRewards);
Assert.Equal(
rewards,
legacyRewards);
}

[Fact]
Expand Down Expand Up @@ -486,13 +499,16 @@ public void RewardOnRedelegate()
delegator1Balance = repo.World.GetBalance(delegator1.Address, delegatee.DelegationCurrency);
var delegator1RewardBalances = delegatee.RewardCurrencies.Select(
c => repo.World.GetBalance(delegator1.Address, c));
var collectedRewards = DelegationFixture.TotalRewardsOfRecords(delegatee, repo);
var collectedRewards = delegatee.RewardCurrencies.Select(
c => repo.World.GetBalance(delegatee.DistributionPoolAddress(), c));
var legacyRewards = DelegationFixture.TotalRewardsOfRecords(delegatee, repo);

var rewards1 = rewards.Select(r => (r * share1).DivRem(totalShares, out _));
Assert.Equal(delegatorInitialBalance - delegatingFAV1, delegator1Balance);
Assert.Equal(delegatorInitialBalance - delegatingFAV2, delegator2Balance);
Assert.Equal(rewards1, delegator1RewardBalances);
Assert.Equal(rewards.Zip(rewards1, (f, s) => f * 2 - s), collectedRewards);
Assert.Equal(rewards.Zip(rewards1, (f, s) => f - s), collectedRewards);
Assert.Equal(rewards, legacyRewards);

shareToRedelegate = repo.GetBond(delegatee, delegator2.Address).Share / 2;
delegator2.Redelegate(delegatee, dstDelegatee, shareToRedelegate, 11L);
Expand All @@ -501,7 +517,9 @@ public void RewardOnRedelegate()
c => repo.World.GetBalance(delegator1.Address, c));
var delegator2RewardBalances = delegatee.RewardCurrencies.Select(
c => repo.World.GetBalance(delegator2.Address, c));
collectedRewards = DelegationFixture.TotalRewardsOfRecords(delegatee, repo);
collectedRewards = delegatee.RewardCurrencies.Select(
c => repo.World.GetBalance(delegatee.DistributionPoolAddress(), c));
legacyRewards = DelegationFixture.TotalRewardsOfRecords(delegatee, repo);

var rewards2 = rewards.Select(r => (r * share2).DivRem(totalShares, out _));
Assert.Equal(delegatorInitialBalance - delegatingFAV1, delegator1Balance);
Expand All @@ -512,8 +530,9 @@ public void RewardOnRedelegate()
// Flushing to remainder pool is now inactive.
// Assert.Equal(delegatee.RewardCurrencies.Select(c => c * 0), collectedRewards);
Assert.Equal(
rewards.Select(r => r * 2).Zip(rewards1.Zip(rewards2, (f, s) => f + s), (f, s) => f - s).ToArray(),
rewards.Zip(rewards1.Zip(rewards2, (f, s) => f + s), (f, s) => f - s).ToArray(),
collectedRewards);
Assert.Equal(rewards, legacyRewards);
}

[Fact]
Expand Down Expand Up @@ -566,13 +585,16 @@ public void RewardOnClaim()
delegator1Balance = repo.World.GetBalance(delegator1.Address, delegatee.DelegationCurrency);
var delegator1RewardBalances = delegatee.RewardCurrencies.Select(
c => repo.World.GetBalance(delegator1.Address, c));
var collectedRewards = DelegationFixture.TotalRewardsOfRecords(delegatee, repo);
var collectedRewards = delegatee.RewardCurrencies.Select(
c => repo.World.GetBalance(delegatee.DistributionPoolAddress(), c));
var legacyRewards = DelegationFixture.TotalRewardsOfRecords(delegatee, repo);

var rewards1 = rewards.Select(r => (r * share1).DivRem(totalShares, out _));
Assert.Equal(delegatorInitialBalance - delegatingFAV1, delegator1Balance);
Assert.Equal(delegatorInitialBalance - delegatingFAV2, delegator2Balance);
Assert.Equal(rewards1, delegator1RewardBalances);
Assert.Equal(rewards.Zip(rewards1, (f, s) => f * 2 - s), collectedRewards);
Assert.Equal(rewards.Zip(rewards1, (f, s) => f - s), collectedRewards);
Assert.Equal(rewards, legacyRewards);

shareToRedelegate = repo.GetBond(delegatee, delegator2.Address).Share / 2;
delegator2.ClaimReward(delegatee, 11L);
Expand All @@ -581,7 +603,9 @@ public void RewardOnClaim()
c => repo.World.GetBalance(delegator1.Address, c));
var delegator2RewardBalances = delegatee.RewardCurrencies.Select(
c => repo.World.GetBalance(delegator2.Address, c));
collectedRewards = DelegationFixture.TotalRewardsOfRecords(delegatee, repo);
collectedRewards = delegatee.RewardCurrencies.Select(
c => repo.World.GetBalance(delegatee.DistributionPoolAddress(), c));
legacyRewards = DelegationFixture.TotalRewardsOfRecords(delegatee, repo);

var rewards2 = rewards.Select(r => (r * share2).DivRem(totalShares, out _));
Assert.Equal(delegatorInitialBalance - delegatingFAV1, delegator1Balance);
Expand All @@ -592,8 +616,9 @@ public void RewardOnClaim()
// Flushing to remainder pool is now inactive.
// Assert.Equal(delegatee.RewardCurrencies.Select(c => c * 0), collectedRewards);
Assert.Equal(
rewards.Select(r => r * 2).Zip(rewards1.Zip(rewards2, (f, s) => f + s), (f, s) => f - s).ToArray(),
rewards.Zip(rewards1.Zip(rewards2, (f, s) => f + s), (f, s) => f - s).ToArray(),
collectedRewards);
Assert.Equal(rewards, legacyRewards);
}
}
}
3 changes: 2 additions & 1 deletion .Lib9c.Tests/Delegation/DummyRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public DummyRepository(IWorld world, IActionContext context)
unbondLockInAccountAddress: new Address("0000000000000000000000000000000000000005"),
rebondGraceAccountAddress: new Address("0000000000000000000000000000000000000006"),
unbondingSetAccountAddress: new Address("0000000000000000000000000000000000000007"),
lumpSumRewardRecordAccountAddress: new Address("0000000000000000000000000000000000000008"))
rewardBaseAccountAddress: new Address("0000000000000000000000000000000000000008"),
lumpSumRewardRecordAccountAddress: new Address("0000000000000000000000000000000000000009"))
{
}

Expand Down
Loading

0 comments on commit eef7e54

Please sign in to comment.