Skip to content

Commit

Permalink
Completed Fennel node connection
Browse files Browse the repository at this point in the history
  • Loading branch information
ts5746 committed Jul 23, 2024
1 parent a71d096 commit 9d801e0
Show file tree
Hide file tree
Showing 10 changed files with 159 additions and 97 deletions.
2 changes: 1 addition & 1 deletion config/blockchains.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ transactionBatchSize = 64
transactionValue = "0"
traceRawTransaction = false
rpcTimeout = 10000
rpcProtocol = "ws"
rpcProtocol = "http"
rpcHost = "localhost"
rpcPort = "9944"
rpcPath = ""
Expand Down
4 changes: 3 additions & 1 deletion lib/blockchains/bitcoin.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ const KeyEncoder = require('key-encoder').default;
const log = require('../common/logger');
const { hash } = require('../common/crypto');
const { ProcessingError } = require('../common/errors');
const { getEmptyState, createSignature } = require('./common');

// Whiteflag modules //
const wfState = require('../protocol/state');

// Whiteflag common blockchain functions //
const { getEmptyState, createSignature } = require('./common/state');

// Bitcoin sub-modules //
const btcRpc = require('./bitcoin/rpc');
const btcAccounts = require('./bitcoin/accounts');
Expand Down
4 changes: 3 additions & 1 deletion lib/blockchains/bitcoin/listener.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ module.exports = {
const log = require('../../common/logger');
const { ignore } = require('../../common/processing');
const { ProcessingError } = require('../../common/errors');
const { determineStartingBlock, logStartingBlock} = require('../common');

// Whiteflag modules //
const wfRxEvent = require('../../protocol/events').rxEvent;
const wfState = require('../../protocol/state');

// Whiteflag common blockchain functions //
const { determineStartingBlock, logStartingBlock} = require('../common/state');

// Bitcoin sub-modules //
const btcRpc = require('./rpc');
const btcAccounts = require('./accounts');
Expand Down
4 changes: 3 additions & 1 deletion lib/blockchains/bitcoin/rpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ module.exports = {

// Whiteflag common functions and classes //
const log = require('../../common/logger');
const { rpcCall, getNodeURL } = require('../common/rpc');

// Whiteflag modules //
const wfState = require('../../protocol/state');

// Whiteflag common blockchain functions //
const { rpcCall, getNodeURL } = require('../common/rpc');

// Module constants //
const STATUSINTERVAL = 1200000; // Every two minutes
const INFOINTERVAL = 3600000; // Every hour
Expand Down
55 changes: 33 additions & 22 deletions lib/blockchains/common/rpc.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';
/**
* @module lib/blockchains/common/rpc
* @summary Whiteflag API blockchain common RPC module
* @description Module for RPC calls over http to a blcokchain node
* @summary Whiteflag API blockchains common RPC module
* @description Module for common blockchain node RPC functions
*/
module.exports = {
rpcCall,
Expand Down Expand Up @@ -35,52 +35,63 @@ function rpcCall(rpcMethod, rpcParams, rpcURL, rpcUser, rpcPass, rpcTimeout = 10
// Prepare request
let rpcOptions = {
url: rpcURL,
auth: {
user: rpcUser,
password: rpcPass
},
method: 'post',
headers:
{
'content-type': 'text/plain'
'content-type': 'application/json'
},
body: JSON.stringify({
'id': 1,
'jsonrpc': '2.0',
'method': rpcMethod,
'params': rpcParams
})
};
// Check authentication
if (rpcUser && rpcPass) {
rpcOptions.authorization = {
user: rpcUser,
password: rpcPass
};
}
// Send request with a timeout
log.trace(MODULELOG, `Making remote procedure call to ${rpcURL}: ${JSON.stringify(rpcOptions.body)}`);
return timeoutPromise(
new Promise((resolve, reject) => {
request(rpcOptions, function rpcRequestCb(err, response, body) {
request(rpcOptions, function rpcRequestCb(err, header, body) {
if (err) return reject(err);
ignore(response);
ignore(header);
let response = {};
try {
if (JSON.parse(body).error !== null) {
return reject(new Error(JSON.stringify(JSON.parse(body).error)));
}
resolve(JSON.parse(body).result);
response = JSON.parse(body);
} catch(jsonErr) {
reject(jsonErr);
}
if (response.error) {
return reject(new Error(JSON.stringify(response.error)));
}
resolve(response.result);
});
}),
rpcTimeout
);
}

/**
* Gets URL of the node from the blockchain configuration
* Gets URL of the blockchain node from the configuration
* @private
* @param {Object} bcConfig blockchain configuration parameters
* @returns {string} the url of the blockchain node
* @param {Object} blockchainConfig blockchain configuration parameters
* @param {boolean} hideCredentials whether to inlude username and password in url
* @returns {string} the url of the Fennel node
*/
function getNodeURL(bcConfig) {
const rpcProtocol = (bcConfig.rpcProtocol || 'http') + '://';
const rpcHost = bcConfig.rpcHost || 'localhost';
const rpcPort = ':' + (bcConfig.rpcPort || '8545');
const rpcPath = bcConfig.rpcPath || '';
return (rpcProtocol + rpcHost + rpcPort + rpcPath);
function getNodeURL(blockchainConfig, hideCredentials = false) {
const rpcProtocol = (blockchainConfig.rpcProtocol || 'http') + '://';
const rpcHost = blockchainConfig.rpcHost || 'localhost';
const rpcPort = blockchainConfig.rpcPort ? ':' + blockchainConfig.rpcPort : '';
const rpcPath = blockchainConfig.rpcPath || '';
let rpcAuth = '';
if (blockchainConfig.username && blockchainConfig.password && !hideCredentials) {
rpcAuth = blockchainConfig.username + ':' + blockchainConfig.password + '@';
}
return (rpcProtocol + rpcAuth + rpcHost + rpcPort + rpcPath);
}
26 changes: 3 additions & 23 deletions lib/blockchains/common.js → lib/blockchains/common/state.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
'use strict';
/**
* @module lib/blockchains/common
* @summary Whiteflag API common blockchains module
* @description Module for common blockchain functions
* @summary Whiteflag API blockchains common state module
* @description Module for common blockchain state functions
*/
module.exports = {
getEmptyState,
getNodeURL,
determineStartingBlock,
logStartingBlock,
createSignature
Expand All @@ -17,7 +16,7 @@ const KeyEncoder = require('key-encoder').default;
const jwt = require('jsonwebtoken');

// Whiteflag common functions and classes //
const log = require('../common/logger');
const log = require('../../common/logger');

/**
* Returns an empty blockchain state
Expand All @@ -32,25 +31,6 @@ function getEmptyState() {
};
}

/**
* Gets URL of the blockchain node from the configuration
* @private
* @param {Object} blockchainConfig blockchain configuration parameters
* @param {boolean} hideCredentials whether to inlude username and password in url
* @returns {string} the url of the Fennel node
*/
function getNodeURL(blockchainConfig, hideCredentials = false) {
const rpcProtocol = (blockchainConfig.rpcProtocol || 'http') + '://';
const rpcHost = blockchainConfig.rpcHost || 'localhost';
const rpcPort = blockchainConfig?.rpcPort ? ':' + blockchainConfig.rpcPort : ''
const rpcPath = blockchainConfig.rpcPath || '';
let rpcAuth = '';
if (blockchainConfig.username && blockchainConfig.password && !hideCredentials) {
rpcAuth = blockchainConfig.username + ':' + blockchainConfig.password + '@';
}
return (rpcProtocol + rpcAuth + rpcHost + rpcPort + rpcPath);
}

/**
* Determines the starting block for a blockchain listener
* @function determineStartingBlock
Expand Down
4 changes: 3 additions & 1 deletion lib/blockchains/ethereum.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ const KeyEncoder = require('key-encoder').default;
const log = require('../common/logger');
const { hash } = require('../common/crypto');
const { ProcessingError } = require('../common/errors');
const { getEmptyState, createSignature } = require('./common');

// Whiteflag modules //
const wfState = require('../protocol/state');

// Whiteflag common blockchain functions //
const { getEmptyState, createSignature } = require('./common/state');

// Ethereum sub-modules //
const ethRpc = require('./ethereum/rpc');
const ethAccounts = require('./ethereum/accounts');
Expand Down
4 changes: 3 additions & 1 deletion lib/blockchains/ethereum/listener.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ module.exports = {
const log = require('../../common/logger');
const { ignore } = require('../../common/processing');
const { ProcessingError } = require('../../common/errors');
const { determineStartingBlock, logStartingBlock} = require('../common');

// Whiteflag modules //
const wfState = require('../../protocol/state');

// Whiteflag common blockchain functions //
const { determineStartingBlock, logStartingBlock} = require('../common/state');

// Whiteflag event emitters //
const wfRxEvent = require('../../protocol/events').rxEvent;

Expand Down
6 changes: 4 additions & 2 deletions lib/blockchains/fennel.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ module.exports = {
const log = require('../common/logger');
const { ignore } = require('../common/processing');
const { ProcessingError, ProtocolError } = require('../common/errors');
const { getEmptyState, createSignature } = require('./common');

// Whiteflag modules //
const wfState = require('../protocol/state');

// Whiteflag common blockchain functions //
const { getEmptyState } = require('./common/state');

// Fennel sub-modules //
const fennelRpc = require('./fennel/rpc');

Expand Down Expand Up @@ -58,7 +60,7 @@ function initFennel(fennelConfig, callback) {
// Connect to Fennel node
fennelRpc.init(fennelConfig, _fennelState)
.then(() => callback(null, _blockchainName))
.catch(err => callback(new Error(`Could not connect to Fennel node: ${err.message}`), _blockchainName));
.catch(initErr => callback(initErr, _blockchainName));
});
}

Expand Down
Loading

0 comments on commit 9d801e0

Please # to comment.