Skip to content

Commit

Permalink
Removed TotalUpdatedFungibleAssets from IWorld
Browse files Browse the repository at this point in the history
  • Loading branch information
greymistcube committed Mar 27, 2024
1 parent 679f2b6 commit 519d754
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 171 deletions.
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);
}
}
}

0 comments on commit 519d754

Please sign in to comment.