public StoredBlock get(Sha256Hash hash, boolean wasUndoableOnly) throws BlockStoreException { // Optimize for chain head if (chainHeadHash != null && chainHeadHash.equals(hash)) return chainHeadBlock; if (verifiedChainHeadHash != null && verifiedChainHeadHash.equals(hash)) return verifiedChainHeadBlock; maybeConnect(); PreparedStatement s = null; try { s = conn.get() .prepareStatement( "SELECT chainWork, height, header, wasUndoable FROM headers WHERE hash = ?"); // We skip the first 4 bytes because (on prodnet) the minimum target has 4 0-bytes byte[] hashBytes = new byte[28]; System.arraycopy(hash.getBytes(), 3, hashBytes, 0, 28); s.setBytes(1, hashBytes); ResultSet results = s.executeQuery(); if (!results.next()) { return null; } // Parse it. if (wasUndoableOnly && !results.getBoolean(4)) return null; BigInteger chainWork = new BigInteger(results.getBytes(1)); int height = results.getInt(2); Block b = new Block(params, results.getBytes(3)); b.verifyHeader(); StoredBlock stored = new StoredBlock(b, chainWork, height); return stored; } catch (SQLException ex) { throw new BlockStoreException(ex); } catch (ProtocolException e) { // Corrupted database. throw new BlockStoreException(e); } catch (VerificationException e) { // Should not be able to happen unless the database contains bad // blocks. throw new BlockStoreException(e); } finally { if (s != null) try { s.close(); } catch (SQLException e) { throw new BlockStoreException("Failed to close PreparedStatement"); } } }
private void checkMerkleRoot() throws VerificationException { Sha256Hash calculatedRoot = calculateMerkleRoot(); if (!calculatedRoot.equals(merkleRoot)) { log.error("Merkle tree did not verify"); throw new VerificationException( "Merkle hashes do not match: " + calculatedRoot + " vs " + merkleRoot); } }
@Override public boolean equals(Object o) { if (o == null || o.getClass() != getClass()) return false; GetBlocksMessage other = (GetBlocksMessage) o; return (other.version == version && locator.size() == other.locator.size() && locator.containsAll(other.locator) && stopHash.equals(other.stopHash)); }
@Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; UTXOsMessage message = (UTXOsMessage) o; if (height != message.height) return false; if (!chainHead.equals(message.chainHead)) return false; if (!Arrays.equals(heights, message.heights)) return false; if (!Arrays.equals(hits, message.hits)) return false; if (!outputs.equals(message.outputs)) return false; return true; }
/** * Returns true if the block height is either not a checkpoint, or is a checkpoint and the hash * matches. */ public boolean passesCheckpoint(int height, Sha256Hash hash) { Sha256Hash checkpointHash = checkpoints.get(height); return checkpointHash == null || checkpointHash.equals(hash); }