From 5805c33e1748fda50c2a23a526c717070e6d7e75 Mon Sep 17 00:00:00 2001 From: Fan Zhang Date: Tue, 12 Sep 2017 09:46:31 -0400 Subject: [PATCH] add getblockheader --- .gitignore | 2 ++ src/bitcoinapi/bitcoinapi.cpp | 36 ++++++++++++++++++++++++++++++ src/bitcoinapi/bitcoinapi.h | 4 ++++ src/bitcoinapi/types.h | 16 ++++++++++++++ src/test/mining.cpp | 41 ++++++++++++++++++++++++++++++++++- 5 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8461078 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +build +.idea diff --git a/src/bitcoinapi/bitcoinapi.cpp b/src/bitcoinapi/bitcoinapi.cpp index cc12ab0..5fee04c 100644 --- a/src/bitcoinapi/bitcoinapi.cpp +++ b/src/bitcoinapi/bitcoinapi.cpp @@ -941,6 +941,42 @@ int BitcoinAPI::getblockcount() { return result.asInt(); } +std::string BitcoinAPI::getblockheaderhex(const std::string& blockhash) { + string command = "getblockheader"; + Value params, result; + params.append(blockhash); + params.append(false); + + result = sendcommand(command, params); + return result.asString(); +} + +blockheader_t BitcoinAPI::getblockheaderjson(const std::string& blockhash) { + string command = "getblockheader"; + Value params, result; + blockheader_t ret; + + params.append(blockhash); + result = sendcommand(command, params); + + ret.hash = result["hash"].asString(); + ret.confirmations = result["confirmations"].asInt(); + ret.height = result["height"].asInt(); + ret.version = result["version"].asInt(); + ret.versionHex = result["verionHex"].asString(); + ret.merkleroot = result["merkleroot"].asString(); + ret.mediantime = result["mediantime"].asUInt(); + ret.nonce = result["nonce"].asUInt(); + ret.bits = result["bits"].asString(); + ret.difficulty = result["difficulty"].asDouble(); + ret.chainwork = result["chainwork"].asString(); + ret.previousblockhash = result["previousblockhash"].asString(); + ret.nextblockhash = result["nextblockhash"].asString(); + + return ret; +} + + void BitcoinAPI::setgenerate(bool generate, int genproclimit) { string command = "setgenerate"; Value params; diff --git a/src/bitcoinapi/bitcoinapi.h b/src/bitcoinapi/bitcoinapi.h index e41cb2b..5d73be7 100644 --- a/src/bitcoinapi/bitcoinapi.h +++ b/src/bitcoinapi/bitcoinapi.h @@ -122,6 +122,10 @@ class BitcoinAPI blockinfo_t getblock(const std::string& blockhash); int getblockcount(); + /* === blockchain === */ + blockheader_t getblockheaderjson(const std::string& blockhash); + std::string getblockheaderhex(const std::string& blockhash); + void setgenerate(bool generate, int genproclimit = -1); bool getgenerate(); double getdifficulty(); diff --git a/src/bitcoinapi/types.h b/src/bitcoinapi/types.h index 5051914..bce86e7 100644 --- a/src/bitcoinapi/types.h +++ b/src/bitcoinapi/types.h @@ -245,6 +245,22 @@ std::string nextblockhash; }; + struct blockheader_t { + std::string hash; + int confirmations; + int height; + int version; + std::string versionHex; + std::string merkleroot; + int mediantime; + unsigned int nonce; + std::string bits; + double difficulty; + std::string chainwork; + std::string previousblockhash; + std::string nextblockhash; + }; + struct mininginfo_t{ int blocks; int currentblocksize; diff --git a/src/test/mining.cpp b/src/test/mining.cpp index 8d8eaf1..039aa4d 100644 --- a/src/test/mining.cpp +++ b/src/test/mining.cpp @@ -1,7 +1,10 @@ #define BOOST_TEST_DYN_LINK -#include + #include +#include +#include + #include "main.cpp" BOOST_AUTO_TEST_SUITE(MiningTests) @@ -80,6 +83,42 @@ BOOST_AUTO_TEST_CASE(GetBlock) { #endif } +BOOST_AUTO_TEST_CASE(GetBlockHeaderHex) { + + MyFixture fx; + std::string response; + + NO_THROW(response = fx.btc.getblockheaderhex("00000000839a8e6886ab5951d76f411475428afc90947ee320161bbf18eb6048")); + BOOST_REQUIRE(response == "010000006fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000982051fd1e4ba744bbbe680e1fee14677ba1a3c3540bf7b1cdb606e857233e0e61bc6649ffff001d01e36299"); + +} + +BOOST_AUTO_TEST_CASE(GetBlockHeaderJSON) { + + MyFixture fx; + blockheader_t response; + + NO_THROW(response = fx.btc.getblockheaderjson("00000000839a8e6886ab5951d76f411475428afc90947ee320161bbf18eb6048")); + BOOST_REQUIRE(response.height == 1); + +#ifdef VERBOSE + std::cout << "=== getblock (10th block) ===" << std::endl; + std::cout << "hash: " << response.hash << std::endl; + std::cout << "confirmations: " << response.confirmations << std::endl; + std::cout << "height: " << response.height << std::endl; + std::cout << "version: " << response.version << std::endl; + std::cout << "versionHex: " << response.versionHex << std::endl; + std::cout << "merkleroot: " << response.merkleroot << std::endl; + std::cout << "mediantime: " << response.mediantime << std::endl; + std::cout << "nonce: " << response.nonce << std::endl; + std::cout << "bits: " << response.bits << std::endl; + std::cout << "difficulty: " << response.difficulty << std::endl; + std::cout << "chainwork: " << response.chainwork << std::endl; + std::cout << "previousblockhash: " << response.previousblockhash << std::endl; + std::cout << "nextblockhash: " << response.nextblockhash << std::endl << std::endl; +#endif +} + BOOST_AUTO_TEST_CASE(GetGenerate) { MyFixture fx;