<commit_before><?php
/**
 * Created by PhpStorm.
 * User: thomas
 * Date: 26/03/15
 * Time: 05:17
 */

namespace BitWasp\Bitcoin\Test\Network\Messages;

use BitWasp\Buffertools\Buffer;
use BitWasp\Bitcoin\Network\Messages\Version;
use BitWasp\Bitcoin\Network\Structure\NetworkAddress;
use BitWasp\Bitcoin\Tests\AbstractTestCase;

class VersionTest extends AbstractTestCase
{
    public function testVersion()
    {
        $v = '60002';
        $services = Buffer::hex('0000000000000001');
        $time = time();
        $recipient = new NetworkAddress(Buffer::hex('1'), '10.0.0.1', '8332');
        $sender = new NetworkAddress(Buffer::hex('1'), '10.0.0.2', '8332');
        $userAgent = "/Satoshi:0.7.2/";
        $lastBlock = 212672;
        $version = new Version(
            $v,
            $services,
            $time,
            $recipient,
            $sender,
            $userAgent,
            $lastBlock,
            true
        );

        $this->assertEquals('version', $version->getNetworkCommand());
        $this->assertEquals($userAgent, $version->getUserAgent());
        $this->assertEquals($v, $version->getVersion());
        $this->assertEquals($time, $version->getTimestamp());
        $this->assertEquals($sender, $version->getSenderAddress());
        $this->assertEquals($recipient, $version->getRecipientAddress());
        $this->assertEquals($services->getInt(), $version->getServices());
        $this->assertEquals($lastBlock, $version->getStartHeight());
        $this->assertInternalType('string', $version->getNonce());
        $this->assertTrue($version->getRelay());
    }

    /**
     * @expectedException \InvalidArgumentException
     */
    public function testVersionFails()
    {
        $v = '60002';
        $services = Buffer::hex('0000000000000001');
        $time = time();
        $recipient = new NetworkAddress(Buffer::hex('1'), '10.0.0.1', '8332');
        $sender = new NetworkAddress(Buffer::hex('1'), '10.0.0.2', '8332');
        $userAgent = "/Satoshi:0.7.2/";
        $lastBlock = 212672;
        $version = new Version(
            $v,
            $services,
            $time,
            $recipient,
            $sender,
            $userAgent,
            $lastBlock,
            1<commit_msg>fix Version tests after adding nonce (Forgot to do this before)<commit_after><?php
/**
 * Created by PhpStorm.
 * User: thomas
 * Date: 26/03/15
 * Time: 05:17
 */

namespace BitWasp\Bitcoin\Test\Network\Messages;

use BitWasp\Bitcoin\Crypto\Random\Random;
use BitWasp\Buffertools\Buffer;
use BitWasp\Bitcoin\Network\Messages\Version;
use BitWasp\Bitcoin\Network\Structure\NetworkAddress;
use BitWasp\Bitcoin\Tests\AbstractTestCase;

class VersionTest extends AbstractTestCase
{
    public function testVersion()
    {
        $v = '60002';
        $services = Buffer::hex('0000000000000001');
        $time = time();
        $recipient = new NetworkAddress(Buffer::hex('1'), '10.0.0.1', '8332');
        $sender = new NetworkAddress(Buffer::hex('1'), '10.0.0.2', '8332');
        $userAgent = new Buffer("/Satoshi:0.7.2/");
        $lastBlock = 212672;
        $random = new Random();
        $nonce = $random->bytes(8)->getInt();
        $version = new Version(
            $v,
            $services,
            $time,
            $recipient,
            $sender,
            $nonce,
            $userAgent,
            $lastBlock,
            true
        );

        $this->assertEquals('version', $version->getNetworkCommand());
        $this->assertEquals($userAgent, $version->getUserAgent());
        $this->assertEquals($v, $version->getVersion());
        $this->assertEquals($time, $version->getTimestamp());
        $this->assertEquals($sender, $version->getSenderAddress());
        $this->assertEquals($recipient, $version->getRecipientAddress());
        $this->assertEquals($services, $version->getServices());
        $this->assertEquals($lastBlock, $version->getStartHeight());
        $this->assertInternalType('string', $version->getNonce());
        $this->assertTrue($version->getRelay());
    }

    /**
     * @expectedException \InvalidArgumentException
     */
    public function testVersionFails()
    {
        $v = '60002';
        $services = Buffer::hex('0000000000000001');
        $time = time();
        $recipient = new NetworkAddress(Buffer::hex('1'), '10.0.0.1', '8332');
        $sender = new NetworkAddress(Buffer::hex('1'), '10.0.0.2', '8332');
        $userAgent = new Buffer("/Satoshi:0.7.2/");
        $lastBlock = 212672;
        $random = new Random();
        $nonce = $random->bytes(8)->getInt();
        new Version(
            $v,
            $services,
            $time,
            $recipient,
            $sender,
            $nonce,
            $userAgent,
            $lastBlock,
            1
{'fraction_non_alphanumeric': 0.16399916857202246, 'fraction_numerical': 0.0465599667428809, 'mean_word_length': 2.407932011331445, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 48, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 8, 'substrings_counts': 4, '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': '1352986', 'n_tokens_mistral': 1541, 'n_tokens_neox': 1399, 'n_words': 250}