Beispiel #1
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
Beispiel #2
0
def test_mined_block_difficulty_limits_at_1():
    last_block= Block(
        time.time(),
        'test_last_hash',
        'test_hash',
        'test_data',
        1,
        0
    )
    time.sleep(MINE_RATE / SECONDS)
    mined_block = Block.mine_block(last_block, 'hola2')
    assert mined_block.difficulty==1
Beispiel #3
0
    def is_valid_chain(chain):
        """
        Validate de incoming chain.
        Enforce the following  rules  of the blokchain:
        -the chain must start with genesis block
        -block must be formatted correctly
        """
        if chain[0] != Block.genesis():
            raise Exception('the genesis block must be invalid')

        for i in range(1, len(chain)):
            block = chain[i]
            last_block = chain[i - 1]
            Block.is_valid_block(last_block, block)
Beispiel #4
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
Beispiel #5
0
def test_is_valid_block_bad_block_hash(last_block,block):

    block.hash='00000000000000000000abbbc'

    with pytest.raises(Exception, match='The block difficulty must be incorrect'):
        Block.is_valid_block(last_block, block)
Beispiel #6
0
def test_is_valid_block_jumped_difficulty(last_block,block):
    jumped_difficulty=10
    block.difficulty=jumped_difficulty
    block.hash=f'{"0"*jumped_difficulty}111abc'
    with pytest.raises(Exception, match='The block hash must only adjust by 1'):
        Block.is_valid_block(last_block, block)
Beispiel #7
0
def test_is_valid_bad_proof_of_work(last_block,block):
    block.hash='ffff'
    with pytest.raises(Exception, match='The proof of requeriment  was not meet'):
        Block.is_valid_block(last_block, block)
Beispiel #8
0
def test_is_valid_block_bad_last_hash(last_block,block):
    block.last_hash='bad-lash-hash'
    with pytest.raises(Exception,match='The block last_hash must be incorrect'):
        Block.is_valid_block(last_block,block)
Beispiel #9
0
def test_is_valid_block(last_block,block):
    Block.is_valid_block(last_block,block)
Beispiel #10
0
def block(last_block):
    return Block.mine_block(last_block,'test-data')
Beispiel #11
0
def last_block():
    return Block.genesis()
Beispiel #12
0
def test_slowly_mined_block():
    last_block=Block.mine_block(Block.genesis(),'hola')
    time.sleep(MINE_RATE/SECONDS)
    mined_block=Block.mine_block(last_block,'hola2')
    assert mined_block.difficulty==last_block.difficulty-1
Beispiel #13
0
def test_quickly_mined_block():
    last_block=Block.mine_block(Block.genesis(),'hola')
    mined_block=Block.mine_block(last_block,'hola2')
    assert mined_block.difficulty==last_block.difficulty+1
Beispiel #14
0
 def add_block(self, data):
     #last_block = self.chain[-1] #el ultimo bloque
     self.chain.append(Block.mine_block(self.chain[-1], data))
Beispiel #15
0
 def __init__(self):
     self.chain = [Block.genesis()]  #agrega el bloque genesis