/** Copy the block without transactions into the provided empty block. */ protected final void copyBitcoinHeaderTo(final Block block) { block.nonce = nonce; block.prevBlockHash = prevBlockHash; block.merkleRoot = getMerkleRoot(); block.version = version; block.time = time; block.difficultyTarget = difficultyTarget; block.transactions = null; block.hash = getHash(); }
/** Copy the block without transactions into the provided empty block. */ protected final void copyBitcoinHeaderTo(final Block block) { block.nonce = nonce; block.prevBlockHash = prevBlockHash; block.merkleRoot = getMerkleRoot(); block.version = version; block.time = time; block.difficultyTarget = difficultyTarget; block.transactions = null; block.hash = getHash(); }
/** Copy the block without transactions into the provided empty block. */ protected final void copyBitcoinHeaderTo(final Block block) { block.nonce = nonce; block.prevBlockHash = prevBlockHash; block.merkleRoot = getMerkleRoot(); block.version = version; block.time = time; block.difficultyTarget = difficultyTarget; block.transactions = null; block.hash = getHash(); }
/** Copy the block without transactions into the provided empty block. */ protected final void copyBitcoinHeaderTo(final Block block) { block.nonce = nonce; block.prevBlockHash = prevBlockHash; block.merkleRoot = getMerkleRoot(); block.version = version; block.time = time; block.difficultyTarget = difficultyTarget; block.transactions = null; block.hash = getHash(); }
/** * Gets a list of leaf hashes which are contained in the partial merkle tree in this filtered block * * @throws ProtocolException If the partial merkle block is invalid or the merkle root of the partial merkle block doesnt match the block header */ public AbstractMap.SimpleEntry<List<Sha256Hash>, List<Integer>> getTransactionHashesAndIndexes() throws VerificationException { if (cachedTransactionHashes != null && cachedTransactionPositions != null) return new AbstractMap.SimpleEntry<List<Sha256Hash>, List<Integer>>(Collections.unmodifiableList(cachedTransactionHashes), Collections.unmodifiableList(cachedTransactionPositions)); List<Sha256Hash> hashesMatched = new LinkedList<>(); List<Integer> positions = new LinkedList<>(); if (header.getMerkleRoot().equals(merkleTree.getTxnHashAndMerkleRoot(hashesMatched, positions))) { cachedTransactionHashes = hashesMatched; cachedTransactionPositions = positions; return new AbstractMap.SimpleEntry<>(Collections.unmodifiableList(cachedTransactionHashes), Collections.unmodifiableList(cachedTransactionPositions)); } else throw new VerificationException("Merkle root of block header does not match merkle root of partial merkle tree."); }
/** * Gets a list of leaf hashes which are contained in the partial merkle tree in this filtered block * * @throws ProtocolException If the partial merkle block is invalid or the merkle root of the partial merkle block doesnt match the block header */ public List<Sha256Hash> getTransactionHashes() throws VerificationException { if (cachedTransactionHashes != null) return Collections.unmodifiableList(cachedTransactionHashes); List<Sha256Hash> hashesMatched = new LinkedList<>(); if (header.getMerkleRoot().equals(merkleTree.getTxnHashAndMerkleRoot(hashesMatched))) { cachedTransactionHashes = hashesMatched; return Collections.unmodifiableList(cachedTransactionHashes); } else throw new VerificationException("Merkle root of block header does not match merkle root of partial merkle tree."); }
/** * Returns a multi-line string containing a description of the contents of * the block. Use for debugging purposes only. */ @Override public String toString() { StringBuilder s = new StringBuilder(); s.append(" block: \n"); s.append(" hash: ").append(getHashAsString()).append('\n'); s.append(" version: ").append(version); String bips = Joiner.on(", ").skipNulls().join(isBIP34() ? "BIP34" : null, isBIP66() ? "BIP66" : null, isBIP65() ? "BIP65" : null); if (!bips.isEmpty()) s.append(" (").append(bips).append(')'); s.append('\n'); s.append(" previous block: ").append(getPrevBlockHash()).append("\n"); s.append(" merkle root: ").append(getMerkleRoot()).append("\n"); s.append(" time: ").append(time).append(" (").append(Utils.dateTimeFormat(time * 1000)).append(")\n"); s.append(" difficulty target (nBits): ").append(difficultyTarget).append("\n"); s.append(" nonce: ").append(nonce).append("\n"); if (transactions != null && transactions.size() > 0) { s.append(" with ").append(transactions.size()).append(" transaction(s):\n"); for (Transaction tx : transactions) { s.append(tx); } } return s.toString(); }
/** * Returns a multi-line string containing a description of the contents of * the block. Use for debugging purposes only. */ @Override public String toString() { StringBuilder s = new StringBuilder(); s.append(" block: \n"); s.append(" hash: ").append(getHashAsString()).append('\n'); s.append(" version: ").append(version); String bips = Joiner.on(", ").skipNulls().join(isBIP34() ? "BIP34" : null, isBIP66() ? "BIP66" : null, isBIP65() ? "BIP65" : null); if (!bips.isEmpty()) s.append(" (").append(bips).append(')'); s.append('\n'); s.append(" previous block: ").append(getPrevBlockHash()).append("\n"); s.append(" merkle root: ").append(getMerkleRoot()).append("\n"); s.append(" time: ").append(time).append(" (").append(Utils.dateTimeFormat(time * 1000)).append(")\n"); s.append(" difficulty target (nBits): ").append(difficultyTarget).append("\n"); s.append(" nonce: ").append(nonce).append("\n"); if (transactions != null && transactions.size() > 0) { s.append(" with ").append(transactions.size()).append(" transaction(s):\n"); for (Transaction tx : transactions) { s.append(tx); } } return s.toString(); }
/** * Gets a list of leaf hashes which are contained in the partial merkle tree in this filtered block * * @throws ProtocolException If the partial merkle block is invalid or the merkle root of the partial merkle block doesnt match the block header */ public List<Sha256Hash> getTransactionHashes() throws VerificationException { if (cachedTransactionHashes != null) return Collections.unmodifiableList(cachedTransactionHashes); List<Sha256Hash> hashesMatched = new LinkedList<>(); List<Integer> positions = new LinkedList<>(); if (header.getMerkleRoot().equals(merkleTree.getTxnHashAndMerkleRoot(hashesMatched, positions))) { cachedTransactionHashes = hashesMatched; cachedTransactionPositions = positions; return Collections.unmodifiableList(cachedTransactionHashes); } else throw new VerificationException("Merkle root of block header does not match merkle root of partial merkle tree."); }
/** * Gets a list of leaf hashes which are contained in the partial merkle tree in this filtered block * * @throws ProtocolException If the partial merkle block is invalid or the merkle root of the partial merkle block doesnt match the block header */ public List<Sha256Hash> getTransactionHashes() throws VerificationException { if (cachedTransactionHashes != null) return Collections.unmodifiableList(cachedTransactionHashes); List<Sha256Hash> hashesMatched = new LinkedList<Sha256Hash>(); if (header.getMerkleRoot().equals(merkleTree.getTxnHashAndMerkleRoot(hashesMatched))) { cachedTransactionHashes = hashesMatched; return Collections.unmodifiableList(cachedTransactionHashes); } else throw new VerificationException("Merkle root of block header does not match merkle root of partial merkle tree."); }
/** * Returns a multi-line string containing a description of the contents of * the block. Use for debugging purposes only. */ @Override public String toString() { StringBuilder s = new StringBuilder(); s.append(" block: \n"); s.append(" hash: ").append(getHashAsString()).append('\n'); s.append(" version: ").append(version); String bips = Joiner.on(", ").skipNulls().join(isBIP34() ? "BIP34" : null, isBIP66() ? "BIP66" : null, isBIP65() ? "BIP65" : null); if (!bips.isEmpty()) s.append(" (").append(bips).append(')'); s.append('\n'); s.append(" previous block: ").append(getPrevBlockHash()).append("\n"); s.append(" merkle root: ").append(getMerkleRoot()).append("\n"); s.append(" time: ").append(time).append(" (").append(Utils.dateTimeFormat(time * 1000)).append(")\n"); s.append(" difficulty target (nBits): ").append(difficultyTarget).append("\n"); s.append(" nonce: ").append(nonce).append("\n"); if (transactions != null && transactions.size() > 0) { s.append(" with ").append(transactions.size()).append(" transaction(s):\n"); for (Transaction tx : transactions) { s.append(tx); } } return s.toString(); }
/** * Gets a list of leaf hashes which are contained in the partial merkle tree in this filtered block * * @throws ProtocolException If the partial merkle block is invalid or the merkle root of the partial merkle block doesnt match the block header */ public List<Sha256Hash> getTransactionHashes() throws VerificationException { if (cachedTransactionHashes != null) return Collections.unmodifiableList(cachedTransactionHashes); List<Sha256Hash> hashesMatched = new LinkedList<Sha256Hash>(); if (header.getMerkleRoot().equals(merkleTree.getTxnHashAndMerkleRoot(hashesMatched))) { cachedTransactionHashes = hashesMatched; return Collections.unmodifiableList(cachedTransactionHashes); } else throw new VerificationException("Merkle root of block header does not match merkle root of partial merkle tree."); }
/** * Returns a multi-line string containing a description of the contents of * the block. Use for debugging purposes only. */ @Override public String toString() { StringBuilder s = new StringBuilder(); s.append(" block: \n"); s.append(" hash: ").append(getHashAsString()).append('\n'); s.append(" version: ").append(version); String bips = Joiner.on(", ").skipNulls().join(isBIP34() ? "BIP34" : null, isBIP66() ? "BIP66" : null, isBIP65() ? "BIP65" : null); if (!bips.isEmpty()) s.append(" (").append(bips).append(')'); s.append('\n'); s.append(" previous block: ").append(getPrevBlockHash()).append("\n"); s.append(" merkle root: ").append(getMerkleRoot()).append("\n"); s.append(" time: ").append(time).append(" (").append(Utils.dateTimeFormat(time * 1000)).append(")\n"); s.append(" difficulty target (nBits): ").append(difficultyTarget).append("\n"); s.append(" nonce: ").append(nonce).append("\n"); if (transactions != null && transactions.size() > 0) { s.append(" with ").append(transactions.size()).append(" transaction(s):\n"); for (Transaction tx : transactions) { s.append(tx); } } return s.toString(); }
void writeHeader(OutputStream stream) throws IOException { // try for cached write first if (headerBytesValid && payload != null && payload.length >= offset + HEADER_SIZE) { stream.write(payload, offset, HEADER_SIZE); return; } // fall back to manual write Utils.uint32ToByteStreamLE(version, stream); stream.write(prevBlockHash.getReversedBytes()); stream.write(getMerkleRoot().getReversedBytes()); Utils.uint32ToByteStreamLE(time, stream); Utils.uint32ToByteStreamLE(difficultyTarget, stream); Utils.uint32ToByteStreamLE(nonce, stream); }
void writeHeader(OutputStream stream) throws IOException { // try for cached write first if (headerBytesValid && payload != null && payload.length >= offset + HEADER_SIZE) { stream.write(payload, offset, HEADER_SIZE); return; } // fall back to manual write Utils.uint32ToByteStreamLE(version, stream); stream.write(prevBlockHash.getReversedBytes()); stream.write(getMerkleRoot().getReversedBytes()); Utils.uint32ToByteStreamLE(time, stream); Utils.uint32ToByteStreamLE(difficultyTarget, stream); Utils.uint32ToByteStreamLE(nonce, stream); }
void writeHeader(OutputStream stream) throws IOException { // try for cached write first if (headerBytesValid && payload != null && payload.length >= offset + HEADER_SIZE) { stream.write(payload, offset, HEADER_SIZE); return; } // fall back to manual write Utils.uint32ToByteStreamLE(version, stream); stream.write(prevBlockHash.getReversedBytes()); stream.write(getMerkleRoot().getReversedBytes()); Utils.uint32ToByteStreamLE(time, stream); Utils.uint32ToByteStreamLE(difficultyTarget, stream); Utils.uint32ToByteStreamLE(nonce, stream); }
void writeHeader(OutputStream stream) throws IOException { // try for cached write first if (headerBytesValid && payload != null && payload.length >= offset + HEADER_SIZE) { stream.write(payload, offset, HEADER_SIZE); return; } // fall back to manual write Utils.uint32ToByteStreamLE(version, stream); stream.write(prevBlockHash.getReversedBytes()); stream.write(getMerkleRoot().getReversedBytes()); Utils.uint32ToByteStreamLE(time, stream); Utils.uint32ToByteStreamLE(difficultyTarget, stream); Utils.uint32ToByteStreamLE(nonce, stream); }
/** * Extract from Litecoin source code, definition of regtest params. * https://github.com/litecoin-project/litecoin/blob/edc66b374ea68107c721062152dd95e6aa037d53/src/chainparams.cpp */ @Override public Block getGenesisBlock() { synchronized (LitecoinRegTestParams.class) { if (genesis == null) { genesis = super.getGenesisBlock(); genesis.setNonce(0); genesis.setDifficultyTarget(0x207fffffL); genesis.setTime(1296688602L); checkState(genesis.getVersion() == 1); checkState(genesis.getMerkleRoot().toString().equals("97ddfbbae6be97fd6cdf3e7ca13232a3afff2353e29badfab7f73011edd4ced9")); checkState(genesis.getHashAsString().toLowerCase().equals("530827f38f93b43ed12af0b3ad25a288dc02ed74d6d7857862df51fc56c416f9")); genesis.verifyHeader(); } return genesis; } }
/** * Get 1 header of the block number 1 (the first one is 0) in the chain */ @Test public void testHeaders1() throws Exception { MessageSerializer serializer = MainNetParams.get().getDefaultSerializer(); byte[] headersMessageBytes = HEX.decode("f9beb4d9686561" + "646572730000000000520000005d4fab8101010000006fe28c0ab6f1b372c1a6a246ae6" + "3f74f931e8365e15a089c68d6190000000000982051fd1e4ba744bbbe680e1fee14677b" + "a1a3c3540bf7b1cdb606e857233e0e61bc6649ffff001d01e3629900"); HeadersMessage headersMessage = (HeadersMessage) serializer.deserialize(ByteBuffer.wrap(headersMessageBytes)); // The first block after the genesis // http://blockexplorer.com/b/1 Block block = headersMessage.getBlockHeaders().get(0); assertEquals("00000000839a8e6886ab5951d76f411475428afc90947ee320161bbf18eb6048", block.getHashAsString()); assertNotNull(block.transactions); assertEquals("0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098", Utils.HEX.encode(block.getMerkleRoot().getBytes())); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); serializer.serialize(headersMessage, byteArrayOutputStream); byte[] serializedBytes = byteArrayOutputStream.toByteArray(); assertArrayEquals(headersMessageBytes, serializedBytes); }