Skip to content

Introduce IAccount.SetValidatorSet() method #3730

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ Version DPoS
- (Libplanet.Action) `ActionEvaluator` constructor requires
`policyBeginBlockActionGetter` and `policyEndBlockActionGetter`
parameters instead of the `policyBlockActionGetter` parameter. [[#3701]]
- (Libplanet.Action) Added `SetValidatorSet` method to `IAccount` interface
and its implementations. [[#3730]]

### Backward-incompatible network protocol changes

Expand All @@ -36,6 +38,7 @@ Version DPoS
### CLI tools

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


Version 4.1.0
Expand Down
5 changes: 5 additions & 0 deletions Libplanet.Action/State/Account.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,11 @@ public IAccount BurnAsset(
public IAccount SetValidator(Validator validator) =>
UpdateValidatorSet(GetValidatorSet().Update(validator));

/// <inheritdoc/>
[Pure]
public IAccount SetValidatorSet(ValidatorSet validatorSet) =>
UpdateValidatorSet(validatorSet);

[Pure]
private Account UpdateState(
Address address,
Expand Down
11 changes: 11 additions & 0 deletions Libplanet.Action/State/IAccount.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,5 +173,16 @@ IAccount BurnAsset(
/// <paramref name="validator"/> set.</returns>
[Pure]
IAccount SetValidator(Validator validator);

/// <summary>
/// Sets the <see cref="ValidatorSet"/> with the given <paramref name="validatorSet"/>.
/// </summary>
/// <param name="validatorSet">
/// A new <see cref="ValidatorSet"/> instance to update.
/// </param>
/// <returns>A new <see cref="IAccount"/> instance with the updated
/// <paramref name="validatorSet"/>.</returns>
[Pure]
IAccount SetValidatorSet(ValidatorSet validatorSet);
}
}
36 changes: 36 additions & 0 deletions Libplanet.Tests/Action/AccountTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,42 @@ public virtual void SetValidator()
Assert.Equal(0, delta.GetValidatorSet().TotalCount);
}

[Fact]
public virtual void SetValidatorSet()
{
var initCount = _keys.Length;
var key3 = new PrivateKey().PublicKey;
var key4 = new PrivateKey().PublicKey;

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

// remove on validator, add two validators
var validators = delta.GetValidatorSet().Validators.ToList();
var removedValidator = validators[0];
validators.Remove(validators[0]);
validators.Add(new Validator(key3, 1));
validators.Add(new Validator(key4, 1));
delta = delta.SetValidatorSet(new ValidatorSet(validators));
Assert.Equal(4, delta.GetValidatorSet().TotalCount);
Assert.Contains(
delta.GetValidatorSet().Validators,
v => v.PublicKey.Equals(validators[0].PublicKey));
Assert.Contains(
delta.GetValidatorSet().Validators,
v => v.PublicKey.Equals(validators[1].PublicKey));
Assert.Contains(
delta.GetValidatorSet().Validators,
v => v.PublicKey.Equals(key3));
Assert.Contains(
delta.GetValidatorSet().Validators,
v => v.PublicKey.Equals(key4));
Assert.DoesNotContain(
delta.GetValidatorSet().Validators,
v => v.PublicKey.Equals(removedValidator.PublicKey));
}

protected FungibleAssetValue Value(int currencyIndex, BigInteger quantity) =>
new FungibleAssetValue(_currencies[currencyIndex], quantity, 0);

Expand Down