Java Code Examples for org.bitcoinj.params.UnitTestParams#get()

The following examples show how to use org.bitcoinj.params.UnitTestParams#get() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: VersionMessageTest.java    From green_android with GNU General Public License v3.0 6 votes vote down vote up
@Test
// Test that we can decode version messages which miss data which some old nodes may not include
public void testDecode() throws Exception {
    NetworkParameters params = UnitTestParams.get();

    VersionMessage ver = new VersionMessage(params, HEX.decode("7111010000000000000000003334a85500000000000000000000000000000000000000000000ffff7f000001479d000000000000000000000000000000000000ffff7f000001479d00000000000000000f2f626974636f696e6a3a302e31332f0004000000"));
    assertFalse(ver.relayTxesBeforeFilter);
    assertEquals(1024, ver.bestHeight);
    assertEquals("/bitcoinj:0.13/", ver.subVer);

    ver = new VersionMessage(params, HEX.decode("711101000000000000000000a634a85500000000000000000000000000000000000000000000ffff7f000001479d000000000000000000000000000000000000ffff7f000001479d00000000000000000f2f626974636f696e6a3a302e31332f0004000001"));
    assertTrue(ver.relayTxesBeforeFilter);
    assertEquals(1024, ver.bestHeight);
    assertEquals("/bitcoinj:0.13/", ver.subVer);

    ver = new VersionMessage(params, HEX.decode("711101000000000000000000c334a85500000000000000000000000000000000000000000000ffff7f000001479d000000000000000000000000000000000000ffff7f000001479d00000000000000000f2f626974636f696e6a3a302e31332f0000000001"));
    assertTrue(ver.relayTxesBeforeFilter);
    assertEquals(0, ver.bestHeight);
    assertEquals("/bitcoinj:0.13/", ver.subVer);

    ver = new VersionMessage(params, HEX.decode("71110100000000000000000048e5e95000000000000000000000000000000000000000000000ffff7f000001479d000000000000000000000000000000000000ffff7f000001479d0000000000000000"));
    assertTrue(ver.relayTxesBeforeFilter);
    assertEquals(0, ver.bestHeight);
    assertEquals("", ver.subVer);
}
 
Example 2
Source File: TransactionInputTest.java    From green_android with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testStandardWalletDisconnect() throws Exception {
    NetworkParameters params = UnitTestParams.get();
    Wallet w = new Wallet(new Context(params));
    w.setCoinSelector(new AllowUnconfirmedCoinSelector());
    Address a = w.currentReceiveAddress();
    Transaction tx1 = FakeTxBuilder.createFakeTxWithoutChangeAddress(params, Coin.COIN, a);
    w.receivePending(tx1, null);
    Transaction tx2 = new Transaction(params);
    tx2.addOutput(Coin.valueOf(99000000), new ECKey());
    w.completeTx(SendRequest.forTx(tx2));

    TransactionInput txInToDisconnect = tx2.getInput(0);

    assertEquals(tx1, txInToDisconnect.getOutpoint().fromTx);
    assertNull(txInToDisconnect.getOutpoint().connectedOutput);

    txInToDisconnect.disconnect();

    assertNull(txInToDisconnect.getOutpoint().fromTx);
    assertNull(txInToDisconnect.getOutpoint().connectedOutput);
}
 
Example 3
Source File: VersionMessageTest.java    From GreenBits with GNU General Public License v3.0 6 votes vote down vote up
@Test
// Test that we can decode version messages which miss data which some old nodes may not include
public void testDecode() throws Exception {
    NetworkParameters params = UnitTestParams.get();

    VersionMessage ver = new VersionMessage(params, HEX.decode("7111010000000000000000003334a85500000000000000000000000000000000000000000000ffff7f000001479d000000000000000000000000000000000000ffff7f000001479d00000000000000000f2f626974636f696e6a3a302e31332f0004000000"));
    assertFalse(ver.relayTxesBeforeFilter);
    assertEquals(1024, ver.bestHeight);
    assertEquals("/bitcoinj:0.13/", ver.subVer);

    ver = new VersionMessage(params, HEX.decode("711101000000000000000000a634a85500000000000000000000000000000000000000000000ffff7f000001479d000000000000000000000000000000000000ffff7f000001479d00000000000000000f2f626974636f696e6a3a302e31332f0004000001"));
    assertTrue(ver.relayTxesBeforeFilter);
    assertEquals(1024, ver.bestHeight);
    assertEquals("/bitcoinj:0.13/", ver.subVer);

    ver = new VersionMessage(params, HEX.decode("711101000000000000000000c334a85500000000000000000000000000000000000000000000ffff7f000001479d000000000000000000000000000000000000ffff7f000001479d00000000000000000f2f626974636f696e6a3a302e31332f0000000001"));
    assertTrue(ver.relayTxesBeforeFilter);
    assertEquals(0, ver.bestHeight);
    assertEquals("/bitcoinj:0.13/", ver.subVer);

    ver = new VersionMessage(params, HEX.decode("71110100000000000000000048e5e95000000000000000000000000000000000000000000000ffff7f000001479d000000000000000000000000000000000000ffff7f000001479d0000000000000000"));
    assertTrue(ver.relayTxesBeforeFilter);
    assertEquals(0, ver.bestHeight);
    assertEquals("", ver.subVer);
}
 
Example 4
Source File: TransactionInputTest.java    From GreenBits with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testStandardWalletDisconnect() throws Exception {
    NetworkParameters params = UnitTestParams.get();
    Wallet w = new Wallet(new Context(params));
    w.setCoinSelector(new AllowUnconfirmedCoinSelector());
    Address a = w.currentReceiveAddress();
    Transaction tx1 = FakeTxBuilder.createFakeTxWithoutChangeAddress(params, Coin.COIN, a);
    w.receivePending(tx1, null);
    Transaction tx2 = new Transaction(params);
    tx2.addOutput(Coin.valueOf(99000000), new ECKey());
    w.completeTx(SendRequest.forTx(tx2));

    TransactionInput txInToDisconnect = tx2.getInput(0);

    assertEquals(tx1, txInToDisconnect.getOutpoint().fromTx);
    assertNull(txInToDisconnect.getOutpoint().connectedOutput);

    txInToDisconnect.disconnect();

    assertNull(txInToDisconnect.getOutpoint().fromTx);
    assertNull(txInToDisconnect.getOutpoint().connectedOutput);
}
 
Example 5
Source File: MessageTest.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
@Test(expected = ProtocolException.class)
public void readStrOfExtremeLength() throws Exception {
    NetworkParameters params = UnitTestParams.get();
    VarInt length = new VarInt(Integer.MAX_VALUE);
    byte[] payload = length.encode();
    new VarStrMessage(params, payload);
}
 
Example 6
Source File: MessageTest.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
@Test(expected = ProtocolException.class)
public void readByteArrayOfExtremeLength() throws Exception {
    NetworkParameters params = UnitTestParams.get();
    VarInt length = new VarInt(Integer.MAX_VALUE);
    byte[] payload = length.encode();
    new VarBytesMessage(params, payload);
}
 
Example 7
Source File: ECKeyTest.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void roundTripDumpedPrivKey() throws Exception {
    ECKey key = new ECKey();
    assertTrue(key.isCompressed());
    NetworkParameters params = UnitTestParams.get();
    String base58 = key.getPrivateKeyEncoded(params).toString();
    ECKey key2 = DumpedPrivateKey.fromBase58(params, base58).getKey();
    assertTrue(key2.isCompressed());
    assertTrue(Arrays.equals(key.getPrivKeyBytes(), key2.getPrivKeyBytes()));
    assertTrue(Arrays.equals(key.getPubKey(), key2.getPubKey()));
}
 
Example 8
Source File: BlockTest.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testUpdateLength() {
    NetworkParameters params = UnitTestParams.get();
    Block block = params.getGenesisBlock().createNextBlockWithCoinbase(Block.BLOCK_VERSION_GENESIS, new ECKey().getPubKey(), Block.BLOCK_HEIGHT_GENESIS);
    assertEquals(block.bitcoinSerialize().length, block.length);
    final int origBlockLen = block.length;
    Transaction tx = new Transaction(params);
    // this is broken until the transaction has > 1 input + output (which is required anyway...)
    //assertTrue(tx.length == tx.bitcoinSerialize().length && tx.length == 8);
    byte[] outputScript = new byte[10];
    Arrays.fill(outputScript, (byte) ScriptOpCodes.OP_FALSE);
    tx.addOutput(new TransactionOutput(params, null, Coin.SATOSHI, outputScript));
    tx.addInput(new TransactionInput(params, null, new byte[] {(byte) ScriptOpCodes.OP_FALSE},
            new TransactionOutPoint(params, 0, Sha256Hash.of(new byte[] { 1 }))));
    int origTxLength = 8 + 2 + 8 + 1 + 10 + 40 + 1 + 1;
    assertEquals(tx.unsafeBitcoinSerialize().length, tx.length);
    assertEquals(origTxLength, tx.length);
    block.addTransaction(tx);
    assertEquals(block.unsafeBitcoinSerialize().length, block.length);
    assertEquals(origBlockLen + tx.length, block.length);
    block.getTransactions().get(1).getInputs().get(0).setScriptBytes(new byte[] {(byte) ScriptOpCodes.OP_FALSE, (byte) ScriptOpCodes.OP_FALSE});
    assertEquals(block.length, origBlockLen + tx.length);
    assertEquals(tx.length, origTxLength + 1);
    block.getTransactions().get(1).getInputs().get(0).clearScriptBytes();
    assertEquals(block.length, block.unsafeBitcoinSerialize().length);
    assertEquals(block.length, origBlockLen + tx.length);
    assertEquals(tx.length, origTxLength - 1);
    block.getTransactions().get(1).addInput(new TransactionInput(params, null, new byte[] {(byte) ScriptOpCodes.OP_FALSE},
            new TransactionOutPoint(params, 0, Sha256Hash.of(new byte[] { 1 }))));
    assertEquals(block.length, origBlockLen + tx.length);
    assertEquals(tx.length, origTxLength + 41); // - 1 + 40 + 1 + 1
}
 
Example 9
Source File: TransactionInputTest.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testUTXOWalletDisconnect() throws Exception {
    final NetworkParameters params = UnitTestParams.get();
    Wallet w = new Wallet(new Context(params));
    Address a = w.currentReceiveAddress();
    final UTXO utxo = new UTXO(Sha256Hash.of(new byte[] { 1, 2, 3 }), 1, Coin.COIN, 0, false,
            ScriptBuilder.createOutputScript(a));
    w.setUTXOProvider(new UTXOProvider() {
        @Override
        public NetworkParameters getParams() {
            return params;
        }

        @Override
        public List<UTXO> getOpenTransactionOutputs(List<Address> addresses) throws UTXOProviderException {
            return Lists.newArrayList(utxo);
        }

        @Override
        public int getChainHeadHeight() throws UTXOProviderException {
            return Integer.MAX_VALUE;
        }
    });

    Transaction tx2 = new Transaction(params);
    tx2.addOutput(Coin.valueOf(99000000), new ECKey());
    w.completeTx(SendRequest.forTx(tx2));

    TransactionInput txInToDisconnect = tx2.getInput(0);

    assertNull(txInToDisconnect.getOutpoint().fromTx);
    assertEquals(utxo.getHash(), txInToDisconnect.getOutpoint().connectedOutput.getParentTransactionHash());

    txInToDisconnect.disconnect();

    assertNull(txInToDisconnect.getOutpoint().fromTx);
    assertNull(txInToDisconnect.getOutpoint().connectedOutput);
}
 
Example 10
Source File: SPVBlockStoreTest.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void basics() throws Exception {
    NetworkParameters params = UnitTestParams.get();
    File f = File.createTempFile("spvblockstore", null);
    f.delete();
    f.deleteOnExit();
    SPVBlockStore store = new SPVBlockStore(params, f);

    Address to = new ECKey().toAddress(params);
    // Check the first block in a new store is the genesis block.
    StoredBlock genesis = store.getChainHead();
    assertEquals(params.getGenesisBlock(), genesis.getHeader());
    assertEquals(0, genesis.getHeight());


    // Build a new block.
    StoredBlock b1 = genesis.build(genesis.getHeader().createNextBlock(to).cloneAsHeader());
    store.put(b1);
    store.setChainHead(b1);
    store.close();

    // Check we can get it back out again if we rebuild the store object.
    store = new SPVBlockStore(params, f);
    StoredBlock b2 = store.get(b1.getHeader().getHash());
    assertEquals(b1, b2);
    // Check the chain head was stored correctly also.
    StoredBlock chainHead = store.getChainHead();
    assertEquals(b1, chainHead);
}
 
Example 11
Source File: MessageTest.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
@Test(expected = ProtocolException.class)
public void readStrOfExtremeLength() throws Exception {
    NetworkParameters params = UnitTestParams.get();
    VarInt length = new VarInt(Integer.MAX_VALUE);
    byte[] payload = length.encode();
    new VarStrMessage(params, payload);
}
 
Example 12
Source File: MessageTest.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
@Test(expected = ProtocolException.class)
public void readByteArrayOfExtremeLength() throws Exception {
    NetworkParameters params = UnitTestParams.get();
    VarInt length = new VarInt(Integer.MAX_VALUE);
    byte[] payload = length.encode();
    new VarBytesMessage(params, payload);
}
 
Example 13
Source File: ECKeyTest.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void roundTripDumpedPrivKey() throws Exception {
    ECKey key = new ECKey();
    assertTrue(key.isCompressed());
    NetworkParameters params = UnitTestParams.get();
    String base58 = key.getPrivateKeyEncoded(params).toString();
    ECKey key2 = DumpedPrivateKey.fromBase58(params, base58).getKey();
    assertTrue(key2.isCompressed());
    assertTrue(Arrays.equals(key.getPrivKeyBytes(), key2.getPrivKeyBytes()));
    assertTrue(Arrays.equals(key.getPubKey(), key2.getPubKey()));
}
 
Example 14
Source File: BlockTest.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testUpdateLength() {
    NetworkParameters params = UnitTestParams.get();
    Block block = params.getGenesisBlock().createNextBlockWithCoinbase(Block.BLOCK_VERSION_GENESIS, new ECKey().getPubKey(), Block.BLOCK_HEIGHT_GENESIS);
    assertEquals(block.bitcoinSerialize().length, block.length);
    final int origBlockLen = block.length;
    Transaction tx = new Transaction(params);
    // this is broken until the transaction has > 1 input + output (which is required anyway...)
    //assertTrue(tx.length == tx.bitcoinSerialize().length && tx.length == 8);
    byte[] outputScript = new byte[10];
    Arrays.fill(outputScript, (byte) ScriptOpCodes.OP_FALSE);
    tx.addOutput(new TransactionOutput(params, null, Coin.SATOSHI, outputScript));
    tx.addInput(new TransactionInput(params, null, new byte[] {(byte) ScriptOpCodes.OP_FALSE},
            new TransactionOutPoint(params, 0, Sha256Hash.of(new byte[] { 1 }))));
    int origTxLength = 8 + 2 + 8 + 1 + 10 + 40 + 1 + 1;
    assertEquals(tx.unsafeBitcoinSerialize().length, tx.length);
    assertEquals(origTxLength, tx.length);
    block.addTransaction(tx);
    assertEquals(block.unsafeBitcoinSerialize().length, block.length);
    assertEquals(origBlockLen + tx.length, block.length);
    block.getTransactions().get(1).getInputs().get(0).setScriptBytes(new byte[] {(byte) ScriptOpCodes.OP_FALSE, (byte) ScriptOpCodes.OP_FALSE});
    assertEquals(block.length, origBlockLen + tx.length);
    assertEquals(tx.length, origTxLength + 1);
    block.getTransactions().get(1).getInputs().get(0).clearScriptBytes();
    assertEquals(block.length, block.unsafeBitcoinSerialize().length);
    assertEquals(block.length, origBlockLen + tx.length);
    assertEquals(tx.length, origTxLength - 1);
    block.getTransactions().get(1).addInput(new TransactionInput(params, null, new byte[] {(byte) ScriptOpCodes.OP_FALSE},
            new TransactionOutPoint(params, 0, Sha256Hash.of(new byte[] { 1 }))));
    assertEquals(block.length, origBlockLen + tx.length);
    assertEquals(tx.length, origTxLength + 41); // - 1 + 40 + 1 + 1
}
 
Example 15
Source File: TransactionInputTest.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testUTXOWalletDisconnect() throws Exception {
    final NetworkParameters params = UnitTestParams.get();
    Wallet w = new Wallet(new Context(params));
    Address a = w.currentReceiveAddress();
    final UTXO utxo = new UTXO(Sha256Hash.of(new byte[] { 1, 2, 3 }), 1, Coin.COIN, 0, false,
            ScriptBuilder.createOutputScript(a));
    w.setUTXOProvider(new UTXOProvider() {
        @Override
        public NetworkParameters getParams() {
            return params;
        }

        @Override
        public List<UTXO> getOpenTransactionOutputs(List<Address> addresses) throws UTXOProviderException {
            return Lists.newArrayList(utxo);
        }

        @Override
        public int getChainHeadHeight() throws UTXOProviderException {
            return Integer.MAX_VALUE;
        }
    });

    Transaction tx2 = new Transaction(params);
    tx2.addOutput(Coin.valueOf(99000000), new ECKey());
    w.completeTx(SendRequest.forTx(tx2));

    TransactionInput txInToDisconnect = tx2.getInput(0);

    assertNull(txInToDisconnect.getOutpoint().fromTx);
    assertEquals(utxo.getHash(), txInToDisconnect.getOutpoint().connectedOutput.getParentTransactionHash());

    txInToDisconnect.disconnect();

    assertNull(txInToDisconnect.getOutpoint().fromTx);
    assertNull(txInToDisconnect.getOutpoint().connectedOutput);
}
 
Example 16
Source File: SPVBlockStoreTest.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void basics() throws Exception {
    NetworkParameters params = UnitTestParams.get();
    File f = File.createTempFile("spvblockstore", null);
    f.delete();
    f.deleteOnExit();
    SPVBlockStore store = new SPVBlockStore(params, f);

    Address to = new ECKey().toAddress(params);
    // Check the first block in a new store is the genesis block.
    StoredBlock genesis = store.getChainHead();
    assertEquals(params.getGenesisBlock(), genesis.getHeader());
    assertEquals(0, genesis.getHeight());


    // Build a new block.
    StoredBlock b1 = genesis.build(genesis.getHeader().createNextBlock(to).cloneAsHeader());
    store.put(b1);
    store.setChainHead(b1);
    store.close();

    // Check we can get it back out again if we rebuild the store object.
    store = new SPVBlockStore(params, f);
    StoredBlock b2 = store.get(b1.getHeader().getHash());
    assertEquals(b1, b2);
    // Check the chain head was stored correctly also.
    StoredBlock chainHead = store.getChainHead();
    assertEquals(b1, chainHead);
}