3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-02-12 10:15:52 +00:00

Lock in BOBS on creation, do not swallow non-IO exceptions

This commit is contained in:
Miron Cuperman 2012-03-23 12:53:54 -07:00
parent 4b1c32584f
commit 576650142c
2 changed files with 39 additions and 15 deletions

View File

@ -152,7 +152,7 @@ public class BoundedOverheadBlockStore implements BlockStore {
try { try {
load(file); load(file);
return; return;
} catch (Exception e) { } catch (IOException e) {
log.error("Failed to load block chain from " + file, e); log.error("Failed to load block chain from " + file, e);
// Fall through and try to create a new one. // Fall through and try to create a new one.
} }
@ -172,6 +172,7 @@ public class BoundedOverheadBlockStore implements BlockStore {
// Create fresh. The d makes writes synchronous. // Create fresh. The d makes writes synchronous.
this.file = new RandomAccessFile(file, "rwd"); this.file = new RandomAccessFile(file, "rwd");
this.channel = this.file.getChannel(); this.channel = this.file.getChannel();
lock();
this.file.write(FILE_FORMAT_VERSION); this.file.write(FILE_FORMAT_VERSION);
} catch (IOException e1) { } catch (IOException e1) {
// We could not load a block store nor could we create a new one! // We could not load a block store nor could we create a new one!
@ -195,21 +196,9 @@ public class BoundedOverheadBlockStore implements BlockStore {
log.info("Reading block store from {}", file); log.info("Reading block store from {}", file);
// Open in synchronous mode. See above. // Open in synchronous mode. See above.
this.file = new RandomAccessFile(file, "rwd"); this.file = new RandomAccessFile(file, "rwd");
try {
lock = this.file.getChannel().tryLock();
} catch (OverlappingFileLockException e) {
lock = null;
}
if (lock == null) {
try {
this.file.close();
} finally {
this.file = null;
}
throw new BlockStoreException("Could not lock file");
}
try {
channel = this.file.getChannel(); channel = this.file.getChannel();
lock();
try {
// Read a version byte. // Read a version byte.
int version = this.file.read(); int version = this.file.read();
if (version == -1) { if (version == -1) {
@ -235,6 +224,22 @@ public class BoundedOverheadBlockStore implements BlockStore {
} }
} }
private void lock() throws IOException, BlockStoreException {
try {
lock = channel.tryLock();
} catch (OverlappingFileLockException e) {
lock = null;
}
if (lock == null) {
try {
this.file.close();
} finally {
this.file = null;
}
throw new BlockStoreException("Could not lock file");
}
}
private void ensureOpen() throws BlockStoreException { private void ensureOpen() throws BlockStoreException {
if (file == null) { if (file == null) {
throw new BlockStoreException("BlockStore was closed"); throw new BlockStoreException("BlockStore was closed");

View File

@ -24,6 +24,7 @@ import org.junit.Test;
import java.io.File; import java.io.File;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
public class BoundedOverheadBlockStoreTest { public class BoundedOverheadBlockStoreTest {
@Test @Test
@ -43,6 +44,8 @@ public class BoundedOverheadBlockStoreTest {
StoredBlock b1 = genesis.build(genesis.getHeader().createNextBlock(to).cloneAsHeader()); StoredBlock b1 = genesis.build(genesis.getHeader().createNextBlock(to).cloneAsHeader());
store.put(b1); store.put(b1);
store.setChainHead(b1); store.setChainHead(b1);
store.close();
// Check we can get it back out again if we rebuild the store object. // Check we can get it back out again if we rebuild the store object.
store = new BoundedOverheadBlockStore(params, temp); store = new BoundedOverheadBlockStore(params, temp);
StoredBlock b2 = store.get(b1.getHeader().getHash()); StoredBlock b2 = store.get(b1.getHeader().getHash());
@ -50,4 +53,20 @@ public class BoundedOverheadBlockStoreTest {
// Check the chain head was stored correctly also. // Check the chain head was stored correctly also.
assertEquals(b1, store.getChainHead()); assertEquals(b1, store.getChainHead());
} }
@Test
public void testLocking() throws Exception {
File temp = File.createTempFile("bitcoinj-test", null, null);
System.out.println(temp.getAbsolutePath());
temp.deleteOnExit();
NetworkParameters params = NetworkParameters.unitTests();
BoundedOverheadBlockStore store = new BoundedOverheadBlockStore(params, temp);
try {
BoundedOverheadBlockStore store1 = new BoundedOverheadBlockStore(params, temp);
fail();
} catch (BlockStoreException e) {
// Expected
}
}
} }