org.bitcoinj.core.BlockChain类的使用及代码示例

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

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

BlockChain介绍

[英]A BlockChain implements the simplified payment verification mode of the Bitcoin protocol. It is the right choice to use for programs that have limited resources as it won't verify transactions signatures or attempt to store all of the block chain. Really, this class should be called SPVBlockChain but for backwards compatibility it is not.
[中]区块链实现比特币协议的简化支付验证模式。它是资源有限的程序的正确选择,因为它不会验证事务签名或尝试存储所有的区块链。实际上,这个类应该称为SPVBlockChain,但为了向后兼容,它不是。

代码示例

代码示例来源:origin: ICOnator/ICOnator-backend

@Bean
public BlockChain bitcoinBlockchain(SPVBlockStore blockStore,
                  Context bitcoinContext, NetworkParameters chainNetworkParameters)
    throws IOException, BlockStoreException {
  if (chainNetworkParameters.equals(MainNetParams.get())) {
    InputStream checkPoints = BitcoinMonitor.class.getClassLoader().getResourceAsStream("checkpoints.txt");
    CheckpointManager.checkpoint(chainNetworkParameters, checkPoints, blockStore, 1498867200L);
  } else if (chainNetworkParameters.equals(TestNet3Params.get())) {
    InputStream checkPoints = BitcoinMonitor.class.getClassLoader().getResourceAsStream("checkpoints-testnet.txt");
    CheckpointManager.checkpoint(chainNetworkParameters, checkPoints, blockStore, 1498867200L);
  }
  return new BlockChain(bitcoinContext, blockStore);
}

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

blockChain = new BlockChain(networkParameters, blockStore);
blockChain.addWallet(wallet);
log.debug("Created block chain '{}' with height '{}'", blockChain, blockChain.getBestChainHeight());

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

@Test
public void falsePositives() throws Exception {
  double decay = AbstractBlockChain.FP_ESTIMATOR_ALPHA;
  assertTrue(0 == chain.getFalsePositiveRate()); // Exactly
  chain.trackFalsePositives(55);
  assertEquals(decay * 55, chain.getFalsePositiveRate(), 1e-4);
  chain.trackFilteredTransactions(550);
  double rate1 = chain.getFalsePositiveRate();
  // Run this scenario a few more time for the filter to converge
  for (int i = 1 ; i < 10 ; i++) {
    chain.trackFalsePositives(55);
    chain.trackFilteredTransactions(550);
  }
  // Ensure we are within 10%
  assertEquals(0.1, chain.getFalsePositiveRate(), 0.01);
  // Check that we get repeatable results after a reset
  chain.resetFalsePositiveEstimate();
  assertTrue(0 == chain.getFalsePositiveRate()); // Exactly
  chain.trackFalsePositives(55);
  assertEquals(decay * 55, chain.getFalsePositiveRate(), 1e-4);
  chain.trackFilteredTransactions(550);
  assertEquals(rate1, chain.getFalsePositiveRate(), 1e-4);
}

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

@Override
protected void rollbackBlockStore(int height) throws BlockStoreException {
  lock.lock();
  try {
    int currentHeight = getBestChainHeight();
    checkArgument(height >= 0 && height <= currentHeight, "Bad height: %s", height);
    if (height == currentHeight)
      return; // nothing to do
    // Look for the block we want to be the new chain head
    StoredBlock newChainHead = blockStore.getChainHead();
    while (newChainHead.getHeight() > height) {
      newChainHead = newChainHead.getPrev(blockStore);
      if (newChainHead == null)
        throw new BlockStoreException("Unreachable height");
    }
    // Modify store directly
    blockStore.put(newChainHead);
    this.setChainHead(newChainHead);
  } finally {
    lock.unlock();
  }
}

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

BlockChain chain = new BlockChain(params, chainStore);
PeerGroup peers = new PeerGroup(params, chain);
peers.addPeerDiscovery(new DnsDiscovery(params));
chain.addWallet(wallet);
peers.addWallet(wallet);

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

@Test
  public void rollbackBlockStore() throws Exception {
    // This test simulates an issue on Android, that causes the VM to crash while receiving a block, so that the
    // block store is persisted but the wallet is not.
    Block b1 = PARAMS.getGenesisBlock().createNextBlock(coinbaseTo);
    Block b2 = b1.createNextBlock(coinbaseTo);
    // Add block 1, no frills.
    assertTrue(chain.add(b1));
    assertEquals(b1.cloneAsHeader(), chain.getChainHead().getHeader());
    assertEquals(1, chain.getBestChainHeight());
    assertEquals(1, wallet.getLastBlockSeenHeight());
    // Add block 2 while wallet is disconnected, to simulate crash.
    chain.removeWallet(wallet);
    assertTrue(chain.add(b2));
    assertEquals(b2.cloneAsHeader(), chain.getChainHead().getHeader());
    assertEquals(2, chain.getBestChainHeight());
    assertEquals(1, wallet.getLastBlockSeenHeight());
    // Add wallet back. This will detect the height mismatch and repair the damage done.
    chain.addWallet(wallet);
    assertEquals(b1.cloneAsHeader(), chain.getChainHead().getHeader());
    assertEquals(1, chain.getBestChainHeight());
    assertEquals(1, wallet.getLastBlockSeenHeight());
    // Now add block 2 correctly.
    assertTrue(chain.add(b2));
    assertEquals(b2.cloneAsHeader(), chain.getChainHead().getHeader());
    assertEquals(2, chain.getBestChainHeight());
    assertEquals(2, wallet.getLastBlockSeenHeight());
  }
}

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

/**
 * <p>Constructs a BlockChain connected to the given wallet and store. To obtain a {@link Wallet} you can construct
 * one from scratch, or you can deserialize a saved wallet from disk using
 * {@link Wallet#loadFromFile(java.io.File, WalletExtension...)}</p>
 *
 * <p>For the store, you should use {@link org.bitcoinj.store.SPVBlockStore} or you could also try a
 * {@link org.bitcoinj.store.MemoryBlockStore} if you want to hold all headers in RAM and don't care about
 * disk serialization (this is rare).</p>
 */
public BlockChain(Context context, Wallet wallet, BlockStore blockStore) throws BlockStoreException {
  this(context, new ArrayList<Wallet>(), blockStore);
  addWallet(wallet);
}

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

private void testDeprecatedBlockVersion(final long deprecatedVersion, final long newVersion)
    throws Exception {
  final BlockStore versionBlockStore = new MemoryBlockStore(PARAMS);
  final BlockChain versionChain = new BlockChain(PARAMS, versionBlockStore);
  // Build a historical chain of version 3 blocks
  long timeSeconds = 1231006505;
  int height = 0;
  FakeTxBuilder.BlockPair chainHead = null;
  // Put in just enough v2 blocks to be a minority
  for (height = 0; height < (PARAMS.getMajorityWindow() - PARAMS.getMajorityRejectBlockOutdated()); height++) {
    chainHead = FakeTxBuilder.createFakeBlock(versionBlockStore, deprecatedVersion, timeSeconds, height);
    versionChain.add(chainHead.block);
    timeSeconds += 60;
  }
  // Fill the rest of the window with v3 blocks
  for (; height < PARAMS.getMajorityWindow(); height++) {
    chainHead = FakeTxBuilder.createFakeBlock(versionBlockStore, newVersion, timeSeconds, height);
    versionChain.add(chainHead.block);
    timeSeconds += 60;
  }
  chainHead = FakeTxBuilder.createFakeBlock(versionBlockStore, deprecatedVersion, timeSeconds, height);
  // Trying to add a new v2 block should result in rejection
  thrown.expect(VerificationException.BlockVersionOutOfDate.class);
  try {
    versionChain.add(chainHead.block);
  } catch(final VerificationException ex) {
    throw (Exception) ex.getCause();
  }
}

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

final BlockChain chain = new BlockChain(PARAMS, store);
final PeerGroup peerGroup = new PeerGroup(PARAMS, chain);
peerGroup.addPeerDiscovery(new DnsDiscovery(PARAMS));
chain.addListener(new AbstractBlockChainListener() {
  @Override
  public void notifyNewBestBlock(StoredBlock block) throws VerificationException {

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

@Test
public void estimatedBlockTime() throws Exception {
  NetworkParameters params = MainNetParams.get();
  BlockChain prod = new BlockChain(new Context(params), new MemoryBlockStore(params));
  Date d = prod.estimateBlockTime(200000);
  // The actual date of block 200,000 was 2012-09-22 10:47:00
  assertEquals(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.US).parse("2012-10-23T08:35:05.000-0700"), d);
}

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

final BlockChain chain = new BlockChain(params, store);
final PeerGroup peerGroup = new PeerGroup(params, chain);
System.out.println("Connecting to " + peerAddress + "...");
System.out.println("Checkpointing up to " + Utils.dateTimeFormat(timeAgo * 1000));
chain.addNewBestBlockListener(Threading.SAME_THREAD, new NewBestBlockListener() {
  @Override
  public void notifyNewBestBlock(StoredBlock block) throws VerificationException {

代码示例来源:origin: ICOnator/ICOnator-backend

@Override
protected void doneDownload() {
  LOG.info("Download done, now sending block numbers.");
  final int startBlockHeight = bitcoinBlockchain.getBestChainHeight();
  messageService.send(new BlockNRBitcoinMessage((long) startBlockHeight, new Date().getTime()));
  bitcoinPeerGroup.addBlocksDownloadedEventListener((peer, block, filteredBlock, blocksLeft) -> {
    if (bitcoinBlockchain.getBestChainHeight() > startBlockHeight) {
      messageService.send(new BlockNRBitcoinMessage((long) bitcoinBlockchain.getBestChainHeight(), new Date().getTime()));
    }
  });
}

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

@Override
  public boolean add(FilteredBlock block) throws VerificationException, PrunedException {
    boolean success = super.add(block);
    if (success) {
      trackFilteredTransactions(block.getTransactionCount());
    }
    return success;
  }
}

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

ECKey receiveKey = wallet2.freshReceiveKey();
int height = 1;
chain.addWallet(wallet2);
chain.add(b1);
  chain.add(b2);
chain.add(b3);
chain.add(b4);
assertEquals(wallet.getBalance(BalanceType.AVAILABLE), COIN);

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

@Test
public void unconnectedBlocks() throws Exception {
  Block b1 = PARAMS.getGenesisBlock().createNextBlock(coinbaseTo);
  Block b2 = b1.createNextBlock(coinbaseTo);
  Block b3 = b2.createNextBlock(coinbaseTo);
  // Connected.
  assertTrue(chain.add(b1));
  // Unconnected but stored. The head of the chain is still b1.
  assertFalse(chain.add(b3));
  assertEquals(chain.getChainHead().getHeader(), b1.cloneAsHeader());
  // Add in the middle block.
  assertTrue(chain.add(b2));
  assertEquals(chain.getChainHead().getHeader(), b3.cloneAsHeader());
}

代码示例来源:origin: ConsensusJ/consensusj

@Override
public Integer getblockcount() {
  if(!kit.isRunning()) {
    return null;
  }
  return kit.chain().getChainHead().getHeight();
}

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

vChain = new BlockChain(params, vStore);
vPeerGroup = createPeerGroup();
if (this.userAgent != null)
  vPeerGroup.addPeerDiscovery(discovery != null ? discovery : new DnsDiscovery(params));
vChain.addWallet(vWallet);
vPeerGroup.addWallet(vWallet);
onSetupCompleted();

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

@Override
protected void rollbackBlockStore(int height) throws BlockStoreException {
  lock.lock();
  try {
    int currentHeight = getBestChainHeight();
    checkArgument(height >= 0 && height <= currentHeight, "Bad height: %s", height);
    if (height == currentHeight)
      return; // nothing to do
    // Look for the block we want to be the new chain head
    StoredBlock newChainHead = blockStore.getChainHead();
    while (newChainHead.getHeight() > height) {
      newChainHead = newChainHead.getPrev(blockStore);
      if (newChainHead == null)
        throw new BlockStoreException("Unreachable height");
    }
    // Modify store directly
    blockStore.put(newChainHead);
    this.setChainHead(newChainHead);
  } finally {
    lock.unlock();
  }
}

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

/**
 * <p>Constructs a BlockChain connected to the given wallet and store. To obtain a {@link Wallet} you can construct
 * one from scratch, or you can deserialize a saved wallet from disk using
 * {@link Wallet#loadFromFile(java.io.File, WalletExtension...)}</p>
 *
 * <p>For the store, you should use {@link org.bitcoinj.store.SPVBlockStore} or you could also try a
 * {@link org.bitcoinj.store.MemoryBlockStore} if you want to hold all headers in RAM and don't care about
 * disk serialization (this is rare).</p>
 */
public BlockChain(Context context, Wallet wallet, BlockStore blockStore) throws BlockStoreException {
  this(context, new ArrayList<Wallet>(), blockStore);
  addWallet(wallet);
}

代码示例来源: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());
}

相关文章