txdb: use new bucket system.

This commit is contained in:
Christopher Jeffrey 2017-11-29 13:50:16 -08:00
parent aa115c2e9d
commit a8e2001015
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD

View File

@ -38,7 +38,7 @@ class TXDB {
this.logger = wdb.logger;
this.wid = wid || 0;
this.prefix = layout.prefix(this.wid);
this.bucket = null;
this.wallet = null;
this.locked = new Set();
}
@ -50,7 +50,7 @@ class TXDB {
async open(wallet) {
this.wid = wallet.wid;
this.prefix = layout.prefix(this.wid);
this.bucket = this.db.bucket(layout.prefix(this.wid));
this.wallet = wallet;
}
@ -67,63 +67,6 @@ class TXDB {
this.wallet.emit(event, data, details);
}
/**
* Bucket
* @returns {Bucket}
*/
bucket() {
return this.db.bucket(this.prefix);
}
/**
* Get.
* @param {String} key
*/
get(key) {
return this.bucket().get(key);
}
/**
* Has.
* @param {String} key
*/
has(key) {
return this.bucket().has(key);
}
/**
* Iterate.
* @param {Object} options
* @returns {Promise}
*/
range(options) {
return this.bucket().range(options);
}
/**
* Iterate.
* @param {Object} options
* @returns {Promise}
*/
keys(options) {
return this.bucket().keys(options);
}
/**
* Iterate.
* @param {Object} options
* @returns {Promise}
*/
values(options) {
return this.bucket().values(options);
}
/**
* Get wallet path for output.
* @param {Output} output
@ -269,7 +212,7 @@ class TXDB {
*/
async getSpent(hash, index) {
const data = await this.get(layout.s(hash, index));
const data = await this.bucket.get(layout.s(hash, index));
if (!data)
return null;
@ -285,7 +228,7 @@ class TXDB {
*/
isSpent(hash, index) {
return this.has(layout.s(hash, index));
return this.bucket.has(layout.s(hash, index));
}
/**
@ -356,7 +299,7 @@ class TXDB {
*/
getBlocks() {
return this.keys({
return this.bucket.keys({
gte: layout.b(0),
lte: layout.b(0xffffffff),
parse: key => layout.bb(key)
@ -370,7 +313,7 @@ class TXDB {
*/
async getBlock(height) {
const data = await this.get(layout.b(height));
const data = await this.bucket.get(layout.b(height));
if (!data)
return null;
@ -387,7 +330,7 @@ class TXDB {
async addBlock(b, hash, block) {
const key = layout.b(block.height);
const data = await this.get(key);
const data = await this.bucket.get(key);
if (!data) {
const blk = BlockRecord.fromMeta(block);
@ -415,7 +358,7 @@ class TXDB {
async removeBlock(b, hash, height) {
const key = layout.b(height);
const data = await this.get(key);
const data = await this.bucket.get(key);
if (!data)
return;
@ -512,7 +455,7 @@ class TXDB {
*/
async insert(wtx, block) {
const b = this.bucket();
const b = this.bucket.batch();
const {tx, hash} = wtx;
const height = block ? block.height : -1;
const details = new Details(wtx, block);
@ -668,7 +611,7 @@ class TXDB {
*/
async confirm(wtx, block) {
const b = this.bucket();
const b = this.bucket.batch();
const {tx, hash} = wtx;
const height = block.height;
const details = new Details(wtx, block);
@ -817,7 +760,7 @@ class TXDB {
*/
async erase(wtx, block) {
const b = this.bucket();
const b = this.bucket.batch();
const {tx, hash} = wtx;
const height = block ? block.height : -1;
const details = new Details(wtx, block);
@ -1005,7 +948,7 @@ class TXDB {
*/
async disconnect(wtx, block) {
const b = this.bucket();
const b = this.bucket.batch();
const {tx, hash, height} = wtx;
const details = new Details(wtx, block);
const state = new BalanceDelta();
@ -1279,7 +1222,7 @@ class TXDB {
getAccountHistoryHashes(acct) {
assert(typeof acct === 'number');
return this.keys({
return this.bucket.keys({
gte: layout.T(acct, encoding.NULL_HASH),
lte: layout.T(acct, encoding.HIGH_HASH),
parse: (key) => {
@ -1301,7 +1244,7 @@ class TXDB {
if (acct !== -1)
return this.getAccountHistoryHashes(acct);
return this.keys({
return this.bucket.keys({
gte: layout.t(encoding.NULL_HASH),
lte: layout.t(encoding.HIGH_HASH),
parse: key => layout.tt(key)
@ -1316,7 +1259,7 @@ class TXDB {
getAccountPendingHashes(acct) {
assert(typeof acct === 'number');
return this.keys({
return this.bucket.keys({
gte: layout.P(acct, encoding.NULL_HASH),
lte: layout.P(acct, encoding.HIGH_HASH),
parse: (key) => {
@ -1338,7 +1281,7 @@ class TXDB {
if (acct !== -1)
return this.getAccountPendingHashes(acct);
return this.keys({
return this.bucket.keys({
gte: layout.p(encoding.NULL_HASH),
lte: layout.p(encoding.HIGH_HASH),
parse: key => layout.pp(key)
@ -1353,7 +1296,7 @@ class TXDB {
getAccountOutpoints(acct) {
assert(typeof acct === 'number');
return this.keys({
return this.bucket.keys({
gte: layout.C(acct, encoding.NULL_HASH, 0),
lte: layout.C(acct, encoding.HIGH_HASH, 0xffffffff),
parse: (key) => {
@ -1375,7 +1318,7 @@ class TXDB {
if (acct !== -1)
return this.getAccountOutpoints(acct);
return this.keys({
return this.bucket.keys({
gte: layout.c(encoding.NULL_HASH, 0),
lte: layout.c(encoding.HIGH_HASH, 0xffffffff),
parse: (key) => {
@ -1402,7 +1345,7 @@ class TXDB {
const start = options.start || 0;
const end = options.end || 0xffffffff;
return this.keys({
return this.bucket.keys({
gte: layout.H(acct, start, encoding.NULL_HASH),
lte: layout.H(acct, end, encoding.HIGH_HASH),
limit: options.limit,
@ -1434,7 +1377,7 @@ class TXDB {
const start = options.start || 0;
const end = options.end || 0xffffffff;
return this.keys({
return this.bucket.keys({
gte: layout.h(start, encoding.NULL_HASH),
lte: layout.h(end, encoding.HIGH_HASH),
limit: options.limit,
@ -1473,7 +1416,7 @@ class TXDB {
const start = options.start || 0;
const end = options.end || 0xffffffff;
return this.keys({
return this.bucket.keys({
gte: layout.M(acct, start, encoding.NULL_HASH),
lte: layout.M(acct, end, encoding.HIGH_HASH),
limit: options.limit,
@ -1505,7 +1448,7 @@ class TXDB {
const start = options.start || 0;
const end = options.end || 0xffffffff;
return this.keys({
return this.bucket.keys({
gte: layout.m(start, encoding.NULL_HASH),
lte: layout.m(end, encoding.HIGH_HASH),
limit: options.limit,
@ -1571,7 +1514,7 @@ class TXDB {
return this.getAccountHistory(acct);
// Fast case
return this.values({
return this.bucket.values({
gte: layout.t(encoding.NULL_HASH),
lte: layout.t(encoding.HIGH_HASH),
parse: data => TXRecord.fromRaw(data)
@ -1630,7 +1573,7 @@ class TXDB {
return this.getAccountCredits(acct);
// Fast case
return this.range({
return this.bucket.range({
gte: layout.c(encoding.NULL_HASH, 0x00000000),
lte: layout.c(encoding.HIGH_HASH, 0xffffffff),
parse: (key, value) => {
@ -1678,7 +1621,7 @@ class TXDB {
for (let i = 0; i < tx.inputs.length; i++)
credits.push(null);
await this.range({
await this.bucket.range({
gte: layout.d(hash, 0x00000000),
lte: layout.d(hash, 0xffffffff),
parse: (key, value) => {
@ -1816,7 +1759,7 @@ class TXDB {
*/
async getTX(hash) {
const raw = await this.get(layout.t(hash));
const raw = await this.bucket.get(layout.t(hash));
if (!raw)
return null;
@ -1902,7 +1845,7 @@ class TXDB {
*/
hasTX(hash) {
return this.has(layout.t(hash));
return this.bucket.has(layout.t(hash));
}
/**
@ -1929,7 +1872,7 @@ class TXDB {
*/
async getCredit(hash, index) {
const data = await this.get(layout.c(hash, index));
const data = await this.bucket.get(layout.c(hash, index));
if (!data)
return null;
@ -1949,7 +1892,7 @@ class TXDB {
*/
async getSpentCoin(spent, prevout) {
const data = await this.get(layout.d(spent.hash, spent.index));
const data = await this.bucket.get(layout.d(spent.hash, spent.index));
if (!data)
return null;
@ -1968,7 +1911,7 @@ class TXDB {
*/
hasSpentCoin(spent) {
return this.has(layout.d(spent.hash, spent.index));
return this.bucket.has(layout.d(spent.hash, spent.index));
}
/**
@ -2003,7 +1946,7 @@ class TXDB {
*/
async hasCoin(hash, index) {
return this.has(layout.c(hash, index));
return this.bucket.has(layout.c(hash, index));
}
/**
@ -2027,7 +1970,7 @@ class TXDB {
*/
async getWalletBalance() {
const data = await this.get(layout.R);
const data = await this.bucket.get(layout.R);
if (!data)
return new Balance();
@ -2042,7 +1985,7 @@ class TXDB {
*/
async getAccountBalance(acct) {
const data = await this.get(layout.r(acct));
const data = await this.bucket.get(layout.r(acct));
if (!data)
return new Balance(acct);
@ -2093,7 +2036,7 @@ class TXDB {
*/
async abandon(hash) {
const result = await this.has(layout.p(hash));
const result = await this.bucket.has(layout.p(hash));
if (!result)
throw new Error('TX not eligible.');