Skip to content

Commit

Permalink
Merge pull request #2645 from planetarium/release/220
Browse files Browse the repository at this point in the history
Backmerge 220
  • Loading branch information
U-lis authored Nov 29, 2024
2 parents 2cfe859 + ac3229c commit d4b5c84
Show file tree
Hide file tree
Showing 26 changed files with 357 additions and 122 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<Project>
<PropertyGroup>
<LibplanetVersion>5.3.2-alpha.1</LibplanetVersion>
<LibplanetVersion>5.4.1</LibplanetVersion>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion Lib9c
Submodule Lib9c updated 344 files
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
using Xunit;
using Lib9cUtils = Lib9c.DevExtensions.Utils;
using CoconaUtils = Libplanet.Extensions.Cocona.Utils;
using Libplanet.Types.Assets;
using Nekoyume.TableData;

namespace NineChronicles.Headless.Executable.Tests.Commands
{
Expand All @@ -41,6 +43,7 @@ public class ChainCommandTest : IDisposable
// Because the purpose of ChainCommandTest is to store and read blockchain data.
private readonly StringIOConsole _console;
private readonly ChainCommand _command;
private readonly Dictionary<string, string> _sheets;

private readonly string _storePath;

Expand All @@ -50,6 +53,7 @@ public ChainCommandTest()
_command = new ChainCommand(_console);

_storePath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
_sheets = TableSheetsImporter.ImportSheets();
}

[Theory]
Expand Down Expand Up @@ -91,14 +95,28 @@ public void Inspect(StoreType storeType)
policyActionsRegistry: blockPolicy.PolicyActionsRegistry,
stateStore,
new NCActionLoader());
var sheets = TableSheetsImporter.ImportSheets();
Block genesisBlock = BlockChain.ProposeGenesisBlock(
transactions: new IAction[]
{
new Initialize(
validatorSet: new ValidatorSet(
new[] { new Validator(proposer.PublicKey, BigInteger.One) }.ToList()
new InitializeStates(
validatorSet: new ValidatorSet(new List<Validator>
{
new Validator(proposer.PublicKey, 10_000_000_000_000_000_000)
}),
rankingState: new RankingState0(),
shopState: new ShopState(),
gameConfigState: new GameConfigState(sheets[nameof(GameConfigSheet)]),
redeemCodeState: new RedeemCodeState(
Bencodex.Types.Dictionary.Empty
.Add("address", RedeemCodeState.Address.Serialize())
.Add("map", Bencodex.Types.Dictionary.Empty)
),
states: ImmutableDictionary.Create<Address, IValue>()
activatedAccountsState: new ActivatedAccountsState(),
goldCurrencyState: new GoldCurrencyState(Currency.Uncapped("ncg", 2, null)),
goldDistributions: new GoldDistribution[] { },
tableSheets: sheets,
pendingActivationStates: new PendingActivationState[] { }
)
}.Select((sa, nonce) => Transaction.Create(nonce, new PrivateKey(), null, new[] { sa.PlainValue }))
.ToImmutableList());
Expand Down Expand Up @@ -151,15 +169,25 @@ public void Truncate(StoreType storeType)
policyActionsRegistry: blockPolicy.PolicyActionsRegistry,
stateStore,
new NCActionLoader());
var validatorSet = new ValidatorSet(
new[] { new Validator(proposer.PublicKey, 10_000_000_000_000_000_000) }.ToList());
var gameConfigState = new GameConfigState(_sheets[nameof(GameConfigSheet)]);
var redeemCodeListSheet = new RedeemCodeListSheet();
Block genesisBlock = BlockChain.ProposeGenesisBlock(
transactions: new IAction[]
{
new Initialize(
validatorSet: new ValidatorSet(
new[] { new Validator(proposer.PublicKey, BigInteger.One) }.ToList()
),
states: ImmutableDictionary.Create<Address, IValue>()
)
new InitializeStates(
validatorSet: validatorSet,
rankingState: new RankingState0(),
shopState: new ShopState(),
tableSheets: _sheets,
gameConfigState: gameConfigState,
redeemCodeState: new RedeemCodeState(redeemCodeListSheet),
adminAddressState: null,
activatedAccountsState: new ActivatedAccountsState(ImmutableHashSet<Address>.Empty),
goldCurrencyState: new GoldCurrencyState(Currency.Uncapped("ncg", 2, null), 0),
goldDistributions: Array.Empty<GoldDistribution>(),
pendingActivationStates: Array.Empty<PendingActivationState>())
}.Select((sa, nonce) => Transaction.Create(nonce, new PrivateKey(), null, new[] { sa.PlainValue }))
.ToImmutableList());
BlockChain chain = BlockChain.Create(
Expand Down Expand Up @@ -407,18 +435,16 @@ private Block MineGenesisBlock()
AdminState adminState =
new AdminState(new Address(genesisConfig.AdminAddress), genesisConfig.AdminValidUntil);
Block genesisBlock = BlockHelper.ProposeGenesisBlock(
new ValidatorSet(new List<Validator>
{
new Validator(GenesisHelper.ValidatorKey.PublicKey, 10_000_000_000_000_000_000)
}),
tableSheets,
goldDistributions,
pendingActivationStates.ToArray(),
adminState,
authorizedMinersState,
ImmutableHashSet<Address>.Empty,
new Dictionary<PublicKey, BigInteger>
{
{
GenesisHelper.ValidatorKey.PublicKey, BigInteger.One
}
},
genesisConfig.ActivationKeyCount != 0,
null,
new PrivateKey(ByteUtil.ParseHex(genesisConfig.PrivateKey))
Expand All @@ -439,7 +465,7 @@ private Block MineGenesisBlock()
block.Hash,
DateTimeOffset.UtcNow,
validator.PublicKey,
null,
10_000_000_000_000_000_000,
VoteFlag.PreCommit).Sign(validator)))
: null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,18 @@ public static Block MineGenesisBlock(
AdminState adminState =
new AdminState(new Address(genesisConfig.AdminAddress), genesisConfig.AdminValidUntil);
Block genesisBlock = BlockHelper.ProposeGenesisBlock(
new ValidatorSet(
genesisValidatorSet?.Select(kv => new Validator(kv.Key, kv.Value)).ToList()
?? new List<Validator>
{
new Validator(ValidatorKey.PublicKey, 10_000_000_000_000_000_000)
}),
tableSheets,
goldDistributions,
pendingActivationStates.ToArray(),
adminState,
authorizedMinersState,
ImmutableHashSet<Address>.Empty,
genesisValidatorSet ?? new Dictionary<PublicKey, BigInteger>
{
{
ValidatorKey.PublicKey,
BigInteger.One
}
},
genesisConfig.ActivationKeyCount != 0,
null,
new PrivateKey(ByteUtil.ParseHex(genesisConfig.PrivateKey))
Expand All @@ -109,7 +108,7 @@ public static void AppendEmptyBlock(BlockChain blockChain)
block.Hash,
new[]
{
new VoteMetadata(block.Index, 0, block.Hash, block.Timestamp, ValidatorKey.PublicKey, null, VoteFlag.PreCommit).Sign(ValidatorKey),
new VoteMetadata(block.Index, 0, block.Hash, block.Timestamp, ValidatorKey.PublicKey, 10_000_000_000_000_000_000, VoteFlag.PreCommit).Sign(ValidatorKey),
}.ToImmutableArray());
blockChain.Append(block, blockCommit);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
<PackageReference Include="MagicOnion.Client" Version="6.1.3" />
</ItemGroup>

<ItemGroup>
<Compile Include="..\Lib9c\.Lib9c.Tests\TableSheets.cs" Link="TableSheets.cs" />
<Compile Include="..\Lib9c\.Lib9c.Tests\TableSheetsImporter.cs" Link="TableSheetsImporter.cs" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\NineChronicles.Headless.Executable\NineChronicles.Headless.Executable.csproj" />
</ItemGroup>
Expand Down
15 changes: 15 additions & 0 deletions NineChronicles.Headless.Executable.Tests/TableSheetsImporter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Collections.Generic;
using System.IO;

namespace NineChronicles.Headless.Executable.Tests
{
public static class TableSheetsImporter
{
public static Dictionary<string, string> ImportSheets() =>
Lib9c.Tests.TableSheetsImporter.ImportSheets(Path
.GetFullPath("../../")
.Replace(
Path.Combine("NineChronicles.Headless.Executable.Tests", "bin"),
Path.Combine("Lib9c", "Lib9c", "TableCSV")));
}
}
29 changes: 23 additions & 6 deletions NineChronicles.Headless.Executable/Commands/GenesisCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Libplanet.Crypto;
using Libplanet.Types.Assets;
using Libplanet.Types.Blocks;
using Libplanet.Types.Consensus;
using Nekoyume;
using Nekoyume.Action;
using Nekoyume.Model.State;
Expand Down Expand Up @@ -168,7 +169,8 @@ private void ProcessValidator(List<Validator>? config, PrivateKey initialValidat
initialValidatorSet.Add(new Validator
{
PublicKey = initialValidator.PublicKey.ToString(),
Power = 1,
// To act as a validator, you need at least 10 GuildGold, and since the DecimalPlaces of GuildGold are 18 digits, it is recommended to specify a value around 10^20.
Power = "10000000000000000000",
}
);
}
Expand Down Expand Up @@ -263,20 +265,33 @@ public void Mine(

ProcessInitialPledgeConfigs(genesisConfig.InitialPledgeConfigs, out var initialPledges);

ISet<Address>? assetMinters = null;
if (genesisConfig.AssetMinters is not null)
{
foreach (var address in genesisConfig.AssetMinters)
{
_console.Out.WriteLine($"Preparing asset minter address {address}...");
}
assetMinters = genesisConfig.AssetMinters.ToHashSet();
}

// Mine genesis block
_console.Out.WriteLine("\nMining genesis block...\n");
Block block = BlockHelper.ProposeGenesisBlock(
validatorSet: new ValidatorSet(
initialValidatorSet.Select(
v => new Libplanet.Types.Consensus.Validator(
new PublicKey(ByteUtil.ParseHex(v.PublicKey)),
BigInteger.Parse(v.Power))).ToList()),
tableSheets: tableSheets,
goldDistributions: initialDepositList.ToArray(),
pendingActivationStates: Array.Empty<PendingActivationState>(),
// FIXME Should remove default value after fixing parameter type on Lib9c side.
adminState: adminState ?? new AdminState(default, 0L),
privateKey: initialMinter,
initialValidators: initialValidatorSet.ToDictionary(
item => new PublicKey(ByteUtil.ParseHex(item.PublicKey)),
item => new BigInteger(item.Power)),
actionBases: adminMeads.Concat(initialMeads).Concat(initialPledges).Concat(GetAdditionalActionBases()),
goldCurrency: currency
goldCurrency: currency,
assetMinters: assetMinters
);

Lib9cUtils.ExportBlock(block, "genesis-block");
Expand Down Expand Up @@ -397,7 +412,7 @@ private struct Validator
{
public string PublicKey { get; set; }

public long Power { get; set; }
public string Power { get; set; }
}

[Serializable]
Expand Down Expand Up @@ -454,6 +469,8 @@ private struct GenesisConfig
public List<MeadConfig>? InitialMeadConfigs { get; set; }

public List<PledgeConfig>? InitialPledgeConfigs { get; set; }

public List<Address>? AssetMinters { get; set; }
}
#pragma warning restore S3459
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
using Libplanet.Types.Tx;
using Serilog;
using Libplanet.Types.Evidence;
using Libplanet.Types.Blocks;
using Libplanet.Types.Assets;

namespace NineChronicles.Headless.Executable.Commands
{
Expand Down Expand Up @@ -51,6 +53,8 @@ public ActionContext(

public int BlockProtocolVersion { get; }

public BlockCommit? LastCommit { get; }

public bool Rehearsal { get; }

public IWorld PreviousState { get; }
Expand All @@ -68,9 +72,7 @@ public void UseGas(long gas)
{
}

public long GasUsed() => 0;

public long GasLimit() => 0;
public FungibleAssetValue? MaxGasPrice => null;

public IRandom GetRandom() => new Random(RandomSeed);
}
Expand Down
20 changes: 4 additions & 16 deletions NineChronicles.Headless.Tests/Action/ActionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,30 @@
using Libplanet.Action.State;
using Libplanet.Crypto;
using Libplanet.Extensions.ActionEvaluatorCommonComponents;
using Libplanet.Types.Assets;
using Libplanet.Types.Blocks;
using Libplanet.Types.Evidence;
using Libplanet.Types.Tx;

namespace NineChronicles.Headless.Tests.Action;

public class ActionContext : IActionContext
{
private long UsedGas { get; set; }

public Address Signer { get; init; }
public TxId? TxId { get; init; }
public Address Miner { get; init; }
public long BlockIndex { get; init; }
public int BlockProtocolVersion { get; init; }
public BlockCommit LastCommit { get; init; }
public IWorld PreviousState { get; init; }
public int RandomSeed { get; init; }
public bool IsPolicyAction { get; init; }
public FungibleAssetValue? MaxGasPrice { get; set; }
public IReadOnlyList<ITransaction> Txs { get; init; }
public IReadOnlyList<EvidenceBase> Evidence { get; init; }
public void UseGas(long gas)
{
UsedGas += gas;
}

public IRandom GetRandom()
{
return new Random(RandomSeed);
}

public long GasUsed()
{
return UsedGas;
}

public long GasLimit()
{
return 0L;
}
}
2 changes: 1 addition & 1 deletion NineChronicles.Headless.Tests/Common/Fixtures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static class Fixtures

public static readonly Address AvatarAddress = new Address("983c3Fbfe8243a0e36D55C6C1aE26A7c8Bb6CBd4");

public static readonly Address StakeStateAddress = StakeState.DeriveAddress(UserAddress);
public static readonly Address StakeStateAddress = LegacyStakeState.DeriveAddress(UserAddress);

public static readonly TableSheets TableSheetsFX = new(TableSheetsImporter.ImportSheets());

Expand Down
2 changes: 1 addition & 1 deletion NineChronicles.Headless.Tests/GraphQLTestUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ InitializeStates initializeStates
0,
block.Hash,
ValidatorPrivateKeys.Select(
k => new VoteMetadata(block.Index, 0, block.Hash, block.Timestamp, k.PublicKey, null, VoteFlag.PreCommit).Sign(k))
k => new VoteMetadata(block.Index, 0, block.Hash, block.Timestamp, k.PublicKey, BigInteger.One, VoteFlag.PreCommit).Sign(k))
.ToImmutableArray());

blockchain.Append(block, blockCommit);
Expand Down
2 changes: 2 additions & 0 deletions NineChronicles.Headless.Tests/GraphTypes/ActionQueryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Libplanet.Common;
using Libplanet.Crypto;
using Libplanet.Types.Assets;
using Libplanet.Types.Consensus;
using Libplanet.Types.Tx;
using Nekoyume;
using Nekoyume.Action;
Expand Down Expand Up @@ -45,6 +46,7 @@ public ActionQueryTest()
new Random().NextBytes(_nonce);
(_activationKey, PendingActivationState pending) = ActivationKey.Create(_activationCodeSeed, _nonce);
var initializeStates = new InitializeStates(
validatorSet: new ValidatorSet(new List<Validator> { new Validator(MinerPrivateKey.PublicKey, 1) }),
rankingState: new RankingState0(),
shopState: new ShopState(),
gameConfigState: new GameConfigState(),
Expand Down
Loading

0 comments on commit d4b5c84

Please sign in to comment.