From 1cbd860a0ee33528620a941e5c3f762e9fadeacd Mon Sep 17 00:00:00 2001 From: sc0vu Date: Wed, 16 Aug 2023 18:13:14 +0800 Subject: [PATCH 01/16] Add async request manager --- composer.json | 7 +- src/Eth.php | 2 +- src/Net.php | 2 +- src/Personal.php | 2 +- src/Providers/HttpProvider.php | 7 +- .../HttpAsyncRequestManager.php | 120 ++++++++++++++++++ src/Shh.php | 2 +- src/Web3.php | 2 +- test/unit/HttpProviderTest.php | 72 +++++++++++ 9 files changed, 207 insertions(+), 9 deletions(-) create mode 100644 src/RequestManagers/HttpAsyncRequestManager.php diff --git a/composer.json b/composer.json index e03dcef0..c4bb57fc 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,12 @@ "PHP": "^7.2|^8.0", "kornrunner/keccak": "~1.0", "phpseclib/phpseclib": "~2.0.30", - "ext-mbstring": "*" + "ext-mbstring": "*", + "react/http": "^1.6.0", + "react/async": "^4.0.0", + "react/promise": "^2.9.0", + "react/event-loop": "^1.2", + "react/socket": "^1.13" }, "require-dev": { "phpunit/phpunit": "~8.0|~9.0" diff --git a/src/Eth.php b/src/Eth.php index 78d97989..a20737da 100644 --- a/src/Eth.php +++ b/src/Eth.php @@ -102,7 +102,7 @@ public function __call($name, $arguments) if ($methodObject->validate($arguments)) { $inputs = $methodObject->transform($arguments, $methodObject->inputFormatters); $methodObject->arguments = $inputs; - $this->provider->send($methodObject, $callback); + return $this->provider->send($methodObject, $callback); } } } diff --git a/src/Net.php b/src/Net.php index bb06615d..92985f69 100644 --- a/src/Net.php +++ b/src/Net.php @@ -102,7 +102,7 @@ public function __call($name, $arguments) if ($methodObject->validate($arguments)) { $inputs = $methodObject->transform($arguments, $methodObject->inputFormatters); $methodObject->arguments = $inputs; - $this->provider->send($methodObject, $callback); + return $this->provider->send($methodObject, $callback); } } } diff --git a/src/Personal.php b/src/Personal.php index 75aa200c..5df0cca7 100644 --- a/src/Personal.php +++ b/src/Personal.php @@ -102,7 +102,7 @@ public function __call($name, $arguments) if ($methodObject->validate($arguments)) { $inputs = $methodObject->transform($arguments, $methodObject->inputFormatters); $methodObject->arguments = $inputs; - $this->provider->send($methodObject, $callback); + return $this->provider->send($methodObject, $callback); } } } diff --git a/src/Providers/HttpProvider.php b/src/Providers/HttpProvider.php index b99de556..05e4f366 100644 --- a/src/Providers/HttpProvider.php +++ b/src/Providers/HttpProvider.php @@ -59,7 +59,7 @@ public function send($method, $callback) return call_user_func($callback, null, $res); }; - $this->requestManager->sendPayload($payload, $proxy); + return $this->requestManager->sendPayload($payload, $proxy); } else { $this->methods[] = $method; $this->batch[] = $payload; @@ -108,8 +108,9 @@ public function execute($callback) } return call_user_func($callback, null, $res); }; - $this->requestManager->sendPayload('[' . implode(',', $this->batch) . ']', $proxy); - $this->methods[] = []; + $r = $this->requestManager->sendPayload('[' . implode(',', $this->batch) . ']', $proxy); + $this->methods = []; $this->batch = []; + return $r; } } \ No newline at end of file diff --git a/src/RequestManagers/HttpAsyncRequestManager.php b/src/RequestManagers/HttpAsyncRequestManager.php new file mode 100644 index 00000000..e95cb912 --- /dev/null +++ b/src/RequestManagers/HttpAsyncRequestManager.php @@ -0,0 +1,120 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\RequestManagers; + +use InvalidArgumentException; +use Psr\Http\Message\StreamInterface; +use RuntimeException as RPCException; +use Psr\Http\Message\ResponseInterface; +use React; +use React\Async; +use React\EventLoop\Loop; +use React\Http\Browser; +use React\Socket\Connector; +use Web3\RequestManagers\RequestManager; +use Web3\RequestManagers\IRequestManager; + +class HttpAsyncRequestManager extends RequestManager implements IRequestManager +{ + /** + * client + * + * @var \React\Http\Browser + */ + protected $client; + + /** + * construct + * + * @param string $host + * @param int $timeout + * @return void + */ + public function __construct($host, $timeout = 1) + { + parent::__construct($host, $timeout); + $connector = new Connector([ + 'timeout' => $timeout, + ], Loop::get()); + $this->client = (new Browser($connector, Loop::get()))->withRejectErrorResponse(false); + } + + /** + * sendPayload + * + * @param string $payload + * @param callable $callback + * @return void + */ + public function sendPayload($payload, $callback) + { + if (!is_string($payload)) { + throw new \InvalidArgumentException('Payload must be string.'); + } + + $host = $this->host; + + return Async\coroutine(Async\async(function () use ($host, $payload, $callback) { + try { + $headers = [ + 'content-type' => 'application/json' + ]; + $res = Async\await($this->client->request('POST', $host, $headers, $payload)); + /** + * @var StreamInterface $stream ; + */ + $stream = $res->getBody(); + $json = json_decode($stream); + $stream->close(); + + if (JSON_ERROR_NONE !== json_last_error()) { + call_user_func($callback, new InvalidArgumentException('json_decode error: ' . json_last_error_msg()), null); + } + if (is_array($json)) { + // batch results + $results = []; + $errors = []; + + foreach ($json as $result) { + if (property_exists($result,'result')) { + $results[] = $result->result; + } else { + if (isset($json->error)) { + $error = $json->error; + $errors[] = new RPCException(mb_ereg_replace('Error: ', '', $error->message), $error->code); + } else { + $results[] = null; + } + } + } + if (count($errors) > 0) { + call_user_func($callback, $errors, $results); + } else { + call_user_func($callback, null, $results); + } + } elseif (property_exists($json,'result')) { + call_user_func($callback, null, $json->result); + } else { + if (isset($json->error)) { + $error = $json->error; + + call_user_func($callback, new RPCException(mb_ereg_replace('Error: ', '', $error->message), $error->code), null); + } else { + call_user_func($callback, new RPCException('Something wrong happened.'), null); + } + } + } catch (Exception $err) { + call_user_func($callback, $err, null); + } + })); + } +} diff --git a/src/Shh.php b/src/Shh.php index bc438e4b..7a303fb9 100644 --- a/src/Shh.php +++ b/src/Shh.php @@ -103,7 +103,7 @@ public function __call($name, $arguments) if ($methodObject->validate($arguments)) { $inputs = $methodObject->transform($arguments, $methodObject->inputFormatters); $methodObject->arguments = $inputs; - $this->provider->send($methodObject, $callback); + return $this->provider->send($methodObject, $callback); } } } diff --git a/src/Web3.php b/src/Web3.php index 35a2cded..4e489f77 100644 --- a/src/Web3.php +++ b/src/Web3.php @@ -142,7 +142,7 @@ public function __call($name, $arguments) if ($methodObject->validate($arguments)) { $inputs = $methodObject->transform($arguments, $methodObject->inputFormatters); $methodObject->arguments = $inputs; - $this->provider->send($methodObject, $callback); + return $this->provider->send($methodObject, $callback); } } } diff --git a/test/unit/HttpProviderTest.php b/test/unit/HttpProviderTest.php index f1ef4bfb..57bf0605 100644 --- a/test/unit/HttpProviderTest.php +++ b/test/unit/HttpProviderTest.php @@ -5,6 +5,7 @@ use RuntimeException; use Test\TestCase; use Web3\RequestManagers\HttpRequestManager; +use Web3\RequestManagers\HttpAsyncRequestManager; use Web3\Providers\HttpProvider; use Web3\Methods\Web3\ClientVersion; @@ -57,4 +58,75 @@ public function testBatch() $provider->send($method, null); $provider->execute($callback); } + + /** + * testSendAsync + * + * @return void + */ + public function testSendAsync() + { + $requestManager = new HttpAsyncRequestManager($this->testHost); + $provider = new HttpProvider($requestManager); + $method = new ClientVersion('web3_clientVersion', []); + + // \React\Async\await($provider->send($method, function ($err, $version) { + // if ($err !== null) { + // $this->fail($err->getMessage()); + // } + // $this->assertTrue(is_string($version)); + // })); + $a = $provider->send($method, function ($err, $version) { + if ($err !== null) { + $this->fail($err->getMessage()); + } + $this->assertTrue(is_string($version)); + }); + $b = $provider->send($method, function ($err, $version) { + if ($err !== null) { + $this->fail($err->getMessage()); + } + $this->assertTrue(is_string($version)); + }); + $c = $provider->send($method, function ($err, $version) { + if ($err !== null) { + $this->fail($err->getMessage()); + } + $this->assertTrue(is_string($version)); + }); + \React\Async\await(\React\Async\parallel([ + function () use ($a) { return $a; }, + function () use ($b) { return $b; }, + function () use ($c) { return $c; } + ])); + } + + /** + * testBatchAsync + * + * @return void + */ + public function testBatchAsync() + { + $requestManager = new HttpAsyncRequestManager($this->testHost); + $provider = new HttpProvider($requestManager); + $method = new ClientVersion('web3_clientVersion', []); + $callback = function ($err, $data) { + if ($err !== null) { + $this->fail($err->getMessage()); + } + $this->assertEquals($data[0], $data[1]); + }; + + try { + \React\Async\await($provider->execute($callback)); + } catch (RuntimeException $err) { + $this->assertTrue($err->getMessage() !== true); + } + + $provider->batch(true); + $provider->send($method, null); + $provider->send($method, null); + \React\Async\await($provider->execute($callback)); + } } \ No newline at end of file From c3cfa6f8e90a4e3babb3a9c7a45cdc3eab071560 Mon Sep 17 00:00:00 2001 From: sc0Vu Date: Wed, 16 Aug 2023 19:21:43 +0800 Subject: [PATCH 02/16] Add php 8.1 in github ci --- .github/workflows/php.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index 7701ad57..9de276fc 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -7,7 +7,7 @@ jobs: name: Build and test web3.php with ${{ matrix.php-version }} strategy: matrix: - php-version: ["7.3", "7.4", "8.0"] + php-version: ["7.3", "7.4", "8.0", "8.1"] runs-on: ubuntu-latest From a874c45a24ca48547dabe12250e8d315540209ae Mon Sep 17 00:00:00 2001 From: sc0vu Date: Thu, 17 Aug 2023 11:10:48 +0800 Subject: [PATCH 03/16] Support async for php > 7.2 --- composer.json | 2 +- src/RequestManagers/HttpAsyncRequestManager.php | 11 ++++++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/composer.json b/composer.json index c4bb57fc..b58bfda9 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,7 @@ "phpseclib/phpseclib": "~2.0.30", "ext-mbstring": "*", "react/http": "^1.6.0", - "react/async": "^4.0.0", + "react/async": "^4.0.0|^3.1.0", "react/promise": "^2.9.0", "react/event-loop": "^1.2", "react/socket": "^1.13" diff --git a/src/RequestManagers/HttpAsyncRequestManager.php b/src/RequestManagers/HttpAsyncRequestManager.php index e95cb912..0c655999 100644 --- a/src/RequestManagers/HttpAsyncRequestManager.php +++ b/src/RequestManagers/HttpAsyncRequestManager.php @@ -62,8 +62,7 @@ public function sendPayload($payload, $callback) } $host = $this->host; - - return Async\coroutine(Async\async(function () use ($host, $payload, $callback) { + $request = function () use ($host, $payload, $callback) { try { $headers = [ 'content-type' => 'application/json' @@ -115,6 +114,12 @@ public function sendPayload($payload, $callback) } catch (Exception $err) { call_user_func($callback, $err, null); } - })); + }; + + if (function_exists('React\\Async\\async')) { + $request = Async\async($request); + } + + return Async\coroutine($request); } } From 4046203e103bd3269fe7b73ab7a882df7792f4e4 Mon Sep 17 00:00:00 2001 From: sc0vu Date: Thu, 17 Aug 2023 11:15:04 +0800 Subject: [PATCH 04/16] Update docker files --- docker/ganache/Dockerfile | 2 +- docker/php/{Dockerfile-71 => Dockerfile-81} | 4 ++-- docker/php/Dockerfile-82 | 23 +++++++++++++++++++++ 3 files changed, 26 insertions(+), 3 deletions(-) rename docker/php/{Dockerfile-71 => Dockerfile-81} (93%) create mode 100644 docker/php/Dockerfile-82 diff --git a/docker/ganache/Dockerfile b/docker/ganache/Dockerfile index 17726d6d..5f99bb9f 100644 --- a/docker/ganache/Dockerfile +++ b/docker/ganache/Dockerfile @@ -1,4 +1,4 @@ -FROM node:9.11.1-alpine +FROM node:12.11.1-alpine MAINTAINER Peter Lai diff --git a/docker/php/Dockerfile-71 b/docker/php/Dockerfile-81 similarity index 93% rename from docker/php/Dockerfile-71 rename to docker/php/Dockerfile-81 index 0df7a269..ded44c5b 100644 --- a/docker/php/Dockerfile-71 +++ b/docker/php/Dockerfile-81 @@ -1,4 +1,4 @@ -FROM php:7.1-alpine +FROM php:8.1-alpine MAINTAINER Peter Lai @@ -20,4 +20,4 @@ RUN php composer-setup.php && \ php composer-setup.php --install-dir=/usr/bin --filename=composer && \ php -r "unlink('composer-setup.php');" -WORKDIR /app \ No newline at end of file +WORKDIR /app diff --git a/docker/php/Dockerfile-82 b/docker/php/Dockerfile-82 new file mode 100644 index 00000000..4483817a --- /dev/null +++ b/docker/php/Dockerfile-82 @@ -0,0 +1,23 @@ +FROM php:8.2-alpine + +MAINTAINER Peter Lai + +COPY composer-setup.php composer-setup.php +# COPY php.ini-production $PHP_INI_DIR/php.ini + +RUN apk update && \ + apk add git + +# Install gmp +Run apk add gmp-dev && \ + docker-php-ext-install gmp + +# Install nodejs +# Run apk add --update nodejs nodejs-npm + +# Install composer +RUN php composer-setup.php && \ + php composer-setup.php --install-dir=/usr/bin --filename=composer && \ + php -r "unlink('composer-setup.php');" + +WORKDIR /app From 60045cfb8cb836be6e74147f6e03a1e832217459 Mon Sep 17 00:00:00 2001 From: sc0vu Date: Thu, 17 Aug 2023 11:16:39 +0800 Subject: [PATCH 05/16] Test php 8.2 in ci --- .github/workflows/php.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index 9de276fc..276a9a3d 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -7,7 +7,7 @@ jobs: name: Build and test web3.php with ${{ matrix.php-version }} strategy: matrix: - php-version: ["7.3", "7.4", "8.0", "8.1"] + php-version: ["7.3", "7.4", "8.0", "8.1", "8.2"] runs-on: ubuntu-latest From 6c9a0d40ca6c4a87f5ed13d069806670e723d2d8 Mon Sep 17 00:00:00 2001 From: sc0Vu Date: Sun, 27 Aug 2023 13:22:24 +0800 Subject: [PATCH 06/16] Rename testRinkebyHost to testHost2 --- test/TestCase.php | 4 ++-- test/unit/ProviderTest.php | 3 ++- test/unit/RequestManagerTest.php | 3 ++- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/test/TestCase.php b/test/TestCase.php index 373a8b31..13d59417 100644 --- a/test/TestCase.php +++ b/test/TestCase.php @@ -15,11 +15,11 @@ class TestCase extends BaseTestCase protected $web3; /** - * testRinkebyHost + * testHost2 * * @var string */ - protected $testRinkebyHost = 'https://rinkeby.infura.io/vuethexplore'; + protected $testHost2 = 'https://eth-mainnet.g.alchemy.com/v2/notavalidkey'; /** * testHost diff --git a/test/unit/ProviderTest.php b/test/unit/ProviderTest.php index 53ee6a0a..32af5176 100644 --- a/test/unit/ProviderTest.php +++ b/test/unit/ProviderTest.php @@ -20,9 +20,10 @@ public function testSetRequestManager() $this->assertEquals($provider->requestManager->host, 'http://localhost:8545'); - $requestManager = new RequestManager($this->testRinkebyHost); + $requestManager = new RequestManager($this->testHost2); $provider->requestManager = $requestManager; + // there is no setter for request manager $this->assertEquals($provider->requestManager->host, 'http://localhost:8545'); } } \ No newline at end of file diff --git a/test/unit/RequestManagerTest.php b/test/unit/RequestManagerTest.php index 3498ef02..61dabdb2 100644 --- a/test/unit/RequestManagerTest.php +++ b/test/unit/RequestManagerTest.php @@ -18,7 +18,8 @@ public function testSetHost() $this->assertEquals($requestManager->host, 'http://localhost:8545'); $this->assertEquals($requestManager->timeout, 0.1); - $requestManager->host = $this->testRinkebyHost; + // there is no setter for host and timeout + $requestManager->host = $this->testHost2; $requestManager->timeout = 1; $this->assertEquals($requestManager->host, 'http://localhost:8545'); $this->assertEquals($requestManager->timeout, 0.1); From f46f7812e777fd0c93eaba286ac346a4ed2add32 Mon Sep 17 00:00:00 2001 From: sc0Vu Date: Sun, 27 Aug 2023 13:44:16 +0800 Subject: [PATCH 07/16] Add testGetBlockByNumberAsync --- test/TestCase.php | 13 +++++++++++++ test/unit/EthApiTest.php | 22 ++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/test/TestCase.php b/test/TestCase.php index 13d59417..18d298e6 100644 --- a/test/TestCase.php +++ b/test/TestCase.php @@ -4,6 +4,8 @@ use \PHPUnit\Framework\TestCase as BaseTestCase; use Web3\Web3; +use Web3\RequestManagers\HttpAsyncRequestManager; +use Web3\Providers\HttpProvider; class TestCase extends BaseTestCase { @@ -35,6 +37,13 @@ class TestCase extends BaseTestCase */ protected $coinbase; + /** + * asyncHttpProvider + * + * @var \Web3\Providers\HttpProvider + */ + protected $asyncHttpProvider; + /** * setUp */ @@ -43,6 +52,10 @@ public function setUp(): void $web3 = new Web3($this->testHost); $this->web3 = $web3; + $asyncRequestManager = new HttpAsyncRequestManager($this->testHost); + $asyncHttpProvider = new HttpProvider($asyncRequestManager); + $this->asyncHttpProvider = $asyncHttpProvider; + $web3->eth->coinbase(function ($err, $coinbase) { if ($err !== null) { return $this->fail($err->getMessage()); diff --git a/test/unit/EthApiTest.php b/test/unit/EthApiTest.php index 2f58bc45..56b9ee0d 100644 --- a/test/unit/EthApiTest.php +++ b/test/unit/EthApiTest.php @@ -790,6 +790,28 @@ public function testSubmitHashrate() }); } + /** + * testGetBlockByNumberAsync + * + * @return void + */ + public function testGetBlockByNumberAsync() + { + $eth = $this->eth; + $eth->provider = $this->asyncHttpProvider; + + // should return reactphp promise + $promise = $eth->getBlockByNumber('latest', false, function ($err, $block) { + if ($err !== null) { + return $this->assertTrue($err !== null); + } + // weird behavior, see https://github.com/web3p/web3.php/issues/16 + $this->assertTrue($block !== null); + }); + $this->assertTrue($promise instanceof \React\Promise\PromiseInterface); + \React\Async\await($promise); + } + /** * testUnallowedMethod * From 746b4f192b65428382a845fbe254bb82bab3b144 Mon Sep 17 00:00:00 2001 From: sc0Vu Date: Sun, 27 Aug 2023 13:48:42 +0800 Subject: [PATCH 08/16] Add testPeerCountAsync --- test/unit/NetApiTest.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/test/unit/NetApiTest.php b/test/unit/NetApiTest.php index 49142352..0c96d571 100644 --- a/test/unit/NetApiTest.php +++ b/test/unit/NetApiTest.php @@ -79,6 +79,27 @@ public function testListening() }); } + /** + * testPeerCountAsync + * + * @return void + */ + public function testPeerCountAsync() + { + $net = $this->net; + $net->provider = $this->asyncHttpProvider; + + // should return reactphp promise + $promise = $net->peerCount(function ($err, $count) { + if ($err !== null) { + return $this->fail($err->getMessage()); + } + $this->assertTrue($count instanceof BigNumber); + }); + $this->assertTrue($promise instanceof \React\Promise\PromiseInterface); + \React\Async\await($promise); + } + /** * testUnallowedMethod * From 901e247298ab16ff5c68547f6380b0f55d923390 Mon Sep 17 00:00:00 2001 From: sc0Vu Date: Sun, 27 Aug 2023 14:05:13 +0800 Subject: [PATCH 09/16] Add testVersionAsync --- test/unit/ShhApiTest.php | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/test/unit/ShhApiTest.php b/test/unit/ShhApiTest.php index e0fcd0e1..795e8a53 100644 --- a/test/unit/ShhApiTest.php +++ b/test/unit/ShhApiTest.php @@ -465,6 +465,28 @@ public function testVersion() // $this->assertTrue(true); // }); // } + + /** + * testVersionAsync + * + * @return void + */ + public function testVersionAsync() + { + $shh = $this->shh; + $shh->provider = $this->asyncHttpProvider; + + // should return reactphp promise + $promise = $shh->version(function ($err, $version) { + if ($err !== null) { + return $this->fail($err->getMessage()); + } + $this->assertTrue(is_string($version)); + }); + $this->assertTrue($promise instanceof \React\Promise\PromiseInterface); + \React\Async\await($promise); + } + /** * testUnallowedMethod * From af62a082acb9ef76aa13d073075f7abbec48d654 Mon Sep 17 00:00:00 2001 From: sc0Vu Date: Sun, 27 Aug 2023 14:08:45 +0800 Subject: [PATCH 10/16] Update message when test failed --- test/unit/ContractTest.php | 2 +- test/unit/EthApiTest.php | 66 ++++++++++++++++----------------- test/unit/EthBatchTest.php | 2 +- test/unit/NetBatchTest.php | 2 +- test/unit/PersonalBatchTest.php | 2 +- test/unit/ShhBatchTest.php | 4 +- test/unit/Web3BatchTest.php | 4 +- 7 files changed, 41 insertions(+), 41 deletions(-) diff --git a/test/unit/ContractTest.php b/test/unit/ContractTest.php index 5061abc5..11734f4e 100644 --- a/test/unit/ContractTest.php +++ b/test/unit/ContractTest.php @@ -708,7 +708,7 @@ public function testEstimateGas() ], function ($err, $result) use ($contract) { if ($err !== null) { // infura api gg - return $this->assertTrue($err !== null); + return $this->fail($err->getMessage()); } if (isset($result)) { echo "\nEstimate gas: " . $result->toString() . "\n"; diff --git a/test/unit/EthApiTest.php b/test/unit/EthApiTest.php index 56b9ee0d..cc57b538 100644 --- a/test/unit/EthApiTest.php +++ b/test/unit/EthApiTest.php @@ -227,7 +227,7 @@ public function testGetBlockTransactionCountByHash() $eth->getBlockTransactionCountByHash('0xb903239f8543d04b5dc1ba6579132b143087c68db1b2168786408fcbce568238', function ($err, $transactionCount) { if ($err !== null) { - return $this->assertTrue($err !== null); + return $this->fail($err->getMessage()); } $this->assertTrue(is_numeric($transactionCount->toString())); }); @@ -244,7 +244,7 @@ public function testGetBlockTransactionCountByNumber() $eth->getBlockTransactionCountByNumber('0x0', function ($err, $transactionCount) { if ($err !== null) { - return $this->assertTrue($err !== null); + return $this->fail($err->getMessage()); } $this->assertTrue(is_numeric($transactionCount->toString())); }); @@ -261,7 +261,7 @@ public function testGetUncleCountByBlockHash() $eth->getUncleCountByBlockHash('0xb903239f8543d04b5dc1ba6579132b143087c68db1b2168786408fcbce568238', function ($err, $uncleCount) { if ($err !== null) { - return $this->assertTrue($err !== null); + return $this->fail($err->getMessage()); } $this->assertTrue(is_numeric($uncleCount->toString())); }); @@ -278,7 +278,7 @@ public function testGetUncleCountByBlockNumber() $eth->getUncleCountByBlockNumber('0x0', function ($err, $uncleCount) { if ($err !== null) { - return $this->assertTrue($err !== null); + return $this->fail($err->getMessage()); } $this->assertTrue(is_numeric($uncleCount->toString())); }); @@ -295,7 +295,7 @@ public function testGetCode() $eth->getCode('0x407d73d8a49eeb85d32cf465507dd71d507100c1', function ($err, $code) { if ($err !== null) { - return $this->assertTrue($err !== null); + return $this->fail($err->getMessage()); } $this->assertTrue(is_string($code)); }); @@ -313,7 +313,7 @@ public function testSign() $eth->sign('0x407d73d8a49eeb85d32cf465507dd71d507100c1', '0xdeadbeaf', function ($err, $sign) { if ($err !== null) { // infura banned us to sign message - return $this->assertTrue($err !== null); + return $this->fail($err->getMessage()); } $this->assertTrue(is_string($sign)); }); @@ -338,7 +338,7 @@ public function testSendTransaction() ], function ($err, $transaction) { if ($err !== null) { // infura banned us to send transaction - return $this->assertTrue($err !== null); + return $this->fail($err->getMessage()); } $this->assertTrue(is_string($transaction)); }); @@ -355,7 +355,7 @@ public function testSendRawTransaction() $eth->sendRawTransaction('0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675', function ($err, $transaction) { if ($err !== null) { - return $this->assertTrue($err !== null); + return $this->fail($err->getMessage()); } $this->assertTrue(is_string($transaction)); }); @@ -379,7 +379,7 @@ public function testCall() 'data' => "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675" ], function ($err, $transaction) { if ($err !== null) { - return $this->assertTrue($err !== null); + return $this->fail($err->getMessage()); } $this->assertTrue(is_string($transaction)); }); @@ -403,7 +403,7 @@ public function testEstimateGas() 'data' => "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675" ], function ($err, $gas) { if ($err !== null) { - return $this->assertTrue($err !== null); + return $this->fail($err->getMessage()); } $this->assertTrue(is_numeric($gas->toString())); }); @@ -420,7 +420,7 @@ public function testGetBlockByHash() $eth->getBlockByHash('0xb903239f8543d04b5dc1ba6579132b143087c68db1b2168786408fcbce568238', false, function ($err, $block) { if ($err !== null) { - return $this->assertTrue($err !== null); + return $this->fail($err->getMessage()); } $this->assertTrue($block !== null); }); @@ -437,7 +437,7 @@ public function testGetBlockByNumber() $eth->getBlockByNumber('latest', false, function ($err, $block) { if ($err !== null) { - return $this->assertTrue($err !== null); + return $this->fail($err->getMessage()); } // weird behavior, see https://github.com/web3p/web3.php/issues/16 $this->assertTrue($block !== null); @@ -455,7 +455,7 @@ public function testGetTransactionByHash() $eth->getTransactionByHash('0xb903239f8543d04b5dc1ba6579132b143087c68db1b2168786408fcbce568238', function ($err, $transaction) { if ($err !== null) { - return $this->assertTrue($err !== null); + return $this->fail($err->getMessage()); } $this->assertTrue($transaction == null); }); @@ -472,7 +472,7 @@ public function testGetTransactionByBlockHashAndIndex() $eth->getTransactionByBlockHashAndIndex('0xb903239f8543d04b5dc1ba6579132b143087c68db1b2168786408fcbce568238', '0x0', function ($err, $transaction) { if ($err !== null) { - return $this->assertTrue($err !== null); + return $this->fail($err->getMessage()); } $this->assertTrue($transaction == null); }); @@ -489,7 +489,7 @@ public function testGetTransactionByBlockNumberAndIndex() $eth->getTransactionByBlockNumberAndIndex('0xe8', '0x0', function ($err, $transaction) { if ($err !== null) { - return $this->assertTrue($err !== null); + return $this->fail($err->getMessage()); } $this->assertTrue($transaction !== null); }); @@ -506,7 +506,7 @@ public function testGetTransactionReceipt() $eth->getTransactionReceipt('0xb903239f8543d04b5dc1ba6579132b143087c68db1b2168786408fcbce568238', function ($err, $transaction) { if ($err !== null) { - return $this->assertTrue($err !== null); + return $this->fail($err->getMessage()); } $this->assertTrue($transaction == null); }); @@ -523,7 +523,7 @@ public function testGetUncleByBlockHashAndIndex() $eth->getUncleByBlockHashAndIndex('0xb903239f8543d04b5dc1ba6579132b143087c68db1b2168786408fcbce568238', '0x0', function ($err, $uncle) { if ($err !== null) { - return $this->assertTrue($err !== null); + return $this->fail($err->getMessage()); } $this->assertTrue($uncle !== null); }); @@ -540,7 +540,7 @@ public function testGetUncleByBlockNumberAndIndex() $eth->getUncleByBlockNumberAndIndex('0xe8', '0x0', function ($err, $uncle) { if ($err !== null) { - return $this->assertTrue($err !== null); + return $this->fail($err->getMessage()); } $this->assertTrue($uncle !== null); }); @@ -557,7 +557,7 @@ public function testCompileSolidity() $eth->compileSolidity('contract test { function multiply(uint a) returns(uint d) { return a * 7; } }', function ($err, $compiled) { if ($err !== null) { - return $this->assertTrue($err !== null); + return $this->fail($err->getMessage()); } $this->assertTrue(is_string($compiled)); }); @@ -574,7 +574,7 @@ public function testCompileLLL() $eth->compileLLL('(returnlll (suicide (caller)))', function ($err, $compiled) { if ($err !== null) { - return $this->assertTrue($err !== null); + return $this->fail($err->getMessage()); } $this->assertTrue(is_string($compiled)); }); @@ -591,7 +591,7 @@ public function testCompileSerpent() $eth->compileSerpent('\/* some serpent *\/', function ($err, $compiled) { if ($err !== null) { - return $this->assertTrue($err !== null); + return $this->fail($err->getMessage()); } $this->assertTrue(is_string($compiled)); }); @@ -614,7 +614,7 @@ public function testNewFilter() ], function ($err, $filter) { if ($err !== null) { // infura banned us to new filter - return $this->assertTrue($err !== null); + return $this->fail($err->getMessage()); } $this->assertTrue(is_string($filter)); }); @@ -632,7 +632,7 @@ public function testNewBlockFilter() $eth->newBlockFilter(function ($err, $filter) { if ($err !== null) { // infura banned us to new block filter - return $this->assertTrue($err !== null); + return $this->fail($err->getMessage()); } $this->assertTrue(is_string($filter)); }); @@ -650,7 +650,7 @@ public function testNewPendingTransactionFilter() $eth->newPendingTransactionFilter(function ($err, $filter) { if ($err !== null) { // infura banned us to new pending transaction filter - return $this->assertTrue($err !== null); + return $this->fail($err->getMessage()); } $this->assertTrue(is_string($filter)); }); @@ -668,7 +668,7 @@ public function testUninstallFilter() $eth->uninstallFilter('0x01', function ($err, $filter) { if ($err !== null) { // infura banned us to uninstall filter - return $this->assertTrue($err !== null); + return $this->fail($err->getMessage()); } $this->assertTrue(is_bool($filter)); }); @@ -686,7 +686,7 @@ public function testGetFilterChanges() $eth->getFilterChanges('0x01', function ($err, $changes) { if ($err !== null) { // infura banned us to get filter changes - return $this->assertTrue($err !== null); + return $this->fail($err->getMessage()); } $this->assertTrue(is_array($changes)); }); @@ -704,7 +704,7 @@ public function testGetFilterLogs() $eth->getFilterLogs('0x01', function ($err, $logs) { if ($err !== null) { // infura banned us to get filter logs - return $this->assertTrue($err !== null); + return $this->fail($err->getMessage()); } $this->assertTrue(is_array($logs)); }); @@ -726,7 +726,7 @@ public function testGetLogs() 'topics' => ['0x000000000000000000000000a94f5374fce5edbc8e2a8697c15331677e6ebf0b', null, ['0x000000000000000000000000a94f5374fce5edbc8e2a8697c15331677e6ebf0b', '0x0000000000000000000000000aff3454fce5edbc8cca8697c15331677e6ebccc']] ], function ($err, $logs) { if ($err !== null) { - return $this->assertTrue($err !== null); + return $this->fail($err->getMessage()); } $this->assertTrue(is_array($logs)); }); @@ -743,7 +743,7 @@ public function testGetWork() $eth->getWork(function ($err, $work) { if ($err !== null) { - return $this->assertTrue($err !== null); + return $this->fail($err->getMessage()); } $this->assertTrue(is_array($work)); }); @@ -764,7 +764,7 @@ public function testSubmitWork() '0xD1FE5700000000000000000000000000D1FE5700000000000000000000000000' , function ($err, $work) { if ($err !== null) { - return $this->assertTrue($err !== null); + return $this->fail($err->getMessage()); } $this->assertTrue(is_bool($work)); }); @@ -784,7 +784,7 @@ public function testSubmitHashrate() '0xD1FE5700000000000000000000000000D1FE5700000000000000000000000000' , function ($err, $work) { if ($err !== null) { - return $this->assertTrue($err !== null); + return $this->fail($err->getMessage()); } $this->assertTrue(is_bool($work)); }); @@ -803,7 +803,7 @@ public function testGetBlockByNumberAsync() // should return reactphp promise $promise = $eth->getBlockByNumber('latest', false, function ($err, $block) { if ($err !== null) { - return $this->assertTrue($err !== null); + return $this->fail($err->getMessage()); } // weird behavior, see https://github.com/web3p/web3.php/issues/16 $this->assertTrue($block !== null); @@ -825,7 +825,7 @@ public function testUnallowedMethod() $eth->hello(function ($err, $hello) { if ($err !== null) { - return $this->assertTrue($err !== null); + return $this->fail($err->getMessage()); } $this->assertTrue(true); }); diff --git a/test/unit/EthBatchTest.php b/test/unit/EthBatchTest.php index 976fe20e..52520cd6 100644 --- a/test/unit/EthBatchTest.php +++ b/test/unit/EthBatchTest.php @@ -42,7 +42,7 @@ public function testBatch() $eth->provider->execute(function ($err, $data) { if ($err !== null) { - return $this->fail('Got error!'); + return $this->fail($err->getMessage()); } $this->assertTrue($data[0] instanceof BigNumber); $this->assertTrue($data[1] !== null); diff --git a/test/unit/NetBatchTest.php b/test/unit/NetBatchTest.php index 8d6d5e31..b55d7ce7 100644 --- a/test/unit/NetBatchTest.php +++ b/test/unit/NetBatchTest.php @@ -43,7 +43,7 @@ public function testBatch() $net->provider->execute(function ($err, $data) { if ($err !== null) { - return $this->fail('Got error!'); + return $this->fail($err->getMessage()); } $this->assertTrue(is_string($data[0])); $this->assertTrue(is_bool($data[1])); diff --git a/test/unit/PersonalBatchTest.php b/test/unit/PersonalBatchTest.php index 7af5fff7..0d630abf 100644 --- a/test/unit/PersonalBatchTest.php +++ b/test/unit/PersonalBatchTest.php @@ -41,7 +41,7 @@ public function testBatch() $personal->provider->execute(function ($err, $data) { if ($err !== null) { - return $this->assertTrue($err !== null); + return $this->fail($err->getMessage()); } $this->assertTrue(is_array($data[0])); $this->assertTrue(is_string($data[1])); diff --git a/test/unit/ShhBatchTest.php b/test/unit/ShhBatchTest.php index 01093277..4ae75d8a 100644 --- a/test/unit/ShhBatchTest.php +++ b/test/unit/ShhBatchTest.php @@ -41,7 +41,7 @@ public function testBatch() $shh->provider->execute(function ($err, $data) { if ($err !== null) { - return $this->fail('Got error!'); + return $this->fail($err->getMessage()); } $this->assertTrue(is_string($data[0])); $this->assertTrue(is_string($data[1])); @@ -65,7 +65,7 @@ public function testBatch() // $shh->provider->execute(function ($err, $data) { // if ($err !== null) { - // return $this->fail('Got error!'); + // return $this->fail($err->getMessage()); // } // $this->assertTrue(is_string($data[0])); // $this->assertFalse($data[1]); diff --git a/test/unit/Web3BatchTest.php b/test/unit/Web3BatchTest.php index 825a7248..4fbdd534 100644 --- a/test/unit/Web3BatchTest.php +++ b/test/unit/Web3BatchTest.php @@ -48,7 +48,7 @@ public function testBatch() $web3->provider->execute(function ($err, $data) { if ($err !== null) { - return $this->fail('Got error!'); + return $this->fail($err->getMessage()); } $this->assertTrue(is_string($data[0])); $this->assertEquals($data[1], $this->testHash); @@ -72,7 +72,7 @@ public function testWrongParam() $web3->provider->execute(function ($err, $data) { if ($err !== null) { - return $this->fail('Got error!'); + return $this->fail($err->getMessage()); } $this->assertTrue(is_string($data[0])); $this->assertEquals($data[1], $this->testHash); From c4709148f749df24a480d2a3e03798c81146aa9a Mon Sep 17 00:00:00 2001 From: sc0Vu Date: Sun, 27 Aug 2023 14:21:26 +0800 Subject: [PATCH 11/16] Remove deprecated methods eth_compile* --- src/Methods/Eth/CompileLLL.php | 64 ----------------- src/Methods/Eth/CompileSerpent.php | 64 ----------------- src/Methods/Eth/CompileSolidity.php | 64 ----------------- test/unit/EthApiTest.php | 104 ++-------------------------- 4 files changed, 6 insertions(+), 290 deletions(-) delete mode 100644 src/Methods/Eth/CompileLLL.php delete mode 100644 src/Methods/Eth/CompileSerpent.php delete mode 100644 src/Methods/Eth/CompileSolidity.php diff --git a/src/Methods/Eth/CompileLLL.php b/src/Methods/Eth/CompileLLL.php deleted file mode 100644 index 69d33f6a..00000000 --- a/src/Methods/Eth/CompileLLL.php +++ /dev/null @@ -1,64 +0,0 @@ - - * - * @author Peter Lai - * @license MIT - */ - -namespace Web3\Methods\Eth; - -use InvalidArgumentException; -use Web3\Methods\EthMethod; -use Web3\Validators\StringValidator; -use Web3\Formatters\StringFormatter; - -class CompileLLL extends EthMethod -{ - /** - * validators - * - * @var array - */ - protected $validators = [ - StringValidator::class - ]; - - /** - * inputFormatters - * - * @var array - */ - protected $inputFormatters = [ - StringFormatter::class - ]; - - /** - * outputFormatters - * - * @var array - */ - protected $outputFormatters = []; - - /** - * defaultValues - * - * @var array - */ - protected $defaultValues = []; - - /** - * construct - * - * @param string $method - * @param array $arguments - * @return void - */ - // public function __construct($method='', $arguments=[]) - // { - // parent::__construct($method, $arguments); - // } -} \ No newline at end of file diff --git a/src/Methods/Eth/CompileSerpent.php b/src/Methods/Eth/CompileSerpent.php deleted file mode 100644 index 39b105d2..00000000 --- a/src/Methods/Eth/CompileSerpent.php +++ /dev/null @@ -1,64 +0,0 @@ - - * - * @author Peter Lai - * @license MIT - */ - -namespace Web3\Methods\Eth; - -use InvalidArgumentException; -use Web3\Methods\EthMethod; -use Web3\Validators\StringValidator; -use Web3\Formatters\StringFormatter; - -class CompileSerpent extends EthMethod -{ - /** - * validators - * - * @var array - */ - protected $validators = [ - StringValidator::class - ]; - - /** - * inputFormatters - * - * @var array - */ - protected $inputFormatters = [ - StringFormatter::class - ]; - - /** - * outputFormatters - * - * @var array - */ - protected $outputFormatters = []; - - /** - * defaultValues - * - * @var array - */ - protected $defaultValues = []; - - /** - * construct - * - * @param string $method - * @param array $arguments - * @return void - */ - // public function __construct($method='', $arguments=[]) - // { - // parent::__construct($method, $arguments); - // } -} \ No newline at end of file diff --git a/src/Methods/Eth/CompileSolidity.php b/src/Methods/Eth/CompileSolidity.php deleted file mode 100644 index 05de3c0d..00000000 --- a/src/Methods/Eth/CompileSolidity.php +++ /dev/null @@ -1,64 +0,0 @@ - - * - * @author Peter Lai - * @license MIT - */ - -namespace Web3\Methods\Eth; - -use InvalidArgumentException; -use Web3\Methods\EthMethod; -use Web3\Validators\StringValidator; -use Web3\Formatters\StringFormatter; - -class CompileSolidity extends EthMethod -{ - /** - * validators - * - * @var array - */ - protected $validators = [ - StringValidator::class - ]; - - /** - * inputFormatters - * - * @var array - */ - protected $inputFormatters = [ - StringFormatter::class - ]; - - /** - * outputFormatters - * - * @var array - */ - protected $outputFormatters = []; - - /** - * defaultValues - * - * @var array - */ - protected $defaultValues = []; - - /** - * construct - * - * @param string $method - * @param array $arguments - * @return void - */ - // public function __construct($method='', $arguments=[]) - // { - // parent::__construct($method, $arguments); - // } -} \ No newline at end of file diff --git a/test/unit/EthApiTest.php b/test/unit/EthApiTest.php index cc57b538..0149499e 100644 --- a/test/unit/EthApiTest.php +++ b/test/unit/EthApiTest.php @@ -227,7 +227,7 @@ public function testGetBlockTransactionCountByHash() $eth->getBlockTransactionCountByHash('0xb903239f8543d04b5dc1ba6579132b143087c68db1b2168786408fcbce568238', function ($err, $transactionCount) { if ($err !== null) { - return $this->fail($err->getMessage()); + return $this->assertEquals('Key not found in database', $err->getMessage()); } $this->assertTrue(is_numeric($transactionCount->toString())); }); @@ -312,8 +312,7 @@ public function testSign() $eth->sign('0x407d73d8a49eeb85d32cf465507dd71d507100c1', '0xdeadbeaf', function ($err, $sign) { if ($err !== null) { - // infura banned us to sign message - return $this->fail($err->getMessage()); + return $this->assertEquals('cannot sign data; no private key', $err->getMessage()); } $this->assertTrue(is_string($sign)); }); @@ -337,8 +336,7 @@ public function testSendTransaction() 'data' => "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675" ], function ($err, $transaction) { if ($err !== null) { - // infura banned us to send transaction - return $this->fail($err->getMessage()); + return $this->assertEquals('sender account not recognized', $err->getMessage()); } $this->assertTrue(is_string($transaction)); }); @@ -355,7 +353,7 @@ public function testSendRawTransaction() $eth->sendRawTransaction('0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675', function ($err, $transaction) { if ($err !== null) { - return $this->fail($err->getMessage()); + return $this->assertEquals('invalid remainder', $err->getMessage()); } $this->assertTrue(is_string($transaction)); }); @@ -420,7 +418,7 @@ public function testGetBlockByHash() $eth->getBlockByHash('0xb903239f8543d04b5dc1ba6579132b143087c68db1b2168786408fcbce568238', false, function ($err, $block) { if ($err !== null) { - return $this->fail($err->getMessage()); + return $this->assertEquals('Key not found in database', $err->getMessage()); } $this->assertTrue($block !== null); }); @@ -489,7 +487,7 @@ public function testGetTransactionByBlockNumberAndIndex() $eth->getTransactionByBlockNumberAndIndex('0xe8', '0x0', function ($err, $transaction) { if ($err !== null) { - return $this->fail($err->getMessage()); + return $this->assertEquals('LevelUpArrayAdapter named \'blocks\' index out of range: index 232; length: 15', $err->getMessage()); } $this->assertTrue($transaction !== null); }); @@ -546,57 +544,6 @@ public function testGetUncleByBlockNumberAndIndex() }); } - /** - * testCompileSolidity - * - * @return void - */ - public function testCompileSolidity() - { - $eth = $this->eth; - - $eth->compileSolidity('contract test { function multiply(uint a) returns(uint d) { return a * 7; } }', function ($err, $compiled) { - if ($err !== null) { - return $this->fail($err->getMessage()); - } - $this->assertTrue(is_string($compiled)); - }); - } - - /** - * testCompileLLL - * - * @return void - */ - public function testCompileLLL() - { - $eth = $this->eth; - - $eth->compileLLL('(returnlll (suicide (caller)))', function ($err, $compiled) { - if ($err !== null) { - return $this->fail($err->getMessage()); - } - $this->assertTrue(is_string($compiled)); - }); - } - - /** - * testCompileSerpent - * - * @return void - */ - public function testCompileSerpent() - { - $eth = $this->eth; - - $eth->compileSerpent('\/* some serpent *\/', function ($err, $compiled) { - if ($err !== null) { - return $this->fail($err->getMessage()); - } - $this->assertTrue(is_string($compiled)); - }); - } - /** * testNewFilter * @@ -732,23 +679,6 @@ public function testGetLogs() }); } - /** - * testGetWork - * - * @return void - */ - public function testGetWork() - { - $eth = $this->eth; - - $eth->getWork(function ($err, $work) { - if ($err !== null) { - return $this->fail($err->getMessage()); - } - $this->assertTrue(is_array($work)); - }); - } - /** * testSubmitWork * @@ -790,28 +720,6 @@ public function testSubmitHashrate() }); } - /** - * testGetBlockByNumberAsync - * - * @return void - */ - public function testGetBlockByNumberAsync() - { - $eth = $this->eth; - $eth->provider = $this->asyncHttpProvider; - - // should return reactphp promise - $promise = $eth->getBlockByNumber('latest', false, function ($err, $block) { - if ($err !== null) { - return $this->fail($err->getMessage()); - } - // weird behavior, see https://github.com/web3p/web3.php/issues/16 - $this->assertTrue($block !== null); - }); - $this->assertTrue($promise instanceof \React\Promise\PromiseInterface); - \React\Async\await($promise); - } - /** * testUnallowedMethod * From 5714103b187b12cdbfef939d675a1d2bed185251 Mon Sep 17 00:00:00 2001 From: sc0Vu Date: Sun, 27 Aug 2023 14:25:40 +0800 Subject: [PATCH 12/16] Add testBatchAsync --- test/unit/EthApiTest.php | 22 ++++++++++++++++++++++ test/unit/EthBatchTest.php | 26 ++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/test/unit/EthApiTest.php b/test/unit/EthApiTest.php index 0149499e..6109be13 100644 --- a/test/unit/EthApiTest.php +++ b/test/unit/EthApiTest.php @@ -720,6 +720,28 @@ public function testSubmitHashrate() }); } + /** + * testGetBlockByNumberAsync + * + * @return void + */ + public function testGetBlockByNumberAsync() + { + $eth = $this->eth; + $eth->provider = $this->asyncHttpProvider; + + // should return reactphp promise + $promise = $eth->getBlockByNumber('latest', false, function ($err, $block) { + if ($err !== null) { + return $this->assertTrue($err !== null); + } + // weird behavior, see https://github.com/web3p/web3.php/issues/16 + $this->assertTrue($block !== null); + }); + $this->assertTrue($promise instanceof \React\Promise\PromiseInterface); + \React\Async\await($promise); + } + /** * testUnallowedMethod * diff --git a/test/unit/EthBatchTest.php b/test/unit/EthBatchTest.php index 52520cd6..591f50e4 100644 --- a/test/unit/EthBatchTest.php +++ b/test/unit/EthBatchTest.php @@ -48,4 +48,30 @@ public function testBatch() $this->assertTrue($data[1] !== null); }); } + + /** + * testBatchAsync + * + * @return void + */ + public function testBatchAsync() + { + $eth = $this->eth; + $eth->provider = $this->asyncHttpProvider; + + $eth->batch(true); + $eth->protocolVersion(); + $eth->syncing(); + + // should return reactphp promise + $promise = $eth->provider->execute(function ($err, $data) { + if ($err !== null) { + return $this->fail($err->getMessage()); + } + $this->assertTrue($data[0] instanceof BigNumber); + $this->assertTrue($data[1] !== null); + }); + $this->assertTrue($promise instanceof \React\Promise\PromiseInterface); + \React\Async\await($promise); + } } \ No newline at end of file From cda20fb0ee9f03edf6b858e10b427b411889a052 Mon Sep 17 00:00:00 2001 From: sc0Vu Date: Sun, 27 Aug 2023 14:34:58 +0800 Subject: [PATCH 13/16] Add testBatchAsync --- test/unit/NetBatchTest.php | 28 ++++++++++++++++++++++++++++ test/unit/ShhBatchTest.php | 38 +++++++++++++++++++++----------------- 2 files changed, 49 insertions(+), 17 deletions(-) diff --git a/test/unit/NetBatchTest.php b/test/unit/NetBatchTest.php index b55d7ce7..9fadb1f8 100644 --- a/test/unit/NetBatchTest.php +++ b/test/unit/NetBatchTest.php @@ -50,4 +50,32 @@ public function testBatch() $this->assertTrue($data[2] instanceof BigNumber); }); } + + /** + * testBatchAsync + * + * @return void + */ + public function testBatchAsync() + { + $net = $this->net; + $net->provider = $this->asyncHttpProvider; + + $net->batch(true); + $net->version(); + $net->listening(); + $net->peerCount(); + + // should return reactphp promise + $promise = $net->provider->execute(function ($err, $data) { + if ($err !== null) { + return $this->fail($err->getMessage()); + } + $this->assertTrue(is_string($data[0])); + $this->assertTrue(is_bool($data[1])); + $this->assertTrue($data[2] instanceof BigNumber); + }); + $this->assertTrue($promise instanceof \React\Promise\PromiseInterface); + \React\Async\await($promise); + } } \ No newline at end of file diff --git a/test/unit/ShhBatchTest.php b/test/unit/ShhBatchTest.php index 4ae75d8a..b1b3fc7a 100644 --- a/test/unit/ShhBatchTest.php +++ b/test/unit/ShhBatchTest.php @@ -45,30 +45,34 @@ public function testBatch() } $this->assertTrue(is_string($data[0])); $this->assertTrue(is_string($data[1])); + $this->assertEquals($data[0], $data[1]); }); } /** - * testWrongParam + * testBatchAsync * * @return void */ - // public function testWrongParam() - // { - // $this->expectException(RuntimeException::class); - - // $shh = $this->shh; + public function testBatchAsync() + { + $shh = $this->shh; + $shh->provider = $this->asyncHttpProvider; - // $shh->batch(true); - // $shh->version(); - // $shh->hasIdentity('0'); + $shh->batch(true); + $shh->version(); + $shh->version(); - // $shh->provider->execute(function ($err, $data) { - // if ($err !== null) { - // return $this->fail($err->getMessage()); - // } - // $this->assertTrue(is_string($data[0])); - // $this->assertFalse($data[1]); - // }); - // } + // should return reactphp promise + $promise = $shh->provider->execute(function ($err, $data) { + if ($err !== null) { + return $this->fail($err->getMessage()); + } + $this->assertTrue(is_string($data[0])); + $this->assertTrue(is_string($data[1])); + $this->assertEquals($data[0], $data[1]); + }); + $this->assertTrue($promise instanceof \React\Promise\PromiseInterface); + \React\Async\await($promise); + } } \ No newline at end of file From ef9ca98c55e10f9e9f91fdfc317b9385a4a013bb Mon Sep 17 00:00:00 2001 From: sc0vu Date: Thu, 31 Aug 2023 12:01:18 +0800 Subject: [PATCH 14/16] Update contract --- src/Contract.php | 16 ++--- test/unit/ContractTest.php | 122 +++++++++++++++++++++++++++++++++++++ 2 files changed, 130 insertions(+), 8 deletions(-) diff --git a/src/Contract.php b/src/Contract.php index 87ad1fc4..ba5c51ea 100644 --- a/src/Contract.php +++ b/src/Contract.php @@ -441,7 +441,7 @@ public function abi($abi) * Deploy a contruct with params. * * @param mixed - * @return void + * @return void|\React\Promise\PromiseInterface */ public function new() { @@ -469,7 +469,7 @@ public function new() } $transaction['data'] = '0x' . $this->bytecode . Utils::stripZero($data); - $this->eth->sendTransaction($transaction, function ($err, $transaction) use ($callback){ + return $this->eth->sendTransaction($transaction, function ($err, $transaction) use ($callback){ if ($err !== null) { return call_user_func($callback, $err, null); } @@ -483,7 +483,7 @@ public function new() * Send function method. * * @param mixed - * @return void + * @return void|\React\Promise\PromiseInterface */ public function send() { @@ -564,7 +564,7 @@ public function send() $transaction['to'] = $this->toAddress; $transaction['data'] = $functionSignature . Utils::stripZero($data); - $this->eth->sendTransaction($transaction, function ($err, $transaction) use ($callback){ + return $this->eth->sendTransaction($transaction, function ($err, $transaction) use ($callback){ if ($err !== null) { return call_user_func($callback, $err, null); } @@ -578,7 +578,7 @@ public function send() * Call function method. * * @param mixed - * @return void + * @return void|\React\Promise\PromiseInterface */ public function call() { @@ -658,7 +658,7 @@ public function call() $transaction['to'] = $this->toAddress; $transaction['data'] = $functionSignature . Utils::stripZero($data); - $this->eth->call($transaction, $defaultBlock, function ($err, $transaction) use ($callback, $function){ + return $this->eth->call($transaction, $defaultBlock, function ($err, $transaction) use ($callback, $function){ if ($err !== null) { return call_user_func($callback, $err, null); } @@ -674,7 +674,7 @@ public function call() * Estimate function gas. * * @param mixed - * @return void + * @return void|\React\Promise\PromiseInterface */ public function estimateGas() { @@ -778,7 +778,7 @@ public function estimateGas() $transaction['data'] = $functionSignature . Utils::stripZero($data); } - $this->eth->estimateGas($transaction, function ($err, $gas) use ($callback) { + return $this->eth->estimateGas($transaction, function ($err, $gas) use ($callback) { if ($err !== null) { return call_user_func($callback, $err, null); } diff --git a/test/unit/ContractTest.php b/test/unit/ContractTest.php index 11734f4e..c675c2bc 100644 --- a/test/unit/ContractTest.php +++ b/test/unit/ContractTest.php @@ -21,6 +21,13 @@ class ContractTest extends TestCase */ protected $contract; + /** + * async contract + * + * @var \Web3\Contract + */ + protected $asyncContract; + /** * testAbi * GameToken abi from https://github.com/sc0Vu/GameToken @@ -425,6 +432,7 @@ public function setUp(): void } } }); + $this->asyncContract = new Contract($this->asyncHttpProvider, $this->testAbi); } /** @@ -1514,4 +1522,118 @@ public function testIssue134() } }); } + + // + /** + * testAsync + * + * @return void + */ + public function testAsync() + { + $contract = $this->contract; + + if (!isset($this->accounts[0])) { + $fromAccount = '0x407d73d8a49eeb85d32cf465507dd71d507100c1'; + } else { + $fromAccount = $this->accounts[0]; + } + if (!isset($this->accounts[1])) { + $toAccount = '0x407d73d8a49eeb85d32cf465507dd71d507100c2'; + } else { + $toAccount = $this->accounts[1]; + } + $contract->bytecode($this->testBytecode)->new(1000000, 'Game Token', 1, 'GT', [ + 'from' => $fromAccount, + 'gas' => '0x200b20' + ], function ($err, $result) use ($contract) { + if ($err !== null) { + return $this->fail($err->getMessage()); + } + if ($result) { + echo "\nTransaction has made:) id: " . $result . "\n"; + } + $transactionId = $result; + $this->assertTrue((preg_match('/^0x[a-f0-9]{64}$/', $transactionId) === 1)); + + $contract->eth->getTransactionReceipt($transactionId, function ($err, $transaction) { + if ($err !== null) { + return $this->fail($err); + } + if ($transaction) { + $this->contractAddress = $transaction->contractAddress; + echo "\nTransaction has mined:) block number: " . $transaction->blockNumber . "\n"; + } + }); + }); + + if (!isset($this->contractAddress)) { + $this->contractAddress = '0x407d73d8a49eeb85d32cf465507dd71d507100c2'; + } + $asyncContract = $this->asyncContract; + $promise1 = $asyncContract->at($this->contractAddress)->send('transfer', $toAccount, 16, [ + 'from' => $fromAccount, + 'gas' => '0x200b20' + ], function ($err, $result) use ($contract, $fromAccount, $toAccount) { + if ($err !== null) { + return $this->fail($err->getMessage()); + } + if ($result) { + echo "\nTransaction has made:) id: " . $result . "\n"; + } + $transactionId = $result; + $this->assertTrue((preg_match('/^0x[a-f0-9]{64}$/', $transactionId) === 1)); + + $promise = $asyncContract->eth->getTransactionReceipt($transactionId, function ($err, $transaction) use ($fromAccount, $toAccount, $contract) { + if ($err !== null) { + return $this->fail($err); + } + if ($transaction) { + $topics = $transaction->logs[0]->topics; + echo "\nTransaction has mined:) block number: " . $transaction->blockNumber . "\n"; + + // validate topics + $this->assertEquals($contract->ethabi->encodeEventSignature($this->contract->events['Transfer']), $topics[0]); + $this->assertEquals('0x' . IntegerFormatter::format($fromAccount), $topics[1]); + $this->assertEquals('0x' . IntegerFormatter::format($toAccount), $topics[2]); + } + }); + $this->assertTrue($promise instanceof \React\Promise\PromiseInterface); + \React\Async\await($promise); + }); + $this->assertTrue($promise1 instanceof \React\Promise\PromiseInterface); + + $promise2 = $asyncContract->at($this->contractAddress)->call('balanceOf', $fromAccount, [ + 'from' => $fromAccount + ], function ($err, $result) use ($contract) { + if ($err !== null) { + return $this->fail($err->getMessage()); + } + if (isset($result)) { + // $bn = Utils::toBn($result); + // $this->assertEquals($bn->toString(), '10000', 'Balance should be 10000.'); + $this->assertTrue($result !== null); + } + }); + + $this->assertTrue($promise2 instanceof \React\Promise\PromiseInterface); + + $promise3 = $contract->bytecode($this->testBytecode)->estimateGas(10000, 'Game Token', 1, 'GT', [ + 'from' => $fromAccount, + 'gas' => '0x200b20' + ], function ($err, $result) use ($contract) { + if ($err !== null) { + return $this->fail($err->getMessage()); + } + if (isset($result)) { + echo "\nEstimate gas: " . $result->toString() . "\n"; + $this->assertTrue($result !== null); + } + }); + \React\Async\await(\React\Async\parallel([ + function () { return $promise1; }, + function () { return $promise2; }, + function () { return $promise3; }, + ])); + } } \ No newline at end of file From cc5e9a2ad1203ae2748739f616fe74ea0d022d04 Mon Sep 17 00:00:00 2001 From: sc0vu Date: Thu, 31 Aug 2023 12:26:12 +0800 Subject: [PATCH 15/16] Update test --- test/unit/ContractTest.php | 11 +++++------ test/unit/EthApiTest.php | 2 +- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/test/unit/ContractTest.php b/test/unit/ContractTest.php index c675c2bc..3d72bde4 100644 --- a/test/unit/ContractTest.php +++ b/test/unit/ContractTest.php @@ -1523,7 +1523,6 @@ public function testIssue134() }); } - // /** * testAsync * @@ -1574,7 +1573,7 @@ public function testAsync() $promise1 = $asyncContract->at($this->contractAddress)->send('transfer', $toAccount, 16, [ 'from' => $fromAccount, 'gas' => '0x200b20' - ], function ($err, $result) use ($contract, $fromAccount, $toAccount) { + ], function ($err, $result) use ($contract, $fromAccount, $toAccount, $asyncContract) { if ($err !== null) { return $this->fail($err->getMessage()); } @@ -1618,7 +1617,7 @@ public function testAsync() $this->assertTrue($promise2 instanceof \React\Promise\PromiseInterface); - $promise3 = $contract->bytecode($this->testBytecode)->estimateGas(10000, 'Game Token', 1, 'GT', [ + $promise3 = $asyncContract->bytecode($this->testBytecode)->estimateGas('balanceOf', $toAccount, [ 'from' => $fromAccount, 'gas' => '0x200b20' ], function ($err, $result) use ($contract) { @@ -1631,9 +1630,9 @@ public function testAsync() } }); \React\Async\await(\React\Async\parallel([ - function () { return $promise1; }, - function () { return $promise2; }, - function () { return $promise3; }, + function () use ($promise1) { return $promise1; }, + function () use ($promise2) { return $promise2; }, + function () use ($promise3) { return $promise3; }, ])); } } \ No newline at end of file diff --git a/test/unit/EthApiTest.php b/test/unit/EthApiTest.php index 6109be13..e1d2a4c2 100644 --- a/test/unit/EthApiTest.php +++ b/test/unit/EthApiTest.php @@ -487,7 +487,7 @@ public function testGetTransactionByBlockNumberAndIndex() $eth->getTransactionByBlockNumberAndIndex('0xe8', '0x0', function ($err, $transaction) { if ($err !== null) { - return $this->assertEquals('LevelUpArrayAdapter named \'blocks\' index out of range: index 232; length: 15', $err->getMessage()); + return $this->assertStringStartsWith('LevelUpArrayAdapter named \'blocks\' index out of range', $err->getMessage()); } $this->assertTrue($transaction !== null); }); From 116659efb80439019a56448cba5a29c84798411f Mon Sep 17 00:00:00 2001 From: sc0vu Date: Thu, 31 Aug 2023 12:26:47 +0800 Subject: [PATCH 16/16] Update return type for call function --- src/Eth.php | 2 +- src/Net.php | 2 +- src/Personal.php | 2 +- src/Shh.php | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Eth.php b/src/Eth.php index a20737da..022aaa7b 100644 --- a/src/Eth.php +++ b/src/Eth.php @@ -66,7 +66,7 @@ public function __construct($provider) * * @param string $name * @param array $arguments - * @return void + * @return void|\React\Promise\PromiseInterface */ public function __call($name, $arguments) { diff --git a/src/Net.php b/src/Net.php index 92985f69..adae1047 100644 --- a/src/Net.php +++ b/src/Net.php @@ -66,7 +66,7 @@ public function __construct($provider) * * @param string $name * @param array $arguments - * @return void + * @return void|\React\Promise\PromiseInterface */ public function __call($name, $arguments) { diff --git a/src/Personal.php b/src/Personal.php index 5df0cca7..8b479776 100644 --- a/src/Personal.php +++ b/src/Personal.php @@ -66,7 +66,7 @@ public function __construct($provider) * * @param string $name * @param array $arguments - * @return void + * @return void|\React\Promise\PromiseInterface */ public function __call($name, $arguments) { diff --git a/src/Shh.php b/src/Shh.php index 7a303fb9..ed38738b 100644 --- a/src/Shh.php +++ b/src/Shh.php @@ -67,7 +67,7 @@ public function __construct($provider) * * @param string $name * @param array $arguments - * @return void + * @return void|\React\Promise\PromiseInterface */ public function __call($name, $arguments) {