Esempio n. 1
0
def test_is_block_valid():
    last_block = Block.genesis_block()
    block = Block.mine_block(last_block, "dummy data")
    Block.is_block_valid(last_block, block)

    # bad last hash check
    block = Block.mine_block(last_block, "dummy data")
    block.last_hash = "evil_last_hash"
    with pytest.raises(
            Exception,
            match=
            "The last block hash and the last hash of the new block should match"
    ):
        Block.is_block_valid(last_block, block)

    # bad proof of work
    block = Block.mine_block(last_block, "dummy data")
    block.hash = "aaaaa"
    with pytest.raises(Exception, match="Proof of work requirement not met"):
        Block.is_block_valid(last_block, block)

    # difficulty tampered
    difficulty = 10
    block = Block.mine_block(last_block, "dummy data")
    block.difficulty = difficulty
    block.hash = f"{'0' * difficulty}1111a"

    with pytest.raises(Exception,
                       match="The block difficulty must only adjust by 1"):
        Block.is_block_valid(last_block, block)
Esempio n. 2
0
def test_slowly_mined_block():
    last_block = Block.mine_block(Block.genesis(), 'foo')
    # Suspend execution of the calling thread for the given number of seconds.
    time.sleep(MINE_RATE / SECONDS)
    mined_block = Block.mine_block(last_block, 'bar')

    assert mined_block.difficulty == last_block.difficulty - 1
Esempio n. 3
0
def test_slowly_mined_block():
    last_block = Block.mine_block(Block.genesis(), 'foo')
    time.sleep(MINE_RATE / SECONDS)
    mined_block  = Block.mine_block(last_block, 'bar')
    
    
    assert mined_block.difficulty == last_block.difficulty - 1
Esempio n. 4
0
def test_slowly_mined_block():
    last_block = Block.mine_block(Block.genesis(), 'foo')
    print(f'last_block: {last_block}')
    time.sleep(MINE_RATE_SEC)
    mined_block = Block.mine_block(last_block, 'bar')
    print(f'mined_block: {mined_block}')
    assert mined_block.difficulty == last_block.difficulty - 1
def test_slowly_mined_block():
    genesis_block = Block.genesis()
    last_block = Block.mine_block(genesis_block, 'foo')
    time.sleep(MINE_RATE / SECONDS)
    mined_block = Block.mine_block(last_block, 'bar')

    assert (mined_block.difficulty == 1) | (mined_block.difficulty
                                            == (last_block.difficulty - 1))
def test_quickly_mined_block():
    """Asumimos que el bloque será minado rápidamente, así que la 
	   dificultad debería aumentar en un nivel
	"""
    last_block = Block.mine_block(Block.genesis(), "foo")
    mined_block = Block.mine_block(last_block, "bar")

    assert mined_block.difficulty == last_block.difficulty + 1
def test_slowly_mined_block():
    last_block = Block.mine_block(Block.genesis(), 'foo')
    time.sleep(MINE_RATE / SECONDS)
    # /SECONDS as arg is in seconds and the mine rate is calculated in Nanoseconds
    # (otherwise the time would sleep for 4 billions seconds)
    mined_block = Block.mine_block(last_block, 'bar')

    assert mined_block.difficulty == last_block.difficulty - 1
Esempio n. 8
0
def test_slowly_mined_block():
    last_block = Block.mine_block(Block.genesis(), 'test')

    time.sleep(MINE_RATE / SECONDS)

    mined_block = Block.mine_block(last_block, 'test2')

    # Should be one lower due to its delay
    assert mined_block.difficulty == last_block.difficulty - 1
Esempio n. 9
0
def test_slowly_mined_block():
    # Mimic mined blocks that trigger a difficulty lower due to slow mining
    last_block = Block.mine_block(Block.genesis(), "foo")

    # Create a delay in between mining blocks
    time.sleep(DELAY)

    mined_block = Block.mine_block(last_block, "bar")

    assert mined_block.difficulty == last_block.difficulty - 1
Esempio n. 10
0
def test_quickly_mined_block():
    '''
        Purpose:
            Test that the difficulty to mine a block increases when a block is
            mined to quickly.
    '''
    last_block = Block.mine_block(Block.genesis(), 'foo')
    mined_block = Block.mine_block(last_block, 'bar')
    # Assert that difficulty INCREASES by 1 when a block is mined too quickly.
    assert (mined_block.difficulty == last_block.difficulty + 1)
Esempio n. 11
0
def test_slowly_mined_block():
    '''
        Purpose:
            Test that the difficulty to mine a block decreases when a block is
            mined too slowly.
    '''
    last_block = Block.mine_block(Block.genesis(), 'foo')
    time.sleep(MINE_RATE / SECONDS)
    mined_block = Block.mine_block(last_block, 'bar')
    # Assert that difficulty DECREASES by 1 when a block is mined too slowly.
    assert (mined_block.difficulty == last_block.difficulty - 1)
def test_slowly_mined_block():
    """Creamos un delay para que el bloque sea minado lentamente, así que la 
	   dificultad debería disminuir en un nivel, MINE_RATE está en nanosegundos,
	   por ello hay que dividirlo por la variable SEGUNDOS que son los 
	   los nanosegundos en un segundo
	"""
    last_block = Block.mine_block(Block.genesis(), "foo")
    time.sleep(MINE_RATE / SECONDS)
    mined_block = Block.mine_block(last_block, "bar")

    assert mined_block.difficulty == last_block.difficulty - 1
Esempio n. 13
0
def test_mined_block_difficulty_limits_at_1():
    last_block = Block(time.time_ns(), 'test_last_hash', 'test_hash',
                       'test_data', 1, 0)
    time.sleep(MINE_RATE / SECONDS)
    mined_block = Block.mine_block(last_block, 'bar')

    assert mined_block.difficulty == 1
Esempio n. 14
0
def test_limit_mined_block_difficulty_to_1():
    last_block = Block(time.time_ns(), "test_last_hash", "test_hash",
                       "test_data", 1, 0)
    time.sleep(MINE_RATE / SECONDS)
    mined_block = Block.mine_block(last_block, "bar")

    assert mined_block.difficulty == 1
Esempio n. 15
0
 def add_block(self, data):
     """
     Add Block to the end of the chain
     :param data:
     :return:
     """
     self.chain.append(Block.mine_block(self.chain[-1], data))
Esempio n. 16
0
def test_quickly_mined_block_difficulty_limits_at_lower_difficulty():
    last_block = Block(time.time_ns(), 'test_last_hash', 'test_hash', 'foo',
                       LOWER_DIFFICULTY, 0)
    time.sleep(MINE_RATE / SECONDS)
    mined_block = Block.mine_block(last_block, 'bar')

    assert mined_block.difficulty == LOWER_DIFFICULTY
Esempio n. 17
0
def test_mined_difficulty_limits_at_1():
    last_block = Block(time(), 'test_last_hash', 'test_hash', 'test_data', 1,
                       0)
    sleep(MINE_RATE)
    mined_block = Block.mine_block(last_block, 'bar')

    assert mined_block.difficulty == 1
Esempio n. 18
0
def test_is_valid_bad_block():
    last_block = Block.genesis()
    block = Block.mine_block(last_block, 'foo')
    block.last_hash = 'evil_lash_harsh'

    with pytest.raises(Exception, match='he block last_hash must be correct'):
        Block.is_valid_block(last_block, block)
Esempio n. 19
0
def test_is_valid_block_bad_last_hash():
    last_block = Block.genesis() 
    block = Block.mine_block(last_block, 'test')
    block.last_hash = 'bad_last_hash'

    with pytest.raises(Exception, match='last_hash must be correct'):
        Block.is_valid_block(last_block, block)
Esempio n. 20
0
def test_difficulty_not_less_one():
    last_block = Block(time.time_ns(), 'test_last_hash', 'test_hash',
                       'test_data', 1, 0)
    time.sleep(MINE_RATE / SECONDS)
    mined_block = Block.mine_block(last_block, 'new')

    assert mined_block.difficulty == 1
Esempio n. 21
0
	def add_block(self, data):
		"""Adds an instance from the Block class to
		   the attribute chain (list)
		"""
		last_block = self.chain[-1]

		self.chain.append(Block.mine_block(last_block, data))
Esempio n. 22
0
def test_mine_block():
    last_block = Block.genesis()
    data = 'test-data'
    block = Block.mine_block(last_block, data)

    assert isinstance(block, Block)
    assert block.data == data
    assert block.last_hash == last_block.hash
Esempio n. 23
0
def test_mine_block():
    last_block = Block.genesis()
    data = 'test-data'
    block = Block.mine_block(last_block, data)
    # Insure block object is an instance of the block itself
    assert isinstance(block, Block)
    assert block.data == data
    assert block.last_hash == last_block.hash
Esempio n. 24
0
def test_lowest_mining_difficulty():
    last_block = Block(time.time_ns(), 'test-last_hash', 'test-hash',
                       'test-data', 1, 0)

    time.sleep(MINE_RATE / SECONDS)
    mined_block = Block.mine_block(last_block, 'bar')

    assert mined_block.difficulty == 1  #ensure that it isnt lower than 1
Esempio n. 25
0
def test_mine_block_difficulty_limits_at_1():
    # Create a mined block with lowest possible difficulty (1)
    last_block = Block(time.time_ns(), "test_last_hash", "test_hash",
                       "test-data", 1, 0)

    time.sleep(DELAY)
    mined_block = Block.mine_block(last_block, "bar")

    assert mined_block.difficulty == 1
Esempio n. 26
0
def test_mine_block():
    gen = Block.genesis()
    data = 'test'
    block = Block.mine_block(gen,data)

    assert isinstance(block, Block)
    assert block.last_hash == gen.hash
    assert block.data == data
    assert hex_to_bin(block.hash)[0:block.difficulty] == '0'* block.difficulty
Esempio n. 27
0
def test_mine_block():
    last_block = Block.genesis()
    data = 'test-data'
    block = Block.mine_block(last_block, data)

    assert isinstance(block, Block)
    assert block.data == data
    assert block.last_hash == last_block.hash
    assert hex_to_binary(block.hash)[0:block.difficulty] == '0' * block.difficulty
Esempio n. 28
0
def test_mine_block_leading_zeros():
    # Set up a block
    last_block = Block.genesis()
    data = "test-data"
    block = Block.mine_block(last_block, data)

    # Check that the block hash has the correct number of leading zeros
    assert hex_to_binary(
        block.hash)[0:block.difficulty] == "0" * block.difficulty
Esempio n. 29
0
def test_mined_block_minimum_difficulty():
    last_block = Block(time_ns(), 'test_last_hash', 'test_hash', 'test_data',
                       1, 0)

    sleep(MINE_RATE / SECONDS)

    mined_block = Block.mine_block(last_block, 'bar')

    assert last_block.difficulty == mined_block.difficulty
Esempio n. 30
0
def test_mine_block():
    last_block = Block.genesis()
    data = 'test'
    block = Block.mine_block(last_block, data)

    assert isinstance(
        block, Block)  # it will check wheather block is object of Block class
    assert block.data == data
    assert block.last_hash == last_block.hash
    assert block.hash[0:block.difficulty] == '0' * block.difficulty