Skip to content

Commit

Permalink
Merge branch 'development' into test/improve-exception-test
Browse files Browse the repository at this point in the history
  • Loading branch information
eugene-doobu authored Nov 20, 2024
2 parents 1c1ed43 + f2951f4 commit 0543ab3
Show file tree
Hide file tree
Showing 69 changed files with 10,929 additions and 4,256 deletions.
18 changes: 14 additions & 4 deletions .Lib9c.Benchmarks/Lib9c.Benchmarks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,24 @@
<PackageReference Include="BenchmarkDotNet" Version="0.12.1" />
</ItemGroup>

<ItemGroup Condition="!'$(UseLocalLibplanet)'">
<PackageReference Include="Libplanet" Version="$(LibplanetVersion)" />
<PackageReference Include="Libplanet.RocksDBStore" Version="$(LibplanetVersion)" />
<PackageReference Include="Libplanet.Crypto.Secp256k1" Version="$(LibplanetVersion)" />
<PackageReference Include="Libplanet.Mocks" Version="$(LibplanetVersion)" />
</ItemGroup>

<ItemGroup Condition="'$(UseLocalLibplanet)'">
<ProjectReference Include="$(LibplanetDirectory)\src\Libplanet\Libplanet.csproj" />
<ProjectReference Include="$(LibplanetDirectory)\src\Libplanet.RocksDBStore\Libplanet.RocksDBStore.csproj" />
<ProjectReference Include="$(LibplanetDirectory)\src\Libplanet.Crypto.Secp256k1\Libplanet.Crypto.Secp256k1.csproj" />
<ProjectReference Include="$(LibplanetDirectory)\test\Libplanet.Mocks\Libplanet.Mocks.csproj" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\.Lib9c.Tests\Lib9c.Tests.csproj" />
<ProjectReference Include="..\.Libplanet\src\Libplanet.RocksDBStore\Libplanet.RocksDBStore.csproj" />
<ProjectReference Include="..\.Libplanet\src\Libplanet\Libplanet.csproj" />
<ProjectReference Include="..\Lib9c\Lib9c.csproj" />
<ProjectReference Include="..\Lib9c.Policy\Lib9c.Policy.csproj" />
<ProjectReference Include="..\Libplanet.Crypto.Secp256k1\Libplanet.Crypto.Secp256k1.csproj" />
<ProjectReference Include="..\.Libplanet\test\Libplanet.Mocks\Libplanet.Mocks.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ public void WantedMultipleSeason()
NcgReward = 0 * NCG, // No Raffle Reward
FavReward = new Dictionary<int, int>
{
{ 10035, 84 }, // 100NCG * 1.2 * 0.7 Fixed / 1 NCG Ratio * 100% contribution for season 3
{ 10036, 84 }, // 100NCG * 1.2 * 0.7 Fixed / 1 NCG Ratio * 100% contribution for season 3
},
ItemReward = new Dictionary<int, int>
{
Expand Down Expand Up @@ -732,7 +732,7 @@ public void ExploreMultipleSeason()
NcgReward = 40 * NCG,
FavReward = new Dictionary<int, int>
{
{ 10035, 40 }, // (100 AP * 0.4 Exchange / 1 Ratio * 100% contribution) for season 3
{ 10036, 40 }, // (100 AP * 0.4 Exchange / 1 Ratio * 100% contribution) for season 3
},
ItemReward = new Dictionary<int, int>
{
Expand Down
181 changes: 181 additions & 0 deletions .Lib9c.Tests/Action/ClaimGiftsTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
namespace Lib9c.Tests.Action
{
using System;
using System.Linq;
using Libplanet.Action.State;
using Libplanet.Crypto;
using Libplanet.Mocks;
using Nekoyume;
using Nekoyume.Action;
using Nekoyume.Model.Item;
using Nekoyume.Model.State;
using Nekoyume.Module;
using Xunit;

public class ClaimGiftsTest
{
private readonly TableSheets _tableSheets;
private readonly IWorld _state;

public ClaimGiftsTest()
{
_state = new World(MockUtil.MockModernWorldState);

var tableCsv = TableSheetsImporter.ImportSheets();
foreach (var (key, value) in tableCsv)
{
_state = _state.SetLegacyState(Addresses.GetSheetAddress(key), value.Serialize());
}

_tableSheets = new TableSheets(tableCsv);
}

[Theory]
[InlineData(1)]
[InlineData(300)]
[InlineData(600)]
[InlineData(1200)]
public void Execute_Success(long blockIndex)
{
var agentAddress = new PrivateKey().Address;
var avatarAddress = Addresses.GetAvatarAddress(agentAddress, 0);

var avatarState = AvatarState.Create(
avatarAddress,
agentAddress,
0,
_tableSheets.GetAvatarSheets(),
default);
var state = _state.SetAvatarState(avatarAddress, avatarState);

if (!_tableSheets.ClaimableGiftsSheet.TryFindRowByBlockIndex(blockIndex, out var row))
{
throw new Exception();
}

Execute(
state,
avatarAddress,
agentAddress,
row.Id,
blockIndex,
row.Items.ToArray()
);
}

[Fact]
public void Execute_ClaimableGiftsNotAvailableException()
{
var agentAddress = new PrivateKey().Address;
var avatarAddress = Addresses.GetAvatarAddress(agentAddress, 0);

var avatarState = AvatarState.Create(
avatarAddress,
agentAddress,
0,
_tableSheets.GetAvatarSheets(),
default);
var state = _state.SetAvatarState(avatarAddress, avatarState);
var sheet = _tableSheets.ClaimableGiftsSheet;

Assert.Throws<ClaimableGiftsNotAvailableException>(() =>
{
var row = sheet.Values.OrderBy(row => row.StartedBlockIndex).First();
Execute(
state,
avatarAddress,
agentAddress,
row.Id,
row.StartedBlockIndex - 1,
row.Items.ToArray()
);
});
Assert.Throws<ClaimableGiftsNotAvailableException>(() =>
{
var row = sheet.Values.OrderByDescending(row => row.EndedBlockIndex).First();
Execute(
state,
avatarAddress,
agentAddress,
row.Id,
row.EndedBlockIndex + 1,
row.Items.ToArray()
);
});
}

[Fact]
public void Execute_AlreadyClaimedGiftsException()
{
var agentAddress = new PrivateKey().Address;
var avatarAddress = Addresses.GetAvatarAddress(agentAddress, 0);

var avatarState = AvatarState.Create(
avatarAddress,
agentAddress,
0,
_tableSheets.GetAvatarSheets(),
default);
var state = _state.SetAvatarState(avatarAddress, avatarState);

var row = _tableSheets.ClaimableGiftsSheet.Values.First();
var blockIndex = row.StartedBlockIndex;

var nextState = Execute(
state,
avatarAddress,
agentAddress,
row.Id,
blockIndex,
row.Items.ToArray()
);
Assert.Throws<AlreadyClaimedGiftsException>(() =>
{
Execute(
nextState,
avatarAddress,
agentAddress,
row.Id,
blockIndex + 1,
row.Items.ToArray()
);
});
}

private IWorld Execute(
IWorld previousState,
Address avatarAddress,
Address agentAddress,
int giftId,
long blockIndex,
(int itemId, int quantity, bool tradable)[] expected)
{
var prevClaimedGifts = _state.GetClaimedGifts(avatarAddress);

var action = new ClaimGifts(avatarAddress, giftId);
var actionContext = new ActionContext
{
PreviousState = previousState,
Signer = agentAddress,
BlockIndex = blockIndex,
};

var nextState = action.Execute(actionContext);

// Check claimed gifts.
var nextClaimedGifts = nextState.GetClaimedGifts(avatarAddress);
Assert.Equal(prevClaimedGifts.Count + 1, nextClaimedGifts.Count);

// Check Inventory.
var inventory = nextState.GetInventoryV2(avatarAddress);
foreach (var (itemId, quantity, tradable) in expected)
{
Assert.True(inventory.TryGetItem(itemId, out var inventoryItem));
Assert.Equal(quantity, inventoryItem.count);
Assert.Equal(tradable, inventoryItem.item is ITradableItem);
}

return nextState;
}
}
}
38 changes: 29 additions & 9 deletions .Lib9c.Tests/Action/CreatePledgeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace Lib9c.Tests.Action
using Nekoyume.Action;
using Nekoyume.Model.State;
using Nekoyume.Module;
using Nekoyume.Module.Guild;
using Serilog;
using Xunit;
using Xunit.Abstractions;
Expand All @@ -24,25 +25,29 @@ public CreatePledgeTest(ITestOutputHelper outputHelper)
}

[Theory]
[InlineData(true, null)]
[InlineData(false, typeof(PermissionDeniedException))]
public void Execute(bool admin, Type exc)
[InlineData(true, false, null)]
[InlineData(true, true, null)]
[InlineData(false, false, typeof(PermissionDeniedException))]
public void Execute(bool admin, bool plPatron, Type exc)
{
var adminAddress = new PrivateKey().Address;
var poolAddress = new PrivateKey().Address;
var adminState = new AdminState(adminAddress, 150L);
var patronAddress = new PrivateKey().Address;
var patronAddress = plPatron
? MeadConfig.PatronAddress
: new PrivateKey().Address;
var mead = Currencies.Mead;
var agentAddress = new PrivateKey().Address;
var pledgeAddress = agentAddress.GetPledgeAddress();
var pledgedAddress = new PrivateKey().Address;
var pledgeAddress = pledgedAddress.GetPledgeAddress();
var agentAddress = new Nekoyume.TypedAddress.AgentAddress(pledgedAddress);
var context = new ActionContext();
var states = new World(MockUtil.MockModernWorldState)
.SetLegacyState(Addresses.Admin, adminState.Serialize())
.MintAsset(context, patronAddress, 4 * 500 * mead);

var agentAddresses = new List<(Address, Address)>
{
(agentAddress, pledgeAddress),
(pledgedAddress, pledgeAddress),
};
for (var i = 0; i < 499; i++)
{
Expand All @@ -67,9 +72,24 @@ public void Execute(bool admin, Type exc)
if (exc is null)
{
var nextState = action.Execute(actionContext);

Assert.Equal(0 * mead, nextState.GetBalance(patronAddress, mead));
Assert.Equal(4 * mead, nextState.GetBalance(agentAddress, mead));
Assert.Equal(4 * mead, nextState.GetBalance(pledgedAddress, mead));

var planetariumGuildOwner = Nekoyume.Action.Guild.GuildConfig.PlanetariumGuildOwner;
var guildAddress = nextState.GetJoinedGuild(planetariumGuildOwner);
Assert.NotNull(guildAddress);
Assert.True(nextState.TryGetGuild(guildAddress.Value, out var guild));
Assert.Equal(planetariumGuildOwner, guild.GuildMasterAddress);
if (!plPatron)
{
Assert.Null(nextState.GetJoinedGuild(agentAddress));
}
else
{
var joinedGuildAddress = Assert.IsType<Nekoyume.TypedAddress.GuildAddress>(nextState.GetJoinedGuild(agentAddress));
Assert.True(nextState.TryGetGuild(joinedGuildAddress, out var joinedGuild));
Assert.Equal(Nekoyume.Action.Guild.GuildConfig.PlanetariumGuildOwner, joinedGuild.GuildMasterAddress);
}
}
else
{
Expand Down
4 changes: 4 additions & 0 deletions .Lib9c.Tests/Action/EventDungeonBattleTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,10 @@ out var balance
)
);
Assert.Equal(0 * _ncgCurrency, balance);
var arenaData = _tableSheets.ArenaSheet.GetRoundByBlockIndex(scheduleRow.StartBlockIndex);
var feeStoreAddress =
Nekoyume.Arena.ArenaHelper.DeriveArenaAddress(arenaData.ChampionshipId, arenaData.Round);
Assert.Equal(ncgHas, nextStates.GetBalance(feeStoreAddress, _ncgCurrency));
}

[Theory]
Expand Down
9 changes: 8 additions & 1 deletion .Lib9c.Tests/Lib9c.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,14 @@
<ProjectReference Include="..\Lib9c\Lib9c.csproj" />
<ProjectReference Include="..\Lib9c.Utils\Lib9c.Utils.csproj" />
<ProjectReference Include="..\Lib9c.MessagePack\Lib9c.MessagePack.csproj" />
<ProjectReference Include="..\.Libplanet\test\Libplanet.Mocks\Libplanet.Mocks.csproj" />
</ItemGroup>

<ItemGroup Condition="!'$(UseLocalLibplanet)'">
<PackageReference Include="Libplanet.Mocks" Version="$(LibplanetVersion)" />
</ItemGroup>

<ItemGroup Condition="'$(UseLocalLibplanet)'">
<ProjectReference Include="$(LibplanetDirectory)\test\Libplanet.Mocks\Libplanet.Mocks.csproj" />
</ItemGroup>

<ItemGroup Condition=" '$(Configuration)' != 'DevEx' ">
Expand Down
33 changes: 33 additions & 0 deletions .Lib9c.Tests/TableData/Event/ClaimableGiftsSheetTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
namespace Lib9c.Tests.TableData.Event
{
using Nekoyume.TableData;
using Xunit;

public class ClaimableGiftsSheetTest
{
[Fact]
public void Set()
{
const string csv = @"id,started_block_index,ended_block_index,item_1_id,item_1_quantity,item_1_tradable,item_2_id,item_2_quantity,item_2_tradable,item_3_id,item_3_quantity,item_3_tradable,item_4_id,item_4_quantity,item_4_tradable,item_5_id,item_5_quantity,item_5_tradable
1,1,250,600402,5,true,,,,,,,,,,,,
2,251,500,40100030,1,true,,,,,,,,,,,,
3,501,1000,49900022,1,true,,,,,,,,,,,,
4,1001,1500,40100028,1,true,,,,,,,,,,,,
5,1501,2000,400000,5,false,,,,,,,,,,,,";

var sheet = new ClaimableGiftsSheet();
sheet.Set(csv);
Assert.Equal(5, sheet.Count);
Assert.NotNull(sheet.First);
Assert.NotNull(sheet.Last);
var row = sheet.First;
Assert.Equal(1, row.Id);
Assert.Equal(1, row.StartedBlockIndex);
Assert.Equal(250, row.EndedBlockIndex);
Assert.Single(row.Items);
Assert.Equal(600402, row.Items[0].itemId);
Assert.Equal(5, row.Items[0].quantity);
Assert.True(row.Items[0].tradable);
}
}
}
2 changes: 2 additions & 0 deletions .Lib9c.Tests/TableSheets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,8 @@ public TableSheets(Dictionary<string, string> sheets, bool ignoreFailedGetProper

/* Custom Craft */

public ClaimableGiftsSheet ClaimableGiftsSheet { get; private set; }

public void ItemSheetInitialize()
{
ItemSheet ??= new ItemSheet();
Expand Down
9 changes: 8 additions & 1 deletion .Lib9c.Tools/Lib9c.Tools.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,18 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\.Libplanet\src\Libplanet.RocksDBStore\Libplanet.RocksDBStore.csproj" />
<ProjectReference Include="..\Lib9c\Lib9c.csproj" />
<ProjectReference Include="..\Lib9c.DevExtensions\Lib9c.DevExtensions.csproj" />
</ItemGroup>

<ItemGroup Condition="!'$(UseLocalLibplanet)'">
<PackageReference Include="Libplanet.RocksDBStore" Version="$(LibplanetVersion)" />
</ItemGroup>

<ItemGroup Condition="'$(UseLocalLibplanet)'">
<ProjectReference Include="$(LibplanetDirectory)\src\Libplanet.RocksDBStore\Libplanet.RocksDBStore.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Cocona.Lite" Version="1.5.0" />
<PackageReference Include="Serilog" Version="2.10.0" />
Expand Down
1 change: 0 additions & 1 deletion .Libplanet
Submodule .Libplanet deleted from 46cff6
Loading

0 comments on commit 0543ab3

Please sign in to comment.