You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rpcrawtransaction.cpp 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. // Copyright (c) 2010 Satoshi Nakamoto
  2. // Copyright (c) 2009-2012 The Bitcoin developers
  3. // Distributed under the MIT/X11 software license, see the accompanying
  4. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  5. #include <boost/assign/list_of.hpp>
  6. #include "base58.h"
  7. #include "bitcoinrpc.h"
  8. #include "db.h"
  9. #include "init.h"
  10. #include "main.h"
  11. #include "net.h"
  12. #include "wallet.h"
  13. using namespace std;
  14. using namespace boost;
  15. using namespace boost::assign;
  16. using namespace json_spirit;
  17. //
  18. // Utilities: convert hex-encoded Values
  19. // (throws error if not hex).
  20. //
  21. uint256 ParseHashV(const Value& v, string strName)
  22. {
  23. string strHex;
  24. if (v.type() == str_type)
  25. strHex = v.get_str();
  26. if (!IsHex(strHex)) // Note: IsHex("") is false
  27. throw JSONRPCError(RPC_INVALID_PARAMETER, strName+" must be hexadecimal string (not '"+strHex+"')");
  28. uint256 result;
  29. result.SetHex(strHex);
  30. return result;
  31. }
  32. uint256 ParseHashO(const Object& o, string strKey)
  33. {
  34. return ParseHashV(find_value(o, strKey), strKey);
  35. }
  36. vector<unsigned char> ParseHexV(const Value& v, string strName)
  37. {
  38. string strHex;
  39. if (v.type() == str_type)
  40. strHex = v.get_str();
  41. if (!IsHex(strHex))
  42. throw JSONRPCError(RPC_INVALID_PARAMETER, strName+" must be hexadecimal string (not '"+strHex+"')");
  43. return ParseHex(strHex);
  44. }
  45. vector<unsigned char> ParseHexO(const Object& o, string strKey)
  46. {
  47. return ParseHexV(find_value(o, strKey), strKey);
  48. }
  49. void ScriptPubKeyToJSON(const CScript& scriptPubKey, Object& out)
  50. {
  51. txnouttype type;
  52. vector<CTxDestination> addresses;
  53. int nRequired;
  54. out.push_back(Pair("asm", scriptPubKey.ToString()));
  55. out.push_back(Pair("hex", HexStr(scriptPubKey.begin(), scriptPubKey.end())));
  56. if (!ExtractDestinations(scriptPubKey, type, addresses, nRequired))
  57. {
  58. out.push_back(Pair("type", GetTxnOutputType(TX_NONSTANDARD)));
  59. return;
  60. }
  61. out.push_back(Pair("reqSigs", nRequired));
  62. out.push_back(Pair("type", GetTxnOutputType(type)));
  63. Array a;
  64. BOOST_FOREACH(const CTxDestination& addr, addresses)
  65. a.push_back(CBitcoinAddress(addr).ToString());
  66. out.push_back(Pair("addresses", a));
  67. }
  68. void TxToJSON(const CTransaction& tx, const uint256 hashBlock, Object& entry)
  69. {
  70. entry.push_back(Pair("txid", tx.GetHash().GetHex()));
  71. entry.push_back(Pair("version", tx.nVersion));
  72. entry.push_back(Pair("locktime", (boost::int64_t)tx.nLockTime));
  73. Array vin;
  74. BOOST_FOREACH(const CTxIn& txin, tx.vin)
  75. {
  76. Object in;
  77. if (tx.IsCoinBase())
  78. in.push_back(Pair("coinbase", HexStr(txin.scriptSig.begin(), txin.scriptSig.end())));
  79. else
  80. {
  81. in.push_back(Pair("txid", txin.prevout.hash.GetHex()));
  82. in.push_back(Pair("vout", (boost::int64_t)txin.prevout.n));
  83. Object o;
  84. o.push_back(Pair("asm", txin.scriptSig.ToString()));
  85. o.push_back(Pair("hex", HexStr(txin.scriptSig.begin(), txin.scriptSig.end())));
  86. in.push_back(Pair("scriptSig", o));
  87. }
  88. in.push_back(Pair("sequence", (boost::int64_t)txin.nSequence));
  89. vin.push_back(in);
  90. }
  91. entry.push_back(Pair("vin", vin));
  92. Array vout;
  93. for (unsigned int i = 0; i < tx.vout.size(); i++)
  94. {
  95. const CTxOut& txout = tx.vout[i];
  96. Object out;
  97. out.push_back(Pair("value", ValueFromAmount(txout.nValue)));
  98. out.push_back(Pair("n", (boost::int64_t)i));
  99. Object o;
  100. ScriptPubKeyToJSON(txout.scriptPubKey, o);
  101. out.push_back(Pair("scriptPubKey", o));
  102. vout.push_back(out);
  103. }
  104. entry.push_back(Pair("vout", vout));
  105. if (hashBlock != 0)
  106. {
  107. entry.push_back(Pair("blockhash", hashBlock.GetHex()));
  108. map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hashBlock);
  109. if (mi != mapBlockIndex.end() && (*mi).second)
  110. {
  111. CBlockIndex* pindex = (*mi).second;
  112. if (pindex->IsInMainChain())
  113. {
  114. entry.push_back(Pair("confirmations", 1 + nBestHeight - pindex->nHeight));
  115. entry.push_back(Pair("time", (boost::int64_t)pindex->nTime));
  116. entry.push_back(Pair("blocktime", (boost::int64_t)pindex->nTime));
  117. }
  118. else
  119. entry.push_back(Pair("confirmations", 0));
  120. }
  121. }
  122. }
  123. Value getrawtransaction(const Array& params, bool fHelp)
  124. {
  125. if (fHelp || params.size() < 1 || params.size() > 2)
  126. throw runtime_error(
  127. "getrawtransaction <txid> [verbose=0]\n"
  128. "If verbose=0, returns a string that is\n"
  129. "serialized, hex-encoded data for <txid>.\n"
  130. "If verbose is non-zero, returns an Object\n"
  131. "with information about <txid>.");
  132. uint256 hash = ParseHashV(params[0], "parameter 1");
  133. bool fVerbose = false;
  134. if (params.size() > 1)
  135. fVerbose = (params[1].get_int() != 0);
  136. CTransaction tx;
  137. uint256 hashBlock = 0;
  138. if (!GetTransaction(hash, tx, hashBlock, true))
  139. throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available about transaction");
  140. CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
  141. ssTx << tx;
  142. string strHex = HexStr(ssTx.begin(), ssTx.end());
  143. if (!fVerbose)
  144. return strHex;
  145. Object result;
  146. result.push_back(Pair("hex", strHex));
  147. TxToJSON(tx, hashBlock, result);
  148. return result;
  149. }
  150. Value listunspent(const Array& params, bool fHelp)
  151. {
  152. if (fHelp || params.size() > 3)
  153. throw runtime_error(
  154. "listunspent [minconf=1] [maxconf=9999999] [\"address\",...]\n"
  155. "Returns array of unspent transaction outputs\n"
  156. "with between minconf and maxconf (inclusive) confirmations.\n"
  157. "Optionally filtered to only include txouts paid to specified addresses.\n"
  158. "Results are an array of Objects, each of which has:\n"
  159. "{txid, vout, scriptPubKey, amount, confirmations}");
  160. RPCTypeCheck(params, list_of(int_type)(int_type)(array_type));
  161. int nMinDepth = 1;
  162. if (params.size() > 0)
  163. nMinDepth = params[0].get_int();
  164. int nMaxDepth = 9999999;
  165. if (params.size() > 1)
  166. nMaxDepth = params[1].get_int();
  167. set<CBitcoinAddress> setAddress;
  168. if (params.size() > 2)
  169. {
  170. Array inputs = params[2].get_array();
  171. BOOST_FOREACH(Value& input, inputs)
  172. {
  173. CBitcoinAddress address(input.get_str());
  174. if (!address.IsValid())
  175. throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, string("Invalid Bitcoin address: ")+input.get_str());
  176. if (setAddress.count(address))
  177. throw JSONRPCError(RPC_INVALID_PARAMETER, string("Invalid parameter, duplicated address: ")+input.get_str());
  178. setAddress.insert(address);
  179. }
  180. }
  181. Array results;
  182. vector<COutput> vecOutputs;
  183. pwalletMain->AvailableCoins(vecOutputs, false);
  184. BOOST_FOREACH(const COutput& out, vecOutputs)
  185. {
  186. if (out.nDepth < nMinDepth || out.nDepth > nMaxDepth)
  187. continue;
  188. if (setAddress.size())
  189. {
  190. CTxDestination address;
  191. if (!ExtractDestination(out.tx->vout[out.i].scriptPubKey, address))
  192. continue;
  193. if (!setAddress.count(address))
  194. continue;
  195. }
  196. int64 nValue = out.tx->vout[out.i].nValue;
  197. const CScript& pk = out.tx->vout[out.i].scriptPubKey;
  198. Object entry;
  199. entry.push_back(Pair("txid", out.tx->GetHash().GetHex()));
  200. entry.push_back(Pair("vout", out.i));
  201. entry.push_back(Pair("scriptPubKey", HexStr(pk.begin(), pk.end())));
  202. if (pk.IsPayToScriptHash())
  203. {
  204. CTxDestination address;
  205. if (ExtractDestination(pk, address))
  206. {
  207. const CScriptID& hash = boost::get<const CScriptID&>(address);
  208. CScript redeemScript;
  209. if (pwalletMain->GetCScript(hash, redeemScript))
  210. entry.push_back(Pair("redeemScript", HexStr(redeemScript.begin(), redeemScript.end())));
  211. }
  212. }
  213. entry.push_back(Pair("amount",ValueFromAmount(nValue)));
  214. entry.push_back(Pair("confirmations",out.nDepth));
  215. results.push_back(entry);
  216. }
  217. return results;
  218. }
  219. Value createrawtransaction(const Array& params, bool fHelp)
  220. {
  221. if (fHelp || params.size() != 2)
  222. throw runtime_error(
  223. "createrawtransaction [{\"txid\":txid,\"vout\":n},...] {address:amount,...}\n"
  224. "Create a transaction spending given inputs\n"
  225. "(array of objects containing transaction id and output number),\n"
  226. "sending to given address(es).\n"
  227. "Returns hex-encoded raw transaction.\n"
  228. "Note that the transaction's inputs are not signed, and\n"
  229. "it is not stored in the wallet or transmitted to the network.");
  230. RPCTypeCheck(params, list_of(array_type)(obj_type));
  231. Array inputs = params[0].get_array();
  232. Object sendTo = params[1].get_obj();
  233. CTransaction rawTx;
  234. BOOST_FOREACH(const Value& input, inputs)
  235. {
  236. const Object& o = input.get_obj();
  237. uint256 txid = ParseHashO(o, "txid");
  238. const Value& vout_v = find_value(o, "vout");
  239. if (vout_v.type() != int_type)
  240. throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, missing vout key");
  241. int nOutput = vout_v.get_int();
  242. if (nOutput < 0)
  243. throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, vout must be positive");
  244. CTxIn in(COutPoint(txid, nOutput));
  245. rawTx.vin.push_back(in);
  246. }
  247. set<CBitcoinAddress> setAddress;
  248. BOOST_FOREACH(const Pair& s, sendTo)
  249. {
  250. CBitcoinAddress address(s.name_);
  251. if (!address.IsValid())
  252. throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, string("Invalid Bitcoin address: ")+s.name_);
  253. if (setAddress.count(address))
  254. throw JSONRPCError(RPC_INVALID_PARAMETER, string("Invalid parameter, duplicated address: ")+s.name_);
  255. setAddress.insert(address);
  256. CScript scriptPubKey;
  257. scriptPubKey.SetDestination(address.Get());
  258. int64 nAmount = AmountFromValue(s.value_);
  259. CTxOut out(nAmount, scriptPubKey);
  260. rawTx.vout.push_back(out);
  261. }
  262. CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
  263. ss << rawTx;
  264. return HexStr(ss.begin(), ss.end());
  265. }
  266. Value decoderawtransaction(const Array& params, bool fHelp)
  267. {
  268. if (fHelp || params.size() != 1)
  269. throw runtime_error(
  270. "decoderawtransaction <hex string>\n"
  271. "Return a JSON object representing the serialized, hex-encoded transaction.");
  272. vector<unsigned char> txData(ParseHexV(params[0], "argument"));
  273. CDataStream ssData(txData, SER_NETWORK, PROTOCOL_VERSION);
  274. CTransaction tx;
  275. try {
  276. ssData >> tx;
  277. }
  278. catch (std::exception &e) {
  279. throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "TX decode failed");
  280. }
  281. Object result;
  282. TxToJSON(tx, 0, result);
  283. return result;
  284. }
  285. Value signrawtransaction(const Array& params, bool fHelp)
  286. {
  287. if (fHelp || params.size() < 1 || params.size() > 4)
  288. throw runtime_error(
  289. "signrawtransaction <hex string> [{\"txid\":txid,\"vout\":n,\"scriptPubKey\":hex,\"redeemScript\":hex},...] [<privatekey1>,...] [sighashtype=\"ALL\"]\n"
  290. "Sign inputs for raw transaction (serialized, hex-encoded).\n"
  291. "Second optional argument (may be null) is an array of previous transaction outputs that\n"
  292. "this transaction depends on but may not yet be in the block chain.\n"
  293. "Third optional argument (may be null) is an array of base58-encoded private\n"
  294. "keys that, if given, will be the only keys used to sign the transaction.\n"
  295. "Fourth optional argument is a string that is one of six values; ALL, NONE, SINGLE or\n"
  296. "ALL|ANYONECANPAY, NONE|ANYONECANPAY, SINGLE|ANYONECANPAY.\n"
  297. "Returns json object with keys:\n"
  298. " hex : raw transaction with signature(s) (hex-encoded string)\n"
  299. " complete : 1 if transaction has a complete set of signature (0 if not)"
  300. + HelpRequiringPassphrase());
  301. RPCTypeCheck(params, list_of(str_type)(array_type)(array_type)(str_type), true);
  302. vector<unsigned char> txData(ParseHexV(params[0], "argument 1"));
  303. CDataStream ssData(txData, SER_NETWORK, PROTOCOL_VERSION);
  304. vector<CTransaction> txVariants;
  305. while (!ssData.empty())
  306. {
  307. try {
  308. CTransaction tx;
  309. ssData >> tx;
  310. txVariants.push_back(tx);
  311. }
  312. catch (std::exception &e) {
  313. throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "TX decode failed");
  314. }
  315. }
  316. if (txVariants.empty())
  317. throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "Missing transaction");
  318. // mergedTx will end up with all the signatures; it
  319. // starts as a clone of the rawtx:
  320. CTransaction mergedTx(txVariants[0]);
  321. bool fComplete = true;
  322. // Fetch previous transactions (inputs):
  323. CCoinsView viewDummy;
  324. CCoinsViewCache view(viewDummy);
  325. {
  326. LOCK(mempool.cs);
  327. CCoinsViewCache &viewChain = *pcoinsTip;
  328. CCoinsViewMemPool viewMempool(viewChain, mempool);
  329. view.SetBackend(viewMempool); // temporarily switch cache backend to db+mempool view
  330. BOOST_FOREACH(const CTxIn& txin, mergedTx.vin) {
  331. const uint256& prevHash = txin.prevout.hash;
  332. CCoins coins;
  333. view.GetCoins(prevHash, coins); // this is certainly allowed to fail
  334. }
  335. view.SetBackend(viewDummy); // switch back to avoid locking mempool for too long
  336. }
  337. bool fGivenKeys = false;
  338. CBasicKeyStore tempKeystore;
  339. if (params.size() > 2 && params[2].type() != null_type)
  340. {
  341. fGivenKeys = true;
  342. Array keys = params[2].get_array();
  343. BOOST_FOREACH(Value k, keys)
  344. {
  345. CBitcoinSecret vchSecret;
  346. bool fGood = vchSecret.SetString(k.get_str());
  347. if (!fGood)
  348. throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid private key");
  349. CKey key;
  350. bool fCompressed;
  351. CSecret secret = vchSecret.GetSecret(fCompressed);
  352. key.SetSecret(secret, fCompressed);
  353. tempKeystore.AddKey(key);
  354. }
  355. }
  356. else
  357. EnsureWalletIsUnlocked();
  358. // Add previous txouts given in the RPC call:
  359. if (params.size() > 1 && params[1].type() != null_type)
  360. {
  361. Array prevTxs = params[1].get_array();
  362. BOOST_FOREACH(Value& p, prevTxs)
  363. {
  364. if (p.type() != obj_type)
  365. throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "expected object with {\"txid'\",\"vout\",\"scriptPubKey\"}");
  366. Object prevOut = p.get_obj();
  367. RPCTypeCheck(prevOut, map_list_of("txid", str_type)("vout", int_type)("scriptPubKey", str_type)("redeemScript",str_type));
  368. uint256 txid = ParseHashO(prevOut, "txid");
  369. int nOut = find_value(prevOut, "vout").get_int();
  370. if (nOut < 0)
  371. throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "vout must be positive");
  372. vector<unsigned char> pkData(ParseHexO(prevOut, "scriptPubKey"));
  373. CScript scriptPubKey(pkData.begin(), pkData.end());
  374. CCoins coins;
  375. if (view.GetCoins(txid, coins)) {
  376. if (coins.IsAvailable(nOut) && coins.vout[nOut].scriptPubKey != scriptPubKey) {
  377. string err("Previous output scriptPubKey mismatch:\n");
  378. err = err + coins.vout[nOut].scriptPubKey.ToString() + "\nvs:\n"+
  379. scriptPubKey.ToString();
  380. throw JSONRPCError(RPC_DESERIALIZATION_ERROR, err);
  381. }
  382. // what todo if txid is known, but the actual output isn't?
  383. }
  384. if ((unsigned int)nOut >= coins.vout.size())
  385. coins.vout.resize(nOut+1);
  386. coins.vout[nOut].scriptPubKey = scriptPubKey;
  387. coins.vout[nOut].nValue = 0; // we don't know the actual output value
  388. view.SetCoins(txid, coins);
  389. // if redeemScript given and not using the local wallet (private keys
  390. // given), add redeemScript to the tempKeystore so it can be signed:
  391. Value v = find_value(prevOut, "redeemScript");
  392. if (fGivenKeys && scriptPubKey.IsPayToScriptHash() && !(v == Value::null))
  393. {
  394. vector<unsigned char> rsData(ParseHexV(v, "redeemScript"));
  395. CScript redeemScript(rsData.begin(), rsData.end());
  396. tempKeystore.AddCScript(redeemScript);
  397. }
  398. }
  399. }
  400. const CKeyStore& keystore = (fGivenKeys ? tempKeystore : *pwalletMain);
  401. int nHashType = SIGHASH_ALL;
  402. if (params.size() > 3 && params[3].type() != null_type)
  403. {
  404. static map<string, int> mapSigHashValues =
  405. boost::assign::map_list_of
  406. (string("ALL"), int(SIGHASH_ALL))
  407. (string("ALL|ANYONECANPAY"), int(SIGHASH_ALL|SIGHASH_ANYONECANPAY))
  408. (string("NONE"), int(SIGHASH_NONE))
  409. (string("NONE|ANYONECANPAY"), int(SIGHASH_NONE|SIGHASH_ANYONECANPAY))
  410. (string("SINGLE"), int(SIGHASH_SINGLE))
  411. (string("SINGLE|ANYONECANPAY"), int(SIGHASH_SINGLE|SIGHASH_ANYONECANPAY))
  412. ;
  413. string strHashType = params[3].get_str();
  414. if (mapSigHashValues.count(strHashType))
  415. nHashType = mapSigHashValues[strHashType];
  416. else
  417. throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid sighash param");
  418. }
  419. bool fHashSingle = ((nHashType & ~SIGHASH_ANYONECANPAY) == SIGHASH_SINGLE);
  420. // Sign what we can:
  421. for (unsigned int i = 0; i < mergedTx.vin.size(); i++)
  422. {
  423. CTxIn& txin = mergedTx.vin[i];
  424. CCoins coins;
  425. if (!view.GetCoins(txin.prevout.hash, coins) || !coins.IsAvailable(txin.prevout.n))
  426. {
  427. fComplete = false;
  428. continue;
  429. }
  430. const CScript& prevPubKey = coins.vout[txin.prevout.n].scriptPubKey;
  431. txin.scriptSig.clear();
  432. // Only sign SIGHASH_SINGLE if there's a corresponding output:
  433. if (!fHashSingle || (i < mergedTx.vout.size()))
  434. SignSignature(keystore, prevPubKey, mergedTx, i, nHashType);
  435. // ... and merge in other signatures:
  436. BOOST_FOREACH(const CTransaction& txv, txVariants)
  437. {
  438. txin.scriptSig = CombineSignatures(prevPubKey, mergedTx, i, txin.scriptSig, txv.vin[i].scriptSig);
  439. }
  440. if (!VerifyScript(txin.scriptSig, prevPubKey, mergedTx, i, SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_STRICTENC, 0))
  441. fComplete = false;
  442. }
  443. Object result;
  444. CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
  445. ssTx << mergedTx;
  446. result.push_back(Pair("hex", HexStr(ssTx.begin(), ssTx.end())));
  447. result.push_back(Pair("complete", fComplete));
  448. return result;
  449. }
  450. Value sendrawtransaction(const Array& params, bool fHelp)
  451. {
  452. if (fHelp || params.size() < 1 || params.size() > 1)
  453. throw runtime_error(
  454. "sendrawtransaction <hex string>\n"
  455. "Submits raw transaction (serialized, hex-encoded) to local node and network.");
  456. // parse hex string from parameter
  457. vector<unsigned char> txData(ParseHexV(params[0], "parameter"));
  458. CDataStream ssData(txData, SER_NETWORK, PROTOCOL_VERSION);
  459. CTransaction tx;
  460. // deserialize binary data stream
  461. try {
  462. ssData >> tx;
  463. }
  464. catch (std::exception &e) {
  465. throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "TX decode failed");
  466. }
  467. uint256 hashTx = tx.GetHash();
  468. bool fHave = false;
  469. CCoinsViewCache &view = *pcoinsTip;
  470. CCoins existingCoins;
  471. {
  472. fHave = view.GetCoins(hashTx, existingCoins);
  473. if (!fHave) {
  474. // push to local node
  475. CValidationState state;
  476. if (!tx.AcceptToMemoryPool(state, true, false))
  477. throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "TX rejected"); // TODO: report validation state
  478. }
  479. }
  480. if (fHave) {
  481. if (existingCoins.nHeight < 1000000000)
  482. throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "transaction already in block chain");
  483. // Not in block, but already in the memory pool; will drop
  484. // through to re-relay it.
  485. } else {
  486. SyncWithWallets(hashTx, tx, NULL, true);
  487. }
  488. RelayTransaction(tx, hashTx);
  489. return hashTx.GetHex();
  490. }