Delete watched scripts methods. Fixed queueOnScriptsChanged threading.

This commit is contained in:
Alon Muroch 2014-10-21 09:57:44 +03:00 committed by Andreas Schildbach
parent 207ba9fd9d
commit 6e40d4d7cd
7 changed files with 104 additions and 9 deletions

View file

@ -51,7 +51,7 @@ public abstract class AbstractWalletEventListener extends AbstractKeyChainEventL
}
@Override
public void onScriptsAdded(Wallet wallet, List<Script> scripts) {
public void onScriptsChanged(Wallet wallet, List<Script> scripts, boolean isAddingScripts) {
onChange();
}

View file

@ -190,7 +190,7 @@ public class PeerGroup extends AbstractExecutionThreadService implements Transac
}
}
@Override public void onScriptsAdded(Wallet wallet, List<Script> scripts) {
@Override public void onScriptsChanged(Wallet wallet, List<Script> scripts, boolean isAddingScripts) {
queueRecalc(true);
}

View file

@ -766,11 +766,59 @@ public class Wallet extends BaseTaggableObject implements Serializable, BlockCha
} finally {
keychainLock.writeLock().unlock();
}
queueOnScriptsAdded(scripts);
queueOnScriptsChanged(scripts, true);
saveNow();
return added;
}
/**
* Removes the given output scripts from the wallet that were being watched.
*
* @return true if successful
*/
public boolean removeWatchedAddress(final Address address) {
return removeWatchedAddresses(ImmutableList.of(address));
}
/**
* Removes the given output scripts from the wallet that were being watched.
*
* @return true if successful
*/
public boolean removeWatchedAddresses(final List<Address> addresses) {
List<Script> scripts = Lists.newArrayList();
for (Address address : addresses) {
Script script = ScriptBuilder.createOutputScript(address);
scripts.add(script);
}
return removeWatchedScripts(scripts);
}
/**
* Removes the given output scripts from the wallet that were being watched.
*
* @return true if successful
*/
public boolean removeWatchedScripts(final List<Script> scripts) {
lock.lock();
try {
for (final Script script : scripts) {
if (!watchedScripts.contains(script))
continue;
watchedScripts.remove(script);
}
queueOnScriptsChanged(scripts, false);
saveNow();
return true;
} finally {
lock.unlock();
}
}
/**
* Returns all addresses watched by this wallet.
*/
@ -2175,12 +2223,12 @@ public class Wallet extends BaseTaggableObject implements Serializable, BlockCha
}
}
protected void queueOnScriptsAdded(final List<Script> scripts) {
protected void queueOnScriptsChanged(final List<Script> scripts, final boolean isAddingScripts) {
for (final ListenerRegistration<WalletEventListener> registration : eventListeners) {
registration.executor.execute(new Runnable() {
@Override
public void run() {
registration.listener.onScriptsAdded(Wallet.this, scripts);
registration.listener.onScriptsChanged(Wallet.this, scripts, isAddingScripts);
}
});
}

View file

@ -116,6 +116,10 @@ public interface WalletEventListener extends KeyChainEventListener {
*/
void onWalletChanged(Wallet wallet);
/** Called whenever a new watched script is added to the wallet. */
void onScriptsAdded(Wallet wallet, List<Script> scripts);
/**
* Called whenever a new watched script is added to the wallet.
*
* @param isAddingScripts will be true if added scripts, false if removed scripts.
*/
void onScriptsChanged(Wallet wallet, List<Script> scripts, boolean isAddingScripts);
}

View file

@ -52,5 +52,5 @@ public class NativeWalletEventListener implements WalletEventListener {
public native void onKeysAdded(List<ECKey> keys);
@Override
public native void onScriptsAdded(Wallet wallet, List<Script> scripts);
public native void onScriptsChanged(Wallet wallet, List<Script> scripts, boolean isAddingScripts);
}

View file

@ -1242,6 +1242,49 @@ public class WalletTest extends TestWithWallet {
assertEquals(watchedAddress, watchedAddresses.get(0));
}
@Test
public void removeWatchedAddresses() {
List<Address> addressesForRemoval = new ArrayList<Address>();
for (int i = 0; i < 10; i++) {
ECKey key = new ECKey();
Address watchedAddress = key.toAddress(params);
addressesForRemoval.add(watchedAddress);
wallet.addWatchedAddress(watchedAddress);
}
wallet.removeWatchedAddresses(addressesForRemoval);
for (Address addr : addressesForRemoval)
assertFalse(wallet.isAddressWatched(addr));
assertFalse(wallet.isRequiringUpdateAllBloomFilter());
}
@Test
public void removeScriptsBloomFilter() throws Exception {
List<Address> addressesForRemoval = new ArrayList<Address>();
for (int i = 0; i < 10; i++) {
ECKey key = new ECKey();
Address watchedAddress = key.toAddress(params);
addressesForRemoval.add(watchedAddress);
wallet.addWatchedAddress(watchedAddress);
}
wallet.removeWatchedAddresses(addressesForRemoval);
for (Address addr : addressesForRemoval) {
Transaction t1 = createFakeTx(params, CENT, addr);
StoredBlock b1 = createFakeBlock(blockStore, t1).storedBlock;
TransactionOutPoint outPoint = new TransactionOutPoint(params, 0, t1);
// Note that this has a 1e-12 chance of failing this unit test due to a false positive
assertFalse(wallet.getBloomFilter(1e-12).contains(outPoint.bitcoinSerialize()));
wallet.receiveFromBlock(t1, b1, BlockChain.NewBlockType.BEST_CHAIN, 0);
assertFalse(wallet.getBloomFilter(1e-12).contains(outPoint.bitcoinSerialize()));
}
}
@Test
public void marriedKeychainBloomFilter() throws Exception {
createMarriedWallet(2, 2);

View file

@ -91,7 +91,7 @@ public class Kit {
}
@Override
public void onScriptsAdded(Wallet wallet, List<Script> scripts) {
public void onScriptsChanged(Wallet wallet, List<Script> scripts, boolean isAddingScripts) {
System.out.println("new script added");
}
}