Esempio n. 1
0
def make_headers(count, header=None):
    if header is None:
        last_hash = HASH_INITIAL_BLOCK
    else:
        last_hash = header.hash()
    tweak = last_hash
    headers = []
    for i in range(count):
        headers.append(
            BlockHeader(version=1,
                        previous_block_hash=last_hash,
                        merkle_root=make_hash(i, tweak),
                        timestamp=GENESIS_TIME + i * 600,
                        difficulty=DEFAULT_DIFFICULTY,
                        nonce=i * 137))
        last_hash = headers[-1].hash()
    return headers
Esempio n. 2
0
 def get_blockheader_with_transaction_hashes(self, block_hash):
     URL = "%s/api/block/%s" % (self.base_url, b2h_rev(block_hash))
     r = json.loads(urlopen(URL).read().decode("utf8"))
     version = r.get("version")
     previous_block_hash = h2b_rev(r.get("previousblockhash"))
     merkle_root = h2b_rev(r.get("merkleroot"))
     timestamp = r.get("time")
     difficulty = int(r.get("bits"), 16)
     nonce = int(r.get("nonce"))
     tx_hashes = [h2b_rev(tx_hash) for tx_hash in r.get("tx")]
     blockheader = BlockHeader(version, previous_block_hash, merkle_root, timestamp, difficulty, nonce)
     if blockheader.hash() != block_hash:
         return None, None
     calculated_hash = merkle(tx_hashes, double_sha256)
     if calculated_hash != merkle_root:
         return None, None
     blockheader.height = r.get("height")
     return blockheader, tx_hashes