Skip to content

Commit 72bc7e1

Browse files
committed
Merge #10146: Better error handling for submitblock
30f30c0 Add braces to submitblock per current style. (Gregory Maxwell) 4f15ea1 Check transaction count early in submitblock. (Gregory Maxwell) ada0caa Make GetWitnessCommitmentIndex callable on blocks without a coinbase txn. (Gregory Maxwell) Tree-SHA512: 02dcd337ad9cdd8e4fa6a42c009d016026d1229c193676ed6fcc9ce55e924fedec57f516ac1e95c3db0985243ba908307338ce783a70416cb292bed881002bfc
2 parents 471ed00 + 30f30c0 commit 72bc7e1

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

src/rpc/mining.cpp

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,7 @@ class submitblock_StateCatcher : public CValidationInterface
720720

721721
UniValue submitblock(const JSONRPCRequest& request)
722722
{
723-
if (request.fHelp || request.params.size() < 1 || request.params.size() > 2)
723+
if (request.fHelp || request.params.size() < 1 || request.params.size() > 2) {
724724
throw std::runtime_error(
725725
"submitblock \"hexdata\" ( \"jsonparametersobject\" )\n"
726726
"\nAttempts to submit new block to network.\n"
@@ -738,11 +738,17 @@ UniValue submitblock(const JSONRPCRequest& request)
738738
+ HelpExampleCli("submitblock", "\"mydata\"")
739739
+ HelpExampleRpc("submitblock", "\"mydata\"")
740740
);
741+
}
741742

742743
std::shared_ptr<CBlock> blockptr = std::make_shared<CBlock>();
743744
CBlock& block = *blockptr;
744-
if (!DecodeHexBlk(block, request.params[0].get_str()))
745+
if (!DecodeHexBlk(block, request.params[0].get_str())) {
745746
throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "Block decode failed");
747+
}
748+
749+
if (block.vtx.empty() || !block.vtx[0]->IsCoinBase()) {
750+
throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "Block does not start with a coinbase");
751+
}
746752

747753
uint256 hash = block.GetHash();
748754
bool fBlockPresent = false;
@@ -751,10 +757,12 @@ UniValue submitblock(const JSONRPCRequest& request)
751757
BlockMap::iterator mi = mapBlockIndex.find(hash);
752758
if (mi != mapBlockIndex.end()) {
753759
CBlockIndex *pindex = mi->second;
754-
if (pindex->IsValid(BLOCK_VALID_SCRIPTS))
760+
if (pindex->IsValid(BLOCK_VALID_SCRIPTS)) {
755761
return "duplicate";
756-
if (pindex->nStatus & BLOCK_FAILED_MASK)
762+
}
763+
if (pindex->nStatus & BLOCK_FAILED_MASK) {
757764
return "duplicate-invalid";
765+
}
758766
// Otherwise, we might only have the header - process the block before returning
759767
fBlockPresent = true;
760768
}
@@ -772,14 +780,15 @@ UniValue submitblock(const JSONRPCRequest& request)
772780
RegisterValidationInterface(&sc);
773781
bool fAccepted = ProcessNewBlock(Params(), blockptr, true, NULL);
774782
UnregisterValidationInterface(&sc);
775-
if (fBlockPresent)
776-
{
777-
if (fAccepted && !sc.found)
783+
if (fBlockPresent) {
784+
if (fAccepted && !sc.found) {
778785
return "duplicate-inconclusive";
786+
}
779787
return "duplicate";
780788
}
781-
if (!sc.found)
789+
if (!sc.found) {
782790
return "inconclusive";
791+
}
783792
return BIP22ValidationResult(sc.state);
784793
}
785794

src/validation.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2901,9 +2901,11 @@ bool IsWitnessEnabled(const CBlockIndex* pindexPrev, const Consensus::Params& pa
29012901
static int GetWitnessCommitmentIndex(const CBlock& block)
29022902
{
29032903
int commitpos = -1;
2904-
for (size_t o = 0; o < block.vtx[0]->vout.size(); o++) {
2905-
if (block.vtx[0]->vout[o].scriptPubKey.size() >= 38 && block.vtx[0]->vout[o].scriptPubKey[0] == OP_RETURN && block.vtx[0]->vout[o].scriptPubKey[1] == 0x24 && block.vtx[0]->vout[o].scriptPubKey[2] == 0xaa && block.vtx[0]->vout[o].scriptPubKey[3] == 0x21 && block.vtx[0]->vout[o].scriptPubKey[4] == 0xa9 && block.vtx[0]->vout[o].scriptPubKey[5] == 0xed) {
2906-
commitpos = o;
2904+
if (!block.vtx.empty()) {
2905+
for (size_t o = 0; o < block.vtx[0]->vout.size(); o++) {
2906+
if (block.vtx[0]->vout[o].scriptPubKey.size() >= 38 && block.vtx[0]->vout[o].scriptPubKey[0] == OP_RETURN && block.vtx[0]->vout[o].scriptPubKey[1] == 0x24 && block.vtx[0]->vout[o].scriptPubKey[2] == 0xaa && block.vtx[0]->vout[o].scriptPubKey[3] == 0x21 && block.vtx[0]->vout[o].scriptPubKey[4] == 0xa9 && block.vtx[0]->vout[o].scriptPubKey[5] == 0xed) {
2907+
commitpos = o;
2908+
}
29072909
}
29082910
}
29092911
return commitpos;

0 commit comments

Comments
 (0)