Skip to content

Commit

Permalink
Changed ToFungibleAssetKey to use HashDigest<SHA1> instead of Currency
Browse files Browse the repository at this point in the history
  • Loading branch information
greymistcube committed Dec 26, 2023
1 parent 0762683 commit 025dd07
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 16 deletions.
8 changes: 4 additions & 4 deletions Libplanet.Action.Tests/Mocks/MockAccountState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public IReadOnlyList<IValue> GetStates(IReadOnlyList<Address> addresses) =>
addresses.Select(GetState).ToList();

public FungibleAssetValue GetBalance(Address address, Currency currency) =>
Trie.Get(KeyConverters.ToFungibleAssetKey(address, currency)) is Integer rawValue
Trie.Get(KeyConverters.ToFungibleAssetKey(address, currency.Hash)) is Integer rawValue
? FungibleAssetValue.FromRawValue(currency, rawValue)
: currency * 0;

Expand Down Expand Up @@ -108,7 +108,7 @@ public MockAccountState SetBalance(
public MockAccountState SetBalance(
(Address Address, Currency Currency) pair, BigInteger rawAmount) =>
new MockAccountState(Trie.Set(
KeyConverters.ToFungibleAssetKey(pair.Address, pair.Currency),
KeyConverters.ToFungibleAssetKey(pair.Address, pair.Currency.Hash),
new Integer(rawAmount)));

public MockAccountState AddBalance(Address address, FungibleAssetValue amount) =>
Expand All @@ -122,7 +122,7 @@ public MockAccountState AddBalance(
(Address Address, Currency Currency) pair, BigInteger rawAmount) =>
SetBalance(
pair,
(Trie.Get(KeyConverters.ToFungibleAssetKey(pair.Address, pair.Currency)) is
(Trie.Get(KeyConverters.ToFungibleAssetKey(pair.Address, pair.Currency.Hash)) is
Integer amount ? amount : 0) + rawAmount);

public MockAccountState SubtractBalance(
Expand All @@ -137,7 +137,7 @@ public MockAccountState SubtractBalance(
(Address Address, Currency Currency) pair, BigInteger rawAmount) =>
SetBalance(
pair,
(Trie.Get(KeyConverters.ToFungibleAssetKey(pair.Address, pair.Currency)) is
(Trie.Get(KeyConverters.ToFungibleAssetKey(pair.Address, pair.Currency.Hash)) is
Integer amount ? amount : 0) - rawAmount);

public MockAccountState TransferBalance(
Expand Down
2 changes: 1 addition & 1 deletion Libplanet.Action.Tests/State/KeyConvertersTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void ToKeysSpec()
Assert.Equal(
new KeyBytes(
$"_{ByteUtil.Hex(address.ByteArray)}_{ByteUtil.Hex(currency.Hash.ByteArray)}"),
KeyConverters.ToFungibleAssetKey(address, currency));
KeyConverters.ToFungibleAssetKey(address, currency.Hash));

Assert.Equal(
new KeyBytes($"__{ByteUtil.Hex(currency.Hash.ByteArray)}"),
Expand Down
4 changes: 2 additions & 2 deletions Libplanet.Action/State/Account.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,12 @@ private Account UpdateFungibleAssets(
? new Account(
new AccountState(
Trie
.Set(ToFungibleAssetKey(address, currency), new Integer(amount))
.Set(ToFungibleAssetKey(address, currency.Hash), new Integer(amount))
.Set(ToTotalSupplyKey(currency.Hash), new Integer(sa))),
TotalUpdatedFungibleAssets.Add((address, currency)))
: new Account(
new AccountState(
Trie.Set(ToFungibleAssetKey(address, currency), new Integer(amount))),
Trie.Set(ToFungibleAssetKey(address, currency.Hash), new Integer(amount))),
TotalUpdatedFungibleAssets.Add((address, currency)));

[Pure]
Expand Down
2 changes: 1 addition & 1 deletion Libplanet.Action/State/AccountState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public AccountState(ITrie trie)
/// <inheritdoc cref="IAccountState.GetBalance"/>
public FungibleAssetValue GetBalance(Address address, Currency currency)
{
IValue? value = Trie.Get(ToFungibleAssetKey(address, currency));
IValue? value = Trie.Get(ToFungibleAssetKey(address, currency.Hash));
return value is Integer i
? FungibleAssetValue.FromRawValue(currency, i)
: currency * 0;
Expand Down
7 changes: 3 additions & 4 deletions Libplanet.Action/State/KeyConverters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using Libplanet.Common;
using Libplanet.Crypto;
using Libplanet.Store.Trie;
using Libplanet.Types.Assets;

namespace Libplanet.Action.State
{
Expand Down Expand Up @@ -48,11 +47,11 @@ internal static KeyBytes ToStateKey(Address address)
return new KeyBytes(buffer);
}

// $"_{ByteUtil.Hex(address.ByteArray)}_{ByteUtil.Hex(currency.Hash.ByteArray)}"
internal static KeyBytes ToFungibleAssetKey(Address address, Currency currency)
// $"_{ByteUtil.Hex(address.ByteArray)}_{ByteUtil.Hex(currencyHash.ByteArray)}"
internal static KeyBytes ToFungibleAssetKey(Address address, HashDigest<SHA1> currencyHash)
{
var addressBytes = address.ByteArray;
var currencyBytes = currency.Hash.ByteArray;
var currencyBytes = currencyHash.ByteArray;
byte[] buffer = new byte[addressBytes.Length * 2 + currencyBytes.Length * 2 + 2];

buffer[0] = _underScore;
Expand Down
8 changes: 4 additions & 4 deletions Libplanet.Tests/Action/ActionEvaluatorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -378,16 +378,16 @@ DumbAction MakeAction(Address address, char identifier, Address? transferTo = nu
dirty1[ToStateKey(DumbAction.RandomRecordsAddress)]);
Assert.Equal(
(Integer)new FungibleAssetValue(DumbAction.DumbCurrency, -5, 0).RawValue,
dirty1[ToFungibleAssetKey(addresses[0], DumbAction.DumbCurrency)]);
dirty1[ToFungibleAssetKey(addresses[0], DumbAction.DumbCurrency.Hash)]);
Assert.Equal(
(Integer)new FungibleAssetValue(DumbAction.DumbCurrency).RawValue,
dirty1[ToFungibleAssetKey(addresses[1], DumbAction.DumbCurrency)]);
dirty1[ToFungibleAssetKey(addresses[1], DumbAction.DumbCurrency.Hash)]);
Assert.Equal(
(Integer)new FungibleAssetValue(DumbAction.DumbCurrency).RawValue,
dirty1[ToFungibleAssetKey(addresses[2], DumbAction.DumbCurrency)]);
dirty1[ToFungibleAssetKey(addresses[2], DumbAction.DumbCurrency.Hash)]);
Assert.Equal(
(Integer)new FungibleAssetValue(DumbAction.DumbCurrency, 5, 0).RawValue,
dirty1[ToFungibleAssetKey(addresses[3], DumbAction.DumbCurrency)]);
dirty1[ToFungibleAssetKey(addresses[3], DumbAction.DumbCurrency.Hash)]);

Transaction[] block2Txs =
{
Expand Down

0 comments on commit 025dd07

Please sign in to comment.