Ejemplo n.º 1
0
def test_valid_reward_transaction():
    reward_transaction = Transaction.reward_transaction(Wallet())
    Transaction.is_valid_transaction(reward_transaction)
Ejemplo n.º 2
0
def test_invalid_reward_transaction_extra_recipient():
    reward_transaction = Transaction.reward_transaction(Wallet())
    reward_transaction.output['extra_recipient'] = 50

    with pytest.raises(Exception, match="Invalid mining reward"):
        Transaction.is_valid_transaction(reward_transaction)
Ejemplo n.º 3
0
def test_is_valid_transaction_chain_duplicate_transactions(blockchain_three_blocks):
    transaction = Transaction(Wallet(), 'recipient', 1).to_json()
    blockchain_three_blocks.add_block([transaction, transaction])

    with pytest.raises(Exception, match='is not unique'):
        Blockchain.is_valid_transaction_chain(blockchain_three_blocks.chain)
Ejemplo n.º 4
0
def test_valid_transaction_with_invalid_outputs():
    sender_wallet = Wallet()
    transaction = Transaction(sender_wallet, 'recipient', 50)
    transaction.output[sender_wallet.address] = 9001
    with pytest.raises(Exception, match='Invalid transaction output values'):
        Transaction.is_valid_transaction(transaction)
Ejemplo n.º 5
0
def test_reward_transaction():
    miner_wallet = Wallet()
    transaction = Transaction.reward_transaction(miner_wallet)

    assert transaction.input == MINING_REWARD_INPUT
    assert transaction.output[miner_wallet.address] == MINING_REWARD
Ejemplo n.º 6
0
def test_transaction_exceed_balance():
    with pytest.raises(Exception, match="Amount exceeds balance"):
        Transaction(Wallet(), "recipient", 9001)
def test_transaction_exceeds_balance():
    with pytest.raises(Exception, match='Amount exceeds balance'):
        Transaction(Wallet(), 'testwallet', 1039)
Ejemplo n.º 8
0
def test_valid_transaction():
    Transaction.is_valid_transaction(Transaction(Wallet(), 'recipient', 50))
Ejemplo n.º 9
0
def test_invalid_transaction_chain_duplicate_transactions(blockchain_init):
    transaction = Transaction(Wallet(), "recipient", 1).to_json()
    blockchain_init.add_block([transaction, transaction])

    with pytest.raises(Exception, match="is not unique."):
        Blockchain.is_valid_transaction_chain(blockchain_init.chain)
Ejemplo n.º 10
0
def blockchain_ten_blocks():
    blockchain = BlockChain()
    for i in range(5):
        blockchain.add_block([Transaction(Wallet(), 'abcd', i).to_json()])
    return blockchain
Ejemplo n.º 11
0
def blockchain_init():
    blockchain = Blockchain()
    for i in range(3):
        blockchain.add_block([Transaction(Wallet(), "recipient", i).to_json()])

    return blockchain
Ejemplo n.º 12
0
def test_is_valid_transaction_chain_bad_transaction(blockchain_three_blocks):
    bad_transaction = Transaction(Wallet(), 'recipient', 1)
    bad_transaction.input['signature'] = Wallet().sign(bad_transaction.output)
    blockchain_three_blocks.add_block([bad_transaction.to_json()])
    with pytest.raises(Exception):
        Blockchain.is_valid_transaction_chain(blockchain_three_blocks.chain)
Ejemplo n.º 13
0
def test_valid_transaction_with_invalid_signature():
    transaction = Transaction(Wallet(), "recipient", 50)
    transaction.input["signature"] = Wallet().sign(transaction.output)
    with pytest.raises(Exception, match="Invalid signature"):
        Transaction.is_valid_transaction(transaction)
Ejemplo n.º 14
0
def test_transaction_exceeds_balance():
    with pytest.raises(Exception, match='Amount exceeds balance'):
        Transaction(Wallet(), 'recipient', 9001)
Ejemplo n.º 15
0
def test_set_transaction():
    transaction_pool = TransactionPool()
    transaction = Transaction(Wallet(), "adc193bd", 1)
    transaction_pool.set_transaction(transaction)

    assert transaction_pool.transaction_map[transaction.id] == transaction
Ejemplo n.º 16
0
def test_transaction_update_exceeds_balance():
    sender_wallet = Wallet()
    transaction = Transaction(sender_wallet, 'recipient', 50)

    with pytest.raises(Exception, match='Amount exceeds balance'):
        transaction.update(sender_wallet, 'new_recipient', 9001)
Ejemplo n.º 17
0
def test_set_transaction():
    transaction_pool = TransactionPool()
    transaction = Transaction(Wallet(), 'recipient', 1)
    transaction_pool.set_transaction(transaction)

    assert transaction_pool.transaction_map[transaction.id] == transaction
Ejemplo n.º 18
0
def test_valid_transaction_with_invalid_signature():
    transaction = Transaction(Wallet(), 'recipient', 50)
    transaction.input['signature'] = Wallet().sign(transaction.output)

    with pytest.raises(Exception, match='Invalid signature'):
        Transaction.is_valid_transaction(transaction)
Ejemplo n.º 19
0
def blockchain_three_blocks():
    blockchain = Blockchain()
    for i in range(3):
        blockchain.add_block([Transaction(Wallet(), 'recipient', i).to_json()])
    return blockchain
Ejemplo n.º 20
0
    return jsonify(list(know_addresses))


PORT = config('PORT')
url = config('API_URL')
api_url = f'{url}:{PORT}'

if os.environ.get('PEER') == 'True':
    PORT = random.randint(5001, 6000)

    result = requests.get(f'{api_url}/blockchain')

    result_blockchain = Blockchain.from_json(result.json())

    try:
        blockchain.replace_chain(result_blockchain.chain)
        print('\n -- Successfully synchronized the local chain')
    except Exception as e:
        print(f'\n -- Error synchronizing: {e}')

if os.environ.get('SEED_DATA') == 'True':
    for i in range(15):
        blockchain.add_block([
            Transaction(Wallet(),
                        Wallet().address, random.randint(2, 50)).to_json(),
            Transaction(Wallet(),
                        Wallet().address, random.randint(2, 50)).to_json()
        ])

app.run(port=PORT)
Ejemplo n.º 21
0
def test_transaction_exceeds_balance():
    with pytest.raises(Exception):
        Transaction(Wallet(), 'recipient', 9001)