Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/development' into test/new-stake
Browse files Browse the repository at this point in the history
  • Loading branch information
eugene-doobu committed Jan 9, 2025
2 parents a51f794 + f52482d commit 553f13c
Show file tree
Hide file tree
Showing 88 changed files with 790 additions and 782 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using Libplanet.Common;
using Libplanet.Crypto;

namespace Libplanet.Extensions.ActionEvaluatorCommonComponents.Tests;
namespace Lib9c.ActionEvaluatorCommonComponents.Tests;

public class ActionEvaluationSerializerTest
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@


<ItemGroup>
<ProjectReference Include="..\.Libplanet.Extensions.ActionEvaluatorCommonComponents\Libplanet.Extensions.ActionEvaluatorCommonComponents.csproj" />
<ProjectReference Include="..\.Lib9c.ActionEvaluatorCommonComponents\Lib9c.ActionEvaluatorCommonComponents.csproj" />
</ItemGroup>

</Project>
68 changes: 68 additions & 0 deletions .Lib9c.ActionEvaluatorCommonComponents/ActionContextMarshaller.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using System.Security.Cryptography;
using Bencodex;
using Bencodex.Types;
using Libplanet.Action;
using Libplanet.Common;
using Libplanet.Crypto;
using Libplanet.Types.Tx;
using Boolean = Bencodex.Types.Boolean;

namespace Lib9c.ActionEvaluatorCommonComponents
{
public static class ActionContextMarshaller
{
private static readonly Codec Codec = new Codec();

public static byte[] Serialize(this ICommittedActionContext actionContext)
{
return Codec.Encode(Marshal(actionContext));
}

public static Dictionary Marshal(this ICommittedActionContext actionContext)
{
var dictionary = Bencodex.Types.Dictionary.Empty
.Add("signer", actionContext.Signer.ToHex())
.Add("miner", actionContext.Miner.ToHex())
.Add("block_index", actionContext.BlockIndex)
.Add("block_protocol_version", actionContext.BlockProtocolVersion)
.Add("previous_states", actionContext.PreviousState.ByteArray)
.Add("random_seed", actionContext.RandomSeed)
.Add("block_action", actionContext.IsPolicyAction);

if (actionContext.TxId is { } txId)
{
dictionary = dictionary.Add("tx_id", txId.ByteArray);
}

return dictionary;
}

public static ICommittedActionContext Unmarshal(Dictionary dictionary)
{
return new CommittedActionContext(
signer: new Address(((Text)dictionary["signer"]).Value),
txId: dictionary.TryGetValue((Text)"tx_id", out IValue txIdValue) &&
txIdValue is Binary txIdBinaryValue
? new TxId(txIdBinaryValue.ByteArray)
: null,
miner: new Address(((Text)dictionary["miner"]).Value),
blockIndex: (Integer)dictionary["block_index"],
blockProtocolVersion: (Integer)dictionary["block_protocol_version"],
previousState: new HashDigest<SHA256>(dictionary["previous_states"]),
randomSeed: (Integer)dictionary["random_seed"],
isPolicyAction: (Boolean)dictionary["block_action"]
);
}

public static ICommittedActionContext Deserialize(byte[] serialized)
{
var decoded = Codec.Decode(serialized);
if (!(decoded is Dictionary dictionary))
{
throw new ArgumentException($"Expected 'Dictionary' but {decoded.GetType().Name}", nameof(serialized));
}

return Unmarshal(dictionary);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System.Security.Cryptography;
using Bencodex;
using Bencodex.Types;
using Libplanet.Action;
using Libplanet.Common;

namespace Lib9c.ActionEvaluatorCommonComponents
{
public static class ActionEvaluationMarshaller
{
private static readonly Codec Codec = new Codec();

public static byte[] Serialize(this ICommittedActionEvaluation actionEvaluation)
{
return Codec.Encode(Marshal(actionEvaluation));
}

public static Dictionary Marshal(this ICommittedActionEvaluation actionEvaluation) =>
Dictionary.Empty
.Add("action", actionEvaluation.Action)
.Add("output_states", actionEvaluation.OutputState.ByteArray)
.Add("input_context", actionEvaluation.InputContext.Marshal())
.Add("exception", actionEvaluation.Exception?.GetType().FullName is { } typeName ? (Text)typeName : Null.Value);

public static ICommittedActionEvaluation Unmarshal(IValue value)
{
if (value is not Dictionary dictionary)
{
throw new ArgumentException(nameof(value));
}

return new CommittedActionEvaluation(
dictionary["action"],
ActionContextMarshaller.Unmarshal((Dictionary)dictionary["input_context"]),
new HashDigest<SHA256>(dictionary["output_states"]),
dictionary["exception"] is Text typeName ? new Exception(typeName) : null
);
}

public static ICommittedActionEvaluation Deserialize(byte[] serialized)
{
var decoded = Codec.Decode(serialized);
return Unmarshal(decoded);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<OutputPath>.bin</OutputPath>
<IntermediateOutputPath>.obj</IntermediateOutputPath>
</PropertyGroup>

<ItemGroup Condition="!'$(UseLocalLibplanet)'">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using Bencodex;
using Bencodex.Types;
using Libplanet.Types.Blocks;

namespace Lib9c.ActionEvaluatorCommonComponents
{
public static class PreEvaluationBlockMarshaller
{
private static readonly Codec Codec = new Codec();

// Copied form Libplanet/Blocks/BlockMarshaler.cs
// Block fields:
private static readonly byte[] HeaderKey = { 0x48 }; // 'H'
private static readonly byte[] TransactionsKey = { 0x54 }; // 'T'

public static Dictionary Marshal(this IPreEvaluationBlock block)
{
return Dictionary.Empty
.Add(HeaderKey, BlockMarshaler.MarshalPreEvaluationBlockHeader(block))
.Add(TransactionsKey, new List(block.Transactions.Select(TransactionMarshaller.Serialize)));
}

public static IPreEvaluationBlock Unmarshal(Dictionary dictionary)
{
return new PreEvaluationBlock(
BlockMarshaler.UnmarshalPreEvaluationBlockHeader((Dictionary)dictionary[HeaderKey]),
BlockMarshaler.UnmarshalBlockTransactions(dictionary),
BlockMarshaler.UnmarshalBlockEvidence(dictionary));
}

public static byte[] Serialize(this IPreEvaluationBlock block)
{
return Codec.Encode(Marshal(block));
}

public static IPreEvaluationBlock Deserialize(byte[] serialized)
{
return Unmarshal((Dictionary)Codec.Decode(serialized));
}
}
}
15 changes: 15 additions & 0 deletions .Lib9c.ActionEvaluatorCommonComponents/Random.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Libplanet.Action;

namespace Lib9c.ActionEvaluatorCommonComponents
{
public class Random : System.Random, IRandom
{
public Random(int seed)
: base(seed)
{
Seed = seed;
}

public int Seed { get; private set; }
}
}
24 changes: 24 additions & 0 deletions .Lib9c.ActionEvaluatorCommonComponents/TransactionMarshaller.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Bencodex;
using Bencodex.Types;
using Libplanet.Types.Tx;

namespace Lib9c.ActionEvaluatorCommonComponents
{
public static class TransactionMarshaller
{
private static readonly Codec Codec = new Codec();

// Copied from Libplanet/Tx/TxMarshaler.cs
private static readonly Binary SignatureKey = new Binary(new byte[] { 0x53 }); // 'S'

public static Dictionary Marshal(this ITransaction transaction)
{
return transaction.MarshalUnsignedTx().Add(SignatureKey, transaction.Signature);
}

public static byte[] Serialize(this ITransaction transaction)
{
return Codec.Encode(Marshal(transaction));
}
}
}
2 changes: 2 additions & 0 deletions .Lib9c.Plugin.Shared/Lib9c.Plugin.Shared.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<OutputPath>.bin</OutputPath>
<IntermediateOutputPath>.obj</IntermediateOutputPath>
</PropertyGroup>

</Project>
2 changes: 1 addition & 1 deletion .Lib9c.Plugin/Lib9c.Plugin.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\.Libplanet.Extensions.ActionEvaluatorCommonComponents\Libplanet.Extensions.ActionEvaluatorCommonComponents.csproj" />
<ProjectReference Include="..\.Lib9c.ActionEvaluatorCommonComponents\Lib9c.ActionEvaluatorCommonComponents.csproj" />
<ProjectReference Include="..\Lib9c\Lib9c.csproj" />
<ProjectReference Include="..\.Lib9c.Plugin.Shared\Lib9c.Plugin.Shared.csproj">
<Private>false</Private>
Expand Down
2 changes: 1 addition & 1 deletion .Lib9c.Plugin/PluginActionEvaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using Lib9c.Plugin.Shared;
using Libplanet.Action;
using Libplanet.Common;
using Libplanet.Extensions.ActionEvaluatorCommonComponents;
using Lib9c.ActionEvaluatorCommonComponents;
using Libplanet.Store;
using Nekoyume.Action;
using Nekoyume.Action.Loader;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,7 @@ FungibleAssetValue expectedRemainingNcg
expectedRemainingNcg,
resultState.GetBalance(seasonBountyBoardAddress, NCG)
);
Assert.True(resultState.GetBalance(Addresses.RewardPool, NCG) > 0 * NCG);
Assert.True(resultState.GetExplorer(1, TesterAvatarAddress).Claimed);

if (anotherExplorerCount > 0)
Expand Down
8 changes: 7 additions & 1 deletion .Lib9c.Tests/Action/AdventureBoss/UnlockFloorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace Lib9c.Tests.Action.AdventureBoss
using Nekoyume;
using Nekoyume.Action;
using Nekoyume.Action.AdventureBoss;
using Nekoyume.Action.Guild.Migration.LegacyModels;
using Nekoyume.Model.Item;
using Nekoyume.Model.State;
using Nekoyume.Module;
Expand Down Expand Up @@ -84,7 +85,8 @@ public class UnlockFloorTest
.SetAgentState(WantedAddress, WantedState)
.SetAvatarState(TesterAvatarAddress, TesterAvatarState)
.SetAgentState(TesterAddress, TesterState)
.MintAsset(new ActionContext(), WantedAddress, 1_000_000 * NCG);
.MintAsset(new ActionContext(), WantedAddress, 1_000_000 * NCG)
.SetDelegationMigrationHeight(0);

[Theory]
// Success
Expand Down Expand Up @@ -217,6 +219,10 @@ Type exc
var inventory = resultState.GetInventoryV2(TesterAvatarAddress);
Assert.Null(inventory.Items.FirstOrDefault(i => i.item.Id == 600202));
}
else
{
Assert.True(resultState.GetBalance(Addresses.RewardPool, NCG) > 0 * NCG);
}

Assert.Equal(
expectedFloor,
Expand Down
2 changes: 1 addition & 1 deletion .Lib9c.Tests/Action/BuyMultipleTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ public void Execute(params ShopItemData[] productDatas)
// Case for backward compatibility of `Buy`
if (product.ContainsInInventory)
{
sellerAvatarState.inventory.AddItem2((ItemBase)nonFungibleItem);
sellerAvatarState.inventory.AddItem((ItemBase)nonFungibleItem);
}

var shopItemId = Guid.NewGuid();
Expand Down
13 changes: 6 additions & 7 deletions .Lib9c.Tests/Action/BuyTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ namespace Lib9c.Tests.Action
using Nekoyume;
using Nekoyume.Action;
using Nekoyume.Action.Guild.Migration.LegacyModels;
using Nekoyume.Arena;
using Nekoyume.Model;
using Nekoyume.Model.Item;
using Nekoyume.Model.Mail;
Expand All @@ -26,7 +25,6 @@ namespace Lib9c.Tests.Action
using Serilog;
using Xunit;
using Xunit.Abstractions;
using static Lib9c.SerializeKeys;

public class BuyTest
{
Expand Down Expand Up @@ -685,7 +683,7 @@ public void Execute_ReconfigureFungibleItem(params OrderData[] orderDataList)

var dummyItem = ItemFactory.CreateTradableMaterial(
_tableSheets.MaterialItemSheet.OrderedList.First(r => r.ItemSubType == ItemSubType.Hourglass));
sellerAvatarState.inventory.AddItem2((ItemBase)dummyItem, orderDataList.Sum(x => x.ItemCount));
sellerAvatarState.inventory.AddItem((ItemBase)dummyItem, orderDataList.Sum(x => x.ItemCount));

foreach (var orderData in orderDataList)
{
Expand Down Expand Up @@ -724,7 +722,7 @@ public void Execute_ReconfigureFungibleItem(params OrderData[] orderDataList)
);
_initialState.SetAvatarState(orderData.SellerAvatarAddress, sellerAvatarState);

var sellItem = order.Sell3(sellerAvatarState);
var sellItem = order.Sell(sellerAvatarState);
var orderDigest = order.Digest(sellerAvatarState, _tableSheets.CostumeStatSheet);

var digestListAddress = OrderDigestListState.DeriveAddress(firstData.SellerAvatarAddress);
Expand Down Expand Up @@ -767,9 +765,10 @@ public void Execute_ReconfigureFungibleItem(params OrderData[] orderDataList)
.SetLegacyState(orderDigestListState.Address, orderDigestListState.Serialize());
}

var sumCount = orderDataList.Sum(x => x.ItemCount);
Assert.Equal(1, sellerAvatarState.inventory.Items.Count);
Assert.Equal(sumCount, sellerAvatarState.inventory.Items.First().count);
// 2 -> because Locked item see AddFungibleItem(ItemBase itemBase, int count = 1, ILock iLock = null)
Assert.Equal(2, sellerAvatarState.inventory.Items.Count);
Assert.Equal(orderDataList[0].ItemCount, sellerAvatarState.inventory.Items.First().count);
Assert.Equal(orderDataList[1].ItemCount, sellerAvatarState.inventory.Items.Last().count);

var buyAction = new Buy
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#nullable enable

namespace Lib9c.Tests.Action.CustomEquipmentCraft
{
#nullable enable

using System;
using System.Collections.Generic;
using System.Globalization;
Expand All @@ -16,6 +16,7 @@ namespace Lib9c.Tests.Action.CustomEquipmentCraft
using Nekoyume.Action.CustomEquipmentCraft;
using Nekoyume.Action.Exceptions;
using Nekoyume.Action.Exceptions.CustomEquipmentCraft;
using Nekoyume.Action.Guild.Migration.LegacyModels;
using Nekoyume.Battle;
using Nekoyume.Exceptions;
using Nekoyume.Model.Elemental;
Expand Down Expand Up @@ -75,6 +76,7 @@ public CustomEquipmentCraftTest()
GameConfigState.Address,
new GameConfigState(sheets["GameConfigSheet"]).Serialize()
)
.SetDelegationMigrationHeight(0)
;

for (var i = 0; i < 4; i++)
Expand Down Expand Up @@ -371,6 +373,7 @@ public void Execute(

var gameConfig = state.GetGameConfigState();
var materialList = new List<int> { ScrollItemId, CircleItemId, };
bool costExist = false;
if (enoughMaterials)
{
var relationshipSheet = _tableSheets.CustomEquipmentCraftRelationshipSheet;
Expand Down Expand Up @@ -411,6 +414,7 @@ public void Execute(
{
if (nextRow.GoldAmount > 0)
{
costExist = true;
state = state.MintAsset(
context,
_agentAddress,
Expand Down Expand Up @@ -488,6 +492,10 @@ public void Execute(
// Test
var gold = resultState.GetGoldCurrency();
Assert.Equal(0 * gold, resultState.GetBalance(_agentAddress, gold));
if (costExist)
{
Assert.True(resultState.GetBalance(Addresses.RewardPool, gold) > 0 * gold);
}

var inventory = resultState.GetInventoryV2(_avatarAddress);
foreach (var material in materialList)
Expand Down
Loading

0 comments on commit 553f13c

Please sign in to comment.