Skip to content

Commit 3dcbb9b

Browse files
committed
Abstract DecodeHexBlk and BIP22ValidationResult functions out of submitblock
1 parent 132ea9b commit 3dcbb9b

File tree

3 files changed

+41
-21
lines changed

3 files changed

+41
-21
lines changed

src/core_io.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <string>
99
#include <vector>
1010

11+
class CBlock;
1112
class CScript;
1213
class CTransaction;
1314
class uint256;
@@ -16,6 +17,7 @@ class UniValue;
1617
// core_read.cpp
1718
extern CScript ParseScript(std::string s);
1819
extern bool DecodeHexTx(CTransaction& tx, const std::string& strHexTx);
20+
extern bool DecodeHexBlk(CBlock&, const std::string& strHexBlk);
1921
extern uint256 ParseHashUV(const UniValue& v, const std::string& strName);
2022
extern std::vector<unsigned char> ParseHexUV(const UniValue& v, const std::string& strName);
2123

src/core_read.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "core_io.h"
66

7+
#include "core/block.h"
78
#include "core/transaction.h"
89
#include "script/script.h"
910
#include "serialize.h"
@@ -108,6 +109,23 @@ bool DecodeHexTx(CTransaction& tx, const std::string& strHexTx)
108109
return true;
109110
}
110111

112+
bool DecodeHexBlk(CBlock& block, const std::string& strHexBlk)
113+
{
114+
if (!IsHex(strHexBlk))
115+
return false;
116+
117+
std::vector<unsigned char> blockData(ParseHex(strHexBlk));
118+
CDataStream ssBlock(blockData, SER_NETWORK, PROTOCOL_VERSION);
119+
try {
120+
ssBlock >> block;
121+
}
122+
catch (const std::exception &) {
123+
return false;
124+
}
125+
126+
return true;
127+
}
128+
111129
uint256 ParseHashUV(const UniValue& v, const string& strName)
112130
{
113131
string strHex;

src/rpcmining.cpp

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,25 @@ Value prioritisetransaction(const Array& params, bool fHelp)
283283
}
284284

285285

286+
// NOTE: Assumes a conclusive result; if result is inconclusive, it must be handled by caller
287+
static Value BIP22ValidationResult(const CValidationState& state)
288+
{
289+
if (state.IsValid())
290+
return Value::null;
291+
292+
std::string strRejectReason = state.GetRejectReason();
293+
if (state.IsError())
294+
throw JSONRPCError(RPC_VERIFY_ERROR, strRejectReason);
295+
if (state.IsInvalid())
296+
{
297+
if (strRejectReason.empty())
298+
return "rejected";
299+
return strRejectReason;
300+
}
301+
// Should be impossible
302+
return "valid?";
303+
}
304+
286305
Value getblocktemplate(const Array& params, bool fHelp)
287306
{
288307
if (fHelp || params.size() > 1)
@@ -566,15 +585,9 @@ Value submitblock(const Array& params, bool fHelp)
566585
+ HelpExampleRpc("submitblock", "\"mydata\"")
567586
);
568587

569-
vector<unsigned char> blockData(ParseHex(params[0].get_str()));
570-
CDataStream ssBlock(blockData, SER_NETWORK, PROTOCOL_VERSION);
571588
CBlock pblock;
572-
try {
573-
ssBlock >> pblock;
574-
}
575-
catch (const std::exception &) {
589+
if (!DecodeHexBlk(pblock, params[0].get_str()))
576590
throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "Block decode failed");
577-
}
578591

579592
CValidationState state;
580593
submitblock_StateCatcher sc(pblock.GetHash());
@@ -587,20 +600,7 @@ Value submitblock(const Array& params, bool fHelp)
587600
return "inconclusive";
588601
state = sc.state;
589602
}
590-
if (state.IsError())
591-
{
592-
std::string strRejectReason = state.GetRejectReason();
593-
throw JSONRPCError(RPC_VERIFY_ERROR, strRejectReason);
594-
}
595-
if (state.IsInvalid())
596-
{
597-
std::string strRejectReason = state.GetRejectReason();
598-
if (strRejectReason.empty())
599-
return "rejected";
600-
return strRejectReason;
601-
}
602-
603-
return Value::null;
603+
return BIP22ValidationResult(state);
604604
}
605605

606606
Value estimatefee(const Array& params, bool fHelp)

0 commit comments

Comments
 (0)