Coverage Summary for Class: MessageCodesResolver (org.ethereum.net.rlpx)

Class Class, % Method, % Line, %
MessageCodesResolver 0% (0/1) 0% (0/12) 0% (0/24)


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.rlpx; 21  22 import org.ethereum.net.client.Capability; 23 import org.ethereum.net.eth.EthVersion; 24 import org.ethereum.net.eth.message.EthMessageCodes; 25 import org.ethereum.net.p2p.P2pMessageCodes; 26  27 import java.util.Collections; 28 import java.util.HashMap; 29 import java.util.List; 30 import java.util.Map; 31  32 import static org.ethereum.net.eth.EthVersion.*; 33  34 /** 35  * @author Mikhail Kalinin 36  * @since 14.07.2015 37  */ 38 public class MessageCodesResolver { 39  40  private Map<String, Integer> offsets = new HashMap<>(); 41  42  public MessageCodesResolver() { 43  } 44  45  public MessageCodesResolver(List<Capability> caps) { 46  init(caps); 47  } 48  49  public void init(List<Capability> caps) { 50  Collections.sort(caps); 51  int offset = P2pMessageCodes.USER.asByte() + 1; 52  53  for (Capability capability : caps) { 54  if (capability.getName().equals(Capability.RSK)) { 55  setEthOffset(offset); 56  EthVersion v = fromCode(capability.getVersion()); 57  offset += EthMessageCodes.values(v).length; 58  } 59  60  } 61  } 62  63  public byte withP2pOffset(byte code) { 64  return withOffset(code, Capability.P2P); 65  } 66  67  public byte withEthOffset(byte code) { 68  return withOffset(code, Capability.RSK); 69  } 70  71  public byte withOffset(byte code, String cap) { 72  byte offset = getOffset(cap); 73  return (byte)(code + offset); 74  } 75  76  public byte resolveP2p(byte code) { 77  return resolve(code, Capability.P2P); 78  } 79  80  public byte resolveEth(byte code) { 81  return resolve(code, Capability.RSK); 82  } 83  84  private byte resolve(byte code, String cap) { 85  byte offset = getOffset(cap); 86  return (byte)(code - offset); 87  } 88  89  private byte getOffset(String cap) { 90  Integer offset = offsets.get(cap); 91  return offset == null ? 0 : offset.byteValue(); 92  } 93  94  public void setEthOffset(int offset) { 95  setOffset(Capability.RSK, offset); 96  } 97  98  private void setOffset(String cap, int offset) { 99  offsets.put(cap, offset); 100  } 101 }