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

Commit

Permalink
Merge branch 'dev' into revert-cache
Browse files Browse the repository at this point in the history
  • Loading branch information
Gal Rogozinski committed Apr 22, 2020
2 parents 646b78e + 22d806e commit 7ee0467
Show file tree
Hide file tree
Showing 8 changed files with 1 addition and 195 deletions.
8 changes: 0 additions & 8 deletions src/main/java/com/iota/iri/network/neighbor/Neighbor.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.iota.iri.network.neighbor;

import com.iota.iri.network.protocol.Handshake;
import com.iota.iri.network.protocol.Heartbeat;

import java.io.IOException;
import java.nio.ByteBuffer;
Expand Down Expand Up @@ -40,13 +39,6 @@ public interface Neighbor {
*/
Handshake handshake() throws IOException;

/**
* Instructs the {@link Neighbor} to read from its source channel a {@link Heartbeat} packet.
* @return The {@link Heartbeat} object
* @throws IOException thrown when reading from the source channels fails
*/
Heartbeat heartbeat() throws IOException;

/**
* Instructs the {@link Neighbor} to send the given {@link ByteBuffer} to its destination channel.
*
Expand Down
10 changes: 0 additions & 10 deletions src/main/java/com/iota/iri/network/neighbor/impl/NeighborImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ private enum ReadState {
private NeighborMetrics metrics = new NeighborMetricsImpl();
private MessageReader msgReader;
private Handshake handshake = new Handshake();
private Heartbeat heartbeat = new Heartbeat();

/**
* Creates a new {@link NeighborImpl} using the given channel.
Expand Down Expand Up @@ -86,12 +85,6 @@ public Handshake handshake() throws IOException {
return handshake;
}

@Override
public Heartbeat heartbeat() throws IOException {
read();
return heartbeat;
}

@Override
public int read() throws IOException {
int bytesRead = msgReader.readMessage(channel);
Expand Down Expand Up @@ -163,9 +156,6 @@ private void handleMessage(ByteBuffer msg) {
case TRANSACTION_GOSSIP:
txPipeline.process(this, msg);
break;
case HEARTBEAT:
heartbeat = Heartbeat.fromByteBuffer(msg);
break;
default:
// do nothing
}
Expand Down
61 changes: 0 additions & 61 deletions src/main/java/com/iota/iri/network/protocol/Heartbeat.java

This file was deleted.

21 changes: 0 additions & 21 deletions src/main/java/com/iota/iri/network/protocol/Protocol.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,6 @@ public class Protocol {
*/
public final static int GOSSIP_REQUESTED_TX_HASH_BYTES_LENGTH = 49;

/**
* The amount of bytes to store first and last solid milestone index
*/
public final static byte PROTOCOL_HEARTBEAT_BYTES_LENGTH = 8;

/**
* Parses the given buffer into a {@link ProtocolHeader}.
*
Expand Down Expand Up @@ -107,22 +102,6 @@ public static ByteBuffer createTransactionGossipPacket(TransactionViewModel tvm,
return buf;
}

/**
* Creates a new heartbeat packet.
*
* @param heartbeat The heartbeat to add to the packet
* @return a {@link ByteBuffer} containing the transaction gossip packet.
*/
public static ByteBuffer createHeartbeatPacket(Heartbeat heartbeat) {
final short payloadLengthBytes = Protocol.PROTOCOL_HEARTBEAT_BYTES_LENGTH;
ByteBuffer buf = ByteBuffer.allocate(ProtocolMessage.HEADER.getMaxLength() + payloadLengthBytes);
addProtocolHeader(buf, ProtocolMessage.HEARTBEAT, payloadLengthBytes);
buf.putInt(heartbeat.getFirstSolidMilestoneIndex());
buf.putInt(heartbeat.getLastSolidMilestoneIndex());
buf.flip();
return buf;
}

/**
* Adds the protocol header to the given {@link ByteBuffer}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ public enum ProtocolMessage {
* take up their full 1604 bytes as the signature message fragment of the tx is truncated.
*/
TRANSACTION_GOSSIP((byte) 2, (short) (Protocol.GOSSIP_REQUESTED_TX_HASH_BYTES_LENGTH + TransactionTruncator.NON_SIG_TX_PART_BYTES_LENGTH
+ TransactionTruncator.SIG_DATA_MAX_BYTES_LENGTH), true),

HEARTBEAT((byte) 3, Protocol.PROTOCOL_HEARTBEAT_BYTES_LENGTH, true);
+ TransactionTruncator.SIG_DATA_MAX_BYTES_LENGTH), true);

private static final ProtocolMessage[] lookup = new ProtocolMessage[256];

Expand All @@ -46,7 +44,6 @@ public enum ProtocolMessage {
lookup[0] = HEADER;
lookup[1] = HANDSHAKE;
lookup[2] = TRANSACTION_GOSSIP;
lookup[3] = HEARTBEAT;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public static MessageReader create(ProtocolMessage protoMsg) throws UnknownMessa
case HEADER:
case HANDSHAKE:
case TRANSACTION_GOSSIP:
case HEARTBEAT:
return create(protoMsg, protoMsg.getMaxLength());
// there might be message types in the future which need a separate message reader implementation
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;

import com.iota.iri.network.protocol.Heartbeat;
import com.iota.iri.network.protocol.Protocol;
import com.iota.iri.network.protocol.ProtocolMessage;
import org.junit.Rule;
import org.junit.Test;
Expand Down Expand Up @@ -209,62 +207,4 @@ public void markingTheNeighborForDisconnectWillNeverMakeItReadyForMessagesAgain(
neighbor.setState(NeighborState.READY_FOR_MESSAGES);
assertEquals("should be marked for disconnect", NeighborState.MARKED_FOR_DISCONNECT, neighbor.getState());
}

@Test
public void writeHeartbeat() {
Heartbeat heartbeat = new Heartbeat();
heartbeat.setFirstSolidMilestoneIndex(1);
heartbeat.setLastSolidMilestoneIndex(2);
ByteBuffer heartbeatPacket = Protocol.createHeartbeatPacket(heartbeat);

Neighbor neighbor = new NeighborImpl<>(selector, new FakeChannel() {

@Override
public int write(ByteBuffer buf) {
int bytesWritten = 0;
while (buf.hasRemaining()) {
buf.get();
bytesWritten++;
}
return bytesWritten;
}
}, localAddr, serverSocketPort, pipeline);

neighbor.send(heartbeatPacket);

try {
assertEquals("should have written the entire heartbeat packet", heartbeatPacket.capacity(), neighbor.write());
} catch (IOException e) {
fail("didn't expect an exception");
}
}

@Test
public void readHeartbeat() {
Heartbeat heartbeat = new Heartbeat();
heartbeat.setFirstSolidMilestoneIndex(1);
heartbeat.setLastSolidMilestoneIndex(2);
ByteBuffer heartbeatPacket = Protocol.createHeartbeatPacket(heartbeat);

Neighbor neighbor = new NeighborImpl<>(selector, new FakeChannel() {
// fake having a heartbeat packet in the socket
@Override
public int read(ByteBuffer dst) {
while (dst.hasRemaining()) {
dst.put(heartbeatPacket.get());
}
return 0;
}
}, localAddr, serverSocketPort, pipeline);

// set the neighbor as ready for other messages
neighbor.setState(NeighborState.READY_FOR_MESSAGES);

try {
Heartbeat readHeartbeat = neighbor.heartbeat();
assertEquals("fsmi of sent and read heartbeat should be equal", readHeartbeat.getFirstSolidMilestoneIndex(), heartbeat.getFirstSolidMilestoneIndex());
} catch (IOException e) {
fail("didnt expect an exception: " + e.getMessage());
}
}
}
30 changes: 0 additions & 30 deletions src/test/java/com/iota/iri/network/protocol/ProtocolTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,34 +98,4 @@ public void createTransactionGossipPacket() {
assertEquals("should be of type tx gossip message", ProtocolMessage.TRANSACTION_GOSSIP.getTypeID(), buf.get());
assertEquals("should have correct message length", expectedMessageSize, buf.getShort());
}

@Test
public void createHeartbeatPacket(){
Heartbeat heartbeat = new Heartbeat();
heartbeat.setFirstSolidMilestoneIndex(1);
heartbeat.setLastSolidMilestoneIndex(2);

ByteBuffer buf = Protocol.createHeartbeatPacket(heartbeat);
final int expectedMessageSize = Protocol.PROTOCOL_HEARTBEAT_BYTES_LENGTH;
assertEquals("buffer should have the right capacity",
Protocol.PROTOCOL_HEADER_BYTES_LENGTH + expectedMessageSize, buf.capacity());
assertEquals("should be of type heartbeat message", ProtocolMessage.HEARTBEAT.getTypeID(), buf.get());
assertEquals("should have correct message length", expectedMessageSize, buf.getShort());
}

@Test
public void parseHeartbeat() {
Heartbeat heartbeat;
try {
ByteBuffer buf = ByteBuffer.allocate(Protocol.PROTOCOL_HEARTBEAT_BYTES_LENGTH);
buf.putInt(1);
buf.putInt(2);
buf.flip();
heartbeat = Heartbeat.fromByteBuffer(buf);
assertEquals("should have correct first solid milestone index", heartbeat.getFirstSolidMilestoneIndex(), 1);
assertEquals("Should have correct last solid milestone index", heartbeat.getLastSolidMilestoneIndex(), 2);
} catch (Exception e) {
fail("didn't expect any exceptions");
}
}
}

0 comments on commit 7ee0467

Please sign in to comment.