Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add client id to admin_peers RPC API #7717

Merged
merged 4 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 27 additions & 54 deletions src/Nethermind/Nethermind.Core/Crypto/PublicKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class PublicKey : IEquatable<PublicKey>
public const int PrefixedLengthInBytes = 65;
public const int LengthInBytes = 64;
private Address? _address;
private Hash256? _hash256;

private byte[]? _prefixedBytes;
private readonly int _hashCode;
Expand All @@ -29,14 +30,12 @@ public PublicKey(ReadOnlySpan<byte> 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();
Expand All @@ -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
Expand All @@ -72,75 +84,36 @@ 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<byte> 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<byte> publicKeyBytes)
{
Span<byte> hash = ValueKeccak.Compute(publicKeyBytes).BytesAsSpan;
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<byte>(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()
{
string value = Bytes.ToHexString(false);
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<PublicKeyAsKey>
Expand Down
23 changes: 14 additions & 9 deletions src/Nethermind/Nethermind.JsonRpc/Modules/Admin/AdminRpcModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
7 changes: 5 additions & 2 deletions src/Nethermind/Nethermind.JsonRpc/Modules/Admin/PeerInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand All @@ -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 = 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();
Expand Down