Skip to content

Commit

Permalink
chore: Fix refund action to accept enumerable of tuples
Browse files Browse the repository at this point in the history
  • Loading branch information
OnedgeLee committed Nov 20, 2024
1 parent b400655 commit 3b06f84
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,72 +30,46 @@ public FixToRefundFromNonValidatorTest()
[Fact]
public void PlainValue()
{
var addresses = Enumerable.Range(0, 5).Select(_ => new PrivateKey().Address).ToList();
var amounts = Enumerable.Range(0, 5).Select(i => (i + 1) * 10).ToList();

var plainValue = new FixToRefundFromNonValidator(
addresses,
amounts
).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(addresses, recon.Targets);
Assert.Equal(amounts, recon.Amounts);
Assert.Equal(targets, recon.Targets);
}

[Fact]
public void Execute()
{
var addresses = Enumerable.Range(0, 5).Select(_ => new PrivateKey().Address).ToList();
var amounts = Enumerable.Range(0, 5).Select(i => (i + 1) * 10).ToList();
var targets = Enumerable.Range(0, 5).Select(i => (new PrivateKey().Address, (i + 1) * 10)).ToList();

var world = new FixToRefundFromNonValidator(
addresses,
amounts
).Execute(new ActionContext
var world = new FixToRefundFromNonValidator(targets).Execute(new ActionContext
{
PreviousState = _world,
Signer = _adminAddress,
BlockIndex = 2L,
});

foreach (var item in addresses.Select((a, i) => (a, i)))
foreach (var item in targets)
{
Assert.Equal(
Currencies.GuildGold * ((item.i + 1) * 10),
world.GetBalance(StakeState.DeriveAddress(item.a), Currencies.GuildGold));
Currencies.GuildGold * item.Item2,
world.GetBalance(StakeState.DeriveAddress(item.Item1), Currencies.GuildGold));
}

Assert.Equal(
Currencies.GuildGold * (500 - amounts.Sum()),
Currencies.GuildGold * (500 - targets.Select(t => t.Item2).Sum()),
world.GetBalance(Addresses.NonValidatorDelegatee, Currencies.GuildGold));
}

[Fact]
public void AssertWhenDifferentLengthArgument()
{
var addresses = Enumerable.Range(0, 5).Select(_ => new PrivateKey().Address).ToList();
var amounts = Enumerable.Range(0, 4).Select(i => (i + 1) * 10).ToList();

Assert.Throws<ArgumentException>(() =>
{
new FixToRefundFromNonValidator(addresses, amounts);
});
}

[Fact]
public void AssertWhenExecutedByNonAdmin()
{
var addresses = Enumerable.Range(0, 5).Select(_ => new PrivateKey().Address).ToList();
var amounts = Enumerable.Range(0, 5).Select(i => (i + 1) * 10);
var targets = Enumerable.Range(0, 5).Select(i => (new PrivateKey().Address, (i + 1) * 10)).ToList();

Assert.Throws<PermissionDeniedException>(() =>
{
new FixToRefundFromNonValidator(
addresses,
amounts
).Execute(new ActionContext
new FixToRefundFromNonValidator(targets).Execute(new ActionContext
{
PreviousState = _world,
Signer = new PrivateKey().Address,
Expand Down
38 changes: 12 additions & 26 deletions Lib9c/Action/Guild/Migration/FixToRefundFromNonValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,55 +22,41 @@ public class FixToRefundFromNonValidator : ActionBase

private const string TargetsKey = "t";

private const string AmountsKey = "a";

public List<Address> Targets { get; private set; }

public List<int> Amounts { get; private set; }
public List<(Address Address, int Amount)> Targets { get; private set; }

public FixToRefundFromNonValidator()
{
}

public FixToRefundFromNonValidator(
IEnumerable<Address> targets,
IEnumerable<int> amounts)
IEnumerable<(Address, int)> targets)
{
Targets = targets.ToList();
Amounts = amounts.ToList();

if (Targets.Count != Amounts.Count)
{
throw new ArgumentException("The number of targets and amounts must be the same.");
}
}

public override IValue PlainValue => Dictionary.Empty
.Add("type_id", TypeIdentifier)
.Add("values", Dictionary.Empty
.Add(TargetsKey, new List(Targets.Select(t => t.Bencoded)))
.Add(AmountsKey, new List(Amounts)));
.Add(
TargetsKey,
new List(Targets.Select(t =>
new List(t.Item1.Bencoded, (Integer)t.Item2)))));

public override void LoadPlainValue(IValue plainValue)
{
if (plainValue is not Dictionary root ||
!root.TryGetValue((Text)"values", out var rawValues) ||
rawValues is not Dictionary values ||
!values.TryGetValue((Text)TargetsKey, out var rawTarget) ||
rawTarget is not List targets ||
!values.TryGetValue((Text)AmountsKey, out var rawAmounts) ||
rawAmounts is not List amounts)
rawTarget is not List targets)
{
throw new InvalidCastException();
}

Targets = targets.Select(t => new Address(t)).ToList();
Amounts = amounts.Select(a => (int)(Integer)a).ToList();

if (Targets.Count != Amounts.Count)
{
throw new ArgumentException("The number of targets and amounts must be the same.");
}
Targets = targets.Select(
t => (
new Address(((List)t)[0]),
(int)(Integer)((List)t)[1])).ToList();
}

public override IWorld Execute(IActionContext context)
Expand All @@ -89,7 +75,7 @@ public override IWorld Execute(IActionContext context)
throw new PermissionDeniedException(adminState, context.Signer);
}

foreach (var ta in Targets.Zip(Amounts, (f, s) => (f, s)))
foreach (var ta in Targets)
{
world = RefundFromNonValidator(context, world, ta);
}
Expand Down

0 comments on commit 3b06f84

Please sign in to comment.