blockstore: do not write block twice

This commit is contained in:
Braydon Fuller 2019-03-15 08:54:48 -07:00
parent 58e623e30a
commit 5cbbcf5409
No known key found for this signature in database
GPG Key ID: F24F232D108B3AD4
2 changed files with 27 additions and 0 deletions

View File

@ -297,6 +297,11 @@ class FileBlockStore extends AbstractBlockStore {
this.writing = true;
if (await this.db.has(layout.b.encode(type, hash))) {
this.writing = false;
return false;
}
const mlength = 8;
const blength = data.length;
const length = data.length + mlength;
@ -352,6 +357,8 @@ class FileBlockStore extends AbstractBlockStore {
await b.write();
this.writing = false;
return true;
}
/**

View File

@ -391,9 +391,11 @@ describe('BlockStore', function() {
const open = fs.open;
const close = fs.close;
let allocate = null;
let has = null;
beforeEach(() => {
allocate = store.allocate;
has = store.db.has;
});
afterEach(() => {
@ -402,6 +404,7 @@ describe('BlockStore', function() {
fs.open = open;
fs.close = close;
store.allocate = allocate;
store.db.has = has;
});
it('will error if total magic bytes not written', async () => {
@ -416,6 +419,7 @@ describe('BlockStore', function() {
filepath: '/tmp/.bcoin/blocks/blk00020.dat'
};
};
store.db.has = () => false;
fs.open = () => 7;
fs.close = () => undefined;
fs.write = () => 0;
@ -445,6 +449,7 @@ describe('BlockStore', function() {
filepath: '/tmp/.bcoin/blocks/blk00020.dat'
};
};
store.db.has = () => false;
fs.open = () => 7;
fs.close = () => undefined;
fs.write = (fd, buffer, offset, length, position) => {
@ -685,6 +690,21 @@ describe('BlockStore', function() {
}
});
it('will not duplicate a block on disk', async () => {
const block = random.randomBytes(128);
const hash = random.randomBytes(32);
const first = await store.write(hash, block);
assert.equal(first, true);
const second = await store.write(hash, block);
assert.equal(second, false);
const pruned = await store.prune(hash);
assert.equal(pruned, true);
assert.equal(await fs.exists(store.filepath(types.BLOCK, 0)), false);
});
it('will return null if block not found', async () => {
const hash = random.randomBytes(32);
const block = await store.read(hash);