<commit_before><?php

namespace BitWasp\Bitcoin\Tests\Serializer\Block;

use BitWasp\Bitcoin\Math\Math;
use BitWasp\Bitcoin\Network\NetworkFactory;
use BitWasp\Bitcoin\Tests\AbstractTestCase;
use BitWasp\Buffertools\Parser;
use BitWasp\Buffertools\Buffer;
use BitWasp\Bitcoin\Serializer\Block\BlockHeaderSerializer;
use BitWasp\Bitcoin\Serializer\Transaction\TransactionSerializer;
use BitWasp\Bitcoin\Serializer\Block\BlockSerializer;
use BitWasp\Bitcoin\Serializer\Block\BitcoindBlockSerializer;

class BitcoindBlockSerializerTest extends AbstractTestCase
{
    public function testGenesis()
    {
        $math = new Math();
        $bhs = new BlockHeaderSerializer();
        $txs = new TransactionSerializer();
        $bs = new BlockSerializer($math, $bhs, $txs);

        $network = NetworkFactory::bitcoin();
        $bds = new BitcoindBlockSerializer($network, $bs);

        $buffer = new Buffer($this->dataFile('genesis.dat'));
        $parser = new Parser($buffer);

        $block = $bds->fromParser($parser);

        $this->assertEquals('000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f', $block->getHeader()->getHash()->getHex());
    }

    public function testParseSerialize()
    {
        $math = new Math();
        $bhs = new BlockHeaderSerializer();
        $txs = new TransactionSerializer();
        $bs = new BlockSerializer($math, $bhs, $txs);

        $network = NetworkFactory::bitcoin();
        $bds = new BitcoindBlockSerializer($network, $bs);

        $buffer = new Buffer($this->dataFile('genesis.dat'));
        $parser = new Parser($buffer);

        $block = $bds->fromParser($parser);

        $this->assertEquals($buffer->getBinary(), $bds->serialize($block)->getBinary());
    }
}<commit_msg>Add test for whenever network bytes don't match<commit_after><?php

namespace BitWasp\Bitcoin\Tests\Serializer\Block;

use BitWasp\Bitcoin\Math\Math;
use BitWasp\Bitcoin\Network\NetworkFactory;
use BitWasp\Bitcoin\Tests\AbstractTestCase;
use BitWasp\Buffertools\Parser;
use BitWasp\Buffertools\Buffer;
use BitWasp\Bitcoin\Serializer\Block\BlockHeaderSerializer;
use BitWasp\Bitcoin\Serializer\Transaction\TransactionSerializer;
use BitWasp\Bitcoin\Serializer\Block\BlockSerializer;
use BitWasp\Bitcoin\Serializer\Block\BitcoindBlockSerializer;

class BitcoindBlockSerializerTest extends AbstractTestCase
{
    public function testGenesis()
    {
        $math = new Math();
        $bhs = new BlockHeaderSerializer();
        $txs = new TransactionSerializer();
        $bs = new BlockSerializer($math, $bhs, $txs);

        $network = NetworkFactory::bitcoin();
        $bds = new BitcoindBlockSerializer($network, $bs);

        $buffer = new Buffer($this->dataFile('genesis.dat'));
        $parser = new Parser($buffer);

        $block = $bds->fromParser($parser);

        $this->assertEquals('000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f', $block->getHeader()->getHash()->getHex());
    }

    /**
     * @expectedException \RuntimeException
     */
    public function testWithInvalidNetBytes()
    {
        $math = new Math();
        $bhs = new BlockHeaderSerializer();
        $txs = new TransactionSerializer();
        $bs = new BlockSerializer($math, $bhs, $txs);

        $network = NetworkFactory::bitcoin();
        $bds = new BitcoindBlockSerializer($network, $bs);

        $buffer = new Buffer('\x00\x00\x00\x00'.substr($this->dataFile('genesis.dat'), 4));
        //echo $buffer->getHex() . "\n";
        $parser = new Parser($buffer);

        $block = $bds->fromParser($parser);

        $this->assertEquals('000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f', $block->getHeader()->getHash()->getHex());
    }


    public function testParseSerialize()
    {
        $math = new Math();
        $bhs = new BlockHeaderSerializer();
        $txs = new TransactionSerializer();
        $bs = new BlockSerializer($math, $bhs, $txs);

        $network = NetworkFactory::bitcoin();
        $bds = new BitcoindBlockSerializer($network, $bs);

        $buffer = new Buffer($this->dataFile('genesis.dat'));
        $parser = new Parser($buffer);

        $block = $bds->fromParser($parser);

        $this->assertEquals($buffer->getBinary(), $bds->serialize($block)->getBinary());
    }
}
{'fraction_non_alphanumeric': 0.14022398506766215, 'fraction_numerical': 0.03359776014932338, 'mean_word_length': 3.680131004366812, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 3, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3951545', 'n_tokens_mistral': 1388, 'n_tokens_neox': 1285, 'n_words': 228}