Skip to content

Commit f592584

Browse files
author
minium
committed
Merge branch 'debug'
2 parents c9b8211 + 97e7397 commit f592584

9 files changed

+152
-170
lines changed

src/bitcoinapi/bitcoinapi.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ BitcoinAPI::BitcoinAPI(const string& user, const string& password, const string&
3434
: httpClient(new HttpClient("http://" + user + ":" + password + "@" + host + ":" + NumberToString(port))),
3535
client(new Client(*httpClient, JSONRPC_CLIENT_V1))
3636
{
37-
httpClient->SetTimeout(20000);
37+
httpClient->SetTimeout(50000);
3838
}
3939

4040
BitcoinAPI::~BitcoinAPI()
@@ -174,7 +174,7 @@ vector<nodeinfo_t> BitcoinAPI::getaddednodeinfo(bool dns, const std::string& nod
174174
netaddress_t net;
175175

176176
net.address = val2["address"].asString();
177-
net.connected = val2["connected"].asBool();
177+
net.connected = val2["connected"].asString();
178178

179179
node.addresses.push_back(net);
180180
}

src/bitcoinapi/exception.h

+53-15
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@
1313
#include <string>
1414
#include <sstream>
1515

16+
#include <jsoncpp/json/json.h>
17+
#include <jsoncpp/json/reader.h>
18+
#include <jsoncpp/json/value.h>
19+
20+
using Json::Value;
21+
using Json::Reader;
22+
23+
1624
class BitcoinException: public std::exception
1725
{
1826
private:
@@ -21,9 +29,8 @@ class BitcoinException: public std::exception
2129

2230
public:
2331
explicit BitcoinException(int errcode, const std::string& message) {
24-
this->code = errcode;
25-
this->msg = parse(message);
26-
32+
this->code = parseCode(message);
33+
this->msg = parseMessage(message);
2734
}
2835
~BitcoinException() throw() { };
2936

@@ -35,22 +42,53 @@ class BitcoinException: public std::exception
3542
return msg;
3643
}
3744

38-
std::string parse(const std::string& in){
39-
std::string out = in;
40-
std::string pattern = ": ";
41-
unsigned int pos = out.find(pattern);
42-
if(pos <= out.size()){
43-
out.erase(0, pos+pattern.size());
44-
out[0] = toupper(out[0]);
45+
46+
std::string removePrefix(const std::string& in, const std::string& pattern){
47+
std::string ret = in;
48+
49+
unsigned int pos = ret.find(pattern);
50+
51+
if(pos <= ret.size()){
52+
ret.erase(0, pos+pattern.size());
53+
}
54+
55+
return ret;
56+
}
57+
58+
59+
int parseCode(const std::string& in){
60+
Value root;
61+
Reader reader;
62+
63+
/* Remove JSON prefix */
64+
std::string strJson = removePrefix(in, "INTERNAL_ERROR: : ");
65+
int ret = -1;
66+
67+
/* Parse error message */
68+
bool parsingSuccessful = reader.parse(strJson.c_str(), root);
69+
if(parsingSuccessful) {
70+
ret = root["error"]["code"].asInt();
4571
}
4672

47-
return out;
73+
return ret;
4874
}
4975

50-
virtual const char* what() const throw (){
51-
std::stringstream out;
52-
out << "Error " << code << ": " << msg;
53-
return out.str().c_str();
76+
std::string parseMessage(const std::string& in){
77+
Value root;
78+
Reader reader;
79+
80+
/* Remove JSON prefix */
81+
std::string strJson = removePrefix(in, "INTERNAL_ERROR: : ");
82+
std::string ret = "Error during parsing of >>" + strJson + "<<";
83+
84+
/* Parse error message */
85+
bool parsingSuccessful = reader.parse(strJson.c_str(), root);
86+
if(parsingSuccessful) {
87+
ret = removePrefix(root["error"]["message"].asString(), "Error: ");
88+
ret[0] = toupper(ret[0]);
89+
}
90+
91+
return ret;
5492
}
5593
};
5694

src/test/accounting.cpp

+24-39
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ BOOST_AUTO_TEST_CASE(GetBalance) {
1717

1818
MyFixture fx;
1919

20-
BOOST_REQUIRE_NO_THROW(fx.btc.getbalance());
21-
BOOST_REQUIRE_NO_THROW(fx.btc.getbalance(""));
20+
NO_THROW(fx.btc.getbalance());
21+
NO_THROW(fx.btc.getbalance(""));
2222

2323
#ifdef VERBOSE
2424
double response = fx.btc.getbalance();
@@ -32,7 +32,7 @@ BOOST_AUTO_TEST_CASE(GetReceivedByAccount) {
3232

3333
MyFixture fx;
3434

35-
BOOST_REQUIRE_NO_THROW(fx.btc.getreceivedbyaccount(""));
35+
NO_THROW(fx.btc.getreceivedbyaccount(""));
3636

3737
#ifdef VERBOSE
3838
double response = fx.btc.getreceivedbyaccount("");
@@ -47,7 +47,7 @@ BOOST_AUTO_TEST_CASE(GetReceivedByAddress) {
4747

4848
std::string param = "36m2bHwsUnkpGsZgVAEmeJRf2CeViDm6RV";
4949

50-
BOOST_REQUIRE_NO_THROW(fx.btc.getreceivedbyaddress(param));
50+
NO_THROW(fx.btc.getreceivedbyaddress(param));
5151

5252
#ifdef VERBOSE
5353
double response = fx.btc.getreceivedbyaddress(param);
@@ -62,7 +62,7 @@ BOOST_AUTO_TEST_CASE(ListReceivedByAccount) {
6262
MyFixture fx;
6363

6464
std::vector<accountinfo_t> response;
65-
BOOST_REQUIRE_NO_THROW(response = fx.btc.listreceivedbyaccount(1,true));
65+
NO_THROW(response = fx.btc.listreceivedbyaccount(1,true));
6666

6767
#ifdef VERBOSE
6868
std::cout << "=== listreceivedbyaccount ===" << std::endl;
@@ -80,7 +80,7 @@ BOOST_AUTO_TEST_CASE(ListReceivedByAddress) {
8080
MyFixture fx;
8181

8282
std::vector<addressinfo_t> response;
83-
BOOST_REQUIRE_NO_THROW(response = fx.btc.listreceivedbyaddress(1,true));
83+
NO_THROW(response = fx.btc.listreceivedbyaddress(1,true));
8484

8585
#ifdef VERBOSE
8686
std::cout << "=== listreceivedbyaccount ===" << std::endl;
@@ -106,12 +106,8 @@ BOOST_AUTO_TEST_CASE(GetTransaction) {
106106

107107
std::string txid = "5a8f750129702d4e0ccd3e6fa91193d8191ea9742a36835b43d3b3c56ad816d1";
108108
gettransaction_t response;
109-
try {
110-
response = fx.btc.gettransaction(txid, false);
111-
} catch (BitcoinException& e) {
112-
BOOST_REQUIRE(e.getCode() == -5);
113-
return;
114-
}
109+
110+
NO_THROW_EXCEPT(response = fx.btc.gettransaction(txid, false), -5);
115111

116112
#ifdef VERBOSE
117113
std::cout << "=== gettransaction ===" << std::endl;
@@ -151,7 +147,7 @@ BOOST_AUTO_TEST_CASE(ListTransactions) {
151147
MyFixture fx;
152148

153149
std::vector<transactioninfo_t> response;
154-
BOOST_REQUIRE_NO_THROW(response = fx.btc.listtransactions());
150+
NO_THROW(response = fx.btc.listtransactions());
155151

156152
#ifdef VERBOSE
157153
std::cout << "=== listtransactions ===" << std::endl;
@@ -185,14 +181,9 @@ BOOST_AUTO_TEST_CASE(GetAccount) {
185181
std::string address;
186182
std::string response;
187183

188-
try {
189-
address = fx.btc.getnewaddress("TestUser");
190-
} catch (BitcoinException& e) {
191-
BOOST_REQUIRE(e.getCode() == -12);
192-
BOOST_WARN_MESSAGE(false, e.getMessage());
193-
}
184+
NO_THROW_EXCEPT(address = fx.btc.getnewaddress("TestUser"), -12);
194185

195-
BOOST_REQUIRE_NO_THROW(response = fx.btc.getaccount(address));
186+
NO_THROW(response = fx.btc.getaccount(address));
196187
BOOST_REQUIRE(response == "TestUser");
197188

198189
#ifdef VERBOSE
@@ -209,16 +200,10 @@ BOOST_AUTO_TEST_CASE(GetAccountAddress) {
209200
std::string response;
210201

211202
/* Unlock wallet and refill the keypool */
212-
try {
213-
fx.btc.walletpassphrase("123456", 10);
214-
} catch (BitcoinException& e) {
215-
BOOST_REQUIRE(e.getCode() == -14);
216-
BOOST_WARN_MESSAGE(false, e.getMessage());
217-
return;
218-
}
203+
NO_THROW_EXCEPT(fx.btc.walletpassphrase("123456", 10), -14);
219204

220-
BOOST_REQUIRE_NO_THROW(fx.btc.keypoolrefill());
221-
BOOST_REQUIRE_NO_THROW(address = fx.btc.getnewaddress("TestUser"));
205+
NO_THROW(fx.btc.keypoolrefill());
206+
NO_THROW(address = fx.btc.getnewaddress("TestUser"));
222207
BOOST_REQUIRE(address.length() >= 27 && address.length() <= 34);
223208

224209
#ifdef VERBOSE
@@ -232,7 +217,7 @@ BOOST_AUTO_TEST_CASE(GetAddressesByAccount) {
232217
MyFixture fx;
233218

234219
std::vector<std::string> response;
235-
BOOST_REQUIRE_NO_THROW(response = fx.btc.getaddressesbyaccount("TestUser"));
220+
NO_THROW(response = fx.btc.getaddressesbyaccount("TestUser"));
236221
BOOST_REQUIRE(response.size() >= 1);
237222

238223
#ifdef VERBOSE
@@ -249,7 +234,7 @@ BOOST_AUTO_TEST_CASE(ListAccounts) {
249234
MyFixture fx;
250235

251236
std::map<std::string, double> response;
252-
BOOST_REQUIRE_NO_THROW(response = fx.btc.listaccounts());
237+
NO_THROW(response = fx.btc.listaccounts());
253238

254239
#ifdef VERBOSE
255240
std::cout << "=== listaccounts ===" << std::endl;
@@ -287,8 +272,8 @@ BOOST_AUTO_TEST_CASE(SetAccount) {
287272
MyFixture fx;
288273

289274
std::string address;
290-
BOOST_REQUIRE_NO_THROW(address = fx.btc.getnewaddress("TestUser"))
291-
BOOST_REQUIRE_NO_THROW(fx.btc.setaccount(address,"TestUser2"));
275+
NO_THROW_EXCEPT(address = fx.btc.getnewaddress("TestUser"), -12);
276+
NO_THROW(fx.btc.setaccount(address,"TestUser2"));
292277
}
293278

294279
BOOST_AUTO_TEST_CASE(SendToAddress) {
@@ -348,7 +333,7 @@ BOOST_AUTO_TEST_CASE(GetTxOut) {
348333
std::string txid = "105d8fd318533d4559492b85a27039f2bf8bdfed39b8e671753289eaeb3829ae";
349334
utxoinfo_t response;
350335

351-
BOOST_REQUIRE_NO_THROW(response = fx.btc.gettxout(txid, 0));
336+
NO_THROW(response = fx.btc.gettxout(txid, 0));
352337

353338
#ifdef VERBOSE
354339
std::cout << "=== gettxout ===" << std::endl;
@@ -374,7 +359,7 @@ BOOST_AUTO_TEST_CASE(GetTxOutSetInfo) {
374359

375360
utxosetinfo_t response;
376361

377-
BOOST_REQUIRE_NO_THROW(response = fx.btc.gettxoutsetinfo());
362+
NO_THROW(response = fx.btc.gettxoutsetinfo());
378363

379364
#ifdef VERBOSE
380365
std::cout << "=== gettxoutsetinfo ===" << std::endl;
@@ -393,7 +378,7 @@ BOOST_AUTO_TEST_CASE(ListUnspent) {
393378
MyFixture fx;
394379

395380
std::vector<unspenttxout_t> response;
396-
BOOST_REQUIRE_NO_THROW(response = fx.btc.listunspent());
381+
NO_THROW(response = fx.btc.listunspent());
397382

398383
#ifdef VERBOSE
399384
std::cout << "=== listunspent ===" << std::endl;
@@ -415,7 +400,7 @@ BOOST_AUTO_TEST_CASE(ListLockUnspent) {
415400
MyFixture fx;
416401

417402
std::vector<txout_t> response;
418-
BOOST_REQUIRE_NO_THROW(response = fx.btc.listlockunspent());
403+
NO_THROW(response = fx.btc.listlockunspent());
419404

420405
#ifdef VERBOSE
421406
std::cout << "=== listlockunspent ===" << std::endl;
@@ -436,7 +421,7 @@ BOOST_AUTO_TEST_CASE(LockUnspent) {
436421
param.push_back(tmp);
437422
bool response;
438423

439-
BOOST_REQUIRE_NO_THROW(response = fx.btc.lockunspent(false, param));
424+
NO_THROW(response = fx.btc.lockunspent(false, param));
440425
BOOST_REQUIRE(response == true);
441426
}
442427

@@ -445,7 +430,7 @@ BOOST_AUTO_TEST_CASE(ListAddressGroupings) {
445430
MyFixture fx;
446431
std::vector< std::vector<addressgrouping_t> > response;
447432

448-
BOOST_REQUIRE_NO_THROW(response = fx.btc.listaddressgroupings());
433+
NO_THROW(response = fx.btc.listaddressgroupings());
449434

450435
#ifdef VERBOSE
451436
std::cout << "=== listaddressgroupings ===" << std::endl;

src/test/general.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ BOOST_AUTO_TEST_SUITE(GeneralTests)
1010
BOOST_AUTO_TEST_CASE(GetInfo) {
1111

1212
MyFixture fx;
13-
14-
getinfo_t info = fx.btc.getinfo();
13+
getinfo_t info;
14+
15+
NO_THROW(info = fx.btc.getinfo());
1516
BOOST_REQUIRE(info.protocolversion >= 70002);
1617

1718
#ifdef VERBOSE

src/test/main.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,23 @@
66
#include <bitcoinapi/bitcoinapi.h>
77
#include <bitcoinapi/exception.h>
88

9+
#define NO_THROW(METHOD) \
10+
try { \
11+
(METHOD); \
12+
} catch (BitcoinException& e) { \
13+
BOOST_REQUIRE_MESSAGE(false, e.what()); \
14+
}
15+
16+
#define NO_THROW_EXCEPT(METHOD, EXCEPTION) \
17+
try { \
18+
(METHOD); \
19+
} catch (BitcoinException& e) { \
20+
std::stringstream err; \
21+
err << "Error (" << e.getCode() << "): " << e.getMessage(); \
22+
BOOST_REQUIRE_MESSAGE(e.getCode() == (EXCEPTION), err.str()); \
23+
BOOST_WARN_MESSAGE(false, err.str()); \
24+
return; \
25+
}
926

1027
struct MyFixture {
1128

0 commit comments

Comments
 (0)