From c00cc746415eb76cceb5f96416e54cbec22c326e Mon Sep 17 00:00:00 2001 From: anhnhgutech Date: Mon, 4 Nov 2024 13:39:33 +0700 Subject: [PATCH 1/3] fix: add client id in admin_peers --- .../Nethermind.JsonRpc/Modules/Admin/PeerInfo.cs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/Nethermind/Nethermind.JsonRpc/Modules/Admin/PeerInfo.cs b/src/Nethermind/Nethermind.JsonRpc/Modules/Admin/PeerInfo.cs index b567abd394b..c4c1cddf753 100644 --- a/src/Nethermind/Nethermind.JsonRpc/Modules/Admin/PeerInfo.cs +++ b/src/Nethermind/Nethermind.JsonRpc/Modules/Admin/PeerInfo.cs @@ -6,12 +6,14 @@ using System.Net; using Nethermind.Network; using Nethermind.Stats.Model; +using Nethermind.Core.Crypto; namespace Nethermind.JsonRpc.Modules.Admin { public class PeerInfo { - public string ClientId { get; set; } + public string Name { get; set; } + public string Id { get; } public string Host { get; set; } public int Port { get; set; } public string Address { get; set; } @@ -36,7 +38,8 @@ public PeerInfo(Peer peer, bool includeDetails) $"{nameof(PeerInfo)} cannot be created for a {nameof(Peer)} with an unknown {peer.Node}"); } - ClientId = peer.Node.ClientId; + Name = peer.Node.ClientId; + Id = CalculateClientId(peer.Node.Id); Host = peer.Node.Host is null ? null : IPAddress.Parse(peer.Node.Host).MapToIPv4().ToString(); Port = peer.Node.Port; Address = peer.Node.Address.ToString(); @@ -51,5 +54,11 @@ public PeerInfo(Peer peer, bool includeDetails) LastSignal = (peer.InSession ?? peer.OutSession)?.LastPingUtc.ToString(CultureInfo.InvariantCulture); } } + + private string CalculateClientId(PublicKey nodeKey) + { + byte[] publicKeyBytes = nodeKey.Bytes; + return (publicKeyBytes is null ? Keccak.Zero : Keccak.Compute(publicKeyBytes)).ToString(false); + } } } From 50633128fa92bdb4591b2ebade4cfadf5802ccd8 Mon Sep 17 00:00:00 2001 From: Anh Nguyen Hoang <106639913+anhnhgutech@users.noreply.github.com> Date: Mon, 4 Nov 2024 16:54:13 +0700 Subject: [PATCH 2/3] Update src/Nethermind/Nethermind.JsonRpc/Modules/Admin/PeerInfo.cs Co-authored-by: Lukasz Rozmej --- src/Nethermind/Nethermind.JsonRpc/Modules/Admin/PeerInfo.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Nethermind/Nethermind.JsonRpc/Modules/Admin/PeerInfo.cs b/src/Nethermind/Nethermind.JsonRpc/Modules/Admin/PeerInfo.cs index c4c1cddf753..8bceb48f0e9 100644 --- a/src/Nethermind/Nethermind.JsonRpc/Modules/Admin/PeerInfo.cs +++ b/src/Nethermind/Nethermind.JsonRpc/Modules/Admin/PeerInfo.cs @@ -58,7 +58,7 @@ public PeerInfo(Peer peer, bool includeDetails) private string CalculateClientId(PublicKey nodeKey) { byte[] publicKeyBytes = nodeKey.Bytes; - return (publicKeyBytes is null ? Keccak.Zero : Keccak.Compute(publicKeyBytes)).ToString(false); + return (publicKeyBytes is null ? Keccak.Zero.ValueHash256 : ValueKeccak.Compute(publicKeyBytes)).ToString(false); } } } From 52584c1cf5a14b31fc1517b997d5c1e70b191a9a Mon Sep 17 00:00:00 2001 From: "lukasz.rozmej" Date: Tue, 5 Nov 2024 09:29:25 +0100 Subject: [PATCH 3/3] Refactor, add PublicKey.Hash --- .../Nethermind.Core/Crypto/PublicKey.cs | 81 +++++++------------ .../Modules/Admin/AdminRpcModule.cs | 23 +++--- .../Modules/Admin/PeerInfo.cs | 8 +- 3 files changed, 42 insertions(+), 70 deletions(-) diff --git a/src/Nethermind/Nethermind.Core/Crypto/PublicKey.cs b/src/Nethermind/Nethermind.Core/Crypto/PublicKey.cs index f2d8f1ea2c6..84d5d1b6c64 100644 --- a/src/Nethermind/Nethermind.Core/Crypto/PublicKey.cs +++ b/src/Nethermind/Nethermind.Core/Crypto/PublicKey.cs @@ -16,6 +16,7 @@ public class PublicKey : IEquatable public const int PrefixedLengthInBytes = 65; public const int LengthInBytes = 64; private Address? _address; + private Hash256? _hash256; private byte[]? _prefixedBytes; private readonly int _hashCode; @@ -29,14 +30,12 @@ public PublicKey(ReadOnlySpan bytes) { if (bytes.Length != LengthInBytes && bytes.Length != PrefixedLengthInBytes) { - throw new ArgumentException($"{nameof(PublicKey)} should be {LengthInBytes} bytes long", - nameof(bytes)); + throw new ArgumentException($"{nameof(PublicKey)} should be {LengthInBytes} bytes long", nameof(bytes)); } if (bytes.Length == PrefixedLengthInBytes && bytes[0] != 0x04) { - throw new ArgumentException( - $"Expected prefix of 0x04 for {PrefixedLengthInBytes} bytes long {nameof(PublicKey)}"); + throw new ArgumentException($"Expected prefix of 0x04 for {PrefixedLengthInBytes} bytes long {nameof(PublicKey)}"); } Bytes = bytes.Slice(bytes.Length - 64, 64).ToArray(); @@ -49,13 +48,26 @@ public Address Address { if (_address is null) { - LazyInitializer.EnsureInitialized(ref _address, ComputeAddress); + LazyInitializer.EnsureInitialized(ref _address, () => new Address(Hash.Bytes[12..].ToArray())); } return _address; } } + public Hash256 Hash + { + get + { + if (_hash256 is null) + { + LazyInitializer.EnsureInitialized(ref _hash256, () => Keccak.Compute(Bytes)); + } + + return _hash256; + } + } + public byte[] Bytes { get; } public byte[] PrefixedBytes @@ -72,21 +84,7 @@ public byte[] PrefixedBytes } } - public bool Equals(PublicKey? other) - { - if (other is null) - { - return false; - } - - return Core.Extensions.Bytes.AreEqual(Bytes, other.Bytes); - } - - private Address ComputeAddress() - { - Span hash = ValueKeccak.Compute(Bytes).BytesAsSpan; - return new Address(hash[12..].ToArray()); - } + public bool Equals(PublicKey? other) => other is not null && Core.Extensions.Bytes.AreEqual(Bytes, other.Bytes); public static Address ComputeAddress(ReadOnlySpan publicKeyBytes) { @@ -94,27 +92,15 @@ public static Address ComputeAddress(ReadOnlySpan publicKeyBytes) return new Address(hash[12..].ToArray()); } - public override bool Equals(object? obj) - { - return Equals(obj as PublicKey); - } + public override bool Equals(object? obj) => Equals(obj as PublicKey); - public override int GetHashCode() - { - return _hashCode; - } + public override int GetHashCode() => _hashCode; private static int GetHashCode(byte[] bytes) => new ReadOnlySpan(bytes).FastHash(); - public override string ToString() - { - return Bytes.ToHexString(true); - } + public override string ToString() => Bytes.ToHexString(true); - public string ToString(bool with0X) - { - return Bytes.ToHexString(with0X); - } + public string ToString(bool with0X) => Bytes.ToHexString(with0X); public string ToShortString() { @@ -122,25 +108,12 @@ public string ToShortString() return $"{value[..6]}...{value[^6..]}"; } - public static bool operator ==(PublicKey? a, PublicKey? b) - { - if (a is null) - { - return b is null; - } - - if (b is null) - { - return false; - } + public static bool operator ==(PublicKey? a, PublicKey? b) => + a is null + ? b is null + : b is not null && Core.Extensions.Bytes.AreEqual(a.Bytes, b.Bytes); - return Core.Extensions.Bytes.AreEqual(a.Bytes, b.Bytes); - } - - public static bool operator !=(PublicKey? a, PublicKey? b) - { - return !(a == b); - } + public static bool operator !=(PublicKey? a, PublicKey? b) => !(a == b); } public readonly struct PublicKeyAsKey(PublicKey key) : IEquatable diff --git a/src/Nethermind/Nethermind.JsonRpc/Modules/Admin/AdminRpcModule.cs b/src/Nethermind/Nethermind.JsonRpc/Modules/Admin/AdminRpcModule.cs index 22a2e715903..86f5fb7cedb 100644 --- a/src/Nethermind/Nethermind.JsonRpc/Modules/Admin/AdminRpcModule.cs +++ b/src/Nethermind/Nethermind.JsonRpc/Modules/Admin/AdminRpcModule.cs @@ -48,15 +48,20 @@ public AdminRpcModule( private void BuildNodeInfo() { - _nodeInfo = new NodeInfo(); - _nodeInfo.Name = ProductInfo.ClientId; - _nodeInfo.Enode = _enode.Info; - byte[] publicKeyBytes = _enode.PublicKey?.Bytes; - _nodeInfo.Id = (publicKeyBytes is null ? Keccak.Zero : Keccak.Compute(publicKeyBytes)).ToString(false); - _nodeInfo.Ip = _enode.HostIp?.ToString(); - _nodeInfo.ListenAddress = $"{_enode.HostIp}:{_enode.Port}"; - _nodeInfo.Ports.Discovery = _networkConfig.DiscoveryPort; - _nodeInfo.Ports.Listener = _networkConfig.P2PPort; + _nodeInfo = new NodeInfo + { + Name = ProductInfo.ClientId, + Enode = _enode.Info, + Id = (_enode.PublicKey?.Hash ?? Keccak.Zero).ToString(false), + Ip = _enode.HostIp?.ToString(), + ListenAddress = $"{_enode.HostIp}:{_enode.Port}", + Ports = + { + Discovery = _networkConfig.DiscoveryPort, + Listener = _networkConfig.P2PPort + } + }; + UpdateEthProtocolInfo(); } diff --git a/src/Nethermind/Nethermind.JsonRpc/Modules/Admin/PeerInfo.cs b/src/Nethermind/Nethermind.JsonRpc/Modules/Admin/PeerInfo.cs index 8bceb48f0e9..0deae1a7a67 100644 --- a/src/Nethermind/Nethermind.JsonRpc/Modules/Admin/PeerInfo.cs +++ b/src/Nethermind/Nethermind.JsonRpc/Modules/Admin/PeerInfo.cs @@ -39,7 +39,7 @@ public PeerInfo(Peer peer, bool includeDetails) } Name = peer.Node.ClientId; - Id = CalculateClientId(peer.Node.Id); + Id = peer.Node.Id.Hash.ToString(false); Host = peer.Node.Host is null ? null : IPAddress.Parse(peer.Node.Host).MapToIPv4().ToString(); Port = peer.Node.Port; Address = peer.Node.Address.ToString(); @@ -54,11 +54,5 @@ public PeerInfo(Peer peer, bool includeDetails) LastSignal = (peer.InSession ?? peer.OutSession)?.LastPingUtc.ToString(CultureInfo.InvariantCulture); } } - - private string CalculateClientId(PublicKey nodeKey) - { - byte[] publicKeyBytes = nodeKey.Bytes; - return (publicKeyBytes is null ? Keccak.Zero.ValueHash256 : ValueKeccak.Compute(publicKeyBytes)).ToString(false); - } } }