Skip to content

Commit

Permalink
Merge pull request #392 from clark800/server
Browse files Browse the repository at this point in the history
Convert server methods, add unit tests, and cleanup
  • Loading branch information
clark800 committed Jul 2, 2015
2 parents 1a7cdd7 + 32ca23a commit 077a534
Show file tree
Hide file tree
Showing 14 changed files with 76 additions and 222 deletions.
2 changes: 1 addition & 1 deletion src/api/common/constants.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';
const Transaction = require('./core').Transaction;
const Transaction = require('./utils').core.Transaction;
const flagIndices = Transaction.set_clear_flags.AccountSet;

const AccountFlagIndices = {
Expand Down
2 changes: 0 additions & 2 deletions src/api/common/core.js

This file was deleted.

3 changes: 1 addition & 2 deletions src/api/common/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
const utils = require('./utils');

module.exports = {
core: require('./core'),
core: utils.core,
constants: require('./constants'),
errors: require('./errors'),
validate: require('./validate'),
server: require('./server'),
dropsToXrp: utils.dropsToXrp,
xrpToDrops: utils.xrpToDrops,
toRippledAmount: utils.toRippledAmount,
Expand Down
2 changes: 1 addition & 1 deletion src/api/common/schema-validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const _ = require('lodash');
const fs = require('fs');
const path = require('path');
const validator = require('is-my-json-valid');
const ripple = require('./core');
const ripple = require('./utils').core;
const ValidationError = require('./errors').ValidationError;

let SCHEMAS = {};
Expand Down
116 changes: 0 additions & 116 deletions src/api/common/server.js

This file was deleted.

2 changes: 2 additions & 0 deletions src/api/common/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';
const BigNumber = require('bignumber.js');
const core = require('../../core');

function dropsToXrp(drops) {
return (new BigNumber(drops)).dividedBy(1000000.0).toString();
Expand Down Expand Up @@ -42,6 +43,7 @@ function composeAsync(wrapper, callback) {
}

module.exports = {
core,
dropsToXrp,
xrpToDrops,
toRippledAmount,
Expand Down
2 changes: 1 addition & 1 deletion src/api/common/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
const _ = require('lodash');
const ValidationError = require('./errors').ValidationError;
const schemaValidate = require('./schema-validator');
const ripple = require('./core');
const ripple = require('./utils').core;

function error(text) {
return new ValidationError(text);
Expand Down
14 changes: 8 additions & 6 deletions src/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
const ripple = require('./common').core;
const server = require('./server/server');
const connect = server.connect;
// const getServerStatus = server.getServerStatus;
// const getFee = server.getFee;
// const isConnected = server.isConnected;
const disconnect = server.disconnect;
const getServerInfo = server.getServerInfo;
const getFee = server.getFee;
const isConnected = server.isConnected;
const getTransaction = require('./ledger/transaction');
const getAccountTransactions = require('./ledger/transactions');
const getTrustlines = require('./ledger/trustlines');
Expand All @@ -29,9 +30,10 @@ function RippleAPI(options) {

RippleAPI.prototype = {
connect,
// getServerStatus,
// getFee,
// isConnected,
disconnect,
isConnected,
getServerInfo,
getFee,

getTransaction,
getAccountTransactions,
Expand Down
55 changes: 0 additions & 55 deletions src/api/ledger/utils.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
/* eslint-disable valid-jsdoc */
'use strict';
const _ = require('lodash');
const async = require('async');
const asyncify = require('simple-asyncify');
const common = require('../common');
const ripple = common.core;

// If the marker is omitted from a response, you have reached the end
// getter(marker, limit, callback), callback(error, {marker, results})
Expand Down Expand Up @@ -46,26 +42,6 @@ function renameCounterpartyToIssuerInOrder(order) {
return _.assign({}, order, _.omit(changes, _.isUndefined));
}

function isValidHash256(hash) {
return ripple.UInt256.is_valid(hash);
}

function parseLedger(ledger) {
if (/^current$|^closed$|^validated$/.test(ledger)) {
return ledger;
}

if (ledger && Number(ledger) >= 0 && isFinite(Number(ledger))) {
return Number(ledger);
}

if (isValidHash256(ledger)) {
return ledger;
}

return 'validated';
}

function signum(num) {
return (num === 0) ? 0 : (num > 0 ? 1 : -1);
}
Expand All @@ -87,41 +63,10 @@ function compareTransactions(first, second) {
return Number(first.ledgerVersion) < Number(second.ledgerVersion) ? -1 : 1;
}

function attachDate(api, baseTransactions, callback) {
const groupedTx = _.groupBy(baseTransactions, function(tx) {
return tx.ledger_index;
});

function attachDateToTransactions(transactions, data) {
return _.map(transactions, function(tx) {
return _.assign(tx, {date: data.ledger.close_time});
});
}

function getLedger(ledgerIndex, _callback) {
api.remote.requestLedger({ledger_index: ledgerIndex}, _callback);
}

function attachDateToLedgerTransactions(_groupedTx, ledger, _callback) {
const transactions = _groupedTx[ledger];
async.waterfall([
_.partial(getLedger, Number(ledger)),
asyncify(_.partial(attachDateToTransactions, transactions))
], _callback);
}

const ledgers = _.keys(groupedTx);
const flatMap = async.seq(async.map, asyncify(_.flatten));
const iterator = _.partial(attachDateToLedgerTransactions, groupedTx);
flatMap(ledgers, iterator, callback);
}

module.exports = {
parseLedger: parseLedger,
compareTransactions: compareTransactions,
renameCounterpartyToIssuer: renameCounterpartyToIssuer,
renameCounterpartyToIssuerInOrder: renameCounterpartyToIssuerInOrder,
attachDate: attachDate,
getRecursive: getRecursive,
wrapCatch: common.wrapCatch,
common: common
Expand Down
30 changes: 21 additions & 9 deletions src/api/server/server.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,45 @@
'use strict';
const common = require('../common');

// If a ledger is not received in this time, consider the connection offline
const CONNECTION_TIMEOUT = 1000 * 30;

function connect(callback) {
this.remote.connect(callback);
}

function disconnect(callback) {
this.remote.disconnect(callback);
}

function isUpToDate(remote) {
const server = remote.getServer();
return Boolean(server) && (remote._stand_alone
|| (Date.now() - server._lastLedgerClose) <= CONNECTION_TIMEOUT);
}

function isConnected() {
return common.server.isConnected(this.remote);
return Boolean(this.remote._ledger_current_index) && isUpToDate(this.remote);
}

function getServerStatus(callback) {
common.server.getStatus(this.remote, function(error, status) {
function getServerInfo(callback) {
this.remote.requestServerInfo((error, info) => {
if (error) {
callback(new common.errors.RippledNetworkError(error.message));
} else {
callback(null, status);
callback(null, info);
}
});
}

function getFee(callback) {
const fee = this.remote.createTransaction()._computeFee();
callback(null, {fee: common.dropsToXrp(fee)});
function getFee() {
return common.dropsToXrp(this.remote.createTransaction()._computeFee());
}


module.exports = {
connect: connect,
disconnect: disconnect,
isConnected: isConnected,
getServerStatus: getServerStatus,
getServerInfo: getServerInfo,
getFee: getFee
};
13 changes: 0 additions & 13 deletions src/api/transaction/settings.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/* @flow */
/* eslint-disable valid-jsdoc */
'use strict';
const _ = require('lodash');
const assert = require('assert');
Expand Down Expand Up @@ -28,16 +27,6 @@ function setTransactionFlags(transaction, values) {
}
}

/**
* Set fields on a transaction based on input and fields schema object
*
* @param {Transaction} transaction
* @param {Object} input - Object whose properties are used to set fields on
* the transaction
* @param {Object} fieldSchema - Object that holds the schema of each field
*
* @returns undefined
*/
function setTransactionFields(transaction, input) {
const fieldSchema = AccountFields;
for (const fieldName in fieldSchema) {
Expand Down Expand Up @@ -78,8 +67,6 @@ function setTransactionFields(transaction, input) {
}

/**
* Convert a numerical transfer rate in ripple-rest format to ripple-lib
*
* Note: A fee of 1% requires 101% of the destination to be sent for the
* destination to receive 100%.
* The transfer rate is specified as the input amount as fraction of 1.
Expand Down
16 changes: 0 additions & 16 deletions src/api/transaction/utils.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,8 @@
/* @flow */
/* eslint-disable valid-jsdoc */
'use strict';
const BigNumber = require('bignumber.js');
const common = require('../common');

/**
* Helper that sets bit flags on transactions
*
* @param {Transaction} transaction - Transaction object that is used to submit
* requests to ripple
* @param {Object} options
* @param {Object} options.flags - Holds flag names to set on transaction when
* parameter values are true or false on input
* @param {Object} options.input - Holds parameter values
* @param {String} options.clear_setting - Used to check if parameter values
* besides false mean false
*
*
* @returns undefined
*/
function setTransactionBitFlags(transaction: any, values: any, flags: any):
void {
for (const flagName in flags) {
Expand Down

0 comments on commit 077a534

Please sign in to comment.