mirror of
https://github.com/bitcoinj/bitcoinj.git
synced 2024-11-19 18:00:39 +01:00
BlockChainTest: use TestNet3Params
exclusively, except two tests
`difficultyTransitions()` uses `UnitTestParams` only. `estimatedBlockTime()` uses `MainNetParams` only.
This commit is contained in:
parent
47ea55e1a6
commit
a61761c5ab
@ -64,29 +64,22 @@ public class BlockChainTest {
|
|||||||
@Rule
|
@Rule
|
||||||
public ExpectedException thrown = ExpectedException.none();
|
public ExpectedException thrown = ExpectedException.none();
|
||||||
|
|
||||||
|
private Wallet testNetWallet;
|
||||||
|
private MemoryBlockStore testNetStore;
|
||||||
private BlockChain testNetChain;
|
private BlockChain testNetChain;
|
||||||
|
|
||||||
private Wallet wallet;
|
|
||||||
private BlockChain unitTestChain;
|
|
||||||
private BlockStore unitTestStore;
|
|
||||||
private Address coinbaseTo;
|
private Address coinbaseTo;
|
||||||
|
|
||||||
private static final TestNet3Params TESTNET = TestNet3Params.get();
|
private static final TestNet3Params TESTNET = TestNet3Params.get();
|
||||||
private static final NetworkParameters UNITTEST = UnitTestParams.get();
|
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() throws Exception {
|
public void setUp() throws Exception {
|
||||||
BriefLogFormatter.initVerbose();
|
BriefLogFormatter.initVerbose();
|
||||||
TimeUtils.setMockClock(); // Use mock clock
|
TimeUtils.setMockClock(); // Use mock clock
|
||||||
Context.propagate(new Context(100, Coin.ZERO, false, false));
|
Context.propagate(new Context(100, Coin.ZERO, false, false));
|
||||||
testNetChain = new BlockChain(TESTNET, Wallet.createDeterministic(TESTNET, ScriptType.P2PKH), new MemoryBlockStore(TESTNET.getGenesisBlock()));
|
testNetWallet = Wallet.createDeterministic(TESTNET, ScriptType.P2PKH);
|
||||||
Context.propagate(new Context(100, Coin.ZERO, false, false));
|
testNetStore = new MemoryBlockStore(TESTNET.getGenesisBlock());
|
||||||
wallet = Wallet.createDeterministic(TESTNET, ScriptType.P2PKH);
|
testNetChain = new BlockChain(TESTNET, testNetWallet, testNetStore);
|
||||||
wallet.freshReceiveKey();
|
coinbaseTo = testNetWallet.currentReceiveKey().toAddress(ScriptType.P2PKH, BitcoinNetwork.TESTNET);
|
||||||
coinbaseTo = wallet.currentReceiveKey().toAddress(ScriptType.P2PKH, BitcoinNetwork.TESTNET);
|
|
||||||
|
|
||||||
unitTestStore = new MemoryBlockStore(UNITTEST.getGenesisBlock());
|
|
||||||
unitTestChain = new BlockChain(UNITTEST, wallet, unitTestStore);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -118,33 +111,40 @@ public class BlockChainTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void receiveCoins() throws Exception {
|
public void receiveCoins() throws Exception {
|
||||||
|
Context.propagate(new Context(100, Coin.ZERO, false, true));
|
||||||
int height = 1;
|
int height = 1;
|
||||||
// Quick check that we can actually receive coins.
|
// Quick check that we can actually receive coins.
|
||||||
Transaction tx1 = createFakeTx(TESTNET,
|
Transaction tx1 = createFakeTx(TESTNET,
|
||||||
COIN,
|
COIN,
|
||||||
wallet.currentReceiveKey().toAddress(ScriptType.P2PKH, BitcoinNetwork.TESTNET));
|
testNetWallet.currentReceiveKey().toAddress(ScriptType.P2PKH, BitcoinNetwork.TESTNET));
|
||||||
Block b1 = createFakeBlock(unitTestStore, height, tx1).block;
|
Block b1 = createFakeBlock(testNetStore, height, tx1).block;
|
||||||
unitTestChain.add(b1);
|
testNetChain.add(b1);
|
||||||
assertTrue(wallet.getBalance().signum() > 0);
|
assertTrue(testNetWallet.getBalance().signum() > 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void unconnectedBlocks() throws Exception {
|
public void unconnectedBlocks() throws Exception {
|
||||||
Block b1 = UNITTEST.getGenesisBlock().createNextBlock(coinbaseTo);
|
Context.propagate(new Context(100, Coin.ZERO, false, true));
|
||||||
|
Block b1 = TESTNET.getGenesisBlock().createNextBlock(coinbaseTo);
|
||||||
Block b2 = b1.createNextBlock(coinbaseTo);
|
Block b2 = b1.createNextBlock(coinbaseTo);
|
||||||
Block b3 = b2.createNextBlock(coinbaseTo);
|
Block b3 = b2.createNextBlock(coinbaseTo);
|
||||||
// Connected.
|
// Connected.
|
||||||
assertTrue(unitTestChain.add(b1));
|
assertTrue(testNetChain.add(b1));
|
||||||
// Unconnected but stored. The head of the chain is still b1.
|
// Unconnected but stored. The head of the chain is still b1.
|
||||||
assertFalse(unitTestChain.add(b3));
|
assertFalse(testNetChain.add(b3));
|
||||||
assertEquals(unitTestChain.getChainHead().getHeader(), b1.cloneAsHeader());
|
assertEquals(testNetChain.getChainHead().getHeader(), b1.cloneAsHeader());
|
||||||
// Add in the middle block.
|
// Add in the middle block.
|
||||||
assertTrue(unitTestChain.add(b2));
|
assertTrue(testNetChain.add(b2));
|
||||||
assertEquals(unitTestChain.getChainHead().getHeader(), b3.cloneAsHeader());
|
assertEquals(testNetChain.getChainHead().getHeader(), b3.cloneAsHeader());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void difficultyTransitions() throws Exception {
|
public void difficultyTransitions() throws Exception {
|
||||||
|
NetworkParameters UNITTEST = UnitTestParams.get();
|
||||||
|
BlockChain unitTestChain = new BlockChain(UNITTEST,
|
||||||
|
Wallet.createDeterministic(UNITTEST, ScriptType.P2PKH),
|
||||||
|
new MemoryBlockStore(UNITTEST.getGenesisBlock()));
|
||||||
|
|
||||||
// Add a bunch of blocks in a loop until we reach a difficulty transition point. The unit test params have an
|
// Add a bunch of blocks in a loop until we reach a difficulty transition point. The unit test params have an
|
||||||
// artificially shortened period.
|
// artificially shortened period.
|
||||||
Block prev = UNITTEST.getGenesisBlock();
|
Block prev = UNITTEST.getGenesisBlock();
|
||||||
@ -239,8 +239,7 @@ public class BlockChainTest {
|
|||||||
|
|
||||||
private void testDeprecatedBlockVersion(final long deprecatedVersion, final long newVersion)
|
private void testDeprecatedBlockVersion(final long deprecatedVersion, final long newVersion)
|
||||||
throws Exception {
|
throws Exception {
|
||||||
final BlockStore versionBlockStore = new MemoryBlockStore(UNITTEST.getGenesisBlock());
|
Context.propagate(new Context(100, Coin.ZERO, false, true));
|
||||||
final BlockChain versionChain = new BlockChain(UNITTEST, versionBlockStore);
|
|
||||||
|
|
||||||
// Build a historical chain of version 3 blocks
|
// Build a historical chain of version 3 blocks
|
||||||
Instant time = Instant.ofEpochSecond(1231006505);
|
Instant time = Instant.ofEpochSecond(1231006505);
|
||||||
@ -248,23 +247,23 @@ public class BlockChainTest {
|
|||||||
FakeTxBuilder.BlockPair chainHead = null;
|
FakeTxBuilder.BlockPair chainHead = null;
|
||||||
|
|
||||||
// Put in just enough v2 blocks to be a minority
|
// Put in just enough v2 blocks to be a minority
|
||||||
for (height = 0; height < (UNITTEST.getMajorityWindow() - UNITTEST.getMajorityRejectBlockOutdated()); height++) {
|
for (height = 0; height < (TESTNET.getMajorityWindow() - TESTNET.getMajorityRejectBlockOutdated()); height++) {
|
||||||
chainHead = FakeTxBuilder.createFakeBlock(versionBlockStore, deprecatedVersion, time, height);
|
chainHead = FakeTxBuilder.createFakeBlock(testNetStore, deprecatedVersion, time, height);
|
||||||
versionChain.add(chainHead.block);
|
testNetChain.add(chainHead.block);
|
||||||
time = time.plus(1, ChronoUnit.MINUTES);
|
time = time.plus(1, ChronoUnit.MINUTES);
|
||||||
}
|
}
|
||||||
// Fill the rest of the window with v3 blocks
|
// Fill the rest of the window with v3 blocks
|
||||||
for (; height < UNITTEST.getMajorityWindow(); height++) {
|
for (; height < TESTNET.getMajorityWindow(); height++) {
|
||||||
chainHead = FakeTxBuilder.createFakeBlock(versionBlockStore, newVersion, time, height);
|
chainHead = FakeTxBuilder.createFakeBlock(testNetStore, newVersion, time, height);
|
||||||
versionChain.add(chainHead.block);
|
testNetChain.add(chainHead.block);
|
||||||
time = time.plus(1, ChronoUnit.MINUTES);
|
time = time.plus(1, ChronoUnit.MINUTES);
|
||||||
}
|
}
|
||||||
|
|
||||||
chainHead = FakeTxBuilder.createFakeBlock(versionBlockStore, deprecatedVersion, time, height);
|
chainHead = FakeTxBuilder.createFakeBlock(testNetStore, deprecatedVersion, time, height);
|
||||||
// Trying to add a new v2 block should result in rejection
|
// Trying to add a new v2 block should result in rejection
|
||||||
thrown.expect(VerificationException.BlockVersionOutOfDate.class);
|
thrown.expect(VerificationException.BlockVersionOutOfDate.class);
|
||||||
try {
|
try {
|
||||||
versionChain.add(chainHead.block);
|
testNetChain.add(chainHead.block);
|
||||||
} catch(final VerificationException ex) {
|
} catch(final VerificationException ex) {
|
||||||
throw (Exception) ex.getCause();
|
throw (Exception) ex.getCause();
|
||||||
}
|
}
|
||||||
@ -272,27 +271,29 @@ public class BlockChainTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void duplicates() throws Exception {
|
public void duplicates() throws Exception {
|
||||||
|
Context.propagate(new Context(100, Coin.ZERO, false, true));
|
||||||
// Adding a block twice should not have any effect, in particular it should not send the block to the wallet.
|
// Adding a block twice should not have any effect, in particular it should not send the block to the wallet.
|
||||||
Block b1 = UNITTEST.getGenesisBlock().createNextBlock(coinbaseTo);
|
Block b1 = TESTNET.getGenesisBlock().createNextBlock(coinbaseTo);
|
||||||
Block b2 = b1.createNextBlock(coinbaseTo);
|
Block b2 = b1.createNextBlock(coinbaseTo);
|
||||||
Block b3 = b2.createNextBlock(coinbaseTo);
|
Block b3 = b2.createNextBlock(coinbaseTo);
|
||||||
assertTrue(unitTestChain.add(b1));
|
assertTrue(testNetChain.add(b1));
|
||||||
assertEquals(b1, unitTestChain.getChainHead().getHeader());
|
assertEquals(b1, testNetChain.getChainHead().getHeader());
|
||||||
assertTrue(unitTestChain.add(b2));
|
assertTrue(testNetChain.add(b2));
|
||||||
assertEquals(b2, unitTestChain.getChainHead().getHeader());
|
assertEquals(b2, testNetChain.getChainHead().getHeader());
|
||||||
assertTrue(unitTestChain.add(b3));
|
assertTrue(testNetChain.add(b3));
|
||||||
assertEquals(b3, unitTestChain.getChainHead().getHeader());
|
assertEquals(b3, testNetChain.getChainHead().getHeader());
|
||||||
assertTrue(unitTestChain.add(b2)); // add old block
|
assertTrue(testNetChain.add(b2)); // add old block
|
||||||
assertEquals(b3, unitTestChain.getChainHead().getHeader()); // block didn't change, duplicate was spotted
|
assertEquals(b3, testNetChain.getChainHead().getHeader()); // block didn't change, duplicate was spotted
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void intraBlockDependencies() throws Exception {
|
public void intraBlockDependencies() throws Exception {
|
||||||
|
Context.propagate(new Context(100, Coin.ZERO, false, true));
|
||||||
// Covers issue 166 in which transactions that depend on each other inside a block were not always being
|
// Covers issue 166 in which transactions that depend on each other inside a block were not always being
|
||||||
// considered relevant.
|
// considered relevant.
|
||||||
Address somebodyElse = new ECKey().toAddress(ScriptType.P2PKH, BitcoinNetwork.TESTNET);
|
Address somebodyElse = new ECKey().toAddress(ScriptType.P2PKH, BitcoinNetwork.TESTNET);
|
||||||
Block b1 = UNITTEST.getGenesisBlock().createNextBlock(somebodyElse);
|
Block b1 = TESTNET.getGenesisBlock().createNextBlock(somebodyElse);
|
||||||
ECKey key = wallet.freshReceiveKey();
|
ECKey key = testNetWallet.freshReceiveKey();
|
||||||
Address addr = key.toAddress(ScriptType.P2PKH, BitcoinNetwork.TESTNET);
|
Address addr = key.toAddress(ScriptType.P2PKH, BitcoinNetwork.TESTNET);
|
||||||
// Create a tx that gives us some coins, and another that spends it to someone else in the same block.
|
// Create a tx that gives us some coins, and another that spends it to someone else in the same block.
|
||||||
Transaction t1 = FakeTxBuilder.createFakeTx(TESTNET, COIN, addr);
|
Transaction t1 = FakeTxBuilder.createFakeTx(TESTNET, COIN, addr);
|
||||||
@ -302,60 +303,61 @@ public class BlockChainTest {
|
|||||||
b1.addTransaction(t1);
|
b1.addTransaction(t1);
|
||||||
b1.addTransaction(t2);
|
b1.addTransaction(t2);
|
||||||
b1.solve();
|
b1.solve();
|
||||||
unitTestChain.add(b1);
|
testNetChain.add(b1);
|
||||||
assertEquals(Coin.ZERO, wallet.getBalance());
|
assertEquals(Coin.ZERO, testNetWallet.getBalance());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void coinbaseTransactionAvailability() throws Exception {
|
public void coinbaseTransactionAvailability() throws Exception {
|
||||||
|
Context.propagate(new Context(100, Coin.ZERO, false, true));
|
||||||
// Check that a coinbase transaction is only available to spend after NetworkParameters.getSpendableCoinbaseDepth() blocks.
|
// Check that a coinbase transaction is only available to spend after NetworkParameters.getSpendableCoinbaseDepth() blocks.
|
||||||
|
|
||||||
// Create a second wallet to receive the coinbase spend.
|
// Create a second wallet to receive the coinbase spend.
|
||||||
Wallet wallet2 = Wallet.createDeterministic(TESTNET, ScriptType.P2PKH);
|
Wallet wallet2 = Wallet.createDeterministic(TESTNET, ScriptType.P2PKH);
|
||||||
ECKey receiveKey = wallet2.freshReceiveKey();
|
ECKey receiveKey = wallet2.freshReceiveKey();
|
||||||
int height = 1;
|
int height = 1;
|
||||||
unitTestChain.addWallet(wallet2);
|
testNetChain.addWallet(wallet2);
|
||||||
|
|
||||||
Address addressToSendTo = receiveKey.toAddress(ScriptType.P2PKH, BitcoinNetwork.TESTNET);
|
Address addressToSendTo = receiveKey.toAddress(ScriptType.P2PKH, BitcoinNetwork.TESTNET);
|
||||||
|
|
||||||
// Create a block, sending the coinbase to the coinbaseTo address (which is in the wallet).
|
// Create a block, sending the coinbase to the coinbaseTo address (which is in the wallet).
|
||||||
Block b1 = UNITTEST.getGenesisBlock().createNextBlockWithCoinbase(Block.BLOCK_VERSION_GENESIS, wallet.currentReceiveKey().getPubKey(), height++);
|
Block b1 = TESTNET.getGenesisBlock().createNextBlockWithCoinbase(Block.BLOCK_VERSION_GENESIS, testNetWallet.currentReceiveKey().getPubKey(), height++);
|
||||||
unitTestChain.add(b1);
|
testNetChain.add(b1);
|
||||||
final Transaction coinbaseTransaction = b1.getTransactions().get(0);
|
final Transaction coinbaseTransaction = b1.getTransactions().get(0);
|
||||||
|
|
||||||
// Check a transaction has been received.
|
// Check a transaction has been received.
|
||||||
assertNotNull(coinbaseTransaction);
|
assertNotNull(coinbaseTransaction);
|
||||||
|
|
||||||
// The coinbase tx is not yet available to spend.
|
// The coinbase tx is not yet available to spend.
|
||||||
assertEquals(Coin.ZERO, wallet.getBalance());
|
assertEquals(Coin.ZERO, testNetWallet.getBalance());
|
||||||
assertEquals(FIFTY_COINS, wallet.getBalance(BalanceType.ESTIMATED));
|
assertEquals(FIFTY_COINS, testNetWallet.getBalance(BalanceType.ESTIMATED));
|
||||||
assertFalse(coinbaseTransaction.isMature());
|
assertFalse(coinbaseTransaction.isMature());
|
||||||
|
|
||||||
// Attempt to spend the coinbase - this should fail as the coinbase is not mature yet.
|
// Attempt to spend the coinbase - this should fail as the coinbase is not mature yet.
|
||||||
try {
|
try {
|
||||||
wallet.createSend(addressToSendTo, valueOf(49, 0));
|
testNetWallet.createSend(addressToSendTo, valueOf(49, 0));
|
||||||
fail();
|
fail();
|
||||||
} catch (InsufficientMoneyException e) {
|
} catch (InsufficientMoneyException e) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check that the coinbase is unavailable to spend for the next spendableCoinbaseDepth - 2 blocks.
|
// Check that the coinbase is unavailable to spend for the next spendableCoinbaseDepth - 2 blocks.
|
||||||
for (int i = 0; i < UNITTEST.getSpendableCoinbaseDepth() - 2; i++) {
|
for (int i = 0; i < TESTNET.getSpendableCoinbaseDepth() - 2; i++) {
|
||||||
// Non relevant tx - just for fake block creation.
|
// Non relevant tx - just for fake block creation.
|
||||||
Transaction tx2 = createFakeTx(TESTNET, COIN, new ECKey().toAddress(ScriptType.P2PKH, TESTNET.network()));
|
Transaction tx2 = createFakeTx(TESTNET, COIN, new ECKey().toAddress(ScriptType.P2PKH, TESTNET.network()));
|
||||||
|
|
||||||
Block b2 = createFakeBlock(unitTestStore, height++, tx2).block;
|
Block b2 = createFakeBlock(testNetStore, height++, tx2).block;
|
||||||
unitTestChain.add(b2);
|
testNetChain.add(b2);
|
||||||
|
|
||||||
// Wallet still does not have the coinbase transaction available for spend.
|
// Wallet still does not have the coinbase transaction available for spend.
|
||||||
assertEquals(Coin.ZERO, wallet.getBalance());
|
assertEquals(Coin.ZERO, testNetWallet.getBalance());
|
||||||
assertEquals(FIFTY_COINS, wallet.getBalance(BalanceType.ESTIMATED));
|
assertEquals(FIFTY_COINS, testNetWallet.getBalance(BalanceType.ESTIMATED));
|
||||||
|
|
||||||
// The coinbase transaction is still not mature.
|
// The coinbase transaction is still not mature.
|
||||||
assertFalse(coinbaseTransaction.isMature());
|
assertFalse(coinbaseTransaction.isMature());
|
||||||
|
|
||||||
// Attempt to spend the coinbase - this should fail.
|
// Attempt to spend the coinbase - this should fail.
|
||||||
try {
|
try {
|
||||||
wallet.createSend(addressToSendTo, valueOf(49, 0));
|
testNetWallet.createSend(addressToSendTo, valueOf(49, 0));
|
||||||
fail();
|
fail();
|
||||||
} catch (InsufficientMoneyException e) {
|
} catch (InsufficientMoneyException e) {
|
||||||
}
|
}
|
||||||
@ -363,28 +365,28 @@ public class BlockChainTest {
|
|||||||
|
|
||||||
// Give it one more block - should now be able to spend coinbase transaction. Non relevant tx.
|
// Give it one more block - should now be able to spend coinbase transaction. Non relevant tx.
|
||||||
Transaction tx3 = createFakeTx(TESTNET, COIN, new ECKey().toAddress(ScriptType.P2PKH, TESTNET.network()));
|
Transaction tx3 = createFakeTx(TESTNET, COIN, new ECKey().toAddress(ScriptType.P2PKH, TESTNET.network()));
|
||||||
Block b3 = createFakeBlock(unitTestStore, height++, tx3).block;
|
Block b3 = createFakeBlock(testNetStore, height++, tx3).block;
|
||||||
unitTestChain.add(b3);
|
testNetChain.add(b3);
|
||||||
|
|
||||||
// Wallet now has the coinbase transaction available for spend.
|
// Wallet now has the coinbase transaction available for spend.
|
||||||
assertEquals(FIFTY_COINS, wallet.getBalance());
|
assertEquals(FIFTY_COINS, testNetWallet.getBalance());
|
||||||
assertEquals(FIFTY_COINS, wallet.getBalance(BalanceType.ESTIMATED));
|
assertEquals(FIFTY_COINS, testNetWallet.getBalance(BalanceType.ESTIMATED));
|
||||||
assertTrue(coinbaseTransaction.isMature());
|
assertTrue(coinbaseTransaction.isMature());
|
||||||
|
|
||||||
// Create a spend with the coinbase BTC to the address in the second wallet - this should now succeed.
|
// Create a spend with the coinbase BTC to the address in the second wallet - this should now succeed.
|
||||||
Transaction coinbaseSend2 = wallet.createSend(addressToSendTo, valueOf(49, 0));
|
Transaction coinbaseSend2 = testNetWallet.createSend(addressToSendTo, valueOf(49, 0));
|
||||||
assertNotNull(coinbaseSend2);
|
assertNotNull(coinbaseSend2);
|
||||||
|
|
||||||
// Commit the coinbaseSpend to the first wallet and check the balances decrement.
|
// Commit the coinbaseSpend to the first wallet and check the balances decrement.
|
||||||
wallet.commitTx(coinbaseSend2);
|
testNetWallet.commitTx(coinbaseSend2);
|
||||||
assertEquals(COIN, wallet.getBalance(BalanceType.ESTIMATED));
|
assertEquals(COIN, testNetWallet.getBalance(BalanceType.ESTIMATED));
|
||||||
// Available balance is zero as change has not been received from a block yet.
|
// Available balance is zero as change has not been received from a block yet.
|
||||||
assertEquals(ZERO, wallet.getBalance(BalanceType.AVAILABLE));
|
assertEquals(ZERO, testNetWallet.getBalance(BalanceType.AVAILABLE));
|
||||||
|
|
||||||
// Give it one more block - change from coinbaseSpend should now be available in the first wallet.
|
// Give it one more block - change from coinbaseSpend should now be available in the first wallet.
|
||||||
Block b4 = createFakeBlock(unitTestStore, height++, coinbaseSend2).block;
|
Block b4 = createFakeBlock(testNetStore, height++, coinbaseSend2).block;
|
||||||
unitTestChain.add(b4);
|
testNetChain.add(b4);
|
||||||
assertEquals(COIN, wallet.getBalance(BalanceType.AVAILABLE));
|
assertEquals(COIN, testNetWallet.getBalance(BalanceType.AVAILABLE));
|
||||||
|
|
||||||
// Check the balances in the second wallet.
|
// Check the balances in the second wallet.
|
||||||
assertEquals(valueOf(49, 0), wallet2.getBalance(BalanceType.ESTIMATED));
|
assertEquals(valueOf(49, 0), wallet2.getBalance(BalanceType.ESTIMATED));
|
||||||
@ -429,56 +431,57 @@ public class BlockChainTest {
|
|||||||
@Test
|
@Test
|
||||||
public void falsePositives() {
|
public void falsePositives() {
|
||||||
double decay = AbstractBlockChain.FP_ESTIMATOR_ALPHA;
|
double decay = AbstractBlockChain.FP_ESTIMATOR_ALPHA;
|
||||||
assertTrue(0 == unitTestChain.getFalsePositiveRate()); // Exactly
|
assertTrue(0 == testNetChain.getFalsePositiveRate()); // Exactly
|
||||||
unitTestChain.trackFalsePositives(55);
|
testNetChain.trackFalsePositives(55);
|
||||||
assertEquals(decay * 55, unitTestChain.getFalsePositiveRate(), 1e-4);
|
assertEquals(decay * 55, testNetChain.getFalsePositiveRate(), 1e-4);
|
||||||
unitTestChain.trackFilteredTransactions(550);
|
testNetChain.trackFilteredTransactions(550);
|
||||||
double rate1 = unitTestChain.getFalsePositiveRate();
|
double rate1 = testNetChain.getFalsePositiveRate();
|
||||||
// Run this scenario a few more time for the filter to converge
|
// Run this scenario a few more time for the filter to converge
|
||||||
for (int i = 1 ; i < 10 ; i++) {
|
for (int i = 1 ; i < 10 ; i++) {
|
||||||
unitTestChain.trackFalsePositives(55);
|
testNetChain.trackFalsePositives(55);
|
||||||
unitTestChain.trackFilteredTransactions(550);
|
testNetChain.trackFilteredTransactions(550);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensure we are within 10%
|
// Ensure we are within 10%
|
||||||
assertEquals(0.1, unitTestChain.getFalsePositiveRate(), 0.01);
|
assertEquals(0.1, testNetChain.getFalsePositiveRate(), 0.01);
|
||||||
|
|
||||||
// Check that we get repeatable results after a reset
|
// Check that we get repeatable results after a reset
|
||||||
unitTestChain.resetFalsePositiveEstimate();
|
testNetChain.resetFalsePositiveEstimate();
|
||||||
assertTrue(0 == unitTestChain.getFalsePositiveRate()); // Exactly
|
assertTrue(0 == testNetChain.getFalsePositiveRate()); // Exactly
|
||||||
|
|
||||||
unitTestChain.trackFalsePositives(55);
|
testNetChain.trackFalsePositives(55);
|
||||||
assertEquals(decay * 55, unitTestChain.getFalsePositiveRate(), 1e-4);
|
assertEquals(decay * 55, testNetChain.getFalsePositiveRate(), 1e-4);
|
||||||
unitTestChain.trackFilteredTransactions(550);
|
testNetChain.trackFilteredTransactions(550);
|
||||||
assertEquals(rate1, unitTestChain.getFalsePositiveRate(), 1e-4);
|
assertEquals(rate1, testNetChain.getFalsePositiveRate(), 1e-4);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void rollbackBlockStore() throws Exception {
|
public void rollbackBlockStore() throws Exception {
|
||||||
|
Context.propagate(new Context(100, Coin.ZERO, false, true));
|
||||||
// This test simulates an issue on Android, that causes the VM to crash while receiving a block, so that the
|
// 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 store is persisted but the wallet is not.
|
||||||
Block b1 = UNITTEST.getGenesisBlock().createNextBlock(coinbaseTo);
|
Block b1 = TESTNET.getGenesisBlock().createNextBlock(coinbaseTo);
|
||||||
Block b2 = b1.createNextBlock(coinbaseTo);
|
Block b2 = b1.createNextBlock(coinbaseTo);
|
||||||
// Add block 1, no frills.
|
// Add block 1, no frills.
|
||||||
assertTrue(unitTestChain.add(b1));
|
assertTrue(testNetChain.add(b1));
|
||||||
assertEquals(b1.cloneAsHeader(), unitTestChain.getChainHead().getHeader());
|
assertEquals(b1.cloneAsHeader(), testNetChain.getChainHead().getHeader());
|
||||||
assertEquals(1, unitTestChain.getBestChainHeight());
|
assertEquals(1, testNetChain.getBestChainHeight());
|
||||||
assertEquals(1, wallet.getLastBlockSeenHeight());
|
assertEquals(1, testNetWallet.getLastBlockSeenHeight());
|
||||||
// Add block 2 while wallet is disconnected, to simulate crash.
|
// Add block 2 while wallet is disconnected, to simulate crash.
|
||||||
unitTestChain.removeWallet(wallet);
|
testNetChain.removeWallet(testNetWallet);
|
||||||
assertTrue(unitTestChain.add(b2));
|
assertTrue(testNetChain.add(b2));
|
||||||
assertEquals(b2.cloneAsHeader(), unitTestChain.getChainHead().getHeader());
|
assertEquals(b2.cloneAsHeader(), testNetChain.getChainHead().getHeader());
|
||||||
assertEquals(2, unitTestChain.getBestChainHeight());
|
assertEquals(2, testNetChain.getBestChainHeight());
|
||||||
assertEquals(1, wallet.getLastBlockSeenHeight());
|
assertEquals(1, testNetWallet.getLastBlockSeenHeight());
|
||||||
// Add wallet back. This will detect the height mismatch and repair the damage done.
|
// Add wallet back. This will detect the height mismatch and repair the damage done.
|
||||||
unitTestChain.addWallet(wallet);
|
testNetChain.addWallet(testNetWallet);
|
||||||
assertEquals(b1.cloneAsHeader(), unitTestChain.getChainHead().getHeader());
|
assertEquals(b1.cloneAsHeader(), testNetChain.getChainHead().getHeader());
|
||||||
assertEquals(1, unitTestChain.getBestChainHeight());
|
assertEquals(1, testNetChain.getBestChainHeight());
|
||||||
assertEquals(1, wallet.getLastBlockSeenHeight());
|
assertEquals(1, testNetWallet.getLastBlockSeenHeight());
|
||||||
// Now add block 2 correctly.
|
// Now add block 2 correctly.
|
||||||
assertTrue(unitTestChain.add(b2));
|
assertTrue(testNetChain.add(b2));
|
||||||
assertEquals(b2.cloneAsHeader(), unitTestChain.getChainHead().getHeader());
|
assertEquals(b2.cloneAsHeader(), testNetChain.getChainHead().getHeader());
|
||||||
assertEquals(2, unitTestChain.getBestChainHeight());
|
assertEquals(2, testNetChain.getBestChainHeight());
|
||||||
assertEquals(2, wallet.getLastBlockSeenHeight());
|
assertEquals(2, testNetWallet.getLastBlockSeenHeight());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user