示例#1
0
def test_BlockProposal():
    ls = LockSet()
    for i in range(10):
        s = Signature(i, 2, 3)
        ls.add(Locked(s, '0' * 32))

    bls = LockSet()
    for i in range(10):
        s = Signature(i, 1, 2)
        bls.add(Locked(s, '0' * 32))

    assert len(ls) == 10
    assert ls.has_quorum
    block = Block(1, 2, 4, '0' * 32, bls)
    p = BlockProposal(Signature(1, 2, 4), ls, block)
    p2 = BlockProposal(Signature(1, 2, 4), ls, block)
    assert p == p2
示例#2
0
 def mk_ls(h, r):
     ls = LockSet()
     votes = [
         Locked(Signature(i, h, r), '0' * 32)
         for i in range(ls.eligible_votes)
     ]
     for i, v in enumerate(votes):
         ls.add(v)
     return ls
示例#3
0
 def __init__(self, heightmanager, round=0):
     self.hm = heightmanager
     self.cm = heightmanager.cm
     self.log = self.hm.log
     assert isinstance(round, int)
     self.round = round
     self.height = heightmanager.height
     self.lockset = LockSet()
     self.proposal = None
     self.lock = None
     self.timeout_time = None
     print('A:%d Created RoundManager H:%d R:%d' %
           (self.cm.coinbase, self.hm.height, self.round))
示例#4
0
def test_LockSet_isvalid():
    ls = LockSet()
    votes = [
        Locked(Signature(i, 2, 3), '0' * 32) for i in range(ls.eligible_votes)
    ]
    for i, v in enumerate(votes):
        ls.add(v)
        assert len(ls) == i + 1
        if len(ls) < ls.eligible_votes * 2 / 3.:
            assert not ls.is_valid
        else:
            assert ls.is_valid
            assert ls.has_quorum  # same blockhash
示例#5
0
def test_LockSet():
    ls = LockSet()
    assert not ls
    assert len(ls) == 0
    v1 = Locked(Signature(1, 2, 3), '0' * 32)
    v2 = Locked(Signature(1, 2, 3), '0' * 32)
    ls.add(v1)
    assert ls
    assert len(ls) == 1
    lsh = ls.hash
    ls.add(v1)
    assert lsh == ls.hash
    assert len(ls) == 1
    ls.add(v2)
    assert lsh == ls.hash
    assert len(ls) == 1
示例#6
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
    hash_ = lambda v: sha3(str(v))

    for method, permutations in combinations.items():
        for set_ in permutations:
            assert len(set_) >= 7
            ls = LockSet()
            for address, p in enumerate(set_):
                if p is not None:
                    ls.add(Locked(Signature(address, h, r), hash_(p)))
                else:
                    ls.add(NotLocked(Signature(address, h, r)))
            assert len(ls) >= 7
            assert getattr(ls, method)

            # 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]