Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
self.store.get(DB.PREFIXES.VERSION, options, function(err, buffer) {
var version;
if (err instanceof levelup.errors.NotFoundError) {
// The initial version (1) of the database didn't store the version number
version = 1;
} else if (err) {
return callback(err);
} else {
version = buffer.readUInt32BE();
}
if (self.version !== version) {
var helpUrl = 'https://github.com/bitpay/bitcore-node/blob/master/docs/services/db.md#how-to-reindex';
return callback(new Error(
'The version of the database "' + version + '" does not match the expected version "' +
self.version + '". A recreation of "' + self.dataPath + '" (can take several hours) is ' +
'required or to switch versions of software to match. Please see ' + helpUrl +
' for more information.'
));
}
// mostly from level-packager
const levelMore = (location, options) => {
if (typeof options !== 'object' || options === null) options = {}
const store = options.store || leveldown
delete options.store
;['destroy', 'repair'].forEach(function (m) {
if (typeof store[m] === 'function') {
levelMore[m] = () => store[m].apply(store, arguments)
}
})
return levelup(encode(store(location), options), options)
}
levelMore.errors = levelup.errors
function levelPlugin (fastify, opts, next) {
if (!opts.name && (!opts.options || !opts.options.store)) {
return next(new Error('Missing database name'))
}
opts.options = opts.options || {}
fastify
.decorate('level', levelMore(opts.name, opts.options))
.addHook('onClose', close)
next()
}
function close (fastify, done) {
fastify.level.close(done)
self.store.get(DB.PREFIXES.TIP, options, function(err) {
if (err instanceof levelup.errors.NotFoundError) {
// The database is brand new and doesn't have a tip stored
// we can skip version checking
return callback();
} else if (err) {
return callback(err);
}
self.store.get(DB.PREFIXES.VERSION, options, function(err, buffer) {
var version;
if (err instanceof levelup.errors.NotFoundError) {
// The initial version (1) of the database didn't store the version number
version = 1;
} else if (err) {
return callback(err);
} else {
version = buffer.readUInt32BE();
}
it('will NOT check the version if a tip is not found', function(done) {
var db = new DB(config);
db.store = {};
db.store.get = sinon.stub().callsArgWith(2, new levelup.errors.NotFoundError());
db._checkVersion(done);
});
it('will NOT give an error if the versions match', function(done) {
it('genesis block if no metadata is found in the db', function(done) {
var db = new DB(baseConfig);
db.genesis = Block.fromBuffer(genesisBuffer);
db.store = {
get: sinon.stub().callsArgWith(2, new levelup.errors.NotFoundError())
};
db.connectBlock = sinon.stub().callsArg(1);
db.sync = sinon.stub();
db.loadTip(function() {
should.exist(db.tip);
db.tip.hash.should.equal('00000000b873e79784647a6c82962c70d228557d24a747ea4d1b8bbe878e1206');
done();
});
});
this._store.get(key, opts, function(err, data) {
if(err && err instanceof levelup.errors.NotFoundError) {
return cb();
}
if (err) {
return cb(err);
}
cb(null, data);
});
this.node.services.db.store.get(key, dbOptions, function(err, buffer) {
if (err instanceof levelup.errors.NotFoundError) {
return callback(null, false);
} else if (err) {
return callback(err);
}
var value = encoding.decodeInputValueMap(buffer);
callback(null, {
inputTxId: value.inputTxId.toString('hex'),
inputIndex: value.inputIndex
});
});
};
return levelup(encode(leveldown(location, options), options), options, callback)
}
function isObject (o) {
return typeof o === 'object' && o !== null
}
['destroy', 'repair'].forEach(function (m) {
if (typeof leveldown[m] === 'function') {
Level[m] = function () {
leveldown[m].apply(leveldown, arguments)
}
}
})
Level.errors = levelup.errors
return Level
}
BlockService.prototype._getLatestHash = function() {
var self = this;
return Promise.try(function() {
return self.database.getAsync(Index.tip);
}).catch(LevelUp.errors.NotFoundError, function() {
return null;
});
};
/**
ldb.get(["channel", eci], function (err, channel){
if(err){
if(err.notFound){
err = new levelup.errors.NotFoundError("ECI not found: " + eci);
err.notFound = true;
}
callback(err);
return;
}
var privateKey = channel.sovrin.secret.encryptionPrivateKey;
privateKey = bs58.decode(privateKey);
var sharedSecret = channel.sovrin.sharedSecret;
if (!sharedSecret) {
sharedSecret = sovrinDID.getSharedSecret(otherPublicKey, privateKey);
ldb.put(["channel", eci, "sovrin", "secret", "sharedSecret"], bs58.encode(sharedSecret), function (err) {
if (err) {
callback(err);
}
});
}