Skip to content
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
23 changes: 16 additions & 7 deletions core/src/main/java/org/bitcoinj/core/Peer.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.bitcoinj.utils.ListenerRegistration;
import org.bitcoinj.utils.Threading;
import org.bitcoinj.wallet.Wallet;
import org.jspecify.annotations.NonNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -83,6 +84,8 @@ public class Peer extends PeerSocketHandler {
private static final Logger log = LoggerFactory.getLogger(Peer.class);
protected final ReentrantLock lock = Threading.lock(Peer.class);

private final @NonNull PeerAddress peerAddress;

private final NetworkParameters params;
private final AbstractBlockChain blockChain;
private final long requiredServices;
Expand Down Expand Up @@ -247,7 +250,8 @@ public Peer(NetworkParameters params, VersionMessage ver, PeerAddress remoteAddr
*/
public Peer(NetworkParameters params, VersionMessage ver, PeerAddress remoteAddress,
@Nullable AbstractBlockChain chain, long requiredServices, int downloadTxDependencyDepth) {
super(remoteAddress, params.getDefaultSerializer());
super(params.getDefaultSerializer());
this.peerAddress = remoteAddress;
this.params = Objects.requireNonNull(params);
this.versionMessage = Objects.requireNonNull(ver);
this.vDownloadTxDependencyDepth = chain != null ? downloadTxDependencyDepth : 0;
Expand Down Expand Up @@ -284,6 +288,12 @@ public Peer(NetworkParameters params, AbstractBlockChain blockChain, PeerAddress
this.versionMessage.appendToSubVer(thisSoftwareName, thisSoftwareVersion, null);
}

@Override
@NonNull
public PeerAddress getAddress() {
return peerAddress;
}

/** Registers a listener that is invoked when new blocks are downloaded. */
public void addBlocksDownloadedEventListener(BlocksDownloadedEventListener listener) {
addBlocksDownloadedEventListener(Threading.USER_THREAD, listener);
Expand Down Expand Up @@ -744,16 +754,15 @@ protected void processTransaction(final Transaction tx) throws VerificationExcep
return;
}
if (currentFilteredBlock != null) {
if (!currentFilteredBlock.provideTransaction(tx)) {
// Got a tx that didn't fit into the filtered block, so we must have received everything.
endFilteredBlock(currentFilteredBlock);
currentFilteredBlock = null;
// Fall through to process tx as a loose transaction.
} else {
if (currentFilteredBlock.provideTransaction(tx)) {
// Don't tell wallets or listeners about this tx as they'll learn about it when the filtered block is
// fully downloaded instead.
return;
}
// Got a tx that didn't fit into the filtered block, so we must have received everything.
endFilteredBlock(currentFilteredBlock);
currentFilteredBlock = null;
// Fall through to process tx as a loose transaction.
}
// It's a broadcast transaction. Tell all wallets about this tx so they can check if it's relevant or not.
for (final Wallet wallet : wallets) {
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/org/bitcoinj/core/PeerGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -2382,15 +2382,15 @@ protected Peer selectDownloadPeer(List<Peer> peers) {
if (peerHeight < mostCommonChainHeight || peerHeight > mostCommonChainHeight + 1)
continue;
candidates.add(peer);
highestPriority = Math.max(highestPriority, getPriority(peer.peerAddress));
highestPriority = Math.max(highestPriority, getPriority(peer.getAddress()));
}
if (candidates.isEmpty())
return null;

// If there is a difference in priority, consider only the highest.
for (Iterator<Peer> i = candidates.iterator(); i.hasNext(); ) {
Peer peer = i.next();
if (getPriority(peer.peerAddress) < highestPriority)
if (getPriority(peer.getAddress()) < highestPriority)
i.remove();
}

Expand Down
11 changes: 5 additions & 6 deletions core/src/main/java/org/bitcoinj/core/PeerSocketHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.bitcoinj.net.StreamConnection;
import org.bitcoinj.net.TimeoutHandler;
import org.bitcoinj.utils.Threading;
import org.jspecify.annotations.NonNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -52,7 +53,6 @@ public abstract class PeerSocketHandler implements TimeoutHandler, StreamConnect
private final SocketTimeoutTask timeoutTask;

private final MessageSerializer serializer;
protected final PeerAddress peerAddress;
// If we close() before we know our writeTarget, set this to true to call writeTarget.closeConnection() right away.
private boolean closePending = false;
// writeTarget will be thread-safe, and may call into PeerGroup, which calls us, so we should call it unlocked
Expand All @@ -65,8 +65,7 @@ public abstract class PeerSocketHandler implements TimeoutHandler, StreamConnect
private int largeReadBufferPos;
private BitcoinSerializer.BitcoinPacketHeader header;

protected PeerSocketHandler(PeerAddress peerAddress, MessageSerializer messageSerializer) {
this.peerAddress = Objects.requireNonNull(peerAddress);
protected PeerSocketHandler(MessageSerializer messageSerializer) {
this.serializer = Objects.requireNonNull(messageSerializer);
this.timeoutTask = new SocketTimeoutTask(this::timeoutOccurred);
}
Expand Down Expand Up @@ -220,12 +219,12 @@ public int getMaxMessageSize() {
return Message.MAX_SIZE;
}

// getAddress() is only used for logging and exception messages in PeerSocketHandler
/**
* @return the IP address and port of peer.
*/
public PeerAddress getAddress() {
return peerAddress;
}
@NonNull
public abstract PeerAddress getAddress();

/** Catch any exceptions, logging them and then closing the channel. */
private void exceptionCaught(Exception e) {
Expand Down
1 change: 1 addition & 0 deletions examples/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ plugins {

dependencies {
implementation project(':bitcoinj-core')
implementation 'org.jspecify:jspecify:1.0.0'
implementation 'info.picocli:picocli:4.7.6'
implementation 'org.slf4j:slf4j-jdk14:2.0.16'
}
Expand Down
2 changes: 1 addition & 1 deletion integration-test/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ plugins {
dependencies {
implementation project(':bitcoinj-core')
implementation project(':bitcoinj-examples')
api 'org.jspecify:jspecify:1.0.0'

testImplementation 'org.jspecify:jspecify:1.0.0'
testImplementation 'org.slf4j:slf4j-jdk14:2.0.16'
testImplementation "org.junit.jupiter:junit-jupiter-api:5.11.4"
testImplementation "org.junit.jupiter:junit-jupiter-params:5.11.4"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,16 @@ public abstract class InboundMessageQueuer extends PeerSocketHandler {

@Nullable public Peer peer;
@Nullable public BloomFilter lastReceivedFilter;
private final PeerAddress peerAddress;

protected InboundMessageQueuer(BitcoinSerializer serializer) {
super(PeerAddress.simple(new InetSocketAddress(InetAddress.getLoopbackAddress(), 2000)), serializer);
super(serializer);
peerAddress = PeerAddress.simple(new InetSocketAddress(InetAddress.getLoopbackAddress(), 2000));
}

@Override
public PeerAddress getAddress() {
return peerAddress;
}

public Message nextMessage() {
Expand Down
1 change: 1 addition & 0 deletions test-support/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ plugins {

dependencies {
api project(':bitcoinj-base')
api 'org.jspecify:jspecify:1.0.0'
}

tasks.withType(JavaCompile) {
Expand Down
1 change: 1 addition & 0 deletions tools/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ plugins {

dependencies {
implementation project(':bitcoinj-core')
implementation 'org.jspecify:jspecify:1.0.0'
implementation 'info.picocli:picocli:4.7.6'
implementation 'org.slf4j:slf4j-jdk14:2.0.16'
}
Expand Down
Loading