Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pk910 committed May 29, 2023
1 parent 4b09a38 commit cce6203
Show file tree
Hide file tree
Showing 9 changed files with 688 additions and 108 deletions.
2 changes: 1 addition & 1 deletion src/services/EnsResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class EnsResolver {

private initEns() {
if(faucetConfig.ensResolver) {
let provider = new Web3.providers.HttpProvider(faucetConfig.ensResolver.rpcHost);
let provider = typeof faucetConfig.ensResolver.rpcHost == "object" ? faucetConfig.ensResolver.rpcHost : new Web3.providers.HttpProvider(faucetConfig.ensResolver.rpcHost);
this.ens = new ENS(provider, faucetConfig.ensResolver.ensAddr || undefined, Web3);
}
else {
Expand Down
2 changes: 1 addition & 1 deletion src/services/EthClaimManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ export class EthClaimManager {
return null;
}

private async processQueue() {
public async processQueue() {
if(this.queueProcessing)
return;
this.queueProcessing = true;
Expand Down
7 changes: 5 additions & 2 deletions src/services/EthWalletManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export class EthWalletManager {
private walletState: WalletState;
private tokenState: FaucetTokenState;
private lastWalletRefresh: number;
private txReceiptPollInterval = 30000;

public initialize() {
if(this.initialized)
Expand Down Expand Up @@ -97,7 +98,9 @@ export class EthWalletManager {

private startWeb3() {
let provider: any;
if(faucetConfig.ethRpcHost.match(/^wss?:\/\//))
if(faucetConfig.ethRpcHost && typeof faucetConfig.ethRpcHost === "object")
provider = faucetConfig.ethRpcHost as any;
else if(faucetConfig.ethRpcHost.match(/^wss?:\/\//))
provider = new Web3.providers.WebsocketProvider(faucetConfig.ethRpcHost);
else if(faucetConfig.ethRpcHost.match(/^\//))
provider = new Web3.providers.IpcProvider(faucetConfig.ethRpcHost, net);
Expand Down Expand Up @@ -447,7 +450,7 @@ export class EthWalletManager {
try {
let receipt: TransactionReceipt;
do {
await sleepPromise(30000); // 30 secs
await sleepPromise(this.txReceiptPollInterval); // 30 secs
receipt = await this.web3.eth.getTransactionReceipt(txhash);
ServiceManager.GetService(FaucetProcess).emitLog(FaucetLogLevel.WARNING, "Polled transaction receipt for " + txhash + ": " + (receipt ? "found!" : "pending"));
} while(!receipt);
Expand Down
4 changes: 2 additions & 2 deletions src/services/PoWRewardLimiter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ export class PoWRewardLimiter {
private balanceRestriction: number;
private balanceRestrictionsRefresh: number;

private refreshIpInfoMatchRestrictions() {
public refreshIpInfoMatchRestrictions(force?: boolean) {
let now = Math.floor((new Date()).getTime() / 1000);
let refresh = faucetConfig.ipInfoMatchRestrictedRewardFile ? faucetConfig.ipInfoMatchRestrictedRewardFile.refresh : 30;
if(this.ipInfoMatchRestrictionsRefresh > now - refresh)
if(this.ipInfoMatchRestrictionsRefresh > now - refresh && !force)
return;

this.ipInfoMatchRestrictionsRefresh = now;
Expand Down
73 changes: 73 additions & 0 deletions tests/EnsResolver.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import 'mocha';
import sinon from 'sinon';
import { expect } from 'chai';
import { unbindTestStubs, FakeProvider } from './common';
import { faucetConfig, loadFaucetConfig } from '../src/common/FaucetConfig';
import { FaucetProcess } from '../src/common/FaucetProcess';
import { EnsResolver } from '../src/services/EnsResolver';
import { ServiceManager } from '../src/common/ServiceManager';

describe("ENS Resolver", () => {
let globalStubs;
let fakeProvider;

beforeEach(() => {
globalStubs = {
"FaucetProcess.emitLog": sinon.stub(FaucetProcess.prototype, "emitLog"),
};
fakeProvider = new FakeProvider();
loadFaucetConfig(true);
faucetConfig.faucetStats = null;
faucetConfig.ethRpcHost = fakeProvider;
});
afterEach(() => {
return unbindTestStubs();
});

it("check ens resolver", async () => {
faucetConfig.ensResolver = {
rpcHost: fakeProvider,
ensAddr: null,
};
fakeProvider.injectResponse("net_version", "5");
fakeProvider.injectResponse("eth_call", "0x0000000000000000000000004b1488b7a6b320d2d721406204abc3eeaa9ad329");
let ensResolver = new EnsResolver();
ensResolver.initialize();
let resolveResult = await ensResolver.resolveEnsName("test.eth");
expect(resolveResult).equal("0x4B1488B7a6B320d2D721406204aBc3eeAa9AD329", "unexpected address");
});
it("check ens resolver (disabled)", async () => {
faucetConfig.ensResolver = null;
let ensResolver = new EnsResolver();
ensResolver.initialize();
let exception;
try {
await ensResolver.resolveEnsName("test.eth");
} catch(ex) {
exception = ex;
}
expect(exception).equal("ENS resolver not enabled", "unexpected error");
});
it("check ens resolver (dynamic enable/disable)", async () => {
faucetConfig.ensResolver = null;
let ensResolver = new EnsResolver();
ensResolver.initialize();
let exception;
try {
await ensResolver.resolveEnsName("test.eth");
} catch(ex) {
exception = ex;
}
expect(exception).equal("ENS resolver not enabled", "unexpected error");
faucetConfig.ensResolver = {
rpcHost: fakeProvider,
ensAddr: null,
};
fakeProvider.injectResponse("net_version", "5");
fakeProvider.injectResponse("eth_call", "0x0000000000000000000000004b1488b7a6b320d2d721406204abc3eeaa9ad329");
ServiceManager.GetService(FaucetProcess).emit("reload");
let resolveResult = await ensResolver.resolveEnsName("test.eth");
expect(resolveResult).equal("0x4B1488B7a6B320d2D721406204aBc3eeAa9AD329", "unexpected address");
});

});
Loading

0 comments on commit cce6203

Please sign in to comment.