Skip to content
This repository was archived by the owner on Aug 23, 2020. It is now read-only.

Remove magic number from handshake #1694

Merged
merged 2 commits into from
Jan 6, 2020
Merged
Changes from 1 commit
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
14 changes: 10 additions & 4 deletions src/main/java/com/iota/iri/network/protocol/Handshake.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ public class Handshake {
*/
public final static int BYTE_ENCODED_COO_ADDRESS_BYTES_LENGTH = 49;

/**
* The amount of bytes in the handshake payload is
* <i>source port length + timestamp size + coo address size in bytes + mwm byte + supported version bytes
*/
public static final short PAYLOAD_LENGTH_BYTES = (short) (Character.BYTES + Long.BYTES +
BYTE_ENCODED_COO_ADDRESS_BYTES_LENGTH + Byte.BYTES + Protocol.SUPPORTED_PROTOCOL_VERSIONS.length);

/**
* The state of the handshaking.
*/
Expand All @@ -36,15 +43,14 @@ public enum State {
*/
public static ByteBuffer createHandshakePacket(char ownSourcePort, byte[] ownByteEncodedCooAddress,
byte ownUsedMWM) {
short maxLength = ProtocolMessage.HANDSHAKE.getMaxLength();
final short payloadLengthBytes = (short) (maxLength - (maxLength - 60) + Protocol.SUPPORTED_PROTOCOL_VERSIONS.length);
ByteBuffer buf = ByteBuffer.allocate(ProtocolMessage.HEADER.getMaxLength() + payloadLengthBytes);
Protocol.addProtocolHeader(buf, ProtocolMessage.HANDSHAKE, payloadLengthBytes);
ByteBuffer buf = ByteBuffer.allocate(ProtocolMessage.HEADER.getMaxLength() + PAYLOAD_LENGTH_BYTES);
Protocol.addProtocolHeader(buf, ProtocolMessage.HANDSHAKE, PAYLOAD_LENGTH_BYTES);
buf.putChar(ownSourcePort);
buf.putLong(System.currentTimeMillis());
buf.put(ownByteEncodedCooAddress);
buf.put(ownUsedMWM);
buf.put(Protocol.SUPPORTED_PROTOCOL_VERSIONS);
//update PAYLOAD_LENGTH_BYTES if changed
buf.flip();
return buf;
}
Expand Down