Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SON-348] Transaction hash not saved after Bitcoin transaction is sent #343

Merged
merged 1 commit into from
Apr 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ class sidechain_net_handler {
virtual void process_primary_wallet() = 0;
virtual bool process_deposit(const son_wallet_deposit_object &swdo) = 0;
virtual bool process_withdrawal(const son_wallet_withdraw_object &swwo) = 0;
virtual std::string process_sidechain_transaction(const sidechain_transaction_object &sto, bool &complete) = 0;
virtual bool send_sidechain_transaction(const sidechain_transaction_object &sto, std::string &sidechain_transaction) = 0;
virtual std::string process_sidechain_transaction(const sidechain_transaction_object &sto) = 0;
virtual std::string send_sidechain_transaction(const sidechain_transaction_object &sto) = 0;

protected:
peerplays_sidechain_plugin &plugin;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class bitcoin_rpc_client {
std::vector<btc_txout> listunspent(const uint32_t minconf = 1, const uint32_t maxconf = 9999999);
std::vector<btc_txout> listunspent_by_address_and_amount(const std::string &address, double transfer_amount, const uint32_t minconf = 1, const uint32_t maxconf = 9999999);
std::string loadwallet(const std::string &filename);
bool sendrawtransaction(const std::string &tx_hex);
std::string sendrawtransaction(const std::string &tx_hex);
std::string signrawtransactionwithwallet(const std::string &tx_hash);
std::string unloadwallet(const std::string &filename);
std::string walletlock();
Expand Down Expand Up @@ -89,8 +89,8 @@ class sidechain_net_handler_bitcoin : public sidechain_net_handler {
void process_primary_wallet();
bool process_deposit(const son_wallet_deposit_object &swdo);
bool process_withdrawal(const son_wallet_withdraw_object &swwo);
std::string process_sidechain_transaction(const sidechain_transaction_object &sto, bool &complete);
bool send_sidechain_transaction(const sidechain_transaction_object &sto, std::string &sidechain_transaction);
std::string process_sidechain_transaction(const sidechain_transaction_object &sto);
std::string send_sidechain_transaction(const sidechain_transaction_object &sto);

private:
std::string ip;
Expand All @@ -111,19 +111,19 @@ class sidechain_net_handler_bitcoin : public sidechain_net_handler {
std::string create_withdrawal_transaction(const son_wallet_withdraw_object &swwo);

std::string create_transaction(const std::vector<btc_txout> &inputs, const fc::flat_map<std::string, double> outputs);
std::string sign_transaction(const sidechain_transaction_object &sto, bool &complete);
bool send_transaction(const sidechain_transaction_object &sto, std::string &sidechain_transaction);
std::string sign_transaction(const sidechain_transaction_object &sto);
std::string send_transaction(const sidechain_transaction_object &sto);

std::string create_transaction_raw(const std::vector<btc_txout> &inputs, const fc::flat_map<std::string, double> outputs);
std::string create_transaction_psbt(const std::vector<btc_txout> &inputs, const fc::flat_map<std::string, double> outputs);
std::string create_transaction_standalone(const std::vector<btc_txout> &inputs, const fc::flat_map<std::string, double> outputs);

std::string sign_transaction_raw(const sidechain_transaction_object &sto, bool &complete);
std::string sign_transaction_psbt(const sidechain_transaction_object &sto, bool &complete);
std::string sign_transaction_standalone(const sidechain_transaction_object &sto, bool &complete);
std::string sign_transaction_raw(const sidechain_transaction_object &sto);
std::string sign_transaction_psbt(const sidechain_transaction_object &sto);
std::string sign_transaction_standalone(const sidechain_transaction_object &sto);

bool send_transaction_raw(const sidechain_transaction_object &sto, std::string &sidechain_transaction);
bool send_transaction_psbt(const sidechain_transaction_object &sto, std::string &sidechain_transaction);
std::string send_transaction_raw(const sidechain_transaction_object &sto);
std::string send_transaction_psbt(const sidechain_transaction_object &sto);

void handle_event(const std::string &event_data);
std::vector<info_for_vin> extract_info_from_block(const std::string &_block);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ class sidechain_net_handler_peerplays : public sidechain_net_handler {
void process_primary_wallet();
bool process_deposit(const son_wallet_deposit_object &swdo);
bool process_withdrawal(const son_wallet_withdraw_object &swwo);
std::string process_sidechain_transaction(const sidechain_transaction_object &sto, bool &complete);
bool send_sidechain_transaction(const sidechain_transaction_object &sto, std::string &sidechain_transaction);
std::string process_sidechain_transaction(const sidechain_transaction_object &sto);
std::string send_sidechain_transaction(const sidechain_transaction_object &sto);

private:
void on_applied_block(const signed_block &b);
Expand Down
11 changes: 5 additions & 6 deletions libraries/plugins/peerplays_sidechain/sidechain_net_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ void sidechain_net_handler::process_sidechain_transactions() {
ilog("Sidechain transaction to process: ${sto}", ("sto", sto.id));

bool complete = false;
std::string processed_sidechain_tx = process_sidechain_transaction(sto, complete);
std::string processed_sidechain_tx = process_sidechain_transaction(sto);

if (processed_sidechain_tx.empty()) {
wlog("Sidechain transaction not processed: ${sto}", ("sto", sto.id));
Expand Down Expand Up @@ -398,10 +398,9 @@ void sidechain_net_handler::send_sidechain_transactions() {
std::for_each(idx_range.first, idx_range.second, [&](const sidechain_transaction_object &sto) {
ilog("Sidechain transaction to send: ${sto}", ("sto", sto.id));

std::string sidechain_transaction = "";
bool sent = send_sidechain_transaction(sto, sidechain_transaction);
std::string sidechain_transaction = send_sidechain_transaction(sto);

if (!sent) {
if (sidechain_transaction.empty()) {
wlog("Sidechain transaction not sent: ${sto}", ("sto", sto.id));
return;
}
Expand Down Expand Up @@ -439,11 +438,11 @@ bool sidechain_net_handler::process_withdrawal(const son_wallet_withdraw_object
FC_ASSERT(false, "process_withdrawal not implemented");
}

std::string sidechain_net_handler::process_sidechain_transaction(const sidechain_transaction_object &sto, bool &complete) {
std::string sidechain_net_handler::process_sidechain_transaction(const sidechain_transaction_object &sto) {
FC_ASSERT(false, "process_sidechain_transaction not implemented");
}

bool sidechain_net_handler::send_sidechain_transaction(const sidechain_transaction_object &sto, std::string &sidechain_transaction) {
std::string sidechain_net_handler::send_sidechain_transaction(const sidechain_transaction_object &sto) {
FC_ASSERT(false, "send_sidechain_transaction not implemented");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -603,31 +603,30 @@ std::string bitcoin_rpc_client::loadwallet(const std::string &filename) {
return "";
}

bool bitcoin_rpc_client::sendrawtransaction(const std::string &tx_hex) {
std::string bitcoin_rpc_client::sendrawtransaction(const std::string &tx_hex) {
const auto body = std::string("{\"jsonrpc\": \"1.0\", \"id\":\"sendrawtransaction\", "
"\"method\": \"sendrawtransaction\", \"params\": [") +
std::string("\"") + tx_hex + std::string("\"") + std::string("] }");

const auto reply = send_post_request(body);
const auto reply = send_post_request(body, true);

if (reply.body.empty()) {
wlog("Bitcoin RPC call ${function} failed", ("function", __FUNCTION__));
return false;
return "";
}

std::stringstream ss(std::string(reply.body.begin(), reply.body.end()));
boost::property_tree::ptree json;
boost::property_tree::read_json(ss, json);

if (reply.status == 200) {
return true;
} else if (json.count("error") && !json.get_child("error").empty()) {
const auto error_code = json.get_child("error").get_child("code").get_value<int>();
if (error_code == -27) // transaction already in block chain
return true;
return json.get<std::string>("result");
}

if (json.count("error") && !json.get_child("error").empty()) {
wlog("Bitcoin RPC call ${function} with body ${body} failed with reply '${msg}'", ("function", __FUNCTION__)("body", body)("msg", ss.str()));
}
return false;
return "";
}

std::string bitcoin_rpc_client::signrawtransactionwithwallet(const std::string &tx_hash) {
Expand Down Expand Up @@ -1217,9 +1216,7 @@ bool sidechain_net_handler_bitcoin::process_withdrawal(const son_wallet_withdraw
return false;
}

std::string sidechain_net_handler_bitcoin::process_sidechain_transaction(const sidechain_transaction_object &sto, bool &complete) {
complete = false;

std::string sidechain_net_handler_bitcoin::process_sidechain_transaction(const sidechain_transaction_object &sto) {
//// Uncomment to get signing in order from sto.signers
//son_id_type invalid_signer = son_id_type(0xFFFFFFFF);
//son_id_type next_signer = invalid_signer;
Expand All @@ -1234,13 +1231,11 @@ std::string sidechain_net_handler_bitcoin::process_sidechain_transaction(const s
// return "";
//}

return sign_transaction(sto, complete);
return sign_transaction(sto);
}

bool sidechain_net_handler_bitcoin::send_sidechain_transaction(const sidechain_transaction_object &sto, std::string &sidechain_transaction) {
sidechain_transaction = "";

return send_transaction(sto, sidechain_transaction);
std::string sidechain_net_handler_bitcoin::send_sidechain_transaction(const sidechain_transaction_object &sto) {
return send_transaction(sto);
}

std::string sidechain_net_handler_bitcoin::create_primary_wallet_transaction() {
Expand Down Expand Up @@ -1395,19 +1390,17 @@ std::string sidechain_net_handler_bitcoin::create_transaction(const std::vector<

// Adds signature to transaction
// Function to actually add signature should return transaction with added signature string, or empty string in case of failure
std::string sidechain_net_handler_bitcoin::sign_transaction(const sidechain_transaction_object &sto, bool &complete) {
complete = false;
std::string sidechain_net_handler_bitcoin::sign_transaction(const sidechain_transaction_object &sto) {
std::string new_tx = "";
//new_tx = sign_transaction_raw(sto, complete);
new_tx = sign_transaction_psbt(sto, complete);
//new_tx = sign_transaction_standalone(sto, complete);
//new_tx = sign_transaction_raw(sto);
new_tx = sign_transaction_psbt(sto);
//new_tx = sign_transaction_standalone(sto);
return new_tx;
}

bool sidechain_net_handler_bitcoin::send_transaction(const sidechain_transaction_object &sto, std::string &sidechain_transaction) {
sidechain_transaction = "";
//return send_transaction_raw(sto, sidechain_transaction);
return send_transaction_psbt(sto, sidechain_transaction);
std::string sidechain_net_handler_bitcoin::send_transaction(const sidechain_transaction_object &sto) {
//return send_transaction_raw(sto);
return send_transaction_psbt(sto);
}

std::string sidechain_net_handler_bitcoin::create_transaction_raw(const std::vector<btc_txout> &inputs, const fc::flat_map<std::string, double> outputs) {
Expand Down Expand Up @@ -1479,9 +1472,7 @@ std::string sidechain_net_handler_bitcoin::create_transaction_standalone(const s
return "";
}

std::string sidechain_net_handler_bitcoin::sign_transaction_raw(const sidechain_transaction_object &sto, bool &complete) {
complete = false;

std::string sidechain_net_handler_bitcoin::sign_transaction_raw(const sidechain_transaction_object &sto) {
if (sto.transaction.empty()) {
elog("Signing failed, tx string is empty");
return "";
Expand All @@ -1507,15 +1498,12 @@ std::string sidechain_net_handler_bitcoin::sign_transaction_raw(const sidechain_
bool complete_raw = json_res.get<bool>("complete");

if (complete_raw) {
complete = true;
return new_tx_raw;
}
return new_tx_raw;
}

std::string sidechain_net_handler_bitcoin::sign_transaction_psbt(const sidechain_transaction_object &sto, bool &complete) {
complete = false;

std::string sidechain_net_handler_bitcoin::sign_transaction_psbt(const sidechain_transaction_object &sto) {
if (sto.transaction.empty()) {
elog("Signing failed, tx string is empty");
return "";
Expand Down Expand Up @@ -1571,26 +1559,19 @@ std::string sidechain_net_handler_bitcoin::sign_transaction_psbt(const sidechain
}
}

complete = complete_psbt;
return new_tx_psbt;
}

std::string sidechain_net_handler_bitcoin::sign_transaction_standalone(const sidechain_transaction_object &sto, bool &complete) {
complete = false;
std::string sidechain_net_handler_bitcoin::sign_transaction_standalone(const sidechain_transaction_object &sto) {

complete = true;
return "";
}

bool sidechain_net_handler_bitcoin::send_transaction_raw(const sidechain_transaction_object &sto, std::string &sidechain_transaction) {
sidechain_transaction = "";

std::string sidechain_net_handler_bitcoin::send_transaction_raw(const sidechain_transaction_object &sto) {
return bitcoin_client->sendrawtransaction(sto.transaction);
}

bool sidechain_net_handler_bitcoin::send_transaction_psbt(const sidechain_transaction_object &sto, std::string &sidechain_transaction) {
sidechain_transaction = "";

std::string sidechain_net_handler_bitcoin::send_transaction_psbt(const sidechain_transaction_object &sto) {
vector<std::string> psbts;
for (auto signature : sto.signatures) {
if (!signature.second.empty()) {
Expand All @@ -1606,7 +1587,7 @@ bool sidechain_net_handler_bitcoin::send_transaction_psbt(const sidechain_transa

if (json.count("error") && !json.get_child("error").empty()) {
elog("Failed to combine psbt transactions from ${sto}", ("sto", sto));
return false;
return "";
}

std::string new_tx_psbt = json.get<std::string>("result");
Expand All @@ -1619,7 +1600,7 @@ bool sidechain_net_handler_bitcoin::send_transaction_psbt(const sidechain_transa

if ((json_res.count("hex") == 0) || (json_res.count("complete") == 0)) {
elog("Failed to finalize psbt transaction ${tx}", ("tx", new_tx_psbt));
return false;
return "";
}

std::string new_tx_raw = json_res.get<std::string>("hex");
Expand All @@ -1629,8 +1610,8 @@ bool sidechain_net_handler_bitcoin::send_transaction_psbt(const sidechain_transa
return bitcoin_client->sendrawtransaction(new_tx_raw);
}

return false;
} // namespace peerplays_sidechain
return "";
}

void sidechain_net_handler_bitcoin::handle_event(const std::string &event_data) {
std::string block = bitcoin_client->getblock(event_data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,12 @@ bool sidechain_net_handler_peerplays::process_withdrawal(const son_wallet_withdr
return true;
}

std::string sidechain_net_handler_peerplays::process_sidechain_transaction(const sidechain_transaction_object &sto, bool &complete) {
complete = true;
std::string sidechain_net_handler_peerplays::process_sidechain_transaction(const sidechain_transaction_object &sto) {
return sto.transaction;
}

bool sidechain_net_handler_peerplays::send_sidechain_transaction(const sidechain_transaction_object &sto, std::string &sidechain_transaction) {
sidechain_transaction = "";
return true;
std::string sidechain_net_handler_peerplays::send_sidechain_transaction(const sidechain_transaction_object &sto) {
return sto.transaction;
}

void sidechain_net_handler_peerplays::on_applied_block(const signed_block &b) {
Expand Down