Skip to content

Commit

Permalink
feat: Add AppProtocolOptionsBase for common properties like PrivateKe…
Browse files Browse the repository at this point in the history
…y and APV
  • Loading branch information
s2quake committed Sep 2, 2024
1 parent a77298c commit 530b17a
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 30 deletions.
36 changes: 23 additions & 13 deletions sdk/node/Libplanet.Node.Executable/appsettings-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1103,15 +1103,15 @@
"properties": {
"GenesisKey": {
"type": "string",
"description": "The key of the genesis block.",
"description": "The PrivateKey used to generate the genesis block. This property cannot be used with GenesisBlockPath.",
"pattern": "^[0-9a-fA-F]{64}$"
},
"Validators": {
"type": "array",
"description": "Public keys of the validators.",
"description": "Public keys of the validators. This property cannot be used with GenesisBlockPath.",
"items": {
"type": "string",
"pattern": "^[0-9a-fA-F]{130}|[0-9a-fA-F]{66}$"
"pattern": "^(?:[0-9a-fA-F]{130}|[0-9a-fA-F]{66})$"
}
},
"Timestamp": {
Expand All @@ -1121,7 +1121,7 @@
},
"GenesisBlockPath": {
"type": "string",
"description": "The path of the genesis block."
"description": "The path of the genesis block, which can be a file path or a URI.This property cannot be used with GenesisKey."
}
}
},
Expand Down Expand Up @@ -1186,23 +1186,23 @@
"type": "object",
"additionalProperties": false,
"properties": {
"IsEnabled": {
"type": "boolean"
},
"PrivateKey": {
"type": "string",
"description": "The private key of the node.",
"description": "The private key.",
"pattern": "^[0-9a-fA-F]{64}$"
},
"EndPoint": {
"type": "string",
"pattern": "^(?:(?:[a-zA-Z0-9\\-\\.]+)|(?:\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})):\\d{1,5}$"
},
"AppProtocolVersion": {
"type": "string",
"description": "The version of the application protocol.",
"pattern": "^(?<version>\\d+)/(?<address>[0-9a-fA-F]{40})/(?<signature>(?:[A-Za-z0-9+\\.]{4})*(?:[A-Za-z0-9+\\.]{2}==|[A-Za-z0-9+\\.]{3}=)?)/(?<extra>(?:[A-Za-z0-9+\\.]{4})*(?:[A-Za-z0-9+\\.]{2}==|[A-Za-z0-9+\\.]{3}=)?)$"
},
"IsEnabled": {
"type": "boolean"
},
"EndPoint": {
"type": "string",
"pattern": "^(?:(?:[a-zA-Z0-9\\-\\.]+)|(?:\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})):\\d{1,5}$"
},
"BlocksyncSeedPeer": {
"type": "string",
"description": "The endpoint of the node to block sync.",
Expand All @@ -1215,6 +1215,16 @@
"type": "object",
"additionalProperties": false,
"properties": {
"PrivateKey": {
"type": "string",
"description": "The private key.",
"pattern": "^[0-9a-fA-F]{64}$"
},
"AppProtocolVersion": {
"type": "string",
"description": "The version of the application protocol.",
"pattern": "^(?<version>\\d+)/(?<address>[0-9a-fA-F]{40})/(?<signature>(?:[A-Za-z0-9+\\.]{4})*(?:[A-Za-z0-9+\\.]{2}==|[A-Za-z0-9+\\.]{3}=)?)/(?<extra>(?:[A-Za-z0-9+\\.]{4})*(?:[A-Za-z0-9+\\.]{2}==|[A-Za-z0-9+\\.]{3}=)?)$"
},
"IsEnabled": {
"type": "boolean",
"default": true
Expand Down Expand Up @@ -1265,4 +1275,4 @@
}
}
]
}
}
12 changes: 12 additions & 0 deletions sdk/node/Libplanet.Node/Options/AppProtocolOptionsBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.ComponentModel;
using Libplanet.Node.DataAnnotations;

namespace Libplanet.Node.Options;

public abstract class AppProtocolOptionsBase<T> : OptionsBase<T>
where T : AppProtocolOptionsBase<T>
{
[AppProtocolVersion]
[Description("The version of the application protocol.")]
public string AppProtocolVersion { get; set; } = string.Empty;
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public async Task<string> BuildAsync(CancellationToken cancellationToken)
var settings = new SystemTextJsonSchemaGeneratorSettings
{
ExcludedTypeNames = [optionsType.FullName!],
FlattenInheritanceHierarchy = true,
};
var schemaGenerator = new OptionsSchemaGenerator(settings);
var typeSchema = schemaGenerator.Generate(type);
Expand Down
10 changes: 1 addition & 9 deletions sdk/node/Libplanet.Node/Options/SeedOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,11 @@

namespace Libplanet.Node.Options;

public sealed class SeedOptions : OptionsBase<SeedOptions>
public sealed class SeedOptions : AppProtocolOptionsBase<SeedOptions>
{
public const string BlocksyncSeed = nameof(BlocksyncSeed);
public const string ConsensusSeed = nameof(ConsensusSeed);

[PrivateKey]
[Description("The private key of the seed node.")]
public string PrivateKey { get; set; } = string.Empty;

[AppProtocolVersion]
[Description("The version of the application protocol.")]
public string AppProtocolVersion { get; set; } = string.Empty;

[DnsEndPoint]
[Description("The endpoint of the seed node.")]
public string EndPoint { get; set; } = string.Empty;
Expand Down
8 changes: 2 additions & 6 deletions sdk/node/Libplanet.Node/Options/SwarmOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,19 @@
namespace Libplanet.Node.Options;

[Options(Position)]
public sealed class SwarmOptions : OptionsBase<SwarmOptions>, IEnabledOptions
public sealed class SwarmOptions : AppProtocolOptionsBase<SwarmOptions>, IEnabledOptions
{
public const string Position = "Swarm";

public bool IsEnabled { get; set; }

[PrivateKey]
[Description("The private key of the node.")]
[Description("The private key of Swarm.")]
public string PrivateKey { get; set; } = string.Empty;

[DnsEndPoint]
public string EndPoint { get; set; } = string.Empty;

[AppProtocolVersion]
[Description("The version of the application protocol.")]
public string AppProtocolVersion { get; set; } = string.Empty;

[BoundPeer]
[Description("The endpoint of the node to block sync.")]
public string BlocksyncSeedPeer { get; set; } = string.Empty;
Expand Down
7 changes: 5 additions & 2 deletions sdk/node/Libplanet.Node/Options/ValidatorOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
namespace Libplanet.Node.Options;

[Options(Position)]
public sealed class ValidatorOptions : OptionsBase<ValidatorOptions>, IEnabledOptions
public sealed class ValidatorOptions : AppProtocolOptionsBase<ValidatorOptions>, IEnabledOptions
{
public const string Position = "Validator";

[DefaultValue(true)]
public bool IsEnabled { get; set; }

[PrivateKey]
[Description("The private key of Validator.")]
public string PrivateKey { get; set; } = string.Empty;

[DnsEndPoint]
public string EndPoint { get; set; } = string.Empty;

Expand Down
20 changes: 20 additions & 0 deletions sdk/node/Libplanet.Node/Options/ValidatorOptionsConfigurator.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,34 @@
using Libplanet.Common;
using Libplanet.Crypto;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace Libplanet.Node.Options;

internal sealed class ValidatorOptionsConfigurator(
IOptions<SwarmOptions> swarmOptions,
ILogger<ValidatorOptionsConfigurator> logger)
: OptionsConfiguratorBase<ValidatorOptions>
{
protected override void OnConfigure(ValidatorOptions options)
{
if (options.PrivateKey == string.Empty)
{
options.PrivateKey = swarmOptions.Value.PrivateKey;
logger.LogWarning(
"Validator's private key is not set. Use the swarm's private key: {PrivateKey}",
options.PrivateKey);
}

if (options.AppProtocolVersion == string.Empty)
{
options.AppProtocolVersion = swarmOptions.Value.AppProtocolVersion;
logger.LogWarning(
"Validator's app protocol version is not set. Use the swarm's " +
"app protocol version: {AppProtocolVersion}",
options.AppProtocolVersion);
}

if (options.EndPoint == string.Empty)
{
options.EndPoint = EndPointUtility.ToString(EndPointUtility.Next());
Expand Down

0 comments on commit 530b17a

Please sign in to comment.