chaindb: store network in options.

This commit is contained in:
Christopher Jeffrey 2016-10-24 16:21:24 -07:00
parent d2a9352c9c
commit 926d9cb5aa
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
2 changed files with 27 additions and 17 deletions

View File

@ -14,6 +14,7 @@ var assert = require('assert');
var BufferWriter = require('../utils/writer');
var BufferReader = require('../utils/reader');
var co = require('../utils/co');
var Network = require('../protocol/network');
var CoinView = require('./coinview');
var Coins = require('./coins');
var ldb = require('../db/ldb');
@ -162,9 +163,9 @@ function ChainDB(chain) {
AsyncObject.call(this);
this.chain = chain;
this.options = new ChainOptions(chain.options);
this.logger = chain.logger;
this.network = chain.network;
this.options = new ChainOptions(chain.options);
this.db = ldb({
location: chain.options.location,
@ -224,7 +225,7 @@ ChainDB.prototype._open = co(function* open() {
yield this.db.checkVersion('V', 1);
state = yield this.getState();
options = yield this.getOptions();
// options = yield this.getOptions();
if (options) {
// Verify the options haven't changed.
@ -1671,6 +1672,7 @@ function ChainOptions(options) {
if (!(this instanceof ChainOptions))
return new ChainOptions(options);
this.network = Network.primary;
this.spv = false;
this.witness = false;
this.prune = false;
@ -1684,6 +1686,8 @@ function ChainOptions(options) {
}
ChainOptions.prototype.fromOptions = function fromOptions(options) {
this.network = Network.get(options.network);
if (options.spv != null) {
assert(typeof options.spv === 'boolean');
this.spv = options.spv;
@ -1722,6 +1726,9 @@ ChainOptions.fromOptions = function fromOptions(data) {
};
ChainOptions.prototype.verify = function verify(options) {
if (this.network !== options.network)
throw new Error('Network mismatch for chain.');
if (this.spv && !options.spv)
throw new Error('Cannot retroactively enable SPV.');
@ -1774,6 +1781,7 @@ ChainOptions.prototype.toRaw = function toRaw() {
if (this.indexAddress)
flags |= 1 << 4;
p.writeU32(this.network.magic);
p.writeU32(flags);
p.writeU32(0);
@ -1782,12 +1790,18 @@ ChainOptions.prototype.toRaw = function toRaw() {
ChainOptions.prototype.fromRaw = function fromRaw(data) {
var p = new BufferReader(data);
var flags = p.readU32();
var flags;
this.network = Network.fromMagic(p.readU32());
flags = p.readU32();
this.spv = (flags & 1) !== 0;
this.witness = (flags & 2) !== 0;
this.prune = (flags & 4) !== 0;
this.indexTX = (flags & 8) !== 0;
this.indexAddress = (flags & 16) !== 0;
return this;
};

View File

@ -3204,12 +3204,12 @@ RPC.prototype.importprivkey = co(function* importprivkey(args) {
key = KeyRing.fromSecret(secret);
yield this.wallet.importKey(0, key, null);
yield this.wallet.importKey(0, key);
if (!rescan)
return null;
yield this.node.scan(0);
yield this.walletdb.rescan(0);
return null;
});
@ -3257,10 +3257,10 @@ RPC.prototype.importwallet = co(function* importwallet(args) {
for (i = 0; i < keys.length; i++) {
key = keys[i];
yield this.wallet.importKey(0, key, null);
yield this.wallet.importKey(0, key);
}
yield this.node.scan(0);
yield this.walletdb.rescan(0);
return null;
});
@ -3298,12 +3298,12 @@ RPC.prototype.importpubkey = co(function* importpubkey(args) {
key = KeyRing.fromPublic(pubkey, this.network);
yield this.wallet.importKey(0, key, null);
yield this.wallet.importKey(0, key);
if (!rescan)
return null;
yield this.node.scan(0);
yield this.walletdb.rescan(0);
return null;
});
@ -4012,7 +4012,7 @@ RPC.prototype.walletpassphrase = co(function* walletpassphrase(args) {
});
RPC.prototype.importprunedfunds = co(function* importprunedfunds(args) {
var tx, block, label, height, added;
var tx, block, label, height;
if (args.help || args.length < 2 || args.length > 3) {
throw new RPCError('importprunedfunds'
@ -4047,16 +4047,14 @@ RPC.prototype.importprunedfunds = co(function* importprunedfunds(args) {
tx.ts = block.ts;
tx.height = height;
added = yield this.wallet.add(tx);
if (!added)
if (!(yield this.walletdb.addTX(tx)))
throw new RPCError('No tracked address for TX.');
return null;
});
RPC.prototype.removeprunedfunds = co(function* removeprunedfunds(args) {
var hash, removed;
var hash;
if (args.help || args.length !== 1)
throw new RPCError('removeprunedfunds "txid"');
@ -4066,9 +4064,7 @@ RPC.prototype.removeprunedfunds = co(function* removeprunedfunds(args) {
if (!hash)
throw new RPCError('Invalid parameter.');
removed = yield this.wallet.remove(hash);
if (!removed)
if (!(yield this.wallet.remove(hash)))
throw new RPCError('Transaction not in wallet.');
return null;