Skip to content

Commit

Permalink
txdb: move count and time indexes under the same prefix. (O).
Browse files Browse the repository at this point in the history
  • Loading branch information
nodech committed Nov 18, 2024
1 parent 904756e commit a914b01
Show file tree
Hide file tree
Showing 5 changed files with 610 additions and 627 deletions.
55 changes: 35 additions & 20 deletions lib/wallet/layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ exports.wdb = {
* -------
* R -> wallet balance
* r[account] -> account balance
* I -> Latest Unconfirmed Index
*
* Coin
* ----
Expand All @@ -107,26 +106,36 @@ exports.wdb = {
* -----------
* t[tx-hash] -> extended tx
* T[account][tx-hash] -> dummy (tx by account)
* z[height][index] -> tx hash (tx by count)
* Z[account][height][index] -> tx hash (tx by count + account)
* y[hash] -> count (count for tx)
* x[hash] -> undo count (unconfirmed count for tx)
*
* Confirmed
* ---------
* b[height] -> block record
* h[height][tx-hash] -> dummy (tx by height)
* H[account][height][tx-hash] -> dummy (tx by height + account)
* g[time][height][index][hash] -> dummy (tx by time)
* G[account][time][height][index][hash] -> dummy (tx by time + account)
*
* Unconfirmed
* -----------
* p[hash] -> dummy (pending tx)
* P[account][tx-hash] -> dummy (pending tx by account)
* w[time][count][hash] -> dummy (tx by time)
* W[account][time][count][hash] -> dummy (tx by time + account)
* e[hash] -> undo time (unconfirmed time for tx)
*
* Count and Time Index
* --------------------
* Ol - Latest Unconfirmed Index
* Oc[hash] - count (count for tx)
* Ou[hash] - undo count (unconfirmed count for tx)
* Ot[height][index] -> tx hash (tx by count)
* OT[account][height][index] -> tx hash (tx by count + account)
*
* Count and Time Index - Confirmed
* --------------------
* Oi[time][height][index][hash] -> dummy (tx by time)
* OI[account][time][height][index][hash] -> dummy (tx by time + account)
*
* Count and Time Index - Unconfirmed
* --------------------
* Om[time][count][hash] -> dummy (tx by time)
* OM[account][time][count][hash] -> dummy (tx by time + account)
* Oe[hash] -> undo time (unconfirmed time for tx)
*
* Names
* -----
Expand All @@ -145,7 +154,6 @@ exports.txdb = {
// Balance
R: bdb.key('R'),
r: bdb.key('r', ['uint32']),
I: bdb.key('I'),

// Coin
c: bdb.key('c', ['hash256', 'uint32']),
Expand All @@ -156,24 +164,31 @@ exports.txdb = {
// Transaction
t: bdb.key('t', ['hash256']),
T: bdb.key('T', ['uint32', 'hash256']),
z: bdb.key('z', ['uint32', 'uint32']),
Z: bdb.key('Z', ['uint32', 'uint32', 'uint32']),
y: bdb.key('y', ['hash256']),
x: bdb.key('x', ['hash256']),

// Confirmed
b: bdb.key('b', ['uint32']),
h: bdb.key('h', ['uint32', 'hash256']),
H: bdb.key('H', ['uint32', 'uint32', 'hash256']),
g: bdb.key('g', ['uint32', 'uint32', 'uint32', 'hash256']),
G: bdb.key('G', ['uint32', 'uint32', 'uint32', 'uint32', 'hash256']),

// Unconfirmed
p: bdb.key('p', ['hash256']),
P: bdb.key('P', ['uint32', 'hash256']),
w: bdb.key('w', ['uint32', 'uint32', 'hash256']),
W: bdb.key('W', ['uint32', 'uint32', 'uint32', 'hash256']),
e: bdb.key('e', ['hash256']),

// Count and Time Index. (O for general prefix.)
Ol: bdb.key('Ol'),
Oc: bdb.key('Oc', ['hash256']),
Ou: bdb.key('Ou', ['hash256']),
Ot: bdb.key('Ot', ['uint32', 'uint32']),
OT: bdb.key('OT', ['uint32', 'uint32', 'uint32']),

// Count and Time Index - Confirmed
Oi: bdb.key('Oi', ['uint32', 'uint32', 'uint32', 'hash256']),
OI: bdb.key('OI', ['uint32', 'uint32', 'uint32', 'uint32', 'hash256']),

// Count and Time Index - Unconfirmed
Om: bdb.key('Om', ['uint32', 'uint32', 'hash256']),
OM: bdb.key('OM', ['uint32', 'uint32', 'uint32', 'hash256']),
Oe: bdb.key('Oe', ['hash256']),

// Names
A: bdb.key('A', ['hash256']),
Expand Down
93 changes: 48 additions & 45 deletions lib/wallet/migrations.js
Original file line number Diff line number Diff line change
Expand Up @@ -904,16 +904,16 @@ class MigrateTXCountTimeIndex extends AbstractMigration {
const bucket = this.ldb.bucket(txlayout.prefix.encode(wid));

const lastHeight = await bucket.range({
gte: txlayout.z.min(),
lt: txlayout.z.min(this.UNCONFIRMED_HEIGHT),
gte: txlayout.Ot.min(),
lt: txlayout.Ot.min(this.UNCONFIRMED_HEIGHT),
reverse: true,
limit: 1
});

let startHeight = 0;

if (lastHeight.length !== 0) {
const [height] = txlayout.z.decode(lastHeight[0].key);
const [height] = txlayout.Ot.decode(lastHeight[0].key);
startHeight = height;
}

Expand Down Expand Up @@ -996,7 +996,7 @@ class MigrateTXCountTimeIndex extends AbstractMigration {
const txlayout = this.layout.txdb;

// Skip if already migrated.
if (await bucket.get(txlayout.y.encode(txHash)))
if (await bucket.get(txlayout.Oc.encode(txHash)))
return;

const batch = bucket.batch();
Expand Down Expand Up @@ -1052,7 +1052,7 @@ class MigrateTXCountTimeIndex extends AbstractMigration {
// Expanded the following code.
// Add indexing for unconfirmed transactions.
// await this.addCountAndTimeIndexUnconfirmed(b, state.accounts, hash);
const rawLastUnconfirmedIndex = await bucket.get(txlayout.I.encode());
const rawLastUnconfirmedIndex = await bucket.get(txlayout.Ol.encode());
let lastUnconfirmedIndex = 0;

if (rawLastUnconfirmedIndex)
Expand All @@ -1062,18 +1062,18 @@ class MigrateTXCountTimeIndex extends AbstractMigration {
const index = lastUnconfirmedIndex;
const count = { height, index };

batch.put(txlayout.z.encode(height, index), txHash);
batch.put(txlayout.y.encode(txHash), this.encodeTXCount(count));
batch.put(txlayout.Ot.encode(height, index), txHash);
batch.put(txlayout.Oc.encode(txHash), this.encodeTXCount(count));

batch.put(txlayout.e.encode(txHash), fromU32(mtime));
batch.put(txlayout.w.encode(mtime, index, txHash));
batch.put(txlayout.Oe.encode(txHash), fromU32(mtime));
batch.put(txlayout.Om.encode(mtime, index, txHash));

for (const acct of accounts) {
batch.put(txlayout.Z.encode(acct, height, index), txHash);
batch.put(txlayout.W.encode(acct, mtime, index, txHash));
batch.put(txlayout.OT.encode(acct, height, index), txHash);
batch.put(txlayout.OM.encode(acct, mtime, index, txHash));
}

batch.put(txlayout.I.encode(), fromU32(lastUnconfirmedIndex + 1));
batch.put(txlayout.Ol.encode(), fromU32(lastUnconfirmedIndex + 1));

await batch.write();
return;
Expand All @@ -1090,31 +1090,31 @@ class MigrateTXCountTimeIndex extends AbstractMigration {
const height = block.height;
const count = { height, index };

batch.put(txlayout.z.encode(height, index), txHash);
batch.put(txlayout.y.encode(txHash), this.encodeTXCount(count));
batch.put(txlayout.Ot.encode(height, index), txHash);
batch.put(txlayout.Oc.encode(txHash), this.encodeTXCount(count));

const time = extra.medianTime;
batch.put(txlayout.g.encode(time, height, index, txHash));
batch.put(txlayout.Oi.encode(time, height, index, txHash));

for (const acct of accounts) {
batch.put(txlayout.Z.encode(acct, height, index), txHash);
batch.put(txlayout.G.encode(acct, time, height, index, txHash));
batch.put(txlayout.OT.encode(acct, height, index), txHash);
batch.put(txlayout.OI.encode(acct, time, height, index, txHash));
}

// await this.addTimeAndCountIndexUnconfirmedUndo(b, hash);
const rawLastUnconfirmedIndex = await bucket.get(txlayout.I.encode());
const rawLastUnconfirmedIndex = await bucket.get(txlayout.Ol.encode());
let lastUnconfirmedIndex = 0;

if (rawLastUnconfirmedIndex)
lastUnconfirmedIndex = encoding.readU32(rawLastUnconfirmedIndex, 0);

batch.put(txlayout.e.encode(txHash), fromU32(mtime));
batch.put(txlayout.x.encode(txHash), this.encodeTXCount({
batch.put(txlayout.Oe.encode(txHash), fromU32(mtime));
batch.put(txlayout.Ou.encode(txHash), this.encodeTXCount({
height: this.UNCONFIRMED_HEIGHT,
index: lastUnconfirmedIndex
}));

batch.put(txlayout.I.encode(), fromU32(lastUnconfirmedIndex + 1));
batch.put(txlayout.Ol.encode(), fromU32(lastUnconfirmedIndex + 1));
await batch.write();
}

Expand Down Expand Up @@ -1246,9 +1246,6 @@ class MigrateTXCountTimeIndex extends AbstractMigration {
txdb: {
prefix: bdb.key('t', ['uint32']),

// Latest unconfirmed Index.
I: bdb.key('I'),

// We need this for spent inputs in blocks.
// d[tx-hash][index] -> undo coin
d: bdb.key('d', ['hash256', 'uint32']),
Expand All @@ -1259,37 +1256,43 @@ class MigrateTXCountTimeIndex extends AbstractMigration {
// M[account][time][tx-hash] -> dummy (tx by time + account)
M: bdb.key('M', ['uint32', 'uint32', 'hash256']),

// these are not affected by the migration, but here for reference
// This is not affected by the migration, but here for reference
// and time check.
// t[tx-hash] -> extended tx
t: bdb.key('t', ['hash256']),

// Transaction.
// z[height][index] -> tx hash (tx by count)
z: bdb.key('z', ['uint32', 'uint32']),
// Z[account][height][index] -> tx hash (tx by count + account)
Z: bdb.key('Z', ['uint32', 'uint32', 'uint32']),
// y[hash] -> count (count for tx)
y: bdb.key('y', ['hash256']),
// x[hash] -> undo count (unconfirmed count for tx)
x: bdb.key('x', ['hash256']),

// Confirmed.
// b[height] -> block record
b: bdb.key('b', ['uint32']),

// g[time][height][index][hash] -> dummy (tx by time)
g: bdb.key('g', ['uint32', 'uint32', 'uint32', 'hash256']),
// G[account][time][height][index][hash] -> dummy (tx by time + account)
G: bdb.key('G', ['uint32', 'uint32', 'uint32', 'uint32', 'hash256']),
// Count and Time Index.
// Latest unconfirmed Index.
Ol: bdb.key('Ol'),

// Transaction.
// z[height][index] -> tx hash (tx by count)
Ot: bdb.key('Ot', ['uint32', 'uint32']),
// Z[account][height][index] -> tx hash (tx by count + account)
OT: bdb.key('OT', ['uint32', 'uint32', 'uint32']),
// Oc[hash] -> count (count for tx)
Oc: bdb.key('Oc', ['hash256']),
// Ou[hash] -> undo count (unconfirmed count for tx)
Ou: bdb.key('Ou', ['hash256']),

// Unconfirmed.
// w[time][count][hash] -> dummy (tx by time)
w: bdb.key('w', ['uint32', 'uint32', 'hash256']),
// W[account][time][count][hash] -> dummy (tx by time + account)
W: bdb.key('W', ['uint32', 'uint32', 'uint32', 'hash256']),
// e[hash] -> undo time (unconfirmed time for tx)
e: bdb.key('e', ['hash256'])
// Om[time][count][hash] -> dummy (tx by time)
Om: bdb.key('Om', ['uint32', 'uint32', 'hash256']),
// OM[account][time][count][hash] -> dummy (tx by time + account)
OM: bdb.key('OM', ['uint32', 'uint32', 'uint32', 'hash256']),
// Oe[hash] -> undo time (unconfirmed time for tx)
Oe: bdb.key('Oe', ['hash256']),

// Confirmed.
// Oi[time][height][index][hash] -> dummy (tx by time)
Oi: bdb.key('Oi', ['uint32', 'uint32', 'uint32', 'hash256']),

// OI[account][time][height][index][hash] -> dummy(tx by time + account)
OI: bdb.key('OI', ['uint32', 'uint32', 'uint32', 'uint32', 'hash256'])
}
};
}
Expand Down
Loading

0 comments on commit a914b01

Please sign in to comment.