This repository has been archived by the owner on Aug 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 370
adds milestone request to message protocol #1829
Open
acha-bill
wants to merge
1
commit into
iotaledger:dev
Choose a base branch
from
acha-bill:add-milestone-request
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/main/java/com/iota/iri/network/protocol/MilestoneRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.iota.iri.network.protocol; | ||
|
||
import java.nio.ByteBuffer; | ||
|
||
/** | ||
* Defines the information contained in a milestone request | ||
*/ | ||
public class MilestoneRequest { | ||
|
||
private int milestoneIndex; | ||
|
||
/** | ||
* Parses the given message into a {@link MilestoneRequest} object. | ||
* | ||
* @param msg the buffer containing the milestone request info | ||
* @return the {@link MilestoneRequest} object | ||
*/ | ||
public static MilestoneRequest fromByteBuffer(ByteBuffer msg) { | ||
MilestoneRequest milestoneRequest = new MilestoneRequest(); | ||
milestoneRequest.setMilestoneIndex(msg.getInt()); | ||
return milestoneRequest; | ||
} | ||
|
||
/** | ||
* Gets the milestone index of the milestone request | ||
* | ||
* @return The milestone index | ||
*/ | ||
public int getMilestoneIndex() { | ||
return milestoneIndex; | ||
} | ||
|
||
/** | ||
* Sets the milestone index of the milestone request | ||
* | ||
* @param milestoneIndex The milestone index | ||
*/ | ||
public void setMilestoneIndex(int milestoneIndex) { | ||
this.milestoneIndex = milestoneIndex; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,16 +6,13 @@ | |
import com.iota.iri.network.neighbor.Neighbor; | ||
import com.iota.iri.network.neighbor.NeighborState; | ||
import com.iota.iri.network.pipeline.TransactionProcessingPipeline; | ||
import com.iota.iri.network.protocol.Handshake; | ||
import com.iota.iri.network.protocol.*; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
import java.io.IOException; | ||
import java.nio.ByteBuffer; | ||
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; | ||
import org.mockito.Mock; | ||
|
@@ -267,4 +264,60 @@ public int read(ByteBuffer dst) { | |
fail("didnt expect an exception: " + e.getMessage()); | ||
} | ||
} | ||
|
||
@Test | ||
public void writeMilestoneRequest() { | ||
MilestoneRequest milestoneRequest = new MilestoneRequest(); | ||
milestoneRequest.setMilestoneIndex(1); | ||
ByteBuffer milestoneRequestPacket = Protocol.createMilestoneRequestPacket(milestoneRequest); | ||
|
||
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(milestoneRequestPacket); | ||
|
||
try { | ||
assertEquals("should have written the entire milestone request packet", milestoneRequestPacket.capacity(), neighbor.write()); | ||
} catch (IOException e) { | ||
fail("Didn't expect an exception: " + e.getMessage()); | ||
} | ||
} | ||
|
||
@Test | ||
public void readMilestoneRequest() { | ||
MilestoneRequest milestoneRequest = new MilestoneRequest(); | ||
milestoneRequest.setMilestoneIndex(1); | ||
ByteBuffer milestoneRequestPacket = Protocol.createMilestoneRequestPacket(milestoneRequest); | ||
|
||
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(milestoneRequestPacket.get()); | ||
} | ||
return 0; | ||
} | ||
}, localAddr, serverSocketPort, pipeline); | ||
|
||
// set the neighbor as ready for other messages | ||
neighbor.setState(NeighborState.READY_FOR_MESSAGES); | ||
|
||
try { | ||
MilestoneRequest readMilestoneRequest = neighbor.milestoneRequest(); | ||
assertEquals("milestone index of sent and read ms request should be equal", readMilestoneRequest.getMilestoneIndex(), milestoneRequest.getMilestoneIndex()); | ||
} catch (IOException e) { | ||
fail("Didn't expect an exception: " + e.getMessage()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Issue found: Public method and constructor comments are required