def consensus_hash(db, field, previous_consensus_hash, content): cursor = db.cursor() block_index = util.CURRENT_BLOCK_INDEX # Initialise previous hash on first block. if block_index <= config.BLOCK_FIRST: assert not previous_consensus_hash previous_consensus_hash = util.dhash_string(CONSENSUS_HASH_SEED) # Get previous hash. if not previous_consensus_hash: try: previous_consensus_hash = list( cursor.execute( '''SELECT * FROM blocks WHERE block_index = ?''', (block_index - 1, )))[0][field] except IndexError: previous_consensus_hash = None if not previous_consensus_hash: raise ConsensusError( 'Empty previous {} for block {}. Please launch a `reparse`.'. format(field, block_index)) # Calculate current hash. consensus_hash_version = CONSENSUS_HASH_VERSION_TESTNET if config.TESTNET else CONSENSUS_HASH_VERSION_MAINNET calculated_hash = util.dhash_string( previous_consensus_hash + '{}{}'.format(consensus_hash_version, ''.join(content))) # Verify hash (if already in database) or save hash (if not). # NOTE: do not enforce this for messages_hashes, those are more informational (for now at least) found_hash = list( cursor.execute('''SELECT * FROM blocks WHERE block_index = ?''', (block_index, )))[0][field] or None if found_hash and field != 'messages_hash': # Check against existing value. if calculated_hash != found_hash: raise ConsensusError( 'Inconsistent {} for block {} (calculated {}, vs {} in database).' .format(field, block_index, calculated_hash, found_hash)) else: # Save new hash. cursor.execute( '''UPDATE blocks SET {} = ? WHERE block_index = ?'''.format(field), (calculated_hash, block_index)) # Check against checkpoints. checkpoints = CHECKPOINTS_TESTNET if config.TESTNET else CHECKPOINTS_MAINNET if field != 'messages_hash' and block_index in checkpoints and checkpoints[ block_index][field] != calculated_hash: raise ConsensusError('Incorrect {} for block {}.'.format( field, block_index)) return calculated_hash, found_hash
def consensus_hash(db, field, previous_consensus_hash, content): cursor = db.cursor() block_index = util.CURRENT_BLOCK_INDEX # Initialise previous hash on first block. if block_index <= config.BLOCK_FIRST: assert not previous_consensus_hash previous_consensus_hash = util.dhash_string(CONSENSUS_HASH_SEED) # Get previous hash. if not previous_consensus_hash: try: previous_consensus_hash = list( cursor.execute("""SELECT * FROM blocks WHERE block_index = ?""", (block_index - 1,)) )[0][field] except IndexError: previous_consensus_hash = None if not previous_consensus_hash: raise ConsensusError( "Empty previous {} for block {}. Please launch a `reparse`.".format(field, block_index) ) # Calculate current hash. consensus_hash_version = CONSENSUS_HASH_VERSION_TESTNET if config.TESTNET else CONSENSUS_HASH_VERSION_MAINNET calculated_hash = util.dhash_string( previous_consensus_hash + "{}{}".format(consensus_hash_version, "".join(content)) ) # Verify hash (if already in database) or save hash (if not). # NOTE: do not enforce this for messages_hashes, those are more informational (for now at least) found_hash = ( list(cursor.execute("""SELECT * FROM blocks WHERE block_index = ?""", (block_index,)))[0][field] or None ) if found_hash and field != "messages_hash": # Check against existing value. if calculated_hash != found_hash: raise ConsensusError( "Inconsistent {} for block {} (calculated {}, vs {} in database).".format( field, block_index, calculated_hash, found_hash ) ) else: # Save new hash. cursor.execute( """UPDATE blocks SET {} = ? WHERE block_index = ?""".format(field), (calculated_hash, block_index) ) # Check against checkpoints. checkpoints = CHECKPOINTS_TESTNET if config.TESTNET else CHECKPOINTS_MAINNET if field != "messages_hash" and block_index in checkpoints and checkpoints[block_index][field] != calculated_hash: raise ConsensusError("Incorrect {} for block {}.".format(field, block_index)) return calculated_hash, found_hash
def consensus_hash(db, field, previous_consensus_hash, content): cursor = db.cursor() block_index = util.CURRENT_BLOCK_INDEX # Initialise previous hash on first block. if block_index == config.BLOCK_FIRST: assert not previous_consensus_hash previous_consensus_hash = util.dhash_string(CONSENSUS_HASH_SEED) # Get previous hash. if not previous_consensus_hash: try: previous_consensus_hash = list(cursor.execute('''SELECT * FROM blocks WHERE block_index = ?''', (block_index - 1,)))[0][field] except IndexError: previous_consensus_hash = None if not previous_consensus_hash: raise ConsensusError('Empty previous {} for block {}. Please launch a `reparse`.'.format(field, block_index)) # Calculate current hash. consensus_hash_version = CONSENSUS_HASH_VERSION_TESTNET if config.TESTNET else CONSENSUS_HASH_VERSION_MAINNET calculated_hash = util.dhash_string(previous_consensus_hash + '{}{}'.format(consensus_hash_version, ''.join(content))) # Verify hash (if already in database) or save hash (if not). found_hash = list(cursor.execute('''SELECT * FROM blocks WHERE block_index = ?''', (block_index,)))[0][field] if found_hash: # Check against existing value. if calculated_hash != found_hash: raise ConsensusError('Inconsistent {} for block {}.'.format(field, block_index)) else: # Save new hash. cursor.execute('''UPDATE blocks SET {} = ? WHERE block_index = ?'''.format(field), (calculated_hash, block_index)) # Check against checkpoints. checkpoints = CHECKPOINTS_TESTNET if config.TESTNET else CHECKPOINTS_MAINNET if block_index in checkpoints and checkpoints[block_index][field] != calculated_hash: raise ConsensusError('Incorrect {} for block {}.'.format(field, block_index)) return calculated_hash
def insert_block(db, block_index, parse_block=True): """Add blocks to the blockchain.""" cursor = db.cursor() block_hash = util.dhash_string(chr(block_index)) block_time = block_index * 1000 block = (block_index, block_hash, block_time, None, None, None, None) cursor.execute('''INSERT INTO blocks (block_index, block_hash, block_time, ledger_hash, txlist_hash, previous_block_hash, difficulty) VALUES (?,?,?,?,?,?,?)''', block) util.CURRENT_BLOCK_INDEX = block_index # TODO: Correct?! cursor.close() if parse_block: blocks.parse_block(db, block_index, block_time) return block_index, block_hash, block_time