Skip to content

Commit

Permalink
Update with new changes in Gigamonkey. There was a small possibility …
Browse files Browse the repository at this point in the history
…that we wouldn't be able to mine boost outputs that didn't use minimal pushes which has been avoided. There will also be an important bug fix having to do with how push_data is written out.
  • Loading branch information
DanielKrawisz committed Jul 9, 2023
1 parent 5854d35 commit 9cf0d0d
Show file tree
Hide file tree
Showing 13 changed files with 152 additions and 134 deletions.
4 changes: 2 additions & 2 deletions include/jobs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ namespace BoostPOW {
std::map<digest256, working> Jobs;
std::map<Bitcoin::outpoint, digest256> Scripts;

digest256 add_script (const Boost::output_script &z);
void add_prevout (const Boost::prevout &u);
digest256 add_script (const bytes &output_script);
void add_prevout (const Bitcoin::prevout &u);

uint32 remove (function<bool (const working &)>);

Expand Down
2 changes: 1 addition & 1 deletion include/miner.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ namespace BoostPOW {

int add_new_miner (ptr<redeemer>);

void new_job (const Boost::prevout &p);
void new_job (const Bitcoin::prevout &p);
void solved_job (const Bitcoin::outpoint &p);

virtual ~manager () {}
Expand Down
2 changes: 1 addition & 1 deletion include/miner_options.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ namespace BoostPOW {
int (*help) (),
int (*version) (),
int (*spend) (const script_options &),
int (*redeem) (const Bitcoin::outpoint &, const Boost::output_script &, int64, const redeeming_options &),
int (*redeem) (const Bitcoin::outpoint &, const bytes &script, int64 value, const redeeming_options &),
int (*mine) (const mining_options &));
}

Expand Down
12 changes: 7 additions & 5 deletions include/pow_co_api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ using namespace Gigamonkey;

struct inpoint : Bitcoin::outpoint {
using Bitcoin::outpoint::outpoint;

bool valid () const {
return this->Digest.valid ();
}

inpoint (const Bitcoin::txid &t, uint32 i) : outpoint {t, i} {}
};

Expand All @@ -23,10 +25,10 @@ struct pow_co : net::HTTP::client_blocking {
pow_co (net::asio::io_context &io, ptr<net::HTTP::SSL> ssl, string host = "pow.co") :
net::HTTP::client_blocking {ssl, net::HTTP::REST {"https", host}, tools::rate_limiter {3, 1}}, IO {io}, SSL {ssl} {}

list<Boost::prevout> jobs (uint32 limit = 10, double max_difficulty = -1);
list<Bitcoin::prevout> jobs (uint32 limit = 10, double max_difficulty = -1);

Boost::prevout job (const Bitcoin::txid &);
Boost::prevout job (const Bitcoin::outpoint &);
Bitcoin::prevout job (const Bitcoin::txid &);
Bitcoin::prevout job (const Bitcoin::outpoint &);

inpoint spends (const Bitcoin::outpoint &);

Expand All @@ -47,12 +49,12 @@ struct pow_co : net::HTTP::client_blocking {
static JSON encode (string type, const JSON &content);
static bool valid (const JSON &);

static std::optional<Boost::prevout> job_created (const JSON &);
static std::optional<Bitcoin::prevout> job_created (const JSON &);
static std::optional<Bitcoin::outpoint> proof_created (const JSON &);
};

struct websockets_protocol_handlers {
virtual void job_created (const Boost::prevout &) = 0;
virtual void job_created (const Bitcoin::prevout &) = 0;
virtual ~websockets_protocol_handlers () {}
};

Expand Down
19 changes: 11 additions & 8 deletions src/boost_miner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
#include <gigamonkey/script/typed_data_bip_276.hpp>
#include <gigamonkey/schema/hd.hpp>

#include <gigamonkey/ledger.hpp>
#include <gigamonkey/script/machine.hpp>
#include <gigamonkey/signature.hpp>
#include <data/io/wait_for_enter.hpp>

using namespace Gigamonkey;

int spend (const BoostPOW::script_options &options) {
Expand Down Expand Up @@ -142,7 +147,7 @@ struct redeemer final : BoostPOW::redeemer, BoostPOW::multithreaded {

int redeem (
const Bitcoin::outpoint &outpoint,
const Boost::output_script &script,
const bytes &script,
int64 value,
const BoostPOW::redeeming_options &options) {

Expand All @@ -152,9 +157,9 @@ int redeem (

Boost::candidate Job {};

Boost::output_script boost_script {};
bytes boost_script {};

if (value <= 0 || !script.valid ()) {
if (value <= 0 || !Boost::output_script {script}.valid ()) {

Job = Net.job (outpoint);

Expand All @@ -170,9 +175,9 @@ int redeem (
} else {
boost_script = script;
Job = Boost::candidate {
{Boost::prevout {
{Bitcoin::prevout {
outpoint,
Boost::output {
Bitcoin::output {
Bitcoin::satoshi {value},
script}
}}};
Expand All @@ -188,7 +193,7 @@ int redeem (
auto address = options.ReceivingAddresses->next ();

logger::log ("job.mine", JSON {
{"script", typed_data::write (typed_data::mainnet, boost_script.write ())},
{"script", typed_data::write (typed_data::mainnet, boost_script)},
{"difficulty", double (Job.difficulty ())},
{"value", value},
{"outpoint", BoostPOW::write (outpoint)},
Expand All @@ -202,9 +207,7 @@ int redeem (
r.mine ({Job.id (), Boost::puzzle {Job, key}});

r.wait_for_shutdown ();
std::cout << "shut down... deleting fees " << std::endl;
delete Fees;
std::cout << "fees deleted " << std::endl;
return 0;
}

Expand Down
109 changes: 54 additions & 55 deletions src/cosmos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,30 +36,30 @@ int command_generate (int arg_count, char** arg_values) {
return 0;
}

int command_receive(int arg_count, char** arg_values) {
if (arg_count != 1) throw data::exception{"invalid number of arguments; one expected."};
int command_receive (int arg_count, char** arg_values) {
if (arg_count != 1) throw data::exception {"invalid number of arguments; one expected."};

std::string filename{arg_values[0]};
auto w = read_wallet_from_file(filename);
auto w = read_wallet_from_file (filename);

Bitcoin::secret new_key = Bitcoin::secret(w.Master.derive(w.Index));
Bitcoin::secret new_key = Bitcoin::secret (w.Master.derive (w.Index));

w.Index += 1;

std::cout << new_key << " " << new_key.address() << std::endl;
std::cout << new_key << " " << new_key.address () << std::endl;

write_to_file (w, filename);
return 0;
}

int command_import(int arg_count, char** arg_values) {
if (arg_count != 5) throw data::exception{"invalid number of arguments; five expected."};
if (arg_count != 5) throw data::exception {"invalid number of arguments; five expected."};

std::string filename{arg_values[0]};
std::string filename {arg_values[0]};

string arg_txid{arg_values[1]};
string arg_index{arg_values[2]};
string arg_value{arg_values[3]};
string arg_txid {arg_values[1]};
string arg_index {arg_values[2]};
string arg_value {arg_values[3]};
string arg_wif {arg_values[4]};

digest256 txid {arg_txid};
Expand All @@ -70,53 +70,53 @@ int command_import(int arg_count, char** arg_values) {
int64 value;
std::stringstream {arg_value} >> value;

Bitcoin::secret key{arg_wif};
if (!key.valid()) throw data::exception {"could not read secret key"};
Bitcoin::secret key {arg_wif};
if (!key.valid ()) throw data::exception {"could not read secret key"};

auto w = read_wallet_from_file(filename);
auto w = read_wallet_from_file (filename);

w = w.insert(p2pkh_prevout{txid, index, Bitcoin::satoshi{value}, key});
w = w.insert (p2pkh_prevout {txid, index, Bitcoin::satoshi {value}, key});

write_to_file(w, filename);
write_to_file (w, filename);
return 0;
}

int command_send(int arg_count, char** arg_values) {
int command_send (int arg_count, char** arg_values) {
if (arg_count != 3) throw data::exception {"invalid number of arguments; three expected."};

std::string filename {arg_values[0]};
auto w = read_wallet_from_file(filename);
auto w = read_wallet_from_file (filename);

write_to_file (w, filename);
return 0;
}

int command_value(int arg_count, char** arg_values) {
int command_value (int arg_count, char** arg_values) {
if (arg_count != 1) throw data::exception {"invalid number of arguments; one expected."};

std::string filename{arg_values[0]};
std::cout << read_wallet_from_file(filename).value() << std::endl;
std::cout << read_wallet_from_file (filename).value () << std::endl;

return 0;
}

int command_boost(int arg_count, char** arg_values) {
int command_boost (int arg_count, char** arg_values) {

if (arg_count < 4 || arg_count > 7) data::exception {"invalid number of arguments; should at least 4 and at most 7"};

std::string filename{arg_values[0]};
auto w = read_wallet_from_file(filename);
std::string filename {arg_values[0]};
auto w = read_wallet_from_file (filename);

string arg_value{arg_values[1]};
string arg_value {arg_values[1]};

int64 satoshi_value;
std::stringstream{arg_value} >> satoshi_value;
std::stringstream {arg_value} >> satoshi_value;

Bitcoin::satoshi value = satoshi_value;

if (value > w.value()) throw data::exception {"insufficient funds"};
if (value > w.value ()) throw data::exception {"insufficient funds"};

string arg_content{arg_values[2]};
string arg_content {arg_values[2]};

digest256 content {arg_content};
if (!content.valid ()) throw data::exception {} << "could not read content: " << arg_content;
Expand All @@ -127,15 +127,15 @@ int command_boost(int arg_count, char** arg_values) {
diff_stream >> diff;

work::compact target {work::difficulty {diff}};
if (!target.valid ()) throw data::exception{} << "could not read difficulty: " << difficulty_input;
if (!target.valid ()) throw data::exception {} << "could not read difficulty: " << difficulty_input;

bytes topic{};
bytes topic {};
bytes additional_data {};

if (arg_count > 4) {

maybe<bytes> topic_read = encoding::hex::read (string {arg_values[4]});
if (!bool (topic_read) || topic_read->size () > 20) throw data::exception{} << "could not read topic: " << arg_values[4];
if (!bool (topic_read) || topic_read->size () > 20) throw data::exception {} << "could not read topic: " << arg_values[4];
topic = *topic_read;
}

Expand All @@ -151,7 +151,6 @@ int command_boost(int arg_count, char** arg_values) {
string boost_type {arg_values[6]};
if (boost_type == "contract") type = Boost::contract;
else if (boost_type != "bounty") throw data::exception {} << "could not boost type: " << arg_values[6];

}

if (type == Boost::contract) throw data::exception {"We do not do boost contract yet"};
Expand All @@ -161,7 +160,7 @@ int command_boost(int arg_count, char** arg_values) {
BoostPOW::casual_random {}.uint32 (),
additional_data, false);

Boost::output boost_output {value, boost_output_script};
Bitcoin::output boost_output {value, boost_output_script.write ()};
auto bitcoin_output = Bitcoin::output (boost_output);

auto spend = w.spend (bitcoin_output);
Expand Down Expand Up @@ -202,34 +201,34 @@ int command_boost(int arg_count, char** arg_values) {
redeem_tx.Outputs[0].Value,
spend_key});*/

write_to_file(w, filename);
write_to_file (w, filename);
return 0;
}

int command_restore(int arg_count, char** arg_values) {
int command_restore (int arg_count, char** arg_values) {
if (arg_count > 3) throw data::exception {"invalid number of arguments; two or three expected."};

std::string filename{arg_values[0]};
std::string master_human{arg_values[1]};
std::string filename {arg_values[0]};
std::string master_human {arg_values[1]};

HD::BIP_32::secret master{master_human};
if (!master.valid()) throw data::exception {"could not read HD private key"};
HD::BIP_32::secret master {master_human};
if (!master.valid ()) throw data::exception {"could not read HD private key"};

uint32 max_look_ahead = 25;
if (arg_count == 3) {
std::string arg_max_look_ahead{arg_values[2]};
std::stringstream{arg_max_look_ahead} >> max_look_ahead;
std::string arg_max_look_ahead {arg_values[2]};
std::stringstream {arg_max_look_ahead} >> max_look_ahead;
}

auto w = restore(master, max_look_ahead);
auto w = restore (master, max_look_ahead);
std::cout << "wallet is " << w << std::endl;

write_to_file(w, filename);
write_to_file (w, filename);

return 0;
}

int help() {
int help () {

std::cout << "input should be \"function\" \"args\"... where function is "
"\n\tgenerate -- create a new wallet."
Expand Down Expand Up @@ -270,27 +269,27 @@ int help() {
return 0;
}

int main(int arg_count, char** arg_values) {
int main (int arg_count, char** arg_values) {

if(arg_count == 1) return help();
if (arg_count == 1) return help ();
//if (arg_count != 5) return help();

string function{arg_values[1]};
string function {arg_values[1]};

try {

if (function == "generate") return command_generate(arg_count - 2, arg_values + 2);
if (function == "import") return command_import(arg_count - 2, arg_values + 2);
if (function == "value") return command_value(arg_count - 2, arg_values + 2);
if (function == "receive") return command_receive(arg_count - 2, arg_values + 2);
if (function == "send") return command_send(arg_count - 2, arg_values + 2);
if (function == "boost") return command_boost(arg_count - 2, arg_values + 2);
if (function == "restore") return command_restore(arg_count - 2, arg_values + 2);
if (function == "help") return help();
help();
if (function == "generate") return command_generate (arg_count - 2, arg_values + 2);
if (function == "import") return command_import (arg_count - 2, arg_values + 2);
if (function == "value") return command_value (arg_count - 2, arg_values + 2);
if (function == "receive") return command_receive (arg_count - 2, arg_values + 2);
if (function == "send") return command_send (arg_count - 2, arg_values + 2);
if (function == "boost") return command_boost (arg_count - 2, arg_values + 2);
if (function == "restore") return command_restore (arg_count - 2, arg_values + 2);
if (function == "help") return help ();
help ();

} catch (data::exception x) {
std::cout << "Error: " << x.what() << std::endl;
std::cout << "Error: " << x.what () << std::endl;
return 1;
}

Expand Down
Loading

0 comments on commit 9cf0d0d

Please sign in to comment.