Polish message creation

This commit is contained in:
Roman Mandeleil 2015-02-27 09:23:26 +02:00
parent 8af4556a4f
commit 841c4adb62
3 changed files with 26 additions and 13 deletions

View File

@ -1,18 +1,23 @@
package org.ethereum.net.rlpx;
import org.ethereum.crypto.ECKey;
import org.ethereum.util.ByteUtil;
import org.ethereum.util.RLP;
import static org.ethereum.util.ByteUtil.longToBytes;
import static org.ethereum.util.ByteUtil.stripLeadingZeroes;
public class FindNodeMessage extends Message {
public static Message create(byte[] target, ECKey privKey) {
long expiration = System.currentTimeMillis();
long expiration = 3 + System.currentTimeMillis() / 1000;
/* RLP Encode data */
byte[] rlpToken = RLP.encodeElement(target);
byte[] rlpExp = RLP.encodeElement(ByteUtil.longToBytes(expiration));
byte[] tmpExp = longToBytes(expiration);
byte[] rlpExp = RLP.encodeElement(stripLeadingZeroes(tmpExp));
byte[] type = new byte[]{3};
byte[] data = RLP.encodeList(rlpToken, rlpExp);

View File

@ -12,13 +12,15 @@ public class NeighborsMessage extends Message {
long expiration = System.currentTimeMillis();
byte[][] nodeRLPs = new byte[nodes.size()][];
byte[][] nodeRLPs = null;
/* RLP Encode data */
int i = 0;
for (Node node : nodes){
nodeRLPs[i] = node.getRLP();
++i;
if (nodes != null) {
nodeRLPs = new byte[nodes.size()][];
int i = 0;
for (Node node : nodes) {
nodeRLPs[i] = node.getRLP();
++i;
}
}
byte[] rlpListNodes = RLP.encodeList(nodeRLPs);

View File

@ -1,19 +1,25 @@
package org.ethereum.net.rlpx;
import org.ethereum.crypto.ECKey;
import org.ethereum.util.ByteUtil;
import org.ethereum.util.RLP;
import static org.ethereum.util.ByteUtil.longToBytes;
import static org.ethereum.util.ByteUtil.stripLeadingZeroes;
public class PingMessage extends Message {
public static Message create(String ip, int port, ECKey privKey){
long expiration = System.currentTimeMillis();
long expiration = 3 + System.currentTimeMillis() / 1000;
/* RLP Encode data */
byte[] rlpIp = RLP.encodeElement(ip.getBytes());
byte[] rlpPort = RLP.encodeElement(ByteUtil.longToBytes(port));
byte[] rlpExp = RLP.encodeElement(ByteUtil.longToBytes(expiration));
byte[] tmpPort = longToBytes(port);
byte[] rlpPort = RLP.encodeElement(stripLeadingZeroes(tmpPort));
byte[] tmpExp = longToBytes(expiration);
byte[] rlpExp = RLP.encodeElement(stripLeadingZeroes(tmpExp));
byte[] type = new byte[]{1};
byte[] data = RLP.encodeList(rlpIp, rlpPort, rlpExp);