mirror of
https://github.com/Qortal/altcoinj.git
synced 2025-02-07 23:03:04 +00:00
Store blocks on disk during BitcoindComparisonTool to save memory.
This commit is contained in:
parent
a131d08a58
commit
729f2f5312
@ -18,14 +18,21 @@ package com.google.bitcoin.core;
|
|||||||
|
|
||||||
import com.google.bitcoin.store.BlockStoreException;
|
import com.google.bitcoin.store.BlockStoreException;
|
||||||
import com.google.bitcoin.store.FullPrunedBlockStore;
|
import com.google.bitcoin.store.FullPrunedBlockStore;
|
||||||
|
import com.google.bitcoin.store.H2FullPrunedBlockStore;
|
||||||
import com.google.bitcoin.store.MemoryFullPrunedBlockStore;
|
import com.google.bitcoin.store.MemoryFullPrunedBlockStore;
|
||||||
|
import com.google.bitcoin.utils.BlockFileLoader;
|
||||||
import com.google.bitcoin.utils.BriefLogFormatter;
|
import com.google.bitcoin.utils.BriefLogFormatter;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
import java.math.BigInteger;
|
import java.math.BigInteger;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -45,8 +52,8 @@ public class BitcoindComparisonTool {
|
|||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
BriefLogFormatter.init();
|
BriefLogFormatter.init();
|
||||||
System.out.println("USAGE: runLargeReorgs(1/0)");
|
System.out.println("USAGE: bitcoinjBlockStoreLocation runLargeReorgs(1/0)");
|
||||||
boolean runLargeReorgs = Integer.parseInt(args[0]) == 1;
|
boolean runLargeReorgs = Integer.parseInt(args[1]) == 1;
|
||||||
|
|
||||||
params = NetworkParameters.testNet2();
|
params = NetworkParameters.testNet2();
|
||||||
/**
|
/**
|
||||||
@ -70,14 +77,20 @@ public class BitcoindComparisonTool {
|
|||||||
params.genesisBlock.setDifficultyTarget(0x207fFFFFL);
|
params.genesisBlock.setDifficultyTarget(0x207fFFFFL);
|
||||||
// Also set block.nTime = 1296688602; in the same block
|
// Also set block.nTime = 1296688602; in the same block
|
||||||
|
|
||||||
|
File blockFile = File.createTempFile("testBlocks", ".dat");
|
||||||
|
blockFile.deleteOnExit();
|
||||||
|
|
||||||
FullBlockTestGenerator generator = new FullBlockTestGenerator(params);
|
FullBlockTestGenerator generator = new FullBlockTestGenerator(params);
|
||||||
BlockAndValidityList blockList = generator.getBlocksToTest(true, runLargeReorgs);
|
BlockAndValidityList blockList = generator.getBlocksToTest(true, runLargeReorgs, blockFile);
|
||||||
|
Iterator<Block> blocks = new BlockFileLoader(params, Arrays.asList(blockFile));
|
||||||
|
|
||||||
// Only needs to be set in bitcoinj
|
// Only needs to be set in bitcoinj
|
||||||
params.allowEmptyPeerChains = true;
|
params.allowEmptyPeerChains = true;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
store = new MemoryFullPrunedBlockStore(params, blockList.maximumReorgBlockCount);
|
store = new H2FullPrunedBlockStore(params, args[0], blockList.maximumReorgBlockCount);
|
||||||
|
((H2FullPrunedBlockStore)store).resetStore();
|
||||||
|
//store = new MemoryFullPrunedBlockStore(params, blockList.maximumReorgBlockCount);
|
||||||
chain = new FullPrunedBlockChain(params, store);
|
chain = new FullPrunedBlockChain(params, store);
|
||||||
} catch (BlockStoreException e) {
|
} catch (BlockStoreException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
@ -138,8 +151,9 @@ public class BitcoindComparisonTool {
|
|||||||
int invalidBlocks = 0;
|
int invalidBlocks = 0;
|
||||||
for (BlockAndValidity block : blockList.list) {
|
for (BlockAndValidity block : blockList.list) {
|
||||||
boolean threw = false;
|
boolean threw = false;
|
||||||
|
Block nextBlock = blocks.next();
|
||||||
try {
|
try {
|
||||||
if (chain.add(block.block) != block.connects) {
|
if (chain.add(nextBlock) != block.connects) {
|
||||||
log.error("Block didn't match connects flag on block \"" + block.blockName + "\"");
|
log.error("Block didn't match connects flag on block \"" + block.blockName + "\"");
|
||||||
invalidBlocks++;
|
invalidBlocks++;
|
||||||
}
|
}
|
||||||
@ -147,9 +161,11 @@ public class BitcoindComparisonTool {
|
|||||||
threw = true;
|
threw = true;
|
||||||
if (!block.throwsException) {
|
if (!block.throwsException) {
|
||||||
log.error("Block didn't match throws flag on block \"" + block.blockName + "\"");
|
log.error("Block didn't match throws flag on block \"" + block.blockName + "\"");
|
||||||
|
e.printStackTrace();
|
||||||
invalidBlocks++;
|
invalidBlocks++;
|
||||||
} else if (block.connects) {
|
} else if (block.connects) {
|
||||||
log.error("Block didn't match connects flag on block \"" + block.blockName + "\"");
|
log.error("Block didn't match connects flag on block \"" + block.blockName + "\"");
|
||||||
|
e.printStackTrace();
|
||||||
invalidBlocks++;
|
invalidBlocks++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -164,7 +180,7 @@ public class BitcoindComparisonTool {
|
|||||||
invalidBlocks++;
|
invalidBlocks++;
|
||||||
}
|
}
|
||||||
|
|
||||||
bitcoind.sendMessage(block.block);
|
bitcoind.sendMessage(nextBlock);
|
||||||
locator.clear();
|
locator.clear();
|
||||||
locator.add(bitcoindChainHead);
|
locator.add(bitcoindChainHead);
|
||||||
bitcoind.sendMessage(new GetHeadersMessage(params, locator, hashTo));
|
bitcoind.sendMessage(new GetHeadersMessage(params, locator, hashTo));
|
||||||
|
@ -6,6 +6,8 @@ import com.google.bitcoin.script.ScriptBuilder;
|
|||||||
import com.google.common.base.Preconditions;
|
import com.google.common.base.Preconditions;
|
||||||
|
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.math.BigInteger;
|
import java.math.BigInteger;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
@ -75,8 +77,31 @@ public class FullBlockTestGenerator {
|
|||||||
Utils.rollMockClock(0); // Set a mock clock for timestamp tests
|
Utils.rollMockClock(0); // Set a mock clock for timestamp tests
|
||||||
}
|
}
|
||||||
|
|
||||||
public BlockAndValidityList getBlocksToTest(boolean addSigExpensiveBlocks, boolean runLargeReorgs) throws ScriptException, ProtocolException, IOException {
|
public BlockAndValidityList getBlocksToTest(boolean addSigExpensiveBlocks, boolean runLargeReorgs, File blockStorageFile) throws ScriptException, ProtocolException, IOException {
|
||||||
List<BlockAndValidity> blocks = new LinkedList<BlockAndValidity>();
|
final FileOutputStream outStream = blockStorageFile != null ? new FileOutputStream(blockStorageFile) : null;
|
||||||
|
|
||||||
|
List<BlockAndValidity> blocks = new LinkedList<BlockAndValidity>() {
|
||||||
|
@Override
|
||||||
|
public boolean add(BlockAndValidity element) {
|
||||||
|
if (outStream != null) {
|
||||||
|
try {
|
||||||
|
outStream.write((int) (params.packetMagic >>> 24));
|
||||||
|
outStream.write((int) (params.packetMagic >>> 16));
|
||||||
|
outStream.write((int) (params.packetMagic >>> 8));
|
||||||
|
outStream.write((int) (params.packetMagic >>> 0));
|
||||||
|
byte[] block = element.block.bitcoinSerialize();
|
||||||
|
byte[] length = new byte[4];
|
||||||
|
Utils.uint32ToByteArrayBE(block.length, length, 0);
|
||||||
|
outStream.write(Utils.reverseBytes(length));
|
||||||
|
outStream.write(block);
|
||||||
|
element.block = null;
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return super.add(element);
|
||||||
|
}
|
||||||
|
};
|
||||||
BlockAndValidityList ret = new BlockAndValidityList(blocks, 10);
|
BlockAndValidityList ret = new BlockAndValidityList(blocks, 10);
|
||||||
|
|
||||||
Queue<TransactionOutPointWithValue> spendableOutputs = new LinkedList<TransactionOutPointWithValue>();
|
Queue<TransactionOutPointWithValue> spendableOutputs = new LinkedList<TransactionOutPointWithValue>();
|
||||||
@ -1305,13 +1330,16 @@ public class FullBlockTestGenerator {
|
|||||||
b73.getTransactions().get(0).getOutputs().get(0).getScriptPubKey()));
|
b73.getTransactions().get(0).getOutputs().get(0).getScriptPubKey()));
|
||||||
|
|
||||||
if (runLargeReorgs) {
|
if (runLargeReorgs) {
|
||||||
|
// No way you can fit this test in memory
|
||||||
|
Preconditions.checkArgument(blockStorageFile != null);
|
||||||
|
|
||||||
Block lastBlock = b73;
|
Block lastBlock = b73;
|
||||||
int nextHeight = chainHeadHeight + 24;
|
int nextHeight = chainHeadHeight + 24;
|
||||||
TransactionOutPoint lastOutput = new TransactionOutPoint(params, 2, b73.getTransactions().get(1).getHash());
|
TransactionOutPoint lastOutput = new TransactionOutPoint(params, 2, b73.getTransactions().get(1).getHash());
|
||||||
int blockCountAfter73;
|
int blockCountAfter73;
|
||||||
|
|
||||||
List<Sha256Hash> hashesToSpend = new LinkedList<Sha256Hash>(); // all index 0
|
List<Sha256Hash> hashesToSpend = new LinkedList<Sha256Hash>(); // all index 0
|
||||||
final int TRANSACTION_CREATION_BLOCKS = 50;
|
final int TRANSACTION_CREATION_BLOCKS = 100;
|
||||||
for (blockCountAfter73 = 0; blockCountAfter73 < TRANSACTION_CREATION_BLOCKS; blockCountAfter73++) {
|
for (blockCountAfter73 = 0; blockCountAfter73 < TRANSACTION_CREATION_BLOCKS; blockCountAfter73++) {
|
||||||
Block block = createNextBlock(lastBlock, nextHeight++, null, null);
|
Block block = createNextBlock(lastBlock, nextHeight++, null, null);
|
||||||
while (block.getMessageSize() < Block.MAX_BLOCK_SIZE - 500) {
|
while (block.getMessageSize() < Block.MAX_BLOCK_SIZE - 500) {
|
||||||
@ -1389,6 +1417,9 @@ public class FullBlockTestGenerator {
|
|||||||
|
|
||||||
//TODO: Explicitly address MoneyRange() checks
|
//TODO: Explicitly address MoneyRange() checks
|
||||||
|
|
||||||
|
if (outStream != null)
|
||||||
|
outStream.close();
|
||||||
|
|
||||||
// (finally) return the created chain
|
// (finally) return the created chain
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
@ -65,7 +65,7 @@ public class FullPrunedBlockChainTest {
|
|||||||
public void testGeneratedChain() throws Exception {
|
public void testGeneratedChain() throws Exception {
|
||||||
// Tests various test cases from FullBlockTestGenerator
|
// Tests various test cases from FullBlockTestGenerator
|
||||||
FullBlockTestGenerator generator = new FullBlockTestGenerator(unitTestParams);
|
FullBlockTestGenerator generator = new FullBlockTestGenerator(unitTestParams);
|
||||||
BlockAndValidityList blockList = generator.getBlocksToTest(false, false);
|
BlockAndValidityList blockList = generator.getBlocksToTest(false, false, null);
|
||||||
|
|
||||||
store = new MemoryFullPrunedBlockStore(unitTestParams, blockList.maximumReorgBlockCount);
|
store = new MemoryFullPrunedBlockStore(unitTestParams, blockList.maximumReorgBlockCount);
|
||||||
chain = new FullPrunedBlockChain(unitTestParams, store);
|
chain = new FullPrunedBlockChain(unitTestParams, store);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user