mirror of
https://github.com/bitcoinj/bitcoinj.git
synced 2025-03-12 02:08:49 +01:00
Some fixes for LevelDB SPV block store.
This commit is contained in:
parent
ec6f8a3c9d
commit
7d7ba9c8ef
2 changed files with 17 additions and 6 deletions
|
@ -238,7 +238,7 @@ public class WalletAppKit extends AbstractIdleService {
|
||||||
/**
|
/**
|
||||||
* Tests to see if the spvchain file has an operating system file lock on it. Useful for checking if your app
|
* Tests to see if the spvchain file has an operating system file lock on it. Useful for checking if your app
|
||||||
* is already running. If another copy of your app is running and you start the appkit anyway, an exception will
|
* is already running. If another copy of your app is running and you start the appkit anyway, an exception will
|
||||||
* be thrown during the startup process. Returns false if the chain file does not exist.
|
* be thrown during the startup process. Returns false if the chain file does not exist or is a directory.
|
||||||
*/
|
*/
|
||||||
public boolean isChainFileLocked() throws IOException {
|
public boolean isChainFileLocked() throws IOException {
|
||||||
RandomAccessFile file2 = null;
|
RandomAccessFile file2 = null;
|
||||||
|
@ -246,6 +246,8 @@ public class WalletAppKit extends AbstractIdleService {
|
||||||
File file = new File(directory, filePrefix + ".spvchain");
|
File file = new File(directory, filePrefix + ".spvchain");
|
||||||
if (!file.exists())
|
if (!file.exists())
|
||||||
return false;
|
return false;
|
||||||
|
if (file.isDirectory())
|
||||||
|
return false;
|
||||||
file2 = new RandomAccessFile(file, "rw");
|
file2 = new RandomAccessFile(file, "rw");
|
||||||
FileLock lock = file2.getChannel().tryLock();
|
FileLock lock = file2.getChannel().tryLock();
|
||||||
if (lock == null)
|
if (lock == null)
|
||||||
|
|
|
@ -18,7 +18,7 @@ public class LevelDBBlockStore implements BlockStore {
|
||||||
private static final byte[] CHAIN_HEAD_KEY = "chainhead".getBytes();
|
private static final byte[] CHAIN_HEAD_KEY = "chainhead".getBytes();
|
||||||
|
|
||||||
private final Context context;
|
private final Context context;
|
||||||
private final DB db;
|
private DB db;
|
||||||
private final ByteBuffer buffer = ByteBuffer.allocate(StoredBlock.COMPACT_SERIALIZED_SIZE);
|
private final ByteBuffer buffer = ByteBuffer.allocate(StoredBlock.COMPACT_SERIALIZED_SIZE);
|
||||||
private final File path;
|
private final File path;
|
||||||
|
|
||||||
|
@ -35,14 +35,23 @@ public class LevelDBBlockStore implements BlockStore {
|
||||||
options.createIfMissing();
|
options.createIfMissing();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
db = dbFactory.open(directory, options);
|
tryOpen(directory, dbFactory, options);
|
||||||
initStoreIfNeeded();
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new BlockStoreException(e);
|
try {
|
||||||
|
dbFactory.repair(directory, options);
|
||||||
|
tryOpen(directory, dbFactory, options);
|
||||||
|
} catch (IOException e1) {
|
||||||
|
throw new BlockStoreException(e1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initStoreIfNeeded() throws BlockStoreException {
|
private synchronized void tryOpen(File directory, DBFactory dbFactory, Options options) throws IOException, BlockStoreException {
|
||||||
|
db = dbFactory.open(directory, options);
|
||||||
|
initStoreIfNeeded();
|
||||||
|
}
|
||||||
|
|
||||||
|
private synchronized void initStoreIfNeeded() throws BlockStoreException {
|
||||||
if (db.get(CHAIN_HEAD_KEY) != null)
|
if (db.get(CHAIN_HEAD_KEY) != null)
|
||||||
return; // Already initialised.
|
return; // Already initialised.
|
||||||
Block genesis = context.getParams().getGenesisBlock().cloneAsHeader();
|
Block genesis = context.getParams().getGenesisBlock().cloneAsHeader();
|
||||||
|
|
Loading…
Add table
Reference in a new issue