pull/29/head
Paul Shapiro 6 years ago
parent 45b2d42366
commit 05147ea986

@ -39,12 +39,16 @@ using namespace monero_fork_rules;
//
uint8_t monero_fork_rules::get_bulletproof_fork()
{
return 8;
return 8;
}
bool monero_fork_rules::lightwallet_hardeded__use_bulletproofs()
{
return false; // This is temporary (and true only because it's not time for the fork yet) until we have the fork rules supplied by the server
}
//
bool monero_fork_rules::lightwallet_hardcoded__use_fork_rules(uint8_t version, int64_t early_blocks)
{
bool bulletproof = false; // This is temporary (and true only because it's not time for the fork yet) until we have the fork rules supplied by the server
bool bulletproof = lightwallet_hardeded__use_bulletproofs();
if (version >= monero_fork_rules::get_bulletproof_fork()) {
return bulletproof;
}

@ -43,7 +43,7 @@ namespace monero_fork_rules
typedef std::function<bool(uint8_t/*version*/, int64_t/*early_blocks*/)> use_fork_rules_fn_type;
//
uint8_t get_bulletproof_fork();
//
bool lightwallet_hardeded__use_bulletproofs();
//
bool lightwallet_hardcoded__use_fork_rules(uint8_t version, int64_t early_blocks); // convenience
}

@ -65,7 +65,6 @@ uint32_t monero_transfer_utils::default_priority()
uint64_t monero_transfer_utils::estimated_tx_network_fee(
uint64_t fee_per_kb,
uint32_t priority,
network_type nettype,
use_fork_rules_fn_type use_fork_rules_fn
) {
bool bulletproof = use_fork_rules_fn(get_bulletproof_fork(), 0);
@ -192,6 +191,7 @@ size_t monero_transfer_utils::estimate_tx_size(bool use_rct, int n_inputs, int m
uint64_t monero_transfer_utils::calculate_fee(uint64_t fee_per_kb, size_t bytes, uint64_t fee_multiplier)
{
uint64_t kB = (bytes + 1023) / 1024;
//
return kB * fee_per_kb * fee_multiplier;
}
uint64_t monero_transfer_utils::calculate_fee(uint64_t fee_per_kb, const cryptonote::blobdata &blob, uint64_t fee_multiplier)

@ -70,7 +70,6 @@ namespace monero_transfer_utils
uint64_t estimated_tx_network_fee( // convenience function for size + calc
uint64_t fee_per_kb,
uint32_t priority, // when priority=0, falls back to monero_transfer_utils::default_priority()
network_type nettype,
use_fork_rules_fn_type use_fork_rules_fn // this is extracted to a function so that implementations can optionally query the daemon (although this presently implies that such a call remains blocking)
);
//

@ -350,13 +350,76 @@ string serial_bridge::validate_components_for_login(const string &args_string)
return ret_ss.str();
}
//
string serial_bridge::estimate_rct_size(const string &args_string)
string serial_bridge::estimate_rct_tx_size(const string &args_string)
{
boost::property_tree::ptree json_root;
if (!parsed_json_root(args_string, json_root)) {
// it will already have thrown an exception
return error_ret_json_from_message("Invalid JSON");
}
uint32_t size = monero_transfer_utils::estimate_rct_tx_size(
json_root.get<int>("n_inputs"),
json_root.get<int>("mixin"),
json_root.get<int>("n_outputs"),
json_root.get<int>("extra_size"),
json_root.get<bool>("bulletproof")
);
std::ostringstream o;
o << size;
//
boost::property_tree::ptree root;
root.put(ret_json_key__generic_retVal(), o.str());
stringstream ret_ss;
boost::property_tree::write_json(ret_ss, root);
//
return ret_ss.str();
}
string serial_bridge::calculate_fee(const string &args_string)
{
boost::property_tree::ptree json_root;
if (!parsed_json_root(args_string, json_root)) {
// it will already have thrown an exception
return error_ret_json_from_message("Invalid JSON");
}
uint64_t fee = monero_transfer_utils::calculate_fee(
stoull(json_root.get<string>("fee_per_kb")),
stoul(json_root.get<string>("num_bytes")),
stoull(json_root.get<string>("fee_multiplier"))
);
std::ostringstream o;
o << fee;
//
boost::property_tree::ptree root;
root.put(ret_json_key__generic_retVal(), o.str());
stringstream ret_ss;
boost::property_tree::write_json(ret_ss, root);
//
return ret_ss.str();
}
string serial_bridge::estimated_tx_network_fee(const string &args_string)
{
boost::property_tree::ptree json_root;
if (!parsed_json_root(args_string, json_root)) {
// it will already have thrown an exception
return error_ret_json_from_message("Invalid JSON");
}
uint64_t fee = monero_transfer_utils::estimated_tx_network_fee(
stoull(json_root.get<string>("fee_per_kb")),
stoul(json_root.get<string>("priority")),
[] (uint8_t version, int64_t early_blocks) -> bool
{
return lightwallet_hardcoded__use_fork_rules(version, early_blocks);
}
);
std::ostringstream o;
o << fee;
//
boost::property_tree::ptree root;
root.put(ret_json_key__generic_retVal(), o.str());
stringstream ret_ss;
boost::property_tree::write_json(ret_ss, root);
//
return ret_ss.str();
}
//
string serial_bridge::generate_key_image(const string &args_string)

@ -57,9 +57,9 @@ namespace serial_bridge
string seed_and_keys_from_mnemonic(const string &args_string);
string validate_components_for_login(const string &args_string);
//
string estimate_rct_size(const string &args_string);
string estimate_rct_tx_size(const string &args_string);
string calculate_fee(const string &args_string);
// TODO: possibly add EstimatedTransaction_networkFee analog
string estimated_tx_network_fee(const string &args_string);
//
string generate_key_image(const string &args_string);
//

@ -104,7 +104,7 @@ BOOST_AUTO_TEST_CASE(transfers__fee)
};
uint64_t fee_per_kb = 9000000;
uint32_t priority = 2;
uint64_t est_fee = monero_transfer_utils::estimated_tx_network_fee(fee_per_kb, priority, cryptonote::MAINNET, use_fork_rules_fn);
uint64_t est_fee = monero_transfer_utils::estimated_tx_network_fee(fee_per_kb, priority, use_fork_rules_fn);
std::cout << "est_fee with fee_per_kb " << fee_per_kb << ": " << est_fee << std::endl;
BOOST_REQUIRE(est_fee > 0);
}
@ -770,14 +770,90 @@ BOOST_AUTO_TEST_CASE(bridged__validate_components_for_login)
cout << "bridged: validate_components_for_login: pub_spendKey_string: " << *pub_spendKey_string << endl;
}
BOOST_AUTO_TEST_CASE(bridged__estimate_rct_size)
BOOST_AUTO_TEST_CASE(bridged__estimated_tx_network_fee)
{
using namespace serial_bridge;
//
boost::property_tree::ptree root;
root.put("fee_per_kb", "9000000");
root.put("priority", "2");
//
stringstream args_ss;
boost::property_tree::write_json(args_ss, root);
auto ret_string = serial_bridge::estimated_tx_network_fee(args_ss.str());
stringstream ret_stream;
ret_stream << ret_string;
boost::property_tree::ptree ret_tree;
boost::property_tree::read_json(ret_stream, ret_tree);
optional<string> err_string = ret_tree.get_optional<string>(ret_json_key__any__err_msg());
if (err_string != none) {
BOOST_REQUIRE_MESSAGE(false, *err_string);
}
optional<string> fee_string = ret_tree.get_optional<string>(ret_json_key__generic_retVal());
BOOST_REQUIRE(fee_string != none);
BOOST_REQUIRE((*fee_string).size() > 0);
uint64_t fee = stoull(*fee_string);
BOOST_REQUIRE(fee == 504000000);
cout << "bridged__estimated_tx_network_fee: fee: " << fee << endl;
}
BOOST_AUTO_TEST_CASE(bridged__estimate_rct_size)
{
using namespace serial_bridge;
//
boost::property_tree::ptree root;
root.put("n_inputs", 2);
root.put("mixin", monero_transfer_utils::fixed_mixinsize());
root.put("n_outputs", 2);
std::vector<uint8_t> extra; // blank extra
root.put("extra_size", extra.size());
root.put("bulletproof", monero_fork_rules::lightwallet_hardeded__use_bulletproofs());
//
stringstream args_ss;
boost::property_tree::write_json(args_ss, root);
auto ret_string = serial_bridge::estimate_rct_tx_size(args_ss.str());
stringstream ret_stream;
ret_stream << ret_string;
boost::property_tree::ptree ret_tree;
boost::property_tree::read_json(ret_stream, ret_tree);
optional<string> err_string = ret_tree.get_optional<string>(ret_json_key__any__err_msg());
if (err_string != none) {
BOOST_REQUIRE_MESSAGE(false, *err_string);
}
optional<string> size_string = ret_tree.get_optional<string>(ret_json_key__generic_retVal());
BOOST_REQUIRE(size_string != none);
BOOST_REQUIRE((*size_string).size() > 0);
uint64_t size = stoull(*size_string);
BOOST_REQUIRE(size == 13762);
cout << "bridged__estimate_rct_size: size: " << size << endl;
}
//
BOOST_AUTO_TEST_CASE(bridged__calculate_fee)
{
using namespace serial_bridge;
//
boost::property_tree::ptree root;
root.put("fee_per_kb", "9000000");
root.put("num_bytes", "13762");
root.put("fee_multiplier", "4"); // aka priority idx 1 / number 2
//
stringstream args_ss;
boost::property_tree::write_json(args_ss, root);
auto ret_string = serial_bridge::calculate_fee(args_ss.str());
stringstream ret_stream;
ret_stream << ret_string;
boost::property_tree::ptree ret_tree;
boost::property_tree::read_json(ret_stream, ret_tree);
optional<string> err_string = ret_tree.get_optional<string>(ret_json_key__any__err_msg());
if (err_string != none) {
BOOST_REQUIRE_MESSAGE(false, *err_string);
}
optional<string> fee_string = ret_tree.get_optional<string>(ret_json_key__generic_retVal());
BOOST_REQUIRE(fee_string != none);
BOOST_REQUIRE((*fee_string).size() > 0);
uint64_t fee = stoull(*fee_string);
BOOST_REQUIRE(fee == 504000000);
cout << "bridged__calculate_fee: fee: " << fee << endl;
}
BOOST_AUTO_TEST_CASE(bridged__generate_key_image)

Loading…
Cancel
Save