Skip to content

Commit

Permalink
Merge 1782416 into 1c2599f
Browse files Browse the repository at this point in the history
  • Loading branch information
adibas03 committed Aug 2, 2018
2 parents 1c2599f + 1782416 commit be347ec
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 66 deletions.
148 changes: 82 additions & 66 deletions src/Actions/Pending.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,41 +9,48 @@ import { FnSignatures } from '../Enum';
* @param {number} exactPrice (optional) Expected gasPrice.
* @returns {Promise<boolean>} True if a pending transaction to this address exists.
*/
const hasPendingParity = async (
const hasPendingParity = (
conf: any,
txRequest: any,
opts: { type?: string; checkGasPrice?: boolean; exactPrice?: any }
) => {
opts.checkGasPrice = opts.checkGasPrice === undefined ? true : opts.checkGasPrice;
const provider = conf.web3.currentProvider;

return new Promise((resolve, reject) => {
provider.sendAsync(
{
jsonrpc: '2.0',
method: 'parity_pendingTransactions',
params: [],
id: 0o7
},
async (err: Error, res: any) => {
if (err) {
reject(err);
}
return new Promise( async (resolve, reject) => {
try{
await provider.sendAsync(
{
jsonrpc: '2.0',
method: 'parity_pendingTransactions',
params: [],
id: new Date().getTime()
},
async (err: Error, res: any) => {
if (err || res.error || !res.result) {
const errMsg = (err && err.message) || err || (res.error && res.error.message) || res.error;
conf.logger.error(errMsg)
return;
}

for (const count of Object.keys(res.result)) {
if (res.result[count].to === txRequest.address) {
const withValidGasPrice =
res.result[count] &&
(!opts.checkGasPrice ||
(await hasValidGasPrice(conf.web3, res.result[count], opts.exactPrice)));
if (res.result[count] && isOfType(res.result[count], opts.type) && withValidGasPrice) {
resolve(true);
for (const count of Object.keys(res.result)) {
if (res.result[count].to === txRequest.address) {
const withValidGasPrice =
res.result[count] &&
(!opts.checkGasPrice ||
(await hasValidGasPrice(conf, res.result[count], opts.exactPrice)));
if (res.result[count] && isOfType(res.result[count], opts.type) && withValidGasPrice) {
resolve(true);
}
}
}
resolve(false);
}
resolve(false);
}
);
);
} catch (e) {
conf.logger.error(e.message);
return;
}
});
};

Expand All @@ -65,63 +72,71 @@ const hasPendingGeth = (
const provider = conf.web3.currentProvider;

return new Promise((resolve, reject) => {
provider.send(
{
jsonrpc: '2.0',
method: 'txpool_content',
params: [],
id: 0o7
},
async (err: Error, res: any) => {
if (err) {
reject(err);
}
try{
provider.send(
{
jsonrpc: '2.0',
method: 'txpool_content',
params: [],
id: new Date().getTime()
},
async (err: Error, res: any) => {
if (err || res.error || !res.result) {
const errMsg = (err && err.message) || err || (res.error && res.error.message) || res.error;
conf.logger.error(errMsg)
return;
}

for (const account of Object.keys(res.result.pending)) {
for (const nonce in res.result.pending[account]) {
if (res.result.pending[account][nonce].to === txRequest.address) {
const withValidGasPrice =
res.result.pending[account][nonce] &&
(!opts.checkGasPrice ||
(await hasValidGasPrice(
conf.web3,
res.result.pending[account][nonce],
opts.exactPrice
)));
if (
res.result.pending[account][nonce] &&
isOfType(res.result.pending[account][nonce], opts.type) &&
withValidGasPrice
) {
resolve(true);
for (const account of Object.keys(res.result.pending)) {
for (const nonce in res.result.pending[account]) {
if (res.result.pending[account][nonce].to === txRequest.address) {
const withValidGasPrice =
res.result.pending[account][nonce] &&
(!opts.checkGasPrice ||
(await hasValidGasPrice(
conf,
res.result.pending[account][nonce],
opts.exactPrice
)));
if (
res.result.pending[account][nonce] &&
isOfType(res.result.pending[account][nonce], opts.type) &&
withValidGasPrice
) {
resolve(true);
}
}
}
}
resolve(false);
}
resolve(false);
}
);
);
} catch (e) {
conf.logger.error(e.message);
return;
}
});
};

/**
* Uses the Geth specific RPC request `txpool_content` to search
* for pending transactions in the transaction pool.
* @param {Web3} web3 the Web3 instance to use
* @param {Config} conf Config object.
* @param {TransactionReceipt} transaction Ethereum transaction receipt
* @param {number} exactPrice (optional) Expected gasPrice.
* @returns {Promise<boolean>} Transaction, if a pending transaction to this address exists.
*/
const hasValidGasPrice = async (web3: any, transaction: any, exactPrice?: any) => {
const hasValidGasPrice = async (conf: any, transaction: any, exactPrice?: any) => {
if (exactPrice) {
return exactPrice.valueOf() === transaction.gasPrice.valueOf();
}
const spread = 0.3;
let currentGasPrice: number;
await new Promise((resolve, reject) => {
web3.eth.getGasPrice((err: Error, res: any) => {
conf.web3.eth.getGasPrice((err: Error, res: any) => {
if (err) {
reject(err);
conf.logger.error(err)
return;
}
currentGasPrice = res;
resolve(true);
Expand Down Expand Up @@ -153,18 +168,19 @@ const isOfType = (transaction: any, type?: string) => {
* @param {boolean} checkGasPrice (optional, default: true) Check if transaction's gasPrice is sufficient for Network.
* @param {number} exactPrice (optional) Expected gasPrice to compare.
*/
const hasPending = (
const hasPending = async (
conf: any,
txRequest: any,
opts: { type?: string; checkGasPrice?: boolean; exactPrice?: any }
) => {
if (conf.client === 'parity') {
return hasPendingParity(conf, txRequest, opts);
}

if (conf.client === 'geth') {
return hasPendingGeth(conf, txRequest, opts);
}
if (conf.client === 'parity') {
return hasPendingParity(conf, txRequest, opts);
} else if (conf.client === 'geth') {
return hasPendingGeth(conf, txRequest, opts);
} else {
return;
}
};

export default hasPending;
1 change: 1 addition & 0 deletions src/TimeNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export default class TimeNode {
public startupMessage(): void {
this.config.logger.info('EAC-TimeNode');
this.config.logger.info('Version: ' + Version);
this.logNetwork();
}

public logNetwork(): void {
Expand Down
7 changes: 7 additions & 0 deletions test/unit/UnitTestPending.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,13 @@ describe('Pending Unit Tests', () => {
expect(pending).to.be.true;
});

it('Does not process unknown clients', async () => {
const gasPrice = 1 * 1e12;
const config = preConfig(mockConfig(), { client: '', gasPrice });
const pending = await hasPending(config, mockTx({ address: startAddr, gasPrice }), {});
expect(pending).to.be.undefined;
})

it('Detects valid Pending claim requests', async () => {
const expected = [true, true];
const results: any = [];
Expand Down

0 comments on commit be347ec

Please sign in to comment.