def validate_unspents(self, tx_db):
        """
        Spendable objects returned from blockchain.info or
        similar services contain coin_value information that must be trusted
        on faith. Mistaken coin_value data can result in coins being wasted
        to fees.

        This function solves this problem by iterating over the incoming
        transactions, fetching them from the tx_db in full, and verifying
        that the coin_values are as expected.

        Returns the fee for this transaction. If any of the spendables set by
        tx.set_unspents do not match the authenticated transactions, a
        ValidationFailureError is raised.
        """
        tx_hashes = set((tx_in.previous_hash for tx_in in self.txs_in))

        # build a local copy of the DB
        tx_lookup = {}
        for h in tx_hashes:
            if h == ZERO32:
                continue
            the_tx = tx_db.get(h)
            if the_tx is None:
                raise KeyError("hash id %s not in tx_db" % b2h_rev(h))
            if the_tx.hash() != h:
                raise KeyError("attempt to load Tx %s yielded a Tx with id %s" % (h2b_rev(h), the_tx.id()))
            tx_lookup[h] = the_tx

        for idx, tx_in in enumerate(self.txs_in):
            if tx_in.previous_hash == ZERO32:
                continue
            txs_out = tx_lookup[tx_in.previous_hash].txs_out
            if tx_in.previous_index > len(txs_out):
                raise BadSpendableError("tx_out index %d is too big for Tx %s" %
                                        (tx_in.previous_index, b2h_rev(tx_in.previous_hash)))
            tx_out1 = txs_out[tx_in.previous_index]
            tx_out2 = self.unspents[idx]
            if tx_out1.coin_value != tx_out2.coin_value:
                raise BadSpendableError(
                    "unspents[%d] coin value mismatch (%d vs %d)" % (
                        idx, tx_out1.coin_value, tx_out2.coin_value))
            if tx_out1.script != tx_out2.script:
                raise BadSpendableError("unspents[%d] script mismatch!" % idx)

        return self.fee()
def validate_unspents ( self , tx_db ) : tx_hashes = set ( ( tx_in . previous_hash for tx_in in self . txs_in ) ) tx_lookup = { } for h in tx_hashes : if h == ZERO32 : continue the_tx = tx_db . get ( h ) if the_tx is None : raise KeyError ( "hash id %s not in tx_db" % b2h_rev ( h ) ) if the_tx . hash ( ) != h : raise KeyError ( "attempt to load Tx %s yielded a Tx with id %s" % ( h2b_rev ( h ) , the_tx . id ( ) ) ) tx_lookup [ h ] = the_tx for idx , tx_in in enumerate ( self . txs_in ) : if tx_in . previous_hash == ZERO32 : continue txs_out = tx_lookup [ tx_in . previous_hash ] . txs_out if tx_in . previous_index > len ( txs_out ) : raise BadSpendableError ( "tx_out index %d is too big for Tx %s" % ( tx_in . previous_index , b2h_rev ( tx_in . previous_hash ) ) ) tx_out1 = txs_out [ tx_in . previous_index ] tx_out2 = self . unspents [ idx ] if tx_out1 . coin_value != tx_out2 . coin_value : raise BadSpendableError ( "unspents[%d] coin value mismatch (%d vs %d)" % ( idx , tx_out1 . coin_value , tx_out2 . coin_value ) ) if tx_out1 . script != tx_out2 . script : raise BadSpendableError ( "unspents[%d] script mismatch!" % idx ) return self . fee ( )
Spendable objects returned from blockchain.info or
        similar services contain coin_value information that must be trusted
        on faith. Mistaken coin_value data can result in coins being wasted
        to fees.

        This function solves this problem by iterating over the incoming
        transactions, fetching them from the tx_db in full, and verifying
        that the coin_values are as expected.

        Returns the fee for this transaction. If any of the spendables set by
        tx.set_unspents do not match the authenticated transactions, a
        ValidationFailureError is raised.