Skip to content

Commit

Permalink
Merge pull request #3933 from planetarium/exp/sdk/bound-peer
Browse files Browse the repository at this point in the history
Remove BoundPeerUtility
  • Loading branch information
s2quake authored Aug 29, 2024
2 parents 1538d48 + 7777ff6 commit 8a8b57a
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 45 deletions.
4 changes: 2 additions & 2 deletions sdk/node/Libplanet.Node.Executable/appsettings-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1186,7 +1186,7 @@
"BlocksyncSeedPeer": {
"type": "string",
"description": "The endpoint of the node to block sync.",
"pattern": "^$|^[0-9a-fA-F]{130}|[0-9a-fA-F]{66},\\s*(?:(?:[a-zA-Z0-9\\-\\.]+)|(?:\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})):\\d{1,5}$"
"pattern": "^$|^(?:[0-9a-fA-F]{130}|[0-9a-fA-F]{66}),(?:(?:[a-zA-Z0-9\\-\\.]+)|(?:\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})),\\d{1,5}$"
}
}
},
Expand All @@ -1206,7 +1206,7 @@
"ConsensusSeedPeer": {
"type": "string",
"description": "The endpoint of the node to consensus.",
"pattern": "^$|^[0-9a-fA-F]{130}|[0-9a-fA-F]{66},\\s*(?:(?:[a-zA-Z0-9\\-\\.]+)|(?:\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})):\\d{1,5}$"
"pattern": "^$|^(?:[0-9a-fA-F]{130}|[0-9a-fA-F]{66}),(?:(?:[a-zA-Z0-9\\-\\.]+)|(?:\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})),\\d{1,5}$"
}
}
}
Expand Down
33 changes: 0 additions & 33 deletions sdk/node/Libplanet.Node/BoundPeerUtility.cs

This file was deleted.

15 changes: 13 additions & 2 deletions sdk/node/Libplanet.Node/DataAnnotations/BoundPeerAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,21 @@ namespace Libplanet.Node.DataAnnotations;
public sealed class BoundPeerAttribute : RegularExpressionAttribute
{
public BoundPeerAttribute()
: base(
@$"^$|^{PublicKeyAttribute.OriginPattern},\s*{DnsEndPointAttribute.OriginPattern}$")
: base(GetPattern())
{
}

private static string GetPattern()
{
var items = new string[]
{
PublicKeyAttribute.OriginPattern,
DnsEndPointAttribute.HostPattern,
DnsEndPointAttribute.PortPattern,
};
var pattern = string.Join(",", items);
return @$"^$|^{pattern}$";
}
}

[AttributeUsage(AttributeTargets.Property)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ namespace Libplanet.Node.DataAnnotations;
[AttributeUsage(AttributeTargets.Property)]
public sealed class DnsEndPointAttribute : RegularExpressionAttribute
{
public const string OriginPattern
= @"(?:(?:[a-zA-Z0-9\-\.]+)|(?:\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})):\d{1,5}";
public const string HostPattern
= @"(?:(?:[a-zA-Z0-9\-\.]+)|(?:\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}))";

public const string PortPattern = @"\d{1,5}";
public static readonly string OriginPattern
= $"{HostPattern}:{PortPattern}";

public DnsEndPointAttribute()
: base($"^{OriginPattern}$")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Libplanet.Node.DataAnnotations;
[AttributeUsage(AttributeTargets.Property)]
public sealed class PublicKeyAttribute : RegularExpressionAttribute
{
public const string OriginPattern = "[0-9a-fA-F]{130}|[0-9a-fA-F]{66}";
public const string OriginPattern = "(?:[0-9a-fA-F]{130}|[0-9a-fA-F]{66})";

public PublicKeyAttribute()
: base($"^{OriginPattern}$")
Expand Down
9 changes: 4 additions & 5 deletions sdk/node/Libplanet.Node/Services/SwarmService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public async Task StartAsync(CancellationToken cancellationToken)
PrivateKey = ByteUtil.Hex(seedPrivateKey.ByteArray),
EndPoint = EndPointUtility.ToString(EndPointUtility.Next()),
});
_options.BlocksyncSeedPeer = BoundPeerUtility.ToString(_blocksyncSeed.BoundPeer);
_options.BlocksyncSeedPeer = _blocksyncSeed.BoundPeer.PeerString;
await _blocksyncSeed.StartAsync(cancellationToken);
}

Expand All @@ -63,8 +63,7 @@ public async Task StartAsync(CancellationToken cancellationToken)
PrivateKey = ByteUtil.Hex(seedPrivateKey.ByteArray),
EndPoint = EndPointUtility.ToString(EndPointUtility.Next()),
});
validatorOptions.ConsensusSeedPeer
= BoundPeerUtility.ToString(_consensusSeed.BoundPeer);
validatorOptions.ConsensusSeedPeer = _consensusSeed.BoundPeer.PeerString;
await _consensusSeed.StartAsync(cancellationToken);
}

Expand All @@ -74,7 +73,7 @@ public async Task StartAsync(CancellationToken cancellationToken)
var swarmTransport = await CreateTransport(
privateKey: privateKey,
endPoint: swarmEndPoint);
var blocksyncSeedPeer = BoundPeerUtility.Parse(nodeOptions.BlocksyncSeedPeer);
var blocksyncSeedPeer = BoundPeer.ParsePeer(nodeOptions.BlocksyncSeedPeer);
var swarmOptions = new Net.Options.SwarmOptions
{
StaticPeers = [blocksyncSeedPeer],
Expand Down Expand Up @@ -185,7 +184,7 @@ private static async Task<NetMQTransport> CreateTransport(
private static ConsensusReactorOption CreateConsensusReactorOption(
PrivateKey privateKey, ValidatorOptions options)
{
var consensusSeedPeer = BoundPeerUtility.Parse(options.ConsensusSeedPeer);
var consensusSeedPeer = BoundPeer.ParsePeer(options.ConsensusSeedPeer);
var consensusEndPoint = (DnsEndPoint)EndPointUtility.Parse(options.EndPoint);
return new ConsensusReactorOption
{
Expand Down

0 comments on commit 8a8b57a

Please sign in to comment.