walletdb: refactor.
This commit is contained in:
parent
dec45ac265
commit
69242f5228
@ -333,7 +333,6 @@ WalletDB.prototype._sync = co(function* connect() {
|
||||
|
||||
/**
|
||||
* Force a rescan.
|
||||
* @param {ChainClient} chain
|
||||
* @param {Number} height
|
||||
* @returns {Promise}
|
||||
*/
|
||||
@ -359,10 +358,9 @@ WalletDB.prototype._rescan = co(function* rescan(height) {
|
||||
});
|
||||
|
||||
/**
|
||||
* Sync with the chain server (without a lock).
|
||||
* Rescan blockchain from a given height.
|
||||
* @private
|
||||
* @param {Number} height
|
||||
* @param {Hashes[]} hashes
|
||||
* @returns {Promise}
|
||||
*/
|
||||
|
||||
@ -378,9 +376,6 @@ WalletDB.prototype.scan = co(function* scan(height) {
|
||||
|
||||
assert(utils.isUInt32(height), 'WDB: Must pass in a height.');
|
||||
|
||||
if (height > this.state.height)
|
||||
throw new Error('WDB: Cannot rescan future blocks.');
|
||||
|
||||
yield this.rollback(height);
|
||||
|
||||
this.logger.info(
|
||||
@ -1541,7 +1536,6 @@ WalletDB.prototype.init = co(function* init() {
|
||||
|
||||
if (this.client) {
|
||||
tip = yield this.client.getTip();
|
||||
assert(tip);
|
||||
tip = BlockMeta.fromEntry(tip);
|
||||
} else {
|
||||
tip = BlockMeta.fromEntry(this.network.genesis);
|
||||
@ -1569,7 +1563,7 @@ WalletDB.prototype.getState = co(function* getState() {
|
||||
});
|
||||
|
||||
/**
|
||||
* Write the connecting block immediately.
|
||||
* Reset the chain state to a tip/start-block.
|
||||
* @param {BlockMeta} tip
|
||||
* @returns {Promise}
|
||||
*/
|
||||
@ -1612,7 +1606,7 @@ WalletDB.prototype.resetState = co(function* resetState(tip) {
|
||||
});
|
||||
|
||||
/**
|
||||
* Write the connecting block immediately.
|
||||
* Sync the current chain state to tip.
|
||||
* @param {BlockMeta} tip
|
||||
* @returns {Promise}
|
||||
*/
|
||||
@ -1657,22 +1651,36 @@ WalletDB.prototype.syncState = co(function* syncState(tip) {
|
||||
});
|
||||
|
||||
/**
|
||||
* Connect a block.
|
||||
* @param {Wallet} wallet
|
||||
* @param {BlockMapRecord} block
|
||||
* Get a block->wallet map.
|
||||
* @param {Number} height
|
||||
* @returns {Promise}
|
||||
*/
|
||||
|
||||
WalletDB.prototype.getBlockMap = co(function* getBlockMap(height) {
|
||||
var data = yield this.db.get(layout.b(height));
|
||||
|
||||
if (!data)
|
||||
return;
|
||||
|
||||
return BlockMapRecord.fromRaw(height, data);
|
||||
});
|
||||
|
||||
/**
|
||||
* Add block to the global block map.
|
||||
* @param {Wallet} wallet
|
||||
* @param {Number} height
|
||||
* @param {BlockMapRecord} block
|
||||
*/
|
||||
|
||||
WalletDB.prototype.writeBlockMap = function writeBlockMap(wallet, height, block) {
|
||||
var batch = this.batch(wallet);
|
||||
batch.put(layout.b(height), block.toRaw());
|
||||
};
|
||||
|
||||
/**
|
||||
* Connect a block.
|
||||
* Remove a block from the global block map.
|
||||
* @param {Wallet} wallet
|
||||
* @param {BlockMapRecord} block
|
||||
* @returns {Promise}
|
||||
* @param {Number} height
|
||||
*/
|
||||
|
||||
WalletDB.prototype.unwriteBlockMap = function unwriteBlockMap(wallet, height) {
|
||||
@ -1680,13 +1688,28 @@ WalletDB.prototype.unwriteBlockMap = function unwriteBlockMap(wallet, height) {
|
||||
batch.del(layout.b(height));
|
||||
};
|
||||
|
||||
/**
|
||||
* Get a Unspent->Wallet map.
|
||||
* @param {Hash} hash
|
||||
* @param {Number} index
|
||||
* @returns {Promise}
|
||||
*/
|
||||
|
||||
WalletDB.prototype.getOutpointMap = co(function* getOutpointMap(hash, i) {
|
||||
var data = yield this.db.get(layout.o(hash, i));
|
||||
|
||||
if (!data)
|
||||
return;
|
||||
|
||||
return OutpointMapRecord.fromRaw(hash, i, data);
|
||||
});
|
||||
|
||||
/**
|
||||
* Add an outpoint to global unspent map.
|
||||
* @param {Wallet} wallet
|
||||
* @param {Hash} hash
|
||||
* @param {Number} index
|
||||
* @param {OutpointMapRecord} map
|
||||
* @returns {Promise}
|
||||
*/
|
||||
|
||||
WalletDB.prototype.writeOutpointMap = function writeOutpointMap(wallet, hash, i, map) {
|
||||
@ -1702,7 +1725,6 @@ WalletDB.prototype.writeOutpointMap = function writeOutpointMap(wallet, hash, i,
|
||||
* @param {Wallet} wallet
|
||||
* @param {Hash} hash
|
||||
* @param {Number} index
|
||||
* @returns {Promise}
|
||||
*/
|
||||
|
||||
WalletDB.prototype.unwriteOutpointMap = function unwriteOutpointMap(wallet, hash, i) {
|
||||
@ -1710,21 +1732,6 @@ WalletDB.prototype.unwriteOutpointMap = function unwriteOutpointMap(wallet, hash
|
||||
batch.del(layout.o(hash, i));
|
||||
};
|
||||
|
||||
/**
|
||||
* Get a wallet block (with hashes).
|
||||
* @param {Hash} hash
|
||||
* @returns {Promise}
|
||||
*/
|
||||
|
||||
WalletDB.prototype.getBlockMap = co(function* getBlockMap(height) {
|
||||
var data = yield this.db.get(layout.b(height));
|
||||
|
||||
if (!data)
|
||||
return;
|
||||
|
||||
return BlockMapRecord.fromRaw(height, data);
|
||||
});
|
||||
|
||||
/**
|
||||
* Get a wallet block meta.
|
||||
* @param {Hash} hash
|
||||
@ -1760,22 +1767,6 @@ WalletDB.prototype.getTip = co(function* getTip() {
|
||||
return tip;
|
||||
});
|
||||
|
||||
/**
|
||||
* Get a Unspent->Wallet map.
|
||||
* @param {Hash} hash
|
||||
* @param {Number} index
|
||||
* @returns {Promise}
|
||||
*/
|
||||
|
||||
WalletDB.prototype.getOutpointMap = co(function* getOutpointMap(hash, i) {
|
||||
var data = yield this.db.get(layout.o(hash, i));
|
||||
|
||||
if (!data)
|
||||
return;
|
||||
|
||||
return OutpointMapRecord.fromRaw(hash, i, data);
|
||||
});
|
||||
|
||||
/**
|
||||
* Sync with chain height.
|
||||
* @param {Number} height
|
||||
@ -1785,8 +1776,13 @@ WalletDB.prototype.getOutpointMap = co(function* getOutpointMap(hash, i) {
|
||||
WalletDB.prototype.rollback = co(function* rollback(height) {
|
||||
var tip;
|
||||
|
||||
if (this.state.height <= height)
|
||||
if (height > this.state.height)
|
||||
throw new Error('WDB: Cannot rollback to the future.');
|
||||
|
||||
if (height === this.state.height) {
|
||||
this.logger.debug('Rolled back to same height (%d).', height);
|
||||
return;
|
||||
}
|
||||
|
||||
this.logger.info(
|
||||
'Rolling back %d WalletDB blocks to height %d.',
|
||||
@ -1808,7 +1804,7 @@ WalletDB.prototype.rollback = co(function* rollback(height) {
|
||||
|
||||
this.logger.warning(
|
||||
'Rolling back WalletDB to start block (%d).',
|
||||
this.state.tip.height);
|
||||
tip.height);
|
||||
} else {
|
||||
tip.height = 0;
|
||||
tip.hash = this.network.genesis.hash;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user