Skip to content

Commit

Permalink
wallet: monotonic time indexing
Browse files Browse the repository at this point in the history
  • Loading branch information
braydonf committed Sep 14, 2018
1 parent a1af826 commit b5c970a
Show file tree
Hide file tree
Showing 11 changed files with 252 additions and 58 deletions.
8 changes: 6 additions & 2 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
- [x] Monotonic time in wallet for blocks
- [x] Count indexing for confirmed transactions
- [x] RPC methods added for tx history
- [ ] Use `full` event instead of `sleep()` for tests
- [ ] Use block monotonic time for indexing txs
- [x] Use block monotonic time for indexing txs
- [ ] Shift unconfirmed txrecord times and indexes if a block comes in
with a monotonic time greater than any still unconfirmed transactions
- [ ] Add tests for reorgs by invalidating blocks
- [ ] Reorgs for monotonic time
- [ ] Reorgs for tx indexing
Expand All @@ -14,5 +15,8 @@
consider having limits be configurable as they could be hardware specific.
- [ ] Update bdb with support for passing reverse to iterators as needed
- [ ] Consider using bfile.rimraf with `/tmp/*` guards in tests
- [ ] Use `full` event instead of `sleep()` for tests
- [ ] Consider dropping index `M[account][time][hash]` as it may not be needed
- [ ] Use count index for `listtransactions` RPC method
- [ ] Check that rpc help, docs and jsdocs are correct
- [ ] Test and document upgrading existing wallets
2 changes: 1 addition & 1 deletion lib/node/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ class Node extends EventEmitter {
*/

error(err) {
this.logger.error(err);
this.logger.error(err.stack);
this.emit('error', err);
}

Expand Down
4 changes: 2 additions & 2 deletions lib/wallet/layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,15 @@ exports.wdb = {
* d[hash][index] -> undo coin
* s[hash][index] -> spent by hash
* p[hash] -> dummy (pending flag)
* m[time][hash] -> dummy (tx by time)
* m[monotonic-time][hash] -> dummy (tx by 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][time][hash] -> dummy (tx by time + account)
* M[account][monotonic-time][hash] -> dummy (tx by time + account)
* H[account][height][hash] -> dummy (tx by height + account)
* C[account][hash][index] -> dummy (coin by account)
* b[height] -> block record
Expand Down
21 changes: 15 additions & 6 deletions lib/wallet/records.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,33 +236,39 @@ class TXRecord {
* Create tx record.
* @constructor
* @param {TX} tx
* @param {Number} monotonicTime
* @param {BlockMeta?} block
*/

constructor(tx, block) {
constructor(tx, monotonicTime, 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, block);
this.fromTX(tx, monotonicTime, block);
}

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

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

this.tx = tx;
this.hash = tx.hash();
this.monotonicTime = monotonicTime;

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

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

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

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

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

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

if (this.block) {
if (index === -1)
Expand Down Expand Up @@ -399,6 +407,7 @@ 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
16 changes: 9 additions & 7 deletions lib/wallet/rpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -1249,7 +1249,7 @@ class RPC extends RPCBase {
const reverse = valid.bool(2, false);

if (limit > 100)
throw new RPCError(INVALID_PARAMETER, 'Limit above max of 100.');
throw new RPCError(errs.INVALID_PARAMETER, 'Limit above max of 100.');

const txrecords = await wallet.listHistory(name, limit, reverse);

Expand All @@ -1274,7 +1274,7 @@ class RPC extends RPCBase {
const reverse = valid.bool(3, false);

if (limit > 100)
throw new RPCError(INVALID_PARAMETER, 'Limit above max of 100.');
throw new RPCError(errs.INVALID_PARAMETER, 'Limit above max of 100.');

const txrecords = await wallet.listHistoryAfter(name, txid.reverse(), limit, reverse);

Expand All @@ -1299,13 +1299,15 @@ class RPC extends RPCBase {
const reverse = valid.bool(3, false);

if (limit > 100)
throw new RPCError(INVALID_PARAMETER, 'Limit above max of 100.');
throw new RPCError(errs.INVALID_PARAMETER, 'Limit above max of 100.');

const time = new Date(timestamp);
if (time == 'Invalid Date')
throw new RPCError(INVALID_PARAMETER, 'Invalid timestamp.');
const date = new Date(timestamp);
if (date == 'Invalid Date')
throw new RPCError(errs.INVALID_PARAMETER, 'Invalid timestamp.');

const txrecords = wallet.listHistoryByTime(name, time, limit, reverse);
const time = util.time(date);

const txrecords = await wallet.listHistoryByTime(name, time, limit, reverse);

const out = [];
for (let i = 0; i < txrecords.length; i++)
Expand Down
Loading

0 comments on commit b5c970a

Please sign in to comment.