Libbitcoin client
This commit is contained in:
parent
8d5f84f46b
commit
5c638fff71
4 changed files with 33 additions and 39 deletions
|
|
@ -41,8 +41,6 @@ libbitcoin_client::libbitcoin_client(std::string url) :
|
||||||
uint16_t port_num = std::stoi(port);
|
uint16_t port_num = std::stoi(port);
|
||||||
std::string final_url = protocol + "://" + host;
|
std::string final_url = protocol + "://" + host;
|
||||||
|
|
||||||
std::cout << "Final URL is: " << final_url << std::endl;
|
|
||||||
|
|
||||||
libbitcoin::config::endpoint address(final_url, port_num);
|
libbitcoin::config::endpoint address(final_url, port_num);
|
||||||
|
|
||||||
libbitcoin::client::connection_type connection;
|
libbitcoin::client::connection_type connection;
|
||||||
|
|
|
||||||
|
|
@ -187,8 +187,10 @@ public:
|
||||||
bool settle_sidechain_transaction(const sidechain_transaction_object &sto, asset &settle_amount);
|
bool settle_sidechain_transaction(const sidechain_transaction_object &sto, asset &settle_amount);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::string ip;
|
std::string bitcoin_node_ip;
|
||||||
uint32_t zmq_port;
|
std::string libbitcoin_server_ip;
|
||||||
|
uint32_t libbitcoin_zmq_port;
|
||||||
|
uint32_t bitcoin_node_zmq_port;
|
||||||
uint32_t rpc_port;
|
uint32_t rpc_port;
|
||||||
std::string rpc_user;
|
std::string rpc_user;
|
||||||
std::string rpc_password;
|
std::string rpc_password;
|
||||||
|
|
|
||||||
|
|
@ -183,6 +183,8 @@ void peerplays_sidechain_plugin_impl::plugin_set_program_options(
|
||||||
cli.add_options()("bitcoin-node-rpc-user", bpo::value<string>()->default_value("1"), "Bitcoin RPC user");
|
cli.add_options()("bitcoin-node-rpc-user", bpo::value<string>()->default_value("1"), "Bitcoin RPC user");
|
||||||
cli.add_options()("bitcoin-node-rpc-password", bpo::value<string>()->default_value("1"), "Bitcoin RPC password");
|
cli.add_options()("bitcoin-node-rpc-password", bpo::value<string>()->default_value("1"), "Bitcoin RPC password");
|
||||||
cli.add_options()("bitcoin-wallet-name", bpo::value<string>(), "Bitcoin wallet name");
|
cli.add_options()("bitcoin-wallet-name", bpo::value<string>(), "Bitcoin wallet name");
|
||||||
|
cli.add_options()("libbitcoin-server-ip", bpo::value<string>()->default_value("127.0.0.1"), "Libbitcoin server IP address");
|
||||||
|
cli.add_options()("libbitcoin-server-zmq-port", bpo::value<uint32_t>()->default_value(9093), "ZMQ port of libbitcoin server");
|
||||||
cli.add_options()("bitcoin-wallet-password", bpo::value<string>(), "Bitcoin wallet password");
|
cli.add_options()("bitcoin-wallet-password", bpo::value<string>(), "Bitcoin wallet password");
|
||||||
cli.add_options()("bitcoin-private-key", bpo::value<vector<string>>()->composing()->multitoken()->DEFAULT_VALUE_VECTOR(std::make_pair("02d0f137e717fb3aab7aff99904001d49a0a636c5e1342f8927a4ba2eaee8e9772", "cVN31uC9sTEr392DLVUEjrtMgLA8Yb3fpYmTRj7bomTm6nn2ANPr")),
|
cli.add_options()("bitcoin-private-key", bpo::value<vector<string>>()->composing()->multitoken()->DEFAULT_VALUE_VECTOR(std::make_pair("02d0f137e717fb3aab7aff99904001d49a0a636c5e1342f8927a4ba2eaee8e9772", "cVN31uC9sTEr392DLVUEjrtMgLA8Yb3fpYmTRj7bomTm6nn2ANPr")),
|
||||||
"Tuple of [Bitcoin public key, Bitcoin private key] (may specify multiple times)");
|
"Tuple of [Bitcoin public key, Bitcoin private key] (may specify multiple times)");
|
||||||
|
|
@ -247,12 +249,14 @@ void peerplays_sidechain_plugin_impl::plugin_initialize(const boost::program_opt
|
||||||
}
|
}
|
||||||
|
|
||||||
sidechain_enabled_bitcoin = options.at("bitcoin-sidechain-enabled").as<bool>();
|
sidechain_enabled_bitcoin = options.at("bitcoin-sidechain-enabled").as<bool>();
|
||||||
config_ready_bitcoin = options.count("bitcoin-node-ip") &&
|
|
||||||
options.count("bitcoin-node-zmq-port") && options.count("bitcoin-node-rpc-port") &&
|
config_ready_bitcoin = (((options.count("libbitcoin-server-ip") && options.count("libbitcoin-server-zmq-port")) ||
|
||||||
options.count("bitcoin-node-rpc-user") && options.count("bitcoin-node-rpc-password") &&
|
(options.count("bitcoin-node-ip") && options.count("bitcoin-node-zmq-port") &&
|
||||||
options.count("bitcoin-wallet-name") && options.count("bitcoin-wallet-password") &&
|
options.count("bitcoin-node-rpc-port") && options.count("bitcoin-node-rpc-user") &&
|
||||||
options.count("bitcoin-private-key");
|
options.count("bitcoin-node-rpc-password") && options.count("bitcoin-wallet-name") &&
|
||||||
if (sidechain_enabled_bitcoin && !config_ready_bitcoin) {
|
options.count("bitcoin-wallet-password"))) &&
|
||||||
|
options.count("bitcoin-private-key"));
|
||||||
|
if (!config_ready_bitcoin) {
|
||||||
wlog("Haven't set up Bitcoin sidechain parameters");
|
wlog("Haven't set up Bitcoin sidechain parameters");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -373,10 +373,7 @@ std::vector<info_for_vin> bitcoin_libbitcoin_client::getblock(const block_data &
|
||||||
const auto address_base58 = address;
|
const auto address_base58 = address;
|
||||||
info_for_vin vin;
|
info_for_vin vin;
|
||||||
vin.out.hash_tx = libbitcoin::config::hash256(tx.hash()).to_string();
|
vin.out.hash_tx = libbitcoin::config::hash256(tx.hash()).to_string();
|
||||||
// TODO check this condition here
|
vin.out.amount = std::floor(o.value());
|
||||||
// amount.erase(std::remove(amount.begin(), amount.end(), '.'), amount.end());
|
|
||||||
// vin.out.amount = std::stoll(amount);
|
|
||||||
vin.out.amount = o.value();
|
|
||||||
vin.out.n_vout = vout_seq;
|
vin.out.n_vout = vout_seq;
|
||||||
vin.address = address_base58;
|
vin.address = address_base58;
|
||||||
result.push_back(vin);
|
result.push_back(vin);
|
||||||
|
|
@ -424,12 +421,7 @@ btc_tx bitcoin_libbitcoin_client::getrawtransaction(const std::string &txid, con
|
||||||
tx_in.tx_address.emplace_back(ss.str());
|
tx_in.tx_address.emplace_back(ss.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO check this condition here
|
tx_in.tx_amount = std::floor(out.value());
|
||||||
// std::string tx_amount_s = input.second.get<std::string>("value");
|
|
||||||
// tx_amount_s.erase(std::remove(tx_amount_s.begin(), tx_amount_s.end(), '.'), tx_amount_s.end());
|
|
||||||
// tx_in.tx_amount = std::stoll(tx_amount_s);
|
|
||||||
|
|
||||||
tx_in.tx_amount = out.value();
|
|
||||||
tx.tx_in_list.emplace_back(tx_in);
|
tx.tx_in_list.emplace_back(tx_in);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -554,8 +546,8 @@ zmq_listener_libbitcoin::~zmq_listener_libbitcoin() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void zmq_listener_libbitcoin::start() {
|
void zmq_listener_libbitcoin::start() {
|
||||||
|
std::string endpoint_address = "tcp://" + ip;
|
||||||
libbitcoin::config::endpoint address(ip, zmq_port);
|
libbitcoin::config::endpoint address(endpoint_address, zmq_port);
|
||||||
socket.connect(address);
|
socket.connect(address);
|
||||||
thr = std::thread(&zmq_listener_libbitcoin::handle_block, this);
|
thr = std::thread(&zmq_listener_libbitcoin::handle_block, this);
|
||||||
}
|
}
|
||||||
|
|
@ -564,7 +556,7 @@ void zmq_listener_libbitcoin::handle_block() {
|
||||||
poller.add(socket);
|
poller.add(socket);
|
||||||
|
|
||||||
while (!stopped.load()) {
|
while (!stopped.load()) {
|
||||||
// TODO change the hard-coded value
|
// FIXME change the hard-coded value
|
||||||
const auto identifiers = poller.wait(500);
|
const auto identifiers = poller.wait(500);
|
||||||
|
|
||||||
if (identifiers.contains(socket.id())) {
|
if (identifiers.contains(socket.id())) {
|
||||||
|
|
@ -605,8 +597,10 @@ sidechain_net_handler_bitcoin::sidechain_net_handler_bitcoin(peerplays_sidechain
|
||||||
use_bitcoind_client = options.at("use-bitcoind-client").as<bool>();
|
use_bitcoind_client = options.at("use-bitcoind-client").as<bool>();
|
||||||
}
|
}
|
||||||
|
|
||||||
ip = options.at("bitcoin-node-ip").as<std::string>();
|
bitcoin_node_ip = options.at("bitcoin-node-ip").as<std::string>();
|
||||||
zmq_port = options.at("bitcoin-node-zmq-port").as<uint32_t>();
|
bitcoin_node_zmq_port = options.at("bitcoin-node-zmq-port").as<uint32_t>();
|
||||||
|
libbitcoin_server_ip = options.at("libbitcoin-server-ip").as<std::string>();
|
||||||
|
libbitcoin_zmq_port = options.at("libbitcoin-server-zmq-port").as<uint32_t>();
|
||||||
rpc_port = options.at("bitcoin-node-rpc-port").as<uint32_t>();
|
rpc_port = options.at("bitcoin-node-rpc-port").as<uint32_t>();
|
||||||
rpc_user = options.at("bitcoin-node-rpc-user").as<std::string>();
|
rpc_user = options.at("bitcoin-node-rpc-user").as<std::string>();
|
||||||
rpc_password = options.at("bitcoin-node-rpc-password").as<std::string>();
|
rpc_password = options.at("bitcoin-node-rpc-password").as<std::string>();
|
||||||
|
|
@ -631,9 +625,8 @@ sidechain_net_handler_bitcoin::sidechain_net_handler_bitcoin(peerplays_sidechain
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string url = ip + ":" + std::to_string(rpc_port);
|
|
||||||
|
|
||||||
if (use_bitcoind_client) {
|
if (use_bitcoind_client) {
|
||||||
|
std::string url = bitcoin_node_ip + ":" + std::to_string(rpc_port);
|
||||||
if (!wallet_name.empty()) {
|
if (!wallet_name.empty()) {
|
||||||
url = url + "/wallet/" + wallet_name;
|
url = url + "/wallet/" + wallet_name;
|
||||||
}
|
}
|
||||||
|
|
@ -642,20 +635,12 @@ sidechain_net_handler_bitcoin::sidechain_net_handler_bitcoin(peerplays_sidechain
|
||||||
bitcoin_client->loadwallet(wallet_name);
|
bitcoin_client->loadwallet(wallet_name);
|
||||||
}
|
}
|
||||||
|
|
||||||
listener = std::unique_ptr<zmq_listener>(new zmq_listener(ip, zmq_port));
|
listener = std::unique_ptr<zmq_listener>(new zmq_listener(bitcoin_node_ip, bitcoin_node_zmq_port));
|
||||||
listener->start();
|
|
||||||
listener->event_received.connect([this](const block_data &event_data) {
|
|
||||||
std::thread(&sidechain_net_handler_bitcoin::handle_event, this, event_data).detach();
|
|
||||||
});
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
bitcoin_client = std::unique_ptr<bitcoin_libbitcoin_client>(new bitcoin_libbitcoin_client("tcp://localhost"));
|
bitcoin_client = std::unique_ptr<bitcoin_libbitcoin_client>(new bitcoin_libbitcoin_client(libbitcoin_server_ip));
|
||||||
|
|
||||||
listener = std::unique_ptr<zmq_listener_libbitcoin>(new zmq_listener_libbitcoin("tcp://localhost", 9093));
|
listener = std::unique_ptr<zmq_listener_libbitcoin>(new zmq_listener_libbitcoin(libbitcoin_server_ip, libbitcoin_zmq_port));
|
||||||
listener->start();
|
|
||||||
listener->event_received.connect([this](const block_data &event_data) {
|
|
||||||
std::thread(&sidechain_net_handler_bitcoin::handle_event, this, event_data).detach();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string chain_info = bitcoin_client->getblockchaininfo();
|
std::string chain_info = bitcoin_client->getblockchaininfo();
|
||||||
|
|
@ -671,6 +656,11 @@ sidechain_net_handler_bitcoin::sidechain_net_handler_bitcoin(peerplays_sidechain
|
||||||
|
|
||||||
bitcoin_client->getnetworkinfo();
|
bitcoin_client->getnetworkinfo();
|
||||||
|
|
||||||
|
listener->start();
|
||||||
|
listener->event_received.connect([this](const block_data &event_data) {
|
||||||
|
std::thread(&sidechain_net_handler_bitcoin::handle_event, this, event_data).detach();
|
||||||
|
});
|
||||||
|
|
||||||
database.changed_objects.connect([this](const vector<object_id_type> &ids, const flat_set<account_id_type> &accounts) {
|
database.changed_objects.connect([this](const vector<object_id_type> &ids, const flat_set<account_id_type> &accounts) {
|
||||||
on_changed_objects(ids, accounts);
|
on_changed_objects(ids, accounts);
|
||||||
});
|
});
|
||||||
|
|
@ -1206,7 +1196,7 @@ bool sidechain_net_handler_bitcoin::settle_sidechain_transaction(const sidechain
|
||||||
|
|
||||||
if (tx.tx_in_list.empty()) {
|
if (tx.tx_in_list.empty()) {
|
||||||
// This case will result with segmentation fault.
|
// This case will result with segmentation fault.
|
||||||
// TODO check if that happened before introducing libbitcoin
|
// FIXME check if that happened before introducing libbitcoin
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue