Advertisement
Guest User

4diff2-0.6.0

a guest
Apr 27th, 2012
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.50 KB | None | 0 0
  1. diff -Nurp src/bitcoinrpc.cpp src2/bitcoinrpc.cpp
  2. --- src/bitcoinrpc.cpp 2012-04-26 18:04:42.161964416 -0700
  3. +++ src2/bitcoinrpc.cpp 2012-04-26 18:08:55.443529152 -0700
  4. @@ -1786,6 +1786,59 @@ Value validateaddress(const Array& param
  5. return ret;
  6. }
  7.  
  8. +static boost::recursive_mutex mGetWork;
  9. +static map<uint256, pair<CBlock*, CScript> > mapGetWorkNewBlock;
  10. +static CReserveKey *pGetWorkReserveKey = NULL;
  11. +
  12. +void GetWorkBlock(char *pmidstate, char *pdata, char *phash1, uint256 &hashTarget)
  13. +{ // Fill the buffers with a correct work unit
  14. + boost::unique_lock<boost::recursive_mutex> lock(mGetWork);
  15. + static vector<CBlock*> vNewBlock;
  16. + static unsigned int nTransactionsUpdatedLast;
  17. + static CBlockIndex* pindexPrev;
  18. + static int64 nStart;
  19. + static CBlock* pblock;
  20. +
  21. + while (pindexPrev != pindexBest ||
  22. + (nTransactionsUpdated != nTransactionsUpdatedLast && GetTime() - nStart > 60))
  23. + {
  24. + if (pindexPrev != pindexBest)
  25. + {
  26. + // Deallocate old blocks since they're obsolete now
  27. + mapGetWorkNewBlock.clear();
  28. + BOOST_FOREACH(CBlock* pblock, vNewBlock)
  29. + delete pblock;
  30. + vNewBlock.clear();
  31. + }
  32. + nTransactionsUpdatedLast = nTransactionsUpdated;
  33. + pindexPrev = pindexBest;
  34. + nStart = GetTime();
  35. +
  36. + // Create new block
  37. + if (pGetWorkReserveKey == NULL)
  38. + pGetWorkReserveKey = new CReserveKey(pwalletMain);
  39. + pblock = CreateNewBlock(*pGetWorkReserveKey);
  40. + if (!pblock)
  41. + throw JSONRPCError(-7, "Out of memory");
  42. + vNewBlock.push_back(pblock);
  43. + }
  44. +
  45. + // Update nTime
  46. + pblock->nTime = max(pindexPrev->GetMedianTimePast()+1, GetAdjustedTime());
  47. + pblock->nNonce = 0;
  48. +
  49. + // Update nExtraNonce
  50. + static unsigned int nExtraNonce = 0;
  51. + IncrementExtraNonce(pblock, pindexPrev, nExtraNonce);
  52. +
  53. + // Save
  54. + mapGetWorkNewBlock[pblock->hashMerkleRoot] = make_pair(pblock, pblock->vtx[0].vin[0].scriptSig);
  55. +
  56. + // Prebuild hash buffers
  57. + FormatHashBuffers(pblock, pmidstate, pdata, phash1);
  58. + hashTarget = CBigNum().SetCompact(pblock->nBits).getuint256();
  59. +}
  60. +
  61. Value getwork(const Array& params, bool fHelp)
  62. {
  63. if (fHelp || params.size() > 1)
  64. @@ -1804,64 +1857,21 @@ Value getwork(const Array& params, bool
  65. if (IsInitialBlockDownload())
  66. throw JSONRPCError(-10, "Bitcoin is downloading blocks...");
  67.  
  68. - typedef map<uint256, pair<CBlock*, CScript> > mapNewBlock_t;
  69. - static mapNewBlock_t mapNewBlock;
  70. - static vector<CBlock*> vNewBlock;
  71. - static CReserveKey reservekey(pwalletMain);
  72. -
  73. if (params.size() == 0)
  74. {
  75. - // Update block
  76. - static unsigned int nTransactionsUpdatedLast;
  77. - static CBlockIndex* pindexPrev;
  78. - static int64 nStart;
  79. - static CBlock* pblock;
  80. - if (pindexPrev != pindexBest ||
  81. - (nTransactionsUpdated != nTransactionsUpdatedLast && GetTime() - nStart > 60))
  82. - {
  83. - if (pindexPrev != pindexBest)
  84. - {
  85. - // Deallocate old blocks since they're obsolete now
  86. - mapNewBlock.clear();
  87. - BOOST_FOREACH(CBlock* pblock, vNewBlock)
  88. - delete pblock;
  89. - vNewBlock.clear();
  90. - }
  91. - nTransactionsUpdatedLast = nTransactionsUpdated;
  92. - pindexPrev = pindexBest;
  93. - nStart = GetTime();
  94. -
  95. - // Create new block
  96. - pblock = CreateNewBlock(reservekey);
  97. - if (!pblock)
  98. - throw JSONRPCError(-7, "Out of memory");
  99. - vNewBlock.push_back(pblock);
  100. - }
  101. -
  102. - // Update nTime
  103. - pblock->UpdateTime(pindexPrev);
  104. - pblock->nNonce = 0;
  105. -
  106. - // Update nExtraNonce
  107. - static unsigned int nExtraNonce = 0;
  108. - IncrementExtraNonce(pblock, pindexPrev, nExtraNonce);
  109. -
  110. - // Save
  111. - mapNewBlock[pblock->hashMerkleRoot] = make_pair(pblock, pblock->vtx[0].vin[0].scriptSig);
  112. -
  113. - // Prebuild hash buffers
  114. char pmidstate[32];
  115. char pdata[128];
  116. char phash1[64];
  117. - FormatHashBuffers(pblock, pmidstate, pdata, phash1);
  118. + char hexbuf[512];
  119. + uint256 hashTarget;
  120.  
  121. - uint256 hashTarget = CBigNum().SetCompact(pblock->nBits).getuint256();
  122. + GetWorkBlock(pmidstate, pdata, phash1, hashTarget);
  123.  
  124. Object result;
  125. - result.push_back(Pair("midstate", HexStr(BEGIN(pmidstate), END(pmidstate)))); // deprecated
  126. - result.push_back(Pair("data", HexStr(BEGIN(pdata), END(pdata))));
  127. - result.push_back(Pair("hash1", HexStr(BEGIN(phash1), END(phash1)))); // deprecated
  128. - result.push_back(Pair("target", HexStr(BEGIN(hashTarget), END(hashTarget))));
  129. + result.push_back(Pair("midstate", ToHex(pmidstate, 32, hexbuf))); // deprecated
  130. + result.push_back(Pair("data", ToHex(pdata, 128, hexbuf)));
  131. + result.push_back(Pair("hash1", ToHex(phash1, 64, hexbuf))); // deprecated
  132. + result.push_back(Pair("target", ToHex((const char *) &hashTarget, sizeof(hashTarget), hexbuf)));
  133. return result;
  134. }
  135. else
  136. @@ -1877,19 +1887,48 @@ Value getwork(const Array& params, bool
  137. ((unsigned int*)pdata)[i] = ByteReverse(((unsigned int*)pdata)[i]);
  138.  
  139. // Get saved block
  140. - if (!mapNewBlock.count(pdata->hashMerkleRoot))
  141. + if (!mapGetWorkNewBlock.count(pdata->hashMerkleRoot))
  142. return false;
  143. - CBlock* pblock = mapNewBlock[pdata->hashMerkleRoot].first;
  144. + CBlock* pblock = mapGetWorkNewBlock[pdata->hashMerkleRoot].first;
  145.  
  146. pblock->nTime = pdata->nTime;
  147. pblock->nNonce = pdata->nNonce;
  148. - pblock->vtx[0].vin[0].scriptSig = mapNewBlock[pdata->hashMerkleRoot].second;
  149. + pblock->vtx[0].vin[0].scriptSig = mapGetWorkNewBlock[pdata->hashMerkleRoot].second;
  150. pblock->hashMerkleRoot = pblock->BuildMerkleTree();
  151.  
  152. - return CheckWork(pblock, *pwalletMain, reservekey);
  153. + return CheckWork(pblock, *pwalletMain, *pGetWorkReserveKey);
  154. }
  155. }
  156.  
  157. +std::string FastGetWork(const std::string id)
  158. +{ // bypass JSON in the most common case
  159. + if (vNodes.empty())
  160. + throw JSONRPCError(-9, "Bitcoin is not connected!");
  161. +
  162. + if (IsInitialBlockDownload())
  163. + throw JSONRPCError(-10, "Bitcoin is downloading blocks...");
  164. +
  165. + char pmidstate[32];
  166. + char pdata[128];
  167. + char phash1[64];
  168. + char hexbuf[512];
  169. + uint256 hashTarget;
  170. +
  171. + GetWorkBlock(pmidstate, pdata, phash1, hashTarget);
  172. +
  173. + std::string result = "{\"result\":{\"midstate\" : \"";
  174. + result += ToHex(pmidstate, 32, hexbuf);
  175. + result += "\",\"data\":\"";
  176. + result += ToHex(pdata, 128, hexbuf);
  177. + result += "\",\"hash1\":\"";
  178. + result += ToHex(phash1, 64, hexbuf);
  179. + result += "\",\"target\":\"";
  180. + result += ToHex((const char *) &hashTarget, sizeof(hashTarget), hexbuf);
  181. + result += "\"},\"error\":null,\"id\":\"";
  182. + result += id;
  183. + result += "\"}\n";
  184. + return result;
  185. +}
  186.  
  187. Value getmemorypool(const Array& params, bool fHelp)
  188. {
  189. @@ -2546,6 +2585,33 @@ void ThreadRPCServer3(void* parg)
  190. if (mapHeaders["connection"] == "close")
  191. fRun = false;
  192.  
  193. + if ((strRequest.find("\"getwork\"") != std::string::npos) && strRequest.find("[]") != std::string::npos)
  194. + { // This is imperfect code
  195. + std::string id;
  196. + size_t p = strRequest.find("\"id\":");
  197. + if (p != std::string::npos)
  198. + {
  199. + size_t ep = strRequest.find(" ", p+5), e;
  200. + if((e=strRequest.find(",", p+5))<ep) ep = e;
  201. + if((e=strRequest.find("}", p+5))<ep) ep = e;
  202. + id = strRequest.substr(p+5, ep-p-5);
  203. + }
  204. + try
  205. + {
  206. + conn->stream << HTTPReply(200, FastGetWork(id), fRun) << std::flush;
  207. + }
  208. + catch (std::exception& e)
  209. + {
  210. + ErrorReply(conn->stream, JSONRPCError(-1, e.what()), id);
  211. + fRun = false;
  212. + }
  213. + catch (Object& e)
  214. + {
  215. + ErrorReply(conn->stream, e, id);
  216. + fRun = false;
  217. + }
  218. + continue;
  219. + }
  220.  
  221. Value id = Value::null;
  222. try
  223. @@ -2560,17 +2626,39 @@ void ThreadRPCServer3(void* parg)
  224. id = find_value(request, "id");
  225.  
  226. // Parse method
  227. + Value valParams = find_value(request, "params");
  228. Value valMethod = find_value(request, "method");
  229. if (valMethod.type() == null_type)
  230. throw JSONRPCError(-32600, "Missing method");
  231. if (valMethod.type() != str_type)
  232. throw JSONRPCError(-32600, "Method must be a string");
  233. string strMethod = valMethod.get_str();
  234. - if (strMethod != "getwork" && strMethod != "getmemorypool")
  235. + bool bGetWork = strMethod == "getwork";
  236. + if (!bGetWork && strMethod != "getmemorypool")
  237. printf("ThreadRPCServer method=%s\n", strMethod.c_str());
  238. + else if (bGetWork)
  239. + { // is a getwork request
  240. + if( (valParams.type() == array_type) && valParams.get_array().size() == 0 )
  241. + {
  242. + try
  243. + {
  244. + conn->stream << HTTPReply(200, FastGetWork(id.type()==str_type ? id.get_str() : ""), fRun) << std::flush;
  245. + }
  246. + catch (std::exception& e)
  247. + {
  248. + ErrorReply(conn->stream, JSONRPCError(-1, e.what()), id);
  249. + fRun = false;
  250. + }
  251. + catch (Object& e)
  252. + {
  253. + ErrorReply(conn->stream, e, id);
  254. + fRun = false;
  255. + }
  256. + continue;
  257. + }
  258. + }
  259.  
  260. // Parse params
  261. - Value valParams = find_value(request, "params");
  262. Array params;
  263. if (valParams.type() == array_type)
  264. params = valParams.get_array();
  265. diff -Nurp src/util.cpp src2/util.cpp
  266. --- src/util.cpp 2012-04-26 18:04:42.161964416 -0700
  267. +++ src2/util.cpp 2012-04-26 18:08:55.443529152 -0700
  268. @@ -474,6 +474,25 @@ vector<unsigned char> ParseHex(const cha
  269. return vch;
  270. }
  271.  
  272. +char* ToHex(const char *ptr, int len, char *outbuf)
  273. +{
  274. + static char hexmap[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
  275. + '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
  276. + char *outptr = outbuf;
  277. + unsigned char *iptr = (unsigned char *) ptr;
  278. +
  279. + while (len-- > 0)
  280. + {
  281. + *outptr++ = hexmap[*iptr>>4];
  282. + *outptr++ = hexmap[*iptr&15];
  283. + iptr++;
  284. + }
  285. +
  286. + *outptr=0;
  287. + return outbuf;
  288. +}
  289. +
  290. +
  291. vector<unsigned char> ParseHex(const string& str)
  292. {
  293. return ParseHex(str.c_str());
  294. diff -Nurp src/util.h src2/util.h
  295. --- src/util.h 2012-04-26 18:04:42.161964416 -0700
  296. +++ src2/util.h 2012-04-26 18:08:55.443529152 -0700
  297. @@ -146,6 +146,7 @@ void ParseString(const std::string& str,
  298. std::string FormatMoney(int64 n, bool fPlus=false);
  299. bool ParseMoney(const std::string& str, int64& nRet);
  300. bool ParseMoney(const char* pszIn, int64& nRet);
  301. +char* ToHex(const char *ptr, int len, char *outbuf);
  302. std::vector<unsigned char> ParseHex(const char* psz);
  303. std::vector<unsigned char> ParseHex(const std::string& str);
  304. bool IsHex(const std::string& str);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement