Skip to content

Commit

Permalink
wallet: reorg tx monotonic time and count indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
braydonf committed Oct 26, 2018
1 parent 0d82dc2 commit 98940c3
Show file tree
Hide file tree
Showing 10 changed files with 177 additions and 148 deletions.
4 changes: 3 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
- [x] Reorgs for monotonic time
- [x] Full node
- [x] SPV node
- [ ] Reorgs for tx indexing
- [x] Reorgs for tx indexing

- Indexes for unconfirmed txs
- [ ] Index count for unconfirmed txs
Expand Down Expand Up @@ -52,6 +52,8 @@
count of proceeding transactions.
- [ ] Verify that txdb `remove` and `removeRecursive` can only remove pending
transactions, for similar reasons as the above.
- [ ] Verify the transactions that were in a block that has been reorg'ed
make it back into the mempool and mined in the next block.
- [ ] Consider using bfile.rimraf with `/tmp/*` guards in tests

- Documentation and upgrading
Expand Down
8 changes: 6 additions & 2 deletions lib/wallet/layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,17 @@ exports.wdb = {
* d[hash][index] -> undo coin
* s[hash][index] -> spent by hash
* p[hash] -> dummy (pending flag)
* m[monotonic-time][hash] -> dummy (tx by time)
* m[time][hash] -> dummy (tx by time)
* g[monotonic-time][hash] -> dummy (tx by monotonic time)
* z[count] -> dummy (tx by count)
* x[account][count]-> dummy (tx by count + account)
* y[hash] -> count (count for tx)
* w[account]hash] -> count (account count for tx)
* h[height][hash] -> dummy (tx by height)
* T[account][hash] -> dummy (tx by account)
* P[account][hash] -> dummy (pending tx by account)
* M[account][monotonic-time][hash] -> dummy (tx by time + account)
* M[account][time][hash] -> dummy (tx by time + account)
* G[account][monotonic-time][hash] -> dummy (tx by monotonic time + account)
* H[account][height][hash] -> dummy (tx by height + account)
* C[account][hash][index] -> dummy (coin by account)
* b[height] -> block record
Expand All @@ -88,6 +90,7 @@ exports.txdb = {
s: bdb.key('s', ['hash256', 'uint32']),
p: bdb.key('p', ['hash256']),
m: bdb.key('m', ['uint32', 'hash256']),
g: bdb.key('g', ['uint32', 'hash256']),
z: bdb.key('z', ['uint32']),
x: bdb.key('x', ['uint32', 'uint32']),
y: bdb.key('y', ['hash256']),
Expand All @@ -96,6 +99,7 @@ exports.txdb = {
T: bdb.key('T', ['uint32', 'hash256']),
P: bdb.key('P', ['uint32', 'hash256']),
M: bdb.key('M', ['uint32', 'uint32', 'hash256']),
G: bdb.key('G', ['uint32', 'uint32', 'hash256']),
H: bdb.key('H', ['uint32', 'uint32', 'hash256']),
C: bdb.key('C', ['uint32', 'hash256', 'uint32']),
b: bdb.key('b', ['uint32'])
Expand Down
21 changes: 6 additions & 15 deletions lib/wallet/records.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,39 +236,33 @@ class TXRecord {
* Create tx record.
* @constructor
* @param {TX} tx
* @param {Number} monotonicTime
* @param {BlockMeta?} block
*/

constructor(tx, monotonicTime, block) {
constructor(tx, block) {
this.tx = null;
this.hash = null;
this.mtime = util.now();
this.monotonicTime = null;
this.height = -1;
this.block = null;
this.index = -1;
this.time = 0;

if (tx)
this.fromTX(tx, monotonicTime, block);
this.fromTX(tx, block);
}

/**
* Inject properties from tx and block.
* @private
* @param {TX} tx
* @param {Number} monotonicTime
* @param {Block?} block
* @returns {TXRecord}
*/

fromTX(tx, monotonicTime, block) {
assert(Number.isSafeInteger(monotonicTime));

fromTX(tx, block) {
this.tx = tx;
this.hash = tx.hash();
this.monotonicTime = monotonicTime;

if (block)
this.setBlock(block);
Expand All @@ -279,13 +273,12 @@ class TXRecord {
/**
* Instantiate tx record from tx and block.
* @param {TX} tx
* @param {Number} monotonicTime
* @param {Block?} block
* @returns {TXRecord}
*/

static fromTX(tx, monotonicTime, block) {
return new this().fromTX(tx, monotonicTime, block);
static fromTX(tx, block) {
return new this().fromTX(tx, block);
}

/**
Expand Down Expand Up @@ -348,7 +341,7 @@ class TXRecord {
let size = 0;

size += this.tx.getSize();
size += 4 * 2;
size += 4;

if (this.block) {
size += 1;
Expand All @@ -375,7 +368,6 @@ class TXRecord {
this.tx.toWriter(bw);

bw.writeU32(this.mtime);
bw.writeU32(this.monotonicTime);

if (this.block) {
if (index === -1)
Expand Down Expand Up @@ -407,7 +399,6 @@ class TXRecord {

this.hash = this.tx.hash();
this.mtime = br.readU32();
this.monotonicTime = br.readU32();

if (br.readU8() === 1) {
this.block = br.readHash();
Expand Down
Loading

0 comments on commit 98940c3

Please sign in to comment.