示例#1
0
    def validate_transaction(self, tx):

        if not tx.is_valid():
            return False

        available_value = 0
        spending_value = 0
        previous_outputs = []
        for tx_in in tx.inputs:
            id = tx_in.prevout.hex
            tx_out = self.get_utxo(id)
            if not tx_out:
                return False
            previous_outputs.append(tx_out)
            available_value += tx_out.value

        for tx_out in tx.outputs:
            spending_value += tx_out.value

        if spending_value > available_value:
            return False

        for tx_in, tx_out in zip(tx.inputs, previous_outputs):
            script = tx_in.unlocking_script + tx_out.locking_script
            # script.sighashes = [sighash_all(tx)]  # insert tx_out hashes
            result = script.execute(memory={0x100: sighash_all(tx)})
            if not result:
                return False
        return True
示例#2
0
def test_flow_16():
    """
    This MUST NOT fail
    """

    blockchain = Blockchain()

    coinbase_0 = Tx([TxIn(OutPoint("00" * 32, 0), Script())],
                    [TxOut(10**10, Script())])
    coinbase_0.outputs[0].locking_script = lock_p2pkh(
        pubkey_hash_from_prvkey(1))
    origin_transactions = [coinbase_0]
    origin_header = BlockHeader("00" * 32,
                                generate_merkle_root(origin_transactions), 0)
    origin = Block(origin_header, origin_transactions)
    assert blockchain.add_blocks([origin])

    coinbase_1 = Tx(
        [TxIn(OutPoint("00" * 32, 0), Script())],
        [TxOut(5 * 10**9, Script()),
         TxOut(5 * 10**9, Script())],
    )
    tx = Tx(
        [TxIn(OutPoint(coinbase_0.txid, 0), Script())],
        [TxOut(10**10 - 100, Script()),
         TxOut(50, Script())],
    )

    tx.inputs[0].unlocking_script = unlock_p2pkh(sighash_all(tx), 1)

    block_1_transactions = [coinbase_1, tx]
    block_1_header = BlockHeader(origin.header.pow,
                                 generate_merkle_root(block_1_transactions), 0)
    block_1 = Block(block_1_header, block_1_transactions)

    assert blockchain.add_blocks([block_1])

    reset_blockchain()