Skip to content

Commit

Permalink
Panda prove ments (hyperledger#4267)
Browse files Browse the repository at this point in the history
* breaks pandas up, test coverage

Signed-off-by: Justin Florentine <justin+github@florentine.us>
  • Loading branch information
jflo authored and freemanzMrojo committed Aug 19, 2022
1 parent 5bb9a30 commit 95f95a1
Show file tree
Hide file tree
Showing 8 changed files with 298 additions and 216 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.hyperledger.besu.config.GenesisConfigOptions;
import org.hyperledger.besu.consensus.merge.FinalizedBlockHashSupplier;
import org.hyperledger.besu.consensus.merge.MergeContext;
import org.hyperledger.besu.consensus.merge.PandaPrinter;
import org.hyperledger.besu.consensus.qbft.pki.PkiBlockCreationConfiguration;
import org.hyperledger.besu.crypto.NodeKey;
import org.hyperledger.besu.datatypes.Hash;
Expand Down Expand Up @@ -421,6 +422,8 @@ public BesuController build() {
ethProtocolManager,
pivotBlockSelector);

synchronizer.subscribeInSync(new PandaPrinter());

final MiningCoordinator miningCoordinator =
createMiningCoordinator(
protocolSchedule,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ public BesuControllerBuilder storageProvider(final StorageProvider storageProvid
@Override
public BesuController build() {
BesuController controller = super.build();
PandaPrinter.hasTTD();
PostMergeContext.get().setSyncState(controller.getSyncState());
return controller;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,37 @@

package org.hyperledger.besu.consensus.merge;

import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.ethereum.core.Difficulty;
import org.hyperledger.besu.ethereum.core.Synchronizer.InSyncListener;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicBoolean;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class PandaPrinter {
public class PandaPrinter implements InSyncListener, ForkchoiceMessageListener, MergeStateHandler {

private static final Logger LOG = LoggerFactory.getLogger(PandaPrinter.class);
private static final String pandaBanner = PandaPrinter.loadBanner();
private static final AtomicBoolean beenDisplayed = new AtomicBoolean();
private static final String readyBanner = PandaPrinter.loadBanner("/readyPanda.txt");
private static final String ttdBanner = PandaPrinter.loadBanner("/ttdPanda.txt");
private static final String finalizedBanner = PandaPrinter.loadBanner("/finalizedPanda.txt");

private static final AtomicBoolean hasTTD = new AtomicBoolean(false);
private static final AtomicBoolean inSync = new AtomicBoolean(false);
public static final AtomicBoolean readyBeenDisplayed = new AtomicBoolean();
public static final AtomicBoolean ttdBeenDisplayed = new AtomicBoolean();
public static final AtomicBoolean finalizedBeenDisplayed = new AtomicBoolean();

private static String loadBanner() {
private static String loadBanner(final String filename) {
Class<PandaPrinter> c = PandaPrinter.class;
InputStream is = c.getResourceAsStream("/ProofOfPanda3.txt");
InputStream is = c.getResourceAsStream(filename);
StringBuilder resultStringBuilder = new StringBuilder();
try (BufferedReader br =
new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8))) {
Expand All @@ -48,19 +60,71 @@ private static String loadBanner() {
return resultStringBuilder.toString();
}

public static boolean printOnFirstCrossing() {
boolean shouldPrint = beenDisplayed.compareAndSet(false, true);
if (shouldPrint) {
LOG.info("\n" + pandaBanner);
public static void hasTTD() {
PandaPrinter.hasTTD.getAndSet(true);
if (hasTTD.get() && inSync.get()) {
printReadyToMerge();
}
}

public static void inSync() {
PandaPrinter.inSync.getAndSet(true);
if (inSync.get() && hasTTD.get()) {
printReadyToMerge();
}
return shouldPrint;
}

static boolean hasDisplayed() {
return beenDisplayed.get();
public static void printOnFirstCrossing() {
if (!ttdBeenDisplayed.get()) {
LOG.info("\n" + ttdBanner);
}
ttdBeenDisplayed.compareAndSet(false, true);
}

static void resetForTesting() {
beenDisplayed.set(false);
ttdBeenDisplayed.set(false);
readyBeenDisplayed.set(false);
finalizedBeenDisplayed.set(false);
}

public static void printReadyToMerge() {
if (!readyBeenDisplayed.get()) {
LOG.info("\n" + readyBanner);
}
readyBeenDisplayed.compareAndSet(false, true);
}

public static void printFinalized() {
if (!finalizedBeenDisplayed.get()) {
LOG.info("\n" + finalizedBanner);
}
finalizedBeenDisplayed.compareAndSet(false, true);
}

@Override
public void onInSyncStatusChange(final boolean newSyncStatus) {
if (newSyncStatus && hasTTD.get()) {
printReadyToMerge();
}
}

@Override
public void onNewForkchoiceMessage(
final Hash headBlockHash,
final Optional<Hash> maybeFinalizedBlockHash,
final Hash safeBlockHash) {
if (maybeFinalizedBlockHash.isPresent() && !maybeFinalizedBlockHash.get().equals(Hash.ZERO)) {
printFinalized();
}
}

@Override
public void mergeStateChanged(
final boolean isPoS,
final Optional<Boolean> priorState,
final Optional<Difficulty> difficultyStoppedAt) {
if (isPoS && priorState.isPresent() && !priorState.get()) { // just crossed from PoW to PoS
printOnFirstCrossing();
}
}
}
Loading

0 comments on commit 95f95a1

Please sign in to comment.