Coverage Summary for Class: NewBlockHashesMessage (co.rsk.net.messages)

Class Class, % Method, % Line, %
NewBlockHashesMessage 0% (0/1) 0% (0/9) 0% (0/34)


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 package co.rsk.net.messages; 20  21 import org.ethereum.core.BlockIdentifier; 22 import org.ethereum.net.eth.message.EthMessageCodes; 23 import org.ethereum.util.RLP; 24 import org.ethereum.util.RLPList; 25  26 import java.util.ArrayList; 27 import java.util.List; 28  29 /** 30  * Wrapper around an Ethereum NewBlockHashes message on the network<br> 31  * 32  * @see EthMessageCodes#NEW_BLOCK_HASHES 33  * 34  * @author Mikhail Kalinin 35  * @since 05.09.2015 36  */ 37 public class NewBlockHashesMessage extends Message { 38  39  /** 40  * List of identifiers holding hash and number of the blocks 41  */ 42  private List<BlockIdentifier> blockIdentifiers; 43  44  private boolean parsed; 45  private byte[] encoded; 46  47  public NewBlockHashesMessage(byte[] payload) { 48  this.encoded = payload; 49  this.parsed = false; 50  } 51  52  public NewBlockHashesMessage(List<BlockIdentifier> blockIdentifiers) { 53  this.blockIdentifiers = blockIdentifiers; 54  parsed = true; 55  } 56  57  private void parse() { 58  RLPList paramsList = (RLPList) RLP.decode2(encoded).get(0); 59  60  blockIdentifiers = new ArrayList<>(); 61  62  for (int i = 0; i < paramsList.size(); ++i) { 63  RLPList rlpData = ((RLPList) paramsList.get(i)); 64  blockIdentifiers.add(new BlockIdentifier(rlpData)); 65  } 66  parsed = true; 67  } 68  69  private void encode() { 70  List<byte[]> encodedElements = new ArrayList<>(); 71  72  for (BlockIdentifier identifier : blockIdentifiers) { 73  encodedElements.add(identifier.getEncoded()); 74  } 75  76  byte[][] encodedElementArray = encodedElements.toArray(new byte[encodedElements.size()][]); 77  this.encoded = RLP.encodeList(encodedElementArray); 78  } 79  80  81  @Override 82  public MessageType getMessageType() { 83  return MessageType.NEW_BLOCK_HASHES; 84  } 85  86  @Override 87  public byte[] getEncodedMessage() { 88  if (encoded == null) { 89  encode(); 90  } 91  92  return encoded; 93  } 94  95  public List<BlockIdentifier> getBlockIdentifiers() { 96  if (!parsed) { 97  parse(); 98  } 99  100  return blockIdentifiers; 101  } 102  103  @Override 104  public void accept(MessageVisitor v) { 105  v.apply(this); 106  } 107  108  @Override 109  public String toString() { 110  if (!parsed) { 111  parse(); 112  } 113  114  return "[" + getMessageType() + "] (" + blockIdentifiers.size() + ")"; 115  } 116  117 }