diff --git a/src/main/java/org/ethereum/beacon/discovery/schema/IdentitySchemaInterpreter.java b/src/main/java/org/ethereum/beacon/discovery/schema/IdentitySchemaInterpreter.java index b084683aa..3e5037cf1 100644 --- a/src/main/java/org/ethereum/beacon/discovery/schema/IdentitySchemaInterpreter.java +++ b/src/main/java/org/ethereum/beacon/discovery/schema/IdentitySchemaInterpreter.java @@ -52,4 +52,6 @@ NodeRecord createWithNewAddress( NodeRecord createWithUpdatedCustomField( NodeRecord nodeRecord, String newAddress, Bytes value, SecretKey secretKey); + + Bytes calculateNodeId(Bytes publicKey); } diff --git a/src/main/java/org/ethereum/beacon/discovery/schema/IdentitySchemaV4Interpreter.java b/src/main/java/org/ethereum/beacon/discovery/schema/IdentitySchemaV4Interpreter.java index aa9831d8c..00b0f4ffb 100644 --- a/src/main/java/org/ethereum/beacon/discovery/schema/IdentitySchemaV4Interpreter.java +++ b/src/main/java/org/ethereum/beacon/discovery/schema/IdentitySchemaV4Interpreter.java @@ -37,7 +37,7 @@ public class IdentitySchemaV4Interpreter implements IdentitySchemaInterpreter { private final LoadingCache nodeIdCache = CacheBuilder.newBuilder() .maximumSize(4000) - .build(CacheLoader.from(IdentitySchemaV4Interpreter::calculateNodeId)); + .build(CacheLoader.from(IdentitySchemaV4Interpreter::calculateNodeIdImpl)); private static final ImmutableSet ADDRESS_IP_V4_FIELD_NAMES = ImmutableSet.of(EnrField.IP_V4, EnrField.UDP); @@ -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( @@ -152,6 +152,11 @@ public NodeRecord createWithUpdatedCustomField( return newRecord; } + @Override + public Bytes calculateNodeId(final Bytes publicKey) { + return nodeIdCache.getUnchecked(publicKey); + } + private static Optional addressFromFields( final NodeRecord nodeRecord, final String ipField, final String portField) { if (!nodeRecord.containsKey(ipField) || !nodeRecord.containsKey(portField)) { diff --git a/src/test/java/org/ethereum/beacon/discovery/SimpleIdentitySchemaInterpreter.java b/src/test/java/org/ethereum/beacon/discovery/SimpleIdentitySchemaInterpreter.java index 545d5fade..1d8f337ec 100644 --- a/src/test/java/org/ethereum/beacon/discovery/SimpleIdentitySchemaInterpreter.java +++ b/src/test/java/org/ethereum/beacon/discovery/SimpleIdentitySchemaInterpreter.java @@ -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(); + } } diff --git a/src/test/java/org/ethereum/beacon/discovery/schema/IdentitySchemaV4InterpreterTest.java b/src/test/java/org/ethereum/beacon/discovery/schema/IdentitySchemaV4InterpreterTest.java index 6a3343b49..53a7d7dc1 100644 --- a/src/test/java/org/ethereum/beacon/discovery/schema/IdentitySchemaV4InterpreterTest.java +++ b/src/test/java/org/ethereum/beacon/discovery/schema/IdentitySchemaV4InterpreterTest.java @@ -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; @@ -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 getTcpAddressForNodeRecordWithFields( final EnrField... fields) { return interpreter.getTcpAddress(createNodeRecord(fields));