-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Calculate UpdatedAddresses on Lib9c side
- Loading branch information
Showing
2 changed files
with
172 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }) | ||
); | ||
} | ||
} | ||
} | ||
} |