[PIE-2003] Make SyncState variables thread-safe (#95)

Signed-off-by: Meredith Baxter <meredith.baxter@consensys.net>
pull/96/head
mbaxter 5 years ago committed by GitHub
parent 2626687de1
commit 13ae4317b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 14
      ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/sync/state/SyncState.java

@ -25,6 +25,7 @@ import org.hyperledger.besu.plugin.services.BesuEvents.SyncStatusListener;
import org.hyperledger.besu.util.Subscribers; import org.hyperledger.besu.util.Subscribers;
import java.util.Optional; import java.util.Optional;
import java.util.concurrent.atomic.AtomicBoolean;
import com.google.common.annotations.VisibleForTesting; import com.google.common.annotations.VisibleForTesting;
@ -33,12 +34,12 @@ public class SyncState {
private final Blockchain blockchain; private final Blockchain blockchain;
private final EthPeers ethPeers; private final EthPeers ethPeers;
private long startingBlock;
private boolean lastInSync = true;
private final Subscribers<InSyncListener> inSyncListeners = Subscribers.create(); private final Subscribers<InSyncListener> inSyncListeners = Subscribers.create();
private final Subscribers<SyncStatusListener> syncStatusListeners = Subscribers.create(); private final Subscribers<SyncStatusListener> syncStatusListeners = Subscribers.create();
private Optional<SyncTarget> syncTarget = Optional.empty(); private volatile long chainHeightListenerId;
private long chainHeightListenerId; private volatile Optional<SyncTarget> syncTarget = Optional.empty();
private volatile long startingBlock;
private final AtomicBoolean lastInSync = new AtomicBoolean(true);
public SyncState(final Blockchain blockchain, final EthPeers ethPeers) { public SyncState(final Blockchain blockchain, final EthPeers ethPeers) {
this.blockchain = blockchain; this.blockchain = blockchain;
@ -139,7 +140,7 @@ public class SyncState {
replaceSyncTarget(Optional.empty()); replaceSyncTarget(Optional.empty());
} }
private void replaceSyncTarget(final Optional<SyncTarget> newTarget) { private synchronized void replaceSyncTarget(final Optional<SyncTarget> newTarget) {
syncTarget.ifPresent(this::removeEstimatedHeightListener); syncTarget.ifPresent(this::removeEstimatedHeightListener);
syncTarget = newTarget; syncTarget = newTarget;
newTarget.ifPresent(this::addEstimatedHeightListener); newTarget.ifPresent(this::addEstimatedHeightListener);
@ -166,8 +167,7 @@ public class SyncState {
private synchronized void checkInSync() { private synchronized void checkInSync() {
final boolean currentInSync = isInSync(); final boolean currentInSync = isInSync();
if (lastInSync != currentInSync) { if (lastInSync.compareAndSet(!currentInSync, currentInSync)) {
lastInSync = currentInSync;
if (!currentInSync) { if (!currentInSync) {
// when we fall out of sync change our starting block // when we fall out of sync change our starting block
startingBlock = blockchain.getChainHeadBlockNumber(); startingBlock = blockchain.getChainHeadBlockNumber();

Loading…
Cancel
Save