Exemplo n.º 1
0
    def test_get_transaction(self):
        tx = Transaction()
        input = TransactionInput()
        input.address = ""
        tx.add_input(input)
        tx.generate_hash()

        mp = Mempool()
        mp.flush_mempool()
        mp.add_transaction(tx)
        self.assertEqual(mp.get_transaction().to_json(), tx.to_json())
        mp.close()
Exemplo n.º 2
0
    def test_remove_transaction(self):
        tx = Transaction()
        input = TransactionInput()
        input.address = ""
        tx.add_input(input)
        tx.generate_hash()

        mp = Mempool()
        mp.flush_mempool()
        mp.add_transaction(tx)
        self.assertEqual(mp.get_size(), 1)
        mp.remove_transaction(tx.hash)
        self.assertEqual(mp.get_size(), 0)
        mp.close()
Exemplo n.º 3
0
    def test_merkle_root(self):
        block = Block()
        tx = Transaction()
        input = TransactionInput()
        input.address = "test_address"
        tx.add_input(input)
        tx.generate_hash()
        block.add_transaction(tx)
        block.calculate_merkle_root()
        initial_merkle_root = block.merkle_root

        tx1 = Transaction()
        input1 = TransactionInput()
        input1.address = "another_test_address"
        tx1.add_input(input1)
        tx1.generate_hash()
        block.add_transaction(tx1)
        block.calculate_merkle_root()
        final_merkle_root = block.merkle_root

        self.assertNotEqual(initial_merkle_root, final_merkle_root)
Exemplo n.º 4
0
    def test_txs_by_address(self):
        blockchain = BlockChain()
        blockchain.flush_chain()
        block = Block()
        tx = Transaction()
        input1 = TransactionInput()
        input1.address = "test_address"
        tx.add_input(input1)
        tx.generate_hash()
        block.add_transaction(tx)

        tx1 = Transaction()
        output = TransactionOutput()
        output.address = "fake_address"
        output1 = TransactionOutput()
        output1.address = "test_address"
        tx1.add_output(output)
        tx1.add_output(output1)
        tx1.generate_hash()
        block.add_transaction(tx1)

        tx2 = Transaction()
        output2 = TransactionOutput()
        output2.address = "fake_address"
        tx2.add_output(output2)
        tx2.generate_hash()

        block.add_transaction(tx2)
        block.compute_hash()

        blockchain.add_block(block)
        txs = blockchain.get_txs_by_addr("test_address")
        getb = blockchain.get_block(-1)
        print(getb.to_json())
        for tx in txs:
            print(tx.hash, end='\n')

        # self.assertEqual(len(txs), 2)
        print(len(txs))
        blockchain.close()