chaindb: store network in options.
This commit is contained in:
parent
d2a9352c9c
commit
926d9cb5aa
@ -14,6 +14,7 @@ var assert = require('assert');
|
|||||||
var BufferWriter = require('../utils/writer');
|
var BufferWriter = require('../utils/writer');
|
||||||
var BufferReader = require('../utils/reader');
|
var BufferReader = require('../utils/reader');
|
||||||
var co = require('../utils/co');
|
var co = require('../utils/co');
|
||||||
|
var Network = require('../protocol/network');
|
||||||
var CoinView = require('./coinview');
|
var CoinView = require('./coinview');
|
||||||
var Coins = require('./coins');
|
var Coins = require('./coins');
|
||||||
var ldb = require('../db/ldb');
|
var ldb = require('../db/ldb');
|
||||||
@ -162,9 +163,9 @@ function ChainDB(chain) {
|
|||||||
AsyncObject.call(this);
|
AsyncObject.call(this);
|
||||||
|
|
||||||
this.chain = chain;
|
this.chain = chain;
|
||||||
this.options = new ChainOptions(chain.options);
|
|
||||||
this.logger = chain.logger;
|
this.logger = chain.logger;
|
||||||
this.network = chain.network;
|
this.network = chain.network;
|
||||||
|
this.options = new ChainOptions(chain.options);
|
||||||
|
|
||||||
this.db = ldb({
|
this.db = ldb({
|
||||||
location: chain.options.location,
|
location: chain.options.location,
|
||||||
@ -224,7 +225,7 @@ ChainDB.prototype._open = co(function* open() {
|
|||||||
yield this.db.checkVersion('V', 1);
|
yield this.db.checkVersion('V', 1);
|
||||||
|
|
||||||
state = yield this.getState();
|
state = yield this.getState();
|
||||||
options = yield this.getOptions();
|
// options = yield this.getOptions();
|
||||||
|
|
||||||
if (options) {
|
if (options) {
|
||||||
// Verify the options haven't changed.
|
// Verify the options haven't changed.
|
||||||
@ -1671,6 +1672,7 @@ function ChainOptions(options) {
|
|||||||
if (!(this instanceof ChainOptions))
|
if (!(this instanceof ChainOptions))
|
||||||
return new ChainOptions(options);
|
return new ChainOptions(options);
|
||||||
|
|
||||||
|
this.network = Network.primary;
|
||||||
this.spv = false;
|
this.spv = false;
|
||||||
this.witness = false;
|
this.witness = false;
|
||||||
this.prune = false;
|
this.prune = false;
|
||||||
@ -1684,6 +1686,8 @@ function ChainOptions(options) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ChainOptions.prototype.fromOptions = function fromOptions(options) {
|
ChainOptions.prototype.fromOptions = function fromOptions(options) {
|
||||||
|
this.network = Network.get(options.network);
|
||||||
|
|
||||||
if (options.spv != null) {
|
if (options.spv != null) {
|
||||||
assert(typeof options.spv === 'boolean');
|
assert(typeof options.spv === 'boolean');
|
||||||
this.spv = options.spv;
|
this.spv = options.spv;
|
||||||
@ -1722,6 +1726,9 @@ ChainOptions.fromOptions = function fromOptions(data) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
ChainOptions.prototype.verify = function verify(options) {
|
ChainOptions.prototype.verify = function verify(options) {
|
||||||
|
if (this.network !== options.network)
|
||||||
|
throw new Error('Network mismatch for chain.');
|
||||||
|
|
||||||
if (this.spv && !options.spv)
|
if (this.spv && !options.spv)
|
||||||
throw new Error('Cannot retroactively enable SPV.');
|
throw new Error('Cannot retroactively enable SPV.');
|
||||||
|
|
||||||
@ -1774,6 +1781,7 @@ ChainOptions.prototype.toRaw = function toRaw() {
|
|||||||
if (this.indexAddress)
|
if (this.indexAddress)
|
||||||
flags |= 1 << 4;
|
flags |= 1 << 4;
|
||||||
|
|
||||||
|
p.writeU32(this.network.magic);
|
||||||
p.writeU32(flags);
|
p.writeU32(flags);
|
||||||
p.writeU32(0);
|
p.writeU32(0);
|
||||||
|
|
||||||
@ -1782,12 +1790,18 @@ ChainOptions.prototype.toRaw = function toRaw() {
|
|||||||
|
|
||||||
ChainOptions.prototype.fromRaw = function fromRaw(data) {
|
ChainOptions.prototype.fromRaw = function fromRaw(data) {
|
||||||
var p = new BufferReader(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.spv = (flags & 1) !== 0;
|
||||||
this.witness = (flags & 2) !== 0;
|
this.witness = (flags & 2) !== 0;
|
||||||
this.prune = (flags & 4) !== 0;
|
this.prune = (flags & 4) !== 0;
|
||||||
this.indexTX = (flags & 8) !== 0;
|
this.indexTX = (flags & 8) !== 0;
|
||||||
this.indexAddress = (flags & 16) !== 0;
|
this.indexAddress = (flags & 16) !== 0;
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -3204,12 +3204,12 @@ RPC.prototype.importprivkey = co(function* importprivkey(args) {
|
|||||||
|
|
||||||
key = KeyRing.fromSecret(secret);
|
key = KeyRing.fromSecret(secret);
|
||||||
|
|
||||||
yield this.wallet.importKey(0, key, null);
|
yield this.wallet.importKey(0, key);
|
||||||
|
|
||||||
if (!rescan)
|
if (!rescan)
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
yield this.node.scan(0);
|
yield this.walletdb.rescan(0);
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
});
|
});
|
||||||
@ -3257,10 +3257,10 @@ RPC.prototype.importwallet = co(function* importwallet(args) {
|
|||||||
|
|
||||||
for (i = 0; i < keys.length; i++) {
|
for (i = 0; i < keys.length; i++) {
|
||||||
key = keys[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;
|
return null;
|
||||||
});
|
});
|
||||||
@ -3298,12 +3298,12 @@ RPC.prototype.importpubkey = co(function* importpubkey(args) {
|
|||||||
|
|
||||||
key = KeyRing.fromPublic(pubkey, this.network);
|
key = KeyRing.fromPublic(pubkey, this.network);
|
||||||
|
|
||||||
yield this.wallet.importKey(0, key, null);
|
yield this.wallet.importKey(0, key);
|
||||||
|
|
||||||
if (!rescan)
|
if (!rescan)
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
yield this.node.scan(0);
|
yield this.walletdb.rescan(0);
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
});
|
});
|
||||||
@ -4012,7 +4012,7 @@ RPC.prototype.walletpassphrase = co(function* walletpassphrase(args) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
RPC.prototype.importprunedfunds = co(function* importprunedfunds(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) {
|
if (args.help || args.length < 2 || args.length > 3) {
|
||||||
throw new RPCError('importprunedfunds'
|
throw new RPCError('importprunedfunds'
|
||||||
@ -4047,16 +4047,14 @@ RPC.prototype.importprunedfunds = co(function* importprunedfunds(args) {
|
|||||||
tx.ts = block.ts;
|
tx.ts = block.ts;
|
||||||
tx.height = height;
|
tx.height = height;
|
||||||
|
|
||||||
added = yield this.wallet.add(tx);
|
if (!(yield this.walletdb.addTX(tx)))
|
||||||
|
|
||||||
if (!added)
|
|
||||||
throw new RPCError('No tracked address for TX.');
|
throw new RPCError('No tracked address for TX.');
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
});
|
});
|
||||||
|
|
||||||
RPC.prototype.removeprunedfunds = co(function* removeprunedfunds(args) {
|
RPC.prototype.removeprunedfunds = co(function* removeprunedfunds(args) {
|
||||||
var hash, removed;
|
var hash;
|
||||||
|
|
||||||
if (args.help || args.length !== 1)
|
if (args.help || args.length !== 1)
|
||||||
throw new RPCError('removeprunedfunds "txid"');
|
throw new RPCError('removeprunedfunds "txid"');
|
||||||
@ -4066,9 +4064,7 @@ RPC.prototype.removeprunedfunds = co(function* removeprunedfunds(args) {
|
|||||||
if (!hash)
|
if (!hash)
|
||||||
throw new RPCError('Invalid parameter.');
|
throw new RPCError('Invalid parameter.');
|
||||||
|
|
||||||
removed = yield this.wallet.remove(hash);
|
if (!(yield this.wallet.remove(hash)))
|
||||||
|
|
||||||
if (!removed)
|
|
||||||
throw new RPCError('Transaction not in wallet.');
|
throw new RPCError('Transaction not in wallet.');
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user