use of org.bitcoinj.wallet.listeners.WalletCoinsReceivedEventListener 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));
}
Aggregations