Global search and replace to use the param-class specific instance getters and prodNet -> MainNetParams.

The old "prodnet" name could be confusing to non-native speakers or people who just haven't been exposed to Google lingo. Main net is more idiomatic.

Conflicts:
	tools/src/main/java/com/google/bitcoin/tools/BlockImporter.java
This commit is contained in:
Mike Hearn
2013-05-08 16:30:55 +02:00
parent 52586edb33
commit f4c3b6a49d
26 changed files with 174 additions and 149 deletions

View File

@@ -16,6 +16,9 @@
package com.google.bitcoin.core;
import com.google.bitcoin.params.MainNetParams;
import com.google.bitcoin.params.TestNet3Params;
/**
* <p>A Bitcoin address looks like 1MsScoe2fTJoq4ZPdQgqyhgWeoNamYPevy and is derived from an elliptic curve public key
* plus a set of network parameters. Not to be confused with a {@link PeerAddress} or {@link AddressMessage}
@@ -85,9 +88,7 @@ public class Address extends VersionedChecksummedBytes {
*/
public NetworkParameters getParameters() {
// TODO: There should be a more generic way to get all supported networks.
NetworkParameters[] networks =
new NetworkParameters[] { NetworkParameters.testNet(), NetworkParameters.prodNet() };
NetworkParameters[] networks = { TestNet3Params.get(), MainNetParams.get() };
for (NetworkParameters params : networks) {
for (int code : params.getAcceptableAddressCodes()) {
if (code == version) {
@@ -102,9 +103,6 @@ public class Address extends VersionedChecksummedBytes {
* Given an address, examines the version byte and attempts to find a matching NetworkParameters. If you aren't sure
* which network the address is intended for (eg, it was provided by a user), you can use this to decide if it is
* compatible with the current wallet. You should be able to handle a null response from this method.
*
* @param address
* @return a NetworkParameters representing the network the address is intended for, or null if unknown.
*/
public static NetworkParameters getParametersFromAddress(String address) throws AddressFormatException {
try {

View File

@@ -36,8 +36,10 @@ import static com.google.bitcoin.core.Utils.COIN;
/**
* <p>NetworkParameters contains the data needed for working with an instantiation of a Bitcoin chain.</p>
*
* <p>Currently there are only two, the production chain and the test chain. There is also a "unit test chain" which
* is internal to bitcoinj and can't be used on a real network. In future there may be others. </p>
* <p>This is an abstract class, concrete instantiations can be found in the params package. There are four:
* one for the main network ({@link MainNetParams}), one for the public test network, and two others that are
* intended for unit testing and local app development purposes. Although this class contains some aliases for
* them, you are encouraged to call the static get() methods on each specific params class directly.</p>
*/
public abstract class NetworkParameters implements Serializable {
/**
@@ -52,6 +54,8 @@ public abstract class NetworkParameters implements Serializable {
/** The string returned by getId() for the main, production network where people trade things. */
public static final String ID_PRODNET = "org.bitcoin.production";
/** The string returned by getId() for the main, production network where people trade things. */
public static final String ID_MAINNET = "org.bitcoin.production";
/** The string returned by getId() for the testnet. */
public static final String ID_TESTNET = "org.bitcoin.test";
/** Unit test network. */
@@ -59,8 +63,6 @@ public abstract class NetworkParameters implements Serializable {
// TODO: Seed nodes should be here as well.
// TODO: Replace with getters and then finish making all these fields final.
protected Block genesisBlock;
protected BigInteger proofOfWorkLimit;
protected int port;
@@ -177,12 +179,12 @@ public abstract class NetworkParameters implements Serializable {
/** Returns the network parameters for the given string ID or NULL if not recognized. */
public static NetworkParameters fromID(String id) {
if (id.equals(ID_PRODNET)) {
return prodNet();
if (id.equals(ID_MAINNET)) {
return MainNetParams.get();
} else if (id.equals(ID_TESTNET)) {
return testNet();
return TestNet3Params.get();
} else if (id.equals(ID_UNITTESTNET)) {
return unitTests();
return UnitTestParams.get();
} else {
return null;
}

View File

@@ -16,6 +16,8 @@
package com.google.bitcoin.core;
import com.google.bitcoin.params.MainNetParams;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigInteger;
@@ -90,7 +92,7 @@ public class PeerAddress extends ChildMessage {
* Constructs a peer address from the given IP address. Port and protocol version are default for the prodnet.
*/
public PeerAddress(InetAddress addr) {
this(addr, NetworkParameters.prodNet().getPort());
this(addr, MainNetParams.get().getPort());
}
public PeerAddress(InetSocketAddress addr) {

View File

@@ -16,6 +16,8 @@
package com.google.bitcoin.core;
import com.google.bitcoin.params.MainNetParams;
import com.google.bitcoin.params.TestNet3Params;
import org.junit.Test;
import org.spongycastle.util.encoders.Hex;
@@ -24,8 +26,8 @@ import java.util.Arrays;
import static org.junit.Assert.*;
public class AddressTest {
static final NetworkParameters testParams = NetworkParameters.testNet();
static final NetworkParameters prodParams = NetworkParameters.prodNet();
static final NetworkParameters testParams = TestNet3Params.get();
static final NetworkParameters mainParams = MainNetParams.get();
@Test
public void stringification() throws Exception {
@@ -33,7 +35,7 @@ public class AddressTest {
Address a = new Address(testParams, Hex.decode("fda79a24e50ff70ff42f7d89585da5bd19d9e5cc"));
assertEquals("n4eA2nbYqErp7H6jebchxAN59DmNpksexv", a.toString());
Address b = new Address(prodParams, Hex.decode("4a22c3c4cbb31e4d03b15550636762bda0baf85a"));
Address b = new Address(mainParams, Hex.decode("4a22c3c4cbb31e4d03b15550636762bda0baf85a"));
assertEquals("17kzeh4N8g49GFvdDzSf8PjaPfyoD1MndL", b.toString());
}
@@ -42,7 +44,7 @@ public class AddressTest {
Address a = new Address(testParams, "n4eA2nbYqErp7H6jebchxAN59DmNpksexv");
assertEquals("fda79a24e50ff70ff42f7d89585da5bd19d9e5cc", Utils.bytesToHexString(a.getHash160()));
Address b = new Address(prodParams, "17kzeh4N8g49GFvdDzSf8PjaPfyoD1MndL");
Address b = new Address(mainParams, "17kzeh4N8g49GFvdDzSf8PjaPfyoD1MndL");
assertEquals("4a22c3c4cbb31e4d03b15550636762bda0baf85a", Utils.bytesToHexString(b.getHash160()));
}
@@ -74,8 +76,8 @@ public class AddressTest {
fail();
} catch (WrongNetworkException e) {
// Success.
assertEquals(e.verCode, NetworkParameters.prodNet().getAddressHeader());
assertTrue(Arrays.equals(e.acceptableVersions, NetworkParameters.testNet().getAcceptableAddressCodes()));
assertEquals(e.verCode, MainNetParams.get().getAddressHeader());
assertTrue(Arrays.equals(e.acceptableVersions, TestNet3Params.get().getAcceptableAddressCodes()));
} catch (AddressFormatException e) {
fail();
}
@@ -84,8 +86,8 @@ public class AddressTest {
@Test
public void getNetwork() throws Exception {
NetworkParameters params = Address.getParametersFromAddress("17kzeh4N8g49GFvdDzSf8PjaPfyoD1MndL");
assertEquals(NetworkParameters.prodNet().getId(), params.getId());
assertEquals(MainNetParams.get().getId(), params.getId());
params = Address.getParametersFromAddress("n4eA2nbYqErp7H6jebchxAN59DmNpksexv");
assertEquals(NetworkParameters.testNet().getId(), params.getId());
assertEquals(TestNet3Params.get().getId(), params.getId());
}
}

View File

@@ -17,6 +17,7 @@
package com.google.bitcoin.core;
import com.google.bitcoin.params.MainNetParams;
import org.junit.Test;
import org.spongycastle.util.encoders.Hex;
@@ -24,9 +25,7 @@ import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.Arrays;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.*;
public class BitcoinSerializerTest {
private final byte[] addrMessage = Hex.decode("f9beb4d96164647200000000000000001f000000" +
@@ -54,7 +53,7 @@ public class BitcoinSerializerTest {
@Test
public void testAddr() throws Exception {
BitcoinSerializer bs = new BitcoinSerializer(NetworkParameters.prodNet());
BitcoinSerializer bs = new BitcoinSerializer(MainNetParams.get());
// the actual data from https://en.bitcoin.it/wiki/Protocol_specification#addr
ByteArrayInputStream bais = new ByteArrayInputStream(addrMessage);
AddressMessage a = (AddressMessage)bs.deserialize(bais);
@@ -71,7 +70,7 @@ public class BitcoinSerializerTest {
@Test
public void testLazyParsing() throws Exception {
BitcoinSerializer bs = new BitcoinSerializer(NetworkParameters.prodNet(), true, false);
BitcoinSerializer bs = new BitcoinSerializer(MainNetParams.get(), true, false);
ByteArrayInputStream bais = new ByteArrayInputStream(txMessage);
Transaction tx = (Transaction)bs.deserialize(bais);
@@ -96,7 +95,7 @@ public class BitcoinSerializerTest {
}
private void testCachedParsing(boolean lazy) throws Exception {
BitcoinSerializer bs = new BitcoinSerializer(NetworkParameters.prodNet(), lazy, true);
BitcoinSerializer bs = new BitcoinSerializer(MainNetParams.get(), lazy, true);
//first try writing to a fields to ensure uncaching and children are not affected
ByteArrayInputStream bais = new ByteArrayInputStream(txMessage);
@@ -163,7 +162,7 @@ public class BitcoinSerializerTest {
*/
@Test
public void testHeaders1() throws Exception {
BitcoinSerializer bs = new BitcoinSerializer(NetworkParameters.prodNet());
BitcoinSerializer bs = new BitcoinSerializer(MainNetParams.get());
ByteArrayInputStream bais = new ByteArrayInputStream(Hex.decode("f9beb4d9686561" +
"646572730000000000520000005d4fab8101010000006fe28c0ab6f1b372c1a6a246ae6" +
@@ -190,7 +189,7 @@ public class BitcoinSerializerTest {
* Get 6 headers of blocks 1-6 in the chain
*/
public void testHeaders2() throws Exception {
BitcoinSerializer bs = new BitcoinSerializer(NetworkParameters.prodNet());
BitcoinSerializer bs = new BitcoinSerializer(MainNetParams.get());
ByteArrayInputStream bais = new ByteArrayInputStream(Hex.decode("f9beb4d96865616465" +
"72730000000000e701000085acd4ea06010000006fe28c0ab6f1b372c1a6a246ae63f74f931e" +

View File

@@ -17,6 +17,7 @@
package com.google.bitcoin.core;
import com.google.bitcoin.core.Wallet.BalanceType;
import com.google.bitcoin.params.MainNetParams;
import com.google.bitcoin.params.TestNet2Params;
import com.google.bitcoin.store.BlockStore;
import com.google.bitcoin.store.MemoryBlockStore;
@@ -379,7 +380,7 @@ public class BlockChainTest {
@Test
public void estimatedBlockTime() throws Exception {
NetworkParameters params = NetworkParameters.prodNet();
NetworkParameters params = MainNetParams.get();
BlockChain prod = new BlockChain(params, new MemoryBlockStore(params));
Date d = prod.estimateBlockTime(200000);
// The actual date of block 200,000 was 2012-09-22 10:47:00

View File

@@ -1,5 +1,6 @@
package com.google.bitcoin.core;
import com.google.bitcoin.params.MainNetParams;
import org.junit.Test;
import org.spongycastle.util.encoders.Hex;
@@ -48,7 +49,7 @@ public class BloomFilterTest {
@Test
public void walletTest() throws Exception {
NetworkParameters params = NetworkParameters.prodNet();
NetworkParameters params = MainNetParams.get();
DumpedPrivateKey privKey = new DumpedPrivateKey(params, "5Kg1gnAjaLfKiwhhPpGS3QfRg2m6awQvaj98JCZBZQ5SuS2F15C");

View File

@@ -16,8 +16,10 @@
package com.google.bitcoin.core;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import com.google.bitcoin.core.AbstractBlockChain.NewBlockType;
import com.google.bitcoin.core.Wallet.BalanceType;
import com.google.bitcoin.params.MainNetParams;
import org.junit.Test;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
@@ -25,16 +27,14 @@ import java.io.InputStream;
import java.math.BigInteger;
import java.util.List;
import org.junit.Test;
import com.google.bitcoin.core.AbstractBlockChain.NewBlockType;
import com.google.bitcoin.core.Wallet.BalanceType;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
/**
* Test that an example production coinbase transactions can be added to a wallet ok.
*/
public class CoinbaseBlockTest {
static final NetworkParameters params = NetworkParameters.prodNet();
static final NetworkParameters params = MainNetParams.get();
// The address for this private key is 1GqtGtn4fctXuKxsVzRPSLmYWN1YioLi9y.
private static final String MINING_PRIVATE_KEY = "5JDxPrBRghF1EvSBjDigywqfmAjpHPmTJxYtQTYJxJRHLLQA4mG";

View File

@@ -16,17 +16,13 @@
package com.google.bitcoin.core;
import static com.google.bitcoin.core.Utils.reverseBytes;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.math.BigInteger;
import java.security.SecureRandom;
import java.security.SignatureException;
import java.util.Arrays;
import com.google.bitcoin.crypto.EncryptedPrivateKey;
import com.google.bitcoin.crypto.KeyCrypter;
import com.google.bitcoin.crypto.KeyCrypterScrypt;
import com.google.bitcoin.params.MainNetParams;
import com.google.bitcoin.params.TestNet3Params;
import com.google.bitcoin.utils.BriefLogFormatter;
import com.google.protobuf.ByteString;
import org.bitcoinj.wallet.Protos;
import org.bitcoinj.wallet.Protos.ScryptParameters;
import org.junit.Before;
@@ -36,11 +32,13 @@ import org.slf4j.LoggerFactory;
import org.spongycastle.crypto.params.KeyParameter;
import org.spongycastle.util.encoders.Hex;
import com.google.bitcoin.crypto.EncryptedPrivateKey;
import com.google.bitcoin.crypto.KeyCrypter;
import com.google.bitcoin.crypto.KeyCrypterScrypt;
import com.google.bitcoin.utils.BriefLogFormatter;
import com.google.protobuf.ByteString;
import java.math.BigInteger;
import java.security.SecureRandom;
import java.security.SignatureException;
import java.util.Arrays;
import static com.google.bitcoin.core.Utils.reverseBytes;
import static org.junit.Assert.*;
public class ECKeyTest {
public Logger log = LoggerFactory.getLogger(ECKeyTest.class.getName());
@@ -143,16 +141,16 @@ public class ECKeyTest {
public void base58Encoding() throws Exception {
String addr = "mqAJmaxMcG5pPHHc3H3NtyXzY7kGbJLuMF";
String privkey = "92shANodC6Y4evT5kFzjNFQAdjqTtHAnDTLzqBBq4BbKUPyx6CD";
ECKey key = new DumpedPrivateKey(NetworkParameters.testNet(), privkey).getKey();
assertEquals(privkey, key.getPrivateKeyEncoded(NetworkParameters.testNet()).toString());
assertEquals(addr, key.toAddress(NetworkParameters.testNet()).toString());
ECKey key = new DumpedPrivateKey(TestNet3Params.get(), privkey).getKey();
assertEquals(privkey, key.getPrivateKeyEncoded(TestNet3Params.get()).toString());
assertEquals(addr, key.toAddress(TestNet3Params.get()).toString());
}
@Test
public void base58Encoding_leadingZero() throws Exception {
String privkey = "91axuYLa8xK796DnBXXsMbjuc8pDYxYgJyQMvFzrZ6UfXaGYuqL";
ECKey key = new DumpedPrivateKey(NetworkParameters.testNet(), privkey).getKey();
assertEquals(privkey, key.getPrivateKeyEncoded(NetworkParameters.testNet()).toString());
ECKey key = new DumpedPrivateKey(TestNet3Params.get(), privkey).getKey();
assertEquals(privkey, key.getPrivateKeyEncoded(TestNet3Params.get()).toString());
assertEquals(0, key.getPrivKeyBytes()[0]);
}
@@ -161,8 +159,8 @@ public class ECKeyTest {
// Replace the loop bound with 1000 to get some keys with leading zero byte
for (int i = 0 ; i < 20 ; i++) {
ECKey key = new ECKey();
ECKey key1 = new DumpedPrivateKey(NetworkParameters.testNet(),
key.getPrivateKeyEncoded(NetworkParameters.testNet()).toString()).getKey();
ECKey key1 = new DumpedPrivateKey(TestNet3Params.get(),
key.getPrivateKeyEncoded(TestNet3Params.get()).toString()).getKey();
assertEquals(Utils.bytesToHexString(key.getPrivKeyBytes()),
Utils.bytesToHexString(key1.getPrivKeyBytes()));
}
@@ -173,7 +171,7 @@ public class ECKeyTest {
ECKey key = new ECKey();
String message = "Hello World!";
String signatureBase64 = key.signMessage(message);
log.info("Message signed with " + key.toAddress(NetworkParameters.prodNet()) + ": " + signatureBase64);
log.info("Message signed with " + key.toAddress(MainNetParams.get()) + ": " + signatureBase64);
// Should verify correctly.
key.verifyMessage(message, signatureBase64);
try {
@@ -189,9 +187,9 @@ public class ECKeyTest {
// Test vector generated by Bitcoin-Qt.
String message = "hello";
String sigBase64 = "HxNZdo6ggZ41hd3mM3gfJRqOQPZYcO8z8qdX2BwmpbF11CaOQV+QiZGGQxaYOncKoNW61oRuSMMF8udfK54XqI8=";
Address expectedAddress = new Address(NetworkParameters.prodNet(), "14YPSNPi6NSXnUxtPAsyJSuw3pv7AU3Cag");
Address expectedAddress = new Address(MainNetParams.get(), "14YPSNPi6NSXnUxtPAsyJSuw3pv7AU3Cag");
ECKey key = ECKey.signedMessageToKey(message, sigBase64);
Address gotAddress = key.toAddress(NetworkParameters.prodNet());
Address gotAddress = key.toAddress(MainNetParams.get());
assertEquals(expectedAddress, gotAddress);
}

View File

@@ -16,6 +16,7 @@
package com.google.bitcoin.core;
import com.google.bitcoin.params.MainNetParams;
import com.google.bitcoin.store.BlockStore;
import com.google.bitcoin.store.MemoryBlockStore;
import org.junit.Before;
@@ -133,28 +134,28 @@ public class LazyParseByteCacheTest {
@Test
public void testTransactionsLazyRetain() throws Exception {
testTransaction(NetworkParameters.prodNet(), txMessage, false, true, true);
testTransaction(MainNetParams.get(), txMessage, false, true, true);
testTransaction(unitTestParams, tx1BytesWithHeader, false, true, true);
testTransaction(unitTestParams, tx2BytesWithHeader, false, true, true);
}
@Test
public void testTransactionsLazyNoRetain() throws Exception {
testTransaction(NetworkParameters.prodNet(), txMessage, false, true, false);
testTransaction(MainNetParams.get(), txMessage, false, true, false);
testTransaction(unitTestParams, tx1BytesWithHeader, false, true, false);
testTransaction(unitTestParams, tx2BytesWithHeader, false, true, false);
}
@Test
public void testTransactionsNoLazyNoRetain() throws Exception {
testTransaction(NetworkParameters.prodNet(), txMessage, false, false, false);
testTransaction(MainNetParams.get(), txMessage, false, false, false);
testTransaction(unitTestParams, tx1BytesWithHeader, false, false, false);
testTransaction(unitTestParams, tx2BytesWithHeader, false, false, false);
}
@Test
public void testTransactionsNoLazyRetain() throws Exception {
testTransaction(NetworkParameters.prodNet(), txMessage, false, false, true);
testTransaction(MainNetParams.get(), txMessage, false, false, true);
testTransaction(unitTestParams, tx1BytesWithHeader, false, false, true);
testTransaction(unitTestParams, tx2BytesWithHeader, false, false, true);
}

View File

@@ -16,6 +16,7 @@
package com.google.bitcoin.core;
import com.google.bitcoin.params.MainNetParams;
import org.junit.Test;
import org.spongycastle.util.encoders.Hex;
@@ -29,8 +30,8 @@ public class PeerAddressTest
public void testPeerAddressRoundtrip() throws Exception {
// copied verbatim from https://en.bitcoin.it/wiki/Protocol_specification#Network_address
String fromSpec = "010000000000000000000000000000000000ffff0a000001208d";
PeerAddress pa = new PeerAddress(NetworkParameters.prodNet(),
Hex.decode(fromSpec),0,0);
PeerAddress pa = new PeerAddress(MainNetParams.get(),
Hex.decode(fromSpec), 0, 0);
String reserialized = Utils.bytesToHexString(pa.bitcoinSerialize());
assertEquals(reserialized,fromSpec );
}

View File

@@ -15,7 +15,7 @@
*/
package com.google.bitcoin.discovery;
import com.google.bitcoin.core.NetworkParameters;
import com.google.bitcoin.params.MainNetParams;
import org.junit.Test;
import java.net.InetSocketAddress;
@@ -28,13 +28,13 @@ import static org.junit.Assert.assertThat;
public class SeedPeersTest {
@Test
public void getPeer_one() throws Exception{
SeedPeers seedPeers = new SeedPeers(NetworkParameters.prodNet());
SeedPeers seedPeers = new SeedPeers(MainNetParams.get());
assertThat(seedPeers.getPeer(), notNullValue());
}
@Test
public void getPeer_all() throws Exception{
SeedPeers seedPeers = new SeedPeers(NetworkParameters.prodNet());
SeedPeers seedPeers = new SeedPeers(MainNetParams.get());
for(int i = 0; i < SeedPeers.seedAddrs.length; ++i){
assertThat("Failed on index: "+i, seedPeers.getPeer(), notNullValue());
}
@@ -43,7 +43,7 @@ public class SeedPeersTest {
@Test
public void getPeers_length() throws Exception{
SeedPeers seedPeers = new SeedPeers(NetworkParameters.prodNet());
SeedPeers seedPeers = new SeedPeers(MainNetParams.get());
InetSocketAddress[] addresses = seedPeers.getPeers(0, TimeUnit.SECONDS);
assertThat(addresses.length, equalTo(SeedPeers.seedAddrs.length));
}

View File

@@ -17,6 +17,7 @@
package com.google.bitcoin.script;
import com.google.bitcoin.core.*;
import com.google.bitcoin.params.TestNet3Params;
import com.google.common.collect.Lists;
import org.junit.Test;
import org.spongycastle.util.encoders.Hex;
@@ -41,7 +42,7 @@ public class ScriptTest {
static final String pubkeyProg = "76a91433e81a941e64cda12c6a299ed322ddbdd03f8d0e88ac";
static final NetworkParameters params = NetworkParameters.testNet();
static final NetworkParameters params = TestNet3Params.get();
@Test
public void testScriptSig() throws Exception {
@@ -131,8 +132,8 @@ public class ScriptTest {
public void dataDrivenValidScripts() throws Exception {
BufferedReader in = new BufferedReader(new InputStreamReader(
getClass().getResourceAsStream("script_valid.json"), Charset.forName("UTF-8")));
NetworkParameters params = NetworkParameters.testNet();
NetworkParameters params = TestNet3Params.get();
// Poor man's JSON parser (because pulling in a lib for this is overkill)
String script = "";
@@ -168,8 +169,8 @@ public class ScriptTest {
public void dataDrivenInvalidScripts() throws Exception {
BufferedReader in = new BufferedReader(new InputStreamReader(
getClass().getResourceAsStream("script_invalid.json"), Charset.forName("UTF-8")));
NetworkParameters params = NetworkParameters.testNet();
NetworkParameters params = TestNet3Params.get();
// Poor man's JSON parser (because pulling in a lib for this is overkill)
String script = "";
@@ -290,8 +291,8 @@ public class ScriptTest {
public void dataDrivenValidTransactions() throws Exception {
BufferedReader in = new BufferedReader(new InputStreamReader(
getClass().getResourceAsStream("tx_valid.json"), Charset.forName("UTF-8")));
NetworkParameters params = NetworkParameters.testNet();
NetworkParameters params = TestNet3Params.get();
// Poor man's (aka. really, really poor) JSON parser (because pulling in a lib for this is probably not overkill)
List<JSONObject> tx = new ArrayList<JSONObject>(3);
@@ -340,8 +341,8 @@ public class ScriptTest {
public void dataDrivenInvalidTransactions() throws Exception {
BufferedReader in = new BufferedReader(new InputStreamReader(
getClass().getResourceAsStream("tx_invalid.json"), Charset.forName("UTF-8")));
NetworkParameters params = NetworkParameters.testNet();
NetworkParameters params = TestNet3Params.get();
// Poor man's (aka. really, really poor) JSON parser (because pulling in a lib for this is probably overkill)
List<JSONObject> tx = new ArrayList<JSONObject>(1);

View File

@@ -3,6 +3,7 @@ package com.google.bitcoin.store;
import com.google.bitcoin.core.*;
import com.google.bitcoin.core.TransactionConfidence.ConfidenceType;
import com.google.bitcoin.params.MainNetParams;
import com.google.bitcoin.utils.BriefLogFormatter;
import com.google.protobuf.ByteString;
import org.bitcoinj.wallet.Protos;
@@ -142,7 +143,7 @@ public class WalletProtobufSerializerTest {
assertEquals(1, wallet1.getLastBlockSeenHeight());
// Test the Satoshi genesis block (hash of all zeroes) is roundtripped ok.
Block genesisBlock = NetworkParameters.prodNet().getGenesisBlock();
Block genesisBlock = MainNetParams.get().getGenesisBlock();
wallet.setLastBlockSeenHash(genesisBlock.getHash());
Wallet wallet2 = roundTrip(wallet);
assertEquals(genesisBlock.getHash(), wallet2.getLastBlockSeenHash());

View File

@@ -20,8 +20,9 @@ package com.google.bitcoin.uri;
import com.google.bitcoin.core.Address;
import com.google.bitcoin.core.AddressFormatException;
import com.google.bitcoin.core.NetworkParameters;
import com.google.bitcoin.core.Utils;
import com.google.bitcoin.params.MainNetParams;
import com.google.bitcoin.params.TestNet3Params;
import org.junit.Test;
import java.io.UnsupportedEncodingException;
@@ -33,7 +34,7 @@ public class BitcoinURITest {
private BitcoinURI testObject = null;
private static final String PRODNET_GOOD_ADDRESS = "1KzTSfqjF2iKCduwz59nv2uqh1W2JsTxZH";
private static final String MAINNET_GOOD_ADDRESS = "1KzTSfqjF2iKCduwz59nv2uqh1W2JsTxZH";
/**
* Tests conversion to Bitcoin URI
@@ -44,13 +45,13 @@ public class BitcoinURITest {
*/
@Test
public void testConvertToBitcoinURI() throws Exception {
Address goodAddress = new Address(NetworkParameters.prodNet(), PRODNET_GOOD_ADDRESS);
Address goodAddress = new Address(MainNetParams.get(), MAINNET_GOOD_ADDRESS);
// simple example
assertEquals("bitcoin:" + PRODNET_GOOD_ADDRESS + "?amount=12.34&label=Hello&message=AMessage", BitcoinURI.convertToBitcoinURI(goodAddress, Utils.toNanoCoins("12.34"), "Hello", "AMessage"));
assertEquals("bitcoin:" + MAINNET_GOOD_ADDRESS + "?amount=12.34&label=Hello&message=AMessage", BitcoinURI.convertToBitcoinURI(goodAddress, Utils.toNanoCoins("12.34"), "Hello", "AMessage"));
// example with spaces, ampersand and plus
assertEquals("bitcoin:" + PRODNET_GOOD_ADDRESS + "?amount=12.34&label=Hello%20World&message=Mess%20%26%20age%20%2B%20hope", BitcoinURI.convertToBitcoinURI(goodAddress, Utils.toNanoCoins("12.34"), "Hello World", "Mess & age + hope"));
assertEquals("bitcoin:" + MAINNET_GOOD_ADDRESS + "?amount=12.34&label=Hello%20World&message=Mess%20%26%20age%20%2B%20hope", BitcoinURI.convertToBitcoinURI(goodAddress, Utils.toNanoCoins("12.34"), "Hello World", "Mess & age + hope"));
// amount negative
try {
@@ -61,30 +62,30 @@ public class BitcoinURITest {
}
// no amount, label present, message present
assertEquals("bitcoin:" + PRODNET_GOOD_ADDRESS + "?label=Hello&message=glory", BitcoinURI.convertToBitcoinURI(goodAddress, null, "Hello", "glory"));
assertEquals("bitcoin:" + MAINNET_GOOD_ADDRESS + "?label=Hello&message=glory", BitcoinURI.convertToBitcoinURI(goodAddress, null, "Hello", "glory"));
// amount present, no label, message present
assertEquals("bitcoin:" + PRODNET_GOOD_ADDRESS + "?amount=0.1&message=glory", BitcoinURI.convertToBitcoinURI(goodAddress, Utils.toNanoCoins("0.1"), null, "glory"));
assertEquals("bitcoin:" + PRODNET_GOOD_ADDRESS + "?amount=0.1&message=glory", BitcoinURI.convertToBitcoinURI(goodAddress, Utils.toNanoCoins("0.1"), "", "glory"));
assertEquals("bitcoin:" + MAINNET_GOOD_ADDRESS + "?amount=0.1&message=glory", BitcoinURI.convertToBitcoinURI(goodAddress, Utils.toNanoCoins("0.1"), null, "glory"));
assertEquals("bitcoin:" + MAINNET_GOOD_ADDRESS + "?amount=0.1&message=glory", BitcoinURI.convertToBitcoinURI(goodAddress, Utils.toNanoCoins("0.1"), "", "glory"));
// amount present, label present, no message
assertEquals("bitcoin:" + PRODNET_GOOD_ADDRESS + "?amount=12.34&label=Hello", BitcoinURI.convertToBitcoinURI(goodAddress,Utils.toNanoCoins("12.34"), "Hello", null));
assertEquals("bitcoin:" + PRODNET_GOOD_ADDRESS + "?amount=12.34&label=Hello", BitcoinURI.convertToBitcoinURI(goodAddress, Utils.toNanoCoins("12.34"), "Hello", ""));
assertEquals("bitcoin:" + MAINNET_GOOD_ADDRESS + "?amount=12.34&label=Hello", BitcoinURI.convertToBitcoinURI(goodAddress,Utils.toNanoCoins("12.34"), "Hello", null));
assertEquals("bitcoin:" + MAINNET_GOOD_ADDRESS + "?amount=12.34&label=Hello", BitcoinURI.convertToBitcoinURI(goodAddress, Utils.toNanoCoins("12.34"), "Hello", ""));
// amount present, no label, no message
assertEquals("bitcoin:" + PRODNET_GOOD_ADDRESS + "?amount=1000", BitcoinURI.convertToBitcoinURI(goodAddress, Utils.toNanoCoins("1000"), null, null));
assertEquals("bitcoin:" + PRODNET_GOOD_ADDRESS + "?amount=1000", BitcoinURI.convertToBitcoinURI(goodAddress, Utils.toNanoCoins("1000"), "", ""));
assertEquals("bitcoin:" + MAINNET_GOOD_ADDRESS + "?amount=1000", BitcoinURI.convertToBitcoinURI(goodAddress, Utils.toNanoCoins("1000"), null, null));
assertEquals("bitcoin:" + MAINNET_GOOD_ADDRESS + "?amount=1000", BitcoinURI.convertToBitcoinURI(goodAddress, Utils.toNanoCoins("1000"), "", ""));
// no amount, label present, no message
assertEquals("bitcoin:" + PRODNET_GOOD_ADDRESS + "?label=Hello", BitcoinURI.convertToBitcoinURI(goodAddress, null, "Hello", null));
assertEquals("bitcoin:" + MAINNET_GOOD_ADDRESS + "?label=Hello", BitcoinURI.convertToBitcoinURI(goodAddress, null, "Hello", null));
// no amount, no label, message present
assertEquals("bitcoin:" + PRODNET_GOOD_ADDRESS + "?message=Agatha", BitcoinURI.convertToBitcoinURI(goodAddress, null, null, "Agatha"));
assertEquals("bitcoin:" + PRODNET_GOOD_ADDRESS + "?message=Agatha", BitcoinURI.convertToBitcoinURI(goodAddress, null, "", "Agatha"));
assertEquals("bitcoin:" + MAINNET_GOOD_ADDRESS + "?message=Agatha", BitcoinURI.convertToBitcoinURI(goodAddress, null, null, "Agatha"));
assertEquals("bitcoin:" + MAINNET_GOOD_ADDRESS + "?message=Agatha", BitcoinURI.convertToBitcoinURI(goodAddress, null, "", "Agatha"));
// no amount, no label, no message
assertEquals("bitcoin:" + PRODNET_GOOD_ADDRESS, BitcoinURI.convertToBitcoinURI(goodAddress, null, null, null));
assertEquals("bitcoin:" + PRODNET_GOOD_ADDRESS, BitcoinURI.convertToBitcoinURI(goodAddress, null, "", ""));
assertEquals("bitcoin:" + MAINNET_GOOD_ADDRESS, BitcoinURI.convertToBitcoinURI(goodAddress, null, null, null));
assertEquals("bitcoin:" + MAINNET_GOOD_ADDRESS, BitcoinURI.convertToBitcoinURI(goodAddress, null, "", ""));
}
/**
@@ -95,7 +96,7 @@ public class BitcoinURITest {
*/
@Test
public void testGood_Simple() throws BitcoinURIParseException {
testObject = new BitcoinURI(NetworkParameters.prodNet(), BitcoinURI.BITCOIN_SCHEME + ":" + PRODNET_GOOD_ADDRESS);
testObject = new BitcoinURI(MainNetParams.get(), BitcoinURI.BITCOIN_SCHEME + ":" + MAINNET_GOOD_ADDRESS);
assertNotNull(testObject);
assertNull("Unexpected amount", testObject.getAmount());
assertNull("Unexpected label", testObject.getLabel());
@@ -108,7 +109,7 @@ public class BitcoinURITest {
@Test
public void testBad_Scheme() {
try {
testObject = new BitcoinURI(NetworkParameters.prodNet(), "blimpcoin:" + PRODNET_GOOD_ADDRESS);
testObject = new BitcoinURI(MainNetParams.get(), "blimpcoin:" + MAINNET_GOOD_ADDRESS);
fail("Expecting BitcoinURIParseException");
} catch (BitcoinURIParseException e) {
}
@@ -121,14 +122,14 @@ public class BitcoinURITest {
public void testBad_BadSyntax() {
// Various illegal characters
try {
testObject = new BitcoinURI(NetworkParameters.prodNet(), BitcoinURI.BITCOIN_SCHEME + "|" + PRODNET_GOOD_ADDRESS);
testObject = new BitcoinURI(MainNetParams.get(), BitcoinURI.BITCOIN_SCHEME + "|" + MAINNET_GOOD_ADDRESS);
fail("Expecting BitcoinURIParseException");
} catch (BitcoinURIParseException e) {
assertTrue(e.getMessage().contains("Bad URI syntax"));
}
try {
testObject = new BitcoinURI(NetworkParameters.prodNet(), BitcoinURI.BITCOIN_SCHEME + ":" + PRODNET_GOOD_ADDRESS + "\\");
testObject = new BitcoinURI(MainNetParams.get(), BitcoinURI.BITCOIN_SCHEME + ":" + MAINNET_GOOD_ADDRESS + "\\");
fail("Expecting BitcoinURIParseException");
} catch (BitcoinURIParseException e) {
assertTrue(e.getMessage().contains("Bad URI syntax"));
@@ -136,7 +137,7 @@ public class BitcoinURITest {
// Separator without field
try {
testObject = new BitcoinURI(NetworkParameters.prodNet(), BitcoinURI.BITCOIN_SCHEME + ":");
testObject = new BitcoinURI(MainNetParams.get(), BitcoinURI.BITCOIN_SCHEME + ":");
fail("Expecting BitcoinURIParseException");
} catch (BitcoinURIParseException e) {
assertTrue(e.getMessage().contains("Bad URI syntax"));
@@ -149,7 +150,7 @@ public class BitcoinURITest {
@Test
public void testBad_Address() {
try {
testObject = new BitcoinURI(NetworkParameters.prodNet(), BitcoinURI.BITCOIN_SCHEME);
testObject = new BitcoinURI(MainNetParams.get(), BitcoinURI.BITCOIN_SCHEME);
fail("Expecting BitcoinURIParseException");
} catch (BitcoinURIParseException e) {
}
@@ -161,7 +162,7 @@ public class BitcoinURITest {
@Test
public void testBad_IncorrectAddressType() {
try {
testObject = new BitcoinURI(NetworkParameters.testNet(), BitcoinURI.BITCOIN_SCHEME + ":" + PRODNET_GOOD_ADDRESS);
testObject = new BitcoinURI(TestNet3Params.get(), BitcoinURI.BITCOIN_SCHEME + ":" + MAINNET_GOOD_ADDRESS);
fail("Expecting BitcoinURIParseException");
} catch (BitcoinURIParseException e) {
assertTrue(e.getMessage().contains("Bad address"));
@@ -177,17 +178,17 @@ public class BitcoinURITest {
@Test
public void testGood_Amount() throws BitcoinURIParseException {
// Test the decimal parsing
testObject = new BitcoinURI(NetworkParameters.prodNet(), BitcoinURI.BITCOIN_SCHEME + ":" + PRODNET_GOOD_ADDRESS
testObject = new BitcoinURI(MainNetParams.get(), BitcoinURI.BITCOIN_SCHEME + ":" + MAINNET_GOOD_ADDRESS
+ "?amount=9876543210.12345678");
assertEquals("987654321012345678", testObject.getAmount().toString());
// Test the decimal parsing
testObject = new BitcoinURI(NetworkParameters.prodNet(), BitcoinURI.BITCOIN_SCHEME + ":" + PRODNET_GOOD_ADDRESS
testObject = new BitcoinURI(MainNetParams.get(), BitcoinURI.BITCOIN_SCHEME + ":" + MAINNET_GOOD_ADDRESS
+ "?amount=.12345678");
assertEquals("12345678", testObject.getAmount().toString());
// Test the integer parsing
testObject = new BitcoinURI(NetworkParameters.prodNet(), BitcoinURI.BITCOIN_SCHEME + ":" + PRODNET_GOOD_ADDRESS
testObject = new BitcoinURI(MainNetParams.get(), BitcoinURI.BITCOIN_SCHEME + ":" + MAINNET_GOOD_ADDRESS
+ "?amount=9876543210");
assertEquals("987654321000000000", testObject.getAmount().toString());
}
@@ -200,7 +201,7 @@ public class BitcoinURITest {
*/
@Test
public void testGood_Label() throws BitcoinURIParseException {
testObject = new BitcoinURI(NetworkParameters.prodNet(), BitcoinURI.BITCOIN_SCHEME + ":" + PRODNET_GOOD_ADDRESS
testObject = new BitcoinURI(MainNetParams.get(), BitcoinURI.BITCOIN_SCHEME + ":" + MAINNET_GOOD_ADDRESS
+ "?label=Hello%20World");
assertEquals("Hello World", testObject.getLabel());
}
@@ -216,7 +217,7 @@ public class BitcoinURITest {
public void testGood_LabelWithAmpersandAndPlus() throws BitcoinURIParseException, UnsupportedEncodingException {
String testString = "Hello Earth & Mars + Venus";
String encodedLabel = BitcoinURI.encodeURLString(testString);
testObject = new BitcoinURI(NetworkParameters.prodNet(), BitcoinURI.BITCOIN_SCHEME + ":" + PRODNET_GOOD_ADDRESS + "?label="
testObject = new BitcoinURI(MainNetParams.get(), BitcoinURI.BITCOIN_SCHEME + ":" + MAINNET_GOOD_ADDRESS + "?label="
+ encodedLabel);
assertEquals(testString, testObject.getLabel());
}
@@ -233,7 +234,7 @@ public class BitcoinURITest {
// Moscow in Russian in Cyrillic
String moscowString = "\u041c\u043e\u0441\u043a\u0432\u0430";
String encodedLabel = BitcoinURI.encodeURLString(moscowString);
testObject = new BitcoinURI(NetworkParameters.prodNet(), BitcoinURI.BITCOIN_SCHEME + ":" + PRODNET_GOOD_ADDRESS + "?label="
testObject = new BitcoinURI(MainNetParams.get(), BitcoinURI.BITCOIN_SCHEME + ":" + MAINNET_GOOD_ADDRESS + "?label="
+ encodedLabel);
assertEquals(moscowString, testObject.getLabel());
}
@@ -246,7 +247,7 @@ public class BitcoinURITest {
*/
@Test
public void testGood_Message() throws BitcoinURIParseException {
testObject = new BitcoinURI(NetworkParameters.prodNet(), BitcoinURI.BITCOIN_SCHEME + ":" + PRODNET_GOOD_ADDRESS
testObject = new BitcoinURI(MainNetParams.get(), BitcoinURI.BITCOIN_SCHEME + ":" + MAINNET_GOOD_ADDRESS
+ "?message=Hello%20World");
assertEquals("Hello World", testObject.getMessage());
}
@@ -259,7 +260,7 @@ public class BitcoinURITest {
*/
@Test
public void testGood_Combinations() throws BitcoinURIParseException {
testObject = new BitcoinURI(NetworkParameters.prodNet(), BitcoinURI.BITCOIN_SCHEME + ":" + PRODNET_GOOD_ADDRESS
testObject = new BitcoinURI(MainNetParams.get(), BitcoinURI.BITCOIN_SCHEME + ":" + MAINNET_GOOD_ADDRESS
+ "?amount=9876543210&label=Hello%20World&message=Be%20well");
assertEquals(
"BitcoinURI['address'='1KzTSfqjF2iKCduwz59nv2uqh1W2JsTxZH','amount'='987654321000000000','label'='Hello World','message'='Be well']",
@@ -276,7 +277,7 @@ public class BitcoinURITest {
public void testBad_Amount() throws BitcoinURIParseException {
// Missing
try {
testObject = new BitcoinURI(NetworkParameters.prodNet(), BitcoinURI.BITCOIN_SCHEME + ":" + PRODNET_GOOD_ADDRESS
testObject = new BitcoinURI(MainNetParams.get(), BitcoinURI.BITCOIN_SCHEME + ":" + MAINNET_GOOD_ADDRESS
+ "?amount=");
fail("Expecting BitcoinURIParseException");
} catch (BitcoinURIParseException e) {
@@ -285,7 +286,7 @@ public class BitcoinURITest {
// Non-decimal (BIP 21)
try {
testObject = new BitcoinURI(NetworkParameters.prodNet(), BitcoinURI.BITCOIN_SCHEME + ":" + PRODNET_GOOD_ADDRESS
testObject = new BitcoinURI(MainNetParams.get(), BitcoinURI.BITCOIN_SCHEME + ":" + MAINNET_GOOD_ADDRESS
+ "?amount=12X4");
fail("Expecting BitcoinURIParseException");
} catch (BitcoinURIParseException e) {
@@ -302,7 +303,7 @@ public class BitcoinURITest {
@Test
public void testBad_Label() throws BitcoinURIParseException {
try {
testObject = new BitcoinURI(NetworkParameters.prodNet(), BitcoinURI.BITCOIN_SCHEME + ":" + PRODNET_GOOD_ADDRESS
testObject = new BitcoinURI(MainNetParams.get(), BitcoinURI.BITCOIN_SCHEME + ":" + MAINNET_GOOD_ADDRESS
+ "?label=");
fail("Expecting BitcoinURIParseException");
} catch (BitcoinURIParseException e) {
@@ -319,7 +320,7 @@ public class BitcoinURITest {
@Test
public void testBad_Message() throws BitcoinURIParseException {
try {
testObject = new BitcoinURI(NetworkParameters.prodNet(), BitcoinURI.BITCOIN_SCHEME + ":" + PRODNET_GOOD_ADDRESS
testObject = new BitcoinURI(MainNetParams.get(), BitcoinURI.BITCOIN_SCHEME + ":" + MAINNET_GOOD_ADDRESS
+ "?message=");
fail("Expecting BitcoinURIParseException");
} catch (BitcoinURIParseException e) {
@@ -336,7 +337,7 @@ public class BitcoinURITest {
@Test
public void testBad_Duplicated() throws BitcoinURIParseException {
try {
testObject = new BitcoinURI(NetworkParameters.prodNet(), BitcoinURI.BITCOIN_SCHEME + ":" + PRODNET_GOOD_ADDRESS
testObject = new BitcoinURI(MainNetParams.get(), BitcoinURI.BITCOIN_SCHEME + ":" + MAINNET_GOOD_ADDRESS
+ "?address=aardvark");
fail("Expecting BitcoinURIParseException");
} catch (BitcoinURIParseException e) {
@@ -353,7 +354,7 @@ public class BitcoinURITest {
@Test
public void testBad_TooManyEquals() throws BitcoinURIParseException {
try {
testObject = new BitcoinURI(NetworkParameters.prodNet(), BitcoinURI.BITCOIN_SCHEME + ":" + PRODNET_GOOD_ADDRESS
testObject = new BitcoinURI(MainNetParams.get(), BitcoinURI.BITCOIN_SCHEME + ":" + MAINNET_GOOD_ADDRESS
+ "?label=aardvark=zebra");
fail("Expecting BitcoinURIParseException");
} catch (BitcoinURIParseException e) {
@@ -370,7 +371,7 @@ public class BitcoinURITest {
@Test
public void testBad_TooManyQuestionMarks() throws BitcoinURIParseException {
try {
testObject = new BitcoinURI(NetworkParameters.prodNet(), BitcoinURI.BITCOIN_SCHEME + ":" + PRODNET_GOOD_ADDRESS
testObject = new BitcoinURI(MainNetParams.get(), BitcoinURI.BITCOIN_SCHEME + ":" + MAINNET_GOOD_ADDRESS
+ "?label=aardvark?message=zebra");
fail("Expecting BitcoinURIParseException");
} catch (BitcoinURIParseException e) {
@@ -387,7 +388,7 @@ public class BitcoinURITest {
@Test
public void testUnknown() throws BitcoinURIParseException {
// Unknown not required field
testObject = new BitcoinURI(NetworkParameters.prodNet(), BitcoinURI.BITCOIN_SCHEME + ":" + PRODNET_GOOD_ADDRESS
testObject = new BitcoinURI(MainNetParams.get(), BitcoinURI.BITCOIN_SCHEME + ":" + MAINNET_GOOD_ADDRESS
+ "?aardvark=true");
assertEquals("BitcoinURI['address'='1KzTSfqjF2iKCduwz59nv2uqh1W2JsTxZH','aardvark'='true']", testObject.toString());
@@ -395,7 +396,7 @@ public class BitcoinURITest {
// Unknown not required field (isolated)
try {
testObject = new BitcoinURI(NetworkParameters.prodNet(), BitcoinURI.BITCOIN_SCHEME + ":" + PRODNET_GOOD_ADDRESS
testObject = new BitcoinURI(MainNetParams.get(), BitcoinURI.BITCOIN_SCHEME + ":" + MAINNET_GOOD_ADDRESS
+ "?aardvark");
fail("Expecting BitcoinURIParseException");
} catch (BitcoinURIParseException e) {
@@ -404,7 +405,7 @@ public class BitcoinURITest {
// Unknown and required field
try {
testObject = new BitcoinURI(NetworkParameters.prodNet(), BitcoinURI.BITCOIN_SCHEME + ":" + PRODNET_GOOD_ADDRESS
testObject = new BitcoinURI(MainNetParams.get(), BitcoinURI.BITCOIN_SCHEME + ":" + MAINNET_GOOD_ADDRESS
+ "?req-aardvark=true");
fail("Expecting BitcoinURIParseException");
} catch (BitcoinURIParseException e) {