Example #1
0
def test_dag_conflicts():
    dag = DAG()
    client = MessageClient(light_client=True)
    recipient = Keypair()
    attacker = Keypair()
    entries = 5
    messages = []
    conflicting_messages = []

    for _ in range(entries):
        msg = client.generate_txn_object(recipient.address, 100)
        dag.receive_transaction(msg)
        with pytest.raises(ValueError):
            dag.conflicts.set_preferred(msg.hash)
        messages.append(msg)

    for msg in messages:
        conflict_msg = client.generate_conflicting_txn(msg, attacker.address,
                                                       100)
        dag.receive_transaction(conflict_msg)
        dag.conflicts.set_preferred(msg.hash)
        conflicting_messages.append(conflict_msg)

    conflict_sets = zip(messages, conflicting_messages)

    assert len(dag.conflicts.conflicts) == len(list(conflict_sets))
    assert len(dag.transactions) == len(messages) + len(conflicting_messages)
    txn_hashes = list(dag.transactions.keys())

    for msg, conflict_msg in conflict_sets:
        assert msg.hash in txn_hashes
        assert conflict_msg.hash in txn_hashes
        assert dag.conflicts.is_preferred(msg.hash)
        assert dag.conflicts.get_preferred(msg.hash) == msg.hash
        assert not dag.conflicts.is_preferred(conflict_msg.hash)
Example #2
0
def test_dag_progeny_has_conflict():
    dag = DAG()
    client = MessageClient(light_client=True)
    recipient = Keypair()
    attacker = Keypair()
    entries = 5
    messages = []
    preferred_txns = []

    # first 5 do not have conflicts
    for _ in range(entries):
        msg = client.generate_txn_object(recipient.address, 100)
        dag.receive_transaction(msg)
        assert not dag.progeny_has_conflict(msg)

    for _ in range(entries):
        msg = client.generate_txn_object(recipient.address, 100)
        dag.receive_transaction(msg)
        messages.append(msg)

    for msg in messages:
        conflict_msg = client.generate_conflicting_txn(msg, attacker.address,
                                                       100)
        dag.receive_transaction(conflict_msg)

        for parent in dag.transactions[conflict_msg.hash].parents:
            assert dag.progeny_has_conflict(dag.transactions[parent])
Example #3
0
def test_dag_strongly_preferred():
    dag = DAG()
    client = MessageClient(light_client=True)
    recipient = Keypair()
    attacker = Keypair()
    entries = 5
    messages = []
    conflicting_messages = []

    for _ in range(entries):
        msg = client.generate_txn_object(recipient.address, 100)
        dag.receive_transaction(msg)
        messages.append(msg)

    for msg in messages:
        conflict_msg = client.generate_conflicting_txn(msg, attacker.address,
                                                       100)
        dag.receive_transaction(conflict_msg)
        dag.conflicts.set_preferred(msg.hash)

    all_messages = zip(messages, conflicting_messages)

    for msg, conflict_msg in all_messages:
        assert dag.is_strongly_preferred(msg)
        assert not dag.is_strongly_preferred(conflict_msg)
def test_median_acceptance():
    client = MessageClient(light_client=True)
    recipient = Keypair()
    client.start_collect_metrics()

    entries = 5
    messages = [
        client.generate_txn_object(recipient.address, 100)
        for _ in range(entries)
    ]

    for msg in messages:
        client.receive_transaction(msg)

    # transactions not accepted yet
    median, total = client.calculate_median_acceptance_times()
    assert median == 0
    assert total == 0

    for msg in messages:
        client.dag.transactions[msg.hash].accepted = True
        client.txn_accepted_times[msg.hash] = time.time()

    median, total = client.calculate_median_acceptance_times()
    assert median > 0
    assert total == entries
Example #5
0
def test_dag_confidence():
    dag = DAG()
    client = MessageClient(light_client=True)
    recipient = Keypair()
    entries = 5
    for _ in range(entries):
        msg = client.generate_txn_object(recipient.address, 100)
        dag.receive_transaction(msg)
        dag.update_chit(msg.hash, True)

    for txn_hash in dag.transactions:
        txn = dag.transactions[txn_hash]
        assert dag.confidence(txn) == confidence(len(txn.children))
Example #6
0
def test_dag_is_accepted():
    dag = DAG()
    client = MessageClient(light_client=True)
    recipient = Keypair()
    entries = 10
    messages = []

    for _ in range(entries):
        msg = client.generate_txn_object(recipient.address, 100)
        dag.receive_transaction(msg)
        dag.transactions[msg.hash].chit = True
        dag.transactions[msg.hash].queried = True
        messages.append(msg)

    for msg in messages:
        if dag.confidence(msg) > params.BETA_CONFIDENCE_PARAM:
            assert dag.update_accepted(msg)
            assert dag.transactions[msg.hash].accepted
Example #7
0
def test_dag_check_conflict():
    dag = DAG()
    client = MessageClient(light_client=True)
    recipient = Keypair()
    attacker = Keypair()
    entries = 5
    messages = []

    for _ in range(entries):
        msg = client.generate_txn_object(recipient.address, 100)
        dag.receive_transaction(msg)
        messages.append(msg)

    for msg in messages:
        conflict_msg = client.generate_conflicting_txn(msg, attacker.address,
                                                       100)
        dag.check_for_conflict(conflict_msg)
        assert len(dag.conflicts.get_conflict(conflict_msg.hash)) == 2
        assert len(dag.conflicts.get_conflict(msg.hash)) == 2
Example #8
0
def test_dag_decide_preferrence():
    dag = DAG()
    client = MessageClient(light_client=True)
    recipient = Keypair()
    attacker = Keypair()
    entries = 5
    messages = []
    preferred_txns = []

    for _ in range(entries):
        msg = client.generate_txn_object(recipient.address, 100)
        dag.receive_transaction(msg)
        messages.append(msg)

    for msg in messages:
        conflict_msg = client.generate_conflicting_txn(msg, attacker.address,
                                                       100)
        dag.receive_transaction(conflict_msg)
        preferred = dag.decide_on_preference(conflict_msg.hash)
        assert dag.conflicts.get_preferred(msg.hash) == preferred
        assert dag.conflicts.is_preferred(preferred)
def test_bootstrap_state():
    client = MessageClient(light_client=True)
    recipient = Keypair()
    attacker = Keypair()
    entries = 5

    messages = [
        client.generate_txn_object(recipient.address, 100)
        for _ in range(entries)
    ]
    conflict_messages = [
        client.generate_conflicting_txn(msg, attacker.address, 100)
        for msg in messages
    ]
    all_messages = messages + conflict_messages
    conflict_sets = zip(messages, conflict_messages)
    conflict_sets = [(m.hash, c.hash) for m, c in conflict_sets]

    sync_graph_msg = messages_pb2.SyncGraph()
    sync_graph_msg.transactions.extend(all_messages)

    for conflict_set in conflict_sets:
        conflict_msg = messages_pb2.ConflictSet()
        conflict_msg.hashes.extend(conflict_set)
        sync_graph_msg.conflicts.append(conflict_msg)

    client.bootstrap_graph(sync_graph_msg)

    assert len(client.dag.transactions) == len(all_messages)
    for msg in all_messages:
        assert msg.hash in client.dag.transactions
        assert client.dag.transactions.get(msg.hash) == msg

    for conflict_set in conflict_sets:
        for txn_hash in conflict_set:
            assert client.dag.conflicts.get_conflict(txn_hash)
            assert client.dag.conflicts.get_preferred(txn_hash)