Skip to content

Commit

Permalink
chore(verifier calls): split the function into accepted contracts and…
Browse files Browse the repository at this point in the history
… token
  • Loading branch information
antomor committed Feb 15, 2024
1 parent efe76b9 commit aaca9a2
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 67 deletions.
13 changes: 6 additions & 7 deletions src/RelayServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@ import {
convertGasToTokenAndNative,
calculateFee,
validateExpirationTime,
callVerifierMethod,
queryVerifiers,
getAcceptedTokensFromVerifier,
getAcceptedContractsFromVerifier,
} from './relayServerUtils';
import { getPastEventsForHub } from './getPastEventsForHub';
import type { PastEventOptions } from './definitions';
Expand Down Expand Up @@ -215,9 +216,8 @@ export class RelayServer extends EventEmitter {

const res: HandlerResponse = {};
for (const verifier of verifiers) {
res[utils.getAddress(verifier)] = await callVerifierMethod(
verifier,
'Token'
res[utils.getAddress(verifier)] = await getAcceptedTokensFromVerifier(
verifier
);
}

Expand All @@ -231,9 +231,8 @@ export class RelayServer extends EventEmitter {

const res: HandlerResponse = {};
for (const verifier of verifiers) {
res[utils.getAddress(verifier)] = await callVerifierMethod(
verifier,
'Contract'
res[utils.getAddress(verifier)] = await getAcceptedContractsFromVerifier(
verifier
);
}

Expand Down
47 changes: 29 additions & 18 deletions src/relayServerUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,28 +396,38 @@ async function validateExpirationTime(
}
}

type VerifierQueries = 'Token' | 'Contract';
async function getAcceptedContractsFromVerifier(
verifier: string
): Promise<string[]> {
try {
const provider = getProvider();

const handler = DestinationContractHandler__factory.connect(
verifier,
provider
);

return await handler.getAcceptedContracts();
} catch (error) {
log.warn(
`Couldn't get accepted contracts from verifier ${verifier}`,

Check warning

Code scanning / CodeQL

Log injection Medium

Log entry depends on a
user-provided value
.
error
);
}

async function callVerifierMethod(
verifier: string,
type: VerifierQueries
return [];
}

async function getAcceptedTokensFromVerifier(
verifier: string
): Promise<string[]> {
try {
const provider = getProvider();
if (type === 'Token') {
const handler = TokenHandler__factory.connect(verifier, provider);

return await handler.getAcceptedTokens();
} else {
const handler = DestinationContractHandler__factory.connect(
verifier,
provider
);

return await handler.getAcceptedContracts();
}
const handler = TokenHandler__factory.connect(verifier, provider);

return await handler.getAcceptedTokens();
} catch (error) {
log.warn(`Verifier ${verifier} failed while query ${type} `, error);
log.warn(`Couldn't get accepted tokens from verifier ${verifier}`, error);

Check warning

Code scanning / CodeQL

Log injection Medium

Log entry depends on a
user-provided value
.
}

return [];
Expand Down Expand Up @@ -446,6 +456,7 @@ export {
TRANSFER_HASH,
TRANSFER_FROM_HASH,
validateExpirationTime,
callVerifierMethod,
queryVerifiers,
getAcceptedContractsFromVerifier,
getAcceptedTokensFromVerifier,
};
15 changes: 6 additions & 9 deletions test/unit/RelayServer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ describe('RelayServer tests', function () {
queryVerifiersSpy = sinon.spy(relayServerUtils, 'queryVerifiers');
callVerifierMehodStub = sinon.stub(
relayServerUtils,
'callVerifierMethod'
'getAcceptedTokensFromVerifier'
);
callVerifierMehodStub.returns(addressArray);
trustedVerifiers = new Set<string>();
Expand Down Expand Up @@ -379,7 +379,7 @@ describe('RelayServer tests', function () {
expect(result).to.be.deep.equal(expectedResult);
expect(queryVerifiersSpy).to.be.calledOnce;
for (const v of trustedVerifiers) {
expect(callVerifierMehodStub).to.be.calledWithExactly(v, 'Token');
expect(callVerifierMehodStub).to.be.calledWithExactly(v);
}
});

Expand All @@ -391,7 +391,7 @@ describe('RelayServer tests', function () {

expect(result).to.be.deep.equal(expectedResult);
expect(queryVerifiersSpy).to.be.calledOnce;
expect(callVerifierMehodStub).to.be.calledWithExactly(verifier, 'Token');
expect(callVerifierMehodStub).to.be.calledWithExactly(verifier);
});
});

Expand All @@ -406,7 +406,7 @@ describe('RelayServer tests', function () {
queryVerifiersSpy = sinon.spy(relayServerUtils, 'queryVerifiers');
callVerifierMehodStub = sinon.stub(
relayServerUtils,
'callVerifierMethod'
'getAcceptedContractsFromVerifier'
);
callVerifierMehodStub.returns(addressArray);
trustedVerifiers = new Set<string>();
Expand Down Expand Up @@ -435,7 +435,7 @@ describe('RelayServer tests', function () {
expect(result).to.be.deep.equal(expectedResult);
expect(queryVerifiersSpy).to.be.calledOnce;
for (const v of trustedVerifiers) {
expect(callVerifierMehodStub).to.be.calledWithExactly(v, 'Contract');
expect(callVerifierMehodStub).to.be.calledWithExactly(v);
}
});

Expand All @@ -447,10 +447,7 @@ describe('RelayServer tests', function () {

expect(result).to.be.deep.equal(expectedResult);
expect(queryVerifiersSpy).to.be.calledOnce;
expect(callVerifierMehodStub).to.be.calledWithExactly(
verifier,
'Contract'
);
expect(callVerifierMehodStub).to.be.calledWithExactly(verifier);
});
});
});
64 changes: 31 additions & 33 deletions test/unit/relayServerUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ describe('relayServerUtils tests', function () {
});
});

describe('Function callVerifierMethod()', function () {
describe('', function () {
const addressArray = ['0x145845fd06c85B7EA1AA2d030E1a747B3d8d15D7'];
const verifier = '0x155845fd06c85B7EA1AA2d030E1a747B3d8d15D7';
let getAcceptedTokens: SinonStub;
Expand All @@ -642,46 +642,44 @@ describe('relayServerUtils tests', function () {
.returns(contractHandler);
});

it('should return accepted tokens', async function () {
const tokens = await relayServerUtils.callVerifierMethod(
verifier,
'Token'
);
describe('getAcceptedTokensFromVerifier', function () {
it('should return accepted tokens', async function () {
const tokens = await relayServerUtils.getAcceptedTokensFromVerifier(
verifier
);

expect(getAcceptedTokens).to.be.calledOnce;
expect(tokens).to.be.equal(addressArray);
});
expect(getAcceptedTokens).to.be.calledOnce;
expect(tokens).to.be.equal(addressArray);
});

it('should return accepted contracts', async function () {
const contracts = await relayServerUtils.callVerifierMethod(
verifier,
'Contract'
);
it('should return empty if it fails while retrieving accepted tokens', async function () {
getAcceptedTokens.throws();
const tokens = await relayServerUtils.getAcceptedTokensFromVerifier(
verifier
);

expect(getAcceptedContracts).to.be.calledOnce;
expect(contracts).to.be.equal(addressArray);
expect(getAcceptedTokens).to.be.calledOnce;
expect(tokens).to.be.empty;
});
});

it('should return empty if it fails while retrieving accepted tokens', async function () {
getAcceptedTokens.throws();
const tokens = await relayServerUtils.callVerifierMethod(
verifier,
'Token'
);
describe('getAcceptedContractsFromVerifier', function () {
it('should return accepted contracts', async function () {
const contracts =
await relayServerUtils.getAcceptedContractsFromVerifier(verifier);

expect(getAcceptedTokens).to.be.calledOnce;
expect(tokens).to.be.empty;
});
expect(getAcceptedContracts).to.be.calledOnce;
expect(contracts).to.be.equal(addressArray);
});

it('should return empty if it fails while retrieving accepted contracts', async function () {
getAcceptedContracts.throws();
const contracts = await relayServerUtils.callVerifierMethod(
verifier,
'Contract'
);
it('should return empty if it fails while retrieving accepted contracts', async function () {
getAcceptedContracts.throws();
const contracts =
await relayServerUtils.getAcceptedContractsFromVerifier(verifier);

expect(getAcceptedContracts).to.be.calledOnce;
expect(contracts).to.be.empty;
expect(getAcceptedContracts).to.be.calledOnce;
expect(contracts).to.be.empty;
});
});
});

Expand Down

0 comments on commit aaca9a2

Please sign in to comment.