GetBlocksMessage, GetHeadersMessage: move parse() to static constructor read()

This commit is contained in:
Andreas Schildbach 2023-04-14 22:10:56 +02:00
parent eac2c1fee3
commit a114c6ca0e
3 changed files with 52 additions and 21 deletions

View file

@ -227,9 +227,9 @@ public class BitcoinSerializer extends MessageSerializer {
} else if (command.equals("getdata")) {
return GetDataMessage.read(payload);
} else if (command.equals("getblocks")) {
return new GetBlocksMessage(payload);
return GetBlocksMessage.read(payload);
} else if (command.equals("getheaders")) {
return new GetHeadersMessage(payload);
return GetHeadersMessage.read(payload);
} else if (command.equals("tx")) {
return makeTransaction(payload);
} else if (command.equals("sendaddrv2")) {

View file

@ -40,29 +40,37 @@ public class GetBlocksMessage extends BaseMessage {
protected BlockLocator locator;
protected Sha256Hash stopHash;
/**
* 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 GetBlocksMessage read(ByteBuffer payload) throws BufferUnderflowException, ProtocolException {
long version = ByteUtils.readUint32(payload);
VarInt startCountVarInt = VarInt.read(payload);
check(startCountVarInt.fitsInt(), BufferUnderflowException::new);
int startCount = startCountVarInt.intValue();
if (startCount > 500)
throw new ProtocolException("Number of locators cannot be > 500, received: " + startCount);
BlockLocator locator = new BlockLocator();
for (int i = 0; i < startCount; i++) {
locator = locator.add(Sha256Hash.read(payload));
}
Sha256Hash stopHash = Sha256Hash.read(payload);
return new GetBlocksMessage(version, locator, stopHash);
}
public GetBlocksMessage(long protocolVersion, BlockLocator locator, Sha256Hash stopHash) {
this.version = protocolVersion;
this.locator = locator;
this.stopHash = stopHash;
}
public GetBlocksMessage(ByteBuffer payload) throws ProtocolException {
super(payload);
}
@Override
protected void parse(ByteBuffer payload) throws BufferUnderflowException, ProtocolException {
version = ByteUtils.readUint32(payload);
VarInt startCountVarInt = VarInt.read(payload);
check(startCountVarInt.fitsInt(), BufferUnderflowException::new);
int startCount = startCountVarInt.intValue();
if (startCount > 500)
throw new ProtocolException("Number of locators cannot be > 500, received: " + startCount);
locator = new BlockLocator();
for (int i = 0; i < startCount; i++) {
locator = locator.add(Sha256Hash.read(payload));
}
stopHash = Sha256Hash.read(payload);
throw new UnsupportedOperationException();
}
public BlockLocator getLocator() {

View file

@ -17,9 +17,14 @@
package org.bitcoinj.core;
import org.bitcoinj.base.Sha256Hash;
import org.bitcoinj.base.VarInt;
import org.bitcoinj.base.internal.ByteUtils;
import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;
import static org.bitcoinj.base.internal.Preconditions.check;
/**
* <p>The "getheaders" command is structurally identical to "getblocks", but has different meaning. On receiving this
* message a Bitcoin node returns matching blocks up to the limit, but without the bodies. It is useful as an
@ -29,12 +34,30 @@ import java.nio.ByteBuffer;
* <p>Instances of this class are not safe for use by multiple threads.</p>
*/
public class GetHeadersMessage extends GetBlocksMessage {
public GetHeadersMessage(long protocolVersion, BlockLocator locator, Sha256Hash stopHash) {
super(protocolVersion, locator, stopHash);
/**
* 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 GetHeadersMessage read(ByteBuffer payload) throws BufferUnderflowException, ProtocolException {
long version = ByteUtils.readUint32(payload);
VarInt startCountVarInt = VarInt.read(payload);
check(startCountVarInt.fitsInt(), BufferUnderflowException::new);
int startCount = startCountVarInt.intValue();
if (startCount > 500)
throw new ProtocolException("Number of locators cannot be > 500, received: " + startCount);
BlockLocator locator = new BlockLocator();
for (int i = 0; i < startCount; i++) {
locator = locator.add(Sha256Hash.read(payload));
}
Sha256Hash stopHash = Sha256Hash.read(payload);
return new GetHeadersMessage(version, locator, stopHash);
}
public GetHeadersMessage(ByteBuffer payload) throws ProtocolException {
super(payload);
public GetHeadersMessage(long protocolVersion, BlockLocator locator, Sha256Hash stopHash) {
super(protocolVersion, locator, stopHash);
}
@Override