<commit_before>#!/usr/bin/env python3
# Copyright (c) 2017 The Bitcoin Core developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
"""Test RPC calls related to net.

Tests correspond to code in rpc/net.cpp.
"""

import time
from decimal import Decimal
from itertools import product

import test_framework.messages
from test_framework.avatools import create_coinbase_stakes
from test_framework.key import ECKey
from test_framework.messages import NODE_NETWORK
from test_framework.p2p import P2PInterface
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import (
    assert_approx,
    assert_equal,
    assert_greater_than,
    assert_raises_rpc_error,
    p2p_port,
)
from test_framework.wallet_util import bytes_to_wif


def assert_net_servicesnames(servicesflag, servicenames):
    """Utility that checks if all flags are correctly decoded in
    `getpeerinfo` and `getnetworkinfo`.

    :param servicesflag: The services as an integer.
    :param servicenames: The list of decoded services names, as strings.
    """
    servicesflag_generated = 0
    for servicename in servicenames:
        servicesflag_generated |= getattr(
            test_framework.messages, 'NODE_' + servicename)
    assert servicesflag_generated == servicesflag


class NetTest(BitcoinTestFramework):
    def set_test_params(self):
        self.setup_clean_chain = True
        self.num_nodes = 2
        self.extra_args = [["-enableavalanche=1", "-minrelaytxfee=10"],
                           ["-enableavalanche=1", "-minrelaytxfee=5"]]
        self.supports_cli = False

    def run_test(self):
        # Get out of IBD for the minfeefilter and getpeerinfo tests.
        self.nodes[0].generate(101)
        # Connect nodes both ways.
        self.connect_nodes(0, 1)
        self.connect_nodes(1, 0)
        self.sync_all()

        self.test_connection_count()
        self.test_getpeerinfo()
        self.test_getnettotals()
        self.test_getnetworkinfo()
        self.test_getaddednodeinfo()
        self.test_service_flags()
        self.test_getnodeaddresses()

    def test_connection_count(self):
        self.log.info("Test getconnectioncount")
        # After using `connect_nodes` to connect nodes 0 and 1 to each other.
        assert_equal(self.nodes[0].getconnectioncount(), 2)

    def test_getnettotals(self):
        self.log.info("Test getnettotals")
        # Test getnettotals and getpeerinfo by doing a ping. The bytes
        # sent/received should increase by at least the size of one ping (32
        # bytes) and one pong (32 bytes).
        net_totals_before = self.nodes[0].getnettotals()
        peer_info_before = self.nodes[0].getpeerinfo()
        self.nodes[0].ping()
        self.wait_until(
            lambda:
                self.nodes[0].getnettotals()['totalbytessent']
                >= net_totals_before['totalbytessent'] + 32 * 2,
            timeout=1)
        self.wait_until(
            lambda:
                self.nodes[0].getnettotals()['totalbytesrecv']
                >= net_totals_before['totalbytesrecv'] + 32 * 2,
            timeout=1)

        for peer_before in peer_info_before:
            def peer_after():
                return next(
                    p for p in self.nodes[0].getpeerinfo()
                    if p['id'] == peer_before['id']
                )
            self.wait_until(
                lambda:
                    peer_after()['bytesrecv_per_msg'].get('pong', 0)
                    >= peer_before['bytesrecv_per_msg'].get('pong', 0) + 32,
                timeout=1
            )
            self.wait_until(
                lambda:
                    peer_after()['bytessent_per_msg'].get('ping', 0)
                    >= peer_before['bytessent_per_msg'].get('ping', 0) + 32,
                timeout=1)

    def test_getnetworkinfo(self):
        self.log.info("Test getnetworkinfo")
        info = self.nodes[0].getnetworkinfo()
        assert_equal(info['networkactive'], True)
        assert_equal(info['connections'], 2)
        assert_equal(info['connections_in'], 1)
        assert_equal(info['connections_out'], 1)

        with self.nodes[0].assert_debug_log(expected_msgs=['SetNetworkActive: false\n']):
            self.nodes[0].setnetworkactive(state=False)
        assert_equal(self.nodes[0].getnetworkinfo()['networkactive'], False)
        # Wait a bit for all sockets to close
        self.wait_until(lambda: self.nodes[0].getnetworkinfo()[
            'connections'] == 0, timeout=3)

        with self.nodes[0].assert_debug_log(expected_msgs=['SetNetworkActive: true\n']):
            self.nodes[0].setnetworkactive(state=True)
        # Connect nodes both ways.
        self.connect_nodes(0, 1)
        self.connect_nodes(1, 0)<commit_msg>tests: increase timeout in  getnettotals test<commit_after>#!/usr/bin/env python3
# Copyright (c) 2017 The Bitcoin Core developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
"""Test RPC calls related to net.

Tests correspond to code in rpc/net.cpp.
"""

import time
from decimal import Decimal
from itertools import product

import test_framework.messages
from test_framework.avatools import create_coinbase_stakes
from test_framework.key import ECKey
from test_framework.messages import NODE_NETWORK
from test_framework.p2p import P2PInterface
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import (
    assert_approx,
    assert_equal,
    assert_greater_than,
    assert_raises_rpc_error,
    p2p_port,
)
from test_framework.wallet_util import bytes_to_wif


def assert_net_servicesnames(servicesflag, servicenames):
    """Utility that checks if all flags are correctly decoded in
    `getpeerinfo` and `getnetworkinfo`.

    :param servicesflag: The services as an integer.
    :param servicenames: The list of decoded services names, as strings.
    """
    servicesflag_generated = 0
    for servicename in servicenames:
        servicesflag_generated |= getattr(
            test_framework.messages, 'NODE_' + servicename)
    assert servicesflag_generated == servicesflag


class NetTest(BitcoinTestFramework):
    def set_test_params(self):
        self.setup_clean_chain = True
        self.num_nodes = 2
        self.extra_args = [["-enableavalanche=1", "-minrelaytxfee=10"],
                           ["-enableavalanche=1", "-minrelaytxfee=5"]]
        self.supports_cli = False

    def run_test(self):
        # Get out of IBD for the minfeefilter and getpeerinfo tests.
        self.nodes[0].generate(101)
        # Connect nodes both ways.
        self.connect_nodes(0, 1)
        self.connect_nodes(1, 0)
        self.sync_all()

        self.test_connection_count()
        self.test_getpeerinfo()
        self.test_getnettotals()
        self.test_getnetworkinfo()
        self.test_getaddednodeinfo()
        self.test_service_flags()
        self.test_getnodeaddresses()

    def test_connection_count(self):
        self.log.info("Test getconnectioncount")
        # After using `connect_nodes` to connect nodes 0 and 1 to each other.
        assert_equal(self.nodes[0].getconnectioncount(), 2)

    def test_getnettotals(self):
        self.log.info("Test getnettotals")
        # Test getnettotals and getpeerinfo by doing a ping. The bytes
        # sent/received should increase by at least the size of one ping (32
        # bytes) and one pong (32 bytes).
        net_totals_before = self.nodes[0].getnettotals()
        peer_info_before = self.nodes[0].getpeerinfo()
        self.nodes[0].ping()
        self.wait_until(
            lambda:
                self.nodes[0].getnettotals()['totalbytessent']
                >= net_totals_before['totalbytessent'] + 32 * 2,
            timeout=10)
        self.wait_until(
            lambda:
                self.nodes[0].getnettotals()['totalbytesrecv']
                >= net_totals_before['totalbytesrecv'] + 32 * 2,
            timeout=10)

        for peer_before in peer_info_before:
            def peer_after():
                return next(
                    p for p in self.nodes[0].getpeerinfo()
                    if p['id'] == peer_before['id']
                )
            self.wait_until(
                lambda:
                    peer_after()['bytesrecv_per_msg'].get('pong', 0)
                    >= peer_before['bytesrecv_per_msg'].get('pong', 0) + 32,
                timeout=10
            )
            self.wait_until(
                lambda:
                    peer_after()['bytessent_per_msg'].get('ping', 0)
                    >= peer_before['bytessent_per_msg'].get('ping', 0) + 32,
                timeout=10)

    def test_getnetworkinfo(self):
        self.log.info("Test getnetworkinfo")
        info = self.nodes[0].getnetworkinfo()
        assert_equal(info['networkactive'], True)
        assert_equal(info['connections'], 2)
        assert_equal(info['connections_in'], 1)
        assert_equal(info['connections_out'], 1)

        with self.nodes[0].assert_debug_log(expected_msgs=['SetNetworkActive: false\n']):
            self.nodes[0].setnetworkactive(state=False)
        assert_equal(self.nodes[0].getnetworkinfo()['networkactive'], False)
        # Wait a bit for all sockets to close
        self.wait_until(lambda: self.nodes[0].getnetworkinfo()[
            'connections'] == 0, timeout=3)

        with self.nodes[0].assert_debug_log(expected_msgs=['SetNetworkActive: true\n']):
            self.nodes[0].setnetworkactive(state=True)
        # Connect nodes both ways.
        self.connect_nodes(0, 1)
        self.connect_nodes(1, 0)
{'fraction_non_alphanumeric': 0.10026628430970913, 'fraction_numerical': 0.014952888160589923, 'mean_word_length': 2.5940375414059624, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, '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': '2854536', 'n_tokens_mistral': 2975, 'n_tokens_neox': 2807, 'n_words': 634}