txdb: refactor layout.
This commit is contained in:
parent
53f209dda5
commit
727380a69a
@ -32,259 +32,210 @@ var BufferWriter = require('./writer');
|
||||
* C[account][hash][index] -> dummy (coin by account)
|
||||
*/
|
||||
|
||||
function Layout(wallet) {
|
||||
this.wallet = wallet;
|
||||
}
|
||||
|
||||
/* String Keys
|
||||
Layout.prototype.prefix = function prefix(key) {
|
||||
assert(this.wallet.wid);
|
||||
return 't' + pad32(this.wallet.wid) + key;
|
||||
};
|
||||
var layout = {
|
||||
prefix: function prefix(wid, key) {
|
||||
return 't' + pad32(wid) + key;
|
||||
},
|
||||
|
||||
Layout.prototype.hi = function hi(ch, hash, index) {
|
||||
return this.prefix(ch + hash + pad32(index));
|
||||
};
|
||||
hi: function hi(ch, hash, index) {
|
||||
return this.prefix(ch + hash + pad32(index));
|
||||
},
|
||||
|
||||
Layout.prototype.hii = function hii(key) {
|
||||
key = key.slice(12);
|
||||
return [key.slice(0, 64), +key.slice(64)];
|
||||
};
|
||||
hii: function hii(key) {
|
||||
key = key.slice(12);
|
||||
return [key.slice(0, 64), +key.slice(64)];
|
||||
},
|
||||
|
||||
Layout.prototype.ih = function ih(ch, index, hash) {
|
||||
return this.prefix(ch + pad32(index) + hash);
|
||||
};
|
||||
ih: function ih(ch, index, hash) {
|
||||
return this.prefix(ch + pad32(index) + hash);
|
||||
},
|
||||
|
||||
Layout.prototype.ihh = function ihh(key) {
|
||||
key = key.slice(12);
|
||||
return [+key.slice(0, 10), key.slice(10)];
|
||||
};
|
||||
ihh: function ihh(key) {
|
||||
key = key.slice(12);
|
||||
return [+key.slice(0, 10), key.slice(10)];
|
||||
},
|
||||
|
||||
Layout.prototype.iih = function iih(ch, index, num, hash) {
|
||||
return this.prefix(ch + pad32(index) + pad32(num) + hash);
|
||||
};
|
||||
iih: function iih(ch, index, num, hash) {
|
||||
return this.prefix(ch + pad32(index) + pad32(num) + hash);
|
||||
},
|
||||
|
||||
Layout.prototype.iihh = function iihh(key) {
|
||||
key = key.slice(12);
|
||||
return [+key.slice(0, 10), +key.slice(10, 20), key.slice(20)];
|
||||
};
|
||||
iihh: function iihh(key) {
|
||||
key = key.slice(12);
|
||||
return [+key.slice(0, 10), +key.slice(10, 20), key.slice(20)];
|
||||
},
|
||||
|
||||
Layout.prototype.ihi = function ihi(ch, index, hash, num) {
|
||||
return this.prefix(ch + pad32(index) + hash + pad32(num));
|
||||
};
|
||||
ihi: function ihi(ch, index, hash, num) {
|
||||
return this.prefix(ch + pad32(index) + hash + pad32(num));
|
||||
},
|
||||
|
||||
Layout.prototype.ihii = function ihii(key) {
|
||||
key = key.slice(12);
|
||||
return [+key.slice(0, 10), key.slice(10, 74), +key.slice(74)];
|
||||
};
|
||||
ihii: function ihii(key) {
|
||||
key = key.slice(12);
|
||||
return [+key.slice(0, 10), key.slice(10, 74), +key.slice(74)];
|
||||
},
|
||||
|
||||
Layout.prototype.ha = function ha(ch, hash) {
|
||||
return this.prefix(ch + hash);
|
||||
};
|
||||
ha: function ha(ch, hash) {
|
||||
return this.prefix(ch + hash);
|
||||
},
|
||||
|
||||
Layout.prototype.haa = function haa(key) {
|
||||
key = key.slice(12);
|
||||
return key;
|
||||
haa: function haa(key) {
|
||||
key = key.slice(12);
|
||||
return key;
|
||||
},
|
||||
|
||||
...
|
||||
};
|
||||
*/
|
||||
|
||||
Layout.prototype.prefix = function prefix(key) {
|
||||
var out = new Buffer(5 + key.length);
|
||||
assert(this.wallet.wid);
|
||||
out[0] = 0x74;
|
||||
out.writeUInt32BE(this.wallet.wid, 1);
|
||||
key.copy(out, 5);
|
||||
return out;
|
||||
};
|
||||
|
||||
Layout.prototype.hi = function hi(ch, hash, index) {
|
||||
var key = new Buffer(37);
|
||||
key[0] = ch.charCodeAt(0);
|
||||
key.write(hash, 1, 'hex');
|
||||
key.writeUInt32BE(index, 33, true);
|
||||
return this.prefix(key);
|
||||
};
|
||||
|
||||
Layout.prototype.hii = function hii(key) {
|
||||
key = key.slice(6);
|
||||
return [key.toString('hex', 0, 32), key.readUInt32BE(32, true)];
|
||||
};
|
||||
|
||||
Layout.prototype.ih = function ih(ch, index, hash) {
|
||||
var key = new Buffer(37);
|
||||
key[0] = ch.charCodeAt(0);
|
||||
key.writeUInt32BE(index, 1, true);
|
||||
key.write(hash, 5, 'hex');
|
||||
return this.prefix(key);
|
||||
};
|
||||
|
||||
Layout.prototype.ihh = function ihh(key) {
|
||||
key = key.slice(6);
|
||||
return [key.readUInt32BE(0, true), key.toString('hex', 4, 36)];
|
||||
};
|
||||
|
||||
Layout.prototype.iih = function iih(ch, index, num, hash) {
|
||||
var key = new Buffer(41);
|
||||
key[0] = ch.charCodeAt(0);
|
||||
key.writeUInt32BE(index, 1, true);
|
||||
key.writeUInt32BE(num, 5, true);
|
||||
key.write(hash, 9, 'hex');
|
||||
return this.prefix(key);
|
||||
};
|
||||
|
||||
Layout.prototype.iihh = function iihh(key) {
|
||||
key = key.slice(6);
|
||||
return [
|
||||
key.readUInt32BE(0, true),
|
||||
key.readUInt32BE(4, true),
|
||||
key.toString('hex', 8, 40)
|
||||
];
|
||||
};
|
||||
|
||||
Layout.prototype.ihi = function ihi(ch, index, hash, num) {
|
||||
var key = new Buffer(41);
|
||||
key[0] = ch.charCodeAt(0);
|
||||
key.writeUInt32BE(index, 1, true);
|
||||
key.write(hash, 5, 'hex');
|
||||
key.writeUInt32BE(num, 37, true);
|
||||
return this.prefix(key);
|
||||
};
|
||||
|
||||
Layout.prototype.ihii = function ihii(key) {
|
||||
key = key.slice(6);
|
||||
return [
|
||||
key.readUInt32BE(0, true),
|
||||
key.toString('hex', 4, 36),
|
||||
key.readUInt32BE(36, true)
|
||||
];
|
||||
};
|
||||
|
||||
Layout.prototype.ha = function ha(ch, hash) {
|
||||
var key = new Buffer(33);
|
||||
key[0] = ch.charCodeAt(0);
|
||||
key.write(hash, 1, 'hex');
|
||||
return this.prefix(key);
|
||||
};
|
||||
|
||||
Layout.prototype.haa = function haa(key) {
|
||||
return key.toString('hex', 1);
|
||||
};
|
||||
|
||||
Layout.prototype.t = function t(hash) {
|
||||
// 0x74
|
||||
return this.ha('t', hash);
|
||||
};
|
||||
|
||||
Layout.prototype.tt = function tt(key) {
|
||||
return this.haa(key);
|
||||
};
|
||||
|
||||
Layout.prototype.c = function c(hash, index) {
|
||||
// 0x63
|
||||
return this.hi('c', hash, index);
|
||||
};
|
||||
|
||||
Layout.prototype.cc = function cc(key) {
|
||||
return this.hii(key);
|
||||
};
|
||||
|
||||
Layout.prototype.d = function d(hash, index) {
|
||||
// 0x64
|
||||
return this.hi('d', hash, index);
|
||||
};
|
||||
|
||||
Layout.prototype.dd = function dd(key) {
|
||||
return this.hii(key);
|
||||
};
|
||||
|
||||
Layout.prototype.s = function s(hash, index) {
|
||||
// 0x73
|
||||
return this.hi('s', hash, index);
|
||||
};
|
||||
|
||||
Layout.prototype.ss = function ss(key) {
|
||||
return this.hii(key);
|
||||
};
|
||||
|
||||
Layout.prototype.o = function o(hash, index) {
|
||||
// 0x6f
|
||||
return this.hi('o', hash, index);
|
||||
};
|
||||
|
||||
Layout.prototype.oo = function oo(key) {
|
||||
return this.hii(key);
|
||||
};
|
||||
|
||||
Layout.prototype.p = function p(hash) {
|
||||
// 0x70
|
||||
return this.ha('p', hash);
|
||||
};
|
||||
|
||||
Layout.prototype.pp = function pp(key) {
|
||||
return this.haa(key);
|
||||
};
|
||||
|
||||
Layout.prototype.m = function m(time, hash) {
|
||||
// 0x6d
|
||||
return this.ih('m', time, hash);
|
||||
};
|
||||
|
||||
Layout.prototype.mm = function mm(key) {
|
||||
return this.ihh(key);
|
||||
};
|
||||
|
||||
Layout.prototype.h = function h(height, hash) {
|
||||
// 0x68
|
||||
return this.ih('h', height, hash);
|
||||
};
|
||||
|
||||
Layout.prototype.hh = function hh(key) {
|
||||
return this.ihh(key);
|
||||
};
|
||||
|
||||
Layout.prototype.T = function T(account, hash) {
|
||||
// 0x54
|
||||
return this.ih('T', account, hash);
|
||||
};
|
||||
|
||||
Layout.prototype.Tt = function Tt(key) {
|
||||
return this.ihh(key);
|
||||
};
|
||||
|
||||
Layout.prototype.P = function P(account, hash) {
|
||||
// 0x50
|
||||
return this.ih('P', account, hash);
|
||||
};
|
||||
|
||||
Layout.prototype.Pp = function Pp(key) {
|
||||
return this.ihh(key);
|
||||
};
|
||||
|
||||
Layout.prototype.M = function M(account, time, hash) {
|
||||
// 0x4d
|
||||
return this.iih('M', account, time, hash);
|
||||
};
|
||||
|
||||
Layout.prototype.Mm = function Mm(key) {
|
||||
return this.iihh(key);
|
||||
};
|
||||
|
||||
Layout.prototype.H = function H(account, height, hash) {
|
||||
// 0x48
|
||||
return this.iih('H', account, height, hash);
|
||||
};
|
||||
|
||||
Layout.prototype.Hh = function Hh(key) {
|
||||
return this.iihh(key);
|
||||
};
|
||||
|
||||
Layout.prototype.C = function C(account, hash, index) {
|
||||
// 0x43
|
||||
return this.ihi('C', account, hash, index);
|
||||
};
|
||||
|
||||
Layout.prototype.Cc = function Cc(key) {
|
||||
return this.ihii(key);
|
||||
var layout = {
|
||||
prefix: function prefix(wid, key) {
|
||||
var out = new Buffer(5 + key.length);
|
||||
out[0] = 0x74;
|
||||
out.writeUInt32BE(wid, 1);
|
||||
key.copy(out, 5);
|
||||
return out;
|
||||
},
|
||||
hi: function hi(ch, hash, index) {
|
||||
var key = new Buffer(37);
|
||||
key[0] = ch;
|
||||
key.write(hash, 1, 'hex');
|
||||
key.writeUInt32BE(index, 33, true);
|
||||
return key;
|
||||
},
|
||||
hii: function hii(key) {
|
||||
key = key.slice(6);
|
||||
return [key.toString('hex', 0, 32), key.readUInt32BE(32, true)];
|
||||
},
|
||||
ih: function ih(ch, index, hash) {
|
||||
var key = new Buffer(37);
|
||||
key[0] = ch;
|
||||
key.writeUInt32BE(index, 1, true);
|
||||
key.write(hash, 5, 'hex');
|
||||
return key;
|
||||
},
|
||||
ihh: function ihh(key) {
|
||||
key = key.slice(6);
|
||||
return [key.readUInt32BE(0, true), key.toString('hex', 4, 36)];
|
||||
},
|
||||
iih: function iih(ch, index, num, hash) {
|
||||
var key = new Buffer(41);
|
||||
key[0] = ch;
|
||||
key.writeUInt32BE(index, 1, true);
|
||||
key.writeUInt32BE(num, 5, true);
|
||||
key.write(hash, 9, 'hex');
|
||||
return key;
|
||||
},
|
||||
iihh: function iihh(key) {
|
||||
key = key.slice(6);
|
||||
return [
|
||||
key.readUInt32BE(0, true),
|
||||
key.readUInt32BE(4, true),
|
||||
key.toString('hex', 8, 40)
|
||||
];
|
||||
},
|
||||
ihi: function ihi(ch, index, hash, num) {
|
||||
var key = new Buffer(41);
|
||||
key[0] = ch;
|
||||
key.writeUInt32BE(index, 1, true);
|
||||
key.write(hash, 5, 'hex');
|
||||
key.writeUInt32BE(num, 37, true);
|
||||
return key;
|
||||
},
|
||||
ihii: function ihii(key) {
|
||||
key = key.slice(6);
|
||||
return [
|
||||
key.readUInt32BE(0, true),
|
||||
key.toString('hex', 4, 36),
|
||||
key.readUInt32BE(36, true)
|
||||
];
|
||||
},
|
||||
ha: function ha(ch, hash) {
|
||||
var key = new Buffer(33);
|
||||
key[0] = ch;
|
||||
key.write(hash, 1, 'hex');
|
||||
return key;
|
||||
},
|
||||
haa: function haa(key) {
|
||||
return key.toString('hex', 1);
|
||||
},
|
||||
t: function t(hash) {
|
||||
return layout.ha(0x74, hash);
|
||||
},
|
||||
tt: function tt(key) {
|
||||
return layout.haa(key);
|
||||
},
|
||||
c: function c(hash, index) {
|
||||
return layout.hi(0x63, hash, index);
|
||||
},
|
||||
cc: function cc(key) {
|
||||
return layout.hii(key);
|
||||
},
|
||||
d: function d(hash, index) {
|
||||
return layout.hi(0x64, hash, index);
|
||||
},
|
||||
dd: function dd(key) {
|
||||
return layout.hii(key);
|
||||
},
|
||||
s: function s(hash, index) {
|
||||
return layout.hi(0x73, hash, index);
|
||||
},
|
||||
ss: function ss(key) {
|
||||
return layout.hii(key);
|
||||
},
|
||||
o: function o(hash, index) {
|
||||
return layout.hi(0x6f, hash, index);
|
||||
},
|
||||
oo: function oo(key) {
|
||||
return layout.hii(key);
|
||||
},
|
||||
p: function p(hash) {
|
||||
return layout.ha(0x70, hash);
|
||||
},
|
||||
pp: function pp(key) {
|
||||
return layout.haa(key);
|
||||
},
|
||||
m: function m(time, hash) {
|
||||
return layout.ih(0x6d, time, hash);
|
||||
},
|
||||
mm: function mm(key) {
|
||||
return layout.ihh(key);
|
||||
},
|
||||
h: function h(height, hash) {
|
||||
return layout.ih(0x68, height, hash);
|
||||
},
|
||||
hh: function hh(key) {
|
||||
return layout.ihh(key);
|
||||
},
|
||||
T: function T(account, hash) {
|
||||
return layout.ih(0x54, account, hash);
|
||||
},
|
||||
Tt: function Tt(key) {
|
||||
return layout.ihh(key);
|
||||
},
|
||||
P: function P(account, hash) {
|
||||
return layout.ih(0x50, account, hash);
|
||||
},
|
||||
Pp: function Pp(key) {
|
||||
return layout.ihh(key);
|
||||
},
|
||||
M: function M(account, time, hash) {
|
||||
return layout.iih(0x4d, account, time, hash);
|
||||
},
|
||||
Mm: function Mm(key) {
|
||||
return layout.iihh(key);
|
||||
},
|
||||
H: function H(account, height, hash) {
|
||||
return layout.iih(0x48, account, height, hash);
|
||||
},
|
||||
Hh: function Hh(key) {
|
||||
return layout.iihh(key);
|
||||
},
|
||||
C: function C(account, hash, index) {
|
||||
return layout.ihi(0x43, account, hash, index);
|
||||
},
|
||||
Cc: function Cc(key) {
|
||||
return layout.ihii(key);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
@ -304,7 +255,6 @@ function TXDB(wallet) {
|
||||
this.logger = wallet.db.logger;
|
||||
this.network = wallet.db.network;
|
||||
this.options = wallet.db.options;
|
||||
this.key = new Layout(wallet);
|
||||
|
||||
this.locker = new bcoin.locker(this);
|
||||
this.coinCache = new bcoin.lru(10000, 1);
|
||||
@ -361,6 +311,17 @@ TXDB.prototype._lock = function _lock(func, args, force) {
|
||||
return this.locker.lock(func, args, force);
|
||||
};
|
||||
|
||||
/**
|
||||
* Prefix a key.
|
||||
* @param {Buffer} key
|
||||
* @returns {Buffer} Prefixed key.
|
||||
*/
|
||||
|
||||
TXDB.prototype.prefix = function prefix(key) {
|
||||
assert(this.wallet.wid);
|
||||
return layout.prefix(this.wallet.wid, key);
|
||||
};
|
||||
|
||||
/**
|
||||
* Start a batch.
|
||||
* @returns {Batch}
|
||||
@ -380,7 +341,7 @@ TXDB.prototype.start = function start() {
|
||||
|
||||
TXDB.prototype.put = function put(key, value) {
|
||||
assert(this.current);
|
||||
this.current.put(key, value);
|
||||
this.current.put(this.prefix(key), value);
|
||||
};
|
||||
|
||||
/**
|
||||
@ -390,7 +351,7 @@ TXDB.prototype.put = function put(key, value) {
|
||||
|
||||
TXDB.prototype.del = function del(key) {
|
||||
assert(this.current);
|
||||
this.current.del(key);
|
||||
this.current.del(this.prefix(key));
|
||||
};
|
||||
|
||||
/**
|
||||
@ -420,7 +381,7 @@ TXDB.prototype.drop = function drop() {
|
||||
*/
|
||||
|
||||
TXDB.prototype.fetch = function fetch(key, parse, callback) {
|
||||
this.db.fetch(key, parse, callback);
|
||||
this.db.fetch(this.prefix(key), parse, callback);
|
||||
};
|
||||
|
||||
/**
|
||||
@ -429,7 +390,7 @@ TXDB.prototype.fetch = function fetch(key, parse, callback) {
|
||||
*/
|
||||
|
||||
TXDB.prototype.get = function get(key, callback) {
|
||||
this.db.get(key, callback);
|
||||
this.db.get(this.prefix(key), callback);
|
||||
};
|
||||
|
||||
/**
|
||||
@ -438,7 +399,7 @@ TXDB.prototype.get = function get(key, callback) {
|
||||
*/
|
||||
|
||||
TXDB.prototype.has = function has(key, callback) {
|
||||
this.db.has(key, callback);
|
||||
this.db.has(this.prefix(key), callback);
|
||||
};
|
||||
|
||||
/**
|
||||
@ -448,6 +409,10 @@ TXDB.prototype.has = function has(key, callback) {
|
||||
*/
|
||||
|
||||
TXDB.prototype.iterate = function iterate(options, callback) {
|
||||
if (options.gte)
|
||||
options.gte = this.prefix(options.gte);
|
||||
if (options.lte)
|
||||
options.lte = this.prefix(options.lte);
|
||||
this.db.iterate(options, callback);
|
||||
};
|
||||
|
||||
@ -491,7 +456,7 @@ TXDB.prototype.getInfo = function getInfo(tx, callback) {
|
||||
TXDB.prototype._addOrphan = function _addOrphan(prevout, spender, callback) {
|
||||
var self = this;
|
||||
var p = new BufferWriter();
|
||||
var key = this.key.o(prevout.hash, prevout.index);
|
||||
var key = layout.o(prevout.hash, prevout.index);
|
||||
|
||||
this.get(key, function(err, data) {
|
||||
if (err)
|
||||
@ -520,7 +485,7 @@ TXDB.prototype._getOrphans = function _getOrphans(hash, index, callback) {
|
||||
var self = this;
|
||||
var items = [];
|
||||
|
||||
this.fetch(this.key.o(hash, index), function(data) {
|
||||
this.fetch(layout.o(hash, index), function(data) {
|
||||
var p = new BufferReader(data);
|
||||
var orphans = [];
|
||||
|
||||
@ -666,7 +631,7 @@ TXDB.prototype._resolveOrphans = function _resolveOrphans(tx, index, callback) {
|
||||
if (!orphans)
|
||||
return callback(null, false);
|
||||
|
||||
self.del(self.key.o(hash, index));
|
||||
self.del(layout.o(hash, index));
|
||||
|
||||
coin = bcoin.coin.fromTX(tx, index);
|
||||
|
||||
@ -687,7 +652,7 @@ TXDB.prototype._resolveOrphans = function _resolveOrphans(tx, index, callback) {
|
||||
// Verify that input script is correct, if not - add
|
||||
// output to unspent and remove orphan from storage
|
||||
if (!self.options.verify || orphan.verifyInput(input.index)) {
|
||||
self.put(self.key.d(input.hash, input.index), coin.toRaw());
|
||||
self.put(layout.d(input.hash, input.index), coin.toRaw());
|
||||
return callback(null, true);
|
||||
}
|
||||
|
||||
@ -745,23 +710,23 @@ TXDB.prototype.add = function add(tx, info, callback) {
|
||||
hash = tx.hash('hex');
|
||||
|
||||
self.start();
|
||||
self.put(self.key.t(hash), tx.toExtended());
|
||||
self.put(layout.t(hash), tx.toExtended());
|
||||
|
||||
if (tx.ts === 0)
|
||||
self.put(self.key.p(hash), DUMMY);
|
||||
self.put(layout.p(hash), DUMMY);
|
||||
else
|
||||
self.put(self.key.h(tx.height, hash), DUMMY);
|
||||
self.put(layout.h(tx.height, hash), DUMMY);
|
||||
|
||||
self.put(self.key.m(tx.ps, hash), DUMMY);
|
||||
self.put(layout.m(tx.ps, hash), DUMMY);
|
||||
|
||||
for (i = 0; i < info.accounts.length; i++) {
|
||||
account = info.accounts[i];
|
||||
self.put(self.key.T(account, hash), DUMMY);
|
||||
self.put(layout.T(account, hash), DUMMY);
|
||||
if (tx.ts === 0)
|
||||
self.put(self.key.P(account, hash), DUMMY);
|
||||
self.put(layout.P(account, hash), DUMMY);
|
||||
else
|
||||
self.put(self.key.H(account, tx.height, hash), DUMMY);
|
||||
self.put(self.key.M(account, tx.ps, hash), DUMMY);
|
||||
self.put(layout.H(account, tx.height, hash), DUMMY);
|
||||
self.put(layout.M(account, tx.ps, hash), DUMMY);
|
||||
}
|
||||
|
||||
// Consume unspent money or add orphans
|
||||
@ -783,15 +748,15 @@ TXDB.prototype.add = function add(tx, info, callback) {
|
||||
|
||||
// s/[outpoint-key] -> [spender-hash]|[spender-input-index]
|
||||
spender = bcoin.outpoint.fromTX(tx, i).toRaw();
|
||||
self.put(self.key.s(prevout.hash, prevout.index), spender);
|
||||
self.put(layout.s(prevout.hash, prevout.index), spender);
|
||||
|
||||
// Add orphan, if no parent transaction is yet known
|
||||
if (!input.coin)
|
||||
return self._addOrphan(prevout, spender, next);
|
||||
|
||||
self.del(self.key.c(prevout.hash, prevout.index));
|
||||
self.del(self.key.C(path.account, prevout.hash, prevout.index));
|
||||
self.put(self.key.d(hash, i), input.coin.toRaw());
|
||||
self.del(layout.c(prevout.hash, prevout.index));
|
||||
self.del(layout.C(path.account, prevout.hash, prevout.index));
|
||||
self.put(layout.d(hash, i), input.coin.toRaw());
|
||||
self.balance.sub(input.coin);
|
||||
|
||||
self.coinCache.remove(key);
|
||||
@ -826,8 +791,8 @@ TXDB.prototype.add = function add(tx, info, callback) {
|
||||
self.balance.add(coin);
|
||||
coin = coin.toRaw();
|
||||
|
||||
self.put(self.key.c(hash, i), coin);
|
||||
self.put(self.key.C(path.account, hash, i), DUMMY);
|
||||
self.put(layout.c(hash, i), coin);
|
||||
self.put(layout.C(path.account, hash, i), DUMMY);
|
||||
|
||||
self.coinCache.set(key, coin);
|
||||
|
||||
@ -993,7 +958,7 @@ TXDB.prototype.isDoubleSpend = function isDoubleSpend(tx, callback) {
|
||||
*/
|
||||
|
||||
TXDB.prototype.isSpent = function isSpent(hash, index, callback) {
|
||||
var key = this.key.s(hash, index);
|
||||
var key = layout.s(hash, index);
|
||||
this.fetch(key, function(data) {
|
||||
return bcoin.outpoint.fromRaw(data);
|
||||
}, callback);
|
||||
@ -1040,15 +1005,15 @@ TXDB.prototype._confirm = function _confirm(tx, info, callback) {
|
||||
|
||||
self.start();
|
||||
|
||||
self.put(self.key.t(hash), tx.toExtended());
|
||||
self.put(layout.t(hash), tx.toExtended());
|
||||
|
||||
self.del(self.key.p(hash));
|
||||
self.put(self.key.h(tx.height, hash), DUMMY);
|
||||
self.del(layout.p(hash));
|
||||
self.put(layout.h(tx.height, hash), DUMMY);
|
||||
|
||||
for (i = 0; i < info.accounts.length; i++) {
|
||||
account = info.accounts[i];
|
||||
self.del(self.key.P(account, hash));
|
||||
self.put(self.key.H(account, tx.height, hash), DUMMY);
|
||||
self.del(layout.P(account, hash));
|
||||
self.put(layout.H(account, tx.height, hash), DUMMY);
|
||||
}
|
||||
|
||||
utils.forEachSerial(tx.outputs, function(output, next, i) {
|
||||
@ -1073,7 +1038,7 @@ TXDB.prototype._confirm = function _confirm(tx, info, callback) {
|
||||
coin.height = tx.height;
|
||||
coin = coin.toRaw();
|
||||
|
||||
self.put(self.key.c(hash, i), coin);
|
||||
self.put(layout.c(hash, i), coin);
|
||||
|
||||
self.coinCache.set(key, coin);
|
||||
|
||||
@ -1154,23 +1119,23 @@ TXDB.prototype._remove = function remove(tx, info, callback) {
|
||||
var hash = tx.hash('hex');
|
||||
var i, path, account, key, address, input, output, coin;
|
||||
|
||||
this.del(this.key.t(hash));
|
||||
this.del(layout.t(hash));
|
||||
|
||||
if (tx.ts === 0)
|
||||
this.del(this.key.p(hash));
|
||||
this.del(layout.p(hash));
|
||||
else
|
||||
this.del(this.key.h(tx.height, hash));
|
||||
this.del(layout.h(tx.height, hash));
|
||||
|
||||
this.del(this.key.m(tx.ps, hash));
|
||||
this.del(layout.m(tx.ps, hash));
|
||||
|
||||
for (i = 0; i < info.accounts.length; i++) {
|
||||
account = info.accounts[i];
|
||||
this.del(this.key.T(account, hash));
|
||||
this.del(layout.T(account, hash));
|
||||
if (tx.ts === 0)
|
||||
this.del(this.key.P(account, hash));
|
||||
this.del(layout.P(account, hash));
|
||||
else
|
||||
this.del(this.key.H(account, tx.height, hash));
|
||||
this.del(this.key.M(account, tx.ps, hash));
|
||||
this.del(layout.H(account, tx.height, hash));
|
||||
this.del(layout.M(account, tx.ps, hash));
|
||||
}
|
||||
|
||||
this.fillHistory(tx, function(err) {
|
||||
@ -1198,11 +1163,11 @@ TXDB.prototype._remove = function remove(tx, info, callback) {
|
||||
|
||||
coin = input.coin.toRaw();
|
||||
|
||||
self.put(self.key.c(prevout.hash, prevout.index), coin);
|
||||
self.put(self.key.C(path.account, prevout.hash, prevout.index), DUMMY);
|
||||
self.del(self.key.d(hash, i));
|
||||
self.del(self.key.s(prevout.hash, prevout.index));
|
||||
self.del(self.key.o(prevout.hash, prevout.index));
|
||||
self.put(layout.c(prevout.hash, prevout.index), coin);
|
||||
self.put(layout.C(path.account, prevout.hash, prevout.index), DUMMY);
|
||||
self.del(layout.d(hash, i));
|
||||
self.del(layout.s(prevout.hash, prevout.index));
|
||||
self.del(layout.o(prevout.hash, prevout.index));
|
||||
|
||||
self.coinCache.set(key, coin);
|
||||
}
|
||||
@ -1221,8 +1186,8 @@ TXDB.prototype._remove = function remove(tx, info, callback) {
|
||||
|
||||
self.balance.sub(coin);
|
||||
|
||||
self.del(self.key.c(hash, i));
|
||||
self.del(self.key.C(path.account, hash, i));
|
||||
self.del(layout.c(hash, i));
|
||||
self.del(layout.C(path.account, hash, i));
|
||||
|
||||
self.coinCache.remove(key);
|
||||
}
|
||||
@ -1301,15 +1266,15 @@ TXDB.prototype._unconfirm = function unconfirm(tx, info, callback, force) {
|
||||
tx.index = -1;
|
||||
tx.block = null;
|
||||
|
||||
this.put(this.key.t(hash), tx.toExtended());
|
||||
this.put(layout.t(hash), tx.toExtended());
|
||||
|
||||
this.put(this.key.p(hash), DUMMY);
|
||||
this.del(this.key.h(height, hash));
|
||||
this.put(layout.p(hash), DUMMY);
|
||||
this.del(layout.h(height, hash));
|
||||
|
||||
for (i = 0; i < info.accounts.length; i++) {
|
||||
account = info.accounts[i];
|
||||
this.put(this.key.P(account, hash), DUMMY);
|
||||
this.del(this.key.H(account, height, hash));
|
||||
this.put(layout.P(account, hash), DUMMY);
|
||||
this.del(layout.H(account, height, hash));
|
||||
}
|
||||
|
||||
utils.forEachSerial(tx.outputs, function(output, next, i) {
|
||||
@ -1327,7 +1292,7 @@ TXDB.prototype._unconfirm = function unconfirm(tx, info, callback, force) {
|
||||
coin.height = tx.height;
|
||||
coin = coin.toRaw();
|
||||
|
||||
self.put(self.key.c(hash, i), coin);
|
||||
self.put(layout.c(hash, i), coin);
|
||||
|
||||
self.coinCache.set(key, coin);
|
||||
|
||||
@ -1359,17 +1324,17 @@ TXDB.prototype.getHistoryHashes = function getHistoryHashes(account, callback) {
|
||||
|
||||
this.iterate({
|
||||
gte: account != null
|
||||
? this.key.T(account, constants.NULL_HASH)
|
||||
: this.key.t(constants.NULL_HASH),
|
||||
? layout.T(account, constants.NULL_HASH)
|
||||
: layout.t(constants.NULL_HASH),
|
||||
lte: account != null
|
||||
? this.key.T(account, constants.HIGH_HASH)
|
||||
: this.key.t(constants.HIGH_HASH),
|
||||
? layout.T(account, constants.HIGH_HASH)
|
||||
: layout.t(constants.HIGH_HASH),
|
||||
transform: function(key) {
|
||||
if (account != null) {
|
||||
key = self.key.Tt(key);
|
||||
key = layout.Tt(key);
|
||||
return key[1];
|
||||
}
|
||||
key = self.key.tt(key);
|
||||
key = layout.tt(key);
|
||||
return key[0];
|
||||
}
|
||||
}, callback);
|
||||
@ -1391,17 +1356,17 @@ TXDB.prototype.getUnconfirmedHashes = function getUnconfirmedHashes(account, cal
|
||||
|
||||
this.iterate({
|
||||
gte: account != null
|
||||
? this.key.P(account, constants.NULL_HASH)
|
||||
: this.key.p(constants.NULL_HASH),
|
||||
? layout.P(account, constants.NULL_HASH)
|
||||
: layout.p(constants.NULL_HASH),
|
||||
lte: account != null
|
||||
? this.key.P(account, constants.HIGH_HASH)
|
||||
: this.key.p(constants.HIGH_HASH),
|
||||
? layout.P(account, constants.HIGH_HASH)
|
||||
: layout.p(constants.HIGH_HASH),
|
||||
transform: function(key) {
|
||||
if (account != null) {
|
||||
key = self.key.Pp(key);
|
||||
key = layout.Pp(key);
|
||||
return key[1];
|
||||
}
|
||||
key = self.key.pp(key);
|
||||
key = layout.pp(key);
|
||||
return key[0];
|
||||
}
|
||||
}, callback);
|
||||
@ -1423,17 +1388,17 @@ TXDB.prototype.getCoinHashes = function getCoinHashes(account, callback) {
|
||||
|
||||
this.iterate({
|
||||
gte: account != null
|
||||
? this.key.C(account, constants.NULL_HASH, 0)
|
||||
: this.key.c(constants.NULL_HASH, 0),
|
||||
? layout.C(account, constants.NULL_HASH, 0)
|
||||
: layout.c(constants.NULL_HASH, 0),
|
||||
lte: account != null
|
||||
? this.key.C(account, constants.HIGH_HASH, 0xffffffff)
|
||||
: this.key.c(constants.HIGH_HASH, 0xffffffff),
|
||||
? layout.C(account, constants.HIGH_HASH, 0xffffffff)
|
||||
: layout.c(constants.HIGH_HASH, 0xffffffff),
|
||||
transform: function(key) {
|
||||
if (account != null) {
|
||||
key = self.key.Cc(key);
|
||||
key = layout.Cc(key);
|
||||
return [key[1], key[2]];
|
||||
}
|
||||
key = self.key.cc(key);
|
||||
key = layout.cc(key);
|
||||
return key;
|
||||
}
|
||||
}, callback);
|
||||
@ -1461,19 +1426,19 @@ TXDB.prototype.getHeightRangeHashes = function getHeightRangeHashes(account, opt
|
||||
|
||||
this.iterate({
|
||||
gte: account != null
|
||||
? this.key.H(account, options.start, constants.NULL_HASH)
|
||||
: this.key.h(options.start, constants.NULL_HASH),
|
||||
? layout.H(account, options.start, constants.NULL_HASH)
|
||||
: layout.h(options.start, constants.NULL_HASH),
|
||||
lte: account != null
|
||||
? this.key.H(account, options.end, constants.HIGH_HASH)
|
||||
: this.key.h(options.end, constants.HIGH_HASH),
|
||||
? layout.H(account, options.end, constants.HIGH_HASH)
|
||||
: layout.h(options.end, constants.HIGH_HASH),
|
||||
limit: options.limit,
|
||||
reverse: options.reverse,
|
||||
transform: function(key) {
|
||||
if (account != null) {
|
||||
key = self.key.Hh(key);
|
||||
key = layout.Hh(key);
|
||||
return key[2];
|
||||
}
|
||||
key = self.key.hh(key);
|
||||
key = layout.hh(key);
|
||||
return key[1];
|
||||
}
|
||||
}, callback);
|
||||
@ -1510,19 +1475,19 @@ TXDB.prototype.getRangeHashes = function getRangeHashes(account, options, callba
|
||||
|
||||
this.iterate({
|
||||
gte: account != null
|
||||
? this.key.M(account, options.start, constants.NULL_HASH)
|
||||
: this.key.m(options.start, constants.NULL_HASH),
|
||||
? layout.M(account, options.start, constants.NULL_HASH)
|
||||
: layout.m(options.start, constants.NULL_HASH),
|
||||
lte: account != null
|
||||
? this.key.M(account, options.end, constants.HIGH_HASH)
|
||||
: this.key.m(options.end, constants.HIGH_HASH),
|
||||
? layout.M(account, options.end, constants.HIGH_HASH)
|
||||
: layout.m(options.end, constants.HIGH_HASH),
|
||||
limit: options.limit,
|
||||
reverse: options.reverse,
|
||||
transform: function(key) {
|
||||
if (account != null) {
|
||||
key = self.key.Mm(key);
|
||||
key = layout.Mm(key);
|
||||
return key[2];
|
||||
}
|
||||
key = self.key.mm(key);
|
||||
key = layout.mm(key);
|
||||
return key[1];
|
||||
}
|
||||
}, callback);
|
||||
@ -1613,8 +1578,8 @@ TXDB.prototype.getHistory = function getHistory(account, callback) {
|
||||
|
||||
// Fast case
|
||||
this.iterate({
|
||||
gte: this.key.t(constants.NULL_HASH),
|
||||
lte: this.key.t(constants.HIGH_HASH),
|
||||
gte: layout.t(constants.NULL_HASH),
|
||||
lte: layout.t(constants.HIGH_HASH),
|
||||
values: true,
|
||||
parse: function(data) {
|
||||
return bcoin.tx.fromExtended(data);
|
||||
@ -1757,12 +1722,12 @@ TXDB.prototype.getCoins = function getCoins(account, callback) {
|
||||
|
||||
// Fast case
|
||||
this.iterate({
|
||||
gte: this.key.c(constants.NULL_HASH, 0),
|
||||
lte: this.key.c(constants.HIGH_HASH, 0xffffffff),
|
||||
gte: layout.c(constants.NULL_HASH, 0),
|
||||
lte: layout.c(constants.HIGH_HASH, 0xffffffff),
|
||||
keys: true,
|
||||
values: true,
|
||||
parse: function(data, key) {
|
||||
var parts = self.key.cc(key);
|
||||
var parts = layout.cc(key);
|
||||
var hash = parts[0];
|
||||
var index = parts[1];
|
||||
var coin = bcoin.coin.fromRaw(data);
|
||||
@ -1828,12 +1793,12 @@ TXDB.prototype.fillHistory = function fillHistory(tx, callback) {
|
||||
hash = tx.hash('hex');
|
||||
|
||||
this.iterate({
|
||||
gte: this.key.d(hash, 0),
|
||||
lte: this.key.d(hash, 0xffffffff),
|
||||
gte: layout.d(hash, 0),
|
||||
lte: layout.d(hash, 0xffffffff),
|
||||
keys: true,
|
||||
values: true,
|
||||
parse: function(value, key) {
|
||||
index = self.key.dd(key)[1];
|
||||
index = layout.dd(key)[1];
|
||||
coin = bcoin.coin.fromRaw(value);
|
||||
input = tx.inputs[index];
|
||||
coin.hash = input.prevout.hash;
|
||||
@ -1890,7 +1855,7 @@ TXDB.prototype.fillCoins = function fillCoins(tx, callback) {
|
||||
*/
|
||||
|
||||
TXDB.prototype.getTX = function getTX(hash, callback) {
|
||||
this.fetch(this.key.t(hash), function(tx) {
|
||||
this.fetch(layout.t(hash), function(tx) {
|
||||
return bcoin.tx.fromExtended(tx);
|
||||
}, callback);
|
||||
};
|
||||
@ -1967,7 +1932,7 @@ TXDB.prototype.toDetails = function toDetails(tx, callback) {
|
||||
*/
|
||||
|
||||
TXDB.prototype.hasTX = function hasTX(hash, callback) {
|
||||
this.has(this.key.t(hash), callback);
|
||||
this.has(layout.t(hash), callback);
|
||||
};
|
||||
|
||||
/**
|
||||
@ -1993,7 +1958,7 @@ TXDB.prototype.getCoin = function getCoin(hash, index, callback) {
|
||||
return callback(null, coin);
|
||||
}
|
||||
|
||||
this.fetch(this.key.c(hash, index), function(data) {
|
||||
this.fetch(layout.c(hash, index), function(data) {
|
||||
coin = bcoin.coin.fromRaw(data);
|
||||
coin.hash = hash;
|
||||
coin.index = index;
|
||||
@ -2014,7 +1979,7 @@ TXDB.prototype.hasCoin = function hasCoin(hash, index, callback) {
|
||||
if (this.coinCache.has(key))
|
||||
return callback(null, true);
|
||||
|
||||
this.has(this.key.c(hash, index), callback);
|
||||
this.has(layout.c(hash, index), callback);
|
||||
};
|
||||
|
||||
/**
|
||||
@ -2044,12 +2009,12 @@ TXDB.prototype.getBalance = function getBalance(account, callback) {
|
||||
balance = new Balance(this.wallet);
|
||||
|
||||
this.iterate({
|
||||
gte: this.key.c(constants.NULL_HASH, 0),
|
||||
lte: this.key.c(constants.HIGH_HASH, 0xffffffff),
|
||||
gte: layout.c(constants.NULL_HASH, 0),
|
||||
lte: layout.c(constants.HIGH_HASH, 0xffffffff),
|
||||
keys: true,
|
||||
values: true,
|
||||
parse: function(data, key) {
|
||||
var parts = self.key.cc(key);
|
||||
var parts = layout.cc(key);
|
||||
var hash = parts[0];
|
||||
var index = parts[1];
|
||||
var height = data.readUInt32LE(4, true);
|
||||
@ -2118,7 +2083,7 @@ TXDB.prototype.getAccountBalance = function getBalance(account, callback) {
|
||||
return next();
|
||||
}
|
||||
|
||||
self.get(self.key.c(hash[0], hash[1]), function(err, data) {
|
||||
self.get(layout.c(hash[0], hash[1]), function(err, data) {
|
||||
if (err)
|
||||
return next(err);
|
||||
|
||||
@ -2194,7 +2159,7 @@ TXDB.prototype.zap = function zap(account, age, callback, force) {
|
||||
|
||||
TXDB.prototype.abandon = function abandon(hash, callback, force) {
|
||||
var self = this;
|
||||
this.has(this.key.p(hash), function(err, result) {
|
||||
this.has(layout.p(hash), function(err, result) {
|
||||
if (err)
|
||||
return callback(err);
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user