Esempio n. 1
0
    def test_use_pool(self):
        bc = BlockChain()
        w = Wallet()
        w.create_keys()

        # make some cash
        tx0 = Transaction("0", w.get_address(), 1)
        b = bc.new_block(tx0)
        mine(b)
        self.assertTrue(bc.discard_block(b))

        self.assertEqual(bc.get_wallet_balance(w.get_address()), 1)
        tx_count = 10
        for i in range(tx_count):
            t = Transaction(w.get_address(), 1, 1 / 100)
            w.sign_transaction(t)
            self.assertTrue(bc.add_transaction(t))
        self.assertEqual(len(bc.transaction_pool), tx_count)
        b = bc.new_block(self.tx)

        max_txs_in_block = 2**Tree.MAX_DEPTH
        self.assertEqual(len(list(b.get_transactions())),
                         min(max_txs_in_block, tx_count + 1))

        # Transactions in new block are removed from tx pool when new block is successfully discarded to chain
        b = bc.new_block(self.tx)  # coinbase tx
        mine(b)
        bc.discard_block(b)

        expected_txs_in_tx_pool = max(0, tx_count - (max_txs_in_block - 1))
        self.assertEqual(len(bc.transaction_pool), expected_txs_in_tx_pool)

        leftover_count = min(max_txs_in_block - 1, expected_txs_in_tx_pool)
        self.assertEqual(len(list(bc.new_block(self.tx).get_transactions())),
                         1 + leftover_count)
Esempio n. 2
0
    def test_wallet_balance(self):
        bc = BlockChain()

        w1 = Wallet()
        w1.create_keys()
        addr1 = w1.get_address()

        w2 = Wallet()
        w2.create_keys()
        addr2 = w2.get_address()

        w3 = Wallet()
        w3.create_keys()
        addr3 = w3.get_address()

        tx0 = Transaction("0", addr1, 1)
        b = bc.new_block(tx0)
        mine(b)
        bc.discard_block(b)

        balance = bc.get_wallet_balance(addr1)
        self.assertEqual(balance, 1)

        tx1 = Transaction(addr1, addr2, 0.1)
        tx2 = Transaction(addr1, addr3, 0.2)
        tx3 = Transaction(addr2, addr3, 133)

        w1.sign_transaction(tx1)
        w1.sign_transaction(tx2)
        w2.sign_transaction(tx3)

        self.assertTrue(bc.add_transaction(tx1))
        self.assertTrue(bc.add_transaction(tx2))
        self.assertFalse(bc.add_transaction(tx3))

        tx4 = Transaction(addr1, addr2, 1)
        w1.sign_transaction(tx4)
        self.assertFalse(bc.add_transaction(tx4))

        balance = bc.get_wallet_balance(addr1)
        self.assertEqual(balance, 0.7)

        balance = bc.get_wallet_balance(addr2)
        self.assertEqual(balance, 0.1)

        balance = bc.get_wallet_balance(addr3)
        self.assertEqual(balance, 0.2)

        w4 = Wallet()
        w4.create_keys()
        addr4 = w4.get_address()

        cbtx4 = Transaction("0", addr4, 1)
        b4 = bc.new_block(cbtx4)
        mine(b4)
        bc.discard_block(b4)
        self.assertTrue(bc.get_wallet_balance(addr4), 1)

        ntx4 = Transaction(addr4, "toto", 1, magic=bc.magic)
        w4.sign_transaction(ntx4)
        self.assertTrue(bc.add_transaction(ntx4))
        coinbase4 = Transaction("0", addr4, 1)
        nb4 = bc.new_block(
            coinbase4
        )  # new block nb4 is filled with coinbase4 and ntx4 (fill_block() was called)
        mine(nb4)  # mine block before adding to chain
        logger.debug("DISCARDING...")
        discarded = bc.discard_block(nb4)
        logger.debug("END DISCARD")
        self.assertTrue(discarded)