Esempio n. 1
0
def test_LockSet_quorums():
    combinations = dict(has_quorum=[
        [1] * 7,
        [1] * 7 + [2] * 3,
        [1] * 7 + [None] * 3,
    ],
        has_noquorum=[
        [1] * 3 + [2] * 3 + [None],
        [None] * 7,
        [None] * 10,
        range(10),
        range(7)
    ],
        has_quorum_possible=[
        [1] * 4 + [None] * 3,
        [1] * 4 + [2] * 4,
        [1] * 4 + [2] * 3 + [3] * 3,
        [1] * 6 + [2]
    ])

    r, h = 1, 2
    for method, permutations in combinations.items():
        for set_ in permutations:
            assert len(set_) >= 7
            ls = LockSet(len(privkeys))
            for i, p in enumerate(set_):
                if p is not None:
                    bh = chr(p) * 32
                    v = VoteBlock(h, r, bh)
                else:
                    v = VoteNil(h, r)
                v.sign(privkeys[i])
                ls.add(v)
            assert len(ls) >= 7
            assert getattr(ls, method)
            ls.check()

            # check stable sort
            bhs = ls.blockhashes()
            if len(bhs) > 1:
                assert ishash(bhs[0][0])
                assert isinstance(bhs[0][1], int)
                if bhs[0][1] == bhs[1][1]:
                    assert bhs[0][0] > bhs[1][0]
                else:
                    assert bhs[0][1] > bhs[1][1]

            # test serialization

            s = rlp.encode(ls)
            d = rlp.decode(s, LockSet)

            assert ls == d
            assert id(ls) != id(d)
            assert getattr(ls, method) == getattr(d, method)
Esempio n. 2
0
def test_LockSet_quorums():
    combinations = dict(has_quorum=[
        [1] * 7,
        [1] * 7 + [2] * 3,
        [1] * 7 + [None] * 3,
    ],
                        has_noquorum=[[1] * 3 + [2] * 3 + [None], [None] * 7,
                                      [None] * 10,
                                      range(10),
                                      range(7)],
                        has_quorum_possible=[[1] * 4 + [None] * 3,
                                             [1] * 4 + [2] * 4,
                                             [1] * 4 + [2] * 3 + [3] * 3,
                                             [1] * 6 + [2]])

    r, h = 1, 2
    for method, permutations in combinations.items():
        for set_ in permutations:
            assert len(set_) >= 7
            ls = LockSet(len(privkeys))
            for i, p in enumerate(set_):
                if p is not None:
                    bh = chr(p) * 32
                    v = VoteBlock(h, r, bh)
                else:
                    v = VoteNil(h, r)
                v.sign(privkeys[i])
                ls.add(v)
            assert len(ls) >= 7
            assert getattr(ls, method)
            ls.check()

            # check stable sort
            bhs = ls.blockhashes()
            if len(bhs) > 1:
                assert ishash(bhs[0][0])
                assert isinstance(bhs[0][1], int)
                if bhs[0][1] == bhs[1][1]:
                    assert bhs[0][0] > bhs[1][0]
                else:
                    assert bhs[0][1] > bhs[1][1]

            # test serialization

            s = rlp.encode(ls)
            d = rlp.decode(s, LockSet)

            assert ls == d
            assert id(ls) != id(d)
            assert getattr(ls, method) == getattr(d, method)