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)
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)
def test_LockSet_isvalid(): ls = LockSet(num_eligible_votes=len(privkeys)) bh = "0" * 32 r, h = 2, 3 votes = [VoteBlock(h, r, bh) for i in range(len(privkeys))] for i, v in enumerate(votes): v.sign(privkeys[i]) ls.add(v) assert len(ls) == i + 1 if len(ls) < ls.num_eligible_votes * 2 / 3.0: assert not ls.is_valid else: assert ls.is_valid assert ls.has_quorum # same blockhash ls.check()
def test_LockSet_isvalid(): ls = LockSet(num_eligible_votes=len(privkeys)) bh = '0' * 32 r, h = 2, 3 votes = [VoteBlock(h, r, bh) for i in range(len(privkeys))] for i, v in enumerate(votes): v.sign(privkeys[i]) ls.add(v) assert len(ls) == i + 1 if len(ls) < ls.num_eligible_votes * 2 / 3.: assert not ls.is_valid else: assert ls.is_valid assert ls.has_quorum # same blockhash ls.check()
def test_LockSet_3_quorums(): ls = LockSet(3) v = VoteBlock(0, 0, "0" * 32) v.sign(privkeys[0]) ls.add(v) v = VoteNil(0, 0) v.sign(privkeys[1]) ls.add(v) assert not ls.is_valid v = VoteNil(0, 0) v.sign(privkeys[2]) ls.add(v) assert ls.is_valid assert ls.has_noquorum assert not ls.has_quorum assert not ls.has_quorum_possible assert ls.check()
def test_LockSet_3_quorums(): ls = LockSet(3) v = VoteBlock(0, 0, '0' * 32) v.sign(privkeys[0]) ls.add(v) v = VoteNil(0, 0) v.sign(privkeys[1]) ls.add(v) assert len(ls) == 2 assert not ls.is_valid v = VoteNil(0, 0) v.sign(privkeys[2]) ls.add(v) assert ls.is_valid assert ls.has_noquorum assert not ls.has_quorum assert not ls.has_quorum_possible assert ls.check()