Skip to content

Commit

Permalink
Merge branch 'production-namebase' into bennetthoffman/ch2594/hsd-pro…
Browse files Browse the repository at this point in the history
…xy-http-endpoint-for-sendbididempotent
  • Loading branch information
RevCBH committed Aug 28, 2020
2 parents 2cde6cc + 71b0f09 commit 5a4992d
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/primitives/mtx.js
Original file line number Diff line number Diff line change
Expand Up @@ -1598,6 +1598,9 @@ class CoinSelector {
case 'value':
this.coins.sort(sortValue);
break;
case 'sweepdust':
this.coins.sort(reversed(sortValue))
break;
default:
throw new FundingError(`Bad selection type: ${this.selection}.`);
}
Expand Down Expand Up @@ -1638,6 +1641,9 @@ class CoinSelector {
if (coin.covenant.isNonspendable())
return false;

if (this.selection === 'sweepdust' && coin.value === 0)
return false;

if (this.height === -1)
return true;

Expand Down Expand Up @@ -1898,6 +1904,7 @@ function sortRandom(a, b) {
return Math.random() > 0.5 ? 1 : -1;
}

// sorts from large to small
function sortValue(a, b) {
if (a.height === -1 && b.height !== -1)
return 1;
Expand All @@ -1908,6 +1915,10 @@ function sortValue(a, b) {
return b.value - a.value;
}

function reversed(sortFunc) {
return (a, b) => sortFunc(b, a)
}

function sortInputs(a, b) {
return a.compare(b);
}
Expand Down
30 changes: 30 additions & 0 deletions lib/wallet/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,36 @@ class HTTP extends Server {
res.json(200, { success: true });
});

// Clear cache
this.del('/cache/:cacheName', (req, res) => {
const cacheName = req.params.cacheName;
try {
for (const wallet of this.wdb.wallets.values()) {
wallet.clearCache(cacheName);
}
} catch (err) {
return res.json(400, {error: err.message})
}

return res.json(200, {message: "ok"});
});

// Clear specific cache key
this.del('/cache/:cacheName/:cacheKey', (req, res) => {
const cacheName = req.params.cacheName;
const cacheKey = req.params.cacheKey;

try {
for (const wallet of this.wdb.wallets.values()) {
wallet.clearCache(cacheName, cacheKey);
}
} catch (err) {
return res.json(400, {error: err.message})
}

return res.json(200, {message: "ok"});
})

// List wallets
this.get('/wallet', async (req, res) => {
if (!req.admin) {
Expand Down
26 changes: 26 additions & 0 deletions lib/wallet/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -1215,6 +1215,7 @@ class Wallet extends EventEmitter {
/**
* Fill a transaction with inputs without a lock.
* @private
* @param {MTX} mtx - The mutable transaction to fill
* @see MTX#selectCoins
* @see MTX#fill
*/
Expand Down Expand Up @@ -4948,6 +4949,31 @@ class Wallet extends EventEmitter {
static isWallet(obj) {
return obj instanceof Wallet;
}

/**
* Clear a cache.
* @param {String} cacheName
* @param {String | undefined} cacheKey
*/

clearCache(cacheName, cacheKey) {
let cache;
if(cacheName === 'bid') {
cache = this.sendBidResults;
} else if (cacheName === 'open') {
cache = this.sendOpenResults;
} else if (cacheName === 'update') {
cache = this.sendUpdateResults;
} else {
throw new Error(`invalid cache name: ${cacheName}`)
}

if (cacheKey === null || cacheKey === undefined || cacheKey === '') {
cache.reset();
} else {
cache.remove(cacheKey);
}
}
}

/*
Expand Down

0 comments on commit 5a4992d

Please sign in to comment.