コード例 #1
0
ファイル: BlockTest.java プロジェクト: ricky000/bitcoinj
 @Test
 public void testHeaderParse() throws Exception {
   Block block = params.getDefaultSerializer().makeBlock(blockBytes);
   Block header = block.cloneAsHeader();
   Block reparsed = params.getDefaultSerializer().makeBlock(header.bitcoinSerialize());
   assertEquals(reparsed, header);
 }
コード例 #2
0
ファイル: BlockTest.java プロジェクト: ricky000/bitcoinj
 @Test
 public void testProofOfWork() throws Exception {
   // This params accepts any difficulty target.
   NetworkParameters params = UnitTestParams.get();
   Block block = params.getDefaultSerializer().makeBlock(blockBytes);
   block.setNonce(12346);
   try {
     block.verify(Block.BLOCK_HEIGHT_GENESIS, EnumSet.noneOf(Block.VerifyFlag.class));
     fail();
   } catch (VerificationException e) {
     // Expected.
   }
   // Blocks contain their own difficulty target. The BlockChain verification mechanism is what
   // stops real blocks
   // from containing artificially weak difficulties.
   block.setDifficultyTarget(Block.EASIEST_DIFFICULTY_TARGET);
   // Now it should pass.
   block.verify(Block.BLOCK_HEIGHT_GENESIS, EnumSet.noneOf(Block.VerifyFlag.class));
   // Break the nonce again at the lower difficulty level so we can try solving for it.
   block.setNonce(1);
   try {
     block.verify(Block.BLOCK_HEIGHT_GENESIS, EnumSet.noneOf(Block.VerifyFlag.class));
     fail();
   } catch (VerificationException e) {
     // Expected to fail as the nonce is no longer correct.
   }
   // Should find an acceptable nonce.
   block.solve();
   block.verify(Block.BLOCK_HEIGHT_GENESIS, EnumSet.noneOf(Block.VerifyFlag.class));
   assertEquals(block.getNonce(), 2);
 }
コード例 #3
0
  @Test
  public void testLastBlockSeenHash() throws Exception {
    // Test the lastBlockSeenHash field works.

    // LastBlockSeenHash should be empty if never set.
    Wallet wallet = new Wallet(params);
    Protos.Wallet walletProto = new WalletProtobufSerializer().walletToProto(wallet);
    ByteString lastSeenBlockHash = walletProto.getLastSeenBlockHash();
    assertTrue(lastSeenBlockHash.isEmpty());

    // Create a block.
    Block block = params.getDefaultSerializer().makeBlock(BlockTest.blockBytes);
    Sha256Hash blockHash = block.getHash();
    wallet.setLastBlockSeenHash(blockHash);
    wallet.setLastBlockSeenHeight(1);

    // Roundtrip the wallet and check it has stored the blockHash.
    Wallet wallet1 = roundTrip(wallet);
    assertEquals(blockHash, wallet1.getLastBlockSeenHash());
    assertEquals(1, wallet1.getLastBlockSeenHeight());

    // Test the Satoshi genesis block (hash of all zeroes) is roundtripped ok.
    Block genesisBlock = MainNetParams.get().getGenesisBlock();
    wallet.setLastBlockSeenHash(genesisBlock.getHash());
    Wallet wallet2 = roundTrip(wallet);
    assertEquals(genesisBlock.getHash(), wallet2.getLastBlockSeenHash());
  }
コード例 #4
0
ファイル: BlockTest.java プロジェクト: ricky000/bitcoinj
 @Test
 public void testBlockVerification() throws Exception {
   Block block = params.getDefaultSerializer().makeBlock(blockBytes);
   block.verify(Block.BLOCK_HEIGHT_GENESIS, EnumSet.noneOf(Block.VerifyFlag.class));
   assertEquals(
       "00000000a6e5eb79dcec11897af55e90cd571a4335383a3ccfbc12ec81085935",
       block.getHashAsString());
 }
コード例 #5
0
 /**
  * Parse transactions from payment message.
  *
  * @param params network parameters (needed for transaction deserialization)
  * @param paymentMessage payment message to parse
  * @return list of transactions
  */
 public static List<Transaction> parseTransactionsFromPaymentMessage(
     NetworkParameters params, Protos.Payment paymentMessage) {
   final List<Transaction> transactions =
       new ArrayList<Transaction>(paymentMessage.getTransactionsCount());
   for (final ByteString transaction : paymentMessage.getTransactionsList())
     transactions.add(params.getDefaultSerializer().makeTransaction(transaction.toByteArray()));
   return transactions;
 }
コード例 #6
0
ファイル: BlockTest.java プロジェクト: ricky000/bitcoinj
 @Test
 public void testBitcoinSerialization() throws Exception {
   // We have to be able to reserialize everything exactly as we found it for hashing to work. This
   // test also
   // proves that transaction serialization works, along with all its subobjects like scripts and
   // in/outpoints.
   //
   // NB: This tests the bitcoin serialization protocol.
   Block block = params.getDefaultSerializer().makeBlock(blockBytes);
   assertTrue(Arrays.equals(blockBytes, block.bitcoinSerialize()));
 }
コード例 #7
0
 @Override
 public void deserializeWalletExtension(Wallet containingWallet, byte[] data) throws Exception {
   lock.lock();
   try {
     checkState(this.containingWallet == null || this.containingWallet == containingWallet);
     this.containingWallet = containingWallet;
     NetworkParameters params = containingWallet.getParams();
     ClientState.StoredClientPaymentChannels states =
         ClientState.StoredClientPaymentChannels.parseFrom(data);
     for (ClientState.StoredClientPaymentChannel storedState : states.getChannelsList()) {
       Transaction refundTransaction =
           params
               .getDefaultSerializer()
               .makeTransaction(storedState.getRefundTransaction().toByteArray());
       refundTransaction.getConfidence().setSource(TransactionConfidence.Source.SELF);
       ECKey myKey =
           (storedState.getMyKey().isEmpty())
               ? containingWallet.findKeyFromPubKey(storedState.getMyPublicKey().toByteArray())
               : ECKey.fromPrivate(storedState.getMyKey().toByteArray());
       StoredClientChannel channel =
           new StoredClientChannel(
               Sha256Hash.wrap(storedState.getId().toByteArray()),
               params
                   .getDefaultSerializer()
                   .makeTransaction(storedState.getContractTransaction().toByteArray()),
               refundTransaction,
               myKey,
               Coin.valueOf(storedState.getValueToMe()),
               Coin.valueOf(storedState.getRefundFees()),
               false);
       if (storedState.hasCloseTransactionHash()) {
         Sha256Hash closeTxHash =
             Sha256Hash.wrap(storedState.getCloseTransactionHash().toByteArray());
         channel.close = containingWallet.getTransaction(closeTxHash);
       }
       putChannel(channel, false);
     }
   } finally {
     lock.unlock();
   }
 }
コード例 #8
0
ファイル: BlockTest.java プロジェクト: ricky000/bitcoinj
 @Test
 public void testBadTransactions() throws Exception {
   Block block = params.getDefaultSerializer().makeBlock(blockBytes);
   // Re-arrange so the coinbase transaction is not first.
   Transaction tx1 = block.transactions.get(0);
   Transaction tx2 = block.transactions.get(1);
   block.transactions.set(0, tx2);
   block.transactions.set(1, tx1);
   try {
     block.verify(Block.BLOCK_HEIGHT_GENESIS, EnumSet.noneOf(Block.VerifyFlag.class));
     fail();
   } catch (VerificationException e) {
     // We should get here.
   }
 }
コード例 #9
0
ファイル: BlockTest.java プロジェクト: ricky000/bitcoinj
  @Test
  public void testReceiveCoinbaseTransaction() throws Exception {
    // Block 169482 (hash 0000000000000756935f1ee9d5987857b604046f846d3df56d024cdb5f368665)
    // contains coinbase transactions that are mining pool shares.
    // The private key MINERS_KEY is used to check transactions are received by a wallet correctly.

    // The address for this private key is 1GqtGtn4fctXuKxsVzRPSLmYWN1YioLi9y.
    final String MINING_PRIVATE_KEY = "5JDxPrBRghF1EvSBjDigywqfmAjpHPmTJxYtQTYJxJRHLLQA4mG";

    final long BLOCK_NONCE = 3973947400L;
    final Coin BALANCE_AFTER_BLOCK = Coin.valueOf(22223642);
    final NetworkParameters PARAMS = MainNetParams.get();

    Block block169482 =
        PARAMS
            .getDefaultSerializer()
            .makeBlock(ByteStreams.toByteArray(getClass().getResourceAsStream("block169482.dat")));

    // Check block.
    assertNotNull(block169482);
    block169482.verify(169482, EnumSet.noneOf(Block.VerifyFlag.class));
    assertEquals(BLOCK_NONCE, block169482.getNonce());

    StoredBlock storedBlock =
        new StoredBlock(block169482, BigInteger.ONE, 169482); // Nonsense work - not used in test.

    // Create a wallet contain the miner's key that receives a spend from a coinbase.
    ECKey miningKey = DumpedPrivateKey.fromBase58(PARAMS, MINING_PRIVATE_KEY).getKey();
    assertNotNull(miningKey);
    Context context = new Context(PARAMS);
    Wallet wallet = new Wallet(context);
    wallet.importKey(miningKey);

    // Initial balance should be zero by construction.
    assertEquals(Coin.ZERO, wallet.getBalance());

    // Give the wallet the first transaction in the block - this is the coinbase tx.
    List<Transaction> transactions = block169482.getTransactions();
    assertNotNull(transactions);
    wallet.receiveFromBlock(transactions.get(0), storedBlock, NewBlockType.BEST_CHAIN, 0);

    // Coinbase transaction should have been received successfully but be unavailable to spend (too
    // young).
    assertEquals(BALANCE_AFTER_BLOCK, wallet.getBalance(BalanceType.ESTIMATED));
    assertEquals(Coin.ZERO, wallet.getBalance(BalanceType.AVAILABLE));
  }
コード例 #10
0
 public PeerSocketHandler(NetworkParameters params, PeerAddress peerAddress) {
   checkNotNull(params);
   serializer = params.getDefaultSerializer();
   this.peerAddress = checkNotNull(peerAddress);
 }
コード例 #11
0
 public PeerSocketHandler(NetworkParameters params, InetSocketAddress remoteIp) {
   checkNotNull(params);
   serializer = params.getDefaultSerializer();
   this.peerAddress = new PeerAddress(remoteIp);
 }
コード例 #12
0
ファイル: BlockTest.java プロジェクト: ricky000/bitcoinj
 @SuppressWarnings("deprecation")
 @Test
 public void testDate() throws Exception {
   Block block = params.getDefaultSerializer().makeBlock(blockBytes);
   assertEquals("4 Nov 2010 16:06:04 GMT", block.getTime().toGMTString());
 }