Skip to content

Commit

Permalink
feat: connect properly to beefy votes stream
Browse files Browse the repository at this point in the history
  • Loading branch information
Grigorov-Georgi committed Feb 24, 2025
1 parent 21f3535 commit b3fbbe0
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 27 deletions.
9 changes: 6 additions & 3 deletions src/main/java/com/limechain/network/NetworkService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/com/limechain/network/ProtocolUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
31 changes: 13 additions & 18 deletions src/main/java/com/limechain/network/protocol/beefy/BeefyEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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) {
Expand Down Expand Up @@ -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
}
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit b3fbbe0

Please sign in to comment.