Skip to content

Commit

Permalink
checking for negative values and null starting blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandraRoatis committed Feb 4, 2019
1 parent 072c0e2 commit c3f5431
Showing 1 changed file with 99 additions and 85 deletions.
184 changes: 99 additions & 85 deletions modAionImpl/src/org/aion/zero/impl/db/RecoveryUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,11 @@ public static void pruneOrRecoverState(String pruning_type) {
* them differently.
*/
public static void redoMainChainImport(long startHeight) {
if (startHeight < 0) {
System.out.println("Negative values are not valid as starting height. Nothing to do.");
return;
}

// ensure mining is disabled
CfgAion cfg = CfgAion.inst();
cfg.dbFromXML();
Expand Down Expand Up @@ -440,104 +445,113 @@ public static void redoMainChainImport(long startHeight) {
currentBlock = startHeight;
}

chain.setBestBlock(startBlock);

long topBlockNumber = block.getNumber();
long stepSize = 10_000L;
boolean fail = false;

Pair<ImportResult, AionBlockSummary> result;
final int THOUSAND_MS = 1000;
if (startBlock == null) {
System.out.println(
"The main chain block at level "
+ currentBlock
+ " is missing from the database. Cannot continue importing stored blocks.");
fail = true;
} else {
chain.setBestBlock(startBlock);

boolean fail = false;
long topBlockNumber = block.getNumber();
long stepSize = 10_000L;

long start = System.currentTimeMillis();
Pair<ImportResult, AionBlockSummary> result;
final int THOUSAND_MS = 1000;

// import in increments of 10k blocks
while (currentBlock <= topBlockNumber) {
block = store.getChainBlockByNumber(currentBlock);
if (block == null) {
System.out.println(
"The main chain block at level "
+ currentBlock
+ " is missing from the database. Cannot continue importing stored blocks.");
fail = true;
break;
}
long start = System.currentTimeMillis();

try {
result =
chain.tryToConnectAndFetchSummary(
block, System.currentTimeMillis() / THOUSAND_MS, false);
} catch (Throwable t) {
// we want to see the exception and the block where it occurred
t.printStackTrace();
if (t.getMessage() != null
&& t.getMessage().contains("Invalid Trie state, missing node ")) {
// import in increments of 10k blocks
while (currentBlock <= topBlockNumber) {
block = store.getChainBlockByNumber(currentBlock);
if (block == null) {
System.out.println(
"The exception above is likely due to a pruned database and NOT a consensus problem.\n"
+ "Rebuild the full state by editing the config.xml file or running ./aion.sh --state FULL.\n");
"The main chain block at level "
+ currentBlock
+ " is missing from the database. Cannot continue importing stored blocks.");
fail = true;
break;
}
result =
new Pair<>() {
@Override
public AionBlockSummary setValue(AionBlockSummary value) {
return null;
}

@Override
public ImportResult getLeft() {
return ImportResult.INVALID_BLOCK;
}

@Override
public AionBlockSummary getRight() {
return null;
}
};

fail = true;
}

if (!result.getLeft().isSuccessful()) {
System.out.println("Consensus break at block:\n" + block);
System.out.println(
"Import attempt returned result "
+ result.getLeft()
+ " with summary\n"
+ result.getRight());

if (repo.isValidRoot(store.getBestBlock().getStateRoot())) {
System.out.println("The repository state trie was:\n");
System.out.println(repo.getTrieDump());
try {
result =
chain.tryToConnectAndFetchSummary(
block, System.currentTimeMillis() / THOUSAND_MS, false);
} catch (Throwable t) {
// we want to see the exception and the block where it occurred
t.printStackTrace();
if (t.getMessage() != null
&& t.getMessage().contains("Invalid Trie state, missing node ")) {
System.out.println(
"The exception above is likely due to a pruned database and NOT a consensus problem.\n"
+ "Rebuild the full state by editing the config.xml file or running ./aion.sh --state FULL.\n");
}
result =
new Pair<>() {
@Override
public AionBlockSummary setValue(AionBlockSummary value) {
return null;
}

@Override
public ImportResult getLeft() {
return ImportResult.INVALID_BLOCK;
}

@Override
public AionBlockSummary getRight() {
return null;
}
};

fail = true;
}

fail = true;
break;
}
if (!result.getLeft().isSuccessful()) {
System.out.println("Consensus break at block:\n" + block);
System.out.println(
"Import attempt returned result "
+ result.getLeft()
+ " with summary\n"
+ result.getRight());

if (repo.isValidRoot(store.getBestBlock().getStateRoot())) {
System.out.println("The repository state trie was:\n");
System.out.println(repo.getTrieDump());
}

fail = true;
break;
}

if (currentBlock % stepSize == 0) {
long time = System.currentTimeMillis() - start;
if (currentBlock % stepSize == 0) {
long time = System.currentTimeMillis() - start;

double timePerBlock = time / currentBlock;
long remainingBlocks = topBlockNumber - currentBlock;
double estimate = (timePerBlock * remainingBlocks) / 60_000 + 1; // in minutes
System.out.println(
"Finished with blocks up to "
+ currentBlock
+ " in "
+ time
+ " ms (under "
+ (time / 60_000 + 1)
+ " min). The average time per block is < "
+ String.format("%.0f", timePerBlock + 1)
+ " ms. Completion for remaining "
+ remainingBlocks
+ " blocks estimated to take "
+ String.format("%.0f", estimate)
+ " min.");
}
double timePerBlock = time / currentBlock;
long remainingBlocks = topBlockNumber - currentBlock;
double estimate =
(timePerBlock * remainingBlocks) / 60_000 + 1; // in minutes
System.out.println(
"Finished with blocks up to "
+ currentBlock
+ " in "
+ time
+ " ms (under "
+ (time / 60_000 + 1)
+ " min). The average time per block is < "
+ String.format("%.0f", timePerBlock + 1)
+ " ms. Completion for remaining "
+ remainingBlocks
+ " blocks estimated to take "
+ String.format("%.0f", estimate)
+ " min.");
}

currentBlock++;
currentBlock++;
}
}

if (fail) {
Expand Down

0 comments on commit c3f5431

Please sign in to comment.