From b3fbbe0f48887d48c000edce9c90068e7159db23 Mon Sep 17 00:00:00 2001 From: Grigorov-Georgi Date: Mon, 24 Feb 2025 17:13:31 +0200 Subject: [PATCH] feat: connect properly to beefy votes stream --- .../com/limechain/network/NetworkService.java | 9 ++++-- .../com/limechain/network/ProtocolUtils.java | 7 +++-- .../protocol/beefy/BeefyController.java | 2 +- .../network/protocol/beefy/BeefyEngine.java | 31 ++++++++----------- .../network/protocol/beefy/BeefyProtocol.java | 1 - .../protocol/grandpa/GrandpaEngine.java | 4 +-- 6 files changed, 27 insertions(+), 27 deletions(-) diff --git a/src/main/java/com/limechain/network/NetworkService.java b/src/main/java/com/limechain/network/NetworkService.java index 5e34410c2..fd0691c10 100644 --- a/src/main/java/com/limechain/network/NetworkService.java +++ b/src/main/java/com/limechain/network/NetworkService.java @@ -121,8 +121,11 @@ private void initializeProtocols(ChainService chainService, GenesisBlockHash gen String pingProtocol = ProtocolUtils.PING_PROTOCOL; String chainId = chainService.getChainSpec().getProtocolId(); boolean legacyProtocol = !cliArgs.noLegacyProtocols(); - String protocolId = legacyProtocol ? chainId : - StringUtils.remove0xPrefix(genesisBlockHash.getGenesisHash().toString()); + String genesisBlockHashWithoutPrefix = StringUtils.remove0xPrefix(genesisBlockHash.getGenesisHash().toString()); + String protocolId = legacyProtocol ? + chainId : + genesisBlockHashWithoutPrefix; + String kadProtocolId = ProtocolUtils.getKadProtocol(chainId); String warpProtocolId = ProtocolUtils.getWarpSyncProtocol(protocolId); String lightProtocolId = ProtocolUtils.getLightMessageProtocol(protocolId); @@ -131,7 +134,7 @@ private void initializeProtocols(ChainService chainService, GenesisBlockHash gen String transactionsProtocolId = ProtocolUtils.getTransactionsProtocol(protocolId); String blockAnnounceProtocolId = ProtocolUtils.getBlockAnnounceProtocol(protocolId); String grandpaProtocolId = ProtocolUtils.getGrandpaProtocol(protocolId, legacyProtocol); - String beefyProtocolId = ProtocolUtils.getBeefyProtocol(protocolId, legacyProtocol); + String beefyProtocolId = ProtocolUtils.getBeefyProtocol(genesisBlockHashWithoutPrefix); kademliaService = new KademliaService(kadProtocolId, hostId, isLocalEnabled, clientMode); lightMessagesService = new LightMessagesService(lightProtocolId); diff --git a/src/main/java/com/limechain/network/ProtocolUtils.java b/src/main/java/com/limechain/network/ProtocolUtils.java index ea8a78dfd..6754946db 100644 --- a/src/main/java/com/limechain/network/ProtocolUtils.java +++ b/src/main/java/com/limechain/network/ProtocolUtils.java @@ -39,7 +39,10 @@ public static String getGrandpaProtocol(String chainId, boolean legacyProtocol) return String.format("/%s/grandpa/1", legacyProtocol ? "paritytech" : chainId); } - public static String getBeefyProtocol(String chainId, boolean legacyProtocol) { - return String.format("/%s/beefy/1", legacyProtocol ? "paritytech" : chainId); + // NOTE: Beefy was likely not part of the original protocols and therefore + // only operates with the genesis hash. As a result, it does not support + // the {chainId}/beefy/2 format. + public static String getBeefyProtocol(String genesisBlockHash) { + return String.format("/%s/beefy/2", genesisBlockHash); } } \ No newline at end of file diff --git a/src/main/java/com/limechain/network/protocol/beefy/BeefyController.java b/src/main/java/com/limechain/network/protocol/beefy/BeefyController.java index 6280ad832..b4d5836eb 100644 --- a/src/main/java/com/limechain/network/protocol/beefy/BeefyController.java +++ b/src/main/java/com/limechain/network/protocol/beefy/BeefyController.java @@ -7,8 +7,8 @@ */ public class BeefyController { - protected BeefyEngine engine = new BeefyEngine(); protected final Stream stream; + protected BeefyEngine engine = new BeefyEngine(); public BeefyController(Stream stream) { this.stream = stream; diff --git a/src/main/java/com/limechain/network/protocol/beefy/BeefyEngine.java b/src/main/java/com/limechain/network/protocol/beefy/BeefyEngine.java index af09c6b99..96053041a 100644 --- a/src/main/java/com/limechain/network/protocol/beefy/BeefyEngine.java +++ b/src/main/java/com/limechain/network/protocol/beefy/BeefyEngine.java @@ -22,11 +22,9 @@ public class BeefyEngine { protected ConnectionManager connectionManager; protected BeefyMessageHandler beefyMessageHandler; - protected HandshakeBuilder handshakeBuilder; public BeefyEngine() { connectionManager = ConnectionManager.getInstance(); - handshakeBuilder = new HandshakeBuilder(); beefyMessageHandler = AppBean.getBean(BeefyMessageHandler.class); } @@ -46,18 +44,6 @@ public void receiveRequest(byte[] message, Stream stream) { } } - private void handleHandshake(byte[] message, PeerId peerId, Stream stream) { - if (connectionManager.isBeefyConnected(peerId)) { - log.log(Level.INFO, "Received existing beefy handshake from " + peerId); - stream.close(); - } else { - connectionManager.addBeefyStream(stream); - connectionManager.getPeerInfo(peerId).setNodeRole(message[0]); - log.log(Level.INFO, "Received beefy handshake from " + peerId); - writeHandshakeToStream(stream, peerId); - } - } - private void handleInitiatorStreamMessage(BeefyMessageType messageType, Stream stream) { PeerId peerId = stream.remotePeerId(); if (messageType != BeefyMessageType.HANDSHAKE) { @@ -91,6 +77,18 @@ private void handleResponderStreamMessage(byte[] message, BeefyMessageType messa } } + private void handleHandshake(byte[] message, PeerId peerId, Stream stream) { + if (connectionManager.isBeefyConnected(peerId)) { + log.log(Level.INFO, "Received existing beefy handshake from " + peerId); + stream.close(); + } else { + connectionManager.addBeefyStream(stream); + connectionManager.getPeerInfo(peerId).setNodeRole(message[0]); + log.log(Level.INFO, "Received beefy handshake from " + peerId); + writeHandshakeToStream(stream, peerId); + } + } + private void handleVoteMessage(byte[] message, PeerId peerId) { //TODO } @@ -105,10 +103,7 @@ private void handleSignedCommitmentMessage(byte[] message, PeerId peerId) { * @param peerId peer to send to */ public void writeHandshakeToStream(Stream stream, PeerId peerId) { - byte[] handshake = new byte[] { - (byte) handshakeBuilder.getHandshake().getNodeRole() - }; - + byte[] handshake = new byte[]{0}; log.log(Level.INFO, "Sending beefy handshake to " + peerId); stream.writeAndFlush(handshake); } diff --git a/src/main/java/com/limechain/network/protocol/beefy/BeefyProtocol.java b/src/main/java/com/limechain/network/protocol/beefy/BeefyProtocol.java index a78f3a221..96d2016f5 100644 --- a/src/main/java/com/limechain/network/protocol/beefy/BeefyProtocol.java +++ b/src/main/java/com/limechain/network/protocol/beefy/BeefyProtocol.java @@ -94,7 +94,6 @@ public void onException(Throwable cause) { connectionManager.closeBeefyStream(stream); if (cause != null) { log.log(Level.WARNING, "Beefy Exception: " + cause.getMessage()); - cause.printStackTrace(); } else { log.log(Level.WARNING, "Beefy Exception with unknown cause"); } diff --git a/src/main/java/com/limechain/network/protocol/grandpa/GrandpaEngine.java b/src/main/java/com/limechain/network/protocol/grandpa/GrandpaEngine.java index 18dad7bde..dad63915c 100644 --- a/src/main/java/com/limechain/network/protocol/grandpa/GrandpaEngine.java +++ b/src/main/java/com/limechain/network/protocol/grandpa/GrandpaEngine.java @@ -76,13 +76,13 @@ public void receiveRequest(byte[] message, Stream stream) { } if (stream.isInitiator()) { - handleInitiatorStreamMessage(message, messageType, stream); + handleInitiatorStreamMessage(messageType, stream); } else { handleResponderStreamMessage(message, messageType, stream); } } - private void handleInitiatorStreamMessage(byte[] message, GrandpaMessageType messageType, Stream stream) { + private void handleInitiatorStreamMessage(GrandpaMessageType messageType, Stream stream) { PeerId peerId = stream.remotePeerId(); if (messageType != GrandpaMessageType.HANDSHAKE) { stream.close();