Skip to content

Commit

Permalink
Merge 35e2a74 into ab98cbf
Browse files Browse the repository at this point in the history
  • Loading branch information
adibas03 committed Aug 16, 2018
2 parents ab98cbf + 35e2a74 commit fbfbe92
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 43 deletions.
4 changes: 2 additions & 2 deletions src/Actions/Pending.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,9 @@ const isOfType = (transaction: any, type?: string) => {
const hasPending = async (conf: any, txRequest: any, opts: PendingOpts): Promise<boolean> => {
let result = false;

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

Expand Down
57 changes: 27 additions & 30 deletions src/Config/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default class Config implements IConfigParams {
public autostart: boolean;
public cache: Cache<ICachedTxDetails>;
public claiming: boolean;
public client?: string;
public client?: Promise<string>;
public eac: any;
public economicStrategy?: IEconomicStrategy;
public logger?: ILogger;
Expand Down Expand Up @@ -55,7 +55,11 @@ export default class Config implements IConfigParams {
this.logger = params.logger || new DefaultLogger();

if (!params.disableDetection) {
this.getConnectedClient();
try{
this.client = this.getConnectedClient();
} catch (e) {
this.logger.error(e.message);
}
}

this.cache = new Cache(this.logger);
Expand Down Expand Up @@ -91,26 +95,25 @@ export default class Config implements IConfigParams {
this.util = new W3Util(this.web3);
}

public clientSet(): boolean {
return typeof this.client === 'string';
public clientSet(client?: any): boolean {
client = client || this.client;
return typeof client === 'string';
}

public async awaitClientSet(): Promise<any> {
if (this.clientSet()) {
if (this.clientSet(await this.client)) {
return true;
} else {
return new Promise(resolve => {
setTimeout(() => {
resolve(this.awaitClientSet());
}, 100);
});
return false;
}
}

public async getConnectedClient(): Promise<any> {
return new Promise(async (resolve, reject) => {
public async getConnectedClient(): Promise<string> {
return new Promise(async (resolve): Promise<any> => {
if (!this.web3) {
reject();
const client: any = 'none';
this.logger.error(`Client: No web3!!!`);
return resolve(client);
}
try {
const method = 'txpool_content';
Expand All @@ -121,9 +124,9 @@ export default class Config implements IConfigParams {
params: [],
id: 0x07a
},
async (err: Error, res: any) => {
async (err: Error, res: any): Promise<any> => {
if (!err && !res.error && !this.clientSet()) {
this.client = 'geth';
resolve('geth');
}
resolve();
}
Expand All @@ -133,11 +136,11 @@ export default class Config implements IConfigParams {
resolve();
}
})
.then(async () => {
if (this.clientSet()) {
return;
.then(async (client?: string): Promise<any> => {
if (client) {
return Promise.resolve(client);
}
return new Promise(async (resolve, reject) => {
return new Promise(async (resolve, reject): Promise<any> => {
try {
const method = 'parity_pendingTransactions';
await this.web3.currentProvider.sendAsync(
Expand All @@ -149,7 +152,7 @@ export default class Config implements IConfigParams {
},
async (err: Error, res: any) => {
if (!err && !res.error && !this.clientSet()) {
this.client = 'parity';
resolve('parity');
}
resolve();
}
Expand All @@ -160,17 +163,11 @@ export default class Config implements IConfigParams {
}
});
})
.then(() => {
if (!this.clientSet()) {
this.client = 'unknown';
}
this.logger.debug(`Client: ${this.client.toUpperCase()}`);
return;
.then(async (client?: any) => {
client = client || 'unknown';
this.logger.debug(`Client: ${client.toUpperCase()}`);
return client;
})
.catch(() => {
this.client = 'none';
this.logger.error(`Client: ${this.client.toUpperCase()}`);
});
}

private _economicStrategyToBN(economicStrategy: IEconomicStrategy) {
Expand Down
2 changes: 1 addition & 1 deletion src/Config/IConfigParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ILogger } from '../Logger';

export interface IConfigParams {
autostart?: boolean;
client?: string;
client?: Promise<string>;
claiming?: boolean;
disableDetection?: boolean;
economicStrategy?: IEconomicStrategy;
Expand Down
6 changes: 4 additions & 2 deletions src/Scanner/Scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,10 @@ export default class {
}

public async start(): Promise<boolean> {
if (!this.config.clientSet()) {
await this.config.awaitClientSet();
if (!this.config.clientSet(await this.config.client)) {
if (!(await this.config.awaitClientSet())) {
return false;
}
}
if (!(await this.util.isWatchingEnabled())) {
throw new Error('We are currently supporting nodes with filtering capabilities');
Expand Down
21 changes: 13 additions & 8 deletions test/unit/UnitTestConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ describe('Config unit tests', () => {
assert.equal(config.ms, 4000);
assert.equal(config.scanSpread, 50);
assert.isFalse(config.walletStoresAsPrivateKeys);
assert.isUndefined(config.client);
expect(config.logger).to.exist;
assert.isNull(config.wallet);
assert.isFalse(config.clientSet());
assert.equal(
config.economicStrategy.maxDeposit.toNumber(),
Config.DEFAULT_ECONOMIC_STRATEGY.maxDeposit
Expand All @@ -42,16 +42,22 @@ describe('Config unit tests', () => {
);
});

it('Detect if config client is set', () => {
const config = new Config({ providerUrl });
expect(config.clientSet()).to.be.false;
it('Detects absence of web3', async () => {
const config = new Config({ providerUrl, disableDetection: true });
config.web3 = undefined;
expect(config.getConnectedClient()).to.throw;
})

it('Detect if config client is set', async () => {
const config = new Config({ providerUrl, disableDetection: true });
expect(config.clientSet(await this.client)).to.be.false;
});

it('Detect when config client is set', async () => {
const config = new Config({ providerUrl });
expect(config.clientSet()).to.be.false;
await config.awaitClientSet();
expect(config.clientSet()).to.be.true;
expect(config.clientSet(await config.client)).to.be.true;
});

it('Detects correct config client is set', async () => {
Expand Down Expand Up @@ -83,9 +89,9 @@ describe('Config unit tests', () => {
clients.map(async (client: string) => {
const config = new Config({ providerUrl, disableDetection: true });
config.web3 = Web3(client);
config.getConnectedClient();
config.client = config.getConnectedClient();
await config.awaitClientSet();
found.push(config.client);
found.push(await config.client);
})
);

Expand All @@ -108,7 +114,6 @@ describe('Config unit tests', () => {
ms: 10000,
scanSpread: 100,
walletStoresAsPrivateKeys: true,
client: 'parity',
logger: new DefaultLogger(),
walletStores: [PRIVATE_KEY]
});
Expand Down

0 comments on commit fbfbe92

Please sign in to comment.