Coverage Summary for Class: EthMessageCodes (org.ethereum.net.eth.message)

Class Class, % Method, % Line, %
EthMessageCodes 0% (0/1) 0% (0/6) 0% (0/26)


1 /* 2  * This file is part of RskJ 3  * Copyright (C) 2017 RSK Labs Ltd. 4  * (derived from ethereumJ library, Copyright (c) 2016 <ether.camp>) 5  * 6  * This program is free software: you can redistribute it and/or modify 7  * it under the terms of the GNU Lesser General Public License as published by 8  * the Free Software Foundation, either version 3 of the License, or 9  * (at your option) any later version. 10  * 11  * This program is distributed in the hope that it will be useful, 12  * but WITHOUT ANY WARRANTY; without even the implied warranty of 13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14  * GNU Lesser General Public License for more details. 15  * 16  * You should have received a copy of the GNU Lesser General Public License 17  * along with this program. If not, see <http://www.gnu.org/licenses/>. 18  */ 19  20 package org.ethereum.net.eth.message; 21  22 import org.ethereum.net.eth.EthVersion; 23  24 import java.util.HashMap; 25 import java.util.Map; 26  27 import static org.ethereum.net.eth.EthVersion.*; 28  29 /** 30  * A list of commands for the Ethereum network protocol. 31  * <br> 32  * The codes for these commands are the first byte in every packet. 33  * 34  * @see <a href="https://github.com/ethereum/wiki/wiki/Ethereum-Wire-Protocol"> 35  * https://github.com/ethereum/wiki/wiki/Ethereum-Wire-Protocol</a> 36  */ 37 public enum EthMessageCodes { 38  39  /* Ethereum protocol */ 40  41  /** 42  * {@code [0x00, [PROTOCOL_VERSION, NETWORK_ID, TD, BEST_HASH, GENESIS_HASH] } <br> 43  * 44  * Inform a peer of it's current ethereum state. This message should be 45  * send after the initial handshake and prior to any ethereum related messages. 46  */ 47  STATUS(0x00), 48  49  /** 50  * PV 61 and lower <br> 51  * {@code [+0x01, [hash_0: B_32, hash_1: B_32, ...] } <br> 52  * 53  * PV 62 and upper <br> 54  * {@code [+0x01: P, [hash_0: B_32, number_0: P], [hash_1: B_32, number_1: P], ...] } <br> 55  * 56  * Specify one or more new blocks which have appeared on the network. 57  * To be maximally helpful, nodes should inform peers of all blocks that they may not be aware of. 58  * Including hashes that the sending peer could reasonably be considered to know 59  * (due to the fact they were previously informed of because 60  * that node has itself advertised knowledge of the hashes through NewBlockHashes) 61  * is considered Bad Form, and may reduce the reputation of the sending node. 62  * Including hashes that the sending node later refuses to honour with a proceeding 63  * GetBlocks message is considered Bad Form, and may reduce the reputation of the sending node. 64  * 65  */ 66  NEW_BLOCK_HASHES(0x01), 67  68  /** 69  * {@code [+0x02, [nonce, receiving_address, value, ...], ...] } <br> 70  * 71  * Specify (a) transaction(s) that the peer should make sure is included 72  * on its transaction queue. The items in the list (following the first item 0x12) 73  * are transactions in the format described in the main Ethereum specification. 74  */ 75  TRANSACTIONS(0x02), 76  77  /** 78  * {@code [+0x03: P, block: { P , B_32 }, maxHeaders: P, skip: P, reverse: P in { 0 , 1 } ] } <br> 79  * 80  * Replaces GetBlockHashes since PV 62. <br> 81  * 82  * Require peer to return a BlockHeaders message. 83  * Reply must contain a number of block headers, 84  * of rising number when reverse is 0, falling when 1, skip blocks apart, 85  * beginning at block block (denoted by either number or hash) in the canonical chain, 86  * and with at most maxHeaders items. 87  */ 88  GET_BLOCK_HEADERS(0x03), 89  90  /** 91  * {@code [+0x04, blockHeader_0, blockHeader_1, ...] } <br> 92  * 93  * Replaces BLOCK_HASHES since PV 62. <br> 94  * 95  * Reply to GetBlockHeaders. 96  * The items in the list (following the message ID) are 97  * block headers in the format described in the main Ethereum specification, 98  * previously asked for in a GetBlockHeaders message. 99  * This may validly contain no block headers 100  * if no block headers were able to be returned for the GetBlockHeaders query. 101  */ 102  BLOCK_HEADERS(0x04), 103  104  /** 105  * {@code [+0x05, hash_0: B_32, hash_1: B_32, ...] } <br> 106  * 107  * Replaces GetBlocks since PV 62. <br> 108  * 109  * Require peer to return a BlockBodies message. 110  * Specify the set of blocks that we're interested in with the hashes. 111  */ 112  GET_BLOCK_BODIES(0x05), 113  114  /** 115  * {@code [+0x06, [transactions_0, uncles_0] , ...] } <br> 116  * 117  * Replaces Blocks since PV 62. <br> 118  * 119  * Reply to GetBlockBodies. 120  * The items in the list (following the message ID) are some of the blocks, minus the header, 121  * in the format described in the main Ethereum specification, previously asked for in a GetBlockBodies message. 122  * This may validly contain no block headers 123  * if no block headers were able to be returned for the GetBlockHeaders query. 124  */ 125  BLOCK_BODIES(0x06), 126  127  /** 128  * {@code [+0x07 [blockHeader, transactionList, uncleList], totalDifficulty] } <br> 129  * 130  * Specify a single block that the peer should know about. The composite item 131  * in the list (following the message ID) is a block in the format described 132  * in the main Ethereum specification. 133  */ 134  NEW_BLOCK(0x07), 135  136  RSK_MESSAGE(0x08); 137  138  private int cmd; 139  140  private static final Map<EthVersion, Map<Integer, EthMessageCodes>> intToTypeMap = new HashMap<>(); 141  private static final Map<EthVersion, EthMessageCodes[]> versionToValuesMap = new HashMap<>(); 142  143  static { 144  145  versionToValuesMap.put(V62, new EthMessageCodes[]{ 146  STATUS, 147  NEW_BLOCK_HASHES, 148  TRANSACTIONS, 149  GET_BLOCK_HEADERS, 150  BLOCK_HEADERS, 151  GET_BLOCK_BODIES, 152  BLOCK_BODIES, 153  NEW_BLOCK, 154  RSK_MESSAGE 155  }); 156  157  for (EthVersion v : EthVersion.values()) { 158  Map<Integer, EthMessageCodes> map = new HashMap<>(); 159  intToTypeMap.put(v, map); 160  for (EthMessageCodes code : values(v)) { 161  map.put(code.cmd, code); 162  } 163  } 164  } 165  166  private EthMessageCodes(int cmd) { 167  this.cmd = cmd; 168  } 169  170  public static EthMessageCodes[] values(EthVersion v) { 171  return versionToValuesMap.get(v); 172  } 173  174  public static EthMessageCodes fromByte(byte i, EthVersion v) { 175  Map<Integer, EthMessageCodes> map = intToTypeMap.get(v); 176  return map.get((int) i); 177  } 178  179  public static boolean inRange(byte code, EthVersion v) { 180  EthMessageCodes[] codes = values(v); 181  return code >= codes[0].asByte() && code <= codes[codes.length - 1].asByte(); 182  } 183  184  public byte asByte() { 185  return (byte) (cmd); 186  } 187 }