Esempio n. 1
0
 public function testBlockSerializer()
 {
     $parser = new NetworkMessageSerializer(Bitcoin::getDefaultNetwork());
     $txHex = '01000000' . '01' . '0000000000000000000000000000000000000000000000000000000000000000FFFFFFFF' . '4D' . '04FFFF001D0104455468652054696D65732030332F4A616E2F32303039204368616E63656C6C6F72206F6E206272696E6B206F66207365636F6E64206261696C6F757420666F722062616E6B73' . 'FFFFFFFF' . '01' . '00F2052A01000000' . '43' . '4104678AFDB0FE5548271967F1A67130B7105CD6A828E03909A67962E0EA1F61DEB649F6BC3F4CEF38C4F35504E51EC112DE5C384DF7BA0B8D578A4C702B6BF11D5FAC' . '00000000';
     $blockHex = '01000000' . '0000000000000000000000000000000000000000000000000000000000000000' . '3BA3EDFD7A7B12B27AC72C3E67768F617FC81BC3888A51323A9FB8AA4B1E5E4A' . '29AB5F49' . 'FFFF001D' . '1DAC2B7C' . '01' . $txHex;
     $newBlock = BlockFactory::fromHex($blockHex);
     $block = new Block($newBlock);
     $serialized = $block->getNetworkMessage()->getBuffer();
     $parsed = $parser->parse($serialized)->getPayload();
     $this->assertEquals($block, $parsed);
 }
Esempio n. 2
0
 /**
  * @param PeerState $state
  * @param Peer $peer
  * @param Block $blockMsg
  */
 public function onBlock(PeerState $state, Peer $peer, Block $blockMsg)
 {
     $best = $this->node->chain();
     $headerIdx = $this->node->headers();
     $blockIndex = $this->node->blocks();
     $checkSignatures = (bool) $this->config->getItem('config', 'check_signatures', true);
     $checkSize = (bool) $this->config->getItem('config', 'check_block_size', true);
     $checkMerkleRoot = (bool) $this->config->getItem('config', 'check_merkle_root', true);
     try {
         $index = $blockIndex->accept($blockMsg->getBlock(), $best, $headerIdx, $checkSignatures, $checkSize, $checkMerkleRoot);
         $this->blockDownload->received($best, $peer, $index->getHash());
     } catch (\Exception $e) {
         $header = $blockMsg->getBlock()->getHeader();
         $this->node->emit('event', ['error.onBlock', ['ip' => $peer->getRemoteAddress()->getIp(), 'hash' => $header->getHash()->getHex(), 'error' => $e->getMessage() . PHP_EOL . $e->getTraceAsString()]]);
     }
 }
Esempio n. 3
0
 /**
  * @param Peer $peer
  * @param Block $blockMsg
  */
 public function onBlock(Peer $peer, Block $blockMsg)
 {
     $best = $this->chain();
     $block = $blockMsg->getBlock();
     try {
         $this->blocks->accept($best, $block, $this->headers, $this->utxo);
         $this->chains->checkTips();
         $this->blockDownload->received($best, $peer, $block->getHeader()->getHash());
     } catch (\Exception $e) {
         $header = $block->getHeader();
         echo 'Failed to accept block' . PHP_EOL;
         echo $e->getMessage() . PHP_EOL;
         if ($best->getChain()->containsHash(Buffer::hex($block->getHeader()->getPrevBlock()))) {
             if ($header->getPrevBlock() === $best->getLastBlock()->getHash()) {
                 echo $block->getHeader()->getHash()->getHex() . PHP_EOL;
                 echo $block->getHex() . PHP_EOL;
                 echo 'We have prevblockIndex, so this is weird.';
                 echo $e->getTraceAsString() . PHP_EOL;
                 echo $e->getMessage() . PHP_EOL;
             } else {
                 echo 'Didn\'t elongate the chain, probably from the future..' . PHP_EOL;
             }
         }
     }
 }