-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2189 from longfin/feature/action-obsoletion
- Loading branch information
Showing
6 changed files
with
194 additions
and
6 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,85 @@ | ||
namespace Lib9c.Tests.Policy | ||
{ | ||
using Bencodex.Types; | ||
using Libplanet.Action; | ||
using Libplanet.Action.Loader; | ||
using Libplanet.Action.State; | ||
using Libplanet.Crypto; | ||
using Libplanet.Types.Blocks; | ||
using Libplanet.Types.Tx; | ||
using Nekoyume; | ||
using Nekoyume.Action; | ||
using Nekoyume.Action.Loader; | ||
using Nekoyume.Blockchain.Policy; | ||
using Xunit; | ||
|
||
public class BlockPolicySourceTest | ||
{ | ||
private static readonly BlockHash OdinGenesisHash = BlockHash.FromString( | ||
"4582250d0da33b06779a8475d283d5dd210c683b9b999d74d03fac4f58fa6bce" | ||
); | ||
|
||
private static readonly BlockHash HeimdallGenesisHash = BlockHash.FromString( | ||
"ade4c29773fe83c1a51da6a667a5a26f08848155674637d43fe636b94a320514" | ||
); | ||
|
||
[Fact] | ||
public void IsObsolete() | ||
{ | ||
var transferAsset0 = new TransferAsset0(default, default, Currencies.Crystal * 0); | ||
var odinTx = Transaction.Create( | ||
0, | ||
new PrivateKey(), | ||
OdinGenesisHash, | ||
new[] { transferAsset0.PlainValue } | ||
); | ||
var heimdallTx = Transaction.Create( | ||
0, | ||
new PrivateKey(), | ||
HeimdallGenesisHash, | ||
new[] { transferAsset0.PlainValue } | ||
); | ||
var ncActionLoader = new NCActionLoader(); | ||
|
||
Assert.False(BlockPolicySource.IsObsolete(odinTx, ncActionLoader, 1)); | ||
Assert.True(BlockPolicySource.IsObsolete(odinTx, ncActionLoader, 7_000_700)); | ||
Assert.True(BlockPolicySource.IsObsolete(heimdallTx, ncActionLoader, 1)); | ||
|
||
var odinTx2 = Transaction.Create( | ||
0, | ||
new PrivateKey(), | ||
OdinGenesisHash, | ||
new[] { Dictionary.Empty } | ||
); | ||
var heimdallTx2 = Transaction.Create( | ||
0, | ||
new PrivateKey(), | ||
HeimdallGenesisHash, | ||
new[] { Dictionary.Empty } | ||
); | ||
var newActionLoader = new SingleActionLoader(typeof(NewAction)); | ||
|
||
Assert.False(BlockPolicySource.IsObsolete(odinTx2, newActionLoader, 1)); | ||
Assert.False(BlockPolicySource.IsObsolete(odinTx2, newActionLoader, 50)); | ||
Assert.True(BlockPolicySource.IsObsolete(odinTx2, newActionLoader, 103)); // Due to +2 offset for odin bug | ||
Assert.False(BlockPolicySource.IsObsolete(heimdallTx2, newActionLoader, 1)); | ||
Assert.True(BlockPolicySource.IsObsolete(heimdallTx2, newActionLoader, 51)); | ||
} | ||
|
||
[ActionObsolete(Planet.Odin, 100)] | ||
[ActionObsolete(Planet.Heimdall, 50)] | ||
private class NewAction : IAction | ||
{ | ||
public IValue PlainValue => (Bencodex.Types.Integer)42; | ||
|
||
public IAccount Execute(IActionContext context) | ||
{ | ||
throw new System.NotImplementedException(); | ||
} | ||
|
||
public void LoadPlainValue(IValue plainValue) | ||
{ | ||
} | ||
} | ||
} | ||
} |
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
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
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,32 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Nekoyume.Action; | ||
|
||
namespace Nekoyume | ||
{ | ||
public static class ActionObsoleteAttributeExtension | ||
{ | ||
public static bool IsObsolete( | ||
this IEnumerable<ActionObsoleteAttribute> attrs, | ||
Planet planet, | ||
long blockIndex | ||
) | ||
{ | ||
// it means that action had been obsoleted in the single planet era. | ||
// in that case, check index in Odin only, rest should be obsoleted. | ||
if (attrs.Count() == 1) | ||
{ | ||
if (planet != Planet.Odin) | ||
{ | ||
return true; | ||
} | ||
|
||
// Comparison with ObsoleteIndex + 2 is intended to have backward compatibility with | ||
// a bugged original implementation. | ||
return attrs.Single().ObsoleteIndex + 2 <= blockIndex; | ||
} | ||
|
||
return attrs.Any(attr => (attr.AffectedPlanet == Planet.Odin ? attr.ObsoleteIndex + 2 : attr.ObsoleteIndex) <= blockIndex && attr.AffectedPlanet == planet); | ||
} | ||
} | ||
} |
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,11 @@ | ||
using System; | ||
|
||
namespace Nekoyume | ||
{ | ||
public enum Planet : byte | ||
{ | ||
Odin, | ||
Heimdall, | ||
Idun, | ||
} | ||
} |
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,40 @@ | ||
using Libplanet.Types.Blocks; | ||
using Libplanet.Types.Tx; | ||
|
||
namespace Nekoyume | ||
{ | ||
public static class PlanetExtension | ||
{ | ||
private static readonly BlockHash OdinGenesisHash = BlockHash.FromString( | ||
"4582250d0da33b06779a8475d283d5dd210c683b9b999d74d03fac4f58fa6bce" | ||
); | ||
|
||
// FIXME should be changed after Heimdall/Idun mainnet launches | ||
private static readonly BlockHash HeimdallGenesisHash = BlockHash.FromString( | ||
"ade4c29773fe83c1a51da6a667a5a26f08848155674637d43fe636b94a320514" | ||
); | ||
|
||
private static readonly BlockHash IdunGenesisHash = BlockHash.FromString( | ||
"209b22087045ec834f01249c8661c2734cea41ccc5d8c9a273a4c8c0521d22ec" | ||
); | ||
|
||
public static Planet? DeterminePlanet(this ITransaction tx) | ||
{ | ||
// TODO Replace planet detection to using transaction payload instead. | ||
if (tx.GenesisHash.Equals(OdinGenesisHash)) | ||
{ | ||
return Planet.Odin; | ||
} | ||
if (tx.GenesisHash.Equals(HeimdallGenesisHash)) | ||
{ | ||
return Planet.Heimdall; | ||
} | ||
if (tx.GenesisHash.Equals(IdunGenesisHash)) | ||
{ | ||
return Planet.Idun; | ||
} | ||
|
||
return null; | ||
} | ||
} | ||
} |