X-Git-Url: https://git.novaco.in/?p=novacoin.git;a=blobdiff_plain;f=src%2Frpcblockchain.cpp;h=054dca27d5cfaf368b5d01342c2d610b1639fbc5;hp=ba4ad5c592ff45ab4e8af2ff7353ea80846c6e57;hb=4cdcc9880e8d3398b6009f067dfd96a8aa709724;hpb=f5c5f207c702d70ccba89e8e08a972e779bae9ae diff --git a/src/rpcblockchain.cpp b/src/rpcblockchain.cpp index ba4ad5c..054dca2 100644 --- a/src/rpcblockchain.cpp +++ b/src/rpcblockchain.cpp @@ -5,11 +5,15 @@ #include "main.h" #include "bitcoinrpc.h" +#include +#include +#include +#include using namespace json_spirit; using namespace std; -extern void TxToJSON(const CTransaction& tx, const uint256 hashBlock, json_spirit::Object& entry); +extern void TxToJSON(const CTransaction& tx, const uint256& hashBlock, json_spirit::Object& entry); extern enum Checkpoints::CPMode CheckpointsMode; double GetDifficulty(const CBlockIndex* blockindex) @@ -107,8 +111,8 @@ Object blockToJSON(const CBlock& block, const CBlockIndex* blockindex, bool fPri result.push_back(Pair("version", block.nVersion)); result.push_back(Pair("merkleroot", block.hashMerkleRoot.GetHex())); result.push_back(Pair("mint", ValueFromAmount(blockindex->nMint))); - result.push_back(Pair("time", (boost::int64_t)block.GetBlockTime())); - result.push_back(Pair("nonce", (boost::uint64_t)block.nNonce)); + result.push_back(Pair("time", (int64_t)block.GetBlockTime())); + result.push_back(Pair("nonce", (uint64_t)block.nNonce)); result.push_back(Pair("bits", HexBits(block.nBits))); result.push_back(Pair("difficulty", GetDifficulty(blockindex))); result.push_back(Pair("blocktrust", leftTrim(blockindex->GetBlockTrust().GetHex(), '0'))); @@ -265,14 +269,93 @@ Value getblockbynumber(const Array& params, bool fHelp) while (pblockindex->nHeight > nHeight) pblockindex = pblockindex->pprev; - uint256 hash = *pblockindex->phashBlock; - - pblockindex = mapBlockIndex[hash]; + pblockindex = mapBlockIndex[*pblockindex->phashBlock]; block.ReadFromDisk(pblockindex, true); return blockToJSON(block, pblockindex, params.size() > 1 ? params[1].get_bool() : false); } +bool ExportBlock(const string& strBlockHash, const CDataStream& ssBlock) +{ + boost::filesystem::path pathDest = GetDataDir() / strBlockHash; + if (boost::filesystem::is_directory(pathDest)) + pathDest /= strBlockHash; + + try { + boost::iostreams::stream_buffer buf(pathDest.string()); + ostream exportStream(&buf); + exportStream << HexStr(ssBlock.begin(), ssBlock.end()); + exportStream.flush(); + + printf("Successfully exported block to %s\n", pathDest.string().c_str()); + return true; + } catch(const boost::filesystem::filesystem_error &e) { + printf("error exporting the block data %s (%s)\n", pathDest.string().c_str(), e.what()); + return false; + } +} + + +Value dumpblock(const Array& params, bool fHelp) +{ + if (fHelp || params.size() < 1 || params.size() > 2) + throw runtime_error( + "dumpblock [destination]\n" + "Returns serialized contents of a block with given block-hash."); + + std::string strHash = params[0].get_str(); + uint256 hash(strHash); + + if (mapBlockIndex.count(hash) == 0) + throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found"); + + CBlock block; + CBlockIndex* pblockindex = mapBlockIndex[hash]; + block.ReadFromDisk(pblockindex, true); + + CDataStream ssBlock(SER_NETWORK, PROTOCOL_VERSION); + ssBlock << block; + + if (params.size() > 1) + { + return ExportBlock(params[1].get_str(), ssBlock); + } + + return HexStr(ssBlock.begin(), ssBlock.end()); +} + + +Value dumpblockbynumber(const Array& params, bool fHelp) +{ + if (fHelp || params.size() < 1 || params.size() > 2) + throw runtime_error( + "dumpblockbynumber [destination]\n" + "Returns serialized contents of a block with given block-number."); + + int nHeight = params[0].get_int(); + if (nHeight < 0 || nHeight > nBestHeight) + throw runtime_error("Block number out of range."); + + CBlock block; + CBlockIndex* pblockindex = mapBlockIndex[hashBestChain]; + while (pblockindex->nHeight > nHeight) + pblockindex = pblockindex->pprev; + + pblockindex = mapBlockIndex[*pblockindex->phashBlock]; + block.ReadFromDisk(pblockindex, true); + + CDataStream ssBlock(SER_NETWORK, PROTOCOL_VERSION); + ssBlock << block; + + if (params.size() > 1) + { + return ExportBlock(params[1].get_str(), ssBlock); + } + + return HexStr(ssBlock.begin(), ssBlock.end()); +} + + // get information of sync-checkpoint Value getcheckpoint(const Array& params, bool fHelp) {