org.bitcoinj.core.BlockChain.<init>()方法的使用及代码示例

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

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

BlockChain.<init>介绍

[英]Constructs a BlockChain connected to the given list of listeners and a store.
[中]构建一个连接到给定侦听器列表和存储的区块链。

代码示例

代码示例来源: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: HashEngineering/dashj

chain = new FullPrunedBlockChain(params, (FullPrunedBlockStore) store);
else
  chain = new BlockChain(params, store);

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

blockChain = new BlockChain(networkParameters, blockStore);

代码示例来源:origin: uncleleonfan/FunWallet

blockChain = new BlockChain(Constants.NETWORK_PARAMETERS, wallet, blockStore);
} catch (final BlockStoreException x) {
  throw new Error("blockchain cannot be created", x);

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

public static void main(String[] args) throws Exception {
    BriefLogFormatter.init();
    System.out.println("Connecting to node");
    final NetworkParameters params = TestNet3Params.get();

    BlockStore blockStore = new MemoryBlockStore(params);
    BlockChain chain = new BlockChain(params, blockStore);
    PeerGroup peerGroup = new PeerGroup(params, chain);
    peerGroup.start();
    peerGroup.addAddress(new PeerAddress(InetAddress.getLocalHost(), params.getPort()));
    peerGroup.waitForPeers(1).get();
    Peer peer = peerGroup.getConnectedPeers().get(0);

    Sha256Hash txHash = Sha256Hash.wrap(args[0]);
    ListenableFuture<Transaction> future = peer.getPeerMempoolTransaction(txHash);
    System.out.println("Waiting for node to send us the requested transaction: " + txHash);
    Transaction tx = future.get();
    System.out.println(tx);

    System.out.println("Waiting for node to send us the dependencies ...");
    List<Transaction> deps = peer.downloadDependencies(tx).get();
    for (Transaction dep : deps) {
      System.out.println("Got dependency " + dep.getHashAsString());
    }

    System.out.println("Done.");
    peerGroup.stop();
  }
}

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

public static void main(String[] args) throws Exception {
    BriefLogFormatter.init();
    System.out.println("Connecting to node");
    final NetworkParameters params = TestNet3Params.get();

    BlockStore blockStore = new MemoryBlockStore(params);
    BlockChain chain = new BlockChain(params, blockStore);
    PeerGroup peerGroup = new PeerGroup(params, chain);
    peerGroup.start();
    PeerAddress addr = new PeerAddress(InetAddress.getLocalHost(), params.getPort());
    peerGroup.addAddress(addr);
    peerGroup.waitForPeers(1).get();
    Peer peer = peerGroup.getConnectedPeers().get(0);

    Sha256Hash blockHash = Sha256Hash.wrap(args[0]);
    Future<Block> future = peer.getBlock(blockHash);
    System.out.println("Waiting for node to send us the requested block: " + blockHash);
    Block block = future.get();
    System.out.println(block);
    peerGroup.stopAsync();
  }
}

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

public static void main(String[] args) throws Exception {
    File file = new File(args[0]);
    Wallet wallet = Wallet.loadFromFile(file);
    System.out.println(wallet.toString());

    // Set up the components and link them together.
    final NetworkParameters params = TestNet3Params.get();
    BlockStore blockStore = new MemoryBlockStore(params);
    BlockChain chain = new BlockChain(params, wallet, blockStore);

    final PeerGroup peerGroup = new PeerGroup(params, chain);
    peerGroup.startAsync();

    wallet.addCoinsReceivedEventListener(new WalletCoinsReceivedEventListener() {
      @Override
      public synchronized void onCoinsReceived(Wallet w, Transaction tx, Coin prevBalance, Coin newBalance) {
        System.out.println("\nReceived tx " + tx.getHashAsString());
        System.out.println(tx.toString());
      }
    });

    // Now download and process the block chain.
    peerGroup.downloadBlockChain();
    peerGroup.stopAsync();
    wallet.saveToFile(file);
    System.out.println("\nDone!\n");
    System.out.println(wallet.toString());
  }
}

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

final BlockChain chain = new BlockChain(PARAMS, store);
final PeerGroup peerGroup = new PeerGroup(PARAMS, chain);
peerGroup.addPeerDiscovery(new DnsDiscovery(PARAMS));

代码示例来源: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: blockchain/thunder

public void init () {
  synchronized (initialized) {
    if (!initialized) {
      try {
        blockStore = new SPVBlockStore(Constants.getNetwork(), new File("blockheaders"));
      } catch (Exception e) {
        blockStore = new MemoryBlockStore(Constants.getNetwork());
      }
      try {
        blockChain = new BlockChain(Constants.getNetwork(), blockStore);
        peerGroup = new PeerGroup(Constants.getNetwork(), blockChain);
        peerGroup.addPeerDiscovery(new DnsDiscovery(Constants.getNetwork()));
        peerGroup.setDownloadTxDependencies(false);
        peerGroup.setBloomFilteringEnabled(false);
        peerGroup.setFastCatchupTimeSecs(System.currentTimeMillis());
        peerGroup.start();
        peerGroup.addEventListener(new EventListener(), Threading.SAME_THREAD);
        registerShutdownHook();
        final DownloadProgressTracker listener = new DownloadProgressTracker();
        peerGroup.startBlockChainDownload(listener);
      } catch (Exception e) {
        throw new RuntimeException(e);
      }
      initialized = true;
    }
  }
}

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

private void createMarriedWallet(int threshold, int numKeys, boolean addSigners) throws BlockStoreException {
  wallet = new Wallet(PARAMS);
  blockStore = new MemoryBlockStore(PARAMS);
  chain = new BlockChain(PARAMS, wallet, blockStore);
  List<DeterministicKey> followingKeys = Lists.newArrayList();
  for (int i = 0; i < numKeys - 1; i++) {
    final DeterministicKeyChain keyChain = new DeterministicKeyChain(new SecureRandom());
    DeterministicKey partnerKey = DeterministicKey.deserializeB58(null, keyChain.getWatchingKey().serializePubB58(PARAMS), PARAMS);
    followingKeys.add(partnerKey);
    if (addSigners && i < threshold - 1)
      wallet.addTransactionSigner(new KeyChainTransactionSigner(keyChain));
  }
  MarriedKeyChain chain = MarriedKeyChain.builder()
      .random(new SecureRandom())
      .followingKeys(followingKeys)
      .threshold(threshold).build();
  wallet.addAndActivateHDChain(chain);
}

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

public void setUp() throws Exception {
  BriefLogFormatter.init();
  Context.propagate(new Context(PARAMS, 100, Coin.ZERO, false));
  wallet = new Wallet(PARAMS);
  myKey = wallet.currentReceiveKey();
  myAddress = myKey.toAddress(PARAMS);
  blockStore = new MemoryBlockStore(PARAMS);
  chain = new BlockChain(PARAMS, wallet, blockStore);
}

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

public void setUp(BlockStore blockStore) throws Exception {
  BriefLogFormatter.init();
  Context.propagate(new Context(PARAMS, 100, Coin.ZERO, false));
  this.blockStore = blockStore;
  // Allow subclasses to override the wallet object with their own.
  if (wallet == null) {
    wallet = new Wallet(PARAMS);
    key = wallet.freshReceiveKey();
    address = key.toAddress(PARAMS);
  }
  blockChain = new BlockChain(PARAMS, wallet, blockStore);
  startPeerServers();
  if (clientType == ClientType.NIO_CLIENT_MANAGER || clientType == ClientType.BLOCKING_CLIENT_MANAGER) {
    channels.startAsync();
    channels.awaitRunning();
  }
  socketAddress = new InetSocketAddress("127.0.0.1", 1111);
}

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

@Test
  public void testInitialize() throws BlockStoreException {
    final BlockStore blockStore = new MemoryBlockStore(PARAMS);
    final BlockChain chain = new BlockChain(PARAMS, blockStore);

    // Build a historical chain of version 2 blocks
    long timeSeconds = 1231006505;
    StoredBlock chainHead = null;
    for (int height = 0; height < PARAMS.getMajorityWindow(); height++) {
      chainHead = FakeTxBuilder.createFakeBlock(blockStore, 2, timeSeconds, height).storedBlock;
      assertEquals(2, chainHead.getHeader().getVersion());
      timeSeconds += 60;
    }

    VersionTally instance = new VersionTally(PARAMS);
    instance.initialize(blockStore, chainHead);
    assertEquals(PARAMS.getMajorityWindow(), instance.getCountAtOrAbove(2).intValue());
  }
}

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

@Before
public void setUp() throws Exception {
  BriefLogFormatter.initVerbose();
  Context.propagate(new Context(testNet, 100, Coin.ZERO, false));
  testNetChain = new BlockChain(testNet, new Wallet(testNet), new MemoryBlockStore(testNet));
  Context.propagate(new Context(PARAMS, 100, Coin.ZERO, false));
  wallet = new Wallet(PARAMS) {
    @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;
      }
    }
  };
  wallet.freshReceiveKey();
  resetBlockStore();
  chain = new BlockChain(PARAMS, wallet, blockStore);
  coinbaseTo = wallet.currentReceiveKey().toAddress(PARAMS);
}

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

@Override
@Before
public void setUp() throws Exception {
  Utils.setMockClock(); // Use mock clock
  super.setUp();
  Context.propagate(new Context(PARAMS, 100, Coin.ZERO, false));
  wallet.addExtension(new StoredPaymentChannelClientStates(wallet, new TransactionBroadcaster() {
    @Override
    public TransactionBroadcast broadcastTransaction(Transaction tx) {
      fail();
      return null;
    }
  }));
  sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, COIN);
  chain = new BlockChain(PARAMS, wallet, blockStore); // Recreate chain as sendMoneyToWallet will confuse it
  serverWallet = new Wallet(PARAMS);
  serverKey = serverWallet.freshReceiveKey();
  chain.addWallet(serverWallet);
  broadcasts = new LinkedBlockingQueue<>();
  mockBroadcaster = new TransactionBroadcaster() {
    @Override
    public TransactionBroadcast broadcastTransaction(Transaction tx) {
      SettableFuture<Transaction> future = SettableFuture.create();
      broadcasts.add(new TxFuturePair(tx, future));
      return TransactionBroadcast.createMockBroadcast(tx, future);
    }
  };
}

代码示例来源: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

Wallet wallet = Wallet.fromWatchingKeyB58(PARAMS, serialized, 0);
blockStore = new MemoryBlockStore(PARAMS);
chain = new BlockChain(PARAMS, wallet, blockStore);

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

@Before
public void setUp() throws Exception {
  BriefLogFormatter.init();
  Utils.setMockClock(); // Use mock clock
  Context.propagate(new Context(PARAMS, 100, Coin.ZERO, false));
  MemoryBlockStore blockStore = new MemoryBlockStore(PARAMS);
  wallet = new Wallet(PARAMS);
  ECKey key1 = wallet.freshReceiveKey();
  ECKey key2 = wallet.freshReceiveKey();
  chain = new BlockChain(PARAMS, wallet, blockStore);
  coinsTo = key1.toAddress(PARAMS);
  coinsTo2 = key2.toAddress(PARAMS);
  someOtherGuy = new ECKey().toAddress(PARAMS);
}

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

相关文章