@@ -1021,6 +1021,53 @@ static RPCHelpMan addpeeraddress()
10211021 };
10221022}
10231023
1024+ static RPCHelpMan sendmsgtopeer ()
1025+ {
1026+ return RPCHelpMan{
1027+ " sendmsgtopeer" ,
1028+ " Send a p2p message to a peer specified by id.\n "
1029+ " The message type and body must be provided, the message header will be generated.\n "
1030+ " This RPC is for testing only." ,
1031+ {
1032+ {" peer_id" , RPCArg::Type::NUM, RPCArg::Optional::NO, " The peer to send the message to." },
1033+ {" msg_type" , RPCArg::Type::STR, RPCArg::Optional::NO, strprintf (" The message type (maximum length %i)" , CMessageHeader::COMMAND_SIZE)},
1034+ {" msg" , RPCArg::Type::STR_HEX, RPCArg::Optional::NO, " The serialized message body to send, in hex, without a message header" },
1035+ },
1036+ RPCResult{RPCResult::Type::NONE, " " , " " },
1037+ RPCExamples{
1038+ HelpExampleCli (" sendmsgtopeer" , " 0 \" addr\" \" ffffff\" " ) + HelpExampleRpc (" sendmsgtopeer" , " 0 \" addr\" \" ffffff\" " )},
1039+ [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue {
1040+ const NodeId peer_id{request.params [0 ].get_int ()};
1041+ const std::string& msg_type{request.params [1 ].get_str ()};
1042+ if (msg_type.size () > CMessageHeader::COMMAND_SIZE) {
1043+ throw JSONRPCError (RPC_INVALID_PARAMETER, strprintf (" Error: msg_type too long, max length is %i" , CMessageHeader::COMMAND_SIZE));
1044+ }
1045+ const std::string& msg{request.params [2 ].get_str ()};
1046+ if (!msg.empty () && !IsHex (msg)) {
1047+ throw JSONRPCError (RPC_INVALID_PARAMETER, " Error parsing input for msg" );
1048+ }
1049+
1050+ NodeContext& node = EnsureAnyNodeContext (request.context );
1051+ CConnman& connman = EnsureConnman (node);
1052+
1053+ CSerializedNetMsg msg_ser;
1054+ msg_ser.data = ParseHex (msg);
1055+ msg_ser.m_type = msg_type;
1056+
1057+ bool success = connman.ForNode (peer_id, [&](CNode* node) {
1058+ connman.PushMessage (node, std::move (msg_ser));
1059+ return true ;
1060+ });
1061+
1062+ if (!success) {
1063+ throw JSONRPCError (RPC_MISC_ERROR, " Error: Could not send message to peer" );
1064+ }
1065+
1066+ return NullUniValue;
1067+ },
1068+ };
1069+ }
1070+
10241071static RPCHelpMan setmnthreadactive ()
10251072{
10261073 return RPCHelpMan{" setmnthreadactive" ,
@@ -1070,6 +1117,7 @@ static const CRPCCommand commands[] =
10701117
10711118 { " hidden" , &addconnection, },
10721119 { " hidden" , &addpeeraddress, },
1120+ { " hidden" , &sendmsgtopeer },
10731121 { " hidden" , &setmnthreadactive },
10741122};
10751123// clang-format on
0 commit comments