def test_process_pre_prepare_multisig(bls_bft_replicas, state_root, quorums):
    multi_sig = calculate_multi_sig_for_first(bls_bft_replicas, quorums, state_root)
    for sender_bls_bft in bls_bft_replicas:
        pre_prepare = create_pre_prepare_bls_multisig(
            bls_multi_sig=multi_sig, state_root=state_root)
        for verifier_bls_bft in bls_bft_replicas:
            verifier_bls_bft.process_pre_prepare(pre_prepare, sender_bls_bft.node_id)
def test_validate_pre_prepare_correct_multi_sig(bls_bft_replicas, state_root, quorums):
    multi_sig = calculate_multi_sig_for_first(bls_bft_replicas, quorums, state_root)
    for sender_bls_bft_replica in bls_bft_replicas:
        pre_prepare = create_pre_prepare_bls_multisig(
            bls_multi_sig=multi_sig, state_root=state_root)
        for verifier_bls_bft_replica in bls_bft_replicas:
            verifier_bls_bft_replica.validate_pre_prepare(pre_prepare,
                                                          sender_bls_bft_replica.node_id)
def test_validate_pre_prepare_incorrect_multi_sig(bls_bft_replicas, state_root, quorums):
    changed_root = generate_state_root()
    changed_multi_sig = calculate_multi_sig_for_first(bls_bft_replicas, quorums, changed_root)
    for sender_bls_bft in bls_bft_replicas:
        pre_prepare = create_pre_prepare_bls_multisig(
            bls_multi_sig=changed_multi_sig, state_root=state_root)
        for verifier_bls_bft in bls_bft_replicas:
            status = verifier_bls_bft.validate_pre_prepare(pre_prepare,
                                                           sender_bls_bft.node_id)
            assert status == BlsBftReplica.PPR_BLS_MULTISIG_WRONG
def test_multi_sig_saved_shared_with_pre_prepare(bls_bft_replicas, quorums, state_root):
    multi_sig = calculate_multi_sig_for_first(bls_bft_replicas, quorums, state_root)
    pre_prepare = create_pre_prepare_bls_multisig(
        bls_multi_sig=multi_sig, state_root=state_root)

    multi_sigs = []
    for bls_bft_replica in bls_bft_replicas:
        bls_bft_replica.process_pre_prepare(pre_prepare, bls_bft_replicas[0].node_id)
        multi_sig = bls_bft_replica._bls_bft.bls_store.get(state_root)
        assert multi_sig
        multi_sigs.append(multi_sig)

    # all saved multi-sigs are equal
    assert all(x == multi_sigs[0] for x in multi_sigs)
def test_preprepare_multisig_replaces_saved(bls_bft_replicas, quorums, state_root):
    # have locally calculated multi-sigs
    key = (0, 0)
    for sender_bls_bft_replica in bls_bft_replicas:
        commit = create_commit_bls_sig(
            sender_bls_bft_replica,
            key,
            state_root)
        for verifier_bls_bft_replica in bls_bft_replicas:
            # use 3 of 4 commits only
            if verifier_bls_bft_replica != sender_bls_bft_replica:
                verifier_bls_bft_replica.process_commit(commit,
                                                        sender_bls_bft_replica.node_id)
    process_ordered(key, bls_bft_replicas, state_root, quorums)

    # get locally calculated multi-sigs
    local_multi_sigs = {}
    for bls_bft_replica in bls_bft_replicas:
        local_multi_sigs[bls_bft_replica.node_id] = bls_bft_replica._bls_bft.bls_store.get(state_root)

    # have multi-sig for PrePrepare (make it different from the local one by using al 4 nodes)
    multi_sig = calculate_multi_sig_for_first(bls_bft_replicas, quorums, state_root)
    pre_prepare = create_pre_prepare_bls_multisig(
        bls_multi_sig=multi_sig, state_root=state_root)

    # get multi-sigs get with PrePrepare and make sure they differ from local ones
    # the local ones must be overridden
    multi_sigs = []
    for bls_bft_replica in bls_bft_replicas:
        bls_bft_replica.process_pre_prepare(pre_prepare, bls_bft_replicas[0].node_id)
        multi_sig = bls_bft_replica._bls_bft.bls_store.get(state_root)
        local_multi_sig = local_multi_sigs[bls_bft_replica.node_id]
        assert multi_sig
        assert local_multi_sig
        assert multi_sig != local_multi_sig
        multi_sigs.append(multi_sig)

    # all saved multi-sigs are equal
    assert all(x == multi_sigs[0] for x in multi_sigs)