Fold CoinbaseBlockTest into BlockTest.

This commit is contained in:
Andreas Schildbach
2015-11-23 16:39:05 +01:00
parent 723df867ff
commit fe736d2554
2 changed files with 47 additions and 87 deletions

View File

@@ -18,6 +18,10 @@
package org.bitcoinj.core;
import com.google.common.io.ByteStreams;
import org.bitcoinj.core.AbstractBlockChain.NewBlockType;
import org.bitcoinj.core.Wallet.BalanceType;
import org.bitcoinj.params.MainNetParams;
import org.bitcoinj.params.TestNet2Params;
import org.bitcoinj.params.TestNet3Params;
import org.bitcoinj.params.UnitTestParams;
@@ -28,6 +32,7 @@ import org.junit.Test;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.List;
import static org.bitcoinj.core.Utils.HEX;
import static org.junit.Assert.*;
@@ -194,4 +199,46 @@ public class BlockTest {
assertEquals("000000007590ba495b58338a5806c2b6f10af921a70dbd814e0da3c6957c0c03", block.getHashAsString());
block.verify(32768, EnumSet.of(Block.VerifyFlag.HEIGHT_IN_COINBASE));
}
@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));
}
}

View File

@@ -1,87 +0,0 @@
/**
* Copyright 2012 Google Inc.
* Copyright 2015 Andreas Schildbach
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.bitcoinj.core;
import org.bitcoinj.core.AbstractBlockChain.NewBlockType;
import org.bitcoinj.core.Wallet.BalanceType;
import org.bitcoinj.params.MainNetParams;
import org.junit.Before;
import org.junit.Test;
import com.google.common.io.ByteStreams;
import java.math.BigInteger;
import java.util.EnumSet;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
/**
* Test that an example mainnet coinbase transactions can be added to a wallet ok.
*/
public class CoinbaseBlockTest {
static final NetworkParameters params = MainNetParams.get();
// The address for this private key is 1GqtGtn4fctXuKxsVzRPSLmYWN1YioLi9y.
private static final String MINING_PRIVATE_KEY = "5JDxPrBRghF1EvSBjDigywqfmAjpHPmTJxYtQTYJxJRHLLQA4mG";
private static final int BLOCK_OF_INTEREST = 169482;
private static final long BLOCK_NONCE = 3973947400L;
private static final Coin BALANCE_AFTER_BLOCK = Coin.valueOf(22223642);
@Before
public void setUp() throws Exception {
Context context = new Context(params);
}
@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.
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, BLOCK_OF_INTEREST); // 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));
}
}