Skip to content
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

🧹 Removed TotalUpdatedFungibleAssets from IWorld #3714

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 @@ -12,6 +12,8 @@ To be released.

- (Libplanet.Action) Added `Txs` property of
type `IReadOnlyList<ITransaction>?` to `IActionContext`. [[#3713]]
- (Libplanet.Action) Removed `TotalFungibleAssets` property from
`IWorld`. [[#3714]]

### Backward-incompatible network protocol changes

Expand All @@ -28,6 +30,7 @@ To be released.
### CLI tools

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


Version 4.2.0
Expand Down
6 changes: 2 additions & 4 deletions Libplanet.Action/ActionEvaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -576,8 +576,7 @@ private static IWorld CommitLegacyWorld(IWorld prevWorld, IStateStore stateStore
new WorldBaseState(
stateStore.Commit(prevWorld.GetAccount(ReservedAddresses.LegacyAccount).Trie),
stateStore),
new WorldDelta(),
prevWorld.TotalUpdatedFungibleAssets);
new WorldDelta());
}

private static IWorld CommitWorld(IWorld prevWorld, IStateStore stateStore)
Expand All @@ -592,8 +591,7 @@ private static IWorld CommitWorld(IWorld prevWorld, IStateStore stateStore)

return new World(
new WorldBaseState(stateStore.Commit(worldTrie), stateStore),
new WorldDelta(),
prevWorld.TotalUpdatedFungibleAssets);
new WorldDelta());
}

[Pure]
Expand Down
8 changes: 0 additions & 8 deletions Libplanet.Action/State/IWorld.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,6 @@ public interface IWorld : IWorldState
[Pure]
IWorldDelta Delta { get; }

/// <summary>
/// A set of <see cref="Address"/> and <see cref="Currency"/> pairs where
/// each pair has its asoociated <see cref="FungibleAssetValue"/> changed
/// since the previous <see cref="Block"/>'s output states.
/// </summary>
[Pure]
IImmutableSet<(Address, Currency)> TotalUpdatedFungibleAssets { get; }

/// <summary>
/// Gets the <see cref="IAccount"/> of the given <paramref name="address"/>.
/// </summary>
Expand Down
39 changes: 4 additions & 35 deletions Libplanet.Action/State/World.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,19 @@ public class World : IWorld
private readonly IWorldState _baseState;

public World(IWorldState baseState)
: this(baseState, new WorldDelta(), ImmutableHashSet<(Address, Currency)>.Empty)
: this(baseState, new WorldDelta())
{
}

public World(
IWorldState baseState,
IWorldDelta delta,
IImmutableSet<(Address, Currency)> totalUpdatedFungibleAssets)
public World(IWorldState baseState, IWorldDelta delta)
{
_baseState = baseState;
Delta = delta;
TotalUpdatedFungibleAssets = totalUpdatedFungibleAssets;
}

/// <inheritdoc cref="IWorld.Delta"/>
public IWorldDelta Delta { get; }

/// <inheritdoc/>
public IImmutableSet<(Address, Currency)> TotalUpdatedFungibleAssets { get; }

/// <inheritdoc cref="IWorldState.Trie"/>
[Pure]
public ITrie Trie => _baseState.Trie;
Expand Down Expand Up @@ -72,10 +65,7 @@ public IWorld SetAccount(Address address, IAccount account)
nameof(address));
}

return new World(
_baseState,
Delta.SetAccount(address, account),
TotalUpdatedFungibleAssets);
return new World(_baseState, Delta.SetAccount(address, account));
}

/// <inheritdoc cref="IWorldState.GetBalance"/>
Expand Down Expand Up @@ -219,24 +209,6 @@ public IWorld TransferAsset(
public IWorld SetValidator(Validator validator) =>
UpdateValidatorSet(GetValidatorSet().Update(validator));

private IWorld SetAccount(
Address address,
IAccount account,
IImmutableSet<(Address, Currency)> totalUpdatedFungibleAssets)
{
if (Legacy && !address.Equals(ReservedAddresses.LegacyAccount))
{
throw new ArgumentException(
$"Cannot set a non-legacy account ({address}) to a legacy {nameof(IWorld)}.",
nameof(address));
}

return new World(
_baseState,
Delta.SetAccount(address, account),
totalUpdatedFungibleAssets);
}

private IWorld UpdateFungibleAssets(
Address address,
Currency currency,
Expand All @@ -252,10 +224,7 @@ private IWorld UpdateFungibleAssets(
GetAccount(ReservedAddresses.LegacyAccount).Trie
.Set(ToFungibleAssetKey(address, currency), new Integer(amount))));

return SetAccount(
ReservedAddresses.LegacyAccount,
account,
TotalUpdatedFungibleAssets.Add((address, currency)));
return SetAccount(ReservedAddresses.LegacyAccount, account);
}

private IWorld UpdateValidatorSet(ValidatorSet validatorSet)
Expand Down
79 changes: 0 additions & 79 deletions Libplanet.Tests/Action/ActionEvaluatorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -967,85 +967,6 @@ public void OrderTxsForEvaluation(
.Select(tx => tx.Signer.ToString())));
}

[Fact]
public void TotalUpdatedFungibleAssets()
{
var privateKeys = Enumerable.Range(0, 3).Select(_ => new PrivateKey()).ToList();
var gas = Currency.Uncapped("GAS", 0, null);

var gasFillActions = privateKeys.Select(pk => new UseGasAction()
{
GasUsage = 0,
Memo = "Refill",
MintValue = gas * 100,
Receiver = pk.Address,
});

var (chain, actionEvaluator) = MakeBlockChainAndActionEvaluator(
policy: _policy,
store: _storeFx.Store,
stateStore: _storeFx.StateStore,
actionLoader: new SingleActionLoader(typeof(UseGasAction)),
actions: gasFillActions,
privateKey: ChainPrivateKey);
var addresses = privateKeys.Select(privateKey => privateKey.Address).ToList();

// Only addresses[0] and addresses[1] are able to mint
var currency = Currency.Uncapped(
"FOO", 0, addresses.Take(2).ToImmutableHashSet());

var useGasActions = privateKeys.Select(pk => new UseGasAction()
{
GasUsage = 1,
Memo = "Use",
MintValue = currency * 1,
Receiver = pk.Address,
}).ToList();

var txs = privateKeys.Select((privateKey, i) =>
Transaction.Create(
0,
privateKey,
chain.Genesis.Hash,
new[] { useGasActions[i] }.ToPlainValues(),
FungibleAssetValue.FromRawValue(gas, 1),
2))
.ToList();

var genesis = chain.Genesis;
var block = chain.ProposeBlock(
GenesisProposer, txs.ToImmutableList(), CreateBlockCommit(chain.Tip));

var evals = actionEvaluator.EvaluateBlock(
block,
new World(chain.GetWorldState(block.PreviousHash)));

// Does not include policy block action
Assert.Equal(3, evals.Count());
var latest = evals.Last().OutputState;

// Only addresses[0] and addresses[1] succeeded in minting
Assert.Equal(
2,
latest
.TotalUpdatedFungibleAssets
.Count(updated => updated.Item2.Equals(currency)));
Assert.Equal(
4,
latest
.TotalUpdatedFungibleAssets
.Count(updated => updated.Item2.Equals(gas)));
Assert.Contains(
(addresses[0], currency),
latest.TotalUpdatedFungibleAssets);
Assert.Contains(
(addresses[1], currency),
latest.TotalUpdatedFungibleAssets);
Assert.DoesNotContain(
(addresses[2], currency),
latest.TotalUpdatedFungibleAssets);
}

[Fact]
public void EvaluateActionAndCollectFee()
{
Expand Down
45 changes: 0 additions & 45 deletions Libplanet.Tests/Action/WorldV1Test.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Linq;
using Libplanet.Action;
using Libplanet.Action.State;
using Libplanet.Action.Tests.Common;
Expand Down Expand Up @@ -118,49 +117,5 @@ public override void MintAsset()
Assert.Throws<SupplyOverflowException>(
() => _initWorld.MintAsset(_initContext, _addr[0], Value(4, 200)));
}

[Fact]
public virtual void TotalUpdatedFungibleAssets()
{
IWorld delta0 = _initWorld;
IActionContext context0 = _initContext;

// currencies[0] (FOO) allows only _addr[0] to burn
delta0 = delta0.BurnAsset(context0, _addr[0], Value(0, 1));
Assert.Contains(
(_addr[0], Value(0, 0).Currency), delta0.TotalUpdatedFungibleAssets);
Assert.DoesNotContain(
(_addr[0], Value(1, 0).Currency), delta0.TotalUpdatedFungibleAssets);

// currencies[1] (BAR) allows _addr[0] & _addr[1] to mint and burn
delta0 = delta0.MintAsset(context0, _addr[0], Value(1, 1));
Assert.Contains(
(_addr[0], Value(0, 0).Currency),
delta0.TotalUpdatedFungibleAssets);
Assert.Contains(
(_addr[0], Value(1, 0).Currency),
delta0.TotalUpdatedFungibleAssets);
Assert.DoesNotContain(
_addr[1],
delta0.TotalUpdatedFungibleAssets.Select(pair => pair.Item1));

context0 = CreateContext(delta0, _addr[1]);
delta0 = delta0.BurnAsset(context0, _addr[1], Value(1, 1));

// _addr[0] burned currencies[0] & minted currencies[1]
// _addr[1] burned currencies[1]
Assert.Contains(
(_addr[0], Value(0, 0).Currency),
delta0.TotalUpdatedFungibleAssets);
Assert.Contains(
(_addr[0], Value(1, 0).Currency),
delta0.TotalUpdatedFungibleAssets);
Assert.DoesNotContain(
(_addr[1], Value(0, 0).Currency),
delta0.TotalUpdatedFungibleAssets);
Assert.Contains(
(_addr[1], Value(1, 0).Currency),
delta0.TotalUpdatedFungibleAssets);
}
}
}
Loading