Skip to content

Commit

Permalink
Merge pull request planetarium#3680 from OnedgeLee/fix/mortgage
Browse files Browse the repository at this point in the history
Fix Mortgage not to clean TotalUpdatedFungibleAssets
  • Loading branch information
OnedgeLee committed Feb 22, 2024
2 parents d7305f4 + 9bfdbff commit 88657ff
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 13 deletions.
6 changes: 6 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ Version 4.0.6

To be released.

- (Libplanet.Action) Fixed a bug where `FeeCollector.Mortgage()`
unintentionally resets accumulated `Account.TotalUpdatedFungibleAssets`.
[[#3680]]

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


Version 4.0.5
-------------
Expand Down
2 changes: 1 addition & 1 deletion Libplanet.Action/FeeCollector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public IWorld Mortgage(IWorld world)
throw new InsufficientBalanceException(msg, _context.Signer, balance);
}

IAccount nextAccount = new Account(account).BurnAsset(
IAccount nextAccount = account.BurnAsset(
_context,
_context.Signer,
realGasPrice * _context.GasLimit());
Expand Down
51 changes: 39 additions & 12 deletions Libplanet.Tests/Action/ActionEvaluatorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -968,25 +968,46 @@ public void EvaluatePolicyBlockAction()
[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(MintAction)),
genesisBlock: _storeFx.GenesisBlock,
actionLoader: new SingleActionLoader(typeof(UseGasAction)),
actions: gasFillActions,
privateKey: ChainPrivateKey);
var privateKeys = Enumerable.Range(0, 3).Select(_ => new PrivateKey()).ToList();
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 mintAction = new MintAction();
mintAction.LoadPlainValue(currency.Serialize());
var txs = privateKeys.Select(privateKey =>
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[] { mintAction }.ToPlainValues()))
0,
privateKey,
chain.Genesis.Hash,
new[] { useGasActions[i] }.ToPlainValues(),
FungibleAssetValue.FromRawValue(gas, 1),
2))
.ToList();

var genesis = chain.Genesis;
Expand All @@ -1005,7 +1026,15 @@ public void TotalUpdatedFungibleAssets()
Assert.Equal(
2,
latest
.GetAccount(ReservedAddresses.LegacyAccount).TotalUpdatedFungibleAssets.Count);
.GetAccount(ReservedAddresses.LegacyAccount)
.TotalUpdatedFungibleAssets
.Count(updated => updated.Item2.Equals(currency)));
Assert.Equal(
4,
latest
.GetAccount(ReservedAddresses.LegacyAccount)
.TotalUpdatedFungibleAssets
.Count(updated => updated.Item2.Equals(gas)));
Assert.Contains(
(addresses[0], currency),
latest
Expand All @@ -1014,10 +1043,8 @@ public void TotalUpdatedFungibleAssets()
(addresses[1], currency),
latest.GetAccount(ReservedAddresses.LegacyAccount).TotalUpdatedFungibleAssets);
Assert.DoesNotContain(
addresses[2],
latest
.GetAccount(ReservedAddresses.LegacyAccount)
.TotalUpdatedFungibleAssets.Select(pair => pair.Item1));
(addresses[2], currency),
latest.GetAccount(ReservedAddresses.LegacyAccount).TotalUpdatedFungibleAssets);
}

[Fact]
Expand Down

0 comments on commit 88657ff

Please sign in to comment.