Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add WalletResetEventListener for wallet.reset() #267

Merged
merged 1 commit into from
Dec 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions core/src/main/java/org/bitcoinj/wallet/Wallet.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
import org.bitcoinj.wallet.listeners.WalletCoinsReceivedEventListener;
import org.bitcoinj.wallet.listeners.WalletCoinsSentEventListener;
import org.bitcoinj.wallet.listeners.WalletReorganizeEventListener;
import org.bitcoinj.wallet.listeners.WalletResetEventListener;
import org.slf4j.*;
import org.bouncycastle.crypto.params.*;

Expand Down Expand Up @@ -203,6 +204,8 @@ protected boolean removeEldestEntry(Map.Entry<Sha256Hash, Transaction> eldest) {
= new CopyOnWriteArrayList<>();
private final CopyOnWriteArrayList<ListenerRegistration<WalletReorganizeEventListener>> reorganizeListeners
= new CopyOnWriteArrayList<>();
private final CopyOnWriteArrayList<ListenerRegistration<WalletResetEventListener>> resetListeners
= new CopyOnWriteArrayList<>();
private final CopyOnWriteArrayList<ListenerRegistration<ScriptsChangeEventListener>> scriptChangeListeners
= new CopyOnWriteArrayList<>();
private final CopyOnWriteArrayList<ListenerRegistration<TransactionConfidenceEventListener>> transactionConfidenceListeners
Expand Down Expand Up @@ -3041,6 +3044,14 @@ public void addReorganizeEventListener(Executor executor, WalletReorganizeEventL
reorganizeListeners.add(new ListenerRegistration<>(listener, executor));
}

/**
* Adds an event listener object. Methods on this object are called when something interesting happens,
* like receiving money. The listener is executed by the given executor.
*/
public void addResetEventListener(Executor executor, WalletResetEventListener listener) {
// This is thread safe, so we don't need to take the lock.
resetListeners.add(new ListenerRegistration<>(listener, executor));
}
/**
* Adds an event listener object. Methods on this object are called when scripts
* watched by this wallet change. Runs the listener methods in the user thread.
Expand Down Expand Up @@ -3122,6 +3133,14 @@ public boolean removeReorganizeEventListener(WalletReorganizeEventListener liste
return ListenerRegistration.removeFromList(listener, reorganizeListeners);
}

/**
* Removes the given event listener object. Returns true if the listener was removed, false if that listener
* was never added.
*/
public boolean removeResetEventListener(WalletResetEventListener listener) {
return ListenerRegistration.removeFromList(listener, resetListeners);
}

/**
* Removes the given event listener object. Returns true if the listener was removed, false if that listener
* was never added.
Expand Down Expand Up @@ -3208,6 +3227,18 @@ public void run() {
}
}

protected void queueOnReset() {
checkState(lock.isHeldByCurrentThread());
for (final ListenerRegistration<WalletResetEventListener> registration : resetListeners) {
registration.executor.execute(new Runnable() {
@Override
public void run() {
registration.listener.onWalletReset(Wallet.this);
}
});
}
}

protected void queueOnScriptsChanged(final List<Script> scripts, final boolean isAddingScripts) {
for (final ListenerRegistration<ScriptsChangeEventListener> registration : scriptChangeListeners) {
registration.executor.execute(new Runnable() {
Expand Down Expand Up @@ -3424,6 +3455,7 @@ public void reset() {
lastBlockSeenHeight = -1; // Magic value for 'never'.
lastBlockSeenTimeSecs = 0;
saveLater();
queueOnReset();
maybeQueueOnWalletChanged();
} finally {
lock.unlock();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright 2024 Dash Core Group
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.bitcoinj.wallet.listeners;

import org.bitcoinj.wallet.Wallet;

public interface WalletResetEventListener {
/**
* <p>This is called when the wallet is reset and all transactions are removed.</p>
*
*/
void onWalletReset(Wallet wallet);
}
Loading