Skip to content

Commit

Permalink
Exit after fail + 5 rules
Browse files Browse the repository at this point in the history
  • Loading branch information
TheBlueMatt committed Oct 2, 2014
1 parent 82f8c0e commit 0f7b5d8
Showing 1 changed file with 30 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -220,10 +220,7 @@ public Message onPreMessageReceived(Peer peer, Message m) {
locator.add(params.getGenesisBlock().getHash());
Sha256Hash hashTo = new Sha256Hash("0000000000000000000000000000000000000000000000000000000000000000");

int differingBlocks = 0;
int invalidBlocks = 0;
int mempoolRulesFailed = 0;
Sha256Hash lastBlockTestedHash = null;
int rulesSinceFirstFail = 0;
for (Rule rule : blockList.list) {
if (rule instanceof BlockAndValidity) {
BlockAndValidity block = (BlockAndValidity) rule;
Expand All @@ -244,35 +241,34 @@ public Message onPreMessageReceived(Peer peer, Message m) {
throw e;
}
}
lastBlockTestedHash = block.blockHash;
currentBlock.block = nextBlock;
log.info("Testing block {} {}", block.ruleName, currentBlock.block.getHash());
try {
if (chain.add(nextBlock) != block.connects) {
log.error("Block didn't match connects flag on block \"" + block.ruleName + "\"");
invalidBlocks++;
log.error("ERROR: Block didn't match connects flag on block \"" + block.ruleName + "\"");
rulesSinceFirstFail++;
}
} catch (VerificationException e) {
threw = true;
if (!block.throwsException) {
log.error("Block didn't match throws flag on block \"" + block.ruleName + "\"");
log.error("ERROR: Block didn't match throws flag on block \"" + block.ruleName + "\"");
e.printStackTrace();
invalidBlocks++;
rulesSinceFirstFail++;
} else if (block.connects) {
log.error("Block didn't match connects flag on block \"" + block.ruleName + "\"");
log.error("ERROR: Block didn't match connects flag on block \"" + block.ruleName + "\"");
e.printStackTrace();
invalidBlocks++;
rulesSinceFirstFail++;
}
}
if (!threw && block.throwsException) {
log.error("Block didn't match throws flag on block \"" + block.ruleName + "\"");
invalidBlocks++;
log.error("ERROR: Block didn't match throws flag on block \"" + block.ruleName + "\"");
rulesSinceFirstFail++;
} else if (!chain.getChainHead().getHeader().getHash().equals(block.hashChainTipAfterBlock)) {
log.error("New block head didn't match the correct value after block \"" + block.ruleName + "\"");
invalidBlocks++;
log.error("ERROR: New block head didn't match the correct value after block \"" + block.ruleName + "\"");
rulesSinceFirstFail++;
} else if (chain.getChainHead().getHeight() != block.heightAfterBlock) {
log.error("New block head didn't match the correct height after block " + block.ruleName);
invalidBlocks++;
log.error("ERROR: New block head didn't match the correct height after block " + block.ruleName);
rulesSinceFirstFail++;
}

// Shouldnt double-request
Expand Down Expand Up @@ -301,8 +297,8 @@ public Message onPreMessageReceived(Peer peer, Message m) {
if (shouldntRequest) {
Thread.sleep(100);
if (blocksRequested.contains(nextBlock.getHash())) {
log.error("bitcoind re-requested block " + block.ruleName + " with hash " + nextBlock.getHash());
invalidBlocks++;
log.error("ERROR: bitcoind re-requested block " + block.ruleName + " with hash " + nextBlock.getHash());
rulesSinceFirstFail++;
}
}
// If the block throws, we may want to get bitcoind to request the same block again
Expand All @@ -314,8 +310,8 @@ public Message onPreMessageReceived(Peer peer, Message m) {
bitcoind.sendMessage(new GetHeadersMessage(params, locator, hashTo));
bitcoind.ping().get();
if (!chain.getChainHead().getHeader().getHash().equals(bitcoindChainHead)) {
differingBlocks++;
log.error("bitcoind and bitcoinj acceptance differs on block \"" + block.ruleName + "\"");
rulesSinceFirstFail++;
log.error("ERROR: bitcoind and bitcoinj acceptance differs on block \"" + block.ruleName + "\"");
}
if (block.sendOnce)
preloadedBlocks.remove(nextBlock.getHash());
Expand All @@ -325,11 +321,11 @@ public Message onPreMessageReceived(Peer peer, Message m) {
bitcoind.sendMessage(message);
bitcoind.ping().get();
if (mostRecentInv == null && !((MemoryPoolState) rule).mempool.isEmpty()) {
log.error("bitcoind had an empty mempool, but we expected some transactions on rule " + rule.ruleName);
mempoolRulesFailed++;
log.error("ERROR: bitcoind had an empty mempool, but we expected some transactions on rule " + rule.ruleName);
rulesSinceFirstFail++;
} else if (mostRecentInv != null && ((MemoryPoolState) rule).mempool.isEmpty()) {
log.error("bitcoind had a non-empty mempool, but we expected an empty one on rule " + rule.ruleName);
mempoolRulesFailed++;
log.error("ERROR: bitcoind had a non-empty mempool, but we expected an empty one on rule " + rule.ruleName);
rulesSinceFirstFail++;
} else if (mostRecentInv != null) {
Set<InventoryItem> originalRuleSet = new HashSet<InventoryItem>(((MemoryPoolState)rule).mempool);
boolean matches = mostRecentInv.items.size() == ((MemoryPoolState)rule).mempool.size();
Expand All @@ -345,19 +341,21 @@ public Message onPreMessageReceived(Peer peer, Message m) {
log.info(" The expected mempool was: ");
for (InventoryItem item : originalRuleSet)
log.info(" " + item.hash);
mempoolRulesFailed++;
rulesSinceFirstFail++;
}
mostRecentInv = null;
} else {
throw new RuntimeException("Unknown rule");
}
if (rulesSinceFirstFail > 0)
rulesSinceFirstFail++;
if (rulesSinceFirstFail > 6)
System.exit(1);
}

log.info("Done testing.\n" +
"Blocks which were not handled the same between bitcoind/bitcoinj: " + differingBlocks + "\n" +
"Blocks which should/should not have been accepted but weren't/were: " + invalidBlocks + "\n" +
"Transactions which were/weren't in memory pool but shouldn't/should have been: " + mempoolRulesFailed + "\n" +
"Unexpected inv messages: " + unexpectedInvs.get());
System.exit(differingBlocks > 0 || invalidBlocks > 0 || mempoolRulesFailed > 0 || unexpectedInvs.get() > 0 ? 1 : 0);
if (unexpectedInvs.get() > 0)
log.error("ERROR: Got " + unexpectedInvs.get() + " unexpected invs from bitcoind");
log.info("Done testing.");
System.exit(rulesSinceFirstFail > 0 || unexpectedInvs.get() > 0 ? 1 : 0);
}
}

0 comments on commit 0f7b5d8

Please sign in to comment.