BitcoinSerializer: use Network not NetworkParameters

Also deprecate (unused) method `getParameters()`.
This commit is contained in:
Sean Gilligan 2023-05-01 09:04:10 -07:00 committed by Andreas Schildbach
parent 8bf4f57888
commit 2526f5c78a
4 changed files with 35 additions and 13 deletions

View File

@ -17,6 +17,7 @@
package org.bitcoinj.core;
import org.bitcoinj.base.Network;
import org.bitcoinj.base.Sha256Hash;
import org.bitcoinj.base.internal.ByteUtils;
import org.slf4j.Logger;
@ -49,7 +50,7 @@ public class BitcoinSerializer extends MessageSerializer {
private static final Logger log = LoggerFactory.getLogger(BitcoinSerializer.class);
private static final int COMMAND_LEN = 12;
private final NetworkParameters params;
private final Network network;
private final int packetMagic;
private final int protocolVersion;
@ -83,28 +84,49 @@ public class BitcoinSerializer extends MessageSerializer {
/**
* Constructs a BitcoinSerializer with the given behavior.
*
* @param params networkParams used to create Messages instances and determining packetMagic
* @param params networkParams used to determine packetMagic
*/
@Deprecated
public BitcoinSerializer(NetworkParameters params) {
this(params, ProtocolVersion.CURRENT.intValue());
this(params.network());
}
/**
* Constructs a BitcoinSerializer with the given behavior.
*
* @param params networkParams used to create Messages instances and determining packetMagic
* @param network used to determine packetMagic
*/
public BitcoinSerializer(Network network) {
this(network, ProtocolVersion.CURRENT.intValue());
}
/**
* Constructs a BitcoinSerializer with the given behavior.
*
* @param params networkParams used to determine packetMagic
* @param protocolVersion the protocol version to use
*/
@Deprecated
public BitcoinSerializer(NetworkParameters params, int protocolVersion) {
this.params = params;
this.packetMagic = params.getPacketMagic();
this(params.network, protocolVersion);
}
/**
* Constructs a BitcoinSerializer with the given behavior.
*
* @param network used to determine packetMagic
* @param protocolVersion the protocol version to use
*/
public BitcoinSerializer(Network network, int protocolVersion) {
this.network = network;
this.packetMagic = NetworkParameters.of(network).getPacketMagic();
this.protocolVersion = protocolVersion;
}
@Override
public BitcoinSerializer withProtocolVersion(int protocolVersion) {
return protocolVersion == this.protocolVersion ?
this : new BitcoinSerializer(params, protocolVersion);
this : new BitcoinSerializer(network, protocolVersion);
}
@Override
@ -271,8 +293,9 @@ public class BitcoinSerializer extends MessageSerializer {
/**
* Get the network parameters for this serializer.
*/
@Deprecated
public NetworkParameters getParameters() {
return params;
return NetworkParameters.of(network);
}
/**

View File

@ -248,7 +248,7 @@ public abstract class BitcoinNetworkParams extends NetworkParameters {
@Override
public BitcoinSerializer getSerializer() {
return new BitcoinSerializer(this);
return new BitcoinSerializer(network);
}
@Override

View File

@ -17,7 +17,7 @@
package org.bitcoinj.core;
import com.google.common.io.BaseEncoding;
import org.bitcoinj.params.RegTestParams;
import org.bitcoinj.base.BitcoinNetwork;
import org.junit.Test;
import java.nio.ByteBuffer;
@ -26,7 +26,6 @@ import static org.junit.Assert.assertTrue;
public class SendHeadersMessageTest {
private static final BaseEncoding HEX = BaseEncoding.base16().lowerCase();
private static final NetworkParameters REGTEST = RegTestParams.get();
@Test
public void decodeAndEncode() throws Exception {
@ -40,7 +39,7 @@ public class SendHeadersMessageTest {
+ "c96fe88d4a0f01ed9dedae2b6f9e00da94cad0fecaae66ecf689bf71b50000000000000000000000000000000000000000000000000");
ByteBuffer buffer = ByteBuffer.wrap(message);
BitcoinSerializer serializer = new BitcoinSerializer(REGTEST);
BitcoinSerializer serializer = new BitcoinSerializer(BitcoinNetwork.REGTEST);
assertTrue(serializer.deserialize(buffer) instanceof SendHeadersMessage);
}
}

View File

@ -56,7 +56,7 @@ import static org.junit.Assert.fail;
@RunWith(value = Parameterized.class)
public class FilteredBlockAndPartialMerkleTreeTest extends TestWithPeerGroup {
private static final BitcoinSerializer SERIALIZER = new BitcoinSerializer(TESTNET);
private static final BitcoinSerializer SERIALIZER = new BitcoinSerializer(BitcoinNetwork.TESTNET);
@Parameterized.Parameters
public static Collection<ClientType[]> parameters() {