diff --git a/core/src/main/java/org/bitcoinj/core/BitcoinSerializer.java b/core/src/main/java/org/bitcoinj/core/BitcoinSerializer.java index 4b9454084..fdbee455a 100644 --- a/core/src/main/java/org/bitcoinj/core/BitcoinSerializer.java +++ b/core/src/main/java/org/bitcoinj/core/BitcoinSerializer.java @@ -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); } /** diff --git a/core/src/main/java/org/bitcoinj/params/BitcoinNetworkParams.java b/core/src/main/java/org/bitcoinj/params/BitcoinNetworkParams.java index 8c732bce9..4b49f65bb 100644 --- a/core/src/main/java/org/bitcoinj/params/BitcoinNetworkParams.java +++ b/core/src/main/java/org/bitcoinj/params/BitcoinNetworkParams.java @@ -248,7 +248,7 @@ public abstract class BitcoinNetworkParams extends NetworkParameters { @Override public BitcoinSerializer getSerializer() { - return new BitcoinSerializer(this); + return new BitcoinSerializer(network); } @Override diff --git a/core/src/test/java/org/bitcoinj/core/SendHeadersMessageTest.java b/core/src/test/java/org/bitcoinj/core/SendHeadersMessageTest.java index 737832887..eafac639d 100644 --- a/core/src/test/java/org/bitcoinj/core/SendHeadersMessageTest.java +++ b/core/src/test/java/org/bitcoinj/core/SendHeadersMessageTest.java @@ -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); } } diff --git a/integration-test/src/test/java/org/bitcoinj/core/FilteredBlockAndPartialMerkleTreeTest.java b/integration-test/src/test/java/org/bitcoinj/core/FilteredBlockAndPartialMerkleTreeTest.java index 32b9cfdfd..b59c11cf1 100644 --- a/integration-test/src/test/java/org/bitcoinj/core/FilteredBlockAndPartialMerkleTreeTest.java +++ b/integration-test/src/test/java/org/bitcoinj/core/FilteredBlockAndPartialMerkleTreeTest.java @@ -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 parameters() {