3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Block { public $nonce; public function __construct($index, $timestamp, $data, $previousHash = null) { $this->index = $index; $this->timestamp = $timestamp; $this->data = $data; $this->previousHash = $previousHash; $this->hash = $this->calculateHash(); $this->nonce = 0; } public function calculateHash() { return hash("sha256", $this->index.$this->previousHash.$this->timestamp.((string)$this->data).$this->nonce); } } class BlockChain { /** * Instantiates a new Blockchain. */ public function __construct() { $this->chain = [$this->createGenesisBlock()]; $this->difficulty = 4; } /** * Creates the genesis block. */ private function createGenesisBlock() { return new Block(0, strtotime("2017-01-01"), "Genesis Block"); } /** * Gets the last block of the chain. */ public function getLastBlock() { return $this->chain[count($this->chain)-1]; } /** * Pushes a new block onto the chain. */ public function push($block) { $block->previousHash = $this->getLastBlock()->hash; $this->mine($block); array_push($this->chain, $block); } /** * Mines a block. */ public function mine($block) { while (substr($block->hash, 0, $this->difficulty) !== str_repeat("0", $this->difficulty)) { $block->nonce++; $block->hash = $block->calculateHash(); } echo "Block mined: ".$block->hash."\n"; } /** * Validates the blockchain's integrity. True if the blockchain is valid, false otherwise. */ public function isValid() { for ($i = 1; $i < count($this->chain); $i++) { $currentBlock = $this->chain[$i]; $previousBlock = $this->chain[$i-1]; if ($currentBlock->hash != $currentBlock->calculateHash()) { return false; } if ($currentBlock->previousHash != $previousBlock->hash) { return false; } } return true; } } /* Set up a simple chain and mine two blocks. */ $testCoin = new BlockChain(); echo "mining block 1...\n"; $testCoin->push(new Block(1, strtotime("now"), "amount: 4")); echo "mining block 2...\n"; $testCoin->push(new Block(2, strtotime("now"), "amount: 10")); echo json_encode($testCoin, JSON_PRETTY_PRINT);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/iqUn3
function name:  (null)
number of ops:  33
compiled vars:  !0 = $testCoin
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   98     0  E >   NEW                                              $1      'BlockChain'
          1        DO_FCALL                                      0          
          2        ASSIGN                                                   !0, $1
  100     3        ECHO                                                     'mining+block+1...%0A'
  101     4        INIT_METHOD_CALL                                         !0, 'push'
          5        NEW                                              $4      'Block'
          6        SEND_VAL_EX                                              1
          7        INIT_FCALL                                               'strtotime'
          8        SEND_VAL                                                 'now'
          9        DO_ICALL                                         $5      
         10        SEND_VAR_NO_REF_EX                                       $5
         11        SEND_VAL_EX                                              'amount%3A+4'
         12        DO_FCALL                                      0          
         13        SEND_VAR_NO_REF_EX                                       $4
         14        DO_FCALL                                      0          
  103    15        ECHO                                                     'mining+block+2...%0A'
  104    16        INIT_METHOD_CALL                                         !0, 'push'
         17        NEW                                              $8      'Block'
         18        SEND_VAL_EX                                              2
         19        INIT_FCALL                                               'strtotime'
         20        SEND_VAL                                                 'now'
         21        DO_ICALL                                         $9      
         22        SEND_VAR_NO_REF_EX                                       $9
         23        SEND_VAL_EX                                              'amount%3A+10'
         24        DO_FCALL                                      0          
         25        SEND_VAR_NO_REF_EX                                       $8
         26        DO_FCALL                                      0          
  106    27        INIT_FCALL                                               'json_encode'
         28        SEND_VAR                                                 !0
         29        SEND_VAL                                                 128
         30        DO_ICALL                                         $12     
         31        ECHO                                                     $12
         32      > RETURN                                                   1

Class Block:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/iqUn3
function name:  __construct
number of ops:  19
compiled vars:  !0 = $index, !1 = $timestamp, !2 = $data, !3 = $previousHash
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    6     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
          3        RECV_INIT                                        !3      null
    8     4        ASSIGN_OBJ                                               'index'
          5        OP_DATA                                                  !0
    9     6        ASSIGN_OBJ                                               'timestamp'
          7        OP_DATA                                                  !1
   10     8        ASSIGN_OBJ                                               'data'
          9        OP_DATA                                                  !2
   11    10        ASSIGN_OBJ                                               'previousHash'
         11        OP_DATA                                                  !3
   12    12        INIT_METHOD_CALL                                         'calculateHash'
         13        DO_FCALL                                      0  $9      
         14        ASSIGN_OBJ                                               'hash'
         15        OP_DATA                                                  $9
   13    16        ASSIGN_OBJ                                               'nonce'
         17        OP_DATA                                                  0
   14    18      > RETURN                                                   null

End of function __construct

Function calculatehash:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/iqUn3
function name:  calculateHash
number of ops:  16
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   18     0  E >   INIT_FCALL                                               'hash'
          1        SEND_VAL                                                 'sha256'
          2        FETCH_OBJ_R                                      ~0      'index'
          3        FETCH_OBJ_R                                      ~1      'previousHash'
          4        CONCAT                                           ~2      ~0, ~1
          5        FETCH_OBJ_R                                      ~3      'timestamp'
          6        CONCAT                                           ~4      ~2, ~3
          7        FETCH_OBJ_R                                      ~5      'data'
          8        CAST                                          6  ~6      ~5
          9        CONCAT                                           ~7      ~4, ~6
         10        FETCH_OBJ_R                                      ~8      'nonce'
         11        CONCAT                                           ~9      ~7, ~8
         12        SEND_VAL                                                 ~9
         13        DO_ICALL                                         $10     
         14      > RETURN                                                   $10
   19    15*     > RETURN                                                   null

End of function calculatehash

End of class Block.

Class BlockChain:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/iqUn3
function name:  __construct
number of ops:  8
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   29     0  E >   INIT_METHOD_CALL                                         'createGenesisBlock'
          1        DO_FCALL                                      0  $1      
          2        INIT_ARRAY                                       ~2      $1
          3        ASSIGN_OBJ                                               'chain'
          4        OP_DATA                                                  ~2
   30     5        ASSIGN_OBJ                                               'difficulty'
          6        OP_DATA                                                  4
   31     7      > RETURN                                                   null

End of function __construct

Function creategenesisblock:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/iqUn3
function name:  createGenesisBlock
number of ops:  10
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   38     0  E >   NEW                                              $0      'Block'
          1        SEND_VAL_EX                                              0
          2        INIT_FCALL                                               'strtotime'
          3        SEND_VAL                                                 '2017-01-01'
          4        DO_ICALL                                         $1      
          5        SEND_VAR_NO_REF_EX                                       $1
          6        SEND_VAL_EX                                              'Genesis+Block'
          7        DO_FCALL                                      0          
          8      > RETURN                                                   $0
   39     9*     > RETURN                                                   null

End of function creategenesisblock

Function getlastblock:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/iqUn3
function name:  getLastBlock
number of ops:  7
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   46     0  E >   FETCH_OBJ_R                                      ~1      'chain'
          1        COUNT                                            ~2      ~1
          2        SUB                                              ~3      ~2, 1
          3        FETCH_OBJ_R                                      ~0      'chain'
          4        FETCH_DIM_R                                      ~4      ~0, ~3
          5      > RETURN                                                   ~4
   47     6*     > RETURN                                                   null

End of function getlastblock

Function push:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/iqUn3
function name:  push
number of ops:  15
compiled vars:  !0 = $block
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   52     0  E >   RECV                                             !0      
   54     1        INIT_METHOD_CALL                                         'getLastBlock'
          2        DO_FCALL                                      0  $2      
          3        FETCH_OBJ_R                                      ~3      $2, 'hash'
          4        ASSIGN_OBJ                                               !0, 'previousHash'
          5        OP_DATA                                                  ~3
   55     6        INIT_METHOD_CALL                                         'mine'
          7        SEND_VAR_EX                                              !0
          8        DO_FCALL                                      0          
   56     9        INIT_FCALL                                               'array_push'
         10        FETCH_OBJ_W                                      $5      'chain'
         11        SEND_REF                                                 $5
         12        SEND_VAR                                                 !0
         13        DO_ICALL                                                 
   57    14      > RETURN                                                   null

End of function push

Function mine:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 7
Branch analysis from position: 7
2 jumps found. (Code = 44) Position 1 = 21, Position 2 = 2
Branch analysis from position: 21
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 2
2 jumps found. (Code = 44) Position 1 = 21, Position 2 = 2
Branch analysis from position: 21
Branch analysis from position: 2
filename:       /in/iqUn3
function name:  mine
number of ops:  26
compiled vars:  !0 = $block
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   62     0  E >   RECV                                             !0      
   64     1      > JMP                                                      ->7
   65     2    >   PRE_INC_OBJ                                              !0, 'nonce'
   66     3        INIT_METHOD_CALL                                         !0, 'calculateHash'
          4        DO_FCALL                                      0  $3      
          5        ASSIGN_OBJ                                               !0, 'hash'
          6        OP_DATA                                                  $3
   64     7    >   INIT_FCALL                                               'substr'
          8        FETCH_OBJ_R                                      ~4      !0, 'hash'
          9        SEND_VAL                                                 ~4
         10        SEND_VAL                                                 0
         11        FETCH_OBJ_R                                      ~5      'difficulty'
         12        SEND_VAL                                                 ~5
         13        DO_ICALL                                         $6      
         14        INIT_FCALL                                               'str_repeat'
         15        SEND_VAL                                                 '0'
         16        FETCH_OBJ_R                                      ~7      'difficulty'
         17        SEND_VAL                                                 ~7
         18        DO_ICALL                                         $8      
         19        IS_NOT_IDENTICAL                                         $6, $8
         20      > JMPNZ                                                    ~9, ->2
   69    21    >   FETCH_OBJ_R                                      ~10     !0, 'hash'
         22        CONCAT                                           ~11     'Block+mined%3A+', ~10
         23        CONCAT                                           ~12     ~11, '%0A'
         24        ECHO                                                     ~12
   70    25      > RETURN                                                   null

End of function mine

Function isvalid:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 21
Branch analysis from position: 21
2 jumps found. (Code = 44) Position 1 = 25, Position 2 = 2
Branch analysis from position: 25
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 2
2 jumps found. (Code = 43) Position 1 = 14, Position 2 = 15
Branch analysis from position: 14
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 15
2 jumps found. (Code = 43) Position 1 = 19, Position 2 = 20
Branch analysis from position: 19
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 20
2 jumps found. (Code = 44) Position 1 = 25, Position 2 = 2
Branch analysis from position: 25
Branch analysis from position: 2
filename:       /in/iqUn3
function name:  isValid
number of ops:  27
compiled vars:  !0 = $i, !1 = $currentBlock, !2 = $previousBlock
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   77     0  E >   ASSIGN                                                   !0, 1
          1      > JMP                                                      ->21
   78     2    >   FETCH_OBJ_R                                      ~4      'chain'
          3        FETCH_DIM_R                                      ~5      ~4, !0
          4        ASSIGN                                                   !1, ~5
   79     5        SUB                                              ~8      !0, 1
          6        FETCH_OBJ_R                                      ~7      'chain'
          7        FETCH_DIM_R                                      ~9      ~7, ~8
          8        ASSIGN                                                   !2, ~9
   81     9        FETCH_OBJ_R                                      ~11     !1, 'hash'
         10        INIT_METHOD_CALL                                         !1, 'calculateHash'
         11        DO_FCALL                                      0  $12     
         12        IS_NOT_EQUAL                                             $12, ~11
         13      > JMPZ                                                     ~13, ->15
   82    14    > > RETURN                                                   <false>
   85    15    >   FETCH_OBJ_R                                      ~14     !1, 'previousHash'
         16        FETCH_OBJ_R                                      ~15     !2, 'hash'
         17        IS_NOT_EQUAL                                             ~14, ~15
         18      > JMPZ                                                     ~16, ->20
   86    19    > > RETURN                                                   <false>
   77    20    >   PRE_INC                                                  !0
         21    >   FETCH_OBJ_R                                      ~18     'chain'
         22        COUNT                                            ~19     ~18
         23        IS_SMALLER                                               !0, ~19
         24      > JMPNZ                                                    ~20, ->2
   90    25    > > RETURN                                                   <true>
   91    26*     > RETURN                                                   null

End of function isvalid

End of class BlockChain.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
143.83 ms | 1417 KiB | 25 Q