bitpay / php-bitpay-client

PHP implementation for the BitPay cryptographically secure RESTful API
MIT License
166 stars 147 forks source link

PHP client causes CPU to spike to 100% #242

Closed fwermelinger closed 7 years ago

fwermelinger commented 7 years ago

I am integrating bitpay for a website as a payment provider. The site is built using CodeIgniter. Everything worked fine on my local environment (windows, apache, php7). But when I run the same code on our test or production environment (ubuntu, apache, php7) the library causes the cpu to go to 100% and it does not finish (at least not in useful time. I let it run for up to 1 minute before cancelling the job or apache did it) The VPS I'm running it on has 2 gigs of ram and 2 cores. When I apache stopped the request, the php execution was at Bitpay\Math\RichArbitraryPrecisionIntegerMath\rpmul() : line 174

The code I run is a modification of your 001_ tutorial that creates private and public keys. Here is the source code. When I run this and check top in another terminal, the php process is at 100%. Could you help me solve this problem?

<?php

// If you have not already done so, please run `composer.phar install`
error_reporting(1);
require '/var/www/mysite.com/application/vendor/autoload.php';
echo 'autoload done' . PHP_EOL;

/*
 * Start by creating a PrivateKey object
 */
$privateKey = new \Bitpay\PrivateKey('/var/temp/bitpay.pri');
$privateKey->generate();
echo 'generated privatekey' . PHP_EOL;

/**
 * Once we have a private key, a public key is created from it.
 */
$publicKey = new \Bitpay\PublicKey('/var/tmp/bitpay.pub');
echo 'created publickey object' . PHP_EOL;

// Inject the private key into the public key
$publicKey->setPrivateKey($privateKey);
echo 'set privatekey done' . PHP_EOL;

// Generate the public key
$publicKey->generate();
echo 'generated publickey' . PHP_EOL;

$storageEngine = new \Bitpay\Storage\FilesystemStorage();
$storageEngine->persist($privateKey);
$storageEngine->persist($publicKey);
echo 'keys persisted' . PHP_EOL;

And the output before it stalls:

me@machine:~# php /var/www/bitpay/001_generateKeys.php
autoload done
generated privatekey
created publickey object
set privatekey done
^C
me@machine:~#
pieterpoorthuis commented 7 years ago

Are you running the latest version of the library? (>=2.2.12 )? There has been a fix for a potential infinite loop a year ago.

Which math engine (bcmath or gmp) do you use your Ubuntu server? Can you switch engines to see if this resolves the infinite loop?

fwermelinger commented 7 years ago

That was the problem. I was running version 2.2, probably because I copied the composer instructions from the readme.md php composer.phar require bitpay/php-client:^2.2

I had to change that to bitpay/php-client: 2.2.13 and then install bcmath on the ubuntu server. After that it worked. Might be a good idea to update the readme.md right?

pieterpoorthuis commented 7 years ago

Updated readme.md to use ~ instead of ^, so all composer users get the latest version of the php lib.