Skip to content
This repository was archived by the owner on Oct 4, 2019. It is now read-only.

Commit 1d8b20c

Browse files
authored
Merge pull request #410 from GolosChain/406-broadcast_transaction_with_callback
406 broadcast transaction with callback
2 parents d3037e4 + 13d08cc commit 1d8b20c

File tree

4 files changed

+65
-25
lines changed

4 files changed

+65
-25
lines changed

contribution/config.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ history-per-size = 5760
7171
enable-stale-production = false
7272

7373
# Percent of witnesses (0-99) that must be participating in order to produce blocks
74-
required-participation = false
74+
required-participation = 0
7575

7676
# name of witness controlled by this node (e.g. initwitness )
7777
# witness =

libraries/plugins/network_broadcast_api/include/golos/plugins/network_broadcast_api/network_broadcast_api_plugin.hpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,11 @@ namespace golos {
3535
bool expired = false;
3636
};
3737

38-
/// API, args, return
39-
DEFINE_API_ARGS(broadcast_transaction, msg_pack, void_type)
40-
DEFINE_API_ARGS(broadcast_transaction_synchronous, msg_pack, void_type)
41-
DEFINE_API_ARGS(broadcast_block, msg_pack, void_type)
38+
/// API, args, return
39+
DEFINE_API_ARGS(broadcast_transaction, msg_pack, void_type)
40+
DEFINE_API_ARGS(broadcast_transaction_synchronous, msg_pack, void_type)
41+
DEFINE_API_ARGS(broadcast_block, msg_pack, void_type)
42+
DEFINE_API_ARGS(broadcast_transaction_with_callback, msg_pack, void_type)
4243

4344

4445
using namespace appbase;
@@ -60,6 +61,7 @@ namespace golos {
6061
(broadcast_transaction)
6162
(broadcast_transaction_synchronous)
6263
(broadcast_block)
64+
(broadcast_transaction_with_callback)
6365
)
6466

6567
bool check_max_block_age(int32_t max_block_age) const;

libraries/plugins/network_broadcast_api/network_broadcast_api.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,41 @@ namespace golos {
8989
return broadcast_block_return();
9090
}
9191

92+
93+
DEFINE_API(network_broadcast_api_plugin,broadcast_transaction_with_callback){
94+
// TODO: implement commit semantic for delegating connection handlers
95+
const auto n_args = args.args->size();
96+
FC_ASSERT(n_args >= 1, "Expected at least 1 argument, got 0");
97+
auto trx = args.args->at(0).as<signed_transaction>();
98+
if (n_args > 1) {
99+
const auto max_block_age = args.args->at(1).as<uint32_t>();
100+
FC_ASSERT(!check_max_block_age(max_block_age));
101+
}
102+
103+
trx.validate();
104+
105+
// Delegate connection handlers to callback
106+
msg_pack_transfer transfer(args);
107+
108+
{
109+
boost::lock_guard<boost::mutex> guard(pimpl->_mtx);
110+
pimpl->_callbacks[trx.id()] = [msg = transfer.msg()](broadcast_transaction_synchronous_t r) {
111+
if (msg->valid()) {
112+
msg->result(std::move(r));
113+
}
114+
};
115+
pimpl->_callback_expirations[trx.expiration].push_back(trx.id());
116+
}
117+
118+
119+
pimpl->_chain.db().push_transaction(trx);
120+
pimpl->_p2p.broadcast_transaction(trx);
121+
transfer.complete();
122+
123+
return {};
124+
125+
}
126+
92127
bool network_broadcast_api_plugin::check_max_block_age(int32_t max_block_age) const {
93128
return pimpl->_chain.db().with_read_lock([&]() {
94129
if (max_block_age < 0) {

libraries/plugins/witness/witness.cpp

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -122,27 +122,20 @@ namespace golos {
122122
void witness_plugin::set_program_options(
123123
boost::program_options::options_description &command_line_options,
124124
boost::program_options::options_description &config_file_options) {
125-
string witness_id_example = "initwitness";
125+
string witness_id_example = "initwitness";
126+
126127
command_line_options.add_options()
127-
("enable-stale-production", bpo::bool_switch()->notifier([this](bool e) { pimpl->_production_enabled = e; }),
128-
"Enable block production, even if the chain is stale.")
129-
("required-participation", bpo::bool_switch()->notifier([this](int e) { pimpl->_required_witness_participation = uint32_t(e * STEEMIT_1_PERCENT);
130-
}), "Percent of witnesses (0-99) that must be participating in order to produce blocks")
131-
("witness,w", bpo::value<vector<string>>()->composing()->multitoken(),
132-
("name of witness controlled by this node (e.g. " +
133-
witness_id_example + " )").c_str())
134-
("miner,m", bpo::value<vector<string>>()->composing()->multitoken(),
135-
"name of miner and its private key (e.g. [\"account\",\"WIF PRIVATE KEY\"] )")
136-
("mining-threads,t", bpo::value<uint32_t>(),
137-
"Number of threads to use for proof of work mining")
138-
("private-key", bpo::value<vector<string>>()->composing()->multitoken(),
139-
"WIF PRIVATE KEY to be used by one or more witnesses or miners")
140-
("miner-account-creation-fee", bpo::value<uint64_t>()->implicit_value(100000),
141-
"Account creation fee to be voted on upon successful POW - Minimum fee is 100.000 STEEM (written as 100000)")
142-
("miner-maximum-block-size", bpo::value<uint32_t>()->implicit_value(131072),
143-
"Maximum block size (in bytes) to be voted on upon successful POW - Max block size must be between 128 KB and 750 MB")
144-
("miner-sbd-interest-rate", bpo::value<uint32_t>()->implicit_value(1000),
145-
"SBD interest rate to be vote on upon successful POW - Default interest rate is 10% (written as 1000)");
128+
("enable-stale-production", bpo::value<bool>()->implicit_value(false) , "Enable block production, even if the chain is stale.")
129+
("required-participation", bpo::value<int>()->implicit_value(uint32_t(3 * STEEMIT_1_PERCENT)), "Percent of witnesses (0-99) that must be participating in order to produce blocks")
130+
("witness,w", bpo::value<vector<string>>()->composing()->multitoken(), ("name of witness controlled by this node (e.g. " + witness_id_example + " )").c_str())
131+
("miner,m", bpo::value<vector<string>>()->composing()->multitoken(), "name of miner and its private key (e.g. [\"account\",\"WIF PRIVATE KEY\"] )")
132+
("mining-threads,t", bpo::value<uint32_t>(), "Number of threads to use for proof of work mining")
133+
("private-key", bpo::value<vector<string>>()->composing()->multitoken(), "WIF PRIVATE KEY to be used by one or more witnesses or miners")
134+
("miner-account-creation-fee", bpo::value<uint64_t>()->implicit_value(100000), "Account creation fee to be voted on upon successful POW - Minimum fee is 100.000 STEEM (written as 100000)")
135+
("miner-maximum-block-size", bpo::value<uint32_t>()->implicit_value(131072), "Maximum block size (in bytes) to be voted on upon successful POW - Max block size must be between 128 KB and 750 MB")
136+
("miner-sbd-interest-rate", bpo::value<uint32_t>()->implicit_value(1000), "SBD interest rate to be vote on upon successful POW - Default interest rate is 10% (written as 1000)")
137+
;
138+
146139
config_file_options.add(command_line_options);
147140
}
148141

@@ -174,6 +167,16 @@ namespace golos {
174167
}
175168
}
176169

170+
if(options.count("enable-stale-production")){
171+
pimpl->_production_enabled = options["enable-stale-production"].as<bool>();
172+
}
173+
174+
if(options.count("required-participation")){
175+
int e = static_cast<int>(options["required-participation"].as<int>());
176+
pimpl->_required_witness_participation = uint32_t(e * STEEMIT_1_PERCENT);
177+
}
178+
179+
177180
if (options.count("mining-threads")) {
178181
pimpl->mining_threads_ = std::min(options["mining-threads"].as<uint32_t>(), uint32_t(64));
179182
pimpl->mining_thread_pool_.resize(pimpl->mining_threads_);

0 commit comments

Comments
 (0)