def testTransaction():
    senderWallet = Wallet()
    recipient = 'recipient'
    amount = 50
    transaction = Trasaction(senderWallet, recipient, amount)

    assert transaction.output[recipient] == amount
    assert transaction.output[
        senderWallet.address] == senderWallet.balance - amount

    assert 'timestamp' in transaction.input
    assert transaction.input['amount'] == senderWallet.balance
    assert transaction.input['address'] == senderWallet.address
    assert transaction.input['public_key'] == senderWallet.public_key

    assert Wallet.verify(transaction.input['public_key'], transaction.output,
                         transaction.input['signature'])
Beispiel #2
0
    def msg(self, pubnub, msgObject):
        print(f'\n-- Channel: {msgObject.channel} | Message:{msgObject.msg}')

        if msgObject.channel == Channels['BLOCK']:
            block = Block.from_json(msgObject.msg)
            potentialChain = self.blockchain.chain[:]
            potentialChain.append(block)

            try:
                self.blockchain.replaceChain(potentialChain)
                self.transactionPool.clearTransaction(self.blockchain)
                print('\n -- Succesful replacing the chain')
            except Exception as e:
                print(f'\n -- Did not replace chain:{e}')

        elif msgObject.channel == Channels['TRANSACTION']:
            transaction = Trasaction.from_json(msgObject.msg)
            self.transactionPool.setTransaction(transaction)
            print(f'\n-- Set the new transaction in the transaction pool')
def testClear():
    transactionPool = TransactionPool()
    transaction1 = Trasaction(Wallet(), 'recipient', 1)
    transaction2 = Trasaction(Wallet(), 'recipient', 2)

    transactionPool.setTransaction(transaction1)
    transactionPool.setTransaction(transaction2)

    blockchain = Blockchain()
    blockchain.addBlock([transaction1.to_json(), transaction2.to_json()])

    assert transaction1.id in transactionPool.transcationMap
    assert transaction2.id in transactionPool.transcationMap

    transactionPool.clearTransaction(blockchain)

    assert not transaction1.id in transactionPool.transcationMap
    assert not transaction2.id in transactionPool.transcationMap
def testSetTransaction():
    transactionPool = TransactionPool()
    transaction = Trasaction(Wallet(), 'recipient', 1)
    transactionPool.setTransaction(transaction)

    assert transactionPool.transcationMap[transaction.id] == transaction
Beispiel #5
0
def Blockchain3Blocks():
    blockchain = Blockchain()
    for i in range(3):
        blockchain.addBlock([Trasaction(Wallet(), 'recipient', i).to_json()])
        return blockchain
def testValidRewardTransactionExtraRecipient():
    rewardTransc = Trasaction.rewardTransaction(Wallet())
    rewardTransc.output['extraRecipient'] = 60

    with pytest.raises(Exception, match='Invalid mining reward'):
        Trasaction.isValidTransaction(rewardTransc)
def testValidRewardTransaction():
    rewardTransc = Trasaction.rewardTransaction(Wallet())
    Trasaction.isValidTransaction(rewardTransc)
def testRewardTransaction():
    minerWallet = Wallet()
    transaction = Trasaction.rewardTransaction(minerWallet)

    assert transaction.input == MINING_REWARD_INPUT
    assert transaction.output[minerWallet.address] == MINING_REWARD
def testValidTransactionWithInvalidSignature():
    transaction = Trasaction(Wallet(), 'recipient', 50)
    transaction.input['signature'] = Wallet().sign(transaction.output)

    with pytest.raises(Exception, match='Invalid signature'):
        Trasaction.isValidTransaction(transaction)
def testValidTransactionWithInvalidOutput():
    senderWallet = Wallet()
    transaction = Trasaction(senderWallet, 'recipient', 50)
    transaction.output[senderWallet.address] = 9001
    with pytest.raises(Exception, match='Invalid transaction output values'):
        Trasaction.isValidTransaction(transaction)
def testValidTransaction():
    Trasaction.isValidTransaction(Trasaction(Wallet(), 'recipient', 50))
def testTransactionUpdateBalance():
    senderWallet = Wallet()
    transc = Trasaction(senderWallet, 'recipient', 50)

    with pytest.raises(Exception, match='Amount exceeds balance'):
        transc.update(senderWallet, 'new_recipient', 9001)
def testTransactionExceedsBalance():
    with pytest.raises(Exception, match='Amount exceeds balance'):
        Trasaction(Wallet(), 'recipient', 9001)