Exemplo n.º 1
0
def test_full_message_maker(weights):
    validator_set = ValidatorSet(weights)
    msg_gen = message_maker("full")

    message_makers = msg_gen(validator_set)
    assert len(validator_set) == len(message_makers)
    assert set(validator_set.validators) == set(message_makers)
def test_no_final_message_maker(validator_set):
    msg_gen = message_maker("nofinal")

    senders = validator_set.sorted_by_name()
    receivers = senders[1:] + senders[0:1]

    for i in range(3):
        for j in range(len(validator_set)):
            index = j * 2 % len(validator_set)
            message_paths = msg_gen(validator_set)
            assert len(message_paths) == 2

            # first rr message this round
            first_message_path = message_paths[0]
            assert len(first_message_path) == 2
            assert first_message_path[0] == senders[index]
            assert first_message_path[1] == receivers[index]

            # second rr message this round
            second_message_path = message_paths[1]
            assert len(second_message_path) == 2
            assert second_message_path[0] == senders[(index + 1) %
                                                     len(validator_set)]
            assert second_message_path[1] == receivers[(index + 1) %
                                                       len(validator_set)]
Exemplo n.º 3
0
def test_full_message_maker(weights):
    validator_set = ValidatorSet(weights)
    msg_gen = message_maker("full")

    message_makers = msg_gen(validator_set)
    assert len(validator_set) == len(message_makers)
    assert set(validator_set.validators) == set(message_makers)
Exemplo n.º 4
0
def test_new_simulation_runner(generate_validator_set, protocol, mode, rounds, report_interval):
    msg_gen = utils.message_maker(mode)
    validator_set = generate_validator_set(protocol)
    network = StepNetwork(validator_set, protocol)

    simulation_runner = SimulationRunner(
        validator_set,
        msg_gen,
        protocol,
        network,
        rounds,
        report_interval,
        False,
        False
    )

    assert simulation_runner.validator_set == validator_set
    assert simulation_runner.msg_gen == msg_gen
    assert simulation_runner.round == 0
    assert isinstance(simulation_runner.network, Network)

    if rounds:
        assert simulation_runner.total_rounds == rounds
    else:
        assert simulation_runner.total_rounds == sys.maxsize

    if report_interval:
        assert simulation_runner.report_interval == report_interval
    else:
        assert simulation_runner.report_interval == 1
Exemplo n.º 5
0
def test_simulation_runner_send_messages(
        generate_validator_set,
        protocol,
        mode,
        messages_generated_per_round,
        potential_extra_messages
        ):
    msg_gen = utils.message_maker(mode)
    validator_set = generate_validator_set(protocol)
    network = StepNetwork(validator_set, protocol)

    simulation_runner = SimulationRunner(
        validator_set,
        msg_gen,
        protocol,
        network,
        100,
        20,
        False,
        False
    )

    if protocol == BlockchainProtocol:
        assert len(simulation_runner.network.global_view.justified_messages) == 1
    if protocol == BinaryProtocol:
        assert len(simulation_runner.network.global_view.justified_messages) == len(validator_set)

    initial_message_length = len(simulation_runner.network.global_view.justified_messages)
    for i in range(10):
        simulation_runner.step()
        assert len(simulation_runner.network.global_view.justified_messages) <= \
            initial_message_length + potential_extra_messages + (i+1)*messages_generated_per_round
Exemplo n.º 6
0
def test_random_message_maker(validator_set):
    msg_gen = message_maker("rand")

    for i in range(20):
        message_makers = msg_gen(validator_set)
        assert len(message_makers) == 1
        for validator in message_makers:
            assert validator in validator_set
Exemplo n.º 7
0
def test_random_message_maker(validator_set):
    msg_gen = message_maker("rand")

    for i in range(20):
        message_makers = msg_gen(validator_set)
        assert len(message_makers) == 1
        for validator in message_makers:
            assert validator in validator_set
def test_full_message_maker(weights, pairs):
    validator_set = ValidatorSet(weights)
    msg_gen = message_maker("full")

    message_paths = msg_gen(validator_set)
    for sender_name, receiver_name in pairs:
        sender = validator_set.get_validator_by_name(sender_name)
        receiver = validator_set.get_validator_by_name(receiver_name)
        assert (sender, receiver) in message_paths
Exemplo n.º 9
0
def main():
    config = default_configuration()
    parser = argparse.ArgumentParser(
        description='Run CasperCBC standard simulations.')
    parser.add_argument(
        'mode',
        metavar='Mode',
        type=str,
        choices=MESSAGE_MODES,
        help='specifies how to generate and propogate new messages')
    parser.add_argument('--protocol',
                        type=str,
                        default=config.get("DefaultProtocol"),
                        choices=PROTOCOLS,
                        help='specifies the protocol for the simulation')
    parser.add_argument(
        '--validators',
        type=int,
        default=config.getint("NumValidators"),
        help='specifies the number of validators in validator set')
    parser.add_argument(
        '--rounds',
        type=int,
        default=config.getint("NumRounds"),
        help='specifies the number of rounds to run the simulation')
    parser.add_argument(
        '--report-interval',
        type=int,
        default=config.getint("ReportInterval"),
        help='specifies the interval in rounds at which to plot results')
    parser.add_argument('--hide-display',
                        help='hide simulation display',
                        action='store_true')
    parser.add_argument('--save',
                        help='hide simulation display',
                        action='store_true')

    args = parser.parse_args()
    protocol = select_protocol(args.protocol)

    validator_set = generate_random_gaussian_validator_set(
        protocol, args.validators)

    msg_gen = message_maker(args.mode)
    display = not args.hide_display

    simulation_runner = SimulationRunner(
        validator_set,
        msg_gen,
        protocol,
        total_rounds=args.rounds,
        report_interval=args.report_interval,
        display=display,
        save=args.save,
    )
    simulation_runner.run()
Exemplo n.º 10
0
def test_round_robin_message_maker(validator_set):
    msg_gen = message_maker("rrob")

    assert msg_gen.next_sender_index == 0
    senders = validator_set.sorted_by_name()

    for i in range(3):
        for j in range(len(validator_set)):
            message_makers = msg_gen(validator_set)
            assert len(message_makers) == 1
            validator = message_makers[0]
            assert validator == senders[j]
Exemplo n.º 11
0
def test_round_robin_message_maker(validator_set):
    msg_gen = message_maker("rrob")

    assert msg_gen.next_sender_index == 0
    senders = validator_set.sorted_by_name()

    for i in range(3):
        for j in range(len(validator_set)):
            message_makers = msg_gen(validator_set)
            assert len(message_makers) == 1
            validator = message_makers[0]
            assert validator == senders[j]
def test_simulation_runner_send_messages(generate_validator_set, protocol,
                                         mode, messages_generated_per_round):
    msg_gen = utils.message_maker(mode)
    validator_set = generate_validator_set(protocol)

    simulation_runner = SimulationRunner(validator_set, msg_gen, protocol, 100,
                                         20, False, False)

    assert len(
        simulation_runner.network.global_view.messages) == len(validator_set)

    for i in range(10):
        simulation_runner.step()
        assert len(simulation_runner.network.global_view.messages) == \
            (i + 1) * messages_generated_per_round + len(validator_set)
def test_round_robin_message_maker(validator_set):
    msg_gen = message_maker("rrob")

    assert msg_gen.next_sender_index == 0
    senders = validator_set.sorted_by_name()
    receivers = senders[1:] + senders[0:1]

    for i in range(3):
        for j in range(len(validator_set)):
            message_paths = msg_gen(validator_set)
            assert len(message_paths) == 1
            message_path = message_paths[0]
            assert len(message_path) == 2
            assert message_path[0] == senders[j]
            assert message_path[1] == receivers[j]
Exemplo n.º 14
0
    def run_sim(self, sim_id):
        validator_set = self.validator_set_generator()
        runner = SimulationRunner(
            validator_set,
            message_maker(self.msg_mode),
            self.protocol,
            total_rounds=self.sim_rounds,
            report_interval=self.sim_report_interval,
            display=False,
            save=False
        )
        for interval in range(self.intervals):
            for step in range(runner.report_interval):
                runner.step()

            self._collect_data(runner, sim_id, interval)
Exemplo n.º 15
0
    def run_sim(self, sim_id):
        validator_set = self.validator_set_generator()
        network = self.network_class(validator_set, self.protocol)
        runner = SimulationRunner(
            validator_set,
            message_maker(self.msg_mode),
            self.protocol,
            network,
            total_rounds=self.sim_rounds,
            report_interval=self.sim_report_interval,
            display=False,
            save=False
        )
        for interval in range(self.intervals):
            for step in range(runner.report_interval):
                runner.step()

            self._collect_data(runner, sim_id, interval)
Exemplo n.º 16
0
def test_no_final_message_maker(validator_set):
    msg_gen = message_maker("nofinal")

    senders = validator_set.sorted_by_name()

    for i in range(3):
        for j in range(len(validator_set)):
            index = j * 2 % len(validator_set)
            message_paths = msg_gen(validator_set)
            assert len(message_paths) == 2

            # first rr message this round
            first_validator = message_paths[0]
            assert first_validator == senders[index]

            # second rr message this round
            second_validator = message_paths[1]
            assert second_validator == senders[(index + 1) % len(validator_set)]
Exemplo n.º 17
0
def test_num_messages_genesis(generate_validator_set, genesis_protocol, mode,
                              messages_generated_per_round):
    validator_set = generate_validator_set(genesis_protocol)
    network = NoDelayNetwork(validator_set, genesis_protocol)
    msg_gen = utils.message_maker(mode)
    simulation_runner = SimulationRunner(validator_set, msg_gen,
                                         BlockchainProtocol, network, 100, 20,
                                         False, False)
    analyzer = Analyzer(simulation_runner)

    assert analyzer.num_messages == 1
    potential_extra_messages = len(validator_set) - 1

    for i in range(10):
        simulation_runner.step()
        messages_generated = 1 + (i + 1) * messages_generated_per_round

        assert analyzer.num_messages <= messages_generated + potential_extra_messages
Exemplo n.º 18
0
def test_num_messages(validator_set, mode, messages_generated_per_round):
    msg_gen = utils.message_maker(mode)
    simulation_runner = SimulationRunner(
        validator_set,
        msg_gen,
        BlockchainProtocol,
        100,
        20,
        False,
        False
    )
    analyzer = Analyzer(simulation_runner)

    # due to random_initialization
    assert analyzer.num_messages() == len(validator_set)

    for i in range(10):
        simulation_runner.step()
        assert analyzer.num_messages() == \
            len(validator_set) + (i + 1) * messages_generated_per_round
Exemplo n.º 19
0
def test_num_messages_genesis(generate_validator_set, genesis_protocol, mode, messages_generated_per_round):
    validator_set = generate_validator_set(genesis_protocol)
    network = NoDelayNetwork(validator_set, genesis_protocol)
    msg_gen = utils.message_maker(mode)
    simulation_runner = SimulationRunner(
        validator_set,
        msg_gen,
        BlockchainProtocol,
        network,
        100,
        20,
        False,
        False
    )
    analyzer = Analyzer(simulation_runner)

    assert analyzer.num_messages == 1
    potential_extra_messages = len(validator_set) - 1

    for i in range(10):
        simulation_runner.step()
        messages_generated = 1 + (i + 1) * messages_generated_per_round

        assert analyzer.num_messages <= messages_generated + potential_extra_messages
Exemplo n.º 20
0
def simulation_runner(protocol, validator_set, network):
    msg_gen = utils.message_maker('rand')
    return SimulationRunner(validator_set, msg_gen, protocol, network, 20, 20,
                            False, False)
Exemplo n.º 21
0
def simulation_runner(validator_set):
    msg_gen = utils.message_maker('rand')
    return SimulationRunner(validator_set, msg_gen, BlockchainProtocol, 20, 20,
                            False, False)
Exemplo n.º 22
0
def main():
    config = default_configuration()
    parser = argparse.ArgumentParser(
        description='Run CasperCBC standard simulations.')
    parser.add_argument(
        'mode',
        metavar='Mode',
        type=str,
        choices=MESSAGE_MODES,
        help='specifies how to generate and propogate new messages')
    parser.add_argument('--protocol',
                        type=str,
                        default=config.get("DefaultProtocol"),
                        choices=PROTOCOLS,
                        help='specifies the protocol for the simulation')
    parser.add_argument('--network',
                        type=str,
                        default=config.get("DefaultNetwork"),
                        choices=NETWORKS,
                        help='specifies the network model for the simulation')
    parser.add_argument(
        '--validators',
        type=int,
        default=config.getint("NumValidators"),
        help='specifies the number of validators in validator set')
    parser.add_argument(
        '--rounds',
        type=int,
        default=config.getint("NumRounds"),
        help='specifies the number of rounds to run the simulation')
    parser.add_argument(
        '--report-interval',
        type=int,
        default=config.getint("ReportInterval"),
        help='specifies the interval in rounds at which to plot results')
    parser.add_argument('--hide-display',
                        action="store_true",
                        help='display simulations round by round')
    parser.add_argument('--save',
                        type=str2bool,
                        default=config.getboolean("Save"),
                        help='save the simulation in graphs/ directory')
    parser.add_argument(
        '--justify-messages',
        type=str2bool,
        default=config.getboolean("JustifyMessages"),
        help=
        'force full propagation of all messages in justification of message when sending'
    )

    args = parser.parse_args()
    protocol = select_protocol(args.protocol)
    network_type = select_network(args.network)

    validator_set = generate_random_gaussian_validator_set(
        protocol, args.validators)
    network = network_type(validator_set, protocol)

    msg_gen = message_maker(args.mode)

    simulation_runner = SimulationRunner(
        validator_set,
        msg_gen,
        protocol=protocol,
        network=network,
        total_rounds=args.rounds,
        report_interval=args.report_interval,
        display=(not args.hide_display),
        save=args.save,
    )
    simulation_runner.run()
Exemplo n.º 23
0
def main():
    config = default_configuration()
    parser = argparse.ArgumentParser(description='Run CasperCBC standard simulations.')
    parser.add_argument(
        'mode', metavar='Mode', type=str,
        choices=MESSAGE_MODES,
        help='specifies how to generate and propogate new messages'
    )
    parser.add_argument(
        '--protocol', type=str, default=config.get("DefaultProtocol"),
        choices=PROTOCOLS,
        help='specifies the protocol for the simulation'
    )
    parser.add_argument(
        '--network', type=str, default=config.get("DefaultNetwork"),
        choices=NETWORKS,
        help='specifies the network model for the simulation'
    )
    parser.add_argument(
        '--validators', type=int, default=config.getint("NumValidators"),
        help='specifies the number of validators in validator set'
    )
    parser.add_argument(
        '--rounds', type=int, default=config.getint("NumRounds"),
        help='specifies the number of rounds to run the simulation'
    )
    parser.add_argument(
        '--report-interval', type=int, default=config.getint("ReportInterval"),
        help='specifies the interval in rounds at which to plot results'
    )
    parser.add_argument(
        '--hide-display', action="store_true",
        help='display simulations round by round'
    )
    parser.add_argument(
        '--save', type=str2bool, default=config.getboolean("Save"),
        help='save the simulation in graphs/ directory'
    )
    parser.add_argument(
        '--justify-messages', type=str2bool, default=config.getboolean("JustifyMessages"),
        help='force full propagation of all messages in justification of message when sending'
    )

    args = parser.parse_args()
    protocol = select_protocol(args.protocol)
    network_type = select_network(args.network)

    validator_set = generate_random_gaussian_validator_set(
        protocol,
        args.validators
    )
    network = network_type(validator_set, protocol)

    msg_gen = message_maker(args.mode)

    simulation_runner = SimulationRunner(
        validator_set,
        msg_gen,
        protocol=protocol,
        network=network,
        total_rounds=args.rounds,
        report_interval=args.report_interval,
        display=(not args.hide_display),
        save=args.save,
    )
    simulation_runner.run()