示例#1
0
 def get_blocks(self, hashes):
     with self.sqlite3_lock:
         blocks = []
         for hash in hashes:
             c = self.db.execute('SELECT * FROM blocks WHERE hash=?',
                                 (hash, ))
             row = c.fetchone()
             if row:
                 block = bitcoin.Block(**row)
                 c = self.db.execute(
                     'SELECT hash FROM transactions WHERE block_hash=? ORDER BY position',
                     (block.hash, ))
                 rows = c.fetchall()
                 if rows:
                     transaction_hashes = [row['hash'] for row in rows]
                     block.transactions = self.get_transactions(
                         transaction_hashes)
                 blocks.append(block)
         return blocks
示例#2
0
    def parse_block(self):
        h = hashlib.sha256(
            hashlib.sha256(self.helper.buffer[:4 + 32 + 32 + 4 + 4 +
                                              4]).digest()).digest()
        version = self.helper.uint32()
        prev_hash = self.helper.read(32)
        merkle_root = self.helper.read(32)
        timestamp = self.helper.uint32()
        bits = self.helper.uint32()
        nonce = self.helper.read(4)
        tx_count = self.helper.var_uint()
        txs = []
        for x in range(tx_count):
            txs.append(self.parse_tx())

        b = bitcoin.Block(h, prev_hash, merkle_root, timestamp, bits, nonce,
                          version)
        for tx in txs:
            b.transactions.append(tx)

        return b
示例#3
0
 def next_blocks(self, block):
     with self.sqlite3_lock:
         c = self.db.execute('SELECT * FROM blocks WHERE prev_hash=?',
                             (block.hash, ))
         return [bitcoin.Block(**block) for block in c.fetchall()]
示例#4
0
 def tails(self):
     with self.sqlite3_lock:
         c = self.db.execute(
             'SELECT * FROM blocks WHERE height IS NULL and prev_hash NOT IN (SELECT hash FROM blocks)'
         )
         return [bitcoin.Block(**block) for block in c.fetchall()]