db: migrate to key objects.
This commit is contained in:
parent
a8e2001015
commit
167304666b
@ -496,7 +496,7 @@ class ChainDB {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
async getState() {
|
async getState() {
|
||||||
const data = await this.db.get(layout.R);
|
const data = await this.db.get(layout.R());
|
||||||
|
|
||||||
if (!data)
|
if (!data)
|
||||||
return null;
|
return null;
|
||||||
@ -525,7 +525,7 @@ class ChainDB {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
async getFlags() {
|
async getFlags() {
|
||||||
const data = await this.db.get(layout.O);
|
const data = await this.db.get(layout.O());
|
||||||
|
|
||||||
if (!data)
|
if (!data)
|
||||||
return null;
|
return null;
|
||||||
@ -660,7 +660,7 @@ class ChainDB {
|
|||||||
bw.writeI32(deployment.window);
|
bw.writeI32(deployment.window);
|
||||||
}
|
}
|
||||||
|
|
||||||
batch.put(layout.V, bw.render());
|
batch.put(layout.V(), bw.render());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -670,7 +670,7 @@ class ChainDB {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
async checkDeployments() {
|
async checkDeployments() {
|
||||||
const raw = await this.db.get(layout.V);
|
const raw = await this.db.get(layout.V());
|
||||||
|
|
||||||
assert(raw, 'No deployment table found.');
|
assert(raw, 'No deployment table found.');
|
||||||
|
|
||||||
@ -792,7 +792,7 @@ class ChainDB {
|
|||||||
const flags = ChainFlags.fromOptions(options);
|
const flags = ChainFlags.fromOptions(options);
|
||||||
assert(flags.prune);
|
assert(flags.prune);
|
||||||
|
|
||||||
batch.put(layout.O, flags.toRaw());
|
batch.put(layout.O(), flags.toRaw());
|
||||||
|
|
||||||
await batch.write();
|
await batch.write();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
@ -1414,7 +1414,7 @@ class ChainDB {
|
|||||||
await this.saveBlock(entry, block, view);
|
await this.saveBlock(entry, block, view);
|
||||||
|
|
||||||
// Commit new chain state.
|
// Commit new chain state.
|
||||||
this.put(layout.R, this.pending.commit(hash));
|
this.put(layout.R(), this.pending.commit(hash));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -1467,7 +1467,7 @@ class ChainDB {
|
|||||||
await this.connectBlock(entry, block, view);
|
await this.connectBlock(entry, block, view);
|
||||||
|
|
||||||
// Update chain state.
|
// Update chain state.
|
||||||
this.put(layout.R, this.pending.commit(hash));
|
this.put(layout.R(), this.pending.commit(hash));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -1516,7 +1516,7 @@ class ChainDB {
|
|||||||
const view = await this.disconnectBlock(entry, block);
|
const view = await this.disconnectBlock(entry, block);
|
||||||
|
|
||||||
// Revert chain state to previous tip.
|
// Revert chain state to previous tip.
|
||||||
this.put(layout.R, this.pending.commit(entry.prevBlock));
|
this.put(layout.R(), this.pending.commit(entry.prevBlock));
|
||||||
|
|
||||||
return view;
|
return view;
|
||||||
}
|
}
|
||||||
@ -1575,7 +1575,7 @@ class ChainDB {
|
|||||||
|
|
||||||
// Stop once we hit our target tip.
|
// Stop once we hit our target tip.
|
||||||
if (tip.hash === entry.hash) {
|
if (tip.hash === entry.hash) {
|
||||||
this.put(layout.R, this.pending.commit(tip.hash));
|
this.put(layout.R(), this.pending.commit(tip.hash));
|
||||||
await this.commit();
|
await this.commit();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -1602,7 +1602,7 @@ class ChainDB {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Revert chain state to previous tip.
|
// Revert chain state to previous tip.
|
||||||
this.put(layout.R, this.pending.commit(tip.prevBlock));
|
this.put(layout.R(), this.pending.commit(tip.prevBlock));
|
||||||
|
|
||||||
await this.commit();
|
await this.commit();
|
||||||
|
|
||||||
@ -1891,7 +1891,7 @@ class ChainDB {
|
|||||||
saveFlags() {
|
saveFlags() {
|
||||||
const flags = ChainFlags.fromOptions(this.options);
|
const flags = ChainFlags.fromOptions(this.options);
|
||||||
const batch = this.db.batch();
|
const batch = this.db.batch();
|
||||||
batch.put(layout.O, flags.toRaw());
|
batch.put(layout.O(), flags.toRaw());
|
||||||
return batch.write();
|
return batch.write();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -7,6 +7,7 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const assert = require('assert');
|
const assert = require('assert');
|
||||||
|
const Key = require('bdb/lib/key');
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Database Layout:
|
* Database Layout:
|
||||||
@ -29,168 +30,69 @@ const assert = require('assert');
|
|||||||
* W+C[witaddr-hash][hash][index] -> dummy (coin by address)
|
* W+C[witaddr-hash][hash][index] -> dummy (coin by address)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
const R = new Key('R');
|
||||||
|
const O = new Key('O');
|
||||||
|
const V = new Key('v');
|
||||||
|
const e = new Key('e', ['hash256']);
|
||||||
|
const h = new Key('h', ['hash256']);
|
||||||
|
const H = new Key('H', ['uint32']);
|
||||||
|
const n = new Key('n', ['hash256']);
|
||||||
|
const p = new Key('p', ['hash256']);
|
||||||
|
const b = new Key('b', ['hash256']);
|
||||||
|
const t = new Key('t', ['hash256']);
|
||||||
|
const c = new Key('c', ['hash256', 'uint32']);
|
||||||
|
const u = new Key('u', ['hash256']);
|
||||||
|
const v = new Key('v', ['uint8', 'hash256']);
|
||||||
|
const T160 = new Key('T', ['hash160', 'hash256']);
|
||||||
|
const T256 = new Key(0xab, ['hash256', 'hash256']);
|
||||||
|
const C160 = new Key('C', ['hash160', 'hash256', 'uint32']);
|
||||||
|
const C256 = new Key(0x9a, ['hash256', 'hash256', 'uint32']);
|
||||||
|
|
||||||
const layout = {
|
const layout = {
|
||||||
binary: true,
|
binary: true,
|
||||||
R: Buffer.from([0x52]),
|
R: R.build.bind(R),
|
||||||
O: Buffer.from([0x4f]),
|
O: O.build.bind(O),
|
||||||
V: Buffer.from([0x76]),
|
V: V.build.bind(V),
|
||||||
e: function e(hash) {
|
e: e.build.bind(e),
|
||||||
return pair(0x65, hash);
|
h: h.build.bind(h),
|
||||||
},
|
H: H.build.bind(H),
|
||||||
h: function h(hash) {
|
n: n.build.bind(n),
|
||||||
return pair(0x68, hash);
|
p: p.build.bind(p),
|
||||||
},
|
pp: p.parse.bind(p),
|
||||||
H: function H(height) {
|
b: b.build.bind(b),
|
||||||
return ipair(0x48, height);
|
t: t.build.bind(t),
|
||||||
},
|
c: c.build.bind(c),
|
||||||
n: function n(hash) {
|
u: u.build.bind(u),
|
||||||
return pair(0x6e, hash);
|
v: v.build.bind(v),
|
||||||
},
|
vv: v.parse.bind(v),
|
||||||
p: function p(hash) {
|
|
||||||
return pair(0x70, hash);
|
|
||||||
},
|
|
||||||
b: function b(hash) {
|
|
||||||
return pair(0x62, hash);
|
|
||||||
},
|
|
||||||
t: function t(hash) {
|
|
||||||
return pair(0x74, hash);
|
|
||||||
},
|
|
||||||
c: function c(hash, index) {
|
|
||||||
return bpair(0x63, hash, index);
|
|
||||||
},
|
|
||||||
u: function u(hash) {
|
|
||||||
return pair(0x75, hash);
|
|
||||||
},
|
|
||||||
v: function v(bit, hash) {
|
|
||||||
const key = Buffer.allocUnsafe(1 + 1 + 32);
|
|
||||||
assert(typeof bit === 'number');
|
|
||||||
key[0] = 0x76;
|
|
||||||
key[1] = bit;
|
|
||||||
write(key, hash, 2);
|
|
||||||
return key;
|
|
||||||
},
|
|
||||||
vv: function vv(key) {
|
|
||||||
assert(Buffer.isBuffer(key));
|
|
||||||
assert(key.length === 34);
|
|
||||||
return [key[1], key.toString('hex', 2, 34)];
|
|
||||||
},
|
|
||||||
T: function T(addr, hash) {
|
T: function T(addr, hash) {
|
||||||
let len = addr.length;
|
let len = addr ? addr.length : 0;
|
||||||
|
|
||||||
if (typeof addr === 'string')
|
if (typeof addr === 'string')
|
||||||
len /= 2;
|
len >>>= 1;
|
||||||
|
if (len === 32)
|
||||||
let key;
|
return T256.build(addr, hash);
|
||||||
if (len === 32) {
|
return T160.build(addr, hash);
|
||||||
key = Buffer.allocUnsafe(65);
|
|
||||||
key[0] = 0xab; // W + T
|
|
||||||
write(key, addr, 1);
|
|
||||||
write(key, hash, 33);
|
|
||||||
} else if (len === 20) {
|
|
||||||
key = Buffer.allocUnsafe(53);
|
|
||||||
key[0] = 0x54; // T
|
|
||||||
write(key, addr, 1);
|
|
||||||
write(key, hash, 21);
|
|
||||||
} else {
|
|
||||||
assert(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
return key;
|
|
||||||
},
|
|
||||||
C: function C(addr, hash, index) {
|
|
||||||
let len = addr.length;
|
|
||||||
|
|
||||||
assert(typeof index === 'number');
|
|
||||||
|
|
||||||
if (typeof addr === 'string')
|
|
||||||
len /= 2;
|
|
||||||
|
|
||||||
let key;
|
|
||||||
if (len === 32) {
|
|
||||||
key = Buffer.allocUnsafe(69);
|
|
||||||
key[0] = 0x9a; // W + C
|
|
||||||
write(key, addr, 1);
|
|
||||||
write(key, hash, 33);
|
|
||||||
key.writeUInt32BE(index, 65, true);
|
|
||||||
} else if (len === 20) {
|
|
||||||
key = Buffer.allocUnsafe(57);
|
|
||||||
key[0] = 0x43; // C
|
|
||||||
write(key, addr, 1);
|
|
||||||
write(key, hash, 21);
|
|
||||||
key.writeUInt32BE(index, 53, true);
|
|
||||||
} else {
|
|
||||||
assert(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
return key;
|
|
||||||
},
|
|
||||||
pp: function pp(key) {
|
|
||||||
assert(Buffer.isBuffer(key));
|
|
||||||
assert(key.length === 33);
|
|
||||||
return key.toString('hex', 1, 33);
|
|
||||||
},
|
|
||||||
Cc: function Cc(key) {
|
|
||||||
assert(Buffer.isBuffer(key));
|
|
||||||
|
|
||||||
let hash, index;
|
|
||||||
if (key.length === 69) {
|
|
||||||
hash = key.toString('hex', 33, 65);
|
|
||||||
index = key.readUInt32BE(65, 0);
|
|
||||||
} else if (key.length === 57) {
|
|
||||||
hash = key.toString('hex', 21, 53);
|
|
||||||
index = key.readUInt32BE(53, 0);
|
|
||||||
} else {
|
|
||||||
assert(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
return [hash, index];
|
|
||||||
},
|
},
|
||||||
Tt: function Tt(key) {
|
Tt: function Tt(key) {
|
||||||
assert(Buffer.isBuffer(key));
|
if (key && key[0] === 0xab)
|
||||||
|
return T256.parse(key);
|
||||||
if (key.length === 65)
|
return T160.parse(key);
|
||||||
return key.toString('hex', 33, 65);
|
},
|
||||||
|
C: function C(addr, hash, index) {
|
||||||
assert(key.length === 53);
|
let len = addr ? addr.length : 0;
|
||||||
return key.toString('hex', 21, 53);
|
if (typeof addr === 'string')
|
||||||
|
len >>>= 1;
|
||||||
|
if (len === 32)
|
||||||
|
return C256.build(addr, hash, index);
|
||||||
|
return C160.build(addr, hash, index);
|
||||||
|
},
|
||||||
|
Cc: function Cc(key) {
|
||||||
|
if (key && key[0] === 0x9a)
|
||||||
|
return C256.parse(key);
|
||||||
|
return C160.parse(key);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
|
||||||
* Helpers
|
|
||||||
*/
|
|
||||||
|
|
||||||
function write(data, str, off) {
|
|
||||||
if (Buffer.isBuffer(str))
|
|
||||||
return str.copy(data, off);
|
|
||||||
assert(typeof str === 'string');
|
|
||||||
return data.write(str, off, 'hex');
|
|
||||||
}
|
|
||||||
|
|
||||||
function pair(prefix, hash) {
|
|
||||||
const key = Buffer.allocUnsafe(33);
|
|
||||||
key[0] = prefix;
|
|
||||||
write(key, hash, 1);
|
|
||||||
return key;
|
|
||||||
}
|
|
||||||
|
|
||||||
function ipair(prefix, num) {
|
|
||||||
const key = Buffer.allocUnsafe(5);
|
|
||||||
assert(typeof num === 'number');
|
|
||||||
key[0] = prefix;
|
|
||||||
key.writeUInt32BE(num, 1, true);
|
|
||||||
return key;
|
|
||||||
}
|
|
||||||
|
|
||||||
function bpair(prefix, hash, index) {
|
|
||||||
const key = Buffer.allocUnsafe(37);
|
|
||||||
assert(typeof index === 'number');
|
|
||||||
key[0] = prefix;
|
|
||||||
write(key, hash, 1);
|
|
||||||
key.writeUInt32BE(index, 33, true);
|
|
||||||
return key;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Expose
|
* Expose
|
||||||
*/
|
*/
|
||||||
|
|||||||
@ -7,6 +7,7 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const assert = require('assert');
|
const assert = require('assert');
|
||||||
|
const Key = require('bdb/lib/key');
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Database Layout:
|
* Database Layout:
|
||||||
@ -15,35 +16,20 @@ const assert = require('assert');
|
|||||||
* e[id][hash] -> entry
|
* e[id][hash] -> entry
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
const R = new Key('R');
|
||||||
|
const V = new Key('v');
|
||||||
|
const F = new Key('F');
|
||||||
|
const e = new Key('e', ['hash256']);
|
||||||
|
|
||||||
const layout = {
|
const layout = {
|
||||||
binary: true,
|
binary: true,
|
||||||
R: Buffer.from([0x52]),
|
R: R.build.bind(R),
|
||||||
V: Buffer.from([0x76]),
|
V: V.build.bind(V),
|
||||||
F: Buffer.from([0x46]),
|
F: F.build.bind(F),
|
||||||
e: function e(hash) {
|
e: e.build.bind(e),
|
||||||
const key = Buffer.allocUnsafe(33);
|
ee: e.parse.bind(e)
|
||||||
key[0] = 0x65;
|
|
||||||
write(key, hash, 1);
|
|
||||||
return key;
|
|
||||||
},
|
|
||||||
ee: function ee(key) {
|
|
||||||
assert(Buffer.isBuffer(key));
|
|
||||||
assert(key.length === 33);
|
|
||||||
return key.toString('hex', 1, 33);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
|
||||||
* Helpers
|
|
||||||
*/
|
|
||||||
|
|
||||||
function write(data, str, off) {
|
|
||||||
if (Buffer.isBuffer(str))
|
|
||||||
return str.copy(data, off);
|
|
||||||
assert(typeof str === 'string');
|
|
||||||
return data.write(str, off, 'hex');
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Expose
|
* Expose
|
||||||
*/
|
*/
|
||||||
|
|||||||
@ -9,6 +9,8 @@
|
|||||||
const assert = require('assert');
|
const assert = require('assert');
|
||||||
const layouts = exports;
|
const layouts = exports;
|
||||||
|
|
||||||
|
const Key = require('bdb/lib/key');
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Wallet Database Layout:
|
* Wallet Database Layout:
|
||||||
* p[addr-hash] -> wallet ids
|
* p[addr-hash] -> wallet ids
|
||||||
@ -27,156 +29,46 @@ const layouts = exports;
|
|||||||
* T[hash] -> tx->wid map
|
* T[hash] -> tx->wid map
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
const p = new Key('p', ['hash']);
|
||||||
|
const P = new Key('P', ['uint32', 'hash']);
|
||||||
|
const r = new Key('r', ['uint32', 'uint32', 'hash']);
|
||||||
|
const w = new Key('w', ['uint32']);
|
||||||
|
const l = new Key('l', ['ascii']);
|
||||||
|
const a = new Key('a', ['uint32', 'uint32']);
|
||||||
|
const i = new Key('i', ['uint32', 'ascii']);
|
||||||
|
const n = new Key('n', ['uint32', 'uint32']);
|
||||||
|
const R = new Key('R');
|
||||||
|
const h = new Key('h', ['uint32']);
|
||||||
|
const b = new Key('b', ['uint32']);
|
||||||
|
const o = new Key('o', ['hash256', 'uint32']);
|
||||||
|
const T = new Key('T', ['hash256']);
|
||||||
|
|
||||||
|
// Pp
|
||||||
|
// rr
|
||||||
layouts.walletdb = {
|
layouts.walletdb = {
|
||||||
binary: true,
|
binary: true,
|
||||||
p: function p(hash) {
|
p: p.build.bind(p),
|
||||||
assert(typeof hash === 'string');
|
pp: p.parse.bind(p),
|
||||||
const key = Buffer.allocUnsafe(1 + (hash.length / 2));
|
P: P.build.bind(P),
|
||||||
key[0] = 0x70;
|
Pp: P.parse.bind(P),
|
||||||
key.write(hash, 1, 'hex');
|
r: r.build.bind(r),
|
||||||
return key;
|
rr: r.parse.bind(r),
|
||||||
},
|
w: w.build.bind(w),
|
||||||
pp: function pp(key) {
|
ww: w.parse.bind(w),
|
||||||
assert(Buffer.isBuffer(key));
|
l: l.build.bind(l),
|
||||||
assert(key.length >= 21);
|
ll: l.parse.bind(l),
|
||||||
return key.toString('hex', 1);
|
a: a.build.bind(a),
|
||||||
},
|
i: i.build.bind(i),
|
||||||
P: function P(wid, hash) {
|
ii: i.parse.bind(i),
|
||||||
assert(typeof wid === 'number');
|
n: n.build.bind(n),
|
||||||
assert(typeof hash === 'string');
|
R: R.build.bind(R),
|
||||||
const key = Buffer.allocUnsafe(1 + 4 + (hash.length / 2));
|
h: h.build.bind(h),
|
||||||
key[0] = 0x50;
|
b: b.build.bind(b),
|
||||||
key.writeUInt32BE(wid, 1, true);
|
bb: b.parse.bind(b),
|
||||||
key.write(hash, 5, 'hex');
|
o: o.build.bind(o),
|
||||||
return key;
|
oo: o.parse.bind(o),
|
||||||
},
|
T: T.build.bind(T),
|
||||||
Pp: function Pp(key) {
|
Tt: T.parse.bind(T)
|
||||||
assert(Buffer.isBuffer(key));
|
|
||||||
assert(key.length >= 25);
|
|
||||||
return key.toString('hex', 5);
|
|
||||||
},
|
|
||||||
r: function r(wid, index, hash) {
|
|
||||||
assert(typeof wid === 'number');
|
|
||||||
assert(typeof index === 'number');
|
|
||||||
assert(typeof hash === 'string');
|
|
||||||
const key = Buffer.allocUnsafe(1 + 4 + 4 + (hash.length / 2));
|
|
||||||
key[0] = 0x72;
|
|
||||||
key.writeUInt32BE(wid, 1, true);
|
|
||||||
key.writeUInt32BE(index, 5, true);
|
|
||||||
key.write(hash, 9, 'hex');
|
|
||||||
return key;
|
|
||||||
},
|
|
||||||
rr: function rr(key) {
|
|
||||||
assert(Buffer.isBuffer(key));
|
|
||||||
assert(key.length >= 29);
|
|
||||||
return key.toString('hex', 9);
|
|
||||||
},
|
|
||||||
w: function w(wid) {
|
|
||||||
assert(typeof wid === 'number');
|
|
||||||
const key = Buffer.allocUnsafe(5);
|
|
||||||
key[0] = 0x77;
|
|
||||||
key.writeUInt32BE(wid, 1, true);
|
|
||||||
return key;
|
|
||||||
},
|
|
||||||
ww: function ww(key) {
|
|
||||||
assert(Buffer.isBuffer(key));
|
|
||||||
assert(key.length === 5);
|
|
||||||
return key.readUInt32BE(1, true);
|
|
||||||
},
|
|
||||||
l: function l(id) {
|
|
||||||
assert(typeof id === 'string');
|
|
||||||
const len = Buffer.byteLength(id, 'ascii');
|
|
||||||
const key = Buffer.allocUnsafe(1 + len);
|
|
||||||
key[0] = 0x6c;
|
|
||||||
if (len > 0)
|
|
||||||
key.write(id, 1, 'ascii');
|
|
||||||
return key;
|
|
||||||
},
|
|
||||||
ll: function ll(key) {
|
|
||||||
assert(Buffer.isBuffer(key));
|
|
||||||
assert(key.length >= 1);
|
|
||||||
return key.toString('ascii', 1);
|
|
||||||
},
|
|
||||||
a: function a(wid, index) {
|
|
||||||
assert(typeof wid === 'number');
|
|
||||||
assert(typeof index === 'number');
|
|
||||||
const key = Buffer.allocUnsafe(9);
|
|
||||||
key[0] = 0x61;
|
|
||||||
key.writeUInt32BE(wid, 1, true);
|
|
||||||
key.writeUInt32BE(index, 5, true);
|
|
||||||
return key;
|
|
||||||
},
|
|
||||||
i: function i(wid, name) {
|
|
||||||
assert(typeof wid === 'number');
|
|
||||||
assert(typeof name === 'string');
|
|
||||||
const len = Buffer.byteLength(name, 'ascii');
|
|
||||||
const key = Buffer.allocUnsafe(5 + len);
|
|
||||||
key[0] = 0x69;
|
|
||||||
key.writeUInt32BE(wid, 1, true);
|
|
||||||
if (len > 0)
|
|
||||||
key.write(name, 5, 'ascii');
|
|
||||||
return key;
|
|
||||||
},
|
|
||||||
ii: function ii(key) {
|
|
||||||
assert(Buffer.isBuffer(key));
|
|
||||||
assert(key.length >= 5);
|
|
||||||
return [key.readUInt32BE(1, true), key.toString('ascii', 5)];
|
|
||||||
},
|
|
||||||
n: function n(wid, index) {
|
|
||||||
assert(typeof wid === 'number');
|
|
||||||
assert(typeof index === 'number');
|
|
||||||
const key = Buffer.allocUnsafe(9);
|
|
||||||
key[0] = 0x6e;
|
|
||||||
key.writeUInt32BE(wid, 1, true);
|
|
||||||
key.writeUInt32BE(index, 5, true);
|
|
||||||
return key;
|
|
||||||
},
|
|
||||||
R: Buffer.from([0x52]),
|
|
||||||
h: function h(height) {
|
|
||||||
assert(typeof height === 'number');
|
|
||||||
const key = Buffer.allocUnsafe(5);
|
|
||||||
key[0] = 0x68;
|
|
||||||
key.writeUInt32BE(height, 1, true);
|
|
||||||
return key;
|
|
||||||
},
|
|
||||||
b: function b(height) {
|
|
||||||
assert(typeof height === 'number');
|
|
||||||
const key = Buffer.allocUnsafe(5);
|
|
||||||
key[0] = 0x62;
|
|
||||||
key.writeUInt32BE(height, 1, true);
|
|
||||||
return key;
|
|
||||||
},
|
|
||||||
bb: function bb(key) {
|
|
||||||
assert(Buffer.isBuffer(key));
|
|
||||||
assert(key.length === 5);
|
|
||||||
return key.readUInt32BE(1, true);
|
|
||||||
},
|
|
||||||
o: function o(hash, index) {
|
|
||||||
assert(typeof hash === 'string');
|
|
||||||
assert(typeof index === 'number');
|
|
||||||
const key = Buffer.allocUnsafe(37);
|
|
||||||
key[0] = 0x6f;
|
|
||||||
key.write(hash, 1, 'hex');
|
|
||||||
key.writeUInt32BE(index, 33, true);
|
|
||||||
return key;
|
|
||||||
},
|
|
||||||
oo: function oo(key) {
|
|
||||||
assert(Buffer.isBuffer(key));
|
|
||||||
assert(key.length === 37);
|
|
||||||
return [key.toString('hex', 1, 33), key.readUInt32BE(33, true)];
|
|
||||||
},
|
|
||||||
T: function T(hash, index) {
|
|
||||||
assert(typeof hash === 'string');
|
|
||||||
const key = Buffer.allocUnsafe(33);
|
|
||||||
key[0] = 0x54;
|
|
||||||
key.write(hash, 1, 'hex');
|
|
||||||
return key;
|
|
||||||
},
|
|
||||||
Tt: function Tt(key) {
|
|
||||||
assert(Buffer.isBuffer(key));
|
|
||||||
assert(key.length === 33);
|
|
||||||
return key.toString('hex', 1, 33);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -196,190 +88,55 @@ layouts.walletdb = {
|
|||||||
* r[hash] -> dummy (replace by fee chain)
|
* r[hash] -> dummy (replace by fee chain)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
{
|
||||||
|
const prefix = new Key('t', ['uint32']);
|
||||||
|
const R = new Key('R');
|
||||||
|
const r = new Key('r', ['uint32']);
|
||||||
|
const t = new Key('t', ['hash256']);
|
||||||
|
const c = new Key('c', ['hash256', 'uint32']);
|
||||||
|
const d = new Key('d', ['hash256', 'uint32']);
|
||||||
|
const s = new Key('s', ['hash256', 'uint32']);
|
||||||
|
const p = new Key('p', ['hash256']);
|
||||||
|
const m = new Key('m', ['uint32', 'hash256']);
|
||||||
|
const h = new Key('h', ['uint32', 'hash256']);
|
||||||
|
const T = new Key('T', ['uint32', 'hash256']);
|
||||||
|
const P = new Key('P', ['uint32', 'hash256']);
|
||||||
|
const M = new Key('M', ['uint32', 'uint32', 'hash256']);
|
||||||
|
const H = new Key('H', ['uint32', 'uint32', 'hash256']);
|
||||||
|
const C = new Key('C', ['uint32', 'hash256', 'uint32']);
|
||||||
|
const b = new Key('b', ['uint32']);
|
||||||
|
|
||||||
layouts.txdb = {
|
layouts.txdb = {
|
||||||
binary: true,
|
binary: true,
|
||||||
prefix: function prefix(wid) {
|
prefix: prefix.build.bind(prefix),
|
||||||
assert(typeof wid === 'number');
|
R: R.build.bind(R),
|
||||||
const out = Buffer.allocUnsafe(5);
|
r: r.build.bind(r),
|
||||||
out[0] = 0x74;
|
rr: r.parse.bind(r),
|
||||||
out.writeUInt32BE(wid, 1);
|
t: t.build.bind(t),
|
||||||
return out;
|
tt: t.parse.bind(t),
|
||||||
},
|
c: c.build.bind(c),
|
||||||
R: Buffer.from([0x52]),
|
cc: c.parse.bind(c),
|
||||||
r: function r(acct) {
|
d: d.build.bind(d),
|
||||||
assert(typeof acct === 'number');
|
dd: d.parse.bind(d),
|
||||||
const key = Buffer.allocUnsafe(5);
|
s: s.build.bind(s),
|
||||||
key[0] = 0x72;
|
ss: s.parse.bind(s),
|
||||||
key.writeUInt32BE(acct, 1, true);
|
p: p.build.bind(p),
|
||||||
return key;
|
pp: p.parse.bind(p),
|
||||||
},
|
m: m.build.bind(m),
|
||||||
rr: function rr(key) {
|
mm: m.parse.bind(m),
|
||||||
assert(Buffer.isBuffer(key));
|
h: h.build.bind(h),
|
||||||
assert(key.length === 5);
|
hh: h.parse.bind(h),
|
||||||
return key.readUInt32BE(1, true);
|
T: T.build.bind(T),
|
||||||
},
|
Tt: T.parse.bind(T),
|
||||||
hi: function hi(ch, hash, index) {
|
P: P.build.bind(P),
|
||||||
assert(typeof hash === 'string');
|
Pp: P.parse.bind(P),
|
||||||
assert(typeof index === 'number');
|
M: M.build.bind(M),
|
||||||
const key = Buffer.allocUnsafe(37);
|
Mm: M.parse.bind(M),
|
||||||
key[0] = ch;
|
H: H.build.bind(H),
|
||||||
key.write(hash, 1, 'hex');
|
Hh: H.parse.bind(H),
|
||||||
key.writeUInt32BE(index, 33, true);
|
C: C.build.bind(C),
|
||||||
return key;
|
Cc: C.parse.bind(C),
|
||||||
},
|
b: b.build.bind(b),
|
||||||
hii: function hii(key) {
|
bb: b.parse.bind(b)
|
||||||
assert(Buffer.isBuffer(key));
|
|
||||||
assert(key.length === 37);
|
|
||||||
return [key.toString('hex', 1, 33), key.readUInt32BE(33, true)];
|
|
||||||
},
|
|
||||||
ih: function ih(ch, index, hash) {
|
|
||||||
assert(typeof index === 'number');
|
|
||||||
assert(typeof hash === 'string');
|
|
||||||
const key = Buffer.allocUnsafe(37);
|
|
||||||
key[0] = ch;
|
|
||||||
key.writeUInt32BE(index, 1, true);
|
|
||||||
key.write(hash, 5, 'hex');
|
|
||||||
return key;
|
|
||||||
},
|
|
||||||
ihh: function ihh(key) {
|
|
||||||
assert(Buffer.isBuffer(key));
|
|
||||||
assert(key.length === 37);
|
|
||||||
return [key.readUInt32BE(1, true), key.toString('hex', 5, 37)];
|
|
||||||
},
|
|
||||||
iih: function iih(ch, index, num, hash) {
|
|
||||||
assert(typeof index === 'number');
|
|
||||||
assert(typeof num === 'number');
|
|
||||||
assert(typeof hash === 'string');
|
|
||||||
const key = Buffer.allocUnsafe(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) {
|
|
||||||
assert(Buffer.isBuffer(key));
|
|
||||||
assert(key.length === 41);
|
|
||||||
return [
|
|
||||||
key.readUInt32BE(1, true),
|
|
||||||
key.readUInt32BE(5, true),
|
|
||||||
key.toString('hex', 9, 41)
|
|
||||||
];
|
|
||||||
},
|
|
||||||
ihi: function ihi(ch, index, hash, num) {
|
|
||||||
assert(typeof index === 'number');
|
|
||||||
assert(typeof hash === 'string');
|
|
||||||
assert(typeof num === 'number');
|
|
||||||
const key = Buffer.allocUnsafe(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) {
|
|
||||||
assert(Buffer.isBuffer(key));
|
|
||||||
assert(key.length === 41);
|
|
||||||
return [
|
|
||||||
key.readUInt32BE(1, true),
|
|
||||||
key.toString('hex', 5, 37),
|
|
||||||
key.readUInt32BE(37, true)
|
|
||||||
];
|
|
||||||
},
|
|
||||||
ha: function ha(ch, hash) {
|
|
||||||
assert(typeof hash === 'string');
|
|
||||||
const key = Buffer.allocUnsafe(33);
|
|
||||||
key[0] = ch;
|
|
||||||
key.write(hash, 1, 'hex');
|
|
||||||
return key;
|
|
||||||
},
|
|
||||||
haa: function haa(key) {
|
|
||||||
assert(Buffer.isBuffer(key));
|
|
||||||
assert(key.length === 33);
|
|
||||||
return key.toString('hex', 1, 33);
|
|
||||||
},
|
|
||||||
t: function t(hash) {
|
|
||||||
return this.ha(0x74, hash);
|
|
||||||
},
|
|
||||||
tt: function tt(key) {
|
|
||||||
return this.haa(key);
|
|
||||||
},
|
|
||||||
c: function c(hash, index) {
|
|
||||||
return this.hi(0x63, hash, index);
|
|
||||||
},
|
|
||||||
cc: function cc(key) {
|
|
||||||
return this.hii(key);
|
|
||||||
},
|
|
||||||
d: function d(hash, index) {
|
|
||||||
return this.hi(0x64, hash, index);
|
|
||||||
},
|
|
||||||
dd: function dd(key) {
|
|
||||||
return this.hii(key);
|
|
||||||
},
|
|
||||||
s: function s(hash, index) {
|
|
||||||
return this.hi(0x73, hash, index);
|
|
||||||
},
|
|
||||||
ss: function ss(key) {
|
|
||||||
return this.hii(key);
|
|
||||||
},
|
|
||||||
p: function p(hash) {
|
|
||||||
return this.ha(0x70, hash);
|
|
||||||
},
|
|
||||||
pp: function pp(key) {
|
|
||||||
return this.haa(key);
|
|
||||||
},
|
|
||||||
m: function m(time, hash) {
|
|
||||||
return this.ih(0x6d, time, hash);
|
|
||||||
},
|
|
||||||
mm: function mm(key) {
|
|
||||||
return this.ihh(key);
|
|
||||||
},
|
|
||||||
h: function h(height, hash) {
|
|
||||||
return this.ih(0x68, height, hash);
|
|
||||||
},
|
|
||||||
hh: function hh(key) {
|
|
||||||
return this.ihh(key);
|
|
||||||
},
|
|
||||||
T: function T(account, hash) {
|
|
||||||
return this.ih(0x54, account, hash);
|
|
||||||
},
|
|
||||||
Tt: function Tt(key) {
|
|
||||||
return this.ihh(key);
|
|
||||||
},
|
|
||||||
P: function P(account, hash) {
|
|
||||||
return this.ih(0x50, account, hash);
|
|
||||||
},
|
|
||||||
Pp: function Pp(key) {
|
|
||||||
return this.ihh(key);
|
|
||||||
},
|
|
||||||
M: function M(account, time, hash) {
|
|
||||||
return this.iih(0x4d, account, time, hash);
|
|
||||||
},
|
|
||||||
Mm: function Mm(key) {
|
|
||||||
return this.iihh(key);
|
|
||||||
},
|
|
||||||
H: function H(account, height, hash) {
|
|
||||||
return this.iih(0x48, account, height, hash);
|
|
||||||
},
|
|
||||||
Hh: function Hh(key) {
|
|
||||||
return this.iihh(key);
|
|
||||||
},
|
|
||||||
C: function C(account, hash, index) {
|
|
||||||
return this.ihi(0x43, account, hash, index);
|
|
||||||
},
|
|
||||||
Cc: function Cc(key) {
|
|
||||||
return this.ihii(key);
|
|
||||||
},
|
|
||||||
b: function b(height) {
|
|
||||||
assert(typeof height === 'number');
|
|
||||||
const key = Buffer.allocUnsafe(5);
|
|
||||||
key[0] = 0x62;
|
|
||||||
key.writeUInt32BE(height, 1, true);
|
|
||||||
return key;
|
|
||||||
},
|
|
||||||
bb: function bb(key) {
|
|
||||||
assert(Buffer.isBuffer(key));
|
|
||||||
assert(key.length === 5);
|
|
||||||
return key.readUInt32BE(1, true);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
}
|
||||||
|
|||||||
@ -187,7 +187,7 @@ class TXDB {
|
|||||||
async updateBalance(b, state) {
|
async updateBalance(b, state) {
|
||||||
const balance = await this.getWalletBalance();
|
const balance = await this.getWalletBalance();
|
||||||
state.applyTo(balance);
|
state.applyTo(balance);
|
||||||
b.put(layout.R, balance.toRaw());
|
b.put(layout.R(), balance.toRaw());
|
||||||
return balance;
|
return balance;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1970,7 +1970,7 @@ class TXDB {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
async getWalletBalance() {
|
async getWalletBalance() {
|
||||||
const data = await this.bucket.get(layout.R);
|
const data = await this.bucket.get(layout.R());
|
||||||
|
|
||||||
if (!data)
|
if (!data)
|
||||||
return new Balance();
|
return new Balance();
|
||||||
|
|||||||
@ -339,7 +339,7 @@ class WalletDB extends EventEmitter {
|
|||||||
state.height = tip.height;
|
state.height = tip.height;
|
||||||
state.marked = false;
|
state.marked = false;
|
||||||
|
|
||||||
b.put(layout.R, state.toRaw());
|
b.put(layout.R(), state.toRaw());
|
||||||
|
|
||||||
await b.write();
|
await b.write();
|
||||||
|
|
||||||
@ -864,7 +864,7 @@ class WalletDB extends EventEmitter {
|
|||||||
});
|
});
|
||||||
|
|
||||||
await piter.each((key, value) => {
|
await piter.each((key, value) => {
|
||||||
const hash = layout.Pp(key);
|
const [, hash] = layout.Pp(key);
|
||||||
b.del(key);
|
b.del(key);
|
||||||
return this.removePathMap(b, hash, wid);
|
return this.removePathMap(b, hash, wid);
|
||||||
});
|
});
|
||||||
@ -1253,7 +1253,7 @@ class WalletDB extends EventEmitter {
|
|||||||
return this.db.keys({
|
return this.db.keys({
|
||||||
gte: layout.P(wid, encoding.NULL_HASH),
|
gte: layout.P(wid, encoding.NULL_HASH),
|
||||||
lte: layout.P(wid, encoding.HIGH_HASH),
|
lte: layout.P(wid, encoding.HIGH_HASH),
|
||||||
parse: layout.Pp
|
parse: key => layout.Pp(key)[1]
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1268,7 +1268,7 @@ class WalletDB extends EventEmitter {
|
|||||||
return this.db.keys({
|
return this.db.keys({
|
||||||
gte: layout.r(wid, account, encoding.NULL_HASH),
|
gte: layout.r(wid, account, encoding.NULL_HASH),
|
||||||
lte: layout.r(wid, account, encoding.HIGH_HASH),
|
lte: layout.r(wid, account, encoding.HIGH_HASH),
|
||||||
parse: layout.rr
|
parse: key => layout.rr(key)[2]
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1287,7 +1287,7 @@ class WalletDB extends EventEmitter {
|
|||||||
const paths = [];
|
const paths = [];
|
||||||
|
|
||||||
for (const item of items) {
|
for (const item of items) {
|
||||||
const hash = layout.Pp(item.key);
|
const [, hash] = layout.Pp(item.key);
|
||||||
const path = Path.fromRaw(item.value);
|
const path = Path.fromRaw(item.value);
|
||||||
|
|
||||||
path.hash = hash;
|
path.hash = hash;
|
||||||
@ -1328,7 +1328,7 @@ class WalletDB extends EventEmitter {
|
|||||||
});
|
});
|
||||||
|
|
||||||
await iter.each((k, value) => {
|
await iter.each((k, value) => {
|
||||||
const hash = layout.Pp(k);
|
const [, hash] = layout.Pp(k);
|
||||||
const path = Path.fromRaw(value);
|
const path = Path.fromRaw(value);
|
||||||
|
|
||||||
if (!path.data)
|
if (!path.data)
|
||||||
@ -1361,7 +1361,7 @@ class WalletDB extends EventEmitter {
|
|||||||
});
|
});
|
||||||
|
|
||||||
await iter.each((k, value) => {
|
await iter.each((k, value) => {
|
||||||
const hash = layout.Pp(k);
|
const [, hash] = layout.Pp(k);
|
||||||
const path = Path.fromRaw(value);
|
const path = Path.fromRaw(value);
|
||||||
|
|
||||||
if (!path.data)
|
if (!path.data)
|
||||||
@ -1388,7 +1388,7 @@ class WalletDB extends EventEmitter {
|
|||||||
const wids = await this.db.keys({
|
const wids = await this.db.keys({
|
||||||
gte: layout.w(0x00000000),
|
gte: layout.w(0x00000000),
|
||||||
lte: layout.w(0xffffffff),
|
lte: layout.w(0xffffffff),
|
||||||
parse: k => layout.ww(k)
|
parse: key => layout.ww(key)
|
||||||
});
|
});
|
||||||
|
|
||||||
this.logger.info('Resending from %d wallets.', wids.length);
|
this.logger.info('Resending from %d wallets.', wids.length);
|
||||||
@ -1412,7 +1412,7 @@ class WalletDB extends EventEmitter {
|
|||||||
const hashes = await b.keys({
|
const hashes = await b.keys({
|
||||||
gte: layout.p(encoding.NULL_HASH),
|
gte: layout.p(encoding.NULL_HASH),
|
||||||
lte: layout.p(encoding.HIGH_HASH),
|
lte: layout.p(encoding.HIGH_HASH),
|
||||||
parse: k => layout.pp(k)
|
parse: key => layout.pp(key)
|
||||||
});
|
});
|
||||||
|
|
||||||
if (hashes.length === 0)
|
if (hashes.length === 0)
|
||||||
@ -1496,7 +1496,7 @@ class WalletDB extends EventEmitter {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
async getState() {
|
async getState() {
|
||||||
const data = await this.db.get(layout.R);
|
const data = await this.db.get(layout.R());
|
||||||
|
|
||||||
if (!data)
|
if (!data)
|
||||||
return null;
|
return null;
|
||||||
@ -1534,7 +1534,7 @@ class WalletDB extends EventEmitter {
|
|||||||
|
|
||||||
// Save tip and state.
|
// Save tip and state.
|
||||||
b.put(layout.h(tip.height), tip.toHash());
|
b.put(layout.h(tip.height), tip.toHash());
|
||||||
b.put(layout.R, state.toRaw());
|
b.put(layout.R(), state.toRaw());
|
||||||
|
|
||||||
await b.write();
|
await b.write();
|
||||||
|
|
||||||
@ -1555,7 +1555,7 @@ class WalletDB extends EventEmitter {
|
|||||||
state.marked = true;
|
state.marked = true;
|
||||||
|
|
||||||
const b = this.db.batch();
|
const b = this.db.batch();
|
||||||
b.put(layout.R, state.toRaw());
|
b.put(layout.R(), state.toRaw());
|
||||||
await b.write();
|
await b.write();
|
||||||
|
|
||||||
this.state = state;
|
this.state = state;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user