Skip to content

Commit

Permalink
Add product info in cancel mail
Browse files Browse the repository at this point in the history
  • Loading branch information
ipdae committed Oct 2, 2024
1 parent 84926d5 commit 31fd759
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 4 deletions.
62 changes: 62 additions & 0 deletions .Lib9c.Tests/Action/CancelProductRegistrationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ namespace Lib9c.Tests.Action
using Nekoyume.Action;
using Nekoyume.Helper;
using Nekoyume.Model;
using Nekoyume.Model.Item;
using Nekoyume.Model.Mail;
using Nekoyume.Model.Market;
using Nekoyume.Model.State;
using Nekoyume.Module;
using Newtonsoft.Json.Serialization;
using Serilog;
using Xunit;
using Xunit.Abstractions;
Expand Down Expand Up @@ -210,5 +213,64 @@ public void Execute_Throw_ArgumentOutOfRangeException()

Assert.Throws<ArgumentOutOfRangeException>(() => action.Execute(new ActionContext()));
}

[Theory]
[InlineData(ProductType.FungibleAssetValue)]
[InlineData(ProductType.NonFungible)]
[InlineData(ProductType.Fungible)]
public void Mail_Serialize_BackwardCompatibility(ProductType productType)
{
Product product;
var gold = _goldCurrencyState.Currency;
switch (productType)
{
case ProductType.FungibleAssetValue:
product = new FavProduct
{
SellerAgentAddress = new PrivateKey().Address,
SellerAvatarAddress = new PrivateKey().Address,
Asset = 1 * RuneHelper.StakeRune,
RegisteredBlockIndex = 1L,
ProductId = Guid.NewGuid(),
Price = 1 * gold,
Type = ProductType.FungibleAssetValue,
};
break;
case ProductType.Fungible:
case ProductType.NonFungible:
{
ITradableItem tradableItem = productType == ProductType.Fungible
? ItemFactory.CreateTradableMaterial(_tableSheets.MaterialItemSheet.First)
: (ITradableItem)ItemFactory.CreateItemUsable(_tableSheets.EquipmentItemSheet.First, Guid.NewGuid(), 0L);
product = new ItemProduct
{
SellerAgentAddress = new PrivateKey().Address,
SellerAvatarAddress = new PrivateKey().Address,
RegisteredBlockIndex = 1L,
ProductId = Guid.NewGuid(),
Price = 1 * gold,
Type = ProductType.NonFungible,
ItemCount = 1,
TradableItem = tradableItem,
};
break;
}

default:
throw new ArgumentOutOfRangeException(nameof(productType), productType, null);
}

var mail = new ProductCancelMail(2L, Guid.NewGuid(), 2L, product!.ProductId, product!);
var serialized = (Dictionary)mail.Serialize();
var deserialized = new ProductCancelMail(serialized);
Assert.Equal(serialized, deserialized.Serialize());
// serialized mail on v200220;
serialized = (Dictionary)serialized.Remove((Text)ProductCancelMail.ProductKey);
deserialized = new ProductCancelMail(serialized);
Assert.Equal(deserialized.ProductId, product.ProductId);
Assert.Null(deserialized.Product);
// check serialize not throw exception
deserialized.Serialize();
}
}
}
2 changes: 1 addition & 1 deletion Lib9c/Action/CancelProductRegistration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public static IWorld Cancel(
throw new ArgumentOutOfRangeException(nameof(product));
}

var mail = new ProductCancelMail(context.BlockIndex, productId, context.BlockIndex, productId);
var mail = new ProductCancelMail(context.BlockIndex, productId, context.BlockIndex, productId, product);
avatarState.Update(mail);
states = states.SetLegacyState(productAddress, Null.Value);
return states;
Expand Down
23 changes: 20 additions & 3 deletions Lib9c/Model/Mail/ProductCancelMail.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using Bencodex.Types;
using Nekoyume.Model.Market;
using Nekoyume.Model.State;
using static Lib9c.SerializeKeys;

Expand All @@ -8,15 +9,22 @@ namespace Nekoyume.Model.Mail
[Serializable]
public class ProductCancelMail : Mail
{
public const string ProductKey = "p";
public readonly Guid ProductId;
public ProductCancelMail(long blockIndex, Guid id, long requiredBlockIndex, Guid productId) : base(blockIndex, id, requiredBlockIndex)
public readonly Product Product;
public ProductCancelMail(long blockIndex, Guid id, long requiredBlockIndex, Guid productId, Product product) : base(blockIndex, id, requiredBlockIndex)
{
ProductId = productId;
Product = product;
}

public ProductCancelMail(Dictionary serialized) : base(serialized)
{
ProductId = serialized[ProductIdKey].ToGuid();
if (serialized.ContainsKey(ProductKey))
{
Product = ProductFactory.DeserializeProduct((List) serialized[ProductKey]);
}
}

public override void Read(IMail mail)
Expand All @@ -27,7 +35,16 @@ public override void Read(IMail mail)
public override MailType MailType => MailType.Auction;
protected override string TypeId => nameof(ProductCancelMail);

public override IValue Serialize() => ((Dictionary)base.Serialize())
.Add(ProductIdKey, ProductId.Serialize());
public override IValue Serialize()
{
var dict = ((Dictionary) base.Serialize())
.Add(ProductIdKey, ProductId.Serialize());
if (Product is not null)
{
dict = dict.Add(ProductKey, Product.Serialize());
}

return dict;
}
}
}

0 comments on commit 31fd759

Please sign in to comment.