diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 11ebbb7..00fe049 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -18,7 +18,7 @@ jobs: uses: "shivammathur/setup-php@v2" with: coverage: "pcov" - php-version: "8.0" + php-version: "8.1" ini-values: memory_limit=-1 - name: "Cache Composer dependencies" @@ -27,8 +27,8 @@ jobs: path: | ~/.composer/cache vendor - key: "php-8.0" - restore-keys: "php-8.0" + key: "php-8.1" + restore-keys: "php-8.1" - name: "Validate composer" run: "composer validate" diff --git a/CHANGELOG.md b/CHANGELOG.md index a39dbea..a6e0bc9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,11 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). -## [Unreleased] +## [1.0.0] - 2022-05-22 +- Upgrade to PHP 8 +- Add php-cs-fixer +- Add phpstan by @akondas in #1 +- Configure GitHub Actions by @akondas in #3 ## [0.1.0] - 2018-03-15 ### Added @@ -13,4 +17,3 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - Storing and validate Blockchain - Proof of Work with difficulty (missing consensus on the difficulty) - Communicating with other nodes & controlling the node (based on ReactPHP) - \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..bdfad10 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,4 @@ +FROM php:8.1-cli-alpine +RUN apk add --no-cache bash curl git +RUN docker-php-ext-install pcntl +COPY --from=composer:2.1.5 /usr/bin/composer /usr/bin/composer diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..cd5a4ac --- /dev/null +++ b/Makefile @@ -0,0 +1,7 @@ +build: + docker build -t php-blockchain . + +shell: build + docker run -it --rm --name php-blockchain-dev -v $(PWD):/var/app -w /var/app php-blockchain:latest /bin/bash + +.PHONY: build shell diff --git a/README.md b/README.md index b0677c6..0bb9569 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # Blockchain implementation in PHP -[![Minimum PHP Version](https://img.shields.io/badge/php-%3E%3D%207.2-8892BF.svg)](https://php.net/) -[![Build Status](https://travis-ci.org/akondas/php-blockchain.svg?branch=master)](https://travis-ci.org/akondas/php-blockchain) +[![Minimum PHP Version](https://img.shields.io/badge/php-%3E%3D%208.1-8892BF.svg)](https://php.net/) +[![Build](https://github.com/akondas/php-blockchain/actions/workflows/build.yaml/badge.svg)](https://github.com/akondas/php-blockchain/actions/workflows/build.yaml) [![License](https://poser.pugx.org/akondas/php-blockchain/license.svg)](https://packagist.org/packages/akondas/php-blockchain) Clean code approach to blockchain technology. Learn blockchain by reading source code. diff --git a/composer.json b/composer.json index 0010287..9bc923e 100644 --- a/composer.json +++ b/composer.json @@ -10,7 +10,7 @@ } ], "require": { - "php": "^8.0", + "php": "^8.1", "react/http": "^0.8.1", "friendsofphp/php-cs-fixer": "^3.8" }, diff --git a/composer.lock b/composer.lock index 260b2eb..5e4aedd 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "9a570098c914ecce1eb1637d3a724860", + "content-hash": "ff299feeec7d1b167d0e0d8f577ac65e", "packages": [ { "name": "composer/pcre", @@ -4954,7 +4954,7 @@ "prefer-stable": false, "prefer-lowest": false, "platform": { - "php": "^8.0" + "php": "^8.1" }, "platform-dev": [], "plugin-api-version": "2.1.0" diff --git a/src/Block.php b/src/Block.php index 99e14e1..8f9de4b 100644 --- a/src/Block.php +++ b/src/Block.php @@ -11,57 +11,15 @@ final class Block implements JsonSerializable { public const HASH_ALGORITHM = 'sha256'; - /** - * @var int - */ - private $index; - - /** - * @var string - */ - private $hash; - - /** - * @var string - */ - private $previousHash; - - /** - * @var \DateTimeImmutable - */ - private $createdAt; - - /** - * @var string - */ - private $data; - - /** - * @var int - */ - private $difficulty; - - /** - * @var int - */ - private $nonce; - public function __construct( - int $index, - string $hash, - string $previousHash, - DateTimeImmutable $createdAt, - string $data, - int $difficulty, - int $nonce + private int $index, + private string $hash, + private string $previousHash, + private DateTimeImmutable $createdAt, + private string $data, + private int $difficulty, + private int $nonce ) { - $this->index = $index; - $this->hash = $hash; - $this->previousHash = $previousHash; - $this->createdAt = $createdAt; - $this->data = $data; - $this->difficulty = $difficulty; - $this->nonce = $nonce; } public static function genesis(): self @@ -134,7 +92,7 @@ public static function calculateHash( } /** - * @return mixed[] + * @return array */ public function jsonSerialize(): array { diff --git a/src/Blockchain.php b/src/Blockchain.php index 580afc3..46341e1 100644 --- a/src/Blockchain.php +++ b/src/Blockchain.php @@ -54,7 +54,7 @@ public function withLastBlockOnly(): self } /** - * @return Block[] + * @return non-empty-array */ public function blocks(): array { diff --git a/src/Miner.php b/src/Miner.php index e3ab1ad..b542f1b 100644 --- a/src/Miner.php +++ b/src/Miner.php @@ -9,20 +9,8 @@ final class Miner { - /** - * @var Blockchain - */ - private $blockchain; - - /** - * @var HashDifficulty - */ - private $hashDifficulty; - - public function __construct(Blockchain $blockchain, HashDifficulty $hashDifficulty) + public function __construct(private Blockchain $blockchain, private HashDifficulty $hashDifficulty) { - $this->blockchain = $blockchain; - $this->hashDifficulty = $hashDifficulty; } public function mineBlock(string $data): Block diff --git a/src/Miner/HashDifficulty/ZeroPrefix.php b/src/Miner/HashDifficulty/ZeroPrefix.php index a70d755..4e7c332 100644 --- a/src/Miner/HashDifficulty/ZeroPrefix.php +++ b/src/Miner/HashDifficulty/ZeroPrefix.php @@ -17,7 +17,7 @@ public function hashMatchesDifficulty(string $hash, int $difficulty): bool $binary = $this->binaryString($hash, $difficulty); $prefix = \str_repeat('0', $difficulty); - return \strpos($binary, $prefix) === 0; + return \str_starts_with($binary, $prefix); } private function binaryString(string $hash, int $difficulty): string diff --git a/src/Node.php b/src/Node.php index c6e09bb..4a64bb0 100644 --- a/src/Node.php +++ b/src/Node.php @@ -10,20 +10,8 @@ final class Node { - /** - * @var Miner - */ - private $miner; - - /** - * @var P2pServer - */ - private $p2pServer; - - public function __construct(Miner $miner, P2pServer $p2pServer) + public function __construct(private Miner $miner, private P2pServer $p2pServer) { - $this->miner = $miner; - $this->p2pServer = $p2pServer; } /** diff --git a/src/Node/Message.php b/src/Node/Message.php index 5107491..99af371 100644 --- a/src/Node/Message.php +++ b/src/Node/Message.php @@ -12,20 +12,10 @@ final class Message public const BLOCKCHAIN = 'blockchain'; - /** - * @var string - */ - private $type; - - /** - * @var ?string - */ - private $data; - - public function __construct(string $type, ?string $data = null) - { - $this->type = $type; - $this->data = $data; + public function __construct( + private string $type, + private ?string $data = null + ) { } public function type(): string diff --git a/src/Node/Peer.php b/src/Node/Peer.php index 0b03e22..1df7fed 100644 --- a/src/Node/Peer.php +++ b/src/Node/Peer.php @@ -29,7 +29,7 @@ public function port(): int } /** - * @return mixed[] + * @return array */ public function jsonSerialize(): array { diff --git a/src/WebServer.php b/src/WebServer.php index e0b41c3..a9c3400 100644 --- a/src/WebServer.php +++ b/src/WebServer.php @@ -10,14 +10,8 @@ final class WebServer { - /** - * @var Node - */ - private $node; - - public function __construct(Node $node) + public function __construct(private Node $node) { - $this->node = $node; } public function __invoke(ServerRequestInterface $request): Response diff --git a/src/WebServer/Response/JsonResponse.php b/src/WebServer/Response/JsonResponse.php index 426e5fc..a376d37 100644 --- a/src/WebServer/Response/JsonResponse.php +++ b/src/WebServer/Response/JsonResponse.php @@ -9,10 +9,7 @@ final class JsonResponse extends HttpResponse implements Response { - /** - * @param mixed $data - */ - public function __construct($data) + public function __construct(mixed $data) { parent::__construct(200, ['Content-Type' => 'application/json'], \json_encode($data)); }