Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
d6ce1e3
Merge pull request #1 from tpruvot/next
phm87 Aug 21, 2018
5e20aff
Create ln.sh
phm87 Aug 21, 2018
a73f992
Testnet purposes ONLY (work in progress)
phm87 Aug 21, 2018
b5585b2
Update defaultconfig.php
phm87 Aug 21, 2018
b55e07b
Create 2018-08-lightning-testnet.sql
phm87 Aug 21, 2018
1aa2d9f
Update SiteController.php
phm87 Aug 21, 2018
6820f00
Create ln.php
phm87 Aug 21, 2018
8a168dc
Update defaultconfig.php
phm87 Aug 21, 2018
f0c26f1
Update rc.local
phm87 Aug 29, 2018
3f3c2a4
new column 'work' into workers table
phm87 Sep 1, 2018
4e6b66c
YAAMP_LN_WORKERS & YAAMP_LN_FACTOR
phm87 Sep 1, 2018
2a27149
accounting per worker: to verify & enhance!
phm87 Sep 1, 2018
c6ac57d
LN invoices for mainet: to verify&enhance
phm87 Sep 1, 2018
a97e19d
LN webpage restricted to admin
phm87 Sep 1, 2018
684ebb1
LN webpage only for admin
phm87 Sep 1, 2018
e6a8e9d
invoice per userid
phm87 Sep 1, 2018
64edc94
Update defaultconfig.php
phm87 Sep 1, 2018
6afcad3
Update payment.php
phm87 Sep 2, 2018
b2e7d21
Update defaultconfig.php
phm87 Sep 2, 2018
7fecdd4
Display /site/ln for visitors if testnet
phm87 Sep 2, 2018
66fd97b
Update main.php
phm87 Sep 2, 2018
79104c9
Update main.php
phm87 Sep 2, 2018
4ff7228
Update defaultconfig.php
phm87 Sep 2, 2018
43e7a3d
Update CronjobController.php
phm87 Sep 2, 2018
893ea21
Update index.php
phm87 Sep 2, 2018
81e7d52
Update index.php
phm87 Sep 3, 2018
0f3dc2f
Update defaultconfig.php
phm87 Sep 3, 2018
a1e1fec
Update payment.php
phm87 Sep 3, 2018
4d2ed2b
Update CronjobController.php
phm87 Sep 3, 2018
7c5c2e6
DB: add workerid
phm87 Sep 10, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions rc.local
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 14 additions & 0 deletions sql/2018-08-lightning-testnet.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
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,
`shop` text,
`description` text NOT NULL,
`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`;
15 changes: 15 additions & 0 deletions web/ln.sh
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions web/yaamp/core/backend/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 36 additions & 0 deletions web/yaamp/core/backend/payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -106,6 +114,34 @@ function BackendCoinPayments($coin)
{
$total_to_pay += round($user->balance, 8);
$addresses[$user->username] = round($user->balance, 8);
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)) {
$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...");
Expand Down
22 changes: 22 additions & 0 deletions web/yaamp/defaultconfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,25 @@
// 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_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
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);
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')
*/
);
5 changes: 5 additions & 0 deletions web/yaamp/modules/site/SiteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -1192,4 +1192,9 @@ public function actionMainbtc()
setcookie('mainbtc', '1', time()+60*60*24, '/');
}

public function actionLn()
{
if(LN_ENABLED == false || (YAAMP_LN_NET == 'MAINET' && !$this->admin)) return;
$this->render('/site/ln');
}
}
12 changes: 12 additions & 0 deletions web/yaamp/modules/site/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,18 @@
<?php endif; ?>
<li>As optional password, you can use <b>-p c=&lt;SYMBOL&gt;</b> if yiimp does not set the currency correctly on the Wallet page.</li>
<li>See the "Pool Status" area on the right for PORT numbers. Algorithms without associated coins are disabled.</li>
<?php if (LN_ENABLED): ?>
Lightning Network payments are enabled on this pool on <b><?php echo YAAMP_LN_NET; ?></b>. You can send Lightning Network invoices using <?php
if (YAAMP_LN_NET == 'TESTNET') echo '<a href="/site/ln">LN page</a>';
if (YAAMP_LN_NET == 'TESTNET' && YAAMP_LN_WORKERS == true) echo ' or ';
if (YAAMP_LN_WORKERS == true) {
?>the workername to send the invoice. Example:
<p class="main-left-box" style='padding: 3px; font-size: .8em; background-color: #ffffee; font-family: monospace;'>
-o stratum+tcp://<?= YAAMP_STRATUM_URL ?>:&lt;PORT&gt; -u &lt;WALLET_ADDRESS&gt;.&lt;invoice&gt; [-p &lt;OPTIONS&gt;]</p>
Lightning Network invoices can be generated from a LN wallet or directly on a merchant's website (<a href="http://lightningnetworkstores.com/">List of merchants (mainet)</a>, <a href="http://lightningnetworkstores.com/testnet">List of merchants (testnet)</a>).
<?php
}
endif; ?>

<br>

Expand Down
240 changes: 240 additions & 0 deletions web/yaamp/modules/site/ln.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
<?php

JavascriptFile("/extensions/jqplot/jquery.jqplot.js");
JavascriptFile("/extensions/jqplot/plugins/jqplot.dateAxisRenderer.js");
JavascriptFile("/extensions/jqplot/plugins/jqplot.barRenderer.js");
JavascriptFile("/extensions/jqplot/plugins/jqplot.highlighter.js");
JavascriptFile("/extensions/jqplot/plugins/jqplot.cursor.js");
JavascriptFile('/yaamp/ui/js/auto_refresh.js');

echo <<<END
<!--
<div id='resume_update_button' style='color: #444; background-color: #ffd; border: 1px solid #eea;
padding: 10px; margin-left: 20px; margin-right: 20px; margin-top: 15px; cursor: pointer; display: none;'
onclick='auto_page_resume();' align=center>
<b>Auto refresh is paused - Click to resume</b></div>
-->
<table cellspacing=20 width=100%>
<tr><td valign=top width=50%>
END;

echo <<<END
<div class="main-left-box">
<div class="main-left-title">Lightning Network (testnet)</div>
<div class="main-left-inner">
END;

//echo "<h1>Lightning Network (testnet)</h1>";

echo "<p>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.</p>";
//echo "More informations: <a href=\"https://lightning.network/\">lightning.network</a></p>";
echo "<p><b>This page allows you to pay LN bills/invoices on testnet for any service on testnet.</b></p>";

//echo "<h2> ?</h2>";
?>


<ul>
<!--
<li>Bitcoin, altcoins and mining pools are still in development.</li>
<li>This pool allows miners to pay any testnet bill from <a href="https://testnet.millionbitcoinhomepage.net/">The Million Bitcoin Homepage</a>
(but you can also try with yaals, Starblocks or testnet.satoshis.place) using testnet coins.</li>
<b>How to use ?</b>
<li>1. Go to <a href="https://testnet.millionbitcoinhomepage.net/">The Million Bitcoin Homepage</a>,</li>
<li>2. Make a drawing, click on "Publish your pixels",</li>
<li>3. Copy/paste the payment request (invoice bolt11 reference) into the field here below and click on "Pay my testnet bill".</li>
<li>4. Refresh the page to make sure that the payment was executed.</li>
-->
<?php

$bill = getparam('bill');
if (!empty($bill) && preg_match('/[^A-Za-z0-9]/', $bill)) {
echo "<b>Please type a bolt11 testnet bill. <a href='/site/ln'>Try again</a></b>";
// 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 <<<END
<br />
<div class="main-left-box">
<div class="main-left-title">Invoice payment status</div>
<div class="main-left-inner">
<p>
<b>Your invoice was saved in the list of invoices to pay</b>, it will be paid soon. Please refresh this page in few seconds.<br />
A scheduled task is running once per 20 seconds to pay automatically all bills in the list.
</p>
</div></div>
END;
dborun("INSERT IGNORE INTO invoices(bolt11, status) VALUES (:key,:val)", array(
':key'=>$bill,':val'=>"New"
));
}
else {
echo <<<END
<br />
<div class="main-left-box">
<div class="main-left-title">Invoice payment status</div>
<div class="main-left-inner">
END;
echo "<p><b>This bill is already into the list of bills.</b><br />";
echo "Its status is: <b>".$db_bill->status."</b><br />";
switch ($db_bill->status) {
case 'New':
echo "<b>The scheduled task did not run yet, your invoice will be processed in a short while.</b>";
break;
case 'maxfee':
echo "<b>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.</b>";
break;
case 'synok':
echo "<b>Synthax and destinator of the bill are correct.</b>";
break;
case 'underpay':
echo "Payment of bill is <b>ongoing</b>.";
break;
case 'complete':
echo "<b style='color: green;'>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).</b><br />";
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 "</p><p>";
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 "<b>Shop:</b> <a href='http://$name'>".$name."</a> (".$db_bill->shop.") ";
$found = true;
break;
}
}
if ($found == false) {
echo "<b>Shop:</b> Unknown shop (".$db_bill->shop.").";
}

echo "<br /><b>Amount:</b> ".$db_bill->amount. " millisatoshi (";
echo number_format($db_bill->amount / 1000, 3)." satoshi = ";
echo number_format($db_bill->amount / 100000000000, 11)." bitcoin).<br />";
echo "<b>Description:</b> ".$db_bill->description;
}
echo "</p></div></div>";

}
echo "<br />";
echo "<p>Please refresh this screen to check the status of payment of the testnet bill.<br /></p>";

}
else
echo <<<END
<br />
<div class="main-left-box">
<div class="main-left-title">Pay testnet bill:</div>
<div class="main-left-inner">
<form method="GET" action="/site/ln" style="padding: 10px;">
<label for="bill">Enter your testnet bill:</label>
<input type="text" name="bill" class="main-text-input" placeholder="invoice"/>
<input type="submit" value="Pay invoice" class="main-submit-button" />
<br />
<br />
Your testnet bill should be created on a testnet shop such as <a href="https://testnet.millionbitcoinhomepage.net/">The Million Bitcoin Homepage</a> or another testnet shop that works with the LN node of the pool.
</form>
</div></div>
<br />
END;

echo <<<END
<div class="main-left-box">
<div class="main-left-title">How to use ?</div>
<div class="main-left-inner">
<ul>
<li><b>Bitcoin, altcoins and mining pools are still in development.</b></li>
<li>This pool allows miners to pay any testnet bill from <a href="https://testnet.millionbitcoinhomepage.net/">The Million Bitcoin Homepage</a>
(but you can also try with other LN testnet merchants such as <a href="https://yalls.org/">yaals</a>, <a href="https://starblocks.acinq.co/">
Starblocks</a> or <a href="https://testnet.satoshis.place/">testnet.satoshis.place</a>) with Lightning Network on Bitcoin testnet.</li>
<b>How to use ?</b>
<li>1. Go to <a href="https://testnet.millionbitcoinhomepage.net/">The Million Bitcoin Homepage</a>,</li>
<li>2. Make a drawing, click on "Publish your pixels",</li>
<li>3. Copy/paste the payment request (invoice bolt11 reference) into the field here below and click on "Pay my testnet bill".</li>
<li>4. Refresh the page to make sure that the payment was executed.</li>

</div></div>

<br />

<div class="main-left-box">
<div class="main-left-title">How does it work ?</div>
<div class="main-left-inner">
<p>
<ul>
<li>It is based on magic enhanced features of Bitcoin and Lightning Network. Integration within YiiMP will be released as opensource soon.</li>
<li>More informations: <a href="https://lightning.network/">lightning.network</a></li>
<li><a href="http://lightningnetworkstores.com/testnet">Testnet stores list</a></li>
<li><a href="https://github.com/phm87/LN-testnet-list">Testnet Ressources list</a></li>
</ul>
</p>
END;

echo "</div></div>
<br />";
?>
<br />

</ul>


<?php
echo "</td><td valign=top>";
echo <<<END
<div id='pool_current_results'>
<br><br><br><br><br><br><br><br><br><br>
</div>
END;

echo <<<END
</td></tr></table>

<script>
function page_refresh()
{
pool_current_refresh();
pool_history_refresh();
}
function select_algo(algo)
{
window.location.href = '/site/algo?algo='+algo+'&r=/';
}
////////////////////////////////////////////////////
function pool_current_ready(data)
{
$('#pool_current_results').html(data);
}
function pool_current_refresh()
{
var url = "/site/current_results";
$.get(url, '', pool_current_ready);
}
////////////////////////////////////////////////////
function pool_history_ready(data)
{
$('#pool_history_results').html(data);
}
function pool_history_refresh()
{
var url = "/site/history_results";
$.get(url, '', pool_history_ready);
}
</script>

END;
?>
Loading