Esempio n. 1
0
def test_is_valid_transaction_chain():
    blockchain = Blockchain()
    blockchain_three_blocks = Blockchain()
    for i in range(3):
        blockchain_three_blocks.add_block([Transaction(Wallet(), 'recipient', i).to_json()])

    Blockchain.is_valid_transaction_chain(blockchain_three_blocks.chain)
Esempio n. 2
0
def test_replace_chain():
    blockchain = Blockchain()
    blockchain_three_blocks = Blockchain()
    for i in range(3):
        blockchain_three_blocks.add_block(i)

    blockchain.replace_chain(blockchain_three_blocks.chain)

    assert blockchain_three_blocks.chain == blockchain.chain
Esempio n. 3
0
def test_replace_chain_not_longer():
    blockchain_three_blocks = Blockchain()
    for i in range(3):
        blockchain_three_blocks.add_block(i)

    blockchain = Blockchain()
    
    with pytest.raises(Exception, match="Cannot replace. The incomming chain must be longer"):
        blockchain_three_blocks.replace_chain(blockchain.chain)
Esempio n. 4
0
def test_replace_chain_bad_chain():
    blockchain = Blockchain()
    blockchain_three_blocks = Blockchain()
    for i in range(3):
        blockchain_three_blocks.add_block(i)

    blockchain_three_blocks.chain[1].hash = 'bad-hash'
      
    with pytest.raises(Exception, match="Cannot replace. The incomming chain is invalid"):
        blockchain.replace_chain(blockchain_three_blocks.chain)
def test_replace_chain_shorter_chain(foo_chain):
    blockchain = Blockchain()

    with pytest.raises(
            Exception,
            match="The chain must be longer than the existing chain !!!"):
        foo_chain.replace_chain(blockchain.chain)
Esempio n. 6
0
def blockchain_three_blocks():
    blockchain = Blockchain()

    for i in range(3):
        blockchain.add_block([Transaction(Wallet(), "recipient", i).to_json()])

    return blockchain
Esempio n. 7
0
def test_blockchain_valid_transaction_duplicate_rewardss():
    blockchain = Blockchain()
    transaction = Transactions(Wallet(), 'recipient', 100).to_json()
    reward_transaction = Transactions.transaction_reward(Wallet()).to_json()
    blockchain.add_block([transaction, reward_transaction, reward_transaction])
    with pytest.raises(Exception, match="has multiple rewards"):
        Blockchain.is_chain_transaction_valid(blockchain.chain)
Esempio n. 8
0
def test_add_block():
    blockchain = Blockchain()
    data = '123'
    blockchain.add_block(data)

    assert isinstance(blockchain, Blockchain)
    assert blockchain.chain[-1].data == data
def blockchain():
    blockchain = Blockchain()

    for i in range(5):
        blockchain.add_block(i)

    return blockchain
def test_add_block():
    blockchain = Blockchain()
    data = "test-data"
    blockchain.add_block(data)

    # Check last block data is equal to test data
    assert blockchain.chain[-1].data == data
Esempio n. 11
0
def test_replace_chain_chain_is_invalid(blockchain_three_blocks):
    bc = Blockchain()
    blockchain_three_blocks.chain[1].hash = 'evil hash'

    with pytest.raises(Exception,
                       match='Cannot replace. The incoming chain is invalid'):
        bc.replace_chain(blockchain_three_blocks.chain)
Esempio n. 12
0
def blockchain_three_blocks():
    bc = Blockchain()

    for i in range(3):
        bc.add_block([Transaction(Wallet(), 'recipient', i).to_json()])

    return bc
def test_replace_cahin_no_longer(blockchain_three_blocks):
    blockchain = Blockchain()

    with pytest.raises(
            Exception,
            match='incoming chain must be longer than the local one'):
        blockchain_three_blocks.replace_chain(blockchain.chain)
Esempio n. 14
0
def test_chain_replacement_bad_chain(blockchain_init):
    blockchain = Blockchain()
    blockchain_init.chain[1].hash = "evil_hash"

    with pytest.raises(Exception,
                       match="Cannot replace. The incoming chain is invalid:"):
        blockchain.chain_replacement(blockchain_init.chain)
Esempio n. 15
0
def test_add_block():
    blockchain = Blockchain()
    data = "test-data"

    blockchain.add_block(data=data)

    assert blockchain.chain[-1].data == data
Esempio n. 16
0
def test_replace_chain_smaller_chain(blockchain_with_blocks):
    blockchain = Blockchain()

    with pytest.raises(
            Exception,
            match="incoming chain is not longer than the current chain"):
        blockchain_with_blocks.replace_chain(blockchain.chain)
Esempio n. 17
0
def test_clear_blockchain_transactions():
    transaction_pool = TransactionPool()
    transaction_1 = Transaction(Wallet(), 'first', 1)
    transaction_2 = Transaction(Wallet(), 'second', 2)
    transaction_3 = Transaction(Wallet(), 'three', 3)

    transaction_pool.set_transaction(transaction_1)
    transaction_pool.set_transaction(transaction_2)
    transaction_pool.set_transaction(transaction_3)

    blockchain = Blockchain()
    blockchain.add_block([
        transaction_1.to_json(),
        transaction_2.to_json(),
        transaction_3.to_json()
    ])

    assert transaction_1.id in transaction_pool.transaction_map
    assert transaction_2.id in transaction_pool.transaction_map
    assert transaction_3.id in transaction_pool.transaction_map

    transaction_pool.clear_blockchain_transactions(blockchain)

    assert not transaction_1.id in transaction_pool.transaction_map
    assert not transaction_2.id in transaction_pool.transaction_map
    assert not transaction_3.id in transaction_pool.transaction_map
Esempio n. 18
0
def test_calculate_balance():
    blockchain = Blockchain()
    wallet = Wallet()

    assert Wallet.calculate_balance(blockchain,
                                    wallet.address) == STARTING_BALANCE

    amount = 20
    transacion = Transaction(wallet, 'recipient', amount)
    blockchain.add_block([transacion.to_json()])
    assert Wallet.calculate_balance(
        blockchain, wallet.address) == STARTING_BALANCE - amount

    recieved_amount1 = 10
    recieved_transaction1 = Transaction(Wallet(), wallet.address,
                                        recieved_amount1)
    recieved_amount2 = 40
    recieved_transaction2 = Transaction(Wallet(), wallet.address,
                                        recieved_amount2)
    blockchain.add_block(
        [recieved_transaction1.to_json(),
         recieved_transaction2.to_json()])
    assert Wallet.calculate_balance(
        blockchain, wallet.address
    ) == STARTING_BALANCE - amount + recieved_amount1 + recieved_amount2
Esempio n. 19
0
def test_replace_chain_negative_small_chain(blockchain):
    blockchain.add_block('1')
    blockchain.add_block('2')
    smaller_chain = Blockchain()
    with pytest.raises(Exception,
                       match="Chain length is smaller than current chain."):
        blockchain.replace_chain(smaller_chain.chain)
Esempio n. 20
0
def test_replace_chain_negative_invalid_chain(blockchain):
    invalid_chain = Blockchain()
    invalid_chain.add_block('1')
    invalid_chain.add_block('1')
    invalid_chain.chain[0].hash = "0xabc"
    with pytest.raises(Exception, match="The incoming chain is not valid"):
        blockchain.replace_chain(invalid_chain.chain)
Esempio n. 21
0
def test_is_valid_chain():
    blockchain = Blockchain()

    for i in range(3):
        blockchain.add_block(i)

    Blockchain.is_valid_chain(blockchain.chain)
def test_wallet_balance_amount():
	wallet = Wallet()
	blockchain = Blockchain()
	amount = 34
	tr1 = Transactions(wallet, 'recp1', amount)
	blockchain.add_block([tr1.to_json()])
	assert Wallet.calculate_balance(blockchain, wallet.address) == STARTING_BALANCE - amount
def test_calculate_balance():
    blockchain = Blockchain()
    wallet = Wallet()

    assert wallet.calculate_balance(blockchain,
                                    wallet.address) == STARTING_BALANCE

    amount = 34
    transaction = Transaction(wallet, 'recipient', amount)
    blockchain.add_block([transaction.to_json()])

    assert wallet.calculate_balance(blockchain, wallet.address) ==\
         STARTING_BALANCE - amount

    received_amount_1 = 40
    received_transaction_1 = Transaction(Wallet(), wallet.address,
                                         received_amount_1)
    received_amount_2 = 110
    received_transaction_2 = Transaction(Wallet(), wallet.address,
                                         received_amount_2)

    blockchain.add_block(
        [received_transaction_1.to_json(),
         received_transaction_2.to_json()])

    assert wallet.calculate_balance(blockchain, wallet.address) ==\
         STARTING_BALANCE - amount + received_amount_1 +received_amount_2
Esempio n. 24
0
def test_replace_chain_bad_chain(blockchain_three_blocks):
    blockchain = Blockchain()
    blockchain_three_blocks.chain[1].hash = 'evil_hash'

    with pytest.raises(Exception,
                       match='Cannot replace, the incoming chain is invalid'):
        blockchain.replace_chain(blockchain_three_blocks.chain)
def test_calculate_balance():
    blockchain = Blockchain()
    wallet = Wallet()

    # NEW: new Wallet() `wallet` should have initial balance
    assert Wallet.calculate_balance(blockchain,
                                    wallet.address) == INITIAL_BALANCE

    sent_amt = 50
    outgoing_transaction = Transaction(
        wallet, "a13b2bf4", sent_amt)  # send to `wallet` from "a13b2bf4"
    blockchain.add_block([outgoing_transaction.serialize_to_json()])
    # SEND: `wallet` should have initial balance sans amt
    assert Wallet.calculate_balance(
        blockchain, wallet.address) == INITIAL_BALANCE - sent_amt

    received_amt_1 = 25
    incoming_transaction_1 = Transaction(
        Wallet(), wallet.address,
        received_amt_1)  # receive by `wallet` from new Wallet()
    received_amt_2 = 77
    incoming_transaction_2 = Transaction(Wallet(), wallet.address,
                                         received_amt_2)
    blockchain.add_block([
        incoming_transaction_1.serialize_to_json(),
        incoming_transaction_2.serialize_to_json()
    ])
    # RECEIVE: `wallet` should have initial balance plus incoming amt(s)
    assert Wallet.calculate_balance(
        blockchain, wallet.address
    ) == INITIAL_BALANCE - sent_amt + received_amt_1 + received_amt_2
Esempio n. 26
0
def test_replace_chain_invalid(populated_blockchain):
    blockchain = Blockchain()
    populated_blockchain.chain[1].hash = 'bad_hash'

    with pytest.raises(Exception,
                       match='Cannot replace! Incoming chain is not valid!'):
        blockchain.replace_chain(populated_blockchain.chain)
Esempio n. 27
0
def test_replace_chain_not_longer(blockchain_three_blocks):
    blockchain = Blockchain()

    with pytest.raises(
            Exception,
            match='Cannot replace. The incoming chain must be longer.'):
        blockchain_three_blocks.replace_chain(blockchain.chain)
def test_add_block():
    blockchain = Blockchain()
    data = 'foo'
    blockchain.add_block(data)
    block = blockchain.chain[-1]

    assert block.data == data
    assert block.last_hash == GENESIS_DATA['hash']
Esempio n. 29
0
def blockchain():
    blockchain = Blockchain()
    for i in range(3):
        miner_wallet = Wallet()
        transaction = Transactions(Wallet(), 'recipient', i)
        reward_transaction = Transactions.transaction_reward(miner_wallet)
        blockchain.add_block([transaction.to_json()])
        return blockchain
Esempio n. 30
0
def test_blockchain_instance():
    '''
        Purpose:
            Assert that the blockchain's first block is the gensis block
            instance.
    '''
    blockchain = Blockchain()
    assert(blockchain.chain[0].hash == GEN_DATA['hash'])