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 "?"; +?> + + +
+
+END;
+ dborun("INSERT IGNORE INTO invoices(bolt11, status) VALUES (:key,:val)", array(
+ ':key'=>$bill,':val'=>"New"
+ ));
+}
+else {
+ 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.
+ ";
+
+}
+echo "Invoice payment status
+
+END;
+ echo " This bill is already into the list of bills. ";
+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 " "; +echo " Please refresh this screen to check the status of payment of the testnet bill.
+
+Pay testnet bill:
+
+
+ +END; + +echo << How to use ?
+
+
+ +
+
+How does it work ?
+
+ +
"; +?> + + + + + + ";
+echo << | + +END; + +echo << |
+ -o stratum+tcp://= YAAMP_STRATUM_URL ?>:<PORT> -u <WALLET_ADDRESS>.<invoice> [-p <OPTIONS>]
+-o stratum+tcp://= YAAMP_STRATUM_URL ?>:<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,