Skip to content

Commit

Permalink
Merge pull request #3049 from planetarium/release/1.20.0
Browse files Browse the repository at this point in the history
Release 1.20.0
  • Loading branch information
U-lis authored Nov 27, 2024
2 parents cb3d878 + 64e8cdd commit 06593af
Show file tree
Hide file tree
Showing 459 changed files with 21,305 additions and 11,823 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
1 change: 1 addition & 0 deletions .Lib9c.Miner.Tests/Lib9c.Proposer.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Lib9c.Policy\Lib9c.Policy.csproj" />
<ProjectReference Include="..\Lib9c.Proposer\Lib9c.Proposer.csproj" />
</ItemGroup>

Expand Down
145 changes: 145 additions & 0 deletions .Lib9c.Miner.Tests/ProposerTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
using System.Collections.Immutable;
using Lib9c.Renderers;
using Libplanet.Action;
using Libplanet.Blockchain.Policies;
using Libplanet.Blockchain;
using Libplanet.Crypto;
using Libplanet.Store;
using Libplanet.Store.Trie;
using Libplanet.Types.Assets;
using Libplanet.Types.Blocks;
using Libplanet.Types.Consensus;
using Libplanet.Types.Tx;
using Nekoyume.Action;
using Nekoyume.Action.Loader;
using Nekoyume.Blockchain.Policy;
using Nekoyume.Model.State;
using System.Numerics;

namespace Lib9c.Proposer.Tests
{
public class ProposerTest
{
private readonly PrivateKey _admin;
private readonly PrivateKey _proposer;
private readonly BlockChain _blockChain;

public ProposerTest()
{
_admin = new PrivateKey();
_proposer = new PrivateKey();
var ncg = Currency.Uncapped("ncg", 2, null);
var policy = new DebugPolicy();
var actionTypeLoader = new NCActionLoader();
IStagePolicy stagePolicy = new VolatileStagePolicy();
var mint = new PrepareRewardAssets
{
RewardPoolAddress = _proposer.Address,
Assets = new List<FungibleAssetValue>
{
1 * Currencies.Mead,
},
};

var validatorSet = new ValidatorSet(
new List<Validator> { new(_proposer.PublicKey, 10_000_000_000_000_000_000) });

var initializeStates = new InitializeStates(
validatorSet,
new RankingState0(),
new ShopState(),
new Dictionary<string, string>(),
new GameConfigState(),
new RedeemCodeState(new Dictionary<PublicKey, RedeemCodeState.Reward>()),
new ActivatedAccountsState(),
new GoldCurrencyState(ncg),
new GoldDistribution[] { },
new PendingActivationState[] { });

List <ActionBase> actions = new List<ActionBase>
{
initializeStates,
};

var genesis = BlockChain.ProposeGenesisBlock(
privateKey: _proposer,
transactions: ImmutableList<Transaction>.Empty
.Add(Transaction.Create(
0, _proposer, null, actions.ToPlainValues())),
timestamp: DateTimeOffset.MinValue);

var store = new MemoryStore();
var stateStore = new TrieStateStore(new MemoryKeyValueStore());

_blockChain = BlockChain.Create(
policy,
stagePolicy,
store,
stateStore,
genesis,
new ActionEvaluator(
policy.PolicyActionsRegistry,
stateStore,
new NCActionLoader()
),
new[] { new BlockRenderer(), }
);
}

[Fact]
public void ProposeBlock()
{
Block block = _blockChain.ProposeBlock(_proposer);
_blockChain.Append(
block,
GenerateBlockCommit(
block,
_proposer,
10_000_000_000_000_000_000));
}

[Fact]
public void AssertInvalidProposer()
{
Block block = _blockChain.ProposeBlock(_proposer);
Assert.Throws<InvalidBlockCommitException>(() => _blockChain.Append(
block,
GenerateBlockCommit(
block,
new PrivateKey(),
10_000_000_000_000_000_000)));
}

[Fact]
public void AssertInvalidPower()
{
Block block = _blockChain.ProposeBlock(_proposer);
Assert.Throws<InvalidBlockCommitException>(() => _blockChain.Append(
block,
GenerateBlockCommit(
block,
_proposer,
10_000_000_000_000_000)));
}

private BlockCommit? GenerateBlockCommit(
Block block, PrivateKey privateKey, BigInteger power)
{
return block.Index != 0
? new BlockCommit(
block.Index,
0,
block.Hash,
ImmutableArray.Create(
new VoteMetadata(
block.Index,
0,
block.Hash,
DateTimeOffset.UtcNow,
privateKey.PublicKey,
power,
VoteFlag.PreCommit).Sign(privateKey)))
: null;
}
}
}
12 changes: 7 additions & 5 deletions .Lib9c.Tests/Action/AccountStateDeltaExtensionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void SetWorldBossKillReward(int level, int expectedRune, int expectedCrys
IWorld states = new World(MockUtil.MockModernWorldState);
var rewardInfoAddress = new PrivateKey().Address;
var rewardRecord = new WorldBossKillRewardRecord();
for (int i = 0; i < level; i++)
for (var i = 0; i < level; i++)
{
rewardRecord[i] = false;
}
Expand All @@ -67,11 +67,13 @@ public void SetWorldBossKillReward(int level, int expectedRune, int expectedCrys
);
var bossId = bossState.Id;
var runeWeightSheet = new RuneWeightSheet();
runeWeightSheet.Set($@"id,boss_id,rank,rune_id,weight
runeWeightSheet.Set(
$@"id,boss_id,rank,rune_id,weight
1,{bossId},0,10001,100
");
var killRewardSheet = new WorldBossKillRewardSheet();
killRewardSheet.Set($@"id,boss_id,rank,rune_min,rune_max,crystal,circle
killRewardSheet.Set(
$@"id,boss_id,rank,rune_min,rune_max,crystal,circle
1,{bossId},0,1,1,100,0
");

Expand Down Expand Up @@ -176,8 +178,8 @@ public void Mead(int agentBalance)
var agentContractAddress = _agentAddress.GetPledgeAddress();
var mead = Currencies.Mead;
var price = RequestPledge.DefaultRefillMead * mead;
ActionContext context = new ActionContext();
IWorld states = new World(MockUtil.MockModernWorldState)
var context = new ActionContext();
var states = new World(MockUtil.MockModernWorldState)
.SetLegacyState(
agentContractAddress,
List.Empty.Add(patron.Serialize()).Add(true.Serialize()))
Expand Down
32 changes: 16 additions & 16 deletions .Lib9c.Tests/Action/AccountStateViewExtensionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ public void TryGetAvatarStateKeyNotFoundException()
{
var states = new World(MockUtil.MockModernWorldState)
.SetLegacyState(
default,
Dictionary.Empty
.Add("agentAddress", default(Address).Serialize())
);
default,
Dictionary.Empty
.Add("agentAddress", default(Address).Serialize())
);

Assert.False(states.TryGetAvatarState(default, default, out _));
}
Expand Down Expand Up @@ -110,7 +110,7 @@ public void GetAvatarStateV2()
[InlineData("questList")]
public void GetAvatarStateV2_Throw_FailedLoadStateException(string account)
{
Address accountAddress = account switch
var accountAddress = account switch
{
"inventory" => Addresses.Inventory,
"worldInformation" => Addresses.WorldInformation,
Expand All @@ -131,7 +131,7 @@ public void GetAvatarStateV2_Throw_FailedLoadStateException(string account)
[Fact]
public void TryGetAvatarState()
{
IWorld states = new World(MockUtil.MockModernWorldState).SetAvatarState(_avatarAddress, _avatarState);
var states = new World(MockUtil.MockModernWorldState).SetAvatarState(_avatarAddress, _avatarState);
Assert.True(states.TryGetAvatarState(_agentAddress, _avatarAddress, out _));
}

Expand Down Expand Up @@ -193,8 +193,8 @@ public void GetSheets()
public void GetCrystalCostState(bool exist)
{
IWorld states = new World(MockUtil.MockModernWorldState);
int expectedCount = exist ? 1 : 0;
FungibleAssetValue expectedCrystal = exist
var expectedCount = exist ? 1 : 0;
var expectedCrystal = exist
? 100 * CrystalCalculator.CRYSTAL
: 0 * CrystalCalculator.CRYSTAL;
Address address = default;
Expand All @@ -205,7 +205,7 @@ public void GetCrystalCostState(bool exist)
states = states.SetLegacyState(address, crystalCostState.Serialize());
}

CrystalCostState actual = states.GetCrystalCostState(address);
var actual = states.GetCrystalCostState(address);
Assert.Equal(expectedCount, actual.Count);
Assert.Equal(expectedCrystal, actual.CRYSTAL);
}
Expand All @@ -217,15 +217,15 @@ public void GetCrystalCostState(bool exist)
[InlineData(151_200L, true)]
public void GetCrystalCostStates(long blockIndex, bool previousWeeklyExist)
{
long interval = _tableSheets.CrystalFluctuationSheet.Values.First(r => r.Type == CrystalFluctuationSheet.ServiceType.Combination).BlockInterval;
var interval = _tableSheets.CrystalFluctuationSheet.Values.First(r => r.Type == CrystalFluctuationSheet.ServiceType.Combination).BlockInterval;
var weeklyIndex = (int)(blockIndex / interval);
Address dailyCostAddress =
var dailyCostAddress =
Addresses.GetDailyCrystalCostAddress((int)(blockIndex / CrystalCostState.DailyIntervalIndex));
Address weeklyCostAddress = Addresses.GetWeeklyCrystalCostAddress(weeklyIndex);
Address previousCostAddress = Addresses.GetWeeklyCrystalCostAddress(weeklyIndex - 1);
Address beforePreviousCostAddress = Addresses.GetWeeklyCrystalCostAddress(weeklyIndex - 2);
var weeklyCostAddress = Addresses.GetWeeklyCrystalCostAddress(weeklyIndex);
var previousCostAddress = Addresses.GetWeeklyCrystalCostAddress(weeklyIndex - 1);
var beforePreviousCostAddress = Addresses.GetWeeklyCrystalCostAddress(weeklyIndex - 2);
var crystalCostState = new CrystalCostState(default, 100 * CrystalCalculator.CRYSTAL);
IWorld state = new World(MockUtil.MockModernWorldState)
var state = new World(MockUtil.MockModernWorldState)
.SetLegacyState(dailyCostAddress, crystalCostState.Serialize())
.SetLegacyState(weeklyCostAddress, crystalCostState.Serialize())
.SetLegacyState(previousCostAddress, crystalCostState.Serialize())
Expand Down Expand Up @@ -311,7 +311,7 @@ public void GetCouponWallet()

states = states.SetLegacyState(
agentAddress1.Derive(CouponWalletKey),
(Bencodex.Types.Binary)new byte[] { 0x00 });
(Bencodex.Types.Binary)new byte[] { 0x00, });

Assert.Throws<InvalidCastException>(() => states.GetCouponWallet(agentAddress1));
Assert.Throws<InvalidCastException>(() => states.GetCouponWallet(agentAddress2));
Expand Down
33 changes: 18 additions & 15 deletions .Lib9c.Tests/Action/ActionContextExtensionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public static IEnumerable<object[]> IsMainNetTestcases()
Currency.Legacy("BTC", 2, new Address("47d082a115c63e7b58b1532d20e631538eafadde"))),
false,
};
#pragma warning restore CS0618
#pragma warning restore CS0618
}

public static IEnumerable<object[]> IsPreviewNetTestcases()
Expand Down Expand Up @@ -156,20 +156,23 @@ public void IsMainNet(GoldCurrencyState goldCurrencyState, bool expected)
[Fact]
public void Since()
{
Assert.True(new ActionContext
{
BlockIndex = 1001,
}.Since(1000));

Assert.True(new ActionContext
{
BlockIndex = 0,
}.Since(0));

Assert.False(new ActionContext
{
BlockIndex = 0,
}.Since(1));
Assert.True(
new ActionContext
{
BlockIndex = 1001,
}.Since(1000));

Assert.True(
new ActionContext
{
BlockIndex = 0,
}.Since(0));

Assert.False(
new ActionContext
{
BlockIndex = 0,
}.Since(1));
}
}
}
18 changes: 10 additions & 8 deletions .Lib9c.Tests/Action/ActionEvaluationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ private ActionBase GetAction(Type type)
new PrivateKey().Address,
ItemSubType.Armor,
#pragma warning disable CS0618
// Use of obsolete method Currency.Legacy(): https://github.com/planetarium/lib9c/discussions/1319
// Use of obsolete method Currency.Legacy(): https://github.com/planetarium/lib9c/discussions/1319
Currency.Legacy("NCG", 2, null) * 10
#pragma warning restore CS0618
),
Expand All @@ -181,7 +181,7 @@ private ActionBase GetAction(Type type)
CombinationConsumable _ => new CombinationConsumable(),
CombinationEquipment _ => new CombinationEquipment(),
CreatePendingActivation _ => new CreatePendingActivation(
new PendingActivationState(new byte[] { 0x0 }, new PrivateKey().PublicKey)
new PendingActivationState(new byte[] { 0x0, }, new PrivateKey().PublicKey)
),
DailyReward _ => new DailyReward(),
InitializeStates _ => new InitializeStates
Expand Down Expand Up @@ -437,13 +437,15 @@ private ActionBase GetAction(Type type)
CreatePledge _ => new CreatePledge
{
PatronAddress = new PrivateKey().Address,
AgentAddresses = new[] { (new PrivateKey().Address, new PrivateKey().Address) },
AgentAddresses = new[] { (new PrivateKey().Address, new PrivateKey().Address), },
Mead = 4,
},
TransferAssets _ => new TransferAssets(_sender, new List<(Address, FungibleAssetValue)>
{
(_signer, 1 * _currency),
}),
TransferAssets _ => new TransferAssets(
_sender,
new List<(Address, FungibleAssetValue)>
{
(_signer, 1 * _currency),
}),
RuneSummon _ => new RuneSummon
{
AvatarAddress = _sender,
Expand Down Expand Up @@ -476,7 +478,7 @@ private ActionBase GetAction(Type type)
),
},
},
RetrieveAvatarAssets _ => new RetrieveAvatarAssets(avatarAddress: new PrivateKey().Address),
RetrieveAvatarAssets _ => new RetrieveAvatarAssets(new PrivateKey().Address),
MigrateFee _ => new MigrateFee
{
TransferData = new List<(Address sender, Address recipient, BigInteger amount)>
Expand Down
Loading

0 comments on commit 06593af

Please sign in to comment.