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

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

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

Block.getDifficultyTarget介绍

[英]Returns the difficulty of the proof of work that this block should meet encoded in compact form. The BlockChain verifies that this is not too easy by looking at the length of the chain when the block is added. To find the actual value the hash should be compared against, use org.bitcoinj.core.Block#getDifficultyTargetAsInteger(). Note that this is not the same as the difficulty value reported by the Bitcoin "getdifficulty" RPC that you may see on various block explorers. That number is the result of applying a formula to the underlying difficulty to normalize the minimum to 1. Calculating the difficulty that way is currently unsupported.
[中]返回此块应满足以紧凑形式编码的工作证明的难度。区块链通过在添加区块时查看链的长度来验证这是否太容易。要找到散列应与之进行比较的实际值,请使用org。比特币。果心块#Get困难TargetAsinteger()。请注意,这与您可能在各种块探索器上看到的比特币“GetOfficity”RPC报告的难度值不同。这个数字是应用公式将最小值标准化为1的潜在困难的结果。目前不支持以这种方式计算难度。

代码示例

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

@Override
  /** the testnet rules don't work for regtest, where difficulty stays the same */
  public long calculateNewDifficultyTarget(StoredBlock storedPrev, Block nextBlock, BlockStore blockStore)
      throws VerificationException, BlockStoreException {
    final Block prev = storedPrev.getHeader();
    return prev.getDifficultyTarget();
  }
}

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

private void checkTestnetDifficulty(StoredBlock storedPrev, Block prev, Block next) throws VerificationException, BlockStoreException {
  checkState(lock.isHeldByCurrentThread());
  // After 15th February 2012 the rules on the testnet change to avoid people running up the difficulty
  // and then leaving, making it too hard to mine a block. On non-difficulty transition points, easy
  // blocks are allowed if there has been a span of 20 minutes without one.
  final long timeDelta = next.getTimeSeconds() - prev.getTimeSeconds();
  // There is an integer underflow bug in bitcoin-qt that means mindiff blocks are accepted when time
  // goes backwards.
  if (timeDelta >= 0 && timeDelta > NetworkParameters.TARGET_SPACING * 2) {
    if (next.getDifficultyTargetAsInteger().equals(params.getMaxTarget()))
      return;
    else throw new VerificationException("Unexpected change in difficulty");
  }
  else {
    // Walk backwards until we find a block that doesn't have the easiest proof of work, then check
    // that difficulty is equal to that one.
    StoredBlock cursor = storedPrev;
    while (!cursor.getHeader().equals(params.getGenesisBlock()) &&
        cursor.getHeight() % params.getInterval() != 0 &&
        cursor.getHeader().getDifficultyTargetAsInteger().equals(params.getMaxTarget()))
      cursor = cursor.getPrev(blockStore);
    BigInteger cursorTarget = cursor.getHeader().getDifficultyTargetAsInteger();
    BigInteger newTarget = next.getDifficultyTargetAsInteger();
    if (!cursorTarget.equals(newTarget))
      throw new VerificationException("Testnet block transition that is not allowed: " +
        Long.toHexString(cursor.getHeader().getDifficultyTarget()) + " vs " +
        Long.toHexString(next.getDifficultyTarget()));
  }
}

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

calcDiff = params.getMaxTarget();
int accuracyBytes = (int) (nextBlock.getDifficultyTarget() >>> 24) - 3;
BigInteger receivedDifficulty = nextBlock.getDifficultyTargetAsInteger();
    long nBitsNext = nextBlock.getDifficultyTarget();

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

void verifyDifficulty(BigInteger newTarget, Block nextBlock)
{
  if (newTarget.compareTo(this.getMaxTarget()) > 0) {
    log.info("Difficulty hit proof of work limit: {}", newTarget.toString(16));
    newTarget = this.getMaxTarget();
  }
  int accuracyBytes = (int) (nextBlock.getDifficultyTarget() >>> 24) - 3;
  long receivedTargetCompact = nextBlock.getDifficultyTarget();
  // The calculated difficulty is to a higher precision than received, so reduce here.
  BigInteger mask = BigInteger.valueOf(0xFFFFFFL).shiftLeft(accuracyBytes * 8);
  newTarget = newTarget.and(mask);
  long newTargetCompact = Utils.encodeCompactBits(newTarget);
  if (newTargetCompact != receivedTargetCompact)
    throw new VerificationException("Network provided difficulty bits do not match what was calculated: " +
        Long.toHexString(newTargetCompact) + " vs " + Long.toHexString(receivedTargetCompact));
}

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

if (!cursorTarget.equals(newTarget))
    throw new VerificationException("Testnet block transition that is not allowed: " +
    Long.toHexString(cursor.getHeader().getDifficultyTarget()) + " vs " +
    Long.toHexString(nextBlock.getDifficultyTarget()));

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

/**
 * Calculate the difficulty target expected for the next block after a normal
 * recalculation interval. Does not handle special cases such as testnet blocks
 * being setting the target to maximum for blocks after a long interval.
 *
 * @param previousHeight height of the block immediately before the retarget.
 * @param prev the block immediately before the retarget block.
 * @param nextBlock the block the retarget happens at.
 * @param blockIntervalAgo The last retarget block.
 * @return New difficulty target as compact bytes.
 */
protected long calculateNewDifficultyTargetInner(int previousHeight, final Block prev,
    final Block nextBlock, final Block blockIntervalAgo) {
  return this.calculateNewDifficultyTargetInner(previousHeight, prev.getTimeSeconds(),
    prev.getDifficultyTarget(), blockIntervalAgo.getTimeSeconds(),
    nextBlock.getDifficultyTarget());
}

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

if (!cursorTarget.equals(newTarget))
    throw new VerificationException("Testnet block transition that is not allowed: " +
    Long.toHexString(cursor.getHeader().getDifficultyTarget()) + " vs " +
    Long.toHexString(nextBlock.getDifficultyTarget()));

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

/**
 * Calculate the difficulty target expected for the next block after a normal
 * recalculation interval. Does not handle special cases such as testnet blocks
 * being setting the target to maximum for blocks after a long interval.
 *
 * @param previousHeight height of the block immediately before the retarget.
 * @param prev the block immediately before the retarget block.
 * @param nextBlock the block the retarget happens at.
 * @param blockIntervalAgo The last retarget block.
 * @return New difficulty target as compact bytes.
 */
protected long calculateNewDifficultyTargetInner(int previousHeight, final Block prev,
    final Block nextBlock, final Block blockIntervalAgo) {
  return this.calculateNewDifficultyTargetInner(previousHeight, prev.getTimeSeconds(),
    prev.getDifficultyTarget(), blockIntervalAgo.getTimeSeconds(),
    nextBlock.getDifficultyTarget());
}

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

@Override
public void checkDifficultyTransitions(final StoredBlock storedPrev, final Block nextBlock,
  final BlockStore blockStore) throws VerificationException, BlockStoreException {
  if (!isDifficultyTransitionPoint(storedPrev.getHeight()) && nextBlock.getTime().after(testnetDiffDate)) {
    Block prev = storedPrev.getHeader();
    // After 15th February 2012 the rules on the testnet change to avoid people running up the difficulty
    // and then leaving, making it too hard to mine a block. On non-difficulty transition points, easy
    // blocks are allowed if there has been a span of 20 minutes without one.
    final long timeDelta = nextBlock.getTimeSeconds() - prev.getTimeSeconds();
    // There is an integer underflow bug in bitcoin-qt that means mindiff blocks are accepted when time
    // goes backwards.
    if (timeDelta >= 0 && timeDelta <= NetworkParameters.TARGET_SPACING * 2) {
    // Walk backwards until we find a block that doesn't have the easiest proof of work, then check
    // that difficulty is equal to that one.
    StoredBlock cursor = storedPrev;
    while (!cursor.getHeader().equals(getGenesisBlock()) &&
          cursor.getHeight() % getInterval() != 0 &&
          cursor.getHeader().getDifficultyTargetAsInteger().equals(getMaxTarget()))
        cursor = cursor.getPrev(blockStore);
    BigInteger cursorTarget = cursor.getHeader().getDifficultyTargetAsInteger();
    BigInteger newTarget = nextBlock.getDifficultyTargetAsInteger();
    if (!cursorTarget.equals(newTarget))
        throw new VerificationException("Testnet block transition that is not allowed: " +
        Long.toHexString(cursor.getHeader().getDifficultyTarget()) + " vs " +
        Long.toHexString(nextBlock.getDifficultyTarget()));
    }
  } else {
    super.checkDifficultyTransitions(storedPrev, nextBlock, blockStore);
  }
}

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

@Override
public void checkDifficultyTransitions(StoredBlock storedPrev, Block nextBlock, BlockStore blockStore)
  throws VerificationException, BlockStoreException {
  try {
    final long newTargetCompact = calculateNewDifficultyTarget(storedPrev, nextBlock, blockStore);
    final long receivedTargetCompact = nextBlock.getDifficultyTarget();
    if (newTargetCompact != receivedTargetCompact)
      throw new VerificationException("Network provided difficulty bits do not match what was calculated: " +
          newTargetCompact + " vs " + receivedTargetCompact);
  } catch (CheckpointEncounteredException ex) {
    // Just have to take it on trust then
  }
}

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

@Override
public void checkDifficultyTransitions(final StoredBlock storedPrev, final Block nextBlock,
  final BlockStore blockStore) throws VerificationException, BlockStoreException {
  if (!isDifficultyTransitionPoint(storedPrev.getHeight()) && nextBlock.getTime().after(testnetDiffDate)) {
    Block prev = storedPrev.getHeader();
    // After 15th February 2012 the rules on the testnet change to avoid people running up the difficulty
    // and then leaving, making it too hard to mine a block. On non-difficulty transition points, easy
    // blocks are allowed if there has been a span of 20 minutes without one.
    final long timeDelta = nextBlock.getTimeSeconds() - prev.getTimeSeconds();
    // There is an integer underflow bug in bitcoin-qt that means mindiff blocks are accepted when time
    // goes backwards.
    if (timeDelta >= 0 && timeDelta <= NetworkParameters.TARGET_SPACING * 2) {
    // Walk backwards until we find a block that doesn't have the easiest proof of work, then check
    // that difficulty is equal to that one.
    StoredBlock cursor = storedPrev;
    while (!cursor.getHeader().equals(getGenesisBlock()) &&
          cursor.getHeight() % getInterval() != 0 &&
          cursor.getHeader().getDifficultyTargetAsInteger().equals(getMaxTarget()))
        cursor = cursor.getPrev(blockStore);
    BigInteger cursorTarget = cursor.getHeader().getDifficultyTargetAsInteger();
    BigInteger newTarget = nextBlock.getDifficultyTargetAsInteger();
    if (!cursorTarget.equals(newTarget))
        throw new VerificationException("Testnet block transition that is not allowed: " +
        Long.toHexString(cursor.getHeader().getDifficultyTarget()) + " vs " +
        Long.toHexString(nextBlock.getDifficultyTarget()));
    }
  } else {
    super.checkDifficultyTransitions(storedPrev, nextBlock, blockStore);
  }
}

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

@Override
public void checkDifficultyTransitions(StoredBlock storedPrev, Block nextBlock, BlockStore blockStore)
  throws VerificationException, BlockStoreException {
  try {
    final long newTargetCompact = calculateNewDifficultyTarget(storedPrev, nextBlock, blockStore);
    final long receivedTargetCompact = nextBlock.getDifficultyTarget();
    if (newTargetCompact != receivedTargetCompact)
      throw new VerificationException("Network provided difficulty bits do not match what was calculated: " +
          newTargetCompact + " vs " + receivedTargetCompact);
  } catch (CheckpointEncounteredException ex) {
    // Just have to take it on trust then
  }
}

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

throw new VerificationException("Testnet block transition that is not allowed: " +
      Long.toHexString(Utils.encodeCompactBits(maxTarget)) + " (required min difficulty) vs " +
      Long.toHexString(nextBlock.getDifficultyTarget()));
return;

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

if (nextBlock.getDifficultyTarget() != prev.getDifficultyTarget())
    throw new VerificationException("Unexpected change in difficulty at height " + storedPrev.getHeight() +
        ": " + Long.toHexString(nextBlock.getDifficultyTarget()) + " vs " +
        Long.toHexString(prev.getDifficultyTarget()));
  return;
  timespan = targetTimespan * 4;
BigInteger newTarget = Utils.decodeCompactBits(prev.getDifficultyTarget());
newTarget = newTarget.multiply(BigInteger.valueOf(timespan));
newTarget = newTarget.divide(BigInteger.valueOf(targetTimespan));
int accuracyBytes = (int) (nextBlock.getDifficultyTarget() >>> 24) - 3;
long receivedTargetCompact = nextBlock.getDifficultyTarget();

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

if (nextBlock.getDifficultyTarget() != prev.getDifficultyTarget())
    throw new VerificationException("Unexpected change in difficulty at height " + storedPrev.getHeight() +
        ": " + Long.toHexString(nextBlock.getDifficultyTarget()) + " vs " +
        Long.toHexString(prev.getDifficultyTarget()));
  return;
  timespan = targetTimespan * 4;
BigInteger newTarget = Utils.decodeCompactBits(prev.getDifficultyTarget());
newTarget = newTarget.multiply(BigInteger.valueOf(timespan));
newTarget = newTarget.divide(BigInteger.valueOf(targetTimespan));
int accuracyBytes = (int) (nextBlock.getDifficultyTarget() >>> 24) - 3;
long receivedTargetCompact = nextBlock.getDifficultyTarget();

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

if (nextBlock.getDifficultyTarget() != prev.getDifficultyTarget())
    throw new VerificationException("Unexpected change in difficulty at height " + storedPrev.getHeight() +
        ": " + Long.toHexString(nextBlock.getDifficultyTarget()) + " vs " +
        Long.toHexString(prev.getDifficultyTarget()));
  return;
  timespan = targetTimespan * 4;
BigInteger newTarget = Utils.decodeCompactBits(prev.getDifficultyTarget());
newTarget = newTarget.multiply(BigInteger.valueOf(timespan));
newTarget = newTarget.divide(BigInteger.valueOf(targetTimespan));
int accuracyBytes = (int) (nextBlock.getDifficultyTarget() >>> 24) - 3;
long receivedTargetCompact = nextBlock.getDifficultyTarget();

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

if (nextBlock.getDifficultyTarget() != prev.getDifficultyTarget())
    throw new VerificationException("Unexpected change in difficulty at height " + storedPrev.getHeight() +
        ": " + Long.toHexString(nextBlock.getDifficultyTarget()) + " vs " +
        Long.toHexString(prev.getDifficultyTarget()));
  return;
  timespan = targetTimespan * 4;
BigInteger newTarget = Utils.decodeCompactBits(prev.getDifficultyTarget());
newTarget = newTarget.multiply(BigInteger.valueOf(timespan));
newTarget = newTarget.divide(BigInteger.valueOf(targetTimespan));
int accuracyBytes = (int) (nextBlock.getDifficultyTarget() >>> 24) - 3;
long receivedTargetCompact = nextBlock.getDifficultyTarget();

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

if (nextBlock.getDifficultyTarget() != prev.getDifficultyTarget())
    throw new VerificationException("Unexpected change in difficulty at height " + storedPrev.getHeight() +
        ": " + Long.toHexString(nextBlock.getDifficultyTarget()) + " vs " +
        Long.toHexString(prev.getDifficultyTarget()));
  return;
  timespan = targetTimespan * 4;
BigInteger newTarget = Utils.decodeCompactBits(prev.getDifficultyTarget());
newTarget = newTarget.multiply(BigInteger.valueOf(timespan));
newTarget = newTarget.divide(BigInteger.valueOf(targetTimespan));
int accuracyBytes = (int) (nextBlock.getDifficultyTarget() >>> 24) - 3;
long receivedTargetCompact = nextBlock.getDifficultyTarget();

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

&& cursor.getHeader().getDifficultyTarget() == Utils.encodeCompactBits(this.getMaxTarget())) {
      StoredBlock prevCursor = cursor.getPrev(blockStore);
      if (prevCursor == null) {
    return cursor.getHeader().getDifficultyTarget();
return prev.getDifficultyTarget();
prev.getDifficultyTarget(), blockIntervalAgo.getTimeSeconds(),
nextBlock.getDifficultyTarget());

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

&& cursor.getHeader().getDifficultyTarget() == Utils.encodeCompactBits(this.getMaxTarget())) {
      StoredBlock prevCursor = cursor.getPrev(blockStore);
      if (prevCursor == null) {
    return cursor.getHeader().getDifficultyTarget();
return prev.getDifficultyTarget();
prev.getDifficultyTarget(), blockIntervalAgo.getTimeSeconds(),
nextBlock.getDifficultyTarget());

相关文章