-
Notifications
You must be signed in to change notification settings - Fork 2
/
stubserver.cpp
222 lines (185 loc) · 7.97 KB
/
stubserver.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*************************************************************************
* libjson-rpc-cpp
*************************************************************************
* @file stubserver.cpp
* @date 02.05.2013
* @author Peter Spiess-Knafl <[email protected]>
* @license See attached LICENSE.txt
************************************************************************
* @date 07.03.2020
* @author Blur Network <[email protected]>
* @license See attached LICENSE.txt
************************************************************************/
#include <iostream>
#include <stdio.h>
#include <string>
#include <memory>
#include <stdexcept>
#include <cmath>
#include <jsonrpccpp/client.h>
#include <jsonrpccpp/server/connectors/httpserver.h>
#include <jsonrpccpp/client/connectors/httpclient.h>
#include <boost/program_options.hpp>
#include "gen/abstractstubserver.h"
#include "blur_api.h"
using namespace jsonrpc;
namespace jsonrpc { class HttpClient; class Client; }
class MyStubServer : public AbstractStubServer, public BlurAPI {
public:
MyStubServer(AbstractServerConnector &connector, serverVersion_t type);
std::unique_ptr<BlurAPI> m_blur_api;
virtual void notifyServer();
virtual Json::Value getblockchaininfo();
virtual Json::Value getblock(std::string const& blockhash);
virtual Json::Value getinfo();
virtual Json::Value get_notarization_data();
virtual Json::Value validateaddress(std::string const& address);
virtual std::string getbestblockhash();
virtual std::string getblockhash(int const height);
virtual std::string sendrawtransaction(std::string const& signedhex);
virtual Json::Value signrawtransaction(std::string const& hexstring, Json::Value const& prevtxs);
virtual Json::Value decoderawtransaction(std::string const& signedhex);
virtual Json::Value listunspent(int const minconf, int const maxconf, Json::Value const& addresses);
virtual Json::Value calc_MoM(std::string const& height, std::string const& MoMdepth);
virtual ~MyStubServer();
};
MyStubServer::MyStubServer(AbstractServerConnector &connector,
serverVersion_t type)
: AbstractStubServer(connector, type), m_blur_api(new BlurAPI(username, password, blur_host, blur_port)) {}
MyStubServer::~MyStubServer() { }
void MyStubServer::notifyServer() { std::cout << "Server got notified" << std::endl; }
Json::Value MyStubServer::getblockchaininfo() {
Json::Value result = m_blur_api->getblockchaininfo();
return result;
}
Json::Value MyStubServer::getblock(std::string const& blockhash) {
Json::Value result = m_blur_api->getblock(blockhash);
return result;
}
Json::Value MyStubServer::getinfo() {
Json::Value result = m_blur_api->getblockchaininfo();
return result;
}
Json::Value MyStubServer::get_notarization_data() {
Json::Value result = m_blur_api->get_notarization_data();
return result;
}
Json::Value MyStubServer::validateaddress(std::string const& address) {
Json::Value result = m_blur_api->validateaddress(address);
return result;
}
std::string MyStubServer::getbestblockhash() {
Json::Value temp = m_blur_api->getbestblockhash();
return temp["hex"].asString();
}
std::string MyStubServer::getblockhash(int const height) {
Json::Value result = m_blur_api->getblockhash(height);
std::string ret = result["hash"].asString();
return ret;
}
Json::Value MyStubServer::calc_MoM(std::string const& height, std::string const& MoMdepth) {
Json::Value result = m_blur_api->calc_MoM(height, MoMdepth);
return result;
}
std::string MyStubServer::sendrawtransaction(std::string const& signedhex) {
Json::Value result = m_blur_api->sendrawtransaction(signedhex);
return result["hex"].asString();
}
Json::Value MyStubServer::signrawtransaction(std::string const& hex, Json::Value const& prevtxs) {
Json::Value result = m_blur_api->signrawtransaction(hex, prevtxs);
return result;
}
Json::Value MyStubServer::decoderawtransaction(std::string const& hex) {
Json::Value result = m_blur_api->decoderawtransaction(hex);
return result;
}
Json::Value MyStubServer::listunspent(int const minconf, int const maxconf, Json::Value const& addresses) {
std::list<std::string> addr_list;
for (const auto & each: addresses) {
addr_list.push_back(each.asString());
}
Json::Value blurapi_result = m_blur_api->listunspent(minconf, maxconf, addr_list);
Json::Value result, itemone, itemtwo;
uint16_t vout = 0;
for (auto& each : blurapi_result["entries"]) {
// dpow_fsm.c needs minimum 50 utxos
for (size_t n = 0; n < 50; n++) {
//each["scriptPubKey"] = "21037db090cbbe154b7e0a0742032208250537e42ad822968cd5419ce5913a4d8afbac";
each["amount"] = 0.00010000;
each["vout"] = vout;
vout++;
result.append(each);
}
}
return result;
}
int main(int ac, char** av) {
try {
boost::program_options::options_description desc("Supported command line options are");
desc.add_options()
("help", "Show help text for server startup flags")
("blur-host", boost::program_options::value<std::string>(), "Host address for blur daemon \n (Example: --blur-host=\"127.0.0.1\")")
("blur-port", boost::program_options::value<int>(), "Port for communcation with blur daemon \n (Example: --blur-port=52542)")
("api-port", boost::program_options::value<int>(), "Port for communcation with blurapi stubserver \n (Example: --api-port=8383)");
/* ("username", boost::program_options::value<std::string>(), "Username for blur daemon rpc login \n (Example: --user=\"username\")")
("password", boost::program_options::value<std::string>(), "Password for blur daemon rpc login \n (Example: --password=\"password\")");*/
boost::program_options::variables_map vm;
boost::program_options::store(boost::program_options::parse_command_line(ac, av, desc), vm);
boost::program_options::notify(vm);
if(vm.count("help")) {
std::cout << desc << std::endl;
return 0;
}
if (vm.count("blur-host")) {
std::cout << "Host address set to " << vm["blur-host"].as<std::string>() << ", for blur daemon." << std::endl;
blur_host = vm["blur-host"].as<std::string>();
} else {
std::cout << "No host provided, assuming localhost..." << std::endl;
blur_host = "127.0.0.1";
}
if (vm.count("blur-port")) {
std::cout << "Port set to " << std::to_string(vm["blur-port"].as<int>()) << ", for blur daemon..." << std::endl;
blur_port = vm["blur-port"].as<int>();
} else {
std::cout << "ERROR: No port number provided for blur daemon... Please use \"--blur-port\" startup flag to specify." << std:: endl;
return 1;
}
if (vm.count("api-port")) {
std::cout << "Port set to " << std::to_string(vm["api-port"].as<int>()) << ", for blurapiserver..." << std::endl;
api_port = vm["api-port"].as<int>();
} else {
std::cout << "No api-port set, using default port 8383..." << std::endl;
api_port = 8383;
}
/* if (vm.count("username")) {
username = vm["username"].as<std::string>();
if (vm.count("password") == 0) {
std::cout << "ERROR: Username provided for blur daemon RPC login, but no password!" << std::endl;
return 1;
} else {
password = vm["password"].as<std::string>();
}
}
if (vm.count("password")) {
password = vm["password"].as<std::string>();
if (vm.count("username") == 0) {
std::cout << "ERROR: Password provided for blur daemon RPC login, but no username!" << std::endl;
return 1;
} else {
username = vm["username"].as<std::string>();
}
}*/
} catch (std::exception& e) {
std::cout << "ERROR: Exception when parsing program options: " << e.what() << std::endl;
return 1;
}
HttpServer httpserver(api_port);
MyStubServer s(httpserver,
JSONRPC_SERVER_V1V2); // hybrid server (json-rpc 1.0 & 2.0)
s.StartListening();
std::cout << std::endl << "Hit enter to stop the server" << std::endl;
getchar();
std::cout << "User input detected, stopping blurapiserver..." << std::endl;
s.StopListening();
return 0;
}