Пример #1
0
    def advance_txs(self, txs, eventlog_dict):
        self.tx_hashes.append(b''.join(tx_hash for tx, tx_hash in txs))

        # Use local vars for speed in the loops
        undo_info = []
        history = self.history
        history_size = self.history_size
        tx_num = self.tx_count
        script_hashX = self.coin.hashX_from_script
        s_pack = pack
        put_utxo = self.utxo_cache.__setitem__
        spend_utxo = self.spend_utxo
        undo_info_append = undo_info.append
        touched = self.touched

        eventlogs = self.eventlogs
        eventlog_touched = self.eventlog_touched
        eventlogs_size = self.eventlogs_size

        for tx, tx_hash in txs:
            hashXs = set()
            add_hashX = hashXs.add
            tx_numb = s_pack('<I', tx_num)

            # eventlog
            datas = eventlog_dict.get(hash_to_hex_str(tx_hash), [])
            for data in datas:
                hashY, log_index = data
                eventlogs[hashY].append(array.array('I', [tx_num, log_index]))
                eventlog_touched.add(hashY)
            eventlogs_size += len(datas)

            # Spend the inputs
            if not tx.is_coinbase:
                for txin in tx.inputs:
                    cache_value = spend_utxo(txin.prev_hash, txin.prev_idx)
                    undo_info_append(cache_value)
                    add_hashX(cache_value[:-12])

            # Add the new UTXOs
            for idx, txout in enumerate(tx.outputs):
                # Get the hashX.  Ignore unspendable outputs
                hashX = script_hashX(txout.pk_script)
                if hashX:
                    add_hashX(hashX)
                    put_utxo(tx_hash + s_pack('<H', idx),
                             hashX + tx_numb + s_pack('<Q', txout.value))

            for hashX in hashXs:
                history[hashX].append(tx_num)
            history_size += len(hashXs)
            touched.update(hashXs)
            tx_num += 1

        self.tx_count = tx_num
        self.tx_counts.append(tx_num)
        self.history_size = history_size
        self.eventlogs_size = eventlogs_size

        return undo_info
Пример #2
0
def test_hash_to_hex_str():
    assert lib_hash.hash_to_hex_str(b'hash_to_str') == '7274735f6f745f68736168'