Convert monero-cpp to wownero-cpp, make it compat. with wownero-ng

This commit is contained in:
dsc
2024-03-30 09:31:15 +02:00
parent 1e74300d12
commit 2277be574f
38 changed files with 1696 additions and 2555 deletions
@@ -1,10 +1,10 @@
#include <stdio.h>
#include <iostream>
#include "wallet2.h"
#include "wallet/monero_wallet_full.h"
#include "wallet/wownero_wallet_full.h"
#include "utils/gen_utils.h"
#include "utils/monero_utils.h"
//#include "monero_wallet_model.h"
#include "utils/wownero_utils.h"
//#include "wownero_wallet_model.h"
using namespace std;
@@ -13,16 +13,16 @@ using namespace std;
// constants
const int LOG_LEVEL = 2;
const string DAEMON_URI = "http://localhost:28081";
const monero_network_type NETWORK_TYPE = monero_network_type::TESTNET;
const wownero_network_type NETWORK_TYPE = wownero_network_type::TESTNET;
// ------------------------ MULTISIG STRESS TEST ------------------------------
void synchronize_multisig_participants(vector<monero_wallet*> wallets) {
void synchronize_multisig_participants(vector<wownero_wallet*> wallets) {
// collect multisig hex of all participants to synchronize
vector<string> multisig_hexes;
for (int i = 0; i < wallets.size(); i++) {
monero_wallet* wallet = wallets[i];
wownero_wallet* wallet = wallets[i];
wallet->sync();
multisig_hexes.push_back(wallet->export_multisig_hex()); // TODO: does wallet need saved?
}
@@ -31,13 +31,13 @@ void synchronize_multisig_participants(vector<monero_wallet*> wallets) {
for (int i = 0; i < wallets.size(); i++) {
vector<string> peer_multisig_hexes;
for (int j = 0; j < wallets.size(); j++) if (j != i) peer_multisig_hexes.push_back(multisig_hexes[j]);
monero_wallet* wallet = wallets[i];
wallet->sync(); // TODO monero-project: creating multisig tx fails if wallet not explicitly synced before import_multisig_hex: https://github.com/monero-project/monero/issues/6850
wownero_wallet* wallet = wallets[i];
wallet->sync(); // TODO wownero-project: creating multisig tx fails if wallet not explicitly synced before import_multisig_hex: https://github.com/wownero-project/wownero/issues/6850
wallet->import_multisig_hex(peer_multisig_hexes);
}
}
void init_multisig_wallets(monero_wallet* funding_wallet, vector<monero_wallet*> participants, int M, int N, int account_idx, int num_addresses_to_fund) {
void init_multisig_wallets(wownero_wallet* funding_wallet, vector<wownero_wallet*> participants, int M, int N, int account_idx, int num_addresses_to_fund) {
std::cout << "init_multisig_wallets(" << M << ", " << N << ")" << std::endl;
if (participants.size() != N) throw std::runtime_error("participants != N");
@@ -45,21 +45,21 @@ void init_multisig_wallets(monero_wallet* funding_wallet, vector<monero_wallet*>
// prepare multisig hexes
vector<string> prepared_multisig_hexes;
for (int i = 0; i < N; i++) {
monero_wallet* participant = participants[i];
wownero_wallet* participant = participants[i];
prepared_multisig_hexes.push_back(participant->prepare_multisig());
}
// make wallets multisig
vector<string> made_multisig_hexes;
for (int i = 0; i < participants.size(); i++) {
monero_wallet* participant = participants[i];
wownero_wallet* participant = participants[i];
// // test bad input
// try {
// participant.make_multisig(Arrays.asList("asd", "dsa"), M, TestUtils.WALLET_PASSWORD);
// throw new RuntimeException("Should have thrown error making wallet multisig with incorrect values");
// } catch (MoneroError e) {
// assertEquals("basic_string", e.getMessage()); // TODO (monero-project): improve error message https://github.com/monero-project/monero/issues/8493
// assertEquals("basic_string", e.getMessage()); // TODO (wownero-project): improve error message https://github.com/wownero-project/wownero/issues/8493
// }
// collect prepared multisig hexes from wallet's peers
@@ -90,7 +90,7 @@ void init_multisig_wallets(monero_wallet* funding_wallet, vector<monero_wallet*>
// exchange multisig keys with each wallet and collect results
vector<string> exchange_multisig_hexes;
for (int j = 0; j < participants.size(); j++) {
monero_wallet* participant = participants[j];
wownero_wallet* participant = participants[j];
// // test bad input
// try {
@@ -105,7 +105,7 @@ void init_multisig_wallets(monero_wallet* funding_wallet, vector<monero_wallet*>
for (int k = 0; k < participants.size(); k++) if (k != j) peer_multisig_hexes.push_back(prev_multisig_hexes[k]);
// import the multisig hexes of the wallet's peers
monero_multisig_init_result result = participant->exchange_multisig_keys(peer_multisig_hexes, "");
wownero_multisig_init_result result = participant->exchange_multisig_keys(peer_multisig_hexes, "");
// test result
if (result.m_multisig_hex->empty()) throw runtime_error("m_multisig_hex is empty");
@@ -124,8 +124,8 @@ void init_multisig_wallets(monero_wallet* funding_wallet, vector<monero_wallet*>
}
// validate final multisig
monero_wallet* participant = participants[0];
monero_utils::validate_address(participant->get_primary_address(), NETWORK_TYPE);
wownero_wallet* participant = participants[0];
wownero_utils::validate_address(participant->get_primary_address(), NETWORK_TYPE);
//test_multisig_info(participant->get_multisig_info(), M, N); // TODO
string seed = participant->get_seed();
if (seed.empty()) throw std::runtime_error("seed should not be empty");
@@ -147,9 +147,9 @@ void init_multisig_wallets(monero_wallet* funding_wallet, vector<monero_wallet*>
for (int i = 0; i < account_idx; i++) participant->create_account();
// get destinations to subaddresses within the account of the multisig wallet
vector<shared_ptr<monero_destination>> destinations;
vector<shared_ptr<wownero_destination>> destinations;
for (int i = 0; i < num_addresses_to_fund; i++) {
shared_ptr<monero_destination> destination = make_shared<monero_destination>();
shared_ptr<wownero_destination> destination = make_shared<wownero_destination>();
destination->m_address = participant->get_address(account_idx, i);
destination->m_amount = 200000000000; // TODO: add a 0 for 2 XMR
destinations.push_back(destination);
@@ -162,12 +162,12 @@ void init_multisig_wallets(monero_wallet* funding_wallet, vector<monero_wallet*>
// send funds from the main test wallet to destinations in the first multisig wallet
std::cout << "Sending funds from main wallet" << std::endl;
monero_tx_config tx_config;
wownero_tx_config tx_config;
tx_config.m_account_index = 0;
tx_config.m_destinations = destinations;
tx_config.m_relay = true;
shared_ptr<monero_tx_wallet> tx = funding_wallet->create_tx(tx_config);
monero_utils::free(tx);
shared_ptr<wownero_tx_wallet> tx = funding_wallet->create_tx(tx_config);
wownero_utils::free(tx);
// attempt to start mining
std::cout << "Starting mining" << std::endl;
@@ -181,8 +181,8 @@ void init_multisig_wallets(monero_wallet* funding_wallet, vector<monero_wallet*>
gen_utils::wait_for(5000);
// fetch and test outputs
monero_output_query query;
vector<shared_ptr<monero_output_wallet>> outputs = participant->get_outputs(query);
wownero_output_query query;
vector<shared_ptr<wownero_output_wallet>> outputs = participant->get_outputs(query);
if (outputs.size() == 0) std::cout << "No output reported yet" << endl;
else {
@@ -194,13 +194,13 @@ void init_multisig_wallets(monero_wallet* funding_wallet, vector<monero_wallet*>
// outputs are not spent
// break if output is unlocked
shared_ptr<monero_tx_wallet> tx = std::static_pointer_cast<monero_tx_wallet>(outputs[0]->m_tx);
shared_ptr<wownero_tx_wallet> tx = std::static_pointer_cast<wownero_tx_wallet>(outputs[0]->m_tx);
if (!tx->m_is_locked.get()) {
monero_utils::free(outputs);
wownero_utils::free(outputs);
break;
}
}
monero_utils::free(outputs);
wownero_utils::free(outputs);
}
// stop mining
@@ -210,12 +210,12 @@ void init_multisig_wallets(monero_wallet* funding_wallet, vector<monero_wallet*>
for (int i = 0; i < num_addresses_to_fund; i++) {
if (participant->get_unlocked_balance(account_idx, i) == 0) throw std::runtime_error("balance expected to be > 0");
}
monero_output_query output_query;
wownero_output_query output_query;
output_query.m_account_index = account_idx;
vector<shared_ptr<monero_output_wallet>> outputs = participant->get_outputs(output_query);
vector<shared_ptr<wownero_output_wallet>> outputs = participant->get_outputs(output_query);
if (outputs.size() == 0) throw runtime_error("no outputs returned");
if (outputs.size() < 3) std::cout << "WARNING: not one output per subaddress?";
monero_utils::free(outputs);
wownero_utils::free(outputs);
// wallet requires importing multisig to be reliable
if (!participant->is_multisig_import_needed()) throw std::runtime_error("multisig import should be needed");
@@ -250,7 +250,7 @@ void init_multisig_wallets(monero_wallet* funding_wallet, vector<monero_wallet*>
// }
}
void test_multisig_stress(monero_wallet* funding_wallet, string wallet_name = "") {
void test_multisig_stress(wownero_wallet* funding_wallet, string wallet_name = "") {
// test config
int M = 2;
@@ -264,10 +264,10 @@ void test_multisig_stress(monero_wallet* funding_wallet, string wallet_name = ""
std::cout << "Stress testing multisig wallets: " << wallet_path << std::endl;
// open or create multisig wallets
vector<monero_wallet*> participants;
vector<wownero_wallet*> participants;
try {
for (int i = 0; i < N; i++) {
participants.push_back(monero_wallet_full::open_wallet(wallet_path + string("_") + std::to_string(i), "", NETWORK_TYPE));
participants.push_back(wownero_wallet_full::open_wallet(wallet_path + string("_") + std::to_string(i), "", NETWORK_TYPE));
}
for (int i = 0; i < participants.size(); i++) {
participants[i]->set_daemon_connection(DAEMON_URI);
@@ -276,11 +276,11 @@ void test_multisig_stress(monero_wallet* funding_wallet, string wallet_name = ""
}
} catch (const exception& e) {
for (int i = 0; i < N; i++) {
monero_wallet_config wallet_config;
wownero_wallet_config wallet_config;
wallet_config.m_path = wallet_path + string("_") + std::to_string(i);
wallet_config.m_network_type = monero_network_type::TESTNET;
wallet_config.m_server = monero_rpc_connection(DAEMON_URI);
participants.push_back(monero_wallet_full::create_wallet(wallet_config));
wallet_config.m_network_type = wownero_network_type::TESTNET;
wallet_config.m_server = wownero_rpc_connection(DAEMON_URI);
participants.push_back(wownero_wallet_full::create_wallet(wallet_config));
}
for (int i = 0; i < participants.size(); i++) {
participants[i]->sync();
@@ -288,19 +288,19 @@ void test_multisig_stress(monero_wallet* funding_wallet, string wallet_name = ""
}
init_multisig_wallets(funding_wallet, participants, M, N, account_idx, num_addresses_to_fund);
}
monero_wallet* participant = participants[0];
wownero_wallet* participant = participants[0];
// start mining
funding_wallet->start_mining(1, false, false);
// receive notifications when the wallet receives funds
struct : monero_wallet_listener {
void on_output_received(const monero_output_wallet& output) {
struct : wownero_wallet_listener {
void on_output_received(const wownero_output_wallet& output) {
cout << "Wallet received funds!" << endl;
uint64_t amount = output.m_amount.get();
string tx_hash = output.m_tx->m_hash.get();
bool is_confirmed = output.m_tx->m_is_confirmed.get();
bool is_locked = static_pointer_cast<monero_tx_wallet>(output.m_tx)->m_is_locked.get();
bool is_locked = static_pointer_cast<wownero_tx_wallet>(output.m_tx)->m_is_locked.get();
int account_index = output.m_account_index.get();
int subaddress_index = output.m_subaddress_index.get();
}
@@ -319,30 +319,30 @@ void test_multisig_stress(monero_wallet* funding_wallet, string wallet_name = ""
synchronize_multisig_participants(participants);
// create txs to cycle funds
monero_tx_config tx_config;
wownero_tx_config tx_config;
tx_config.m_account_index = account_idx;
for (int i = 0; i < num_addresses_to_fund; i++) {
shared_ptr<monero_destination> destination = make_shared<monero_destination>();
shared_ptr<wownero_destination> destination = make_shared<wownero_destination>();
destination->m_address = participant->get_address(0, i);
destination->m_amount = unlocked_balance / (num_addresses_to_fund * 3);
tx_config.m_destinations.push_back(destination);
}
vector<shared_ptr<monero_tx_wallet>> txs = participant->create_txs(tx_config);
vector<shared_ptr<wownero_tx_wallet>> txs = participant->create_txs(tx_config);
if (txs.empty()) throw runtime_error("txs is empty");
if ((*txs[0]->m_tx_set.get()->m_multisig_tx_hex).empty()) throw runtime_error("multisig_tx_hex is empty");
//assertNull(txSet.getSignedTxHex());
//assertNull(txSet.getUnsignedTxHex());
// describe multisig tx hex and test
monero_tx_set tx_set = participant->describe_tx_set(*txs[0]->m_tx_set.get());
monero_utils::free(tx_set.m_txs);
wownero_tx_set tx_set = participant->describe_tx_set(*txs[0]->m_tx_set.get());
wownero_utils::free(tx_set.m_txs);
// sign the tx with participants 1 through m - 1 to meet threshold
string multisig_tx_hex = txs[0]->m_tx_set.get()->m_multisig_tx_hex.get();
monero_utils::free(txs);
wownero_utils::free(txs);
std::cout << "Signing" << std::endl;
for (int j = 1; j < M; j++) {
monero_multisig_sign_result result = participants[j]->sign_multisig_tx_hex(multisig_tx_hex);
wownero_multisig_sign_result result = participants[j]->sign_multisig_tx_hex(multisig_tx_hex);
multisig_tx_hex = result.m_signed_multisig_tx_hex.get();
}
@@ -353,15 +353,15 @@ void test_multisig_stress(monero_wallet* funding_wallet, string wallet_name = ""
std::cout << "Tx submitted successfully!" << std::endl;
// fetch the wallet's multisig txs
monero_tx_query tx_query;
wownero_tx_query tx_query;
tx_query.m_hashes = tx_hashes;
vector<shared_ptr<monero_tx_wallet>> multisig_txs = participant->get_txs(tx_query);
vector<shared_ptr<wownero_tx_wallet>> multisig_txs = participant->get_txs(tx_query);
if (multisig_txs.size() != tx_hashes.size()) throw runtime_error("multisigTxs.size() != txHashes.size()");
vector<shared_ptr<monero_tx_wallet>> all_txs = participant->get_txs();
vector<shared_ptr<wownero_tx_wallet>> all_txs = participant->get_txs();
num_txs = all_txs.size();
std::cout << "All txs size: " << all_txs.size() << std::endl;
monero_utils::free(multisig_txs);
monero_utils::free(all_txs);
wownero_utils::free(multisig_txs);
wownero_utils::free(all_txs);
if (num_txs >= max_txs) break;
}
} catch (const exception& e) {
@@ -415,26 +415,26 @@ int main(int argc, const char* argv[]) {
int network_type = 2;
// load test wallet
monero_wallet* wallet = monero_wallet_full::open_wallet("../../test_wallets/test_wallet_1", "supersecretpassword123", monero_network_type::TESTNET);
wownero_wallet* wallet = wownero_wallet_full::open_wallet("../../test_wallets/test_wallet_1", "supersecretpassword123", wownero_network_type::TESTNET);
wallet->set_daemon_connection(DAEMON_URI);
wallet->sync();
wallet->start_syncing(5000);
// get txs
vector<shared_ptr<monero_tx_wallet>> txs = wallet->get_txs();
vector<shared_ptr<wownero_tx_wallet>> txs = wallet->get_txs();
MINFO("Wallet has " << txs.size() << " txs");
for (int i = 0; i < txs.size() && i < 10; i++) MINFO(txs[i]->serialize());
monero_utils::free(txs);
wownero_utils::free(txs);
// get transfers
vector<shared_ptr<monero_transfer>> transfers = wallet->get_transfers(monero_transfer_query());
vector<shared_ptr<wownero_transfer>> transfers = wallet->get_transfers(wownero_transfer_query());
MINFO("Wallet has " << transfers.size() << " transfers");
monero_utils::free(transfers);
wownero_utils::free(transfers);
// get outputs
vector<shared_ptr<monero_output_wallet>> outputs = wallet->get_outputs(monero_output_query());
vector<shared_ptr<wownero_output_wallet>> outputs = wallet->get_outputs(wownero_output_query());
MINFO("Wallet has " << outputs.size() << " outputs");
monero_utils::free(outputs);
wownero_utils::free(outputs);
// multisig stress test
test_multisig_stress(wallet);