-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3023 from planetarium/release/1.19.3
Fix Refund admin action
- Loading branch information
Showing
2 changed files
with
53 additions
and
121 deletions.
There are no files selected for viewing
114 changes: 30 additions & 84 deletions
114
.Lib9c.Tests/Action/Guild/Migration/FixToRefundFromNonValidatorTest.cs
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 |
---|---|---|
@@ -1,135 +1,81 @@ | ||
namespace Lib9c.Tests.Action.Guild.Migration | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using Lib9c.Tests.Fixtures.TableCSV.Stake; | ||
using Lib9c.Tests.Util; | ||
using System.Linq; | ||
using Libplanet.Action.State; | ||
using Libplanet.Crypto; | ||
using Libplanet.Types.Assets; | ||
using Nekoyume; | ||
using Nekoyume.Action; | ||
using Nekoyume.Action.Guild.Migration; | ||
using Nekoyume.Extensions; | ||
using Nekoyume.Model.Stake; | ||
using Nekoyume.Model.State; | ||
using Nekoyume.Module; | ||
using Nekoyume.TableData.Stake; | ||
using Xunit; | ||
|
||
// TODO: Remove this test class after the migration is completed. | ||
public class FixToRefundFromNonValidatorTest : GuildTestBase | ||
{ | ||
private IWorld _world; | ||
private Currency _ncg; | ||
private Currency _gg; | ||
private Address _agentAddress; | ||
private Address _stakeAddress; | ||
private Address _adminAddress; | ||
|
||
public FixToRefundFromNonValidatorTest() | ||
{ | ||
_agentAddress = new PrivateKey().Address; | ||
_adminAddress = new PrivateKey().Address; | ||
_stakeAddress = StakeState.DeriveAddress(_agentAddress); | ||
_ncg = World.GetGoldCurrency(); | ||
_gg = Currencies.GuildGold; | ||
var sheetsOverride = new Dictionary<string, string> | ||
{ | ||
{ | ||
"StakeRegularFixedRewardSheet_V1", | ||
StakeRegularFixedRewardSheetFixtures.V1 | ||
}, | ||
{ | ||
"StakeRegularFixedRewardSheet_V2", | ||
StakeRegularFixedRewardSheetFixtures.V2 | ||
}, | ||
{ | ||
"StakeRegularRewardSheet_V1", | ||
StakeRegularRewardSheetFixtures.V1 | ||
}, | ||
{ | ||
"StakeRegularRewardSheet_V2", | ||
StakeRegularRewardSheetFixtures.V2 | ||
}, | ||
{ | ||
nameof(StakePolicySheet), | ||
StakePolicySheetFixtures.V2 | ||
}, | ||
}; | ||
(_, _, _, _world) = InitializeUtil.InitializeStates( | ||
agentAddr: _agentAddress, | ||
sheetsOverride: sheetsOverride); | ||
var stakePolicySheet = _world.GetSheet<StakePolicySheet>(); | ||
var contract = new Contract(stakePolicySheet); | ||
var adminState = new AdminState(_adminAddress, 100L); | ||
var stakeState = new StakeState(new Contract(stakePolicySheet), 1L); | ||
|
||
_world = World | ||
.SetLegacyState(Addresses.Admin, adminState.Serialize()) | ||
.SetLegacyState(_stakeAddress, stakeState.Serialize()) | ||
.MintAsset(new ActionContext { }, Addresses.NonValidatorDelegatee, _gg * 10000) | ||
.MintAsset(new ActionContext { }, _stakeAddress, _ncg * 10) | ||
.MintAsset(new ActionContext { }, _stakeAddress, _gg * 5); | ||
.MintAsset(new ActionContext { }, Addresses.NonValidatorDelegatee, Currencies.GuildGold * 500); | ||
} | ||
|
||
[Fact] | ||
public void PlainValue() | ||
{ | ||
var targets = Enumerable.Range(0, 5).Select(i => (new PrivateKey().Address, (i + 1) * 10)).ToList(); | ||
var plainValue = new FixToRefundFromNonValidator(targets).PlainValue; | ||
|
||
var recon = new FixToRefundFromNonValidator(); | ||
recon.LoadPlainValue(plainValue); | ||
Assert.Equal(targets, recon.Targets); | ||
} | ||
|
||
[Fact] | ||
public void Execute() | ||
{ | ||
var world = new FixToRefundFromNonValidator(new Address[] { _agentAddress }).Execute(new ActionContext | ||
var targets = Enumerable.Range(0, 5).Select(i => (new PrivateKey().Address, (i + 1) * 10)).ToList(); | ||
|
||
var world = new FixToRefundFromNonValidator(targets).Execute(new ActionContext | ||
{ | ||
PreviousState = _world, | ||
Signer = _adminAddress, | ||
BlockIndex = 2L, | ||
}); | ||
|
||
Assert.Equal(_gg * 10, world.GetBalance(_stakeAddress, _gg)); | ||
Assert.Equal(_gg * (10000 - 5), world.GetBalance(Addresses.NonValidatorDelegatee, _gg)); | ||
foreach (var item in targets) | ||
{ | ||
Assert.Equal( | ||
Currencies.GuildGold * item.Item2, | ||
world.GetBalance(StakeState.DeriveAddress(item.Item1), Currencies.GuildGold)); | ||
} | ||
|
||
Assert.Equal( | ||
Currencies.GuildGold * (500 - targets.Select(t => t.Item2).Sum()), | ||
world.GetBalance(Addresses.NonValidatorDelegatee, Currencies.GuildGold)); | ||
} | ||
|
||
[Fact] | ||
public void AssertWhenExecutedByNonAdmin() | ||
{ | ||
var targets = Enumerable.Range(0, 5).Select(i => (new PrivateKey().Address, (i + 1) * 10)).ToList(); | ||
|
||
Assert.Throws<PermissionDeniedException>(() => | ||
{ | ||
new FixToRefundFromNonValidator(new Address[] { _agentAddress }).Execute(new ActionContext | ||
new FixToRefundFromNonValidator(targets).Execute(new ActionContext | ||
{ | ||
PreviousState = _world, | ||
Signer = new PrivateKey().Address, | ||
BlockIndex = 2L, | ||
}); | ||
}); | ||
} | ||
|
||
[Fact] | ||
public void AssertWhenHasSufficientGG() | ||
{ | ||
var world = _world | ||
.MintAsset(new ActionContext { }, _stakeAddress, _gg * 5); | ||
Assert.Throws<InvalidOperationException>(() => | ||
{ | ||
new FixToRefundFromNonValidator(new Address[] { _agentAddress }).Execute(new ActionContext | ||
{ | ||
PreviousState = world, | ||
Signer = _adminAddress, | ||
BlockIndex = 2L, | ||
}); | ||
}); | ||
} | ||
|
||
[Fact] | ||
public void AssertWhenLegacyStakeState() | ||
{ | ||
var stakeState = new LegacyStakeState(_stakeAddress, 0L); | ||
var world = _world.SetLegacyState(_stakeAddress, stakeState.Serialize()); | ||
Assert.Throws<InvalidOperationException>(() => | ||
{ | ||
new FixToRefundFromNonValidator(new Address[] { _agentAddress }).Execute(new ActionContext | ||
{ | ||
PreviousState = World, | ||
Signer = _adminAddress, | ||
BlockIndex = 2L, | ||
}); | ||
}); | ||
} | ||
} | ||
} |
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