From 5e20aff8fa762216238c219267eb9e152fd0820b Mon Sep 17 00:00:00 2001 From: phm87 <31578435+phm87@users.noreply.github.com> Date: Tue, 21 Aug 2018 03:37:38 +0200 Subject: [PATCH 01/29] Create ln.sh --- web/ln.sh | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 web/ln.sh diff --git a/web/ln.sh b/web/ln.sh new file mode 100644 index 000000000..124c5c83f --- /dev/null +++ b/web/ln.sh @@ -0,0 +1,15 @@ +#!/bin/bash + +PHP_CLI='php -d max_execution_time=60' + +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +cd ${DIR} + +date +echo started in ${DIR} + +while true; do + ${PHP_CLI} runconsole.php cronjob/runLightning + sleep 20 +done +exec bash From a73f992e739a38efb8d80fe94a5ff8db896cef43 Mon Sep 17 00:00:00 2001 From: phm87 <31578435+phm87@users.noreply.github.com> Date: Tue, 21 Aug 2018 03:47:51 +0200 Subject: [PATCH 02/29] Testnet purposes ONLY (work in progress) Be careful with this ... --- .../modules/thread/CronjobController.php | 324 ++++++++++++++++++ 1 file changed, 324 insertions(+) diff --git a/web/yaamp/modules/thread/CronjobController.php b/web/yaamp/modules/thread/CronjobController.php index 814e8329e..ec8731120 100644 --- a/web/yaamp/modules/thread/CronjobController.php +++ b/web/yaamp/modules/thread/CronjobController.php @@ -39,6 +39,330 @@ private function monitorApache() } } + public function actionRunStartLightning() + { + debuglog(__METHOD__); + set_time_limit(0); + $output = shell_exec('lightningd --bitcoin-rpcconnect=192.168.10.35 --bitcoin-rpcuser=bitcoin --bitcoin-rpcpassword=Test'); + $output = json_decode($output); + debuglog($output); + } + + public function actionRunLightning() + { + debuglog(__METHOD__); + set_time_limit(0); + +// $this->monitorApache(); + + $last_complete = memcache_get($this->memcache->memcache, "cronjob_ln_time_start"); + + // debuglog("Lightning turned on ?"); + $output = shell_exec('sudo lightning-cli -J getinfo'); + $output = json_decode($output); + + $lightning = false; + foreach ($output as $key => $out) + { + if ($key == 'id') { + debuglog("[LN] 1. Lightning is turned ON: OK"); + debuglog("[LN] My LN id = $out"); + $lightning = true; + } + } + if (!$lightning) + debuglog("[LN] Error: Please run lightningd in a screen"); + else { + + + debuglog("[LN] 2. Check LN BTC address"); + $outpute = shell_exec("sudo lightning-cli -J dev-listaddrs"); + $output = json_decode($outpute); + //debuglog("ok"); +// debuglog($outpute); + + $found = false; + //if (isset($output->addresses)) + foreach ($output->addresses as $out) { +// debuglog("in"); // value = ".$out["value"]." status = ".$out["status"]); + if (LN_MY_BTC_ADDRESS == $out->p2sh) { + $found = true; + debuglog("[LN] LN_MY_BTC_ADDRESS found and own : OK"); + break; + } + } + if ($found == false) { + debuglog("[LN] Error: Please create a p2sh-segwit address using lightning-cli newaddr then fill LN_MY_BTC_ADDRESS"); + } + else { + + debuglog("[LN] 3. Check funds"); + + $output = shell_exec("sudo lightning-cli -J listfunds"); + $listfunds = json_decode($output); +// debuglog($output); + + $ln_balance = 0; + + $found = false; + if (count($listfunds->outputs) > 0) + foreach ($listfunds->outputs as $out) { +// debuglog("in"); // value = ".$out["value"]." status = ".$out["status"]); + if ($out->status == "unconfirmed" && $out->output == 1 && count($listfunds->channels) > 0) { + debuglog("[LN] Channel creation ongoing"); + $found = true; + } + + if ($out->status == "confirmed" && $out->output == 1 && count($listfunds->channels) > 0 && $out->value > 100) { + $ln_balance = $out->value; + debuglog("[LN] seems ok, to analyse later"); + $found = true; + } + if ("confirmed" == $out->status && 0 == $out->output && count($listfunds->channels) > 0) { + $ln_balance = $out->value; + $found = true; + debuglog("[LN] Initial funding onchain : OK"); + break; + } + } + if ($found == false) { + debuglog("[LN] Error: Please perform a Bitcoin onchain transaction to ".LN_MY_BTC_ADDRESS); +// testnet faucet : https://testnet.manu.backend.hamburg/faucet + } + else { + + debuglog("LN Balance (not in channels) = ".$ln_balance." sat = ".($ln_balance/100000000)." BTC"); + + debuglog("[LN] 4. Check connections"); + + $output = shell_exec('sudo lightning-cli -J listpeers'); + $output = json_decode($output); + + global $configLNGamePlayers; + foreach ($configLNGamePlayers as $player) { +//debuglog($player[0]); + $connected = false; + foreach ($output->peers as $out) { + if ($out->id == $player[1]) { + debuglog("Connected to player OK"); + $connected = true; + break; + } + } + if ($connected == false) { + debuglog("Connect to player..."); + $ttt = 'sudo lightning-cli -J connect ' . $player[1] . ' ' . $player[2] . ' ' . $player[3]; // . "'"; + //debuglog("OK"); + debuglog($ttt); + + $output = shell_exec($ttt); + debuglog($output); + // todo: handle errors to avoid this peer (into memcache ?) + // -1, "message" : "Connection establishment: Connection timed out + } + } + + debuglog("[LN] 5. Check channels funds : cancelled !"); + +// $output = shell_exec('sudo lightning-cli -J list'); +// $output = json_decode($output); + $found = true; +/* if (count($listfunds->channels) > 0) + foreach ($listfunds->channels as $out) { +// debuglog("in"); // value = ".$out["value"]." status = ".$out["status"]); + if ($out->channel_sat > 100 && isset($out->id_peer) && $out->id_peer == LN_MAIN_NODE) { + $found = true; + debuglog("[LN] Channel credit > 100 : OK"); + //break; + } + else { + debuglog("[LN] Channel $out->peer_id with less than 100 !!! To refund ..."); + // I dont think that the function to refund a channel is already developped in LN ... + $output = shell_exec('sudo lightning-cli close '.$out->id_peer); //. ' ' . max( 16777215, round($ln_balance / LN_FRACTION))); + $output = json_decode($output); + debuglog($output); + break; + } + } +*/ + if ($found == false) { + debuglog("[LN] No channels credited: Channel credit with a fraction of the remaining funds"); + //foreach ($configLNGamePlayers as $player) { + // code : -32602 [message] => 'Funding satoshi must be <= 16777215' +// $output = shell_exec('sudo lightning-cli -J fundchannel '.$out->id_peer. ' ' . min( 16777215, round($ln_balance / LN_FRACTION))); +// $output = json_decode($output); +// debuglog($output); + //} + + } + else { + debuglog("[LN] 6. Channel open on LN : cancelled !"); +/* + $outpute = shell_exec('sudo lightning-cli -J listpeers'); + $output = json_decode($outpute); +//debuglog($outpute); + $found = true; // false; + if (isset($output->peers)) + if(count($output->peers) > 0) + foreach ($output->peers as $out) { + debuglog("Peer ".$out->alias); + if (count($out->channels) > 0) + foreach ($out->channels as $o) { + if ($o->state == "CHANNELD_AWAITING_LOCKIN") { + debuglog("[LN] Waiting for 3 to 6 confirmations on bitcoin network. CHANNELD_AWAITING_LOCKIN:Funding needs more confirmations."); + } + if ($o->state == "CHANNELD_NORMAL") { + debuglog("[LN] Channel open : OK"); + } + if ($o->state == "GOSSIPING") { + $output = shell_exec('sudo lightning-cli -J fundchannel '.$out->id_peer. ' ' . min( 16777215, round($ln_balance / LN_FRACTION))); + $output = json_decode($output); + debuglog($output); + } + if ($o->state == "ONCHAIN") + && in_array("ONCHAIN:All outputs resolved: waiting 99 more blocks before forgetting channel", $o->states)) { + $output = shell_exec('sudo lightning-cli -J fundchannel '.$out->id_peer. ' ' . min( 16777215, round($ln_balance / LN_FRACTION))); + $output = json_decode($output); + debuglog($output); + } + } + } + debuglog("channels chekcs ended"); +*/ + if (true) { + debuglog("Payment will be done"); + + if (true) { // $ln_balance > LN_MIN_PAY) { + +$db_bills = dbolist("SELECT bolt11 from invoices WHERE status='New' ORDER by id ASC"); +if (!$db_bills) { + debuglog("Nothing to pay"); + } +else { + debuglog("To pay"); + foreach ($db_bills as $bill) { +// debuglog("Bill"); +// break; +// } +// } + +//if (!empty($bill) && preg_match('/[^A-Za-z0-9]/', $bill)) { +//echo "Please type a bolt11 testnet bill. Try again"; +// die; +//} + +// debuglog("before"); +// debuglog($bill['bolt11']); + $oo = "sudo lightning-cli -J decodepay ".$bill['bolt11']; + debuglog($oo); + $outpute = shell_exec($oo); + $output = json_decode($outpute); + debuglog($outpute); + if (isset($output->message) && $output->message == "Invalid bolt11: Bad bech32 string") { + // debuglog("clean invalid"); + dborun("UPDATE invoices SET status = 'Invalid' WHERE bolt11='".$bill['bolt11']."'"); + debuglog("Invoice with invalid bolt11"); + break; + } + if (isset($output->description)) { + dborun("UPDATE invoices SET description = '".addslashes(htmlentities($output->description))."' WHERE bolt11='".$bill['bolt11']."'"); + debuglog("Update description"); + } + if (isset($output->payee)) { + dborun("UPDATE invoices SET shop = '".$output->payee."' WHERE bolt11='".$bill['bolt11']."'"); + debuglog("Update shop"); + } + if (isset($output->msatoshi)) { + dborun("UPDATE invoices SET amount = '".$output->msatoshi."' WHERE bolt11='".$bill['bolt11']."'"); + debuglog("Update amount"); + } + + $oo = "sudo lightning-cli -J pay maxfeepercent=4 maxdelay=1200 bolt11=".$bill['bolt11']; + debuglog($oo); + $outpute = shell_exec($oo); +// debuglog("ok"); + $output = json_decode($outpute); + debuglog($outpute); + if (isset($output->message) && $output->message == "Invoice expired") { + dborun("UPDATE invoices SET status = 'Expired' WHERE bolt11='".$bill['bolt11']."'"); + debuglog("Invoice expired"); + break; + } + + if (isset($output->message) && $output->message == "Invalid bolt11: Bad bech32 string") { + // debuglog("clean invalid"); + dborun("UPDATE invoices SET status = 'Invalid' WHERE bolt11='".$bill['bolt11']."'"); + debuglog("Invoice with invalid bolt11"); + break; + } + if (isset($output->message) && (strpos($output->message == "max fee requested is") !== false)) { + // Fee 2001 is 1.352941% of payment 147900; max fee requested is + // debuglog("clean invalid"); + dborun("UPDATE invoices SET status = 'maxfee' WHERE bolt11='".$bill['bolt11']."'"); + debuglog("Invoice with too much fee."); + break; + } + + if (isset($output->status) && $output->status == "complete") { + // debuglog("clean invalid"); + dborun("UPDATE invoices SET status = 'complete', exectime = '".time()."' WHERE bolt11='".$bill['bolt11']."'"); + debuglog("Bill paid ! :-)"); + if (isset($output->msatoshi_sent)) { + dborun("UPDATE invoices SET paid = '".$output->msatoshi_sent."' WHERE bolt11='".$bill['bolt11']."'"); + } + break; + } + +/* + + //debuglog($output); +// if ($output->msatoshi == LN_MIN_PAY * 1000) { // * 1000 because millisat + $outpute = shell_exec('sudo lightning-cli -J pay '.$bill); + $output = json_decode($outpute); + debuglog($outpute); + if ($output->code == 206) // "code" : 206, "message" : "Delay (locktime) is 576 blocks; max delay requested is 500." + // https://github.com/ElementsProject/lightning/issues/1586 issue still opened + { + $outpute = shell_exec('sudo lightning-cli -J pay maxdelay=577 bolt11='.$bill); + $output = json_decode($outpute); + debuglog($outpute); + } + if ($output->code == 200) // "code" : 200, "message" : "Stopped retrying during payment attempt; continue monitoring with pay or listpayments" + { + debuglog("[LN] Err 200: Stopped retrying during payment attempt; continue monitoring with pay or l"); + } + if ($output->code == 205) // "code" : 205, "message" : "Could not find a route" + { + debuglog("[LN] Err 205"); + } + } + else + debuglog("[LN] Bill: bad amount ".$output->msatoshi." ".(LN_MIN_PAY * 1000)); + +*/ +break; + } + } + + +// $bill = file_get_contents(""); + } + } + //debuglog($outpute); + } + + //debuglog($output["id"]); + //debuglog("Refund channel if new payment occured"); + + } + } + } + + memcache_set($this->memcache->memcache, "cronjob_ln_time_start", time()); +// debuglog(__METHOD__); + } + + public function actionRunBlocks() { // screenlog(__FUNCTION__); From b5585b29ddb551be9c49f234e3d3af50875f6e13 Mon Sep 17 00:00:00 2001 From: phm87 <31578435+phm87@users.noreply.github.com> Date: Tue, 21 Aug 2018 03:51:19 +0200 Subject: [PATCH 03/29] Update defaultconfig.php --- web/yaamp/defaultconfig.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/web/yaamp/defaultconfig.php b/web/yaamp/defaultconfig.php index 71e89b769..e89fe79b0 100644 --- a/web/yaamp/defaultconfig.php +++ b/web/yaamp/defaultconfig.php @@ -72,3 +72,13 @@ // cli stuff if (!defined('YIIMP_CLI_ALLOW_TXS')) define('YIIMP_CLI_ALLOW_TXS', false); +// Lightning Network +if (!defined('LN_ENABLED')) define('LN_ENABLED', false); +if (!defined('LN_ENABLED')) define('LN_MY_BTC_ADDRESS', ''); +if (!defined('LN_ENABLED')) define('LN_MY_LN_ADDRESS', ''); +if (!defined('LN_ENABLED')) define('LN_MY_IP', ''); +if (!defined('LN_ENABLED')) define('LN_MY_PORT', '9735'); +if (!defined('LN_ENABLED')) define('LN_FRACTION', 7); // Fraction of main BTC wallet to fund channels +if (!defined('LN_ENABLED')) define('LN_MIN_PAY', 3000); +if (!defined('LN_ENABLED')) define('LN_MIN_PAY', 10000); + From b55e07b131c786485dfa481bc572f5bacd49f18f Mon Sep 17 00:00:00 2001 From: phm87 <31578435+phm87@users.noreply.github.com> Date: Tue, 21 Aug 2018 03:54:40 +0200 Subject: [PATCH 04/29] Create 2018-08-lightning-testnet.sql --- sql/2018-08-lightning-testnet.sql | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 sql/2018-08-lightning-testnet.sql diff --git a/sql/2018-08-lightning-testnet.sql b/sql/2018-08-lightning-testnet.sql new file mode 100644 index 000000000..bcd29cb4a --- /dev/null +++ b/sql/2018-08-lightning-testnet.sql @@ -0,0 +1,10 @@ +CREATE TABLE `invoices` ( + `id` int(11) NOT NULL, + `bolt11` varchar(1000) NOT NULL, + `amount` int(11) NOT NULL, + `paid` int(11) NOT NULL, + `shop` text, + `description` text NOT NULL, + `status` varchar(80) DEFAULT NULL, + `exectime` int(10) UNSIGNED NOT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8; From 1aa2d9fcec4dfbb528b59981abbafbde52b87f05 Mon Sep 17 00:00:00 2001 From: phm87 <31578435+phm87@users.noreply.github.com> Date: Tue, 21 Aug 2018 03:57:22 +0200 Subject: [PATCH 05/29] Update SiteController.php --- web/yaamp/modules/site/SiteController.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/web/yaamp/modules/site/SiteController.php b/web/yaamp/modules/site/SiteController.php index 80cf6a54c..709845713 100644 --- a/web/yaamp/modules/site/SiteController.php +++ b/web/yaamp/modules/site/SiteController.php @@ -1192,4 +1192,8 @@ public function actionMainbtc() setcookie('mainbtc', '1', time()+60*60*24, '/'); } + public function actionLn() + { + $this->render('/site/ln'); + } } From 6820f0042eaadd1e43b667d604d3d2c53df52a72 Mon Sep 17 00:00:00 2001 From: phm87 <31578435+phm87@users.noreply.github.com> Date: Tue, 21 Aug 2018 04:01:33 +0200 Subject: [PATCH 06/29] Create ln.php --- web/yaamp/modules/site/ln.php | 240 ++++++++++++++++++++++++++++++++++ 1 file changed, 240 insertions(+) create mode 100644 web/yaamp/modules/site/ln.php diff --git a/web/yaamp/modules/site/ln.php b/web/yaamp/modules/site/ln.php new file mode 100644 index 000000000..da5c40eed --- /dev/null +++ b/web/yaamp/modules/site/ln.php @@ -0,0 +1,240 @@ + + Auto refresh is paused - Click to resume +--> + +
+END; + +echo << +
Lightning Network (testnet)
+
+END; + +//echo "

Lightning Network (testnet)

"; + +echo "

Lightning Network (LN) is a second-layer network based on bitcoin on-chain transactions. "; +echo "It allows to perform bitcoin transactions with less fees and high speed.

"; +//echo "More informations: lightning.network

"; +echo "

This page allows you to pay LN bills/invoices on testnet for any service on testnet.

"; + +//echo "

?

"; +?> + + +
    + +Please type a bolt11 testnet bill. Try again"; +// die; +} +else if (!empty($bill)) { +// Save bill (or pay it directly if enough funds) +$db_bill = getdbosql('db_invoices', "bolt11=:bolt11", array(':bolt11'=>$bill)); +// etdbo('db_invoices', $bill); + +if (!$db_bill) { + echo << +
    +
    Invoice payment status
    +
    +

    + Your invoice was saved in the list of invoices to pay, it will be paid soon. Please refresh this page in few seconds.
    +A scheduled task is running once per 20 seconds to pay automatically all bills in the list. +

    +
    +END; + dborun("INSERT IGNORE INTO invoices(bolt11, status) VALUES (:key,:val)", array( + ':key'=>$bill,':val'=>"New" + )); +} +else { + echo << +
    +
    Invoice payment status
    +
    +END; + echo "

    This bill is already into the list of bills.
    "; + echo "Its status is: ".$db_bill->status."
    "; + switch ($db_bill->status) { + case 'New': + echo "The scheduled task did not run yet, your invoice will be processed in a short while."; + break; + case 'maxfee': + echo "The fee associated with your invoice is too high compared to the invoice amount. As the maximum fee is 4% for normal invoices, your invoice won't be paid. Please try again with a higher amount."; + break; + case 'synok': + echo "Synthax and destinator of the bill are correct."; + break; + case 'underpay': + echo "Payment of bill is ongoing."; + break; + case 'complete': + echo "Bill of ".$db_bill->amount." millisatoshi is paid ("; + echo number_format($db_bill->amount / 1000, 0)." satoshi = "; + echo number_format($db_bill->amount / 100000000000, 8)." bitcoin).
    "; + echo "Fee was ".($db_bill->paid - $db_bill->amount)." millisatoshi ("; + echo number_format(100*($db_bill->paid - $db_bill->amount)/$db_bill->amount, 2)." %)"; + break; + default: + echo "Err"; + } +echo "

    "; +global $configLNGamePlayers; +$found = false; +if ($db_bill->shop != "") { +//echo $db_bill->shop; +//var_dump($db_bill); + foreach ($configLNGamePlayers as $name => $player) { + if ($player[0] == $db_bill->shop) { + echo "Shop: ".$name." (".$db_bill->shop.") "; + $found = true; + break; + } + } + if ($found == false) { + echo "Shop: Unknown shop (".$db_bill->shop.")."; + } + +echo "
    Amount: ".$db_bill->amount. " millisatoshi ("; + echo number_format($db_bill->amount / 1000, 3)." satoshi = "; + echo number_format($db_bill->amount / 100000000000, 11)." bitcoin).
    "; +echo "Description: ".$db_bill->description; + } +echo "

    "; + +} +echo "
    "; +echo "

    Please refresh this screen to check the status of payment of the testnet bill.

    "; + +} +else +echo << +
    +
    Pay testnet bill:
    +
    +
    + + + +
    +
    +Your testnet bill should be created on a testnet shop such as The Million Bitcoin Homepage or another testnet shop that works with the LN node of the pool. +
    +
    +
    +END; + +echo << +
    How to use ?
    +
    +
      +
    • Bitcoin, altcoins and mining pools are still in development.
    • +
    • This pool allows miners to pay any testnet bill from The Million Bitcoin Homepage +(but you can also try with other LN testnet merchants such as yaals, +Starblocks or testnet.satoshis.place) with Lightning Network on Bitcoin testnet.
    • +How to use ? +
    • 1. Go to The Million Bitcoin Homepage,
    • +
    • 2. Make a drawing, click on "Publish your pixels",
    • +
    • 3. Copy/paste the payment request (invoice bolt11 reference) into the field here below and click on "Pay my testnet bill".
    • +
    • 4. Refresh the page to make sure that the payment was executed.
    • + +
+ +
+ +
+
How does it work ?
+
+

+

+

+END; + +echo "
+
"; +?> +
+ + + + +
"; +echo << +









+ +END; + +echo <<
+ + + +END; +?> From 8a168dcffedf0d835d47c4332568e6cf27ab7ed1 Mon Sep 17 00:00:00 2001 From: phm87 <31578435+phm87@users.noreply.github.com> Date: Tue, 21 Aug 2018 04:06:46 +0200 Subject: [PATCH 07/29] Update defaultconfig.php --- web/yaamp/defaultconfig.php | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/web/yaamp/defaultconfig.php b/web/yaamp/defaultconfig.php index e89fe79b0..912baff85 100644 --- a/web/yaamp/defaultconfig.php +++ b/web/yaamp/defaultconfig.php @@ -74,11 +74,15 @@ // Lightning Network if (!defined('LN_ENABLED')) define('LN_ENABLED', false); -if (!defined('LN_ENABLED')) define('LN_MY_BTC_ADDRESS', ''); -if (!defined('LN_ENABLED')) define('LN_MY_LN_ADDRESS', ''); -if (!defined('LN_ENABLED')) define('LN_MY_IP', ''); -if (!defined('LN_ENABLED')) define('LN_MY_PORT', '9735'); -if (!defined('LN_ENABLED')) define('LN_FRACTION', 7); // Fraction of main BTC wallet to fund channels -if (!defined('LN_ENABLED')) define('LN_MIN_PAY', 3000); -if (!defined('LN_ENABLED')) define('LN_MIN_PAY', 10000); +if (!defined('LN_MY_BTC_ADDRESS')) define('LN_MY_BTC_ADDRESS', ''); +if (!defined('LN_MY_LN_ADDRESS')) define('LN_MY_LN_ADDRESS', ''); +if (!defined('LN_MY_IP')) define('LN_MY_IP', ''); +if (!defined('LN_MY_PORT')) define('LN_MY_PORT', '9735'); +if (!defined('LN_FRACTION')) define('LN_FRACTION', 7); // Fraction of main BTC wallet to fund channels +if (!defined('LN_MIN_PAY')) define('LN_MIN_PAY', 3000); +if (!defined('LN_MAIN_NODE')) define('LN_MAIN_NODE', ''); +if (!isset($configLNGamePlayers)) $configLNGamePlayers = array ( + 'testnet.millionbitcoinhomepage.net' => array('023bcc1daeb7c85208991e993a2eacf86f7d9584a6dc33291bbe5e19c986a31568', '51.15.250.152', '9735'), + 'yalls.org' => array('02212d3ec887188b284dbb7b2e6eb40629a6e14fb049673f22d2a0aa05f902090e', '54.236.55.50', '9735'), + 'testnet.satoshis.place' => array('02dd4cef0192611bc34cd1c3a0a7eb0f381e7229aa3309ae961a7fc0076b4d2bb6', '35.198.136.5', '9735')); From f0c26f165e22f5c20e341f58a202b342f7797133 Mon Sep 17 00:00:00 2001 From: phm87 <31578435+phm87@users.noreply.github.com> Date: Wed, 29 Aug 2018 03:07:24 +0200 Subject: [PATCH 08/29] Update rc.local --- rc.local | 1 + 1 file changed, 1 insertion(+) diff --git a/rc.local b/rc.local index dec333643..9dea15bd2 100644 --- a/rc.local +++ b/rc.local @@ -15,6 +15,7 @@ STRATUM_DIR=/var/stratum screen -dmS main $WEB_DIR/main.sh screen -dmS loop2 $WEB_DIR/loop2.sh screen -dmS blocks $WEB_DIR/blocks.sh +#screen -dmS ln $WEB_DIR/ln.sh screen -dmS debug tail -f $LOG_DIR/debug.log # Stratum instances (skipped/exit if no .conf) From 3f3c2a4995309780425c5b493624503a10aa3217 Mon Sep 17 00:00:00 2001 From: phm87 <31578435+phm87@users.noreply.github.com> Date: Sat, 1 Sep 2018 15:46:39 +0200 Subject: [PATCH 09/29] new column 'work' into workers table --- sql/2018-08-lightning-testnet.sql | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sql/2018-08-lightning-testnet.sql b/sql/2018-08-lightning-testnet.sql index bcd29cb4a..6e296103f 100644 --- a/sql/2018-08-lightning-testnet.sql +++ b/sql/2018-08-lightning-testnet.sql @@ -8,3 +8,5 @@ CREATE TABLE `invoices` ( `status` varchar(80) DEFAULT NULL, `exectime` int(10) UNSIGNED NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +ALTER TABLE `workers` ADD `work` DOUBLE NOT NULL DEFAULT '0.0' AFTER `algo`; From 4e6b66cec8f4d9424e806e39bb88080421c20334 Mon Sep 17 00:00:00 2001 From: phm87 <31578435+phm87@users.noreply.github.com> Date: Sat, 1 Sep 2018 15:49:00 +0200 Subject: [PATCH 10/29] YAAMP_LN_WORKERS & YAAMP_LN_FACTOR --- web/yaamp/defaultconfig.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/web/yaamp/defaultconfig.php b/web/yaamp/defaultconfig.php index 912baff85..b57c9b71c 100644 --- a/web/yaamp/defaultconfig.php +++ b/web/yaamp/defaultconfig.php @@ -81,6 +81,8 @@ if (!defined('LN_FRACTION')) define('LN_FRACTION', 7); // Fraction of main BTC wallet to fund channels if (!defined('LN_MIN_PAY')) define('LN_MIN_PAY', 3000); if (!defined('LN_MAIN_NODE')) define('LN_MAIN_NODE', ''); +if (!define('YAAMP_LN_WORKERS')) define('YAAMP_LN_WORKERS', false); +if (!define('YAAMP_LN_FACTOR')) define('YAAMP_LN_FACTOR', 0.8); if (!isset($configLNGamePlayers)) $configLNGamePlayers = array ( 'testnet.millionbitcoinhomepage.net' => array('023bcc1daeb7c85208991e993a2eacf86f7d9584a6dc33291bbe5e19c986a31568', '51.15.250.152', '9735'), From 2a271493041a4f364471914a4faa2f8e9cea06f7 Mon Sep 17 00:00:00 2001 From: phm87 <31578435+phm87@users.noreply.github.com> Date: Sat, 1 Sep 2018 15:53:17 +0200 Subject: [PATCH 11/29] accounting per worker: to verify & enhance! --- web/yaamp/core/backend/blocks.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/web/yaamp/core/backend/blocks.php b/web/yaamp/core/backend/blocks.php index 8b82548d2..ca5c0bcf5 100644 --- a/web/yaamp/core/backend/blocks.php +++ b/web/yaamp/core/backend/blocks.php @@ -61,6 +61,12 @@ function BackendBlockNew($coin, $db_block) $user->save(); } + $target = yaamp_hashrate_constant($coin->algo); + $interval = yaamp_hashrate_step(); + $delay = time()-$interval; + + dborun("UPDATE workers w SET work = w.work + (SELECT (sum(difficulty) * $target / $interval / 1000) FROM shares WHERE valid AND time>".time()-$interval." AND workerid=w.id)"); + $delay = time() - 5*60; $sqlCond = "time < $delay"; if(!YAAMP_ALLOW_EXCHANGE) // only one coin mined From c6ac57d7e7d196758b4b77d489281b270361411c Mon Sep 17 00:00:00 2001 From: phm87 <31578435+phm87@users.noreply.github.com> Date: Sat, 1 Sep 2018 16:38:00 +0200 Subject: [PATCH 12/29] LN invoices for mainet: to verify&enhance --- web/yaamp/core/backend/payment.php | 36 ++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/web/yaamp/core/backend/payment.php b/web/yaamp/core/backend/payment.php index b3704ea95..09abacc4b 100644 --- a/web/yaamp/core/backend/payment.php +++ b/web/yaamp/core/backend/payment.php @@ -32,6 +32,14 @@ function BackendUserCancelFailedPayment($userid) return 0.0; } +function is_ln_invoice($bill) +{ +if (!empty($bill) && preg_match('/[^A-Za-z0-9]/', $bill)) + return false; +else + return true; // TODO: Enhance the check of the bolt11 invoice (length, first characters, ...) +} + function BackendCoinPayments($coin) { // debuglog("BackendCoinPayments $coin->symbol"); @@ -106,6 +114,34 @@ function BackendCoinPayments($coin) { $total_to_pay += round($user->balance, 8); $addresses[$user->username] = round($user->balance, 8); + if ($coin->symbol=='BTC' && YAAMP_LN_WORKERS == true) { + $ln_works = getdbolist('db_workers', "work > 0 AND worker <> '' AND userid=".$user->id); + foreach($ln_works as $ln_work) { + if (is_ln_invoice($ln_work->worker)) { + $output = shell_exec('sudo lightning-cli -J decodepay '.$ln_work->worker); + $bill = json_decode($output); + // TODO: Enhance and add pool fee + if ($ln_work->work * YAAMP_LN_FACTOR > $bill->msatoshi / 1000 && $user->balance > $bill->msatoshi / 1000) { + $db_bill = getdbosql('db_invoices', "bolt11=:bolt11", array(':bolt11'=>$ln_work->worker)); + if (!$db_bill) { + dborun("INSERT IGNORE INTO invoices(bolt11, status) VALUES (:key,:val)", array( + ':key'=>$bill,':val'=>"New" + )); + } + if ($db_bill && $db_bill->status == "complete") { + // Invoice is already paid, no need to change total_to_pay and addresses + } + else { + // Invoice is not paid yet: some value should be kept to pay it + // TODO: Add pool fee + $total_to_pay -= round($bill->msatoshi / 1000, 8); + $addresses[$user->username] -= round($bill->msatoshi / 1000, 8); + } + } + } + } + } + // transaction xxx has too many sigops: 1035 > 1000 if ($coin->symbol == 'DCR' && count($addresses) > 990) { debuglog("payment: more than 990 {$coin->symbol} users to pay, limit to top balances..."); From a97e19d4ca391fc653d7c4535920d289c3151b86 Mon Sep 17 00:00:00 2001 From: phm87 <31578435+phm87@users.noreply.github.com> Date: Sat, 1 Sep 2018 16:41:07 +0200 Subject: [PATCH 13/29] LN webpage restricted to admin --- web/yaamp/modules/site/SiteController.php | 1 + 1 file changed, 1 insertion(+) diff --git a/web/yaamp/modules/site/SiteController.php b/web/yaamp/modules/site/SiteController.php index 709845713..c4bc2911b 100644 --- a/web/yaamp/modules/site/SiteController.php +++ b/web/yaamp/modules/site/SiteController.php @@ -1194,6 +1194,7 @@ public function actionMainbtc() public function actionLn() { + if(LN_ENABLED == false || !$this->admin) return; $this->render('/site/ln'); } } From 684ebb1d3a99d290117051a442ab412f5cba660b Mon Sep 17 00:00:00 2001 From: phm87 <31578435+phm87@users.noreply.github.com> Date: Sat, 1 Sep 2018 16:45:43 +0200 Subject: [PATCH 14/29] LN webpage only for admin --- web/yaamp/ui/main.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/web/yaamp/ui/main.php b/web/yaamp/ui/main.php index 693791d2f..770022b76 100644 --- a/web/yaamp/ui/main.php +++ b/web/yaamp/ui/main.php @@ -120,6 +120,10 @@ function showPageHeader() if (YAAMP_USE_NICEHASH_API) showItemHeader(controller()->id=='nicehash', '/nicehash', 'Nicehash'); + + if (LN_ENABLED) + showItemHeader(controller()->id=='ln', '/site/ln', 'LN'); + } echo ''; From e6a8e9d65aa3d0c7b5bdaefa092f174d588c497b Mon Sep 17 00:00:00 2001 From: phm87 <31578435+phm87@users.noreply.github.com> Date: Sun, 2 Sep 2018 00:48:22 +0200 Subject: [PATCH 15/29] invoice per userid --- sql/2018-08-lightning-testnet.sql | 1 + 1 file changed, 1 insertion(+) diff --git a/sql/2018-08-lightning-testnet.sql b/sql/2018-08-lightning-testnet.sql index 6e296103f..1fb666e19 100644 --- a/sql/2018-08-lightning-testnet.sql +++ b/sql/2018-08-lightning-testnet.sql @@ -1,5 +1,6 @@ CREATE TABLE `invoices` ( `id` int(11) NOT NULL, + `userid` int(11) NOT NULL, `bolt11` varchar(1000) NOT NULL, `amount` int(11) NOT NULL, `paid` int(11) NOT NULL, From 64edc94a821a9eea75df72ee1c9a9cac57f79ac7 Mon Sep 17 00:00:00 2001 From: phm87 <31578435+phm87@users.noreply.github.com> Date: Sun, 2 Sep 2018 00:50:09 +0200 Subject: [PATCH 16/29] Update defaultconfig.php --- web/yaamp/defaultconfig.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/web/yaamp/defaultconfig.php b/web/yaamp/defaultconfig.php index b57c9b71c..f180e471d 100644 --- a/web/yaamp/defaultconfig.php +++ b/web/yaamp/defaultconfig.php @@ -85,6 +85,10 @@ if (!define('YAAMP_LN_FACTOR')) define('YAAMP_LN_FACTOR', 0.8); if (!isset($configLNGamePlayers)) $configLNGamePlayers = array ( + /* + // testnet: 'testnet.millionbitcoinhomepage.net' => array('023bcc1daeb7c85208991e993a2eacf86f7d9584a6dc33291bbe5e19c986a31568', '51.15.250.152', '9735'), 'yalls.org' => array('02212d3ec887188b284dbb7b2e6eb40629a6e14fb049673f22d2a0aa05f902090e', '54.236.55.50', '9735'), - 'testnet.satoshis.place' => array('02dd4cef0192611bc34cd1c3a0a7eb0f381e7229aa3309ae961a7fc0076b4d2bb6', '35.198.136.5', '9735')); + 'testnet.satoshis.place' => array('02dd4cef0192611bc34cd1c3a0a7eb0f381e7229aa3309ae961a7fc0076b4d2bb6', '35.198.136.5', '9735') + */ + ); From 6afcad3fa78fdbf3716905c431a3030068616f25 Mon Sep 17 00:00:00 2001 From: phm87 <31578435+phm87@users.noreply.github.com> Date: Sun, 2 Sep 2018 12:14:35 +0200 Subject: [PATCH 17/29] Update payment.php --- web/yaamp/core/backend/payment.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web/yaamp/core/backend/payment.php b/web/yaamp/core/backend/payment.php index 09abacc4b..1e7573f13 100644 --- a/web/yaamp/core/backend/payment.php +++ b/web/yaamp/core/backend/payment.php @@ -114,7 +114,7 @@ function BackendCoinPayments($coin) { $total_to_pay += round($user->balance, 8); $addresses[$user->username] = round($user->balance, 8); - if ($coin->symbol=='BTC' && YAAMP_LN_WORKERS == true) { + if ($coin->symbol=='BTC' && LN_ENABLED == true && YAAMP_LN_WORKERS == true) { $ln_works = getdbolist('db_workers', "work > 0 AND worker <> '' AND userid=".$user->id); foreach($ln_works as $ln_work) { if (is_ln_invoice($ln_work->worker)) { From b2e7d2117901125d5641d085c822ce3123fb5ac8 Mon Sep 17 00:00:00 2001 From: phm87 <31578435+phm87@users.noreply.github.com> Date: Sun, 2 Sep 2018 12:15:36 +0200 Subject: [PATCH 18/29] Update defaultconfig.php --- web/yaamp/defaultconfig.php | 1 + 1 file changed, 1 insertion(+) diff --git a/web/yaamp/defaultconfig.php b/web/yaamp/defaultconfig.php index f180e471d..195b30e85 100644 --- a/web/yaamp/defaultconfig.php +++ b/web/yaamp/defaultconfig.php @@ -81,6 +81,7 @@ if (!defined('LN_FRACTION')) define('LN_FRACTION', 7); // Fraction of main BTC wallet to fund channels if (!defined('LN_MIN_PAY')) define('LN_MIN_PAY', 3000); if (!defined('LN_MAIN_NODE')) define('LN_MAIN_NODE', ''); +if (!define('YAAMP_LN_NET')) define('YAAMP_LN_NET', 'TESTNET'); // TESTNET or MAINET if (!define('YAAMP_LN_WORKERS')) define('YAAMP_LN_WORKERS', false); if (!define('YAAMP_LN_FACTOR')) define('YAAMP_LN_FACTOR', 0.8); From 7fecdd4e79c86e90ddb0509127d6242e5538cd2c Mon Sep 17 00:00:00 2001 From: phm87 <31578435+phm87@users.noreply.github.com> Date: Sun, 2 Sep 2018 12:17:50 +0200 Subject: [PATCH 19/29] Display /site/ln for visitors if testnet --- web/yaamp/modules/site/SiteController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web/yaamp/modules/site/SiteController.php b/web/yaamp/modules/site/SiteController.php index c4bc2911b..c06a571e2 100644 --- a/web/yaamp/modules/site/SiteController.php +++ b/web/yaamp/modules/site/SiteController.php @@ -1194,7 +1194,7 @@ public function actionMainbtc() public function actionLn() { - if(LN_ENABLED == false || !$this->admin) return; + if(LN_ENABLED == false || (YAAMP_LN_NET == 'MAINET' && !$this->admin)) return; $this->render('/site/ln'); } } From 66fd97be224ae16c9fbaf5735e8f9250cbba1c3a Mon Sep 17 00:00:00 2001 From: phm87 <31578435+phm87@users.noreply.github.com> Date: Sun, 2 Sep 2018 12:20:40 +0200 Subject: [PATCH 20/29] Update main.php --- web/yaamp/ui/main.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/web/yaamp/ui/main.php b/web/yaamp/ui/main.php index 770022b76..7cff2039e 100644 --- a/web/yaamp/ui/main.php +++ b/web/yaamp/ui/main.php @@ -121,11 +121,13 @@ function showPageHeader() if (YAAMP_USE_NICEHASH_API) showItemHeader(controller()->id=='nicehash', '/nicehash', 'Nicehash'); - if (LN_ENABLED) + if (YAAMP_LN_ENABLED) showItemHeader(controller()->id=='ln', '/site/ln', 'LN'); - } - + + if (YAAMP_LN_ENABLED && YAAMP_LN_NET == 'TESTNET' && !controller()->admin) + showItemHeader(controller()->id=='ln', '/site/ln', 'LN (testnet)'); + echo ''; $mining = getdbosql('db_mining'); From 79104c9522eeb392f82effa1de548c0067cf3a2b Mon Sep 17 00:00:00 2001 From: phm87 <31578435+phm87@users.noreply.github.com> Date: Sun, 2 Sep 2018 12:21:54 +0200 Subject: [PATCH 21/29] Update main.php --- web/yaamp/ui/main.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/web/yaamp/ui/main.php b/web/yaamp/ui/main.php index 7cff2039e..68be000cd 100644 --- a/web/yaamp/ui/main.php +++ b/web/yaamp/ui/main.php @@ -121,11 +121,11 @@ function showPageHeader() if (YAAMP_USE_NICEHASH_API) showItemHeader(controller()->id=='nicehash', '/nicehash', 'Nicehash'); - if (YAAMP_LN_ENABLED) + if (LN_ENABLED) showItemHeader(controller()->id=='ln', '/site/ln', 'LN'); } - if (YAAMP_LN_ENABLED && YAAMP_LN_NET == 'TESTNET' && !controller()->admin) + if (LN_ENABLED && YAAMP_LN_NET == 'TESTNET' && !controller()->admin) showItemHeader(controller()->id=='ln', '/site/ln', 'LN (testnet)'); echo ''; From 4ff7228ec5444417fcfcfb2167242d0e9ba11d6e Mon Sep 17 00:00:00 2001 From: phm87 <31578435+phm87@users.noreply.github.com> Date: Sun, 2 Sep 2018 12:22:53 +0200 Subject: [PATCH 22/29] Update defaultconfig.php --- web/yaamp/defaultconfig.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web/yaamp/defaultconfig.php b/web/yaamp/defaultconfig.php index 195b30e85..bd6172fe6 100644 --- a/web/yaamp/defaultconfig.php +++ b/web/yaamp/defaultconfig.php @@ -79,7 +79,7 @@ if (!defined('LN_MY_IP')) define('LN_MY_IP', ''); if (!defined('LN_MY_PORT')) define('LN_MY_PORT', '9735'); if (!defined('LN_FRACTION')) define('LN_FRACTION', 7); // Fraction of main BTC wallet to fund channels -if (!defined('LN_MIN_PAY')) define('LN_MIN_PAY', 3000); +if (!defined('LN_MIN_PAY')) define('LN_MIN_PAY', 1000); // min payout in satoshis if (!defined('LN_MAIN_NODE')) define('LN_MAIN_NODE', ''); if (!define('YAAMP_LN_NET')) define('YAAMP_LN_NET', 'TESTNET'); // TESTNET or MAINET if (!define('YAAMP_LN_WORKERS')) define('YAAMP_LN_WORKERS', false); From 43e7a3da615a622c33515c2f59948f1e610a56a8 Mon Sep 17 00:00:00 2001 From: phm87 <31578435+phm87@users.noreply.github.com> Date: Sun, 2 Sep 2018 12:24:11 +0200 Subject: [PATCH 23/29] Update CronjobController.php --- web/yaamp/modules/thread/CronjobController.php | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/web/yaamp/modules/thread/CronjobController.php b/web/yaamp/modules/thread/CronjobController.php index ec8731120..7be813779 100644 --- a/web/yaamp/modules/thread/CronjobController.php +++ b/web/yaamp/modules/thread/CronjobController.php @@ -39,15 +39,7 @@ private function monitorApache() } } - public function actionRunStartLightning() - { - debuglog(__METHOD__); - set_time_limit(0); - $output = shell_exec('lightningd --bitcoin-rpcconnect=192.168.10.35 --bitcoin-rpcuser=bitcoin --bitcoin-rpcpassword=Test'); - $output = json_decode($output); - debuglog($output); - } - + public function actionRunLightning() { debuglog(__METHOD__); From 893ea213bba91220ec20e48cf978f33243e2712d Mon Sep 17 00:00:00 2001 From: phm87 <31578435+phm87@users.noreply.github.com> Date: Sun, 2 Sep 2018 12:35:22 +0200 Subject: [PATCH 24/29] Update index.php --- web/yaamp/modules/site/index.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/web/yaamp/modules/site/index.php b/web/yaamp/modules/site/index.php index a1888b8ce..8b4141e7a 100644 --- a/web/yaamp/modules/site/index.php +++ b/web/yaamp/modules/site/index.php @@ -67,6 +67,17 @@
  • As optional password, you can use -p c=<SYMBOL> if yiimp does not set the currency correctly on the Wallet page.
  • See the "Pool Status" area on the right for PORT numbers. Algorithms without associated coins are disabled.
  • + +Lightning Network payments are enabled on this pool on . You can send Lightning Network invoices using LN page'; +if (YAAMP_LN_NET == 'TESTNET' && YAAMP_LN_WORKERS == true) echo ' or '; +if (YAAMP_LN_WORKERS == true) { + ?>the workername to send the invoice. Example: +

    + -o stratum+tcp://:<PORT> -u <WALLET_ADDRESS>.<invoice> [-p <OPTIONS>]

    +
    From 81e7d520479effc9a2b5b7e027784004d68e6078 Mon Sep 17 00:00:00 2001 From: phm87 <31578435+phm87@users.noreply.github.com> Date: Mon, 3 Sep 2018 16:13:13 +0200 Subject: [PATCH 25/29] Update index.php --- web/yaamp/modules/site/index.php | 1 + 1 file changed, 1 insertion(+) diff --git a/web/yaamp/modules/site/index.php b/web/yaamp/modules/site/index.php index 8b4141e7a..824eaffd2 100644 --- a/web/yaamp/modules/site/index.php +++ b/web/yaamp/modules/site/index.php @@ -75,6 +75,7 @@ ?>the workername to send the invoice. Example:

    -o stratum+tcp://:<PORT> -u <WALLET_ADDRESS>.<invoice> [-p <OPTIONS>]

    + Lightning Network invoices can be generated from a LN wallet or directly on a merchant's website (List of merchants (mainet), List of merchants (testnet)). From 0f3dc2fd13cab0143c5dcb82320a1eb1f1fa832a Mon Sep 17 00:00:00 2001 From: phm87 <31578435+phm87@users.noreply.github.com> Date: Mon, 3 Sep 2018 16:38:11 +0200 Subject: [PATCH 26/29] Update defaultconfig.php --- web/yaamp/defaultconfig.php | 1 + 1 file changed, 1 insertion(+) diff --git a/web/yaamp/defaultconfig.php b/web/yaamp/defaultconfig.php index bd6172fe6..7d95e97f6 100644 --- a/web/yaamp/defaultconfig.php +++ b/web/yaamp/defaultconfig.php @@ -76,6 +76,7 @@ if (!defined('LN_ENABLED')) define('LN_ENABLED', false); if (!defined('LN_MY_BTC_ADDRESS')) define('LN_MY_BTC_ADDRESS', ''); if (!defined('LN_MY_LN_ADDRESS')) define('LN_MY_LN_ADDRESS', ''); +if (!defined('LN_COIN')) define('LN_COIN', 'BTC'); if (!defined('LN_MY_IP')) define('LN_MY_IP', ''); if (!defined('LN_MY_PORT')) define('LN_MY_PORT', '9735'); if (!defined('LN_FRACTION')) define('LN_FRACTION', 7); // Fraction of main BTC wallet to fund channels From a1e1fecda5a42697bbc30afafd9e673275d94c38 Mon Sep 17 00:00:00 2001 From: phm87 <31578435+phm87@users.noreply.github.com> Date: Mon, 3 Sep 2018 16:39:08 +0200 Subject: [PATCH 27/29] Update payment.php --- web/yaamp/core/backend/payment.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web/yaamp/core/backend/payment.php b/web/yaamp/core/backend/payment.php index 1e7573f13..3f2915857 100644 --- a/web/yaamp/core/backend/payment.php +++ b/web/yaamp/core/backend/payment.php @@ -114,7 +114,7 @@ function BackendCoinPayments($coin) { $total_to_pay += round($user->balance, 8); $addresses[$user->username] = round($user->balance, 8); - if ($coin->symbol=='BTC' && LN_ENABLED == true && YAAMP_LN_WORKERS == true) { + if ($coin->symbol==LN_COIN && LN_ENABLED == true && YAAMP_LN_WORKERS == true) { $ln_works = getdbolist('db_workers', "work > 0 AND worker <> '' AND userid=".$user->id); foreach($ln_works as $ln_work) { if (is_ln_invoice($ln_work->worker)) { From 4d2ed2ba0a132da817dcca79a6ad03da28eca136 Mon Sep 17 00:00:00 2001 From: phm87 <31578435+phm87@users.noreply.github.com> Date: Mon, 3 Sep 2018 17:01:53 +0200 Subject: [PATCH 28/29] Update CronjobController.php --- .../modules/thread/CronjobController.php | 367 +++++++++--------- 1 file changed, 182 insertions(+), 185 deletions(-) diff --git a/web/yaamp/modules/thread/CronjobController.php b/web/yaamp/modules/thread/CronjobController.php index 7be813779..0f7b026ca 100644 --- a/web/yaamp/modules/thread/CronjobController.php +++ b/web/yaamp/modules/thread/CronjobController.php @@ -42,123 +42,121 @@ private function monitorApache() public function actionRunLightning() { - debuglog(__METHOD__); - set_time_limit(0); + debuglog(__METHOD__); + set_time_limit(0); // $this->monitorApache(); - $last_complete = memcache_get($this->memcache->memcache, "cronjob_ln_time_start"); - - // debuglog("Lightning turned on ?"); - $output = shell_exec('sudo lightning-cli -J getinfo'); - $output = json_decode($output); + $last_complete = memcache_get($this->memcache->memcache, "cronjob_ln_time_start"); - $lightning = false; - foreach ($output as $key => $out) - { - if ($key == 'id') { - debuglog("[LN] 1. Lightning is turned ON: OK"); - debuglog("[LN] My LN id = $out"); - $lightning = true; - } - } - if (!$lightning) - debuglog("[LN] Error: Please run lightningd in a screen"); - else { + // debuglog("Lightning turned on ?"); + $output = shell_exec('sudo lightning-cli -J getinfo'); + $output = json_decode($output); - - debuglog("[LN] 2. Check LN BTC address"); - $outpute = shell_exec("sudo lightning-cli -J dev-listaddrs"); - $output = json_decode($outpute); - //debuglog("ok"); -// debuglog($outpute); - - $found = false; - //if (isset($output->addresses)) - foreach ($output->addresses as $out) { + $lightning = false; + foreach ($output as $key => $out) + { + if ($key == 'id') { + debuglog("[LN] 1. Lightning is turned ON: OK"); + debuglog("[LN] My LN id = $out"); + $lightning = true; + } + } + if (!$lightning) + debuglog("[LN] Error: Please run lightningd in a screen"); + else { + debuglog("[LN] 2. Check LN BTC address"); + $outpute = shell_exec("sudo lightning-cli -J dev-listaddrs"); + $output = json_decode($outpute); + //debuglog("ok"); +// debuglog($outpute); + + $found = false; + //if (isset($output->addresses)) + foreach ($output->addresses as $out) { // debuglog("in"); // value = ".$out["value"]." status = ".$out["status"]); - if (LN_MY_BTC_ADDRESS == $out->p2sh) { - $found = true; - debuglog("[LN] LN_MY_BTC_ADDRESS found and own : OK"); - break; - } - } - if ($found == false) { - debuglog("[LN] Error: Please create a p2sh-segwit address using lightning-cli newaddr then fill LN_MY_BTC_ADDRESS"); - } - else { - - debuglog("[LN] 3. Check funds"); - - $output = shell_exec("sudo lightning-cli -J listfunds"); - $listfunds = json_decode($output); + if (LN_MY_BTC_ADDRESS == $out->p2sh) { + $found = true; + debuglog("[LN] LN_MY_BTC_ADDRESS found and own : OK"); + break; + } + } + if ($found == false) { + debuglog("[LN] Error: Please create a p2sh-segwit address using lightning-cli newaddr then fill LN_MY_BTC_ADDRESS"); + } + else { + debuglog("[LN] 3. Check funds"); // TODO: Enhance the logic + + $output = shell_exec("sudo lightning-cli -J listfunds"); + $listfunds = json_decode($output); // debuglog($output); - $ln_balance = 0; + $ln_balance = 0; - $found = false; - if (count($listfunds->outputs) > 0) - foreach ($listfunds->outputs as $out) { + $found = false; + if (count($listfunds->outputs) > 0) + foreach ($listfunds->outputs as $out) { // debuglog("in"); // value = ".$out["value"]." status = ".$out["status"]); - if ($out->status == "unconfirmed" && $out->output == 1 && count($listfunds->channels) > 0) { - debuglog("[LN] Channel creation ongoing"); - $found = true; - } + if ($out->status == "unconfirmed" && $out->output == 1 && count($listfunds->channels) > 0) { + debuglog("[LN] Channel creation ongoing"); + $found = true; + } - if ($out->status == "confirmed" && $out->output == 1 && count($listfunds->channels) > 0 && $out->value > 100) { - $ln_balance = $out->value; - debuglog("[LN] seems ok, to analyse later"); - $found = true; - } - if ("confirmed" == $out->status && 0 == $out->output && count($listfunds->channels) > 0) { - $ln_balance = $out->value; - $found = true; - debuglog("[LN] Initial funding onchain : OK"); - break; - } - } - if ($found == false) { - debuglog("[LN] Error: Please perform a Bitcoin onchain transaction to ".LN_MY_BTC_ADDRESS); -// testnet faucet : https://testnet.manu.backend.hamburg/faucet - } - else { - - debuglog("LN Balance (not in channels) = ".$ln_balance." sat = ".($ln_balance/100000000)." BTC"); - - debuglog("[LN] 4. Check connections"); - - $output = shell_exec('sudo lightning-cli -J listpeers'); - $output = json_decode($output); + if ($out->status == "confirmed" && $out->output == 1 && count($listfunds->channels) > 0 && $out->value > 100) { + $ln_balance = $out->value; + debuglog("[LN] seems ok (logic to enhance)"); + $found = true; + } + if ("confirmed" == $out->status && 0 == $out->output && count($listfunds->channels) > 0) { + $ln_balance = $out->value; + $found = true; + debuglog("[LN] Initial funding onchain : OK"); + break; + } + } + if ($found == false) { + debuglog("[LN] Error: Please perform a Bitcoin onchain transaction to ".LN_MY_BTC_ADDRESS); + // testnet faucet : https://testnet.manu.backend.hamburg/faucet + } + else { + + debuglog("LN Balance (not in channels) = ".$ln_balance." sat = ".($ln_balance/100000000)." BTC"); + + debuglog("[LN] 4. Check connections"); + + $output = shell_exec('sudo lightning-cli -J listpeers'); + $output = json_decode($output); - global $configLNGamePlayers; - foreach ($configLNGamePlayers as $player) { -//debuglog($player[0]); - $connected = false; - foreach ($output->peers as $out) { - if ($out->id == $player[1]) { - debuglog("Connected to player OK"); - $connected = true; - break; - } - } - if ($connected == false) { - debuglog("Connect to player..."); - $ttt = 'sudo lightning-cli -J connect ' . $player[1] . ' ' . $player[2] . ' ' . $player[3]; // . "'"; - //debuglog("OK"); - debuglog($ttt); - - $output = shell_exec($ttt); - debuglog($output); - // todo: handle errors to avoid this peer (into memcache ?) - // -1, "message" : "Connection establishment: Connection timed out - } - } - - debuglog("[LN] 5. Check channels funds : cancelled !"); + global $configLNGamePlayers; + + foreach ($configLNGamePlayers as $player) { + //debuglog($player[0]); + $connected = false; + foreach ($output->peers as $out) { + if ($out->id == $player[1]) { + debuglog("Connected to player OK"); + $connected = true; + break; + } + } + if ($connected == false) { + debuglog("Connect to player..."); + $ttt = 'sudo lightning-cli -J connect ' . $player[1] . ' ' . $player[2] . ' ' . $player[3]; // . "'"; + //debuglog("OK"); + debuglog($ttt); + + $output = shell_exec($ttt); + debuglog($output); + // todo: handle errors to avoid this peer (into memcache ?) + // -1, "message" : "Connection establishment: Connection timed out + } + } + + debuglog("[LN] 5. Check channels funds : cancelled !"); // $output = shell_exec('sudo lightning-cli -J list'); // $output = json_decode($output); - $found = true; + $found = true; /* if (count($listfunds->channels) > 0) foreach ($listfunds->channels as $out) { // debuglog("in"); // value = ".$out["value"]." status = ".$out["status"]); @@ -177,8 +175,8 @@ public function actionRunLightning() } } */ - if ($found == false) { - debuglog("[LN] No channels credited: Channel credit with a fraction of the remaining funds"); + if ($found == false) { + debuglog("[LN] No channels credited: Channel credit with a fraction of the remaining funds"); //foreach ($configLNGamePlayers as $player) { // code : -32602 [message] => 'Funding satoshi must be <= 16777215' // $output = shell_exec('sudo lightning-cli -J fundchannel '.$out->id_peer. ' ' . min( 16777215, round($ln_balance / LN_FRACTION))); @@ -186,9 +184,9 @@ public function actionRunLightning() // debuglog($output); //} - } - else { - debuglog("[LN] 6. Channel open on LN : cancelled !"); + } + else { + debuglog("[LN] 6. Channel open on LN : cancelled !"); /* $outpute = shell_exec('sudo lightning-cli -J listpeers'); $output = json_decode($outpute); @@ -221,18 +219,17 @@ public function actionRunLightning() } debuglog("channels chekcs ended"); */ - if (true) { - debuglog("Payment will be done"); - - if (true) { // $ln_balance > LN_MIN_PAY) { - -$db_bills = dbolist("SELECT bolt11 from invoices WHERE status='New' ORDER by id ASC"); -if (!$db_bills) { - debuglog("Nothing to pay"); - } -else { - debuglog("To pay"); - foreach ($db_bills as $bill) { + if (true) { + debuglog("Payment will be done"); + if (true) { // $ln_balance > LN_MIN_PAY) { + + $db_bills = dbolist("SELECT bolt11 from invoices WHERE status='New' ORDER by id ASC"); + if (!$db_bills) { + debuglog("Nothing to pay"); + } + else { + debuglog("There are LN invoices to pay"); + foreach ($db_bills as $bill) { // debuglog("Bill"); // break; // } @@ -245,65 +242,65 @@ public function actionRunLightning() // debuglog("before"); // debuglog($bill['bolt11']); - $oo = "sudo lightning-cli -J decodepay ".$bill['bolt11']; - debuglog($oo); - $outpute = shell_exec($oo); - $output = json_decode($outpute); - debuglog($outpute); - if (isset($output->message) && $output->message == "Invalid bolt11: Bad bech32 string") { - // debuglog("clean invalid"); - dborun("UPDATE invoices SET status = 'Invalid' WHERE bolt11='".$bill['bolt11']."'"); - debuglog("Invoice with invalid bolt11"); - break; - } - if (isset($output->description)) { - dborun("UPDATE invoices SET description = '".addslashes(htmlentities($output->description))."' WHERE bolt11='".$bill['bolt11']."'"); - debuglog("Update description"); - } - if (isset($output->payee)) { - dborun("UPDATE invoices SET shop = '".$output->payee."' WHERE bolt11='".$bill['bolt11']."'"); - debuglog("Update shop"); - } - if (isset($output->msatoshi)) { - dborun("UPDATE invoices SET amount = '".$output->msatoshi."' WHERE bolt11='".$bill['bolt11']."'"); - debuglog("Update amount"); - } - - $oo = "sudo lightning-cli -J pay maxfeepercent=4 maxdelay=1200 bolt11=".$bill['bolt11']; - debuglog($oo); - $outpute = shell_exec($oo); + $oo = "sudo lightning-cli -J decodepay ".$bill['bolt11']; + debuglog($oo); + $outpute = shell_exec($oo); + $output = json_decode($outpute); + debuglog($outpute); + if (isset($output->message) && $output->message == "Invalid bolt11: Bad bech32 string") { + // debuglog("clean invalid"); + dborun("UPDATE invoices SET status = 'Invalid' WHERE bolt11='".$bill['bolt11']."'"); + debuglog("Invoice with invalid bolt11"); + break; + } + if (isset($output->description)) { + dborun("UPDATE invoices SET description = '".addslashes(htmlentities($output->description))."' WHERE bolt11='".$bill['bolt11']."'"); + debuglog("Update description"); // TODO: add filter to prevent injections here + } + if (isset($output->payee)) { + dborun("UPDATE invoices SET shop = '".$output->payee."' WHERE bolt11='".$bill['bolt11']."'"); + debuglog("Update shop"); // TODO: add filter to prevent injections here + } + if (isset($output->msatoshi)) { + dborun("UPDATE invoices SET amount = '".$output->msatoshi."' WHERE bolt11='".$bill['bolt11']."'"); + debuglog("Update amount"); + } + + $oo = "sudo lightning-cli -J pay maxfeepercent=4 maxdelay=1200 bolt11=".$bill['bolt11']; + debuglog($oo); + $outpute = shell_exec($oo); // debuglog("ok"); - $output = json_decode($outpute); - debuglog($outpute); - if (isset($output->message) && $output->message == "Invoice expired") { - dborun("UPDATE invoices SET status = 'Expired' WHERE bolt11='".$bill['bolt11']."'"); - debuglog("Invoice expired"); - break; - } - - if (isset($output->message) && $output->message == "Invalid bolt11: Bad bech32 string") { - // debuglog("clean invalid"); - dborun("UPDATE invoices SET status = 'Invalid' WHERE bolt11='".$bill['bolt11']."'"); - debuglog("Invoice with invalid bolt11"); - break; - } - if (isset($output->message) && (strpos($output->message == "max fee requested is") !== false)) { - // Fee 2001 is 1.352941% of payment 147900; max fee requested is - // debuglog("clean invalid"); - dborun("UPDATE invoices SET status = 'maxfee' WHERE bolt11='".$bill['bolt11']."'"); - debuglog("Invoice with too much fee."); - break; - } - - if (isset($output->status) && $output->status == "complete") { - // debuglog("clean invalid"); - dborun("UPDATE invoices SET status = 'complete', exectime = '".time()."' WHERE bolt11='".$bill['bolt11']."'"); - debuglog("Bill paid ! :-)"); - if (isset($output->msatoshi_sent)) { - dborun("UPDATE invoices SET paid = '".$output->msatoshi_sent."' WHERE bolt11='".$bill['bolt11']."'"); - } - break; - } + $output = json_decode($outpute); + debuglog($outpute); + if (isset($output->message) && $output->message == "Invoice expired") { + dborun("UPDATE invoices SET status = 'Expired' WHERE bolt11='".$bill['bolt11']."'"); + debuglog("Invoice expired"); + break; + } + + if (isset($output->message) && $output->message == "Invalid bolt11: Bad bech32 string") { + // debuglog("clean invalid"); + dborun("UPDATE invoices SET status = 'Invalid' WHERE bolt11='".$bill['bolt11']."'"); + debuglog("Invoice with invalid bolt11"); + break; + } + if (isset($output->message) && (strpos($output->message == "max fee requested is") !== false)) { + // Fee 2001 is 1.352941% of payment 147900; max fee requested is + // debuglog("clean invalid"); + dborun("UPDATE invoices SET status = 'maxfee' WHERE bolt11='".$bill['bolt11']."'"); + debuglog("Invoice with too much fee."); + break; + } + + if (isset($output->status) && $output->status == "complete") { + // debuglog("clean invalid"); + dborun("UPDATE invoices SET status = 'complete', exectime = '".time()."' WHERE bolt11='".$bill['bolt11']."'"); + debuglog("Bill paid ! :-)"); + if (isset($output->msatoshi_sent)) { + dborun("UPDATE invoices SET paid = '".$output->msatoshi_sent."' WHERE bolt11='".$bill['bolt11']."'"); + } + break; + } /* @@ -332,22 +329,22 @@ public function actionRunLightning() debuglog("[LN] Bill: bad amount ".$output->msatoshi." ".(LN_MIN_PAY * 1000)); */ -break; - } - } + break; + } + } // $bill = file_get_contents(""); - } - } + } + } //debuglog($outpute); - } + } //debuglog($output["id"]); //debuglog("Refund channel if new payment occured"); - } - } + } + } } memcache_set($this->memcache->memcache, "cronjob_ln_time_start", time()); From 7c5c2e61727e80a95528dccc2d644f0609c64780 Mon Sep 17 00:00:00 2001 From: phm87 <31578435+phm87@users.noreply.github.com> Date: Mon, 10 Sep 2018 14:03:34 +0200 Subject: [PATCH 29/29] DB: add workerid --- sql/2018-08-lightning-testnet.sql | 1 + 1 file changed, 1 insertion(+) diff --git a/sql/2018-08-lightning-testnet.sql b/sql/2018-08-lightning-testnet.sql index 1fb666e19..6b274e432 100644 --- a/sql/2018-08-lightning-testnet.sql +++ b/sql/2018-08-lightning-testnet.sql @@ -1,6 +1,7 @@ CREATE TABLE `invoices` ( `id` int(11) NOT NULL, `userid` int(11) NOT NULL, + `workerid` int(11) NOT NULL, `bolt11` varchar(1000) NOT NULL, `amount` int(11) NOT NULL, `paid` int(11) NOT NULL,