Bugfix: copy event listeners to new HD chains when created.

This commit is contained in:
Mike Hearn 2014-04-04 16:37:41 +02:00
parent 2ce5c16815
commit 704339fdfb
3 changed files with 23 additions and 0 deletions

View file

@ -210,6 +210,10 @@ public class BasicKeyChain implements EncryptableKeyChain {
}
}
public List<ListenerRegistration<KeyChainEventListener>> getListeners() {
return new ArrayList<ListenerRegistration<KeyChainEventListener>>(listeners);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Serialization support

View file

@ -20,6 +20,7 @@ import com.google.bitcoin.core.*;
import com.google.bitcoin.crypto.DeterministicKey;
import com.google.bitcoin.crypto.KeyCrypter;
import com.google.bitcoin.store.UnreadableWalletException;
import com.google.bitcoin.utils.ListenerRegistration;
import com.google.bitcoin.utils.Threading;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
@ -85,6 +86,8 @@ public class KeyChainGroup {
private void createAndActivateNewHDChain() {
final DeterministicKeyChain chain = new DeterministicKeyChain(new SecureRandom());
for (ListenerRegistration<KeyChainEventListener> registration : basic.getListeners())
chain.addEventListener(registration.listener, registration.executor);
if (lookaheadSize >= 0)
chain.setLookaheadSize(lookaheadSize);
chains.add(chain);

View file

@ -2361,4 +2361,20 @@ public class WalletTest extends TestWithWallet {
wallet.notifyTransactionIsInBlock(tx.getHash(), block, AbstractBlockChain.NewBlockType.BEST_CHAIN, 1);
assertEquals(Utils.COIN, wallet.getBalance());
}
@Test
public void keyEvents() throws Exception {
// Check that we can register an event listener, generate some keys and the callbacks are invoked properly.
wallet = new Wallet(params);
final List<ECKey> keys = Lists.newLinkedList();
wallet.addEventListener(new AbstractWalletEventListener() {
@Override
public void onKeysAdded(List<ECKey> k) {
keys.addAll(k);
}
}, Threading.SAME_THREAD);
wallet.setKeychainLookaheadSize(5);
wallet.freshReceiveKey();
assertEquals(6, keys.size());
}
}