11
11
#include " init.h"
12
12
#include " keystore.h"
13
13
#include " validation.h"
14
+ #include " validationinterface.h"
14
15
#include " merkleblock.h"
15
16
#include " net.h"
16
17
#include " policy/policy.h"
30
31
#include " wallet/wallet.h"
31
32
#endif
32
33
34
+ #include < future>
33
35
#include < stdint.h>
34
36
35
37
#include < univalue.h>
@@ -917,7 +919,9 @@ UniValue sendrawtransaction(const JSONRPCRequest& request)
917
919
);
918
920
919
921
ObserveSafeMode ();
920
- LOCK (cs_main);
922
+
923
+ std::promise<void > promise;
924
+
921
925
RPCTypeCheck (request.params , {UniValue::VSTR, UniValue::VBOOL});
922
926
923
927
// parse hex string from parameter
@@ -931,6 +935,8 @@ UniValue sendrawtransaction(const JSONRPCRequest& request)
931
935
if (!request.params [1 ].isNull () && request.params [1 ].get_bool ())
932
936
nMaxRawTxFee = 0 ;
933
937
938
+ { // cs_main scope
939
+ LOCK (cs_main);
934
940
CCoinsViewCache &view = *pcoinsTip;
935
941
bool fHaveChain = false ;
936
942
for (size_t o = 0 ; !fHaveChain && o < tx->vout .size (); o++) {
@@ -952,10 +958,24 @@ UniValue sendrawtransaction(const JSONRPCRequest& request)
952
958
}
953
959
throw JSONRPCError (RPC_TRANSACTION_ERROR, state.GetRejectReason ());
954
960
}
961
+ } else {
962
+ // If wallet is enabled, ensure that the wallet has been made aware
963
+ // of the new transaction prior to returning. This prevents a race
964
+ // where a user might call sendrawtransaction with a transaction
965
+ // to/from their wallet, immediately call some wallet RPC, and get
966
+ // a stale result because callbacks have not yet been processed.
967
+ CallFunctionInValidationInterfaceQueue ([&promise] {
968
+ promise.set_value ();
969
+ });
955
970
}
956
971
} else if (fHaveChain ) {
957
972
throw JSONRPCError (RPC_TRANSACTION_ALREADY_IN_CHAIN, " transaction already in block chain" );
958
973
}
974
+
975
+ } // cs_main
976
+
977
+ promise.get_future ().wait ();
978
+
959
979
if (!g_connman)
960
980
throw JSONRPCError (RPC_CLIENT_P2P_DISABLED, " Error: Peer-to-peer functionality missing or disabled" );
961
981
@@ -964,6 +984,7 @@ UniValue sendrawtransaction(const JSONRPCRequest& request)
964
984
{
965
985
pnode->PushInventory (inv);
966
986
});
987
+
967
988
return hashTx.GetHex ();
968
989
}
969
990
0 commit comments