org.bitcoinj.core.Block.getMerkleRoot()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(15.5k)|赞(0)|评价(0)|浏览(169)

本文整理了Java中org.bitcoinj.core.Block.getMerkleRoot()方法的一些代码示例,展示了Block.getMerkleRoot()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Block.getMerkleRoot()方法的具体详情如下:
包路径:org.bitcoinj.core.Block
类名称:Block
方法名:getMerkleRoot

Block.getMerkleRoot介绍

[英]Returns the merkle root in big endian form, calculating it from transactions if necessary.
[中]以大端形式返回merkle根,必要时从事务中计算。

代码示例

代码示例来源:origin: fr.acinq/bitcoinj-core

/** 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();
}

代码示例来源:origin: cash.bitcoinj/bitcoinj-core

/** 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();
}

代码示例来源:origin: HashEngineering/dashj

/** 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();
}

代码示例来源:origin: greenaddress/GreenBits

/** 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();
}

代码示例来源:origin: fr.acinq/bitcoinj-core

/**
 * 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.");
}

代码示例来源:origin: greenaddress/GreenBits

/**
 * 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.");
}

代码示例来源:origin: cash.bitcoinj/bitcoinj-core

/**
 * 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();
}

代码示例来源:origin: fr.acinq/bitcoinj-core

/**
 * 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();
}

代码示例来源:origin: fr.acinq/bitcoinj-core

/**
 * 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.");
}

代码示例来源:origin: cash.bitcoinj/bitcoinj-core

/**
 * 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.");
}

代码示例来源:origin: greenaddress/GreenBits

/**
 * 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();
}

代码示例来源:origin: HashEngineering/dashj

/**
 * 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.");
}

代码示例来源:origin: HashEngineering/dashj

/**
 * 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();
}

代码示例来源:origin: cash.bitcoinj/bitcoinj-core

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);
}

代码示例来源:origin: HashEngineering/dashj

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);
}

代码示例来源:origin: greenaddress/GreenBits

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);
}

代码示例来源:origin: fr.acinq/bitcoinj-core

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);
}

代码示例来源:origin: dogecoin/libdohj

/**
 * 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;
  }
}

代码示例来源:origin: greenaddress/GreenBits

/**
 * 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);
}

相关文章