Esempio n. 1
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
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_genesis():
    genesis = Block.genesis()

    assert isinstance(genesis, Block)

    for key, value in GENESIS_DATA.items():
        getattr(genesis, key) == value
Esempio n. 4
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. 5
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. 6
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. 7
0
def test_genesis():
    genesis = Block.genesis()

    assert isinstance(genesis, Block)
    # every attirubte matches attribute in genesis block dictionary
    for key, value in GENESIS_DATA.items():
        getattr(genesis, key) == value
Esempio n. 8
0
def test_genesis():
    genesis = Block.genesis()

    # function returns True if the specified object is of the specified type
    assert isinstance(genesis, Block)
    for key, value in GENESIS_DATA.items():
        getattr(genesis, key) == value
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. 10
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. 11
0
def test_genesis():
    genesis = Block.genesis()

    assert isinstance(genesis, Block)

    # Loop through the const genesis, and compare genesis data with the const data
    for key, value in GENESIS_DATA.items():
        getattr(genesis, key) == value
Esempio n. 12
0
 def is_valid_chain(chain):
     if chain[0] != Block.genesis():
         raise Exception('The genesis block must be valid')
     for i in range(1, len(chain)):
         block = chain[i]
         last_block = chain[i - 1]
         Block.is_valid_block(last_block, block)
     Blockchain.is_valid_transaction_chain(chain)
Esempio n. 13
0
def test_genesis():
    genesis = Block.genesis()

    assert isinstance(genesis, Block)
    assert genesis.timestamp == GENESIS_DATA['timestamp']
    assert genesis.last_hash == GENESIS_DATA['last_hash']
    assert genesis.hash == GENESIS_DATA['hash']
    assert genesis.data == GENESIS_DATA['data']
Esempio n. 14
0
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))
Esempio n. 15
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
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
Esempio n. 17
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. 18
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. 19
0
def test_genesis():
    genesis = Block.genesis()
    assert isinstance(genesis, Block)
    assert genesis.timestamp == GENESIS_DATA['timestamp']
    assert genesis.last_hash == GENESIS_DATA['last_hash']
    assert genesis.hash == GENESIS_DATA['hash']
    assert genesis.data == GENESIS_DATA['data']
    for key, value in GENESIS_DATA.items():
        getattr(genesis, key) == value
Esempio n. 20
0
def test_mine():
    previous_block = Block.genesis()
    data = "test data"
    block = Block.mine(previous_block, data)

    assert isinstance(block, Block)
    assert block.data == data
    assert hex_to_binary(
        block.hash)[0:block.difficulty] == "0" * block.difficulty
Esempio n. 21
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. 22
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. 23
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. 24
0
def test_mine_block():
    # Set up a block
    last_block = Block.genesis()
    data = "test-data"
    block = Block.mine_block(last_block, data)

    # Confirm block is instance of Block, has correct data, and last_lash
    assert isinstance(block, Block)
    assert block.data == data
    assert block.last_hash == last_block.hash
Esempio n. 25
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. 26
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
Esempio n. 27
0
def test_mine_block():
    last_block = Block.genesis()
    data = 'test data'
    block = Block.mine_block(last_block, data)

    # First argument is the object that you want to test, second is the class that you want to match object.
    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_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. 30
0
def test_genesis():
    genesis = Block.genesis()

    assert isinstance(genesis, Block)
    # assert genesis.timestamp == GENESIS_DATA['timestamp']
    # assert genesis.last_hash == GENESIS_DATA['last_hash']
    # assert genesis.hash == GENESIS_DATA['hash']
    # assert genesis.data == GENESIS_DATA['data']

    for attr_name, attr_value in GENESIS_DATA.items():
        assert getattr(genesis, attr_name) == attr_value