Skip to content

Commit

Permalink
add additional caching, fix claim success test
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbosworth committed Apr 18, 2018
1 parent 238d339 commit 01ddbe9
Show file tree
Hide file tree
Showing 14 changed files with 68 additions and 17 deletions.
1 change: 1 addition & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
web: node server.js
2 changes: 2 additions & 0 deletions chain/chain_rpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const {SSS_CHAIN_RPC_HOST} = process.env;
const {SSS_CHAIN_RPC_PASS} = process.env;
const {SSS_CHAIN_RPC_USER} = process.env;

const chainTimeoutMs = 3000;
const [regtestRpcHost, regtestRpcPort] = regtest.rpc_host.split(':');
const regtestRpcPass = regtest.rpc_pass;
const regtestRpcUser = regtest.rpc_user;
Expand Down Expand Up @@ -52,6 +53,7 @@ module.exports = ({cmd, network, params}, cbk) => {
const user = credentials.user[network];

chainRpc.init(host, port, user, pass);
chainRpc.setTimeout(chainTimeoutMs);

// Should the params be a single argument instead of array, array-ize it.
const niceParams = !Array.isArray(params || []) ? [params] : params || [];
Expand Down
4 changes: 1 addition & 3 deletions chain/conf/chain_server.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@
"// Default connection values for chain server connections": "",

"regtest": {
"rpc_host": "127.0.0.1",
"rpc_host": "127.0.0.1:18335",
"rpc_pass": "pass",
"rpc_port": 18335,
"rpc_user": "user"
},
"testnet": {
"rpc_host": "127.0.0.1",
"rpc_port": 18332,
"rpc_user": "bitcoinrpc"
}
}
Expand Down
22 changes: 21 additions & 1 deletion chain/get_blockchain_info.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ const chainRpc = require('./chain_rpc');
const {getBestBlockHash} = require('./conf/rpc_commands');
const {getBlockCount} = require('./conf/rpc_commands');

const cacheChainInfoMs = 5000;
const cached = {};

/** Get info about the best chain
{
[is_cache_ok]: <Is Cache Allowed Bool>
network: <Network Name String>
}
Expand All @@ -17,7 +21,17 @@ const {getBlockCount} = require('./conf/rpc_commands');
current_height: <Block Height Number>
}
*/
module.exports = ({network}, cbk) => {
module.exports = (args, cbk) => {
const {network} = args;
const now = Date.now();

if (!!args.is_cache_ok && (cached[network] || {}).expires_at > Date.now()) {
return cbk(null, {
current_hash: cached[network].current_hash,
current_height: cached[network].current_height,
});
}

return asyncAuto({
// Determine the current chain tip hash
getCurrentHash: cbk => chainRpc({network, cmd: getBestBlockHash}, cbk),
Expand All @@ -30,6 +44,12 @@ module.exports = ({network}, cbk) => {
return cbk(err);
}

cached[network] = {
expires_at: Date.now() + cacheChainInfoMs,
current_hash: getCurrentHash,
current_height: getCurrentHeight,
};

return cbk(null, {
current_hash: getCurrentHash,
current_height: getCurrentHeight,
Expand Down
3 changes: 1 addition & 2 deletions chain/spawn_chain_daemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ const unableToStartServer = /Unable.to.start.server/;
*/
module.exports = (args, cbk) => {
const miningKey = Buffer.from(args.mining_public_key, 'hex');
const rpcHost = chainServer.regtest.rpc_host;
const [rpcHost, rpcPort] = chainServer.regtest.rpc_host.split(':');
const rpcPass = chainServer.regtest.rpc_pass;
const rpcPort = chainServer.regtest.rpc_port;
const rpcUser = chainServer.regtest.rpc_user;
const tmpDir = `/tmp/${uuidv4()}`;

Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"cors": "2.8.4",
"express": "4.16.3",
"helmet": "3.12.0",
"ln-service": "alexbosworth/ln-service",
"ln-service": "3.0.4",
"morgan": "1.9.0",
"node-bitcoin-rpc": "1.1.3",
"pug": "2.0.3",
Expand All @@ -35,7 +35,7 @@
"test": "test"
},
"engines": {
"node": "8.9.4",
"node": "8.11.1",
"npm": "5.6.0"
},
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion service/check_swap_status.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const swapRate = 0.015;
module.exports = (args, cbk) => {
return asyncAuto({
// Get the current chain height
getChainInfo: cbk => getBlockchainInfo({network}, cbk),
getChainInfo: cbk => getBlockchainInfo({is_cache_ok: true, network}, cbk),

// Parse the encoded invoice
invoice: cbk => {
Expand Down
8 changes: 7 additions & 1 deletion service/complete_swap_transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@ const {swapScriptInTransaction} = require('./../swaps');
module.exports = (args, cbk) => {
return asyncAuto({
// Check the current state of the blockchain to get a good locktime
getBlockchainInfo: cbk => getBlockchainInfo({network: args.network}, cbk),
getBlockchainInfo: cbk => {
return getBlockchainInfo({
is_cache_ok: true,
network: args.network,
},
cbk);
},

// Figure out what fee is needed to sweep the funds
getFee: cbk => getChainFeeRate({network: args.network}, cbk),
Expand Down
2 changes: 1 addition & 1 deletion service/find_scriptpub_in_block.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ module.exports = (args, cbk) => {

// Find transaction in block
findTransaction: ['getBlock', ({getBlock}, cbk) => {
if (!getBlock.is_cached_result) {
if (args.network !== 'regtest' && !getBlock.is_cached_result) {
cachedBlocks[args.block_hash] = getBlock;

setTimeout(() => cachedBlocks[args.block_hash] = null, cacheBlockMs);
Expand Down
8 changes: 7 additions & 1 deletion service/find_swap_transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,13 @@ const blockSearchRateLimit = 300;
module.exports = (args, cbk) => {
return asyncAuto({
// Get the current block tip hash
getChainInfo: cbk => getBlockchainInfo({network: args.network}, cbk),
getChainInfo: cbk => {
return getBlockchainInfo({
is_cache_ok: args.network !== 'regtest',
network: args.network,
},
cbk);
},

// Check the arguments
validate: cbk => {
Expand Down
18 changes: 18 additions & 0 deletions service/get_invoice_details.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const {lightningDaemon} = require('./../lightning');
const {returnResult} = require('./../async-util');

const approxTxVSize = 200;
const cachedFee = {};
const cacheFeeMs = 60 * 1000;

/** Get invoice details
Expand Down Expand Up @@ -89,11 +91,27 @@ module.exports = (args, cbk) => {

// Get the current chain fees
chainFee: ['invoice', ({invoice}, cbk) => {
const fee = cachedFee[invoice.network];

if (!!fee && fee.expires_at > Date.now()) {
return cbk(null, {
fee_tokens_per_vbyte: fee.fee_tokens_per_vbyte,
is_cached_fee: true,
});
}

return getChainFeeRate({network: invoice.network}, cbk);
}],

// Make sure the chain fee is not too high
checkChainFee: ['chainFee', 'invoice', ({chainFee, invoice}, cbk) => {
if (!chainFee.is_cached_fee) {
cachedFee[invoice.network] = {
expires_at: Date.now() + cacheFeeMs,
fee_tokens_per_vbyte: chainFee.fee_tokens_per_vbyte,
};
}

const approxFee = chainFee.fee_tokens_per_vbyte * approxTxVSize;

if (approxFee / invoice.tokens > args.max_invoice_fee_rate) {
Expand Down
2 changes: 1 addition & 1 deletion service/transaction_has_scriptpub.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ module.exports = (args, cbk) => {

// Parse the transaction hex
transaction: ['getTransaction', ({getTransaction}, cbk) => {
if (!getTransaction.is_cached_result) {
if (args.network !== 'regtest' && !getTransaction.is_cached_result) {
cachedTx[args.transaction_id] = getTransaction.transaction;

setTimeout(() => cachedTx[args.transaction_id] = null, cacheTxMs);
Expand Down
1 change: 1 addition & 0 deletions test/claim_success.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ module.exports = (args, cbk) => {
refund_public_key: refundPk,
refund_public_key_hash: refundPkHash,
timeout_block_height: maturityBlockCount + swapTimeoutBlockCount,
tokens: res.bobUtxo.tokens,
},
cbk);
}],
Expand Down

0 comments on commit 01ddbe9

Please sign in to comment.