Skip to content

Commit 36b1b63

Browse files
author
MarcoFalke
committed
rpc: Expose ProcessNewBlockHeaders
1 parent 3bd25c0 commit 36b1b63

File tree

3 files changed

+54
-1
lines changed

3 files changed

+54
-1
lines changed

src/core_io.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <vector>
1212

1313
class CBlock;
14+
class CBlockHeader;
1415
class CScript;
1516
class CTransaction;
1617
struct CMutableTransaction;
@@ -23,6 +24,7 @@ CScript ParseScript(const std::string& s);
2324
std::string ScriptToAsmStr(const CScript& script, const bool fAttemptSighashDecode = false);
2425
bool DecodeHexTx(CMutableTransaction& tx, const std::string& hex_tx, bool try_no_witness = false, bool try_witness = true);
2526
bool DecodeHexBlk(CBlock&, const std::string& strHexBlk);
27+
bool DecodeHexBlockHeader(CBlockHeader&, const std::string& hex_header);
2628
uint256 ParseHashStr(const std::string&, const std::string& strName);
2729
std::vector<unsigned char> ParseHexUV(const UniValue& v, const std::string& strName);
2830
bool DecodePSBT(PartiallySignedTransaction& psbt, const std::string& base64_tx, std::string& error);

src/core_read.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,20 @@ bool DecodeHexTx(CMutableTransaction& tx, const std::string& hex_tx, bool try_no
145145
return false;
146146
}
147147

148+
bool DecodeHexBlockHeader(CBlockHeader& header, const std::string& hex_header)
149+
{
150+
if (!IsHex(hex_header)) return false;
151+
152+
const std::vector<unsigned char> header_data{ParseHex(hex_header)};
153+
CDataStream ser_header(header_data, SER_NETWORK, PROTOCOL_VERSION);
154+
try {
155+
ser_header >> header;
156+
} catch (const std::exception&) {
157+
return false;
158+
}
159+
return true;
160+
}
161+
148162
bool DecodeHexBlk(CBlock& block, const std::string& strHexBlk)
149163
{
150164
if (!IsHex(strHexBlk))

src/rpc/mining.cpp

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#include <consensus/params.h>
1111
#include <consensus/validation.h>
1212
#include <core_io.h>
13-
#include <validation.h>
1413
#include <key_io.h>
1514
#include <miner.h>
1615
#include <net.h>
@@ -23,6 +22,7 @@
2322
#include <txmempool.h>
2423
#include <util.h>
2524
#include <utilstrencodings.h>
25+
#include <validation.h>
2626
#include <validationinterface.h>
2727
#include <warnings.h>
2828

@@ -763,6 +763,42 @@ static UniValue submitblock(const JSONRPCRequest& request)
763763
return BIP22ValidationResult(sc.state);
764764
}
765765

766+
static UniValue submitheader(const JSONRPCRequest& request)
767+
{
768+
if (request.fHelp || request.params.size() != 1) {
769+
throw std::runtime_error(
770+
"submitheader \"hexdata\"\n"
771+
"\nDecode the given hexdata as a header and submit it as a candidate chain tip if valid."
772+
"\nThrows when the header is invalid.\n"
773+
"\nArguments\n"
774+
"1. \"hexdata\" (string, required) the hex-encoded block header data\n"
775+
"\nResult:\n"
776+
"None"
777+
"\nExamples:\n" +
778+
HelpExampleCli("submitheader", "\"aabbcc\"") +
779+
HelpExampleRpc("submitheader", "\"aabbcc\""));
780+
}
781+
782+
CBlockHeader h;
783+
if (!DecodeHexBlockHeader(h, request.params[0].get_str())) {
784+
throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "Block header decode failed");
785+
}
786+
{
787+
LOCK(cs_main);
788+
if (!LookupBlockIndex(h.hashPrevBlock)) {
789+
throw JSONRPCError(RPC_VERIFY_ERROR, "Must submit previous header (" + h.hashPrevBlock.GetHex() + ") first");
790+
}
791+
}
792+
793+
CValidationState state;
794+
ProcessNewBlockHeaders({h}, state, Params(), /* ppindex */ nullptr, /* first_invalid */ nullptr);
795+
if (state.IsValid()) return NullUniValue;
796+
if (state.IsError()) {
797+
throw JSONRPCError(RPC_VERIFY_ERROR, FormatStateMessage(state));
798+
}
799+
throw JSONRPCError(RPC_VERIFY_ERROR, state.GetRejectReason());
800+
}
801+
766802
static UniValue estimatefee(const JSONRPCRequest& request)
767803
{
768804
throw JSONRPCError(RPC_METHOD_DEPRECATED, "estimatefee was removed in v0.17.\n"
@@ -940,6 +976,7 @@ static const CRPCCommand commands[] =
940976
{ "mining", "prioritisetransaction", &prioritisetransaction, {"txid","dummy","fee_delta"} },
941977
{ "mining", "getblocktemplate", &getblocktemplate, {"template_request"} },
942978
{ "mining", "submitblock", &submitblock, {"hexdata","dummy"} },
979+
{ "mining", "submitheader", &submitheader, {"hexdata"} },
943980

944981

945982
{ "generating", "generatetoaddress", &generatetoaddress, {"nblocks","address","maxtries"} },

0 commit comments

Comments
 (0)