mirror of
https://github.com/Qortal/altcoinj.git
synced 2025-01-31 15:22:16 +00:00
Remove memory usage optimization that was complicating things.
This commit is contained in:
parent
c0a295eed1
commit
6813ff4e69
@ -65,7 +65,7 @@ public class TransactionConfidence implements Serializable {
|
|||||||
private Set<PeerAddress> broadcastBy;
|
private Set<PeerAddress> broadcastBy;
|
||||||
/** The Transaction that this confidence object is associated with. */
|
/** The Transaction that this confidence object is associated with. */
|
||||||
private Transaction transaction;
|
private Transaction transaction;
|
||||||
private transient ArrayList<Listener> listeners;
|
private transient ArrayList<Listener> listeners = new ArrayList<Listener>(1);
|
||||||
|
|
||||||
// TODO: The advice below is a mess. There should be block chain listeners, see issue 94.
|
// TODO: The advice below is a mess. There should be block chain listeners, see issue 94.
|
||||||
/**
|
/**
|
||||||
@ -81,8 +81,6 @@ public class TransactionConfidence implements Serializable {
|
|||||||
public synchronized void addEventListener(Listener listener) {
|
public synchronized void addEventListener(Listener listener) {
|
||||||
if (listener == null)
|
if (listener == null)
|
||||||
throw new IllegalArgumentException("listener is null");
|
throw new IllegalArgumentException("listener is null");
|
||||||
if (listeners == null)
|
|
||||||
listeners = new ArrayList<Listener>(1);
|
|
||||||
listeners.add(listener);
|
listeners.add(listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -90,8 +88,6 @@ public class TransactionConfidence implements Serializable {
|
|||||||
if (listener == null)
|
if (listener == null)
|
||||||
throw new IllegalArgumentException("listener is null");
|
throw new IllegalArgumentException("listener is null");
|
||||||
listeners.remove(listener);
|
listeners.remove(listener);
|
||||||
if (listeners.size() == 0)
|
|
||||||
listeners = null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Describes the state of the transaction in general terms. Properties can be read to learn specifics. */
|
/** Describes the state of the transaction in general terms. Properties can be read to learn specifics. */
|
||||||
@ -130,7 +126,8 @@ public class TransactionConfidence implements Serializable {
|
|||||||
/**
|
/**
|
||||||
* A confidence listener is informed when the level of confidence in a transaction is updated by something, like
|
* A confidence listener is informed when the level of confidence in a transaction is updated by something, like
|
||||||
* for example a {@link Wallet}. You can add listeners to update your user interface or manage your order tracking
|
* for example a {@link Wallet}. You can add listeners to update your user interface or manage your order tracking
|
||||||
* system when confidence levels pass a certain threshold. <b>Note that confidence can go down as well as up.</b>
|
* system when confidence levels pass a certain threshold. <b>Note that confidence can go down as well as up.</b>.
|
||||||
|
* During listener execution, it's safe to remove the current listener but not others.
|
||||||
*/
|
*/
|
||||||
public interface Listener {
|
public interface Listener {
|
||||||
public void onConfidenceChanged(Transaction tx);
|
public void onConfidenceChanged(Transaction tx);
|
||||||
@ -331,11 +328,15 @@ public class TransactionConfidence implements Serializable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void runListeners() {
|
private void runListeners() {
|
||||||
if (listeners == null) return;
|
for (int i = 0; i < listeners.size(); i++) {
|
||||||
for (Listener l : listeners) {
|
Listener l = listeners.get(i);
|
||||||
synchronized (l) {
|
synchronized (l) {
|
||||||
l.onConfidenceChanged(transaction);
|
l.onConfidenceChanged(transaction);
|
||||||
}
|
}
|
||||||
|
if (listeners.get(i) != l) {
|
||||||
|
// Listener removed itself.
|
||||||
|
i--;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -270,10 +270,19 @@ public class Wallet implements Serializable {
|
|||||||
// Event listeners may re-enter so we cannot make assumptions about wallet state after this loop completes.
|
// Event listeners may re-enter so we cannot make assumptions about wallet state after this loop completes.
|
||||||
BigInteger balance = getBalance();
|
BigInteger balance = getBalance();
|
||||||
BigInteger newBalance = balance.add(value);
|
BigInteger newBalance = balance.add(value);
|
||||||
for (WalletEventListener l : eventListeners) {
|
invokeOnCoinsReceived(tx, balance, newBalance);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void invokeOnCoinsReceived(Transaction tx, BigInteger balance, BigInteger newBalance) {
|
||||||
|
for (int i = 0; i < eventListeners.size(); i++) {
|
||||||
|
WalletEventListener l = eventListeners.get(i);
|
||||||
synchronized (l) {
|
synchronized (l) {
|
||||||
l.onCoinsReceived(this, tx, balance, newBalance);
|
l.onCoinsReceived(this, tx, balance, newBalance);
|
||||||
}
|
}
|
||||||
|
if (eventListeners.get(i) != l) {
|
||||||
|
// Listener removed itself.
|
||||||
|
i--;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -409,11 +418,7 @@ public class Wallet implements Serializable {
|
|||||||
//
|
//
|
||||||
// TODO: Decide whether to run the event listeners, if a tx confidence listener already modified the wallet.
|
// TODO: Decide whether to run the event listeners, if a tx confidence listener already modified the wallet.
|
||||||
if (!reorg && bestChain && valueDifference.compareTo(BigInteger.ZERO) > 0 && wtx == null) {
|
if (!reorg && bestChain && valueDifference.compareTo(BigInteger.ZERO) > 0 && wtx == null) {
|
||||||
for (WalletEventListener l : eventListeners) {
|
invokeOnCoinsReceived(tx, prevBalance, getBalance());
|
||||||
synchronized (l) {
|
|
||||||
l.onCoinsReceived(this, tx, prevBalance, getBalance());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -452,11 +457,7 @@ public class Wallet implements Serializable {
|
|||||||
dead.put(doubleSpend.getHash(), doubleSpend);
|
dead.put(doubleSpend.getHash(), doubleSpend);
|
||||||
// Inform the event listeners of the newly dead tx.
|
// Inform the event listeners of the newly dead tx.
|
||||||
doubleSpend.getConfidence().setOverridingTransaction(tx);
|
doubleSpend.getConfidence().setOverridingTransaction(tx);
|
||||||
for (WalletEventListener listener : eventListeners) {
|
invokeOnDeadTransaction(doubleSpend, tx);
|
||||||
synchronized (listener) {
|
|
||||||
listener.onDeadTransaction(this, doubleSpend, tx);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1181,12 +1182,7 @@ public class Wallet implements Serializable {
|
|||||||
Transaction replacement = doubleSpent.getSpentBy().getParentTransaction();
|
Transaction replacement = doubleSpent.getSpentBy().getParentTransaction();
|
||||||
dead.put(tx.getHash(), tx);
|
dead.put(tx.getHash(), tx);
|
||||||
pending.remove(tx.getHash());
|
pending.remove(tx.getHash());
|
||||||
// Inform the event listeners of the newly dead tx.
|
invokeOnDeadTransaction(tx, replacement);
|
||||||
for (WalletEventListener listener : eventListeners) {
|
|
||||||
synchronized (listener) {
|
|
||||||
listener.onDeadTransaction(this, tx, replacement);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1203,6 +1199,19 @@ public class Wallet implements Serializable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void invokeOnDeadTransaction(Transaction tx, Transaction replacement) {
|
||||||
|
for (int i = 0; i < eventListeners.size(); i++) {
|
||||||
|
WalletEventListener listener = eventListeners.get(i);
|
||||||
|
synchronized (listener) {
|
||||||
|
listener.onDeadTransaction(this, tx, replacement);
|
||||||
|
}
|
||||||
|
if (eventListeners.get(i) != listener) {
|
||||||
|
// Listener removed itself.
|
||||||
|
i--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns an immutable view of the transactions currently waiting for network confirmations.
|
* Returns an immutable view of the transactions currently waiting for network confirmations.
|
||||||
*/
|
*/
|
||||||
|
@ -22,7 +22,10 @@ import java.math.BigInteger;
|
|||||||
* Implementing WalletEventListener allows you to learn when the contents of the wallet changes due to
|
* Implementing WalletEventListener allows you to learn when the contents of the wallet changes due to
|
||||||
* receiving money or a block chain re-organize. Methods are called with the event listener object locked so your
|
* receiving money or a block chain re-organize. Methods are called with the event listener object locked so your
|
||||||
* implementation does not have to be thread safe. It may be convenient to derive from
|
* implementation does not have to be thread safe. It may be convenient to derive from
|
||||||
* {@link AbstractWalletEventListener} instead.
|
* {@link AbstractWalletEventListener} instead.<p>
|
||||||
|
*
|
||||||
|
* It is safe to call methods of the wallet during event listener execution, and also for a listener to remove itself.
|
||||||
|
* Other types of modifications generally aren't safe.
|
||||||
*/
|
*/
|
||||||
public interface WalletEventListener {
|
public interface WalletEventListener {
|
||||||
/**
|
/**
|
||||||
|
@ -115,6 +115,10 @@ public class PingService {
|
|||||||
public void onBlocksDownloaded(Peer peer, Block block, int blocksLeft) {
|
public void onBlocksDownloaded(Peer peer, Block block, int blocksLeft) {
|
||||||
super.onBlocksDownloaded(peer, block, blocksLeft);
|
super.onBlocksDownloaded(peer, block, blocksLeft);
|
||||||
|
|
||||||
|
// Don't bother printing during block chain downloads.
|
||||||
|
if (blocksLeft > 0)
|
||||||
|
return;
|
||||||
|
|
||||||
Set<Transaction> transactions = wallet.getTransactions(false, false);
|
Set<Transaction> transactions = wallet.getTransactions(false, false);
|
||||||
if (transactions.size() == 0) return;
|
if (transactions.size() == 0) return;
|
||||||
System.out.println("Confidences of wallet transactions:");
|
System.out.println("Confidences of wallet transactions:");
|
||||||
@ -149,9 +153,7 @@ public class PingService {
|
|||||||
if (tx2.getConfidence().getConfidenceType() == TransactionConfidence.ConfidenceType.BUILDING) {
|
if (tx2.getConfidence().getConfidenceType() == TransactionConfidence.ConfidenceType.BUILDING) {
|
||||||
// Coins were confirmed.
|
// Coins were confirmed.
|
||||||
bounceCoins(tx2);
|
bounceCoins(tx2);
|
||||||
|
tx2.getConfidence().removeEventListener(this);
|
||||||
// TODO: Make this work.
|
|
||||||
// tx2.getConfidence().removeEventListener(this);
|
|
||||||
} else {
|
} else {
|
||||||
System.out.println(String.format("Confidence of %s changed, is now: %s",
|
System.out.println(String.format("Confidence of %s changed, is now: %s",
|
||||||
tx2.getHashAsString(), tx2.getConfidence().toString()));
|
tx2.getHashAsString(), tx2.getConfidence().toString()));
|
||||||
|
Loading…
Reference in New Issue
Block a user