Java Code Examples for org.bitcoinj.wallet.Wallet#freshReceiveKey()

The following examples show how to use org.bitcoinj.wallet.Wallet#freshReceiveKey() . 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: ChannelConnectionTest.java    From bcm-android with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testEmptyWallet() throws Exception {
    Wallet emptyWallet = new Wallet(UNITTEST);
    emptyWallet.freshReceiveKey();
    ChannelTestUtils.RecordingPair pair = ChannelTestUtils.makeRecorders(serverWallet, mockBroadcaster);
    PaymentChannelServer server = pair.server;
    PaymentChannelClient client = new PaymentChannelClient(emptyWallet, myKey, COIN, Sha256Hash.ZERO_HASH, null, clientChannelProperties, pair.clientRecorder);
    client.connectionOpen();
    server.connectionOpen();
    server.receiveMessage(pair.clientRecorder.checkNextMsg(MessageType.CLIENT_VERSION));
    client.receiveMessage(pair.serverRecorder.checkNextMsg(MessageType.SERVER_VERSION));
    try {
        client.receiveMessage(Protos.TwoWayChannelMessage.newBuilder()
                .setInitiate(Protos.Initiate.newBuilder().setExpireTimeSecs(Utils.currentTimeSeconds())
                        .setMinAcceptedChannelSize(CENT.value)
                        .setMultisigKey(ByteString.copyFrom(new ECKey().getPubKey()))
                        .setMinPayment(Transaction.REFERENCE_DEFAULT_MIN_TX_FEE.value))
                .setType(MessageType.INITIATE).build());
        fail();
    } catch (InsufficientMoneyException expected) {
        // This should be thrown.
    }
}
 
Example 2
Source File: ChannelConnectionTest.java    From GreenBits with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testEmptyWallet() throws Exception {
    Wallet emptyWallet = new Wallet(PARAMS);
    emptyWallet.freshReceiveKey();
    ChannelTestUtils.RecordingPair pair = ChannelTestUtils.makeRecorders(serverWallet, mockBroadcaster);
    PaymentChannelServer server = pair.server;
    PaymentChannelClient client = new PaymentChannelClient(emptyWallet, myKey, COIN, Sha256Hash.ZERO_HASH, null, clientChannelProperties, pair.clientRecorder);
    client.connectionOpen();
    server.connectionOpen();
    server.receiveMessage(pair.clientRecorder.checkNextMsg(MessageType.CLIENT_VERSION));
    client.receiveMessage(pair.serverRecorder.checkNextMsg(MessageType.SERVER_VERSION));
    try {
        client.receiveMessage(Protos.TwoWayChannelMessage.newBuilder()
                .setInitiate(Protos.Initiate.newBuilder().setExpireTimeSecs(Utils.currentTimeSeconds())
                        .setMinAcceptedChannelSize(CENT.value)
                        .setMultisigKey(ByteString.copyFrom(new ECKey().getPubKey()))
                        .setMinPayment(Transaction.REFERENCE_DEFAULT_MIN_TX_FEE.value))
                .setType(MessageType.INITIATE).build());
        fail();
    } catch (InsufficientMoneyException expected) {
        // This should be thrown.
    }
}
 
Example 3
Source File: BlockChainTest.java    From bcm-android with GNU General Public License v3.0 6 votes vote down vote up
@Before
public void setUp() throws Exception {
    BriefLogFormatter.initVerbose();
    Context.propagate(new Context(TESTNET, 100, Coin.ZERO, false));
    testNetChain = new BlockChain(TESTNET, new Wallet(TESTNET), new MemoryBlockStore(TESTNET));
    Context.propagate(new Context(UNITTEST, 100, Coin.ZERO, false));
    wallet = new Wallet(UNITTEST) {
        @Override
        public void receiveFromBlock(Transaction tx, StoredBlock block, BlockChain.NewBlockType blockType,
                                     int relativityOffset) throws VerificationException {
            super.receiveFromBlock(tx, block, blockType, relativityOffset);
            BlockChainTest.this.block[0] = block;
            if (isTransactionRelevant(tx) && tx.isCoinBase()) {
                BlockChainTest.this.coinbaseTransaction = tx;
            }
        }
    };
    wallet.freshReceiveKey();

    resetBlockStore();
    chain = new BlockChain(UNITTEST, wallet, blockStore);

    coinbaseTo = LegacyAddress.fromKey(UNITTEST, wallet.currentReceiveKey());
}
 
Example 4
Source File: WalletConfig.java    From bisq-core with GNU Affero General Public License v3.0 6 votes vote down vote up
private Wallet createOrLoadWallet(File walletFile, boolean shouldReplayWallet,
                                  BisqKeyChainGroup keyChainGroup, boolean isBsqWallet, DeterministicSeed restoreFromSeed)
        throws Exception {

    if (restoreFromSeed != null)
        maybeMoveOldWalletOutOfTheWay(walletFile);

    Wallet wallet;
    if (walletFile.exists()) {
        wallet = loadWallet(walletFile, shouldReplayWallet, keyChainGroup.isUseBitcoinDeterministicKeyChain());
    } else {
        wallet = createWallet(keyChainGroup, isBsqWallet);
        wallet.freshReceiveKey();
        wallet.saveToFile(walletFile);
    }

    if (useAutoSave) wallet.autosaveToFile(walletFile, 5, TimeUnit.SECONDS, null);

    return wallet;
}
 
Example 5
Source File: BlockChainTest.java    From green_android with GNU General Public License v3.0 6 votes vote down vote up
@Before
public void setUp() throws Exception {
    BriefLogFormatter.initVerbose();
    Context.propagate(new Context(testNet, 100, Coin.ZERO, false));
    testNetChain = new BlockChain(testNet, new Wallet(testNet), new MemoryBlockStore(testNet));
    Context.propagate(new Context(PARAMS, 100, Coin.ZERO, false));
    wallet = new Wallet(PARAMS) {
        @Override
        public void receiveFromBlock(Transaction tx, StoredBlock block, BlockChain.NewBlockType blockType,
                                     int relativityOffset) throws VerificationException {
            super.receiveFromBlock(tx, block, blockType, relativityOffset);
            BlockChainTest.this.block[0] = block;
            if (isTransactionRelevant(tx) && tx.isCoinBase()) {
                BlockChainTest.this.coinbaseTransaction = tx;
            }
        }
    };
    wallet.freshReceiveKey();

    resetBlockStore();
    chain = new BlockChain(PARAMS, wallet, blockStore);

    coinbaseTo = wallet.currentReceiveKey().toAddress(PARAMS);
}
 
Example 6
Source File: ChannelConnectionTest.java    From green_android with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testEmptyWallet() throws Exception {
    Wallet emptyWallet = new Wallet(PARAMS);
    emptyWallet.freshReceiveKey();
    ChannelTestUtils.RecordingPair pair = ChannelTestUtils.makeRecorders(serverWallet, mockBroadcaster);
    PaymentChannelServer server = pair.server;
    PaymentChannelClient client = new PaymentChannelClient(emptyWallet, myKey, COIN, Sha256Hash.ZERO_HASH, null, clientChannelProperties, pair.clientRecorder);
    client.connectionOpen();
    server.connectionOpen();
    server.receiveMessage(pair.clientRecorder.checkNextMsg(MessageType.CLIENT_VERSION));
    client.receiveMessage(pair.serverRecorder.checkNextMsg(MessageType.SERVER_VERSION));
    try {
        client.receiveMessage(Protos.TwoWayChannelMessage.newBuilder()
                .setInitiate(Protos.Initiate.newBuilder().setExpireTimeSecs(Utils.currentTimeSeconds())
                        .setMinAcceptedChannelSize(CENT.value)
                        .setMultisigKey(ByteString.copyFrom(new ECKey().getPubKey()))
                        .setMinPayment(Transaction.REFERENCE_DEFAULT_MIN_TX_FEE.value))
                .setType(MessageType.INITIATE).build());
        fail();
    } catch (InsufficientMoneyException expected) {
        // This should be thrown.
    }
}
 
Example 7
Source File: WalletConfig.java    From bisq with GNU Affero General Public License v3.0 6 votes vote down vote up
private Wallet createOrLoadWallet(File walletFile, boolean shouldReplayWallet,
                                  BisqKeyChainGroup keyChainGroup, boolean isBsqWallet, DeterministicSeed restoreFromSeed)
        throws Exception {

    if (restoreFromSeed != null)
        maybeMoveOldWalletOutOfTheWay(walletFile);

    Wallet wallet;
    if (walletFile.exists()) {
        wallet = loadWallet(walletFile, shouldReplayWallet, keyChainGroup.isUseBitcoinDeterministicKeyChain());
    } else {
        wallet = createWallet(keyChainGroup, isBsqWallet);
        wallet.freshReceiveKey();
        wallet.saveToFile(walletFile);
    }

    if (useAutoSave) wallet.autosaveToFile(walletFile, 5, TimeUnit.SECONDS, null);

    return wallet;
}
 
Example 8
Source File: ChannelConnectionTest.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
@Override
@Before
public void setUp() throws Exception {
    super.setUp();
    Utils.setMockClock(); // Use mock clock
    Context.propagate(new Context(UNITTEST, 3, Coin.ZERO, false)); // Shorter event horizon for unit tests.
    sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, COIN);
    sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, COIN);
    wallet.addExtension(new StoredPaymentChannelClientStates(wallet, failBroadcaster));
    serverWallet = new Wallet(UNITTEST);
    serverWallet.addExtension(new StoredPaymentChannelServerStates(serverWallet, failBroadcaster));
    serverWallet.freshReceiveKey();
    // Use an atomic boolean to indicate failure because fail()/assert*() dont work in network threads
    fail = new AtomicBoolean(false);

    // Set up a way to monitor broadcast transactions. When you expect a broadcast, you must release a permit
    // to the broadcastTxPause semaphore so state can be queried in between.
    broadcasts = new LinkedBlockingQueue<>();
    broadcastTxPause = new Semaphore(0);
    mockBroadcaster = new TransactionBroadcaster() {
        @Override
        public TransactionBroadcast broadcastTransaction(Transaction tx) {
            broadcastTxPause.acquireUninterruptibly();
            SettableFuture<Transaction> future = SettableFuture.create();
            future.set(tx);
            broadcasts.add(tx);
            return TransactionBroadcast.createMockBroadcast(tx, future);
        }
    };

    // Because there are no separate threads in the tests here (we call back into client/server in server/client
    // handlers), we have lots of lock cycles. A normal user shouldn't have this issue as they are probably not both
    // client+server running in the same thread.
    Threading.warnOnLockCycles();

    ECKey.FAKE_SIGNATURES = true;
}
 
Example 9
Source File: ChainSplitTest.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
    BriefLogFormatter.init();
    Utils.setMockClock(); // Use mock clock
    Context.propagate(new Context(PARAMS, 100, Coin.ZERO, false));
    MemoryBlockStore blockStore = new MemoryBlockStore(PARAMS);
    wallet = new Wallet(PARAMS);
    ECKey key1 = wallet.freshReceiveKey();
    ECKey key2 = wallet.freshReceiveKey();
    chain = new BlockChain(PARAMS, wallet, blockStore);
    coinsTo = key1.toAddress(PARAMS);
    coinsTo2 = key2.toAddress(PARAMS);
    someOtherGuy = new ECKey().toAddress(PARAMS);
}
 
Example 10
Source File: ChannelConnectionTest.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
@Override
@Before
public void setUp() throws Exception {
    super.setUp();
    Utils.setMockClock(); // Use mock clock
    Context.propagate(new Context(PARAMS, 3, Coin.ZERO, false)); // Shorter event horizon for unit tests.
    sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, COIN);
    sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, COIN);
    wallet.addExtension(new StoredPaymentChannelClientStates(wallet, failBroadcaster));
    serverWallet = new Wallet(PARAMS);
    serverWallet.addExtension(new StoredPaymentChannelServerStates(serverWallet, failBroadcaster));
    serverWallet.freshReceiveKey();
    // Use an atomic boolean to indicate failure because fail()/assert*() dont work in network threads
    fail = new AtomicBoolean(false);

    // Set up a way to monitor broadcast transactions. When you expect a broadcast, you must release a permit
    // to the broadcastTxPause semaphore so state can be queried in between.
    broadcasts = new LinkedBlockingQueue<>();
    broadcastTxPause = new Semaphore(0);
    mockBroadcaster = new TransactionBroadcaster() {
        @Override
        public TransactionBroadcast broadcastTransaction(Transaction tx) {
            broadcastTxPause.acquireUninterruptibly();
            SettableFuture<Transaction> future = SettableFuture.create();
            future.set(tx);
            broadcasts.add(tx);
            return TransactionBroadcast.createMockBroadcast(tx, future);
        }
    };

    // Because there are no separate threads in the tests here (we call back into client/server in server/client
    // handlers), we have lots of lock cycles. A normal user shouldn't have this issue as they are probably not both
    // client+server running in the same thread.
    Threading.warnOnLockCycles();

    ECKey.FAKE_SIGNATURES = true;
}
 
Example 11
Source File: PaymentChannelStateTest.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
@Override
@Before
public void setUp() throws Exception {
    Utils.setMockClock(); // Use mock clock
    super.setUp();
    Context.propagate(new Context(PARAMS, 100, Coin.ZERO, false));
    wallet.addExtension(new StoredPaymentChannelClientStates(wallet, new TransactionBroadcaster() {
        @Override
        public TransactionBroadcast broadcastTransaction(Transaction tx) {
            fail();
            return null;
        }
    }));
    sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, COIN);
    chain = new BlockChain(PARAMS, wallet, blockStore); // Recreate chain as sendMoneyToWallet will confuse it
    serverWallet = new Wallet(PARAMS);
    serverKey = serverWallet.freshReceiveKey();
    chain.addWallet(serverWallet);

    broadcasts = new LinkedBlockingQueue<>();
    mockBroadcaster = new TransactionBroadcaster() {
        @Override
        public TransactionBroadcast broadcastTransaction(Transaction tx) {
            SettableFuture<Transaction> future = SettableFuture.create();
            broadcasts.add(new TxFuturePair(tx, future));
            return TransactionBroadcast.createMockBroadcast(tx, future);
        }
    };
}
 
Example 12
Source File: PeerGroupTest.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void receiveTxBroadcastOnAddedWallet() throws Exception {
    // Check that when we receive transactions on all our peers, we do the right thing.
    peerGroup.start();

    // Create a peer.
    InboundMessageQueuer p1 = connectPeer(1);
    
    Wallet wallet2 = new Wallet(PARAMS);
    ECKey key2 = wallet2.freshReceiveKey();
    Address address2 = key2.toAddress(PARAMS);
    
    peerGroup.addWallet(wallet2);
    blockChain.addWallet(wallet2);

    assertEquals(BloomFilter.class, waitForOutbound(p1).getClass());
    assertEquals(MemoryPoolMessage.class, waitForOutbound(p1).getClass());

    Coin value = COIN;
    Transaction t1 = FakeTxBuilder.createFakeTx(PARAMS, value, address2);
    InventoryMessage inv = new InventoryMessage(PARAMS);
    inv.addTransaction(t1);

    inbound(p1, inv);
    assertTrue(outbound(p1) instanceof GetDataMessage);
    inbound(p1, t1);
    // Asks for dependency.
    GetDataMessage getdata = (GetDataMessage) outbound(p1);
    assertNotNull(getdata);
    inbound(p1, new NotFoundMessage(PARAMS, getdata.getItems()));
    pingAndWait(p1);
    assertEquals(value, wallet2.getBalance(Wallet.BalanceType.ESTIMATED));
}
 
Example 13
Source File: ChainSplitTest.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
    BriefLogFormatter.init();
    Utils.setMockClock(); // Use mock clock
    Context.propagate(new Context(PARAMS, 100, Coin.ZERO, false));
    MemoryBlockStore blockStore = new MemoryBlockStore(PARAMS);
    wallet = new Wallet(PARAMS);
    ECKey key1 = wallet.freshReceiveKey();
    ECKey key2 = wallet.freshReceiveKey();
    chain = new BlockChain(PARAMS, wallet, blockStore);
    coinsTo = key1.toAddress(PARAMS);
    coinsTo2 = key2.toAddress(PARAMS);
    someOtherGuy = new ECKey().toAddress(PARAMS);
}
 
Example 14
Source File: ChannelConnectionTest.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
@Override
@Before
public void setUp() throws Exception {
    super.setUp();
    Utils.setMockClock(); // Use mock clock
    Context.propagate(new Context(PARAMS, 3, Coin.ZERO, false)); // Shorter event horizon for unit tests.
    sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, COIN);
    sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, COIN);
    wallet.addExtension(new StoredPaymentChannelClientStates(wallet, failBroadcaster));
    serverWallet = new Wallet(PARAMS);
    serverWallet.addExtension(new StoredPaymentChannelServerStates(serverWallet, failBroadcaster));
    serverWallet.freshReceiveKey();
    // Use an atomic boolean to indicate failure because fail()/assert*() dont work in network threads
    fail = new AtomicBoolean(false);

    // Set up a way to monitor broadcast transactions. When you expect a broadcast, you must release a permit
    // to the broadcastTxPause semaphore so state can be queried in between.
    broadcasts = new LinkedBlockingQueue<>();
    broadcastTxPause = new Semaphore(0);
    mockBroadcaster = new TransactionBroadcaster() {
        @Override
        public TransactionBroadcast broadcastTransaction(Transaction tx) {
            broadcastTxPause.acquireUninterruptibly();
            SettableFuture<Transaction> future = SettableFuture.create();
            future.set(tx);
            broadcasts.add(tx);
            return TransactionBroadcast.createMockBroadcast(tx, future);
        }
    };

    // Because there are no separate threads in the tests here (we call back into client/server in server/client
    // handlers), we have lots of lock cycles. A normal user shouldn't have this issue as they are probably not both
    // client+server running in the same thread.
    Threading.warnOnLockCycles();

    ECKey.FAKE_SIGNATURES = true;
}
 
Example 15
Source File: PaymentChannelStateTest.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
@Override
@Before
public void setUp() throws Exception {
    Utils.setMockClock(); // Use mock clock
    super.setUp();
    Context.propagate(new Context(PARAMS, 100, Coin.ZERO, false));
    wallet.addExtension(new StoredPaymentChannelClientStates(wallet, new TransactionBroadcaster() {
        @Override
        public TransactionBroadcast broadcastTransaction(Transaction tx) {
            fail();
            return null;
        }
    }));
    sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, COIN);
    chain = new BlockChain(PARAMS, wallet, blockStore); // Recreate chain as sendMoneyToWallet will confuse it
    serverWallet = new Wallet(PARAMS);
    serverKey = serverWallet.freshReceiveKey();
    chain.addWallet(serverWallet);

    broadcasts = new LinkedBlockingQueue<>();
    mockBroadcaster = new TransactionBroadcaster() {
        @Override
        public TransactionBroadcast broadcastTransaction(Transaction tx) {
            SettableFuture<Transaction> future = SettableFuture.create();
            broadcasts.add(new TxFuturePair(tx, future));
            return TransactionBroadcast.createMockBroadcast(tx, future);
        }
    };
}
 
Example 16
Source File: PeerGroupTest.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void receiveTxBroadcastOnAddedWallet() throws Exception {
    // Check that when we receive transactions on all our peers, we do the right thing.
    peerGroup.start();

    // Create a peer.
    InboundMessageQueuer p1 = connectPeer(1);

    Wallet wallet2 = new Wallet(UNITTEST);
    ECKey key2 = wallet2.freshReceiveKey();
    Address address2 = LegacyAddress.fromKey(UNITTEST, key2);

    peerGroup.addWallet(wallet2);
    blockChain.addWallet(wallet2);

    assertEquals(BloomFilter.class, waitForOutbound(p1).getClass());
    assertEquals(MemoryPoolMessage.class, waitForOutbound(p1).getClass());

    Coin value = COIN;
    Transaction t1 = FakeTxBuilder.createFakeTx(UNITTEST, value, address2);
    InventoryMessage inv = new InventoryMessage(UNITTEST);
    inv.addTransaction(t1);

    inbound(p1, inv);
    assertTrue(outbound(p1) instanceof GetDataMessage);
    inbound(p1, t1);
    // Asks for dependency.
    GetDataMessage getdata = (GetDataMessage) outbound(p1);
    assertNotNull(getdata);
    inbound(p1, new NotFoundMessage(UNITTEST, getdata.getItems()));
    pingAndWait(p1);
    assertEquals(value, wallet2.getBalance(Wallet.BalanceType.ESTIMATED));
}
 
Example 17
Source File: ChainSplitTest.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
    BriefLogFormatter.init();
    Utils.setMockClock(); // Use mock clock
    Context.propagate(new Context(UNITTEST, 100, Coin.ZERO, false));
    MemoryBlockStore blockStore = new MemoryBlockStore(UNITTEST);
    wallet = new Wallet(UNITTEST);
    ECKey key1 = wallet.freshReceiveKey();
    ECKey key2 = wallet.freshReceiveKey();
    chain = new BlockChain(UNITTEST, wallet, blockStore);
    coinsTo = LegacyAddress.fromKey(UNITTEST, key1);
    coinsTo2 = LegacyAddress.fromKey(UNITTEST, key2);
    someOtherGuy = LegacyAddress.fromKey(UNITTEST, new ECKey());
}
 
Example 18
Source File: PeerGroupTest.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void receiveTxBroadcastOnAddedWallet() throws Exception {
    // Check that when we receive transactions on all our peers, we do the right thing.
    peerGroup.start();

    // Create a peer.
    InboundMessageQueuer p1 = connectPeer(1);
    
    Wallet wallet2 = new Wallet(PARAMS);
    ECKey key2 = wallet2.freshReceiveKey();
    Address address2 = key2.toAddress(PARAMS);
    
    peerGroup.addWallet(wallet2);
    blockChain.addWallet(wallet2);

    assertEquals(BloomFilter.class, waitForOutbound(p1).getClass());
    assertEquals(MemoryPoolMessage.class, waitForOutbound(p1).getClass());

    Coin value = COIN;
    Transaction t1 = FakeTxBuilder.createFakeTx(PARAMS, value, address2);
    InventoryMessage inv = new InventoryMessage(PARAMS);
    inv.addTransaction(t1);

    inbound(p1, inv);
    assertTrue(outbound(p1) instanceof GetDataMessage);
    inbound(p1, t1);
    // Asks for dependency.
    GetDataMessage getdata = (GetDataMessage) outbound(p1);
    assertNotNull(getdata);
    inbound(p1, new NotFoundMessage(PARAMS, getdata.getItems()));
    pingAndWait(p1);
    assertEquals(value, wallet2.getBalance(Wallet.BalanceType.ESTIMATED));
}
 
Example 19
Source File: ParseByteCacheTest.java    From bcm-android with GNU General Public License v3.0 4 votes vote down vote up
@Before
public void setUp() throws Exception {
    Context context = new Context(UNITTEST);
    Wallet wallet = new Wallet(context);
    wallet.freshReceiveKey();

    resetBlockStore();

    Transaction tx1 = createFakeTx(UNITTEST,
            valueOf(2, 0),
            LegacyAddress.fromKey(UNITTEST, wallet.currentReceiveKey()));

    // add a second input so can test granularity of byte cache.
    Transaction prevTx = new Transaction(UNITTEST);
    TransactionOutput prevOut = new TransactionOutput(UNITTEST, prevTx, COIN, LegacyAddress.fromKey(UNITTEST, wallet.currentReceiveKey()));
    prevTx.addOutput(prevOut);
    // Connect it.
    tx1.addInput(prevOut);

    Transaction tx2 = createFakeTx(UNITTEST, COIN,
            LegacyAddress.fromKey(UNITTEST, new ECKey()));

    Block b1 = createFakeBlock(blockStore, BLOCK_HEIGHT_GENESIS, tx1, tx2).block;

    MessageSerializer bs = UNITTEST.getDefaultSerializer();

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    bs.serialize(tx1, bos);
    tx1BytesWithHeader = bos.toByteArray();
    tx1Bytes = tx1.bitcoinSerialize();

    bos.reset();
    bs.serialize(tx2, bos);
    tx2BytesWithHeader = bos.toByteArray();
    tx2Bytes = tx2.bitcoinSerialize();

    bos.reset();
    bs.serialize(b1, bos);
    b1BytesWithHeader = bos.toByteArray();
    b1Bytes = b1.bitcoinSerialize();
}
 
Example 20
Source File: ParseByteCacheTest.java    From GreenBits with GNU General Public License v3.0 4 votes vote down vote up
@Before
public void setUp() throws Exception {
    Context context = new Context(PARAMS);
    Wallet wallet = new Wallet(context);
    wallet.freshReceiveKey();

    resetBlockStore();
    
    Transaction tx1 = createFakeTx(PARAMS,
            valueOf(2, 0),
            wallet.currentReceiveKey().toAddress(PARAMS));
    
    // add a second input so can test granularity of byte cache.
    Transaction prevTx = new Transaction(PARAMS);
    TransactionOutput prevOut = new TransactionOutput(PARAMS, prevTx, COIN, wallet.currentReceiveKey().toAddress(PARAMS));
    prevTx.addOutput(prevOut);
    // Connect it.
    tx1.addInput(prevOut);
    
    Transaction tx2 = createFakeTx(PARAMS, COIN,
            new ECKey().toAddress(PARAMS));

    Block b1 = createFakeBlock(blockStore, BLOCK_HEIGHT_GENESIS, tx1, tx2).block;

    MessageSerializer bs = PARAMS.getDefaultSerializer();
    
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    bs.serialize(tx1, bos);
    tx1BytesWithHeader = bos.toByteArray();
    tx1Bytes = tx1.bitcoinSerialize();
    
    bos.reset();
    bs.serialize(tx2, bos);
    tx2BytesWithHeader = bos.toByteArray();
    tx2Bytes = tx2.bitcoinSerialize();
    
    bos.reset();
    bs.serialize(b1, bos);
    b1BytesWithHeader = bos.toByteArray();
    b1Bytes = b1.bitcoinSerialize();
}