/*
* Copyright (c) [2016] [ <ether.camp> ]
* This file is part of the ethereumJ library.
*
* The ethereumJ library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The ethereumJ library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with the ethereumJ library. If not, see <http://www.gnu.org/licenses/>.
*/
package org.ethereum.net.eth.message;
import org.ethereum.util.RLP;
import org.ethereum.util.RLPList;
import org.ethereum.util.Utils;
import org.spongycastle.util.encoders.Hex;
import java.util.ArrayList;
import java.util.List;
/**
* Wrapper around an Ethereum GetBlockBodies message on the network
*
* @see EthMessageCodes#GET_BLOCK_BODIES
*
* @author Mikhail Kalinin
* @since 04.09.2015
*/
public class GetBlockBodiesMessage extends EthMessage {
/**
* List of block hashes for which to retrieve the block bodies
*/
private List<byte[]> blockHashes;
public GetBlockBodiesMessage(byte[] encoded) {
super(encoded);
}
public GetBlockBodiesMessage(List<byte[]> blockHashes) {
this.blockHashes = blockHashes;
parsed = true;
}
private synchronized void parse() {
if (parsed) return;
RLPList paramsList = (RLPList) RLP.decode2(encoded).get(0);
blockHashes = new ArrayList<>();
for (int i = 0; i < paramsList.size(); ++i) {
blockHashes.add(paramsList.get(i).getRLPData());
}
parsed = true;
}
private void encode() {
List<byte[]> encodedElements = new ArrayList<>();
for (byte[] hash : blockHashes)
encodedElements.add(RLP.encodeElement(hash));
byte[][] encodedElementArray = encodedElements.toArray(new byte[encodedElements.size()][]);
this.encoded = RLP.encodeList(encodedElementArray);
}
@Override
public byte[] getEncoded() {
if (encoded == null) encode();
return encoded;
}
@Override
public Class<BlockBodiesMessage> getAnswerMessage() {
return BlockBodiesMessage.class;
}
public List<byte[]> getBlockHashes() {
parse();
return blockHashes;
}
@Override
public EthMessageCodes getCommand() {
return EthMessageCodes.GET_BLOCK_BODIES;
}
public String toString() {
parse();
StringBuilder payload = new StringBuilder();
payload.append("count( ").append(blockHashes.size()).append(" ) ");
if (logger.isDebugEnabled()) {
for (byte[] hash : blockHashes) {
payload.append(Hex.toHexString(hash).substring(0, 6)).append(" | ");
}
if (!blockHashes.isEmpty()) {
payload.delete(payload.length() - 3, payload.length());
}
} else {
payload.append(Utils.getHashListShort(blockHashes));
}
return "[" + getCommand().name() + " " + payload + "]";
}
}