Skip to content

Commit

Permalink
Expose IdentitySchemaInterpreter.calculateNodeId (#182)
Browse files Browse the repository at this point in the history
  • Loading branch information
zilm13 authored Sep 27, 2024
1 parent 7f6aac8 commit 1e2170e
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,6 @@ NodeRecord createWithNewAddress(

NodeRecord createWithUpdatedCustomField(
NodeRecord nodeRecord, String newAddress, Bytes value, SecretKey secretKey);

Bytes calculateNodeId(Bytes publicKey);
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class IdentitySchemaV4Interpreter implements IdentitySchemaInterpreter {
private final LoadingCache<Bytes, Bytes> nodeIdCache =
CacheBuilder.newBuilder()
.maximumSize(4000)
.build(CacheLoader.from(IdentitySchemaV4Interpreter::calculateNodeId));
.build(CacheLoader.from(IdentitySchemaV4Interpreter::calculateNodeIdImpl));

private static final ImmutableSet<String> ADDRESS_IP_V4_FIELD_NAMES =
ImmutableSet.of(EnrField.IP_V4, EnrField.UDP);
Expand Down Expand Up @@ -74,7 +74,7 @@ public Bytes getNodeId(final NodeRecord nodeRecord) {
return nodeIdCache.getUnchecked(pkey);
}

private static Bytes calculateNodeId(final Bytes publicKey) {
private static Bytes calculateNodeIdImpl(final Bytes publicKey) {
ECPoint pudDestPoint = Functions.publicKeyToPoint(publicKey);
Bytes xPart =
Bytes.wrap(
Expand Down Expand Up @@ -152,6 +152,11 @@ public NodeRecord createWithUpdatedCustomField(
return newRecord;
}

@Override
public Bytes calculateNodeId(final Bytes publicKey) {
return nodeIdCache.getUnchecked(publicKey);
}

private static Optional<InetSocketAddress> addressFromFields(
final NodeRecord nodeRecord, final String ipField, final String portField) {
if (!nodeRecord.containsKey(ipField) || !nodeRecord.containsKey(portField)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,11 @@ public NodeRecord createWithUpdatedCustomField(
sign(newRecord, secretKey);
return newRecord;
}

@Override
public Bytes calculateNodeId(final Bytes publicKey) {
final NodeRecord nodeRecord =
createNodeRecord(publicKey, new InetSocketAddress("127.0.0.1", 2));
return nodeRecord.getNodeId();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.net.InetAddress;
import java.net.InetSocketAddress;
Expand Down Expand Up @@ -284,6 +285,18 @@ public void enrDeserializationWithWrongKeyOrderShouldFail() {
.isInstanceOf(DecodeException.class);
}

@Test
public void calculateNodeIdShouldMatchExpected() {
final Bytes expectedNodeId =
NodeRecordFactory.DEFAULT
.createFromValues(
UInt64.ZERO,
new EnrField(EnrField.PKEY_SECP256K1, PUB_KEY),
new EnrField(EnrField.ID, IdentitySchema.V4))
.getNodeId();
assertEquals(expectedNodeId, interpreter.calculateNodeId(PUB_KEY));
}

private Optional<InetSocketAddress> getTcpAddressForNodeRecordWithFields(
final EnrField... fields) {
return interpreter.getTcpAddress(createNodeRecord(fields));
Expand Down

0 comments on commit 1e2170e

Please sign in to comment.