Compare commits

...

17 commits

Author SHA1 Message Date
Srdjan Obucina
b6514577a7 Remove logs 2020-04-15 03:13:17 +02:00
Srdjan Obucina
d937863949 Align with SONs-base 2020-04-15 01:15:34 +02:00
Srdjan Obucina
40fb681162 Merge branch 'feature/SONs-base' into feature/SON-349_SON-350 2020-04-15 00:41:58 +02:00
Srdjan Obucina
0a5ad52ee1 Merge branch 'feature/SON-353' into feature/SON-349_SON-350 2020-04-14 23:26:46 +02:00
Srdjan Obucina
5ec353f46b Remove logs 2020-04-14 20:42:48 +02:00
Srdjan Obucina
5284a1487c Fix transaction validation 2020-04-14 19:04:17 +02:00
Srdjan Obucina
3f71b7de1f Fix segfault 2020-04-14 07:48:59 +02:00
Srdjan Obucina
703a13dab4 Fix transaction verification 2020-04-14 07:27:18 +02:00
Srdjan Obucina
bc25f6d20d Fix transaction verification 2020-04-14 06:59:20 +02:00
Srdjan Obucina
2d079cacf6 Fix build error 2020-04-14 06:05:53 +02:00
Srdjan Obucina
16a090fc5e Merge branch 'feature/SON-353' into feature/SON-349_SON-350 2020-04-14 05:46:49 +02:00
Srdjan Obucina
4507601992 Refactor proposal approvement 2020-04-14 05:27:14 +02:00
Srdjan Obucina
88c98db1f4 Add proposal checks for deposit and withdrawal 2020-04-14 05:15:17 +02:00
Srdjan Obucina
ba6fd8e690 [SON-353] Refactor PW processing, PW transfer fixed 2020-04-14 04:50:29 +02:00
Srdjan Obucina
7dd0afe257 Handle peerplays deposits with transaction settling 2020-04-13 07:07:42 +02:00
Srdjan Obucina
66713f0b48 Separate transaction settling from deposit/withdrawal processing 2020-04-13 01:43:09 +02:00
Srdjan Obucina
c77c19563c [SON-349, SON-350] Delay BTC asset issue/reserve until tx confirmed on sidchain 2020-04-11 03:18:41 +02:00
18 changed files with 356 additions and 69 deletions

2
docs

@ -1 +1 @@
Subproject commit 8d8b69d82482101279460fa02f814d0e4030966f
Subproject commit 740407c22154634aa0881e6b85e19ad0191e8325

View file

@ -272,6 +272,7 @@ void database::initialize_evaluators()
register_evaluator<sidechain_transaction_create_evaluator>();
register_evaluator<sidechain_transaction_sign_evaluator>();
register_evaluator<sidechain_transaction_send_evaluator>();
register_evaluator<sidechain_transaction_settle_evaluator>();
}
void database::initialize_indexes()

View file

@ -347,6 +347,9 @@ struct get_impacted_account_visitor
void operator()( const sidechain_transaction_send_operation& op ) {
_impacted.insert( op.payer );
}
void operator()( const sidechain_transaction_settle_operation& op ) {
_impacted.insert( op.payer );
}
};
void graphene::chain::operation_get_impacted_accounts( const operation& op, flat_set<account_id_type>& result )

View file

@ -234,7 +234,7 @@
#define SON_DEREGISTER_TIME (60*60*12) // 12 Hours in seconds
#define SON_HEARTBEAT_FREQUENCY (60*3) // 3 minutes in seconds
#define SON_DOWN_TIME (60*3*2) // 2 Heartbeats in seconds
#define SON_BITCOIN_MIN_TX_CONFIRMATIONS (1)
#define SON_BITCOIN_MIN_TX_CONFIRMATIONS (6)
#define SON_PAY_TIME (60*60*24) // 1 day
#define SON_PAY_MAX (GRAPHENE_BLOCKCHAIN_PRECISION * int64_t(200))
#define SWEEPS_DEFAULT_DISTRIBUTION_PERCENTAGE (2*GRAPHENE_1_PERCENT)

View file

@ -159,7 +159,8 @@ namespace graphene { namespace chain {
sidechain_address_delete_operation,
sidechain_transaction_create_operation,
sidechain_transaction_sign_operation,
sidechain_transaction_send_operation
sidechain_transaction_send_operation,
sidechain_transaction_settle_operation
> operation;
/// @} // operations group

View file

@ -50,6 +50,19 @@ namespace graphene { namespace chain {
share_type calculate_fee( const fee_parameters_type& k )const { return 0; }
};
struct sidechain_transaction_settle_operation : public base_operation
{
struct fee_parameters_type { uint64_t fee = 0; };
asset fee;
account_id_type payer;
sidechain_transaction_id_type sidechain_transaction_id;
account_id_type fee_payer()const { return payer; }
share_type calculate_fee( const fee_parameters_type& k )const { return 0; }
};
} } // graphene::chain
FC_REFLECT( graphene::chain::sidechain_transaction_create_operation::fee_parameters_type, (fee) )
@ -68,3 +81,7 @@ FC_REFLECT( graphene::chain::sidechain_transaction_send_operation::fee_parameter
FC_REFLECT( graphene::chain::sidechain_transaction_send_operation, (fee)(payer)
(sidechain_transaction_id)
(sidechain_transaction) )
FC_REFLECT( graphene::chain::sidechain_transaction_settle_operation::fee_parameters_type, (fee) )
FC_REFLECT( graphene::chain::sidechain_transaction_settle_operation, (fee)(payer)
(sidechain_transaction_id) )

View file

@ -31,4 +31,13 @@ public:
object_id_type do_apply(const sidechain_transaction_send_operation& o);
};
class sidechain_transaction_settle_evaluator : public evaluator<sidechain_transaction_settle_evaluator>
{
public:
typedef sidechain_transaction_settle_operation operation_type;
void_result do_evaluate(const sidechain_transaction_settle_operation& o);
object_id_type do_apply(const sidechain_transaction_settle_operation& o);
};
} } // namespace graphene::chain

View file

@ -7,6 +7,14 @@
namespace graphene { namespace chain {
using namespace graphene::db;
enum class sidechain_transaction_status {
invalid,
valid,
complete,
sent,
settled
};
/**
* @class sidechain_transaction_object
* @brief tracks state of sidechain transaction during signing process.
@ -28,14 +36,12 @@ namespace graphene { namespace chain {
uint32_t total_weight = 0;
uint32_t current_weight = 0;
uint32_t threshold = 0;
bool valid = false;
bool complete = false;
bool sent = false;
sidechain_transaction_status status;
};
struct by_object_id;
struct by_sidechain_and_complete;
struct by_sidechain_and_complete_and_sent;
struct by_sidechain_and_status;
using sidechain_transaction_multi_index_type = multi_index_container<
sidechain_transaction_object,
indexed_by<
@ -45,17 +51,10 @@ namespace graphene { namespace chain {
ordered_unique< tag<by_object_id>,
member<sidechain_transaction_object, object_id_type, &sidechain_transaction_object::object_id>
>,
ordered_non_unique< tag<by_sidechain_and_complete>,
ordered_non_unique< tag<by_sidechain_and_status>,
composite_key<sidechain_transaction_object,
member<sidechain_transaction_object, sidechain_type, &sidechain_transaction_object::sidechain>,
member<sidechain_transaction_object, bool, &sidechain_transaction_object::complete>
>
>,
ordered_non_unique< tag<by_sidechain_and_complete_and_sent>,
composite_key<sidechain_transaction_object,
member<sidechain_transaction_object, sidechain_type, &sidechain_transaction_object::sidechain>,
member<sidechain_transaction_object, bool, &sidechain_transaction_object::complete>,
member<sidechain_transaction_object, bool, &sidechain_transaction_object::sent>
member<sidechain_transaction_object, sidechain_transaction_status, &sidechain_transaction_object::status>
>
>
>
@ -63,6 +62,13 @@ namespace graphene { namespace chain {
using sidechain_transaction_index = generic_index<sidechain_transaction_object, sidechain_transaction_multi_index_type>;
} } // graphene::chain
FC_REFLECT_ENUM( graphene::chain::sidechain_transaction_status,
(invalid)
(valid)
(complete)
(sent)
(settled) )
FC_REFLECT_DERIVED( graphene::chain::sidechain_transaction_object, (graphene::db::object ),
(sidechain)
(object_id)
@ -73,6 +79,4 @@ FC_REFLECT_DERIVED( graphene::chain::sidechain_transaction_object, (graphene::db
(total_weight)
(current_weight)
(threshold)
(valid)
(complete)
(sent) )
(status) )

View file

@ -41,9 +41,7 @@ object_id_type sidechain_transaction_create_evaluator::do_apply(const sidechain_
sto.sidechain_transaction = "";
sto.current_weight = 0;
sto.threshold = sto.total_weight * 2 / 3 + 1;
sto.valid = true;
sto.complete = false;
sto.sent = false;
sto.status = sidechain_transaction_status::valid;
});
return new_sidechain_transaction_object.id;
} FC_CAPTURE_AND_RETHROW( ( op ) ) }
@ -68,9 +66,7 @@ void_result sidechain_transaction_sign_evaluator::do_evaluate(const sidechain_tr
}
FC_ASSERT(expected, "Signer not expected");
FC_ASSERT(sto_obj->valid, "Transaction not valid");
FC_ASSERT(!sto_obj->complete, "Transaction signing completed");
FC_ASSERT(!sto_obj->sent, "Transaction already sent");
FC_ASSERT(sto_obj->status == sidechain_transaction_status::valid, "Invalid transaction status");
return void_result();
} FC_CAPTURE_AND_RETHROW( ( op ) ) }
@ -94,7 +90,9 @@ object_id_type sidechain_transaction_sign_evaluator::do_apply(const sidechain_tr
sto.current_weight = sto.current_weight + sto.signers.at(i).weight;
}
}
sto.complete = (sto.current_weight >= sto.threshold);
if (sto.current_weight >= sto.threshold) {
sto.status = sidechain_transaction_status::complete;
}
});
db().modify(son_obj->statistics(db()), [&](son_statistics_object& sso) {
@ -112,9 +110,7 @@ void_result sidechain_transaction_send_evaluator::do_evaluate(const sidechain_tr
const auto &sto_obj = sto_idx.find(op.sidechain_transaction_id);
FC_ASSERT(sto_obj != sto_idx.end(), "Sidechain transaction object not found");
FC_ASSERT(sto_obj->valid, "Transaction not valid");
FC_ASSERT(sto_obj->complete, "Transaction signing not complete");
FC_ASSERT(!sto_obj->sent, "Transaction already sent");
FC_ASSERT(sto_obj->status == sidechain_transaction_status::complete, "Invalid transaction status");
return void_result();
} FC_CAPTURE_AND_RETHROW( ( op ) ) }
@ -126,7 +122,33 @@ object_id_type sidechain_transaction_send_evaluator::do_apply(const sidechain_tr
db().modify(*sto_obj, [&](sidechain_transaction_object &sto) {
sto.sidechain_transaction = op.sidechain_transaction;
sto.sent = true;
sto.status = sidechain_transaction_status::sent;
});
return op.sidechain_transaction_id;
} FC_CAPTURE_AND_RETHROW( ( op ) ) }
void_result sidechain_transaction_settle_evaluator::do_evaluate(const sidechain_transaction_settle_operation &op)
{ try {
FC_ASSERT(db().head_block_time() >= HARDFORK_SON_TIME, "Not allowed until SON HARDFORK"); // can be removed after HF date pass
const auto &sto_idx = db().get_index_type<sidechain_transaction_index>().indices().get<by_id>();
const auto &sto_obj = sto_idx.find(op.sidechain_transaction_id);
FC_ASSERT(sto_obj != sto_idx.end(), "Sidechain transaction object not found");
FC_ASSERT(sto_obj->status == sidechain_transaction_status::sent, "Invalid transaction status");
return void_result();
} FC_CAPTURE_AND_RETHROW( ( op ) ) }
object_id_type sidechain_transaction_settle_evaluator::do_apply(const sidechain_transaction_settle_operation &op)
{ try {
const auto &sto_idx = db().get_index_type<sidechain_transaction_index>().indices().get<by_id>();
auto sto_obj = sto_idx.find(op.sidechain_transaction_id);
db().modify(*sto_obj, [&](sidechain_transaction_object &sto) {
sto.status = sidechain_transaction_status::settled;
});
return op.sidechain_transaction_id;

View file

@ -35,6 +35,7 @@ public:
void process_withdrawals();
void process_sidechain_transactions();
void send_sidechain_transactions();
void settle_sidechain_transactions();
virtual bool process_proposal(const proposal_object &po) = 0;
virtual void process_primary_wallet() = 0;
@ -42,6 +43,7 @@ public:
virtual bool process_withdrawal(const son_wallet_withdraw_object &swwo) = 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;
virtual int64_t settle_sidechain_transaction(const sidechain_transaction_object &sto) = 0;
protected:
peerplays_sidechain_plugin &plugin;

View file

@ -91,6 +91,7 @@ public:
bool process_withdrawal(const son_wallet_withdraw_object &swwo);
std::string process_sidechain_transaction(const sidechain_transaction_object &sto);
std::string send_sidechain_transaction(const sidechain_transaction_object &sto);
int64_t settle_sidechain_transaction(const sidechain_transaction_object &sto);
private:
std::string ip;

View file

@ -19,6 +19,7 @@ public:
bool process_withdrawal(const son_wallet_withdraw_object &swwo);
std::string process_sidechain_transaction(const sidechain_transaction_object &sto);
std::string send_sidechain_transaction(const sidechain_transaction_object &sto);
int64_t settle_sidechain_transaction(const sidechain_transaction_object &sto);
private:
void on_applied_block(const signed_block &b);

View file

@ -22,6 +22,7 @@ public:
void process_withdrawals();
void process_sidechain_transactions();
void send_sidechain_transactions();
void settle_sidechain_transactions();
private:
peerplays_sidechain_plugin &plugin;

View file

@ -57,6 +57,7 @@ public:
void process_withdrawals();
void process_sidechain_transactions();
void send_sidechain_transactions();
void settle_sidechain_transactions();
private:
peerplays_sidechain_plugin &plugin;
@ -401,6 +402,8 @@ void peerplays_sidechain_plugin_impl::son_processing() {
process_sidechain_transactions();
send_sidechain_transactions();
settle_sidechain_transactions();
}
}
}
@ -586,6 +589,10 @@ void peerplays_sidechain_plugin_impl::send_sidechain_transactions() {
net_manager->send_sidechain_transactions();
}
void peerplays_sidechain_plugin_impl::settle_sidechain_transactions() {
net_manager->settle_sidechain_transactions();
}
void peerplays_sidechain_plugin_impl::on_applied_block(const signed_block &b) {
if (first_block_skipped) {
schedule_son_processing();

View file

@ -242,8 +242,6 @@ void sidechain_net_handler::process_proposals() {
const auto po = idx.find(proposal_id);
if (po != idx.end()) {
ilog("Proposal to process: ${po}, SON id ${son_id}", ("po", (*po).id)("son_id", plugin.get_current_son_id()));
if (po->available_active_approvals.find(plugin.get_current_son_object().son_account) != po->available_active_approvals.end()) {
continue;
}
@ -290,6 +288,16 @@ void sidechain_net_handler::process_proposals() {
break;
}
case chain::operation::tag<chain::sidechain_transaction_settle_operation>::value: {
sidechain_transaction_id_type st_id = op_obj_idx_0.get<sidechain_transaction_settle_operation>().sidechain_transaction_id;
const auto &idx = database.get_index_type<sidechain_transaction_index>().indices().get<by_id>();
const auto sto = idx.find(st_id);
if (sto != idx.end()) {
should_process = (sto->sidechain == sidechain);
}
break;
}
default:
should_process = false;
elog("==================================================");
@ -298,16 +306,16 @@ void sidechain_net_handler::process_proposals() {
}
if (should_process) {
ilog("Proposal ${po} will be processed by sidechain handler ${sidechain}", ("po", (*po).id)("sidechain", sidechain));
//ilog("Proposal ${po} will be processed by sidechain handler ${sidechain}", ("po", (*po).id)("sidechain", sidechain));
bool should_approve = process_proposal(*po);
if (should_approve) {
ilog("Proposal ${po} will be approved", ("po", *po));
//ilog("Proposal ${po} will be approved", ("po", (*po).id));
approve_proposal(po->id, plugin.get_current_son_id());
} else {
ilog("Proposal ${po} is not approved", ("po", (*po).id));
//ilog("Proposal ${po} is not approved", ("po", (*po).id));
}
} else {
ilog("Proposal ${po} will not be processed by sidechain handler ${sidechain}", ("po", (*po).id)("sidechain", sidechain));
//ilog("Proposal ${po} will not be processed by sidechain handler ${sidechain}", ("po", (*po).id)("sidechain", sidechain));
}
}
}
@ -341,17 +349,9 @@ void sidechain_net_handler::process_deposits() {
swdp_op.payer = gpo.parameters.son_account();
swdp_op.son_wallet_deposit_id = swdo.id;
asset_issue_operation ai_op;
ai_op.fee = asset(2001000);
ai_op.issuer = gpo.parameters.son_account();
price btc_price = database.get<asset_object>(database.get_global_properties().parameters.btc_asset()).options.core_exchange_rate;
ai_op.asset_to_issue = asset(swdo.peerplays_asset.amount * btc_price.quote.amount / btc_price.base.amount, database.get_global_properties().parameters.btc_asset());
ai_op.issue_to_account = swdo.peerplays_from;
proposal_create_operation proposal_op;
proposal_op.fee_paying_account = plugin.get_current_son_object().son_account;
proposal_op.proposed_ops.emplace_back(swdp_op);
proposal_op.proposed_ops.emplace_back(ai_op);
uint32_t lifetime = (gpo.parameters.block_interval * gpo.active_witnesses.size()) * 3;
proposal_op.expiration_time = time_point_sec(database.head_block_time().sec_since_epoch() + lifetime);
@ -391,15 +391,9 @@ void sidechain_net_handler::process_withdrawals() {
swwp_op.payer = gpo.parameters.son_account();
swwp_op.son_wallet_withdraw_id = swwo.id;
asset_reserve_operation ar_op;
ar_op.fee = asset(2001000);
ar_op.payer = gpo.parameters.son_account();
ar_op.amount_to_reserve = asset(swwo.withdraw_amount, database.get_global_properties().parameters.btc_asset());
proposal_create_operation proposal_op;
proposal_op.fee_paying_account = plugin.get_current_son_object().son_account;
proposal_op.proposed_ops.emplace_back(swwp_op);
proposal_op.proposed_ops.emplace_back(ar_op);
uint32_t lifetime = (gpo.parameters.block_interval * gpo.active_witnesses.size()) * 3;
proposal_op.expiration_time = time_point_sec(database.head_block_time().sec_since_epoch() + lifetime);
@ -416,13 +410,12 @@ void sidechain_net_handler::process_withdrawals() {
}
void sidechain_net_handler::process_sidechain_transactions() {
const auto &idx = database.get_index_type<sidechain_transaction_index>().indices().get<by_sidechain_and_complete>();
const auto &idx_range = idx.equal_range(std::make_tuple(sidechain, false));
const auto &idx = database.get_index_type<sidechain_transaction_index>().indices().get<by_sidechain_and_status>();
const auto &idx_range = idx.equal_range(std::make_tuple(sidechain, sidechain_transaction_status::valid));
std::for_each(idx_range.first, idx_range.second, [&](const sidechain_transaction_object &sto) {
ilog("Sidechain transaction to process: ${sto}", ("sto", sto.id));
bool complete = false;
std::string processed_sidechain_tx = process_sidechain_transaction(sto);
if (processed_sidechain_tx.empty()) {
@ -448,8 +441,8 @@ void sidechain_net_handler::process_sidechain_transactions() {
}
void sidechain_net_handler::send_sidechain_transactions() {
const auto &idx = database.get_index_type<sidechain_transaction_index>().indices().get<by_sidechain_and_complete_and_sent>();
const auto &idx_range = idx.equal_range(std::make_tuple(sidechain, true, false));
const auto &idx = database.get_index_type<sidechain_transaction_index>().indices().get<by_sidechain_and_status>();
const auto &idx_range = idx.equal_range(std::make_tuple(sidechain, sidechain_transaction_status::complete));
std::for_each(idx_range.first, idx_range.second, [&](const sidechain_transaction_object &sto) {
ilog("Sidechain transaction to send: ${sto}", ("sto", sto.id));
@ -478,6 +471,64 @@ void sidechain_net_handler::send_sidechain_transactions() {
});
}
void sidechain_net_handler::settle_sidechain_transactions() {
const auto &idx = database.get_index_type<sidechain_transaction_index>().indices().get<by_sidechain_and_status>();
const auto &idx_range = idx.equal_range(std::make_tuple(sidechain, sidechain_transaction_status::sent));
std::for_each(idx_range.first, idx_range.second, [&](const sidechain_transaction_object &sto) {
ilog("Sidechain transaction to settle: ${sto}", ("sto", sto.id));
int64_t settle_amount = settle_sidechain_transaction(sto);
if (settle_amount < 0) {
wlog("Sidechain transaction not settled: ${sto}", ("sto", sto.id));
return;
}
const chain::global_property_object &gpo = database.get_global_properties();
sidechain_transaction_settle_operation sts_op;
sts_op.payer = gpo.parameters.son_account();
sts_op.sidechain_transaction_id = sto.id;
proposal_create_operation proposal_op;
proposal_op.fee_paying_account = plugin.get_current_son_object().son_account;
proposal_op.proposed_ops.emplace_back(sts_op);
if (settle_amount != 0) {
if (sto.object_id.is<son_wallet_deposit_id_type>()) {
asset_issue_operation ai_op;
ai_op.fee = asset(2001000);
ai_op.issuer = gpo.parameters.son_account();
ai_op.asset_to_issue = asset(settle_amount, database.get_global_properties().parameters.btc_asset());
ai_op.issue_to_account = database.get<son_wallet_deposit_object>(sto.object_id).peerplays_from;
proposal_op.proposed_ops.emplace_back(ai_op);
}
if (sto.object_id.is<son_wallet_withdraw_id_type>()) {
asset_reserve_operation ar_op;
ar_op.fee = asset(2001000);
ar_op.payer = gpo.parameters.son_account();
ar_op.amount_to_reserve = asset(settle_amount, database.get_global_properties().parameters.btc_asset());
proposal_op.proposed_ops.emplace_back(ar_op);
}
}
uint32_t lifetime = (gpo.parameters.block_interval * gpo.active_witnesses.size()) * 3;
proposal_op.expiration_time = time_point_sec(database.head_block_time().sec_since_epoch() + lifetime);
signed_transaction trx = database.create_signed_transaction(plugin.get_private_key(plugin.get_current_son_id()), proposal_op);
trx.validate();
try {
database.push_transaction(trx, database::validation_steps::skip_block_size_check);
if (plugin.app().p2p_node())
plugin.app().p2p_node()->broadcast(net::trx_message(trx));
} catch (fc::exception e) {
elog("Sending proposal for sidechain transaction settle operation failed with exception ${e}", ("e", e.what()));
}
});
}
bool sidechain_net_handler::process_proposal(const proposal_object &po) {
FC_ASSERT(false, "process_proposal not implemented");
}

View file

@ -1020,17 +1020,19 @@ bool sidechain_net_handler_bitcoin::process_proposal(const proposal_object &po)
std::string tx_txid = tx_json.get<std::string>("result.txid");
uint32_t tx_confirmations = tx_json.get<uint32_t>("result.confirmations");
std::string tx_address = "";
uint64_t tx_amount = 0;
uint64_t tx_vout = 0;
int64_t tx_amount = -1;
int64_t tx_vout = -1;
for (auto &input : tx_json.get_child("result.details")) {
tx_address = input.second.get<std::string>("address");
std::string tx_amount_s = input.second.get<std::string>("amount");
tx_amount_s.erase(std::remove(tx_amount_s.begin(), tx_amount_s.end(), '.'), tx_amount_s.end());
tx_amount = std::stoll(tx_amount_s);
std::string tx_vout_s = input.second.get<std::string>("vout");
tx_vout = std::stoll(tx_vout_s);
break;
if ((tx_address == swdo_address) && (input.second.get<std::string>("category") == "receive")) {
std::string tx_amount_s = input.second.get<std::string>("amount");
tx_amount_s.erase(std::remove(tx_amount_s.begin(), tx_amount_s.end(), '.'), tx_amount_s.end());
tx_amount = std::stoll(tx_amount_s);
std::string tx_vout_s = input.second.get<std::string>("vout");
tx_vout = std::stoll(tx_vout_s);
break;
}
}
should_approve = (swdo_txid == tx_txid) &&
@ -1079,6 +1081,11 @@ bool sidechain_net_handler_bitcoin::process_proposal(const proposal_object &po)
break;
}
case chain::operation::tag<chain::sidechain_transaction_settle_operation>::value: {
should_approve = true;
break;
}
default:
should_approve = false;
elog("==================================================");
@ -1265,6 +1272,49 @@ std::string sidechain_net_handler_bitcoin::send_sidechain_transaction(const side
return send_transaction(sto);
}
int64_t sidechain_net_handler_bitcoin::settle_sidechain_transaction(const sidechain_transaction_object &sto) {
int64_t settle_amount = -1;
if (sto.sidechain_transaction.empty()) {
return settle_amount;
}
std::string tx_str = bitcoin_client->gettransaction(sto.sidechain_transaction);
std::stringstream tx_ss(tx_str);
boost::property_tree::ptree tx_json;
boost::property_tree::read_json(tx_ss, tx_json);
if ((tx_json.count("error")) && (!tx_json.get_child("error").empty())) {
return settle_amount;
}
std::string tx_txid = tx_json.get<std::string>("result.txid");
uint32_t tx_confirmations = tx_json.get<uint32_t>("result.confirmations");
std::string tx_address = "";
int64_t tx_amount = -1;
const chain::global_property_object &gpo = database.get_global_properties();
if (tx_confirmations >= gpo.parameters.son_bitcoin_min_tx_confirmations()) {
if (sto.object_id.is<son_wallet_deposit_id_type>()) {
for (auto &input : tx_json.get_child("result.details")) {
if (input.second.get<std::string>("category") == "receive") {
std::string tx_amount_s = input.second.get<std::string>("amount");
tx_amount_s.erase(std::remove(tx_amount_s.begin(), tx_amount_s.end(), '.'), tx_amount_s.end());
tx_amount = std::stoll(tx_amount_s);
break;
}
}
settle_amount = tx_amount;
}
if (sto.object_id.is<son_wallet_withdraw_id_type>()) {
}
}
return settle_amount;
}
std::string sidechain_net_handler_bitcoin::create_primary_wallet_transaction(const son_wallet_object &prev_swo, std::string new_sw_address) {
std::stringstream prev_sw_ss(prev_swo.addresses.find(sidechain_type::bitcoin)->second);
@ -1744,5 +1794,4 @@ void sidechain_net_handler_bitcoin::on_changed_objects_cb(const vector<object_id
}
// =============================================================================
}} // namespace graphene::peerplays_sidechain

View file

@ -8,6 +8,7 @@
#include <boost/property_tree/ptree.hpp>
#include <fc/crypto/base64.hpp>
#include <fc/crypto/hex.hpp>
#include <fc/log/logger.hpp>
#include <fc/network/ip.hpp>
@ -15,12 +16,26 @@
#include <graphene/chain/protocol/son_wallet.hpp>
#include <graphene/chain/son_info.hpp>
#include <graphene/chain/son_wallet_object.hpp>
#include <graphene/utilities/key_conversion.hpp>
namespace graphene { namespace peerplays_sidechain {
sidechain_net_handler_peerplays::sidechain_net_handler_peerplays(peerplays_sidechain_plugin &_plugin, const boost::program_options::variables_map &options) :
sidechain_net_handler(_plugin, options) {
sidechain = sidechain_type::peerplays;
if (options.count("peerplays-private-key")) {
const std::vector<std::string> pub_priv_keys = options["peerplays-private-key"].as<std::vector<std::string>>();
for (const std::string &itr_key_pair : pub_priv_keys) {
auto key_pair = graphene::app::dejsonify<std::pair<std::string, std::string>>(itr_key_pair, 5);
ilog("Peerplays Public Key: ${public}", ("public", key_pair.first));
if (!key_pair.first.length() || !key_pair.second.length()) {
FC_THROW("Invalid public private key pair.");
}
private_keys[key_pair.first] = key_pair.second;
}
}
database.applied_block.connect([&](const signed_block &b) {
on_applied_block(b);
});
@ -115,7 +130,12 @@ bool sidechain_net_handler_peerplays::process_proposal(const proposal_object &po
}
case chain::operation::tag<chain::sidechain_transaction_create_operation>::value: {
should_approve = false;
should_approve = true;
break;
}
case chain::operation::tag<chain::sidechain_transaction_settle_operation>::value: {
should_approve = true;
break;
}
@ -134,7 +154,55 @@ void sidechain_net_handler_peerplays::process_primary_wallet() {
}
bool sidechain_net_handler_peerplays::process_deposit(const son_wallet_deposit_object &swdo) {
return true;
const chain::global_property_object &gpo = database.get_global_properties();
asset_issue_operation ai_op;
ai_op.issuer = gpo.parameters.son_account();
price btc_price = database.get<asset_object>(database.get_global_properties().parameters.btc_asset()).options.core_exchange_rate;
ai_op.asset_to_issue = asset(swdo.peerplays_asset.amount * btc_price.quote.amount / btc_price.base.amount, database.get_global_properties().parameters.btc_asset());
ai_op.issue_to_account = swdo.peerplays_from;
signed_transaction tx;
auto dyn_props = database.get_dynamic_global_properties();
tx.set_reference_block(dyn_props.head_block_id);
tx.set_expiration(database.head_block_time() + gpo.parameters.maximum_time_until_expiration);
tx.operations.push_back(ai_op);
database.current_fee_schedule().set_fee(tx.operations.back());
std::stringstream ss;
fc::raw::pack(ss, tx, 1000);
std::string tx_str = boost::algorithm::hex(ss.str());
if (!tx_str.empty()) {
const chain::global_property_object &gpo = database.get_global_properties();
sidechain_transaction_create_operation stc_op;
stc_op.payer = gpo.parameters.son_account();
stc_op.object_id = swdo.id;
stc_op.sidechain = sidechain;
stc_op.transaction = tx_str;
stc_op.signers = gpo.active_sons;
proposal_create_operation proposal_op;
proposal_op.fee_paying_account = plugin.get_current_son_object().son_account;
proposal_op.proposed_ops.emplace_back(stc_op);
uint32_t lifetime = (gpo.parameters.block_interval * gpo.active_witnesses.size()) * 3;
proposal_op.expiration_time = time_point_sec(database.head_block_time().sec_since_epoch() + lifetime);
signed_transaction trx = database.create_signed_transaction(plugin.get_private_key(plugin.get_current_son_id()), proposal_op);
trx.validate();
try {
database.push_transaction(trx, database::validation_steps::skip_block_size_check);
if (plugin.app().p2p_node())
plugin.app().p2p_node()->broadcast(net::trx_message(trx));
return true;
} catch (fc::exception e) {
elog("Sending proposal for deposit sidechain transaction create operation failed with exception ${e}", ("e", e.what()));
return false;
}
}
return false;
}
bool sidechain_net_handler_peerplays::process_withdrawal(const son_wallet_withdraw_object &swwo) {
@ -142,11 +210,55 @@ bool sidechain_net_handler_peerplays::process_withdrawal(const son_wallet_withdr
}
std::string sidechain_net_handler_peerplays::process_sidechain_transaction(const sidechain_transaction_object &sto) {
return sto.transaction;
std::stringstream ss_trx(boost::algorithm::unhex(sto.transaction));
signed_transaction trx;
fc::raw::unpack(ss_trx, trx, 1000);
fc::optional<fc::ecc::private_key> privkey = graphene::utilities::wif_to_key(get_private_key(plugin.get_current_son_object().sidechain_public_keys.at(sidechain)));
signature_type st = trx.sign(*privkey, database.get_chain_id());
std::stringstream ss_st;
fc::raw::pack(ss_st, st, 1000);
std::string st_str = boost::algorithm::hex(ss_st.str());
return st_str;
}
std::string sidechain_net_handler_peerplays::send_sidechain_transaction(const sidechain_transaction_object &sto) {
return sto.transaction;
std::stringstream ss_trx(boost::algorithm::unhex(sto.transaction));
signed_transaction trx;
fc::raw::unpack(ss_trx, trx, 1000);
for (auto signature : sto.signatures) {
if (!signature.second.empty()) {
std::stringstream ss_st(boost::algorithm::unhex(signature.second));
signature_type st;
fc::raw::unpack(ss_st, st, 1000);
trx.signatures.push_back(st);
trx.signees.clear();
}
}
trx.validate();
try {
database.push_transaction(trx, database::validation_steps::skip_block_size_check);
if (plugin.app().p2p_node())
plugin.app().p2p_node()->broadcast(net::trx_message(trx));
return trx.id().str();
} catch (fc::exception e) {
elog("Sidechain transaction failed with exception ${e}", ("e", e.what()));
return "";
}
return "";
}
int64_t sidechain_net_handler_peerplays::settle_sidechain_transaction(const sidechain_transaction_object &sto) {
int64_t settle_amount = 0;
return settle_amount;
}
void sidechain_net_handler_peerplays::on_applied_block(const signed_block &b) {

View file

@ -75,4 +75,10 @@ void sidechain_net_manager::send_sidechain_transactions() {
}
}
void sidechain_net_manager::settle_sidechain_transactions() {
for (size_t i = 0; i < net_handlers.size(); i++) {
net_handlers.at(i)->settle_sidechain_transactions();
}
}
}} // namespace graphene::peerplays_sidechain