Skip to content

Commit

Permalink
PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
RevCBH committed Aug 27, 2020
1 parent 52dfb22 commit 8d5b7e2
Show file tree
Hide file tree
Showing 6 changed files with 1,110 additions and 57 deletions.
51 changes: 36 additions & 15 deletions lib/wallet/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -671,15 +671,18 @@ class HTTP extends Server {

// Next, add this coin's details to this bucket's statistics
const covenantType = rules.typesByVal[coin.covenant.type];
if (!currentBucket.counts.hasOwnProperty(covenantType)) {
if (!Object.prototype.hasOwnProperty.call(
currentBucket.counts,
covenantType
)) {
currentBucket.counts[covenantType] = 0;
}
currentBucket.totalValue += coin.value;
currentBucket.counts[covenantType] += 1;
}

const keyedBucket = {}
keyedBucket[`${req.wallet.id}/${acct}`] = buckets
const keyedBucket = {};
keyedBucket[`${req.wallet.id}/${acct}`] = buckets;
res.json(200, { distributions: keyedBucket });
});

Expand Down Expand Up @@ -1080,14 +1083,20 @@ class HTTP extends Server {
assert(broadcast ? sign : true, 'Must sign when broadcasting.');

if (idempotencyKey) {
assert(broadcast && sign, 'Must sign and broadcast if using idempotency cache')
assert(
broadcast && sign,
'Must sign and broadcast if using idempotency cache'
);
}

const options = TransactionOptions.fromValidator(valid);
const [mtx, fromCache] = await req.wallet.withOpenCache(idempotencyKey, () =>
req.wallet.createOpen(name, force, options)
const [mtx, fromCache] = await req.wallet.withOpenCache(
idempotencyKey,
() => {
return req.wallet.createOpen(name, force, options);
}
);
res.setHeader('From-Cache', fromCache)
res.setHeader('From-Cache', fromCache);

if (broadcast) {
const tx = await req.wallet.sendMTX(mtx, passphrase);
Expand Down Expand Up @@ -1116,15 +1125,21 @@ class HTTP extends Server {
assert(lockup != null, 'Lockup is required.');
assert(broadcast ? sign : true, 'Must sign when broadcasting.');
if (idempotencyKey) {
assert(broadcast && sign, 'Must sign and broadcast if using idempotency cache')
assert(
broadcast && sign,
'Must sign and broadcast if using idempotency cache'
);
}

const options = TransactionOptions.fromValidator(valid);

const [mtx, fromCache] = await req.wallet.withBidCache(idempotencyKey, () =>
req.wallet.createBid(name, bid, lockup, options)
const [mtx, fromCache] = await req.wallet.withBidCache(
idempotencyKey,
() => {
return req.wallet.createBid(name, bid, lockup, options);
}
);
res.setHeader('From-Cache', fromCache)
res.setHeader('From-Cache', fromCache);

if (broadcast) {
const tx = await req.wallet.sendMTX(mtx, passphrase);
Expand Down Expand Up @@ -1209,7 +1224,10 @@ class HTTP extends Server {
assert(name, 'Must pass name.');
assert(data, 'Must pass data.');
if (idempotencyKey) {
assert(broadcast && sign, 'Must sign and broadcast if using idempotency cache')
assert(
broadcast && sign,
'Must sign and broadcast if using idempotency cache'
);
}

let resource;
Expand All @@ -1221,10 +1239,13 @@ class HTTP extends Server {

const options = TransactionOptions.fromValidator(valid);

const [mtx, fromCache] = await req.wallet.withBidCache(idempotencyKey, () =>
req.wallet.createUpdate(name, resource, options)
const [mtx, fromCache] = await req.wallet.withBidCache(
idempotencyKey,
() => {
return req.wallet.createUpdate(name, resource, options);
}
);
res.setHeader('From-Cache', fromCache)
res.setHeader('From-Cache', fromCache);

if (broadcast) {
const tx = await req.wallet.sendMTX(mtx, passphrase);
Expand Down
26 changes: 18 additions & 8 deletions lib/wallet/rpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2033,9 +2033,14 @@ class RPC extends RPCBase {
async sendOpenIdempotent(args, help) {
const opts = this._validateOpenIdempotent(args, help, 'sendopenidempotent');
const wallet = this.wallet;
const [tx, fromCache] = await wallet.sendOpenIdempotent(opts.name, opts.force, opts.idempotencyKey, {
account: opts.account,
});
const openResult = await wallet.sendOpenIdempotent(
opts.name,
opts.force,
opts.idempotencyKey,
{ account: opts.account }
);
const tx = openResult.result;
const fromCache = openResult.fromCache;

const result = tx.getJSON(this.network);
result.fromCache = fromCache;
Expand Down Expand Up @@ -2110,13 +2115,12 @@ class RPC extends RPCBase {
};
}


async sendBidIdempotent(args, help) {
const opts = this._validateBidIdempotent(args, help, 'sendbididempotent');
const wallet = this.wallet;
const tx = await wallet.sendBid(opts.name, opts.bid, opts.value, {
account: opts.account,
idempotencyKey: opts.idempotencyKey,
idempotencyKey: opts.idempotencyKey
});

return tx.getJSON(this.network);
Expand All @@ -2141,7 +2145,8 @@ class RPC extends RPCBase {
if (bid == null || value == null)
throw new RPCError(errs.TYPE_ERROR, 'Invalid values.');

if (bid > value) throw new RPCError(errs.TYPE_ERROR, 'Invalid bid.');
if (bid > value)
throw new RPCError(errs.TYPE_ERROR, 'Invalid bid.');

return {
name,
Expand Down Expand Up @@ -2268,11 +2273,16 @@ class RPC extends RPCBase {
}

async sendUpdateIdempotent(args, help) {
const opts = this._validateUpdateIdempotent(args, help, 'sendupdateidempotent');
const opts = this._validateUpdateIdempotent(
args,
help,
'sendupdateidempotent'
);

const wallet = this.wallet;
const tx = await wallet.sendUpdate(opts.name, opts.resource, {
account: opts.account,
idempotencyKey: opts.idempotencyKey,
idempotencyKey: opts.idempotencyKey
});

return tx.getJSON(this.network);
Expand Down
54 changes: 34 additions & 20 deletions lib/wallet/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -1860,53 +1860,67 @@ class Wallet extends EventEmitter {
return this.sendMTX(mtx, passphrase);
}

/**
* Perform an action with caching
* @param {LRU} cache the cache to use
* @param {string} idempotencyKey the cache key
* @param {Function} callback which generates a result on a cache miss
* @returns {Promise<{result:Object, fromCache:Boolean}>}
* a result and whether it was from the cache
*/
static async doWithCache(cache, idempotencyKey, callback) {
if (idempotencyKey && cache.has(idempotencyKey)) {
return [cache.get(idempotencyKey), true]
return {result: cache.get(idempotencyKey), fromCache: true};
}

const result = await callback()
const result = await callback();
if (idempotencyKey) {
cache.set(idempotencyKey, result);
}

return [result, false]
return [result, false];
}

/**
* If a key is provided: check the bid cache for a specific idempotencyKey
* or creates a bid and store it in the cache under that key. Otherwise,
* just perform the callback.
* @param {String} idempotencyKey
* @param {Function} bidCallback
*/
async withBidCache(idempotencyKey, bidCallback) {
return await Wallet.doWithCache(this.sendBidResults, idempotencyKey, bidCallback);
return await Wallet.doWithCache(
this.sendBidResults,
idempotencyKey,
bidCallback
);
}

async withOpenCache(idempotencyKey, openCallback) {
return await Wallet.doWithCache(this.sendOpenResults, idempotencyKey, openCallback);
return await Wallet.doWithCache(
this.sendOpenResults,
idempotencyKey,
openCallback
);
}

async withUpdateCache(idempotencyKey, updateCallback) {
return await Wallet.doWithCache(this.sendUpdateResults, idempotencyKey, updateCallback);
return await Wallet.doWithCache(
this.sendUpdateResults,
idempotencyKey,
updateCallback
);
}

/**
* Create and send a bid MTX.
* @param {String} name
* @param {Number} value
* @param {Number} lockup
* @param {Object} options
* @param {String} name the name to bid on
* @param {Number} value the value of the bid
* @param {Number} lockup the total lockup, including blind
* @param {Object} options bid options
* @returns {Object} a bid result
*/
async sendBid(name, value, lockup, options) {
const unlock = await this.fundLock.lock();
try {
const idempotencyKey = options ? options.idempotencyKey : null;
const [result] = await this.withBidCache(idempotencyKey,
const {result} = await this.withBidCache(idempotencyKey,
() => this._sendBid(name, value, lockup, options)
);
return result
return result;
} finally {
unlock();
}
Expand Down Expand Up @@ -2846,7 +2860,7 @@ class Wallet extends EventEmitter {
const unlock = await this.fundLock.lock();
try {
const idempotencyKey = options ? options.idempotencyKey : null;
const [result] = await this.withUpdateCache(idempotencyKey,
const {result} = await this.withUpdateCache(idempotencyKey,
() => this._sendUpdate(name, resource, options)
);
return result;
Expand Down
Loading

0 comments on commit 8d5b7e2

Please sign in to comment.