Skip to content

Commit

Permalink
txdb: Add bid and reveal mapping.
Browse files Browse the repository at this point in the history
bid: Add height to the bid entries.
  • Loading branch information
nodech committed Jun 6, 2024
1 parent 509ffe5 commit e6144b0
Show file tree
Hide file tree
Showing 7 changed files with 295 additions and 19 deletions.
2 changes: 2 additions & 0 deletions lib/wallet/layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ exports.wdb = {
* U[tx-hash] -> name undo record (name undo record by tx hash)
* i[name-hash][tx-hash][index] -> bid (BlindBid by name + tx + index)
* B[name-hash][tx-hash][index] -> reveal (BidReveal by name + tx + index)
* E[name-hash][tx-hash][index] - bid to reveal out (by bid txhash + index)
* v[blind-hash] -> blind (Blind Value by blind hash)
* o[name-hash] -> tx hash OPEN only (tx hash by name hash)
*/
Expand Down Expand Up @@ -163,6 +164,7 @@ exports.txdb = {
U: bdb.key('U', ['hash256']),
i: bdb.key('i', ['hash256', 'hash256', 'uint32']),
B: bdb.key('B', ['hash256', 'hash256', 'uint32']),
E: bdb.key('E', ['hash256', 'hash256', 'uint32']),
v: bdb.key('v', ['hash256']),
o: bdb.key('o', ['hash256'])
};
75 changes: 68 additions & 7 deletions lib/wallet/txdb.js
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,7 @@ class TXDB {
bb.lockup = options.lockup;
bb.blind = options.blind;
bb.own = options.own;
bb.height = options.height;
b.put(layout.i.encode(nameHash, hash, index), bb.encode());
}

Expand Down Expand Up @@ -646,35 +647,81 @@ class TXDB {
return brv;
}

/**
* Get reveal by bid outpoint.
* @param {Buffer} nameHash
* @param {Outpoint} bidOut
* @returns {Promise<BidReveal?>}
*/

async getRevealByBid(nameHash, bidOut) {
const rawOutpoint = await this.bucket.get(
layout.E.encode(nameHash, bidOut.hash, bidOut.index));

if (!rawOutpoint)
return null;

const outpoint = Outpoint.decode(rawOutpoint);
return this.getReveal(nameHash, outpoint);
}

/**
* Get bid by reveal outpoint.
* @param {Buffer} nameHash
* @param {Outpoint} revealOut
* @returns {Promise<BlindBid?>}
*/

async getBidByReveal(nameHash, revealOut) {
const reveal = await this.getReveal(nameHash, revealOut);

if (!reveal)
return null;

return this.getBid(nameHash, reveal.bidPrevout);
}

/**
* Write a reveal.
* @param {Object} b
* @param {Buffer} nameHash
* @param {Outpoint} outpoint
* @param {Object} options
* @param {String} options.name
* @param {Amount} options.value
* @param {Number} options.height
* @param {Boolean} options.own
* @param {Outpoint} options.bidPrevout
* @returns {void}
*/

putReveal(b, nameHash, outpoint, options) {
const {hash, index} = outpoint;
const {bidPrevout} = options;
const brv = new BidReveal();
brv.nameHash = nameHash;
brv.name = options.name;
brv.value = options.value;
brv.height = options.height;
brv.own = options.own;
brv.bidPrevout = bidPrevout;
b.put(layout.B.encode(nameHash, hash, index), brv.encode());
b.put(layout.E.encode(nameHash, bidPrevout.hash, bidPrevout.index),
outpoint.encode());
}

/**
* Delete a reveal.
* @param {Object} b
* @param {Buffer} nameHash
* @param {Outpoint} outpoint
* @param {Outpoint} bidPrevout
*/

removeReveal(b, nameHash, outpoint) {
removeReveal(b, nameHash, outpoint, bidPrevout) {
const {hash, index} = outpoint;
b.del(layout.B.encode(nameHash, hash, index));
b.del(layout.E.encode(nameHash, bidPrevout.hash, bidPrevout.index));
}

/**
Expand Down Expand Up @@ -1974,6 +2021,7 @@ class TXDB {
name,
lockup,
blind,
height,
own: false
});

Expand All @@ -1991,6 +2039,7 @@ class TXDB {
name,
lockup,
blind,
height,
own: true
});

Expand All @@ -2011,18 +2060,20 @@ class TXDB {
ns.setValue(output.value);
}

const {prevout} = tx.inputs[i];

if (!path) {
this.putReveal(b, nameHash, outpoint, {
name: ns.name,
value: output.value,
height: height,
own: false
own: false,
bidPrevout: prevout
});
updated = true;
break;
}

const {prevout} = tx.inputs[i];
const coin = view.getOutput(prevout);

if (coin) {
Expand All @@ -2040,7 +2091,8 @@ class TXDB {
name: ns.name,
value: output.value,
height: height,
own: true
own: true,
bidPrevout: prevout
});

updated = true;
Expand Down Expand Up @@ -2224,8 +2276,9 @@ class TXDB {
break;
}
case types.REVEAL: {
const input = tx.inputs[i];
const nameHash = covenant.getHash(0);
this.removeReveal(b, nameHash, tx.outpoint(i));
this.removeReveal(b, nameHash, tx.outpoint(i), input.prevout);
break;
}
}
Expand Down Expand Up @@ -3959,18 +4012,20 @@ class BlindBid extends bio.Struct {
this.value = -1;
this.lockup = 0;
this.blind = consensus.ZERO_HASH;
this.height = -1;
this.own = false;
}

getSize() {
return 1 + this.name.length + 41;
return 1 + this.name.length + 45;
}

write(bw) {
bw.writeU8(this.name.length);
bw.writeBytes(this.name);
bw.writeU64(this.lockup);
bw.writeBytes(this.blind);
bw.writeU32(this.height);
bw.writeU8(this.own ? 1 : 0);
return bw;
}
Expand All @@ -3979,6 +4034,7 @@ class BlindBid extends bio.Struct {
this.name = br.readBytes(br.readU8());
this.lockup = br.readU64();
this.blind = br.readBytes(32);
this.height = br.readU32();
this.own = br.readU8() === 1;
return this;
}
Expand All @@ -3991,6 +4047,7 @@ class BlindBid extends bio.Struct {
value: this.value === -1 ? undefined : this.value,
lockup: this.lockup,
blind: this.blind.toString('hex'),
height: this.height,
own: this.own
};
}
Expand Down Expand Up @@ -4041,13 +4098,14 @@ class BidReveal extends bio.Struct {
this.name = EMPTY;
this.nameHash = consensus.ZERO_HASH;
this.prevout = new Outpoint();
this.bidPrevout = new Outpoint();
this.value = 0;
this.height = -1;
this.own = false;
}

getSize() {
return 1 + this.name.length + 13;
return 1 + this.name.length + 13 + this.bidPrevout.getSize();
}

write(bw) {
Expand All @@ -4061,6 +4119,7 @@ class BidReveal extends bio.Struct {
bw.writeU64(this.value);
bw.writeU32(height);
bw.writeU8(this.own ? 1 : 0);
this.bidPrevout.write(bw);

return bw;
}
Expand All @@ -4070,6 +4129,7 @@ class BidReveal extends bio.Struct {
this.value = br.readU64();
this.height = br.readU32();
this.own = br.readU8() === 1;
this.bidPrevout.read(br);

if (this.height === 0xffffffff)
this.height = -1;
Expand All @@ -4082,6 +4142,7 @@ class BidReveal extends bio.Struct {
name: this.name.toString('ascii'),
nameHash: this.nameHash.toString('hex'),
prevout: this.prevout.toJSON(),
bidPrevout: this.bidPrevout.toJSON(),
value: this.value,
height: this.height,
own: this.own
Expand Down
44 changes: 44 additions & 0 deletions lib/wallet/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -4694,6 +4694,17 @@ class Wallet extends EventEmitter {
return this.txdb.getBlind(blind);
}

/**
* Get bid
* @param {Buffer} nameHash
* @param {Outpoint} outpoint
* @returns {Promise<BlindBid?>}
*/

async getBid(nameHash, outpoint) {
return this.txdb.getBid(nameHash, outpoint);
}

/**
* Get all bids for name.
* @param {Buffer} nameHash
Expand All @@ -4714,6 +4725,28 @@ class Wallet extends EventEmitter {
return this.txdb.getBids(name ? rules.hashName(name) : null);
}

/**
* Get bid by reveal.
* @param {Buffer} nameHash
* @param {Outpoint} outpoint - reveal outpoint
* @returns {Promise<BlindBid?>}
*/

async getBidByReveal(nameHash, outpoint) {
return this.txdb.getBidByReveal(nameHash, outpoint);
}

/**
* Get reveal.
* @param {Buffer} nameHash
* @param {Outpoint} outpoint
* @returns {BidReveal?}
*/

async getReveal(nameHash, outpoint) {
return this.txdb.getReveal(nameHash, outpoint);
}

/**
* Get all reveals by name.
* @param {Buffer} nameHash
Expand All @@ -4734,6 +4767,17 @@ class Wallet extends EventEmitter {
return this.txdb.getReveals(name ? rules.hashName(name) : null);
}

/**
* Get reveal for bid.
* @param {Buffer} nameHash
* @param {Outpoint} outpoint - bid outpoint
* @returns {Promise<BlindReveal?>}
*/

async getRevealByBid(nameHash, outpoint) {
return this.txdb.getRevealByBid(nameHash, outpoint);
}

/**
* Add a transaction to the wallets TX history.
* @param {TX} tx
Expand Down
2 changes: 1 addition & 1 deletion lib/wallet/walletdb.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class WalletDB extends EventEmitter {
this.feeRate = this.options.feeRate;
this.db = bdb.create(this.options);
this.name = 'wallet';
this.version = 2;
this.version = 3;

// chain state.
this.hasStateCache = false;
Expand Down
Loading

0 comments on commit e6144b0

Please sign in to comment.