Skip to content

Commit

Permalink
Add blacklistblock RPC
Browse files Browse the repository at this point in the history
  • Loading branch information
sipa committed Mar 12, 2013
1 parent 44d7f4c commit ca77397
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/bitcoinrpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ static const CRPCCommand vRPCCommands[] =
{ "createmultisig", &createmultisig, true, true },
{ "getrawmempool", &getrawmempool, true, false },
{ "getblock", &getblock, false, false },
{ "blacklistblock", &blacklistblock, true, false },
{ "getblockhash", &getblockhash, false, false },
{ "gettransaction", &gettransaction, false, false },
{ "listtransactions", &listtransactions, false, false },
Expand Down
1 change: 1 addition & 0 deletions src/bitcoinrpc.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ extern json_spirit::Value settxfee(const json_spirit::Array& params, bool fHelp)
extern json_spirit::Value getrawmempool(const json_spirit::Array& params, bool fHelp);
extern json_spirit::Value getblockhash(const json_spirit::Array& params, bool fHelp);
extern json_spirit::Value getblock(const json_spirit::Array& params, bool fHelp);
extern json_spirit::Value blacklistblock(const json_spirit::Array& params, bool fHelp);
extern json_spirit::Value gettxoutsetinfo(const json_spirit::Array& params, bool fHelp);
extern json_spirit::Value gettxout(const json_spirit::Array& params, bool fHelp);

Expand Down
21 changes: 18 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1208,12 +1208,19 @@ void static InvalidChainFound(CBlockIndex* pindexNew)
printf("InvalidChainFound: Warning: Displayed transactions may not be correct! You may need to upgrade, or other nodes may need to upgrade.\n");
}

void static InvalidBlockFound(CBlockIndex *pindex) {
void InvalidBlockFound(CBlockIndex *pindex) {
pindex->nStatus |= BLOCK_FAILED_VALID;
pblocktree->WriteBlockIndex(CDiskBlockIndex(pindex));
setBlockIndexValid.erase(pindex);
InvalidChainFound(pindex);
CBlockIndex *pindexWalk = pindex;
while (pindexWalk->pnext) {
pindexWalk = pindexWalk->pnext;
pindexWalk->nStatus |= BLOCK_FAILED_CHILD;
printf("Marked %s as descending from invalid\n", BlockHashStr(pindexWalk->GetBlockHash()).c_str());
}
if (pindex->pnext) {
setBlockIndexValid.insert(pindex->pprev);
CValidationState stateDummy;
ConnectBestBlock(stateDummy); // reorganise away from the failed block
}
Expand All @@ -1225,14 +1232,22 @@ bool ConnectBestBlock(CValidationState &state) {

{
std::set<CBlockIndex*,CBlockIndexWorkComparator>::reverse_iterator it = setBlockIndexValid.rbegin();
while (it != setBlockIndexValid.rend() && (*it)->nStatus & BLOCK_FAILED_MASK) {
printf("Not considering failed %s (%i)\n", BlockHashStr((*it)->GetBlockHash()).c_str(), (*it)->nHeight);
it++;
}
if (it == setBlockIndexValid.rend())
return true;
pindexNewBest = *it;
}

if (pindexNewBest == pindexBest || (pindexBest && pindexNewBest->bnChainWork == pindexBest->bnChainWork))
printf("Considering %s (%i)\n", BlockHashStr(pindexNewBest->GetBlockHash()).c_str(), pindexNewBest->nHeight);

if ((pindexNewBest == pindexBest || (pindexBest && pindexNewBest->bnChainWork == pindexBest->bnChainWork)) && !(pindexBest->nStatus & BLOCK_FAILED_MASK))
return true; // nothing to do

printf("Checking ancestry %s (%i)\n", BlockHashStr(pindexNewBest->GetBlockHash()).c_str(), pindexNewBest->nHeight);

// check ancestry
CBlockIndex *pindexTest = pindexNewBest;
std::vector<CBlockIndex*> vAttach;
Expand All @@ -1250,7 +1265,7 @@ bool ConnectBestBlock(CValidationState &state) {
break;
}

if (pindexBest == NULL || pindexTest->bnChainWork > pindexBest->bnChainWork)
if (pindexBest == NULL || pindexTest->bnChainWork > pindexBest->bnChainWork || (pindexBest->nStatus & BLOCK_FAILED_MASK))
vAttach.push_back(pindexTest);

if (pindexTest->pprev == NULL || pindexTest->pnext != NULL) {
Expand Down
1 change: 1 addition & 0 deletions src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ CBlockIndex * InsertBlockIndex(uint256 hash);
bool VerifySignature(const CCoins& txFrom, const CTransaction& txTo, unsigned int nIn, unsigned int flags, int nHashType);
/** Abort with a message */
bool AbortNode(const std::string &msg);
void InvalidBlockFound(CBlockIndex *pindex);



Expand Down
21 changes: 21 additions & 0 deletions src/rpcblockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,27 @@ Value gettxoutsetinfo(const Array& params, bool fHelp)
return ret;
}

Value blacklistblock(const Array &params, bool fHelp)
{
if (fHelp || params.size() < 1)
throw runtime_error(
"blacklistblock <blkid>\n"
"Blacklist a block, and reorganize away from it.\n"
"WARNING: USE ONLY IN EMERGENCY SITUATIONS.\n");

std::string strHash = params[0].get_str();
uint256 hash(strHash);

if (mapBlockIndex.count(hash) == 0)
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found");

CBlockIndex* pblockindex = mapBlockIndex[hash];

InvalidBlockFound(pblockindex);

return Value::null;
}

Value gettxout(const Array& params, bool fHelp)
{
if (fHelp || params.size() < 2 || params.size() > 3)
Expand Down

0 comments on commit ca77397

Please sign in to comment.