Skip to content

Commit

Permalink
Merge pull request #2398 from greymistcube/merge/libplanet-4.0-into-d…
Browse files Browse the repository at this point in the history
…evelopment

🔀 Merge libplanet-4.0 into development
  • Loading branch information
greymistcube authored Feb 1, 2024
2 parents 285d5e9 + 953af4d commit 13af3c4
Show file tree
Hide file tree
Showing 51 changed files with 1,153 additions and 1,013 deletions.
2 changes: 1 addition & 1 deletion Lib9c
Submodule Lib9c updated 330 files
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ public void CheckPairs()
class PostActionEvaluator : IActionEvaluator
{
public IActionLoader ActionLoader => throw new NotSupportedException();
public IReadOnlyList<ICommittedActionEvaluation> Evaluate(IPreEvaluationBlock block, HashDigest<SHA256>? baseStateroothash)
public IReadOnlyList<ICommittedActionEvaluation> Evaluate(
IPreEvaluationBlock block, HashDigest<SHA256>? baseStateRootHash)
{
return new ICommittedActionEvaluation[]
{
Expand All @@ -85,7 +86,8 @@ public IReadOnlyList<ICommittedActionEvaluation> Evaluate(IPreEvaluationBlock bl
class PreActionEvaluator : IActionEvaluator
{
public IActionLoader ActionLoader => throw new NotSupportedException();
public IReadOnlyList<ICommittedActionEvaluation> Evaluate(IPreEvaluationBlock block, HashDigest<SHA256>? baseStateRootHash)
public IReadOnlyList<ICommittedActionEvaluation> Evaluate(
IPreEvaluationBlock block, HashDigest<SHA256>? baseStateRootHash)
{
return new ICommittedActionEvaluation[]
{
Expand Down Expand Up @@ -113,7 +115,7 @@ public void LoadPlainValue(IValue plainValue)
{
}

public IAccount Execute(IActionContext context) => context.PreviousState;
public IWorld Execute(IActionContext context) => context.PreviousState;
}

class MockBlock : IPreEvaluationBlock
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,15 @@ public static IPluginActionEvaluator CreateActionEvaluator(Assembly assembly, st
public static IPluginActionEvaluator CreateActionEvaluator(string pluginPath, string typeName, IKeyValueStore keyValueStore)
=> CreateActionEvaluator(LoadPlugin(pluginPath), typeName, new PluginKeyValueStore(keyValueStore));

public IReadOnlyList<ICommittedActionEvaluation> Evaluate(IPreEvaluationBlock block, HashDigest<SHA256>? baseStateRootHash)
=> _pluginActionEvaluator.Evaluate(
PreEvaluationBlockMarshaller.Serialize(block),
baseStateRootHash is { } srh ? srh.ToByteArray() : null)
public IReadOnlyList<ICommittedActionEvaluation> Evaluate(
IPreEvaluationBlock block,
HashDigest<SHA256>? baseStateRootHash)
{
var evaluations = _pluginActionEvaluator.Evaluate(
PreEvaluationBlockMarshaller.Serialize(block),
baseStateRootHash is { } srh ? srh.ToByteArray() : null)
.Select(eval => ActionEvaluationMarshaller.Deserialize(eval)).ToList().AsReadOnly();
return evaluations;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ private class DummyAction : IAction
{
IValue IAction.PlainValue => Dictionary.Empty;

IAccount IAction.Execute(IActionContext context)
IWorld IAction.Execute(IActionContext context)
{
return context.PreviousState;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using Libplanet.Common;
using Libplanet.Crypto;
using Nekoyume.Action;
using Nekoyume.Action.Factory;
using Nekoyume.Model;
using Nekoyume.Model.State;
using NineChronicles.Headless.Executable.Commands;
Expand Down Expand Up @@ -59,7 +58,6 @@ public void ActivateAccount(bool invalid, int expectedCode)
}
}


[Theory]
[InlineData(10, 0, "transfer asset test1.")]
[InlineData(100, 0, "transfer asset test2.")]
Expand Down Expand Up @@ -142,79 +140,6 @@ public void ClaimStakeReward(string addressString, int expectedCode)
}
}

[Theory]
[InlineData(0L, typeof(ClaimStakeReward2))]
[InlineData(ClaimStakeReward2.ObsoletedIndex, typeof(ClaimStakeReward2))]
[InlineData(ClaimStakeReward2.ObsoletedIndex + 1, typeof(ClaimStakeReward3))]
[InlineData(ClaimStakeReward3.ObsoleteBlockIndex, typeof(ClaimStakeReward3))]
[InlineData(ClaimStakeReward3.ObsoleteBlockIndex + 1, typeof(ClaimStakeReward4))]
[InlineData(ClaimStakeReward4.ObsoleteBlockIndex, typeof(ClaimStakeReward4))]
[InlineData(ClaimStakeReward4.ObsoleteBlockIndex + 1, typeof(ClaimStakeReward5))]
[InlineData(ClaimStakeReward5.ObsoleteBlockIndex, typeof(ClaimStakeReward5))]
[InlineData(ClaimStakeReward5.ObsoleteBlockIndex + 1, typeof(ClaimStakeReward6))]
[InlineData(ClaimStakeReward6.ObsoleteBlockIndex, typeof(ClaimStakeReward6))]
[InlineData(ClaimStakeReward6.ObsoleteBlockIndex + 1, typeof(ClaimStakeReward7))]
[InlineData(ClaimStakeReward7.ObsoleteBlockIndex, typeof(ClaimStakeReward7))]
[InlineData(ClaimStakeReward7.ObsoleteBlockIndex + 1, typeof(ClaimStakeReward8))]
[InlineData(ClaimStakeReward8.ObsoleteBlockIndex, typeof(ClaimStakeReward8))]
[InlineData(ClaimStakeReward8.ObsoleteBlockIndex + 1, typeof(ClaimStakeReward))]
[InlineData(long.MaxValue, typeof(ClaimStakeReward))]
public void ClaimStakeRewardWithBlockIndex(long blockIndex, Type expectedActionType)
{
var filePath = Path.Combine(Path.GetTempPath(), Path.GetTempFileName());
var addr = new PrivateKey().Address;
var resultCode = _command.ClaimStakeReward(
addr.ToHex(),
filePath,
blockIndex: blockIndex);
Assert.Equal(0, resultCode);

var rawAction = Convert.FromBase64String(File.ReadAllText(filePath));
var decoded = (List)_codec.Decode(rawAction);
var plainValue = Assert.IsType<Dictionary>(decoded[1]);
var action = ClaimStakeRewardFactory.CreateByBlockIndex(blockIndex, addr);
Assert.NotNull(action);
var actionType = action.GetType();
Assert.Equal(expectedActionType, actionType);
action.LoadPlainValue(plainValue);
string type = (Text)decoded[0];
Assert.Equal(type, actionType.Name);
}

[Theory]
[InlineData(0, 0, -1)]
[InlineData(1, 9, 0)]
[InlineData(10, 10, -1)]
public void ClaimStakeRewardWithActionVersion(
int actionVersionMin,
int actionVersionMax,
int expectedCode)
{
for (var i = actionVersionMin; i < actionVersionMax + 1; i++)
{
var filePath = Path.Combine(Path.GetTempPath(), Path.GetTempFileName());
var addr = new PrivateKey().Address;
var resultCode = _command.ClaimStakeReward(
addr.ToHex(),
filePath,
actionVersion: i);
Assert.Equal(expectedCode, resultCode);

if (expectedCode < 0)
{
continue;
}

var rawAction = Convert.FromBase64String(File.ReadAllText(filePath));
var decoded = (List)_codec.Decode(rawAction);
var plainValue = Assert.IsType<Dictionary>(decoded[1]);
var action = ClaimStakeRewardFactory.CreateByVersion(i, addr);
action.LoadPlainValue(plainValue);
string type = (Text)decoded[0];
Assert.Equal(action.GetType().Name, type);
}
}

[Theory]
[InlineData("0xab1dce17dCE1Db1424BB833Af6cC087cd4F5CB6d", -1)]
[InlineData("ab1dce17dCE1Db1424BB833Af6cC087cd4F5CB6d", 0)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ public void Sign_Stake(bool gas)
[InlineData(ClaimStakeReward2.ObsoletedIndex - 1, null, false)]
[InlineData(ClaimStakeReward2.ObsoletedIndex, null, true)]
[InlineData(ClaimStakeReward2.ObsoletedIndex + 1, null, false)]
[InlineData(ClaimStakeReward3.ObsoleteBlockIndex - 1, null, true)]
[InlineData(long.MaxValue, null, true)]
[InlineData(null, 1, false)]
[InlineData(null, 2, true)]
Expand All @@ -92,9 +91,7 @@ public void Sign_ClaimStakeReward(long? blockIndex, int? actionVersion, bool gas
var avatarAddress = new Address();
actionCommand.ClaimStakeReward(
avatarAddress.ToHex(),
filePath,
blockIndex,
actionVersion);
filePath);
Assert_Tx(1, filePath, gas);
}

Expand Down
9 changes: 6 additions & 3 deletions NineChronicles.Headless.Executable/Commands/AccountCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using Bencodex;
using Cocona;
using Libplanet.Action.State;
using Libplanet.Blockchain;
using Libplanet.Crypto;
using Libplanet.Store;
Expand All @@ -11,6 +12,7 @@
using Libplanet.Types.Tx;
using Nekoyume.Action;
using Nekoyume.Model.State;
using Nekoyume.Module;
using NineChronicles.Headless.Executable.IO;
using Serilog.Core;
using static NineChronicles.Headless.NCActionUtils;
Expand Down Expand Up @@ -52,15 +54,16 @@ public void Balance(
Block offset = DevExUtils.ParseBlockOffset(chain, block);
_console.Error.WriteLine("The offset block: #{0} {1}.", offset.Index, offset.Hash);

IWorldState worldState = chain.GetWorldState(offset.Hash);
Bencodex.Types.Dictionary goldCurrencyStateDict = (Bencodex.Types.Dictionary)
chain.GetState(GoldCurrencyState.Address);
worldState.GetLegacyState(GoldCurrencyState.Address);
GoldCurrencyState goldCurrencyState = new GoldCurrencyState(goldCurrencyStateDict);
Currency gold = goldCurrencyState.Currency;

if (address is { } addrStr)
{
Address addr = DevExUtils.ParseAddress(addrStr);
FungibleAssetValue balance = chain.GetBalance(addr, gold, offset.Hash);
FungibleAssetValue balance = worldState.GetBalance(addr, gold);
_console.Out.WriteLine("{0}\t{1}", addr, balance);
return;
}
Expand Down Expand Up @@ -96,7 +99,7 @@ public void Balance(
{
if (!printed.Contains(addr))
{
FungibleAssetValue balance = chain.GetBalance(addr, gold, offset.Hash);
FungibleAssetValue balance = worldState.GetBalance(addr, gold);
_console.Out.WriteLine("{0}\t{1}", addr, balance);
printed.Add(addr);
}
Expand Down
35 changes: 2 additions & 33 deletions NineChronicles.Headless.Executable/Commands/ActionCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
using Libplanet.Crypto;
using Libplanet.Types.Assets;
using Nekoyume.Action;
using Nekoyume.Action.Factory;
using Nekoyume.Model;
using NineChronicles.Headless.Executable.IO;

Expand Down Expand Up @@ -220,44 +219,14 @@ public int ClaimStakeReward(
[Argument("AVATAR-ADDRESS", Description = "A hex-encoded avatar address.")]
string encodedAddress,
[Argument("PATH", Description = "A file path of base64 encoded action.")]
string? filePath = null,
[Option("BLOCK-INDEX", Description = "A block index which is used to specifying the action version.")]
long? blockIndex = null,
[Option("ACTION-VERSION", Description = "A version of action.")]
int? actionVersion = null
string? filePath = null
)
{
try
{
if (blockIndex.HasValue && actionVersion.HasValue)
{
throw new CommandExitedException(
"You can't specify both block index and action version at the same time.",
-1);
}

Address avatarAddress = new Address(ByteUtil.ParseHex(encodedAddress));
IClaimStakeReward? action = null;
if (blockIndex.HasValue)
{
action = ClaimStakeRewardFactory.CreateByBlockIndex(
blockIndex.Value,
avatarAddress);
}
else if (actionVersion.HasValue)
{
action = ClaimStakeRewardFactory.CreateByVersion(
actionVersion.Value,
avatarAddress);
}

// NOTE: If neither block index nor action version is specified,
// it will be created by the type of the class.
// I considered to create action with max value of
// block index(i.e., long.MaxValue), but it is not good
// because the action of the next version may come along
// with the current version.
action ??= new ClaimStakeReward(avatarAddress);
action = new ClaimStakeReward(avatarAddress);

byte[] raw = Codec.Encode(new List(
new[]
Expand Down
3 changes: 2 additions & 1 deletion NineChronicles.Headless.Executable/Commands/MarketCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Libplanet.Types.Tx;
using Nekoyume.Action;
using Nekoyume.Model.Item;
using Nekoyume.Module;
using NineChronicles.Headless.Executable.IO;
using Serilog.Core;
using static NineChronicles.Headless.NCActionUtils;
Expand Down Expand Up @@ -130,7 +131,7 @@ public void Query(
{
int? quantity = null;
if (p.OrderId is { } oid &&
chain.GetState(GetOrderAddress(oid)) is Dictionary rawOrder)
chain.GetWorldState().GetLegacyState(GetOrderAddress(oid)) is Dictionary rawOrder)
{
if (OrderFactory.Deserialize(rawOrder) is FungibleOrder fo)
{
Expand Down
Loading

0 comments on commit 13af3c4

Please sign in to comment.