Dont miss blocks

Set a cookie for the last seen block so that one is not missed in the case that two blocks are mined in a very short period of time.
This commit is contained in:
cryptochangements34 2018-02-05 15:46:28 -06:00 committed by GitHub
parent 460f4f6c75
commit 0b15e31672
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -623,17 +623,57 @@ class Monero_Gateway extends WC_Payment_Gateway
} }
return $message; return $message;
} }
public function last_block_seen($height) // sometimes 2 blocks are mined within a few seconds of eacher. Make sure we don't miss one
{
if (!isset($_COOKIE['last_seen_block']))
{
setcookie('last_seen_block', $height, time() + 2700);
return 0;
}
else{
$cookie_block = $_COOKIE['last_seen_block'];
$difference = $height - $cookie_block;
setcookie('last_seen_block', $height, time() + 2700);
return $difference;
}
}
public function verify_non_rpc($payment_id, $amount, $order_id) public function verify_non_rpc($payment_id, $amount, $order_id)
{ {
$tools = new NodeTools(); $tools = new NodeTools();
$bc_height = $tools->get_last_block_height(); $bc_height = $tools->get_last_block_height();
$block_difference = $this->last_block_seen($bc_height);
$txs_from_block = $tools->get_txs_from_block($bc_height); $txs_from_block = $tools->get_txs_from_block($bc_height);
$tx_count = count($txs_from_block) - 1; // The tx at index 0 is a coinbase tx so it can be ignored $tx_count = count($txs_from_block) - 1; // The tx at index 0 is a coinbase tx so it can be ignored
$i = 1;
$output_found; $output_found;
$block_index; $block_index;
if($difference != 0)
{
$txs_from_block_2 = $tools->get_txs_from_block($bc_height - 1);
$tx_count_2 = count($txs_from_block_2) - 1;
$i = 1;
while($i <= $tx_count_2)
{
$tx_hash = $txs_from_block_2[$i]['tx_hash'];
if(strlen($txs_from_block_2[$i]['payment_id']) != 0)
{
$result = $tools->check_tx($tx_hash, $this->address, $this->viewKey);
if($result)
{
$output_found = $result;
$block_index = $i;
$i = $tx_count_2; // finish loop
}
}
$i++;
}
}
$i = 1;
while($i <= $tx_count) while($i <= $tx_count)
{ {
$tx_hash = $txs_from_block[$i]['tx_hash']; $tx_hash = $txs_from_block[$i]['tx_hash'];
@ -649,6 +689,7 @@ class Monero_Gateway extends WC_Payment_Gateway
} }
$i++; $i++;
} }
if(isset($output_found)) if(isset($output_found))
{ {
$amount_atomic_units = $amount * 1000000000000; $amount_atomic_units = $amount * 1000000000000;