Search in sources :

Example 1 with SemaphoredStatementListener

use of org.catena.common.SemaphoredStatementListener in project catena-java by alinush.

the class ReorganizeChainTest method setUp.

@Before
public void setUp() throws Exception {
    Context.propagate(new Context(params, 10000, Transaction.DEFAULT_TX_FEE, true));
    semAppended = new Semaphore(0);
    semWithdrawn = new Semaphore(0);
    // Step 0: Catena wallet already started with a listener that signals when statements are appended and withdrawn
    wallet = new ClientWallet(params);
    wallet.addExtension(new CatenaWalletExtension());
    // Generate a key to send mining rewards to
    genCoinsKey = new ECKey();
    wallet.importKey(genCoinsKey);
    genCoinsAddr = genCoinsKey.toAddress(params);
    // need to tell the wallet about the chain address by calling this
    wallet.addWatchedAddress(genCoinsAddr);
    // Generate a black hole key, where funds are send to be lost
    blackholeAddr = new ECKey().toAddress(params);
    // Add semaphores to wallet
    wallet.addStatementListener(new SemaphoredStatementListener(wallet, semAppended, semWithdrawn));
    wallet.addReorganizeEventListener(new WalletReorganizeEventListener() {

        @Override
        public void onReorganize(Wallet wallet) {
            log.debug("Fork detected!");
        }
    });
    // TODO: can set an onReorganize listener here
    chain = new BlockChain(params, wallet, new MemoryBlockStore(params));
    // Set a listener to wait for those coins
    final Semaphore sem = new Semaphore(0);
    wallet.addCoinsReceivedEventListener(new WalletCoinsReceivedEventListener() {

        @Override
        public void onCoinsReceived(Wallet wallet, Transaction tx, Coin prevBalance, Coin newBalance) {
            // log.debug("Got initial coins from mining reward: {}", newBalance);
            sem.release();
        }
    });
    // Create the first block, so we can get some coins.
    lastBlock = params.getGenesisBlock().createNextBlock(genCoinsAddr);
    chain.add(lastBlock);
    // Bury the first block with enough blocks, so we can spend those coins
    for (int i = 0; i < params.getSpendableCoinbaseDepth(); i++) {
        lastBlock = lastBlock.createNextBlock(blackholeAddr);
        chain.add(lastBlock);
    }
    log.debug("Created {} blocks.", chain.getBestChainHeight());
    assertTrue("timed out waiting to receive coins from block reward", sem.tryAcquire(10, TimeUnit.SECONDS));
    // Create the root-of-trust TXN
    Transaction tx = issueStatement("testchain", false);
    log.debug("Created root-of-trust TXN {}", tx.getHash());
    // wallet.getCatenaExtension().setRootOfTrustTxid(rootOfTrustTxid); // already set by issueStatement
    wallet.addChangeEventListener(Threading.SAME_THREAD, new CatenaWalletListener(wallet));
}
Also used : Context(org.bitcoinj.core.Context) BlockChain(org.bitcoinj.core.BlockChain) ClientWallet(org.catena.client.ClientWallet) Wallet(org.bitcoinj.wallet.Wallet) CatenaWalletExtension(org.catena.common.CatenaWalletExtension) ClientWallet(org.catena.client.ClientWallet) ECKey(org.bitcoinj.core.ECKey) WalletReorganizeEventListener(org.bitcoinj.wallet.listeners.WalletReorganizeEventListener) WalletCoinsReceivedEventListener(org.bitcoinj.wallet.listeners.WalletCoinsReceivedEventListener) Semaphore(java.util.concurrent.Semaphore) MemoryBlockStore(org.bitcoinj.store.MemoryBlockStore) SemaphoredStatementListener(org.catena.common.SemaphoredStatementListener) Coin(org.bitcoinj.core.Coin) Transaction(org.bitcoinj.core.Transaction) Before(org.junit.Before)

Aggregations

Semaphore (java.util.concurrent.Semaphore)1 BlockChain (org.bitcoinj.core.BlockChain)1 Coin (org.bitcoinj.core.Coin)1 Context (org.bitcoinj.core.Context)1 ECKey (org.bitcoinj.core.ECKey)1 Transaction (org.bitcoinj.core.Transaction)1 MemoryBlockStore (org.bitcoinj.store.MemoryBlockStore)1 Wallet (org.bitcoinj.wallet.Wallet)1 WalletCoinsReceivedEventListener (org.bitcoinj.wallet.listeners.WalletCoinsReceivedEventListener)1 WalletReorganizeEventListener (org.bitcoinj.wallet.listeners.WalletReorganizeEventListener)1 ClientWallet (org.catena.client.ClientWallet)1 CatenaWalletExtension (org.catena.common.CatenaWalletExtension)1 SemaphoredStatementListener (org.catena.common.SemaphoredStatementListener)1 Before (org.junit.Before)1