3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-01-31 15:22:16 +00:00

Import from new block dir format

Also fixes a few minor bugs in BlockImporter.
This commit is contained in:
Matt Corallo 2013-03-25 01:03:21 -04:00 committed by Mike Hearn
parent 726dd02472
commit 2e6e0661cb

View File

@ -25,6 +25,7 @@ import com.google.bitcoin.store.H2FullPrunedBlockStore;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
/**
@ -40,19 +41,41 @@ public class BlockImporter {
String defaultDataDir;
if (System.getProperty("os.name").toLowerCase().indexOf("win") >= 0) {
defaultDataDir = System.getenv("APPDATA") + "\\.bitcoin/";
defaultDataDir = System.getenv("APPDATA") + "\\.bitcoin\\blocks\\";
} else {
defaultDataDir = System.getProperty("user.home") + "/.bitcoin/";
defaultDataDir = System.getProperty("user.home") + "/.bitcoin/blocks/";
}
// TODO: Move this to a library function
FileInputStream stream = new FileInputStream(new File(defaultDataDir + "blk0001.dat"));
int i = 0;
for (int j = 0; true; j++) {
FileInputStream stream;
System.out.println("Opening " + defaultDataDir + String.format("blk%05d.dat", j));
try {
stream = new FileInputStream(new File(
defaultDataDir + String.format("blk%05d.dat", j)));
} catch (FileNotFoundException e1) {
System.out.println(defaultDataDir + String.format("blk%05d.dat", j));
break;
}
while (stream.available() > 0) {
try {
while(stream.read() != ((params.packetMagic >>> 24) & 0xff) || stream.read() != ((params.packetMagic >>> 16) & 0xff) ||
stream.read() != ((params.packetMagic >>> 8) & 0xff) || stream.read() != (params.packetMagic & 0xff))
;
int nextChar = stream.read();
while (nextChar != -1) {
if (nextChar != ((params.packetMagic >>> 24) & 0xff)) {
nextChar = stream.read();
continue;
}
nextChar = stream.read();
if (nextChar != ((params.packetMagic >>> 16) & 0xff))
continue;
nextChar = stream.read();
if (nextChar != ((params.packetMagic >>> 8) & 0xff))
continue;
nextChar = stream.read();
if (nextChar == (params.packetMagic & 0xff))
break;
}
} catch (IOException e) {
break;
}
@ -62,16 +85,18 @@ public class BlockImporter {
if (size > Block.MAX_BLOCK_SIZE || size <= 0)
continue;
bytes = new byte[(int) size];
stream.read(bytes, 0, (int)size);
stream.read(bytes, 0, (int) size);
Block block = new Block(params, bytes);
if (store.get(block.getHash()) == null)
chain.add(block);
if (i % 10000 == 0)
if (i % 1000 == 0)
System.out.println(i);
i++;
}
stream.close();
}
System.out.println("Imported " + chain.getChainHead().getHeight() + " blocks.");
System.exit(0);
}
}