HeadersMessage: move parse() to static constructor read()

This commit is contained in:
Andreas Schildbach 2023-04-14 22:37:48 +02:00
parent 92355b1c32
commit 38ea113c2f
2 changed files with 32 additions and 24 deletions

View File

@ -247,7 +247,7 @@ public class BitcoinSerializer extends MessageSerializer {
check(!payload.hasRemaining(), ProtocolException::new);
return new VersionAck();
} else if (command.equals("headers")) {
return new HeadersMessage(payload);
return HeadersMessage.read(payload);
} else if (command.equals("filterload")) {
return makeBloomFilter(payload);
} else if (command.equals("notfound")) {

View File

@ -46,8 +46,36 @@ public class HeadersMessage extends BaseMessage {
private List<Block> blockHeaders;
public HeadersMessage(ByteBuffer payload) throws ProtocolException {
super(payload);
/**
* Deserialize this message from a given payload.
*
* @param payload payload to deserialize from
* @return read message
* @throws BufferUnderflowException if the read message extends beyond the remaining bytes of the payload
*/
public static HeadersMessage read(ByteBuffer payload) throws BufferUnderflowException, ProtocolException {
VarInt numHeadersVarInt = VarInt.read(payload);
check(numHeadersVarInt.fitsInt(), BufferUnderflowException::new);
int numHeaders = numHeadersVarInt.intValue();
if (numHeaders > MAX_HEADERS)
throw new ProtocolException("Too many headers: got " + numHeaders + " which is larger than " +
MAX_HEADERS);
List<Block> blockHeaders = new ArrayList<>();
for (int i = 0; i < numHeaders; ++i) {
final Block newBlockHeader = new Block(payload);
if (newBlockHeader.hasTransactions()) {
throw new ProtocolException("Block header does not end with a null byte");
}
blockHeaders.add(newBlockHeader);
}
if (log.isDebugEnabled()) {
for (int i = 0; i < numHeaders; ++i) {
log.debug(blockHeaders.get(i).toString());
}
}
return new HeadersMessage(blockHeaders);
}
public HeadersMessage(Block... headers) throws ProtocolException {
@ -71,27 +99,7 @@ public class HeadersMessage extends BaseMessage {
@Override
protected void parse(ByteBuffer payload) throws BufferUnderflowException, ProtocolException {
VarInt numHeadersVarInt = VarInt.read(payload);
check(numHeadersVarInt.fitsInt(), BufferUnderflowException::new);
int numHeaders = numHeadersVarInt.intValue();
if (numHeaders > MAX_HEADERS)
throw new ProtocolException("Too many headers: got " + numHeaders + " which is larger than " +
MAX_HEADERS);
blockHeaders = new ArrayList<>();
for (int i = 0; i < numHeaders; ++i) {
final Block newBlockHeader = new Block(payload);
if (newBlockHeader.hasTransactions()) {
throw new ProtocolException("Block header does not end with a null byte");
}
blockHeaders.add(newBlockHeader);
}
if (log.isDebugEnabled()) {
for (int i = 0; i < numHeaders; ++i) {
log.debug(this.blockHeaders.get(i).toString());
}
}
throw new UnsupportedOperationException();
}
public List<Block> getBlockHeaders() {