Exemplo n.º 1
0
 def next_block(last_block):
     # Generate all later blocks in the blockchain
     current_height = last_block.height + 1
     #current_hash = last_block.hash # ToDo: fix hash error from main_block?
     mb = MainBlock(current_height, "3", "4", [], [], [])
     mb.height = current_height
     mb.time = 1562228422767
     return mb
Exemplo n.º 2
0
    def json_string_block():
        """creates a main block for testing purposes"""
        prev_hash = "1"
        miner = "2"
        difficulty = "3"

        mb = MainBlock(prev_hash, miner, difficulty, [], [], [])
        # time is automatically set, so we need to change it for the test
        mb.time = 1562228422767
        return mb.string()
def test_pow_easy():
    # test pow with easy difficulty
    prev_hash = "0"
    miner = "abcdef"
    difficulty = "00ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
    b = MainBlock(prev_hash, miner, difficulty, [], [], [])
    b.height = 0
    p = Pow(b)
    p.start()
    p.join()
    block = p.get_mined_block()
    assert block.hash().startswith("00")
Exemplo n.º 4
0
def test_main_from_json_with_vote_list():
    prev_hash = "1"
    miner = "2"
    difficulty = "3"

    mb = MainBlock(prev_hash, miner, difficulty, get_vote_list(10), [], [])
    mb_copy = main_block.from_json(mb.string(True, False, False))

    assert mb.string() == mb_copy.string()
    assert mb.hash() == mb_copy.hash()
    assert mb.vote_count == 10
    assert mb_copy.vote_count == 10
def test_remove_non_existing_block():
    p = BlockPool()
    mb1 = MainBlock("0", "1", "2", [], [], [])

    with pytest.raises(ValueError):
        p.remove(mb1)

    mb2 = MainBlock("3", "4", "5", [], [], [])
    p.add(mb2)

    with pytest.raises(ValueError):
        p.remove(mb1)
def test_pow_interrupt():
    # test pow with hard difficulty and interrupt the thread
    prev_hash = "0"
    miner = "abcdef"
    difficulty = "00000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
    b = MainBlock(prev_hash, miner, difficulty, [], [], [])
    p = Pow(b)
    p.start()

    with pytest.raises(Exception):
        p.get_mined_block()

    # assert that the thread is still running
    assert p.isAlive()
    assert not p.isInterrupted()

    # stop  the thread and expect a warning that mining was not finished
    with pytest.raises(Exception):
        p.interrupt()

    assert p.isInterrupted()
    # w8 100 ms (10 should be sufficciant, but we want to be sure)
    # for the thread to stop
    # normally one would use p.join(), but the test ensures that it dosent
    # take ages to interrupt the mining
    time.sleep(100 / 1000)
    assert not p.isAlive()
def test_pool():
    p = BlockPool()

    assert p.get_current_height() == 0
    assert not p.get_block_dict()

    mb = MainBlock("0", "1", "2", [], [], [])

    p.add(mb)

    assert p.get_block_dict()[mb.height][0] == mb
Exemplo n.º 8
0
def test_main_block():
    prev_hash = "1"
    miner = "2"
    difficulty = "3"

    mb = MainBlock(prev_hash, miner, difficulty, [], [], [])
    # time is automatically set, so we need to change it for the test
    mb.time = 1562228422767

    assert mb.string(
    ) == '{"prev_hash": "1", "height": 0, "time": 1562228422767, "miner": "2", "difficulty": "3", "nonce": "0000000000000000", "vote_merkle_root": "f1534392279bddbf9d43dde8701cb5be14b82f76ec6607bf8d6ad557f60f304e", "vote_count": 0, "shard_merkle_root": "f1534392279bddbf9d43dde8701cb5be14b82f76ec6607bf8d6ad557f60f304e", "shard_count": 0, "vtx_merkle_root": "f1534392279bddbf9d43dde8701cb5be14b82f76ec6607bf8d6ad557f60f304e", "vtx_count": 0, "next_shard_producers": []}'
    assert str(mb) == mb.string()
    assert mb.hash(
    ) == "8becb0feaf4b853fd84b38ddf3098a14d2f08d890cb7ecbdfdbc18dff9a72bd5"
    assert mb.get_vote_root() == stringutil.empty_root
    assert mb.get_shard_root() == stringutil.empty_root
    assert mb.get_vtx_root() == stringutil.empty_root
Exemplo n.º 9
0
def test_main_from_json():
    prev_hash = "1"
    miner = "2"
    difficulty = "3"

    mb = MainBlock(prev_hash, miner, difficulty, [], [], [])
    mb.height = 1  # make sure height is copied
    time.sleep(1 / 1000)  # make sure time is copied
    mb.nonce = "abcdabcdabcdabcd"  # make sure nonce is copied
    # make sure shard producers are copied
    mb.next_shard_producers = ["a", "b", "c"]
    mb_copy = main_block.from_json(mb.string())

    assert mb.string() == mb_copy.string()
    assert mb.hash() == mb_copy.hash()
Exemplo n.º 10
0
def test_main_from_json_with_all_lists():
    prev_hash = "1"
    miner = "2"
    difficulty = "3"

    mb = MainBlock(prev_hash, miner, difficulty, get_vote_list(10),
                   get_shard_list(10), get_vtx_list(10))
    mb_copy = main_block.from_json(mb.string(True, True, True))

    assert mb.string() == mb_copy.string()
    assert mb.string(True, True, True) == mb_copy.string(True, True, True)
    assert mb.hash() == mb_copy.hash()
def test_current_height():
    p = BlockPool()
    mb1 = MainBlock("0", "1", "2", [], [], [])
    mb2 = MainBlock("3", "4", "5", [], [], [])
    mb1.height = 100
    mb2.height = 101

    p.add(mb1)

    assert p.get_current_height() == 100

    p.add(mb2)

    assert p.get_current_height() == 101
def test_add_and_remove_fork_block():
    height = 100
    p = BlockPool()
    mb1 = MainBlock("0", "1", "2", [], [], [])
    mb2 = MainBlock("3", "4", "5", [], [], [])
    mb1.height = height
    mb2.height = height

    p.add(mb1)
    p.add(mb2)

    assert p.get_block_dict()[height] == [mb1, mb2]

    p.remove(mb1)

    assert p.get_block_dict()[height] == [mb2]
 def handle_new_main_block(self, main_block: MainBlock):
     self.main_block_pool.add_block(main_block)
     loggerutil.debug("got new main_block: " + main_block.str())
Exemplo n.º 14
0
 def create_genesis_block():
     # manually constructs a block with height 0 and an arbitrary previous hash
     mb = MainBlock("0", "1", "3", [], [], [])
     mb.height = 0
     mb.time = 1562228422767
     return mb