Skip to content

Commit

Permalink
Calculate UpdatedAddresses on Lib9c side
Browse files Browse the repository at this point in the history
  • Loading branch information
longfin committed May 3, 2023
1 parent 23b7926 commit 9066f70
Show file tree
Hide file tree
Showing 2 changed files with 172 additions and 0 deletions.
33 changes: 33 additions & 0 deletions .Lib9c.Tests/Action/ActionBaseExtensionsTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
namespace Lib9c.Tests.Action
{
using System.Collections.Immutable;
using Libplanet;
using Libplanet.Action;
using Nekoyume.Action;
using Xunit;

public class ActionBaseExtensionsTest
{
[Fact]
public void CalculateUpdateAddresses()
{
var actions = new PolymorphicAction<ActionBase>[]
{
new TransferAsset(
sender: default,
recipient: default,
amount: Currencies.DailyRewardRune * 1
),
};

IImmutableSet<Address> actual = actions.CalculateUpdateAddresses();
Assert.Equal(
new Address[]
{
default,
},
actual
);
}
}
}
139 changes: 139 additions & 0 deletions Lib9c/Action/ActionBaseExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Security.Cryptography;
using Bencodex.Types;
using Libplanet;
using Libplanet.Action;
using Libplanet.Assets;
using Libplanet.Blocks;
using Libplanet.Tx;

namespace Nekoyume.Action
{
public static class ActionBaseExtensions
{
public static IImmutableSet<Address> CalculateUpdateAddresses(
this IEnumerable<PolymorphicAction<ActionBase>> actions
) => CalculateUpdateAddresses(actions.Select(pa => pa.InnerAction));

public static IImmutableSet<Address> CalculateUpdateAddresses(this IEnumerable<ActionBase> actions)
{
IImmutableSet<Address> addresses = ImmutableHashSet<Address>.Empty;
IActionContext rehearsalContext = new RehearsalActionContext();

foreach (ActionBase action in actions)
{
IAccountStateDelta nextStates = action.Execute(rehearsalContext);
addresses = addresses.Union(nextStates.UpdatedAddresses);
}

return addresses;
}

private class RehearsalActionContext : IActionContext
{
public BlockHash? GenesisHash => default;

public Address Signer => default;

public TxId? TxId => default;

public Address Miner => default;

public long BlockIndex => default;

public bool Rehearsal => true;

public IAccountStateDelta PreviousStates => new AddressTraceStateDelta();

public IRandom Random => default;

public HashDigest<SHA256>? PreviousStateRootHash => default;

public bool BlockAction => default;

public IActionContext GetUnconsumedContext() => null;

public bool IsNativeToken(Currency currency) => false;

public void PutLog(string log)
{
// Method intentionally left empty.
}
}

private class AddressTraceStateDelta : IAccountStateDelta
{
private ImmutableHashSet<Address> _updatedAddresses;

public AddressTraceStateDelta()
: this(ImmutableHashSet<Address>.Empty)
{
}

public AddressTraceStateDelta(ImmutableHashSet<Address> updatedAddresses)
{
_updatedAddresses = updatedAddresses;
}

public IImmutableSet<Address> UpdatedAddresses => _updatedAddresses;

public IImmutableSet<Address> StateUpdatedAddresses => _updatedAddresses;

public IImmutableDictionary<Address, IImmutableSet<Currency>> UpdatedFungibleAssets
=> ImmutableDictionary<Address, IImmutableSet<Currency>>.Empty;

public IImmutableSet<Currency> TotalSupplyUpdatedCurrencies
=> ImmutableHashSet<Currency>.Empty;

public IAccountStateDelta BurnAsset(Address owner, FungibleAssetValue value)
{
return new AddressTraceStateDelta(_updatedAddresses.Union(new [] { owner }));
}

public FungibleAssetValue GetBalance(Address address, Currency currency)
{
throw new NotSupportedException();
}

public IValue GetState(Address address)
{
throw new NotSupportedException();
}

public IReadOnlyList<IValue> GetStates(IReadOnlyList<Address> addresses)
{
throw new NotSupportedException();
}

public FungibleAssetValue GetTotalSupply(Currency currency)
{
throw new NotSupportedException();
}

public IAccountStateDelta MintAsset(Address recipient, FungibleAssetValue value)
{
return new AddressTraceStateDelta(_updatedAddresses.Union(new[] { recipient }));
}

public IAccountStateDelta SetState(Address address, IValue state)
{
return new AddressTraceStateDelta(_updatedAddresses.Union(new[] { address }));
}

public IAccountStateDelta TransferAsset(
Address sender,
Address recipient,
FungibleAssetValue value,
bool allowNegativeBalance = false
)
{
return new AddressTraceStateDelta(
_updatedAddresses.Union(new[] { sender, recipient })
);
}
}
}
}

0 comments on commit 9066f70

Please sign in to comment.