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

x33g5p2x  于2022-01-30 转载在 其他  
字(13.0k)|赞(0)|评价(0)|浏览(97)

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

Transaction.isCoinBase介绍

[英]A coinbase transaction is one that creates a new coin. They are the first transaction in each block and their value is determined by a formula that all implementations of Bitcoin share. In 2011 the value of a coinbase transaction is 50 coins, but in future it will be less. A coinbase transaction is defined not only by its position in a block but by the data in the inputs.
[中]coinbase交易是创造新硬币的交易。它们是每个区块中的第一笔交易,其价值由所有比特币实现共享的公式决定。2011年,coinbase交易的价值是50枚硬币,但在未来会更低。coinbase事务不仅由其在块中的位置定义,还由输入中的数据定义。

代码示例

代码示例来源:origin: Coinomi/coinomi-android

@Override
public boolean isGenerated() {
  return tx.isCoinBase() || tx.isCoinStake();
}

代码示例来源:origin: openwalletGH/openwallet-android

@Override
public boolean isGenerated() {
  return tx.isCoinBase() || tx.isCoinStake();
}

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

/** Adds a transaction to this block, with or without checking the sanity of doing so */
void addTransaction(Transaction t, boolean runSanityChecks) {
  unCacheTransactions();
  if (transactions == null) {
    transactions = new ArrayList<>();
  }
  t.setParent(this);
  if (runSanityChecks && transactions.size() == 0 && !t.isCoinBase())
    throw new RuntimeException("Attempted to add a non-coinbase transaction as the first transaction: " + t);
  else if (runSanityChecks && transactions.size() > 0 && t.isCoinBase())
    throw new RuntimeException("Attempted to add a coinbase transaction when there already is one: " + t);
  transactions.add(t);
  adjustLength(transactions.size(), t.length);
  // Force a recalculation next time the values are needed.
  merkleRoot = null;
  hash = null;
}

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

/** Adds a transaction to this block, with or without checking the sanity of doing so */
void addTransaction(Transaction t, boolean runSanityChecks) {
  unCacheTransactions();
  if (transactions == null) {
    transactions = new ArrayList<>();
  }
  t.setParent(this);
  if (runSanityChecks && transactions.size() == 0 && !t.isCoinBase())
    throw new RuntimeException("Attempted to add a non-coinbase transaction as the first transaction: " + t);
  else if (runSanityChecks && transactions.size() > 0 && t.isCoinBase())
    throw new RuntimeException("Attempted to add a coinbase transaction when there already is one: " + t);
  transactions.add(t);
  adjustLength(transactions.size(), t.length);
  // Force a recalculation next time the values are needed.
  merkleRoot = null;
  hash = null;
}

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

/** Adds a transaction to this block, with or without checking the sanity of doing so */
void addTransaction(Transaction t, boolean runSanityChecks) {
  unCacheTransactions();
  if (transactions == null) {
    transactions = new ArrayList<Transaction>();
  }
  t.setParent(this);
  if (runSanityChecks && transactions.size() == 0 && !t.isCoinBase())
    throw new RuntimeException("Attempted to add a non-coinbase transaction as the first transaction: " + t);
  else if (runSanityChecks && transactions.size() > 0 && t.isCoinBase())
    throw new RuntimeException("Attempted to add a coinbase transaction when there already is one: " + t);
  transactions.add(t);
  adjustLength(transactions.size(), t.length);
  // Force a recalculation next time the values are needed.
  merkleRoot = null;
  hash = null;
}

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

/** Adds a transaction to this block, with or without checking the sanity of doing so */
void addTransaction(Transaction t, boolean runSanityChecks) {
  unCacheTransactions();
  if (transactions == null) {
    transactions = new ArrayList<Transaction>();
  }
  t.setParent(this);
  if (runSanityChecks && transactions.size() == 0 && !t.isCoinBase())
    throw new RuntimeException("Attempted to add a non-coinbase transaction as the first transaction: " + t);
  else if (runSanityChecks && transactions.size() > 0 && t.isCoinBase())
    throw new RuntimeException("Attempted to add a coinbase transaction when there already is one: " + t);
  transactions.add(t);
  adjustLength(transactions.size(), t.length);
  // Force a recalculation next time the values are needed.
  merkleRoot = null;
  hash = null;
}

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

/**
 * Verify the transactions on a block.
 *
 * @param height block height, if known, or -1 otherwise. If provided, used
 * to validate the coinbase input script of v2 and above blocks.
 * @throws VerificationException if there was an error verifying the block.
 */
private void checkTransactions(final int height, final EnumSet<VerifyFlag> flags)
    throws VerificationException {
  // The first transaction in a block must always be a coinbase transaction.
  if (!transactions.get(0).isCoinBase())
    throw new VerificationException("First tx is not coinbase");
  if (flags.contains(Block.VerifyFlag.HEIGHT_IN_COINBASE) && height >= BLOCK_HEIGHT_GENESIS) {
    transactions.get(0).checkCoinBaseHeight(height);
  }
  // The rest must not be.
  for (int i = 1; i < transactions.size(); i++) {
    if (transactions.get(i).isCoinBase())
      throw new VerificationException("TX " + i + " is coinbase when it should not be.");
  }
}

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

/**
 * Verify the transactions on a block.
 *
 * @param height block height, if known, or -1 otherwise. If provided, used
 * to validate the coinbase input script of v2 and above blocks.
 * @throws VerificationException if there was an error verifying the block.
 */
private void checkTransactions(final int height, final EnumSet<VerifyFlag> flags)
    throws VerificationException {
  // The first transaction in a block must always be a coinbase transaction.
  if (!transactions.get(0).isCoinBase())
    throw new VerificationException("First tx is not coinbase");
  if (flags.contains(Block.VerifyFlag.HEIGHT_IN_COINBASE) && height >= BLOCK_HEIGHT_GENESIS) {
    transactions.get(0).checkCoinBaseHeight(height);
  }
  // The rest must not be.
  for (int i = 1; i < transactions.size(); i++) {
    if (transactions.get(i).isCoinBase())
      throw new VerificationException("TX " + i + " is coinbase when it should not be.");
  }
}

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

/**
 * Verify the transactions on a block.
 *
 * @param height block height, if known, or -1 otherwise. If provided, used
 * to validate the coinbase input script of v2 and above blocks.
 * @throws VerificationException if there was an error verifying the block.
 */
private void checkTransactions(final int height, final EnumSet<VerifyFlag> flags)
    throws VerificationException {
  // The first transaction in a block must always be a coinbase transaction.
  if (!transactions.get(0).isCoinBase())
    throw new VerificationException("First tx is not coinbase");
  if (flags.contains(Block.VerifyFlag.HEIGHT_IN_COINBASE) && height >= BLOCK_HEIGHT_GENESIS) {
    transactions.get(0).checkCoinBaseHeight(height);
  }
  // The rest must not be.
  for (int i = 1; i < transactions.size(); i++) {
    if (transactions.get(i).isCoinBase())
      throw new VerificationException("TX " + i + " is coinbase when it should not be.");
  }
}

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

/**
 * Verify the transactions on a block.
 *
 * @param height block height, if known, or -1 otherwise. If provided, used
 * to validate the coinbase input script of v2 and above blocks.
 * @throws VerificationException if there was an error verifying the block.
 */
private void checkTransactions(final int height, final EnumSet<VerifyFlag> flags)
    throws VerificationException {
  // The first transaction in a block must always be a coinbase transaction.
  if (!transactions.get(0).isCoinBase())
    throw new VerificationException("First tx is not coinbase");
  if (flags.contains(Block.VerifyFlag.HEIGHT_IN_COINBASE) && height >= BLOCK_HEIGHT_GENESIS) {
    transactions.get(0).checkCoinBaseHeight(height);
  }
  // The rest must not be.
  for (int i = 1; i < transactions.size(); i++) {
    if (transactions.get(i).isCoinBase())
      throw new VerificationException("TX " + i + " is coinbase when it should not be.");
  }
}

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

/**
 * A transaction is mature if it is either a building coinbase tx that is as deep or deeper than the required coinbase depth, or a non-coinbase tx.
 */
public boolean isMature() {
  if (!isCoinBase())
    return true;
  if (getConfidence().getConfidenceType() != ConfidenceType.BUILDING)
    return false;
  return getConfidence().getDepthInBlocks() >= params.getSpendableCoinbaseDepth();
}

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

/**
 * A transaction is mature if it is either a building coinbase tx that is as deep or deeper than the required coinbase depth, or a non-coinbase tx.
 */
public boolean isMature() {
  if (!isCoinBase())
    return true;
  if (getConfidence().getConfidenceType() != ConfidenceType.BUILDING)
    return false;
  return getConfidence().getDepthInBlocks() >= params.getSpendableCoinbaseDepth();
}

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

/**
 * A transaction is mature if it is either a building coinbase tx that is as deep or deeper than the required coinbase depth, or a non-coinbase tx.
 */
public boolean isMature() {
  if (!isCoinBase())
    return true;
  if (getConfidence().getConfidenceType() != ConfidenceType.BUILDING)
    return false;
  return getConfidence().getDepthInBlocks() >= params.getSpendableCoinbaseDepth();
}

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

@Override
  public void receiveFromBlock(Transaction tx, StoredBlock block, BlockChain.NewBlockType blockType,
                 int relativityOffset) throws VerificationException {
    super.receiveFromBlock(tx, block, blockType, relativityOffset);
    BlockChainTest.this.block[0] = block;
    if (isTransactionRelevant(tx) && tx.isCoinBase()) {
      BlockChainTest.this.coinbaseTransaction = tx;
    }
  }
};

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

/**
 * A transaction is mature if it is either a building coinbase tx that is as deep or deeper than the required
 * coinbase depth, or a non-coinbase tx.
 */
public boolean isMature() {
  if (!isCoinBase())
    return true;
  if (getConfidence().getConfidenceType() != ConfidenceType.BUILDING)
    return false;
  return getConfidence().getDepthInBlocks() >= params.getSpendableCoinbaseDepth();
}

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

/**
 * Check block height is in coinbase input script, for use after BIP 34
 * enforcement is enabled.
 */
public void checkCoinBaseHeight(final int height)
    throws VerificationException {
  checkArgument(height >= Block.BLOCK_HEIGHT_GENESIS);
  checkState(isCoinBase());
  // Check block height is in coinbase input script
  final TransactionInput in = this.getInputs().get(0);
  final ScriptBuilder builder = new ScriptBuilder();
  builder.number(height);
  final byte[] expected = builder.build().getProgram();
  final byte[] actual = in.getScriptBytes();
  if (actual.length < expected.length) {
    throw new VerificationException.CoinbaseHeightMismatch("Block height mismatch in coinbase.");
  }
  for (int scriptIdx = 0; scriptIdx < expected.length; scriptIdx++) {
    if (actual[scriptIdx] != expected[scriptIdx]) {
      throw new VerificationException.CoinbaseHeightMismatch("Block height mismatch in coinbase.");
    }
  }
}

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

/**
 * Check block height is in coinbase input script, for use after BIP 34
 * enforcement is enabled.
 */
public void checkCoinBaseHeight(final int height)
    throws VerificationException {
  checkArgument(height >= Block.BLOCK_HEIGHT_GENESIS);
  checkState(isCoinBase());
  // Check block height is in coinbase input script
  final TransactionInput in = this.getInputs().get(0);
  final ScriptBuilder builder = new ScriptBuilder();
  builder.number(height);
  final byte[] expected = builder.build().getProgram();
  final byte[] actual = in.getScriptBytes();
  if (actual.length < expected.length) {
    throw new VerificationException.CoinbaseHeightMismatch("Block height mismatch in coinbase.");
  }
  for (int scriptIdx = 0; scriptIdx < expected.length; scriptIdx++) {
    if (actual[scriptIdx] != expected[scriptIdx]) {
      throw new VerificationException.CoinbaseHeightMismatch("Block height mismatch in coinbase.");
    }
  }
}

代码示例来源:origin: Multibit-Legacy/multibit-hd

/**
 * @param transaction The Bitcoinj transaction providing the information
 * @param amount      The amount as calculated from the wallet
 */
public TransactionSeenEvent(Transaction transaction, Coin amount) {
 transactionId = transaction.getHashAsString();
 TransactionConfidence confidence = transaction.getConfidence();
 confidenceType = confidence.getConfidenceType();
 if (confidenceType.equals(TransactionConfidence.ConfidenceType.BUILDING)) {
  depthInBlocks = confidence.getDepthInBlocks();
 } else {
  depthInBlocks = DEPTH_IN_BLOCKS_IS_UNDEFINED;
 }
 coinbase = transaction.isCoinBase();
 numberOfPeers = confidence.numBroadcastPeers();
 this.amount = amount;
 firstAppearanceInWallet = false;
}

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

@Test
public void coinbaseTxns() throws Exception {
  // Covers issue 420 where the outpoint index of a coinbase tx input was being mis-serialized.
  Block b = PARAMS.getGenesisBlock().createNextBlockWithCoinbase(Block.BLOCK_VERSION_GENESIS, myKey.getPubKey(), FIFTY_COINS, Block.BLOCK_HEIGHT_GENESIS);
  Transaction coinbase = b.getTransactions().get(0);
  assertTrue(coinbase.isCoinBase());
  BlockChain chain = new BlockChain(PARAMS, myWallet, new MemoryBlockStore(PARAMS));
  assertTrue(chain.add(b));
  // Wallet now has a coinbase tx in it.
  assertEquals(1, myWallet.getTransactions(true).size());
  assertTrue(myWallet.getTransaction(coinbase.getHash()).isCoinBase());
  Wallet wallet2 = roundTrip(myWallet);
  assertEquals(1, wallet2.getTransactions(true).size());
  assertTrue(wallet2.getTransaction(coinbase.getHash()).isCoinBase());
}

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

/** Create a fake coinbase transaction. */
public static Transaction createFakeCoinbaseTx(final NetworkParameters params) {
  TransactionOutPoint outpoint = new TransactionOutPoint(params, -1, Sha256Hash.ZERO_HASH);
  TransactionInput input = new TransactionInput(params, null, new byte[0], outpoint);
  Transaction tx = new Transaction(params);
  tx.addInput(input);
  TransactionOutput outputToMe = new TransactionOutput(params, tx, Coin.FIFTY_COINS,
      new ECKey().toAddress(params));
  tx.addOutput(outputToMe);
  checkState(tx.isCoinBase());
  return tx;
}

相关文章

微信公众号

最新文章

更多

Transaction类方法