Skip to content

Commit

Permalink
Merge pull request #3735 from greymistcube/refactor/iworld
Browse files Browse the repository at this point in the history
♻️ Changed `SetValidator()` to `SetValidatorSet()`
  • Loading branch information
greymistcube authored Apr 9, 2024
2 parents 4e55367 + 1bfd5c5 commit 4f78091
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 62 deletions.
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ To be released.

### Backward-incompatible API changes

- (Libplanet.Action) Removed `IWorld.SetValidator()` extension method.
Use `IWorld.SetValidatorSet()` extension method instead. [[#3735]]

### Backward-incompatible network protocol changes

### Backward-incompatible storage format changes
Expand All @@ -24,6 +27,8 @@ To be released.

### CLI tools

[#3735]: https://github.com/planetarium/libplanet/pull/3735


Version 4.3.0
-------------
Expand Down
4 changes: 1 addition & 3 deletions Libplanet.Action.Tests/Common/DumbAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,7 @@ public IWorld Execute(IActionContext context)

if (Validators is { } validators)
{
world = validators.Aggregate(
world,
(current, validator) => current.SetValidator(validator));
world = world.SetValidatorSet(new ValidatorSet(validators.ToList()));
}

return world;
Expand Down
4 changes: 1 addition & 3 deletions Libplanet.Action.Tests/Common/DumbModernAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,7 @@ public IWorld Execute(IActionContext context)

if (Validators is { } validators)
{
world = validators.Aggregate(
world,
(current, validator) => current.SetValidator(validator));
world = world.SetValidatorSet(new ValidatorSet(validators.ToList()));
}

return world;
Expand Down
7 changes: 5 additions & 2 deletions Libplanet.Action.Tests/Common/SetValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,11 @@ public void LoadPlainValue(IValue plainValue)
}

/// <inheritdoc cref="IAction.Execute(IActionContext)"/>
public IWorld Execute(IActionContext context) =>
context.PreviousState.SetValidator(Validator);
public IWorld Execute(IActionContext context)
{
IWorld world = context.PreviousState;
return world.SetValidatorSet(world.GetValidatorSet().Update(Validator));
}

/// <inheritdoc cref="IEquatable{T}.Equals(T)"/>
public bool Equals(SetValidator other) =>
Expand Down
9 changes: 4 additions & 5 deletions Libplanet.Action/State/IWorldExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -252,16 +252,15 @@ public static ValidatorSet GetValidatorSet(this IWorldState worldState)
}

/// <summary>
/// Sets <paramref name="validator"/> to the stored <see cref="ValidatorSet"/>.
/// If 0 is given as its power, removes the validator from the <see cref="ValidatorSet"/>.
/// Sets <paramref name="validatorSet"/> to the stored <see cref="ValidatorSet"/>.
/// </summary>
/// <param name="world">The <see cref="IWorld"/> to manipulate.</param>
/// <param name="validator">The <see cref="Validator"/> instance to write.</param>
/// <param name="validatorSet">The <see cref="ValidatorSet"/> instance to write.</param>
/// <returns>A new <see cref="IWorld"/> instance with
/// <paramref name="validator"/> set.</returns>
[Pure]
public static IWorld SetValidator(this IWorld world, Validator validator) =>
UpdateValidatorSet(world, world.GetValidatorSet().Update(validator));
public static IWorld SetValidatorSet(this IWorld world, ValidatorSet validatorSet) =>
UpdateValidatorSet(world, validatorSet);

[Pure]
private static IWorld UpdateFungibleAssets(
Expand Down
5 changes: 4 additions & 1 deletion Libplanet.Action/Sys/Initialize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,13 @@ public IWorld Execute(IActionContext context)

if (ValidatorSet is { } vs)
{
var validatorSet = world.GetValidatorSet();
foreach (Validator v in vs.Validators)
{
world = world.SetValidator(v);
validatorSet = validatorSet.Update(v);
}

world = world.SetValidatorSet(validatorSet);
}

IAccount legacyAccount = world.GetAccount(ReservedAddresses.LegacyAccount);
Expand Down
65 changes: 17 additions & 48 deletions Libplanet.Tests/Action/WorldTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using Libplanet.Action;
Expand Down Expand Up @@ -370,55 +371,23 @@ public virtual void BurnAsset()
}

[Fact]
public virtual void SetValidator()
public void SetValidatorSet()
{
Assert.Throws<ArgumentOutOfRangeException>(() =>
_initWorld.SetValidator(new Validator(new PrivateKey().PublicKey, -1))
);

var initCount = _keys.Length;
var key3 = new PrivateKey().PublicKey;
var key4 = new PrivateKey().PublicKey;

IWorld delta = _initWorld;
// delta already has 3 validators
Assert.Equal(initCount, delta.GetValidatorSet().TotalCount);

// nothing happens trying to delete non existing validator
delta = delta.SetValidator(new Validator(key3, 0));
Assert.Equal(initCount, delta.GetValidatorSet().TotalCount);

// add key 3 to the validator set
delta = delta.SetValidator(new Validator(key3, 1));
Assert.Equal(initCount + 1, delta.GetValidatorSet().TotalCount);
Assert.True(delta.GetValidatorSet().Contains(new Validator(key3, 1)));
Assert.False(delta.GetValidatorSet().Contains(new Validator(key4, 1)));

// add key 4 to the validator set
delta = delta.SetValidator(new Validator(key4, 1));
Assert.Equal(initCount + 2, delta.GetValidatorSet().TotalCount);
Assert.True(delta.GetValidatorSet().Contains(new Validator(key3, 1)));
Assert.True(delta.GetValidatorSet().Contains(new Validator(key4, 1)));

// remove key 3 from the validator set
delta = delta.SetValidator(new Validator(key3, 0));
Assert.Equal(initCount + 1, delta.GetValidatorSet().TotalCount);
Assert.False(delta.GetValidatorSet().Contains(new Validator(key3, 1)));
Assert.True(delta.GetValidatorSet().Contains(new Validator(key4, 1)));

// re-add key 3 to the validator set
delta = delta.SetValidator(new Validator(key3, 1));
Assert.Equal(initCount + 2, delta.GetValidatorSet().TotalCount);
Assert.True(delta.GetValidatorSet().Contains(new Validator(key3, 1)));
Assert.True(delta.GetValidatorSet().Contains(new Validator(key4, 1)));

// remove all keys from the validator set
delta = _keys.Aggregate(
delta,
(current, key) => current.SetValidator(new Validator(key.PublicKey, 0)));
delta = delta.SetValidator(new Validator(key3, 0));
delta = delta.SetValidator(new Validator(key4, 0));
Assert.Equal(0, delta.GetValidatorSet().TotalCount);
const int newValidatorCount = 6;
var world = _initWorld;
var keys = Enumerable
.Range(0, newValidatorCount)
.Select(i => new PrivateKey())
.ToList();
var validatorSet = new ValidatorSet(
keys.Select(key => new Validator(key.PublicKey, 1)).ToList());
world = world.SetValidatorSet(validatorSet);

Assert.Equal(newValidatorCount, world.GetValidatorSet().TotalCount);
Assert.NotEqual(_initWorld.GetValidatorSet(), world.GetValidatorSet());

world = world.SetValidatorSet(new ValidatorSet());
Assert.Equal(0, world.GetValidatorSet().TotalCount);
}

protected FungibleAssetValue Value(int currencyIndex, BigInteger quantity) =>
Expand Down

0 comments on commit 4f78091

Please sign in to comment.