Coverage Summary for Class: PingPeerMessage (co.rsk.net.discovery.message)

Class Class, % Method, % Line, %
PingPeerMessage 0% (0/1) 0% (0/9) 0% (0/43)


1 /* 2  * This file is part of RskJ 3  * Copyright (C) 2017 RSK Labs Ltd. 4  * 5  * This program is free software: you can redistribute it and/or modify 6  * it under the terms of the GNU Lesser General Public License as published by 7  * the Free Software Foundation, either version 3 of the License, or 8  * (at your option) any later version. 9  * 10  * This program is distributed in the hope that it will be useful, 11  * but WITHOUT ANY WARRANTY; without even the implied warranty of 12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13  * GNU Lesser General Public License for more details. 14  * 15  * You should have received a copy of the GNU Lesser General Public License 16  * along with this program. If not, see <http://www.gnu.org/licenses/>. 17  */ 18  19 package co.rsk.net.discovery.message; 20  21 import co.rsk.net.discovery.PeerDiscoveryException; 22 import org.apache.commons.lang3.builder.ToStringBuilder; 23 import org.ethereum.crypto.ECKey; 24 import org.ethereum.util.ByteUtil; 25 import org.ethereum.util.RLP; 26 import org.ethereum.util.RLPItem; 27 import org.ethereum.util.RLPList; 28  29 import java.nio.charset.Charset; 30 import java.nio.charset.StandardCharsets; 31 import java.util.OptionalInt; 32  33 import static org.ethereum.util.ByteUtil.intToBytes; 34 import static org.ethereum.util.ByteUtil.longToBytes; 35 import static org.ethereum.util.ByteUtil.stripLeadingZeroes; 36  37 /** 38  * Created by mario on 16/02/17. 39  */ 40 public class PingPeerMessage extends PeerDiscoveryMessage { 41  public static final String MORE_DATA = "PingPeerMessage needs more data"; 42  public static final String MORE_FROM_DATA = "PingPeerMessage needs more data in the from"; 43  private String host; 44  private int port; 45  private String messageId; 46  47  public PingPeerMessage(byte[] wire, byte[] mdc, byte[] signature, byte[] type, byte[] data) { 48  super(wire, mdc, signature, type, data); 49  this.parse(data); 50  } 51  52  private PingPeerMessage() {} 53  54  public static PingPeerMessage create(String host, int port, String check, ECKey privKey, Integer networkId) { 55  /* RLP Encode data */ 56  byte[] rlpIp = RLP.encodeElement(host.getBytes(StandardCharsets.UTF_8)); 57  58  byte[] tmpPort = longToBytes(port); 59  byte[] rlpPort = RLP.encodeElement(stripLeadingZeroes(tmpPort)); 60  61  byte[] rlpIpTo = RLP.encodeElement(host.getBytes(StandardCharsets.UTF_8)); 62  byte[] tmpPortTo = longToBytes(port); 63  byte[] rlpPortTo = RLP.encodeElement(stripLeadingZeroes(tmpPortTo)); 64  byte[] type = new byte[]{(byte) DiscoveryMessageType.PING.getTypeValue()}; 65  byte[] rlpFromList = RLP.encodeList(rlpIp, rlpPort, rlpPort); 66  byte[] rlpToList = RLP.encodeList(rlpIpTo, rlpPortTo, rlpPortTo); 67  byte[] rlpCheck = RLP.encodeElement(check.getBytes(StandardCharsets.UTF_8)); 68  byte[] data; 69  byte[] tmpNetworkId = intToBytes(networkId); 70  byte[] rlpNetworkID = RLP.encodeElement(stripLeadingZeroes(tmpNetworkId)); 71  data = RLP.encodeList(rlpFromList, rlpToList, rlpCheck, rlpNetworkID); 72  73  PingPeerMessage message = new PingPeerMessage(); 74  message.encode(type, data, privKey); 75  76  message.setNetworkId(OptionalInt.of(networkId)); 77  message.messageId = check; 78  message.host = host; 79  message.port = port; 80  81  return message; 82  } 83  84  @Override 85  public final void parse(byte[] data) { 86  RLPList dataList = (RLPList) RLP.decode2OneItem(data, 0); 87  88  if (dataList.size() < 3) { 89  throw new PeerDiscoveryException(MORE_DATA); 90  } 91  92  RLPList fromList = (RLPList) dataList.get(1); 93  RLPItem chk = (RLPItem) dataList.get(2); 94  95  if (fromList.size() != 3) { 96  throw new PeerDiscoveryException(MORE_FROM_DATA); 97  } 98  99  byte[] ipB = fromList.get(0).getRLPData(); 100  101  this.host = new String(ipB, Charset.forName("UTF-8")); 102  this.port = ByteUtil.byteArrayToInt(fromList.get(1).getRLPData()); 103  this.messageId = new String(chk.getRLPData(), Charset.forName("UTF-8")); 104  105  //Message from nodes that do not have this 106  this.setNetworkIdWithRLP(dataList.size()>3?dataList.get(3):null); 107  } 108  109  public String getMessageId() { 110  return this.messageId; 111  } 112  113  public String getHost() { 114  return this.host; 115  } 116  117  public int getPort() { 118  return this.port; 119  } 120  121  @Override 122  public DiscoveryMessageType getMessageType() { 123  return DiscoveryMessageType.PING; 124  } 125  126  @Override 127  public String toString() { 128  return new ToStringBuilder(this) 129  .append(this.host) 130  .append(this.port) 131  .append(this.messageId).toString(); 132  } 133  134 }