BitcoinSerializer: store packetMagic in instance field

This commit is contained in:
Sean Gilligan 2023-05-03 10:43:14 +02:00 committed by Andreas Schildbach
parent 369445d1e8
commit f62259b780

View file

@ -50,6 +50,7 @@ public class BitcoinSerializer extends MessageSerializer {
private static final int COMMAND_LEN = 12; private static final int COMMAND_LEN = 12;
private final NetworkParameters params; private final NetworkParameters params;
private final int packetMagic;
private final int protocolVersion; private final int protocolVersion;
private static final Map<Class<? extends Message>, String> names = new HashMap<>(); private static final Map<Class<? extends Message>, String> names = new HashMap<>();
@ -96,6 +97,7 @@ public class BitcoinSerializer extends MessageSerializer {
*/ */
public BitcoinSerializer(NetworkParameters params, int protocolVersion) { public BitcoinSerializer(NetworkParameters params, int protocolVersion) {
this.params = params; this.params = params;
this.packetMagic = params.getPacketMagic();
this.protocolVersion = protocolVersion; this.protocolVersion = protocolVersion;
} }
@ -116,7 +118,7 @@ public class BitcoinSerializer extends MessageSerializer {
@Override @Override
public void serialize(String name, byte[] message, OutputStream out) throws IOException { public void serialize(String name, byte[] message, OutputStream out) throws IOException {
byte[] header = new byte[4 + COMMAND_LEN + 4 + 4 /* checksum */]; byte[] header = new byte[4 + COMMAND_LEN + 4 + 4 /* checksum */];
ByteUtils.writeInt32BE(params.getPacketMagic(), header, 0); ByteUtils.writeInt32BE(packetMagic, header, 0);
// The header array is initialized to zero by Java so we don't have to worry about // The header array is initialized to zero by Java so we don't have to worry about
// NULL terminating the string here. // NULL terminating the string here.
@ -344,7 +346,7 @@ public class BitcoinSerializer extends MessageSerializer {
byte b = in.get(); byte b = in.get();
// We're looking for a run of bytes that is the same as the packet magic but we want to ignore partial // We're looking for a run of bytes that is the same as the packet magic but we want to ignore partial
// magics that aren't complete. So we keep track of where we're up to with magicCursor. // magics that aren't complete. So we keep track of where we're up to with magicCursor.
byte expectedByte = (byte)(0xFF & params.getPacketMagic() >>> (magicCursor * 8)); byte expectedByte = (byte)(0xFF & packetMagic >>> (magicCursor * 8));
if (b == expectedByte) { if (b == expectedByte) {
magicCursor--; magicCursor--;
if (magicCursor < 0) { if (magicCursor < 0) {