chaindb: error handling.

This commit is contained in:
Christopher Jeffrey 2016-01-21 14:26:13 -08:00
parent a459d190aa
commit 32109c5de5

View File

@ -834,7 +834,7 @@ ChainDB.prototype.getSync = function getSync(height) {
if ((height + 1) * BLOCK_SIZE > this.size) if ((height + 1) * BLOCK_SIZE > this.size)
return; return;
data = this._read(BLOCK_SIZE, height * BLOCK_SIZE); data = this._readSync(BLOCK_SIZE, height * BLOCK_SIZE);
if (!data) if (!data)
return; return;
@ -872,8 +872,11 @@ ChainDB.prototype.getAsync = function getAsync(height, callback) {
this._readAsync(BLOCK_SIZE, height * BLOCK_SIZE, function(err, data) { this._readAsync(BLOCK_SIZE, height * BLOCK_SIZE, function(err, data) {
var entry; var entry;
// We can't ensure the integrity of
// the chain if we get an error.
// Just throw.
if (err) if (err)
return callback(err); throw err;
if (!data) if (!data)
return callback(); return callback();
@ -893,7 +896,7 @@ ChainDB.prototype.getAsync = function getAsync(height, callback) {
}; };
ChainDB.prototype.save = function save(entry) { ChainDB.prototype.save = function save(entry) {
return this.saveAsync(entry); return this.saveSync(entry);
}; };
ChainDB.prototype.saveSync = function saveSync(entry) { ChainDB.prototype.saveSync = function saveSync(entry) {
@ -907,7 +910,7 @@ ChainDB.prototype.saveSync = function saveSync(entry) {
raw = entry.toRaw(); raw = entry.toRaw();
offset = entry.height * BLOCK_SIZE; offset = entry.height * BLOCK_SIZE;
return this._write(raw, offset); return this._writeSync(raw, offset);
}; };
ChainDB.prototype.saveAsync = function saveAsync(entry, callback) { ChainDB.prototype.saveAsync = function saveAsync(entry, callback) {
@ -935,26 +938,25 @@ ChainDB.prototype.saveAsync = function saveAsync(entry, callback) {
offset = entry.height * BLOCK_SIZE; offset = entry.height * BLOCK_SIZE;
return this._writeAsync(raw, offset, function(err, success) { return this._writeAsync(raw, offset, function(err, success) {
// We can't ensure the integrity of
// the chain if we get an error.
// Just throw.
if (err)
throw err;
var item = self._queue[entry.height]; var item = self._queue[entry.height];
// Something tried to write here but couldn't. // Something tried to write here but couldn't.
// Synchronously write it and get it over with. // Synchronously write it and get it over with.
try { try {
if (item && item !== entry) if (item && item !== entry)
success = self._write(item.toRaw(), offset); success = self._writeSync(item.toRaw(), offset);
} catch (e) { } catch (e) {
err = e; err = e;
} }
delete self._queue[entry.height]; delete self._queue[entry.height];
if (err) {
if (callback)
return callback(err);
else
throw err;
}
if (callback) if (callback)
return callback(null, success); return callback(null, success);
}); });
@ -968,7 +970,7 @@ ChainDB.prototype.remove = function remove(height) {
delete this._queue[height]; delete this._queue[height];
} }
this._write(this._nullBlock, height * BLOCK_SIZE); this._writeSync(this._nullBlock, height * BLOCK_SIZE);
delete this._cache[height]; delete this._cache[height];
// If we deleted several blocks at the end, go back // If we deleted several blocks at the end, go back
@ -991,13 +993,13 @@ ChainDB.prototype.remove = function remove(height) {
}; };
ChainDB.prototype.isNull = function isNull(height) { ChainDB.prototype.isNull = function isNull(height) {
var data = this._read(4, height * BLOCK_SIZE); var data = this._readSync(4, height * BLOCK_SIZE);
if (!data) if (!data)
return false; return false;
return utils.read32(data, 0) === 0; return utils.read32(data, 0) === 0;
}; };
ChainDB.prototype._read = function _read(size, offset) { ChainDB.prototype._readSync = function _readSync(size, offset) {
var index = 0; var index = 0;
var data, bytes; var data, bytes;
@ -1017,13 +1019,15 @@ ChainDB.prototype._read = function _read(size, offset) {
} }
} }
} catch (e) { } catch (e) {
; throw e;
} }
this._free(data); this._free(data);
throw new Error('_readSync() failed.');
}; };
ChainDB.prototype._readAsync = function _read(size, offset, callback) { ChainDB.prototype._readAsync = function _readSync(size, offset, callback) {
var self = this; var self = this;
var index = 0; var index = 0;
var data, bytes; var data, bytes;
@ -1033,7 +1037,7 @@ ChainDB.prototype._readAsync = function _read(size, offset, callback) {
data = this._malloc(size); data = this._malloc(size);
(function callee() { (function next() {
fs.read(self.fd, data, index, size, offset, function(err, bytes) { fs.read(self.fd, data, index, size, offset, function(err, bytes) {
if (err) { if (err) {
self._free(data); self._free(data);
@ -1049,7 +1053,7 @@ ChainDB.prototype._readAsync = function _read(size, offset, callback) {
return callback(null, data); return callback(null, data);
} }
callee(); next();
}); });
})(); })();
}; };
@ -1065,7 +1069,7 @@ ChainDB.prototype._writeAsync = function _writeAsync(data, offset, callback) {
self.size += added; self.size += added;
(function callee() { (function next() {
fs.write(self.fd, data, index, size, offset, function(err, bytes) { fs.write(self.fd, data, index, size, offset, function(err, bytes) {
if (err) if (err)
return callback(err); return callback(err);
@ -1077,12 +1081,12 @@ ChainDB.prototype._writeAsync = function _writeAsync(data, offset, callback) {
if (index === data.length) if (index === data.length)
return callback(null, true); return callback(null, true);
callee(); next();
}); });
})(); })();
}; };
ChainDB.prototype._write = function _write(data, offset) { ChainDB.prototype._writeSync = function _writeSync(data, offset) {
var size = data.length; var size = data.length;
var added = Math.max(0, (offset + data.length) - this.size); var added = Math.max(0, (offset + data.length) - this.size);
var index = 0; var index = 0;
@ -1091,17 +1095,21 @@ ChainDB.prototype._write = function _write(data, offset) {
if (offset < 0 || offset == null) if (offset < 0 || offset == null)
return false; return false;
while (bytes = fs.writeSync(this.fd, data, index, size, offset)) { try {
index += bytes; while (bytes = fs.writeSync(this.fd, data, index, size, offset)) {
size -= bytes; index += bytes;
offset += bytes; size -= bytes;
if (index === data.length) { offset += bytes;
this.size += added; if (index === data.length) {
return true; this.size += added;
return true;
}
} }
} catch (e) {
throw e;
} }
return false; throw new Error('_writeSync() failed.');
}; };
/** /**