Skip to content

Commit

Permalink
feat: logging
Browse files Browse the repository at this point in the history
  • Loading branch information
hai-ko committed Aug 3, 2023
1 parent 9c04ac5 commit e57a5ee
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 11 deletions.
8 changes: 6 additions & 2 deletions gateway/handler/signing/signAndEncodeResponse.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ethers, Signer } from "ethers";
import { Logger } from "winston";

/**
* @param signer A signer to sign the request. The address of the signer HAS to be part of
Expand All @@ -14,10 +15,12 @@ export async function signAndEncodeResponse(
resolverAddr: string,
result: string,
calldata: string,
ttl: number = 30000
logger: Logger,
ttl: number = 30000,

): Promise<string> {
const validUntil = Math.floor(Date.now() / 1000 + ttl);

logger.debug({message: 'signAndEncodeResponse', signer, resolverAddr, result, calldata, validUntil})
/**
* This hash has to be compiled the same way as at the OffchainResolver.makeSignatureHash method
* since it'll be compared within the {@see resolveWithProof} function
Expand All @@ -34,5 +37,6 @@ export async function signAndEncodeResponse(
*/
const sig = await signer.signMessage(msgHashDigest);

logger.debug({message: 'signAndEncodeResponse result', result, validUntil, sig})
return ethers.utils.defaultAbiCoder.encode(["bytes", "uint64", "bytes"], [result, validUntil, sig]);
}
6 changes: 4 additions & 2 deletions gateway/handler/signing/signingHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ethers } from "ethers";
import { SigningConfigEntry } from "../../config/Config";

import { signAndEncodeResponse } from "./signAndEncodeResponse";
import { Logger } from "winston";

/**
* Signs the provided calldata using the resolver address and returns the signed and encoded response.
Expand All @@ -12,7 +13,7 @@ import { signAndEncodeResponse } from "./signAndEncodeResponse";
* @param configEntry - The signing configuration entry.
* @returns The signed and encoded response.
*/
export async function signingHandler(calldata: string, resolverAddr: string, configEntry: SigningConfigEntry) {
export async function signingHandler(calldata: string, resolverAddr: string, configEntry: SigningConfigEntry, logger: Logger) {
/**
* Fetches the result from the data source.
*/
Expand All @@ -36,5 +37,6 @@ export async function signingHandler(calldata: string, resolverAddr: string, con
* Sign and encode the response the signingHandler has returned using the private key from the environment variable.
*/
const signer = new ethers.Wallet(singerPk);
return signAndEncodeResponse(signer, resolverAddr, result, calldata);
logger.info({message: 'signingHandler', signer: signer.address});
return signAndEncodeResponse(signer, resolverAddr, result, calldata, logger);
}
15 changes: 10 additions & 5 deletions gateway/http/ccipGateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ import express from "express";
import { ConfigReader } from "../config/ConfigReader";
import { optimismBedrockHandler } from "../handler/optimism-bedrock/optimismBedrockHandler";
import { signingHandler } from "../handler/signing/signingHandler";
import { Logger } from "winston";

/**
* Creates an Express router to handle requests for the CCIP gateway.
* @param configReader - The configuration reader that provides the necessary config entries.
* @returns The Express router for the CCIP gateway.
*/
export function ccipGateway(configReader: ConfigReader) {
export function ccipGateway(configReader: ConfigReader, logger: Logger) {
const router = express.Router();

router.get("/:resolverAddr/:calldata", async (req: express.Request, res: express.Response) => {
Expand All @@ -32,7 +33,7 @@ export function ccipGateway(configReader: ConfigReader) {
/**
* If there is no config entry for the resolverAddr, we return a 404. As there is no way for the gateway to resolve the request
*/
console.log(`Unknown resolver selector pair for resolverAddr: ${resolverAddr}`);
logger.warn(`Unknown resolver selector pair for resolverAddr: ${resolverAddr}`);

res.status(404).send({
message: "Unknown resolver selector pair",
Expand All @@ -45,13 +46,17 @@ export function ccipGateway(configReader: ConfigReader) {
*/
switch (configEntry.type) {
case "signing": {
const response = await signingHandler(calldata, resolverAddr, configEntry);
logger.info({type: 'signing'})
logger.debug({type: 'signing', calldata, resolverAddr, configEntry})
const response = await signingHandler(calldata, resolverAddr, configEntry, logger);
res.status(200).send({ data: response });
break;
}
case "optimism-bedrock": {
logger.info({type: 'optimism-bedrock'})
logger.debug({type: 'optimism-bedrock', calldata, resolverAddr, configEntry})
const response = await optimismBedrockHandler(calldata, resolverAddr, configEntry);
// console.log(response);

res.status(200).send({ data: response });
break;
}
Expand All @@ -62,7 +67,7 @@ export function ccipGateway(configReader: ConfigReader) {
});
}
} catch (e) {
req.app.locals.logger.warn((e as Error).message);
logger.warn((e as Error).message);
res.status(400).send({ message: "ccip gateway error ," + e });
}
});
Expand Down
2 changes: 1 addition & 1 deletion gateway/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ app.use(bodyParser.json());
});

const config = getConfigReader(process.env.CONFIG);
app.use("/", ccipGateway(config));
app.use("/", ccipGateway(config, app.locals.logger));
})();
const port = process.env.PORT || "8081";
server.listen(port, () => {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ccip-resolver",
"version": "0.1.3",
"version": "0.1.4",
"description": "",
"types": "dist/index.d.ts",
"scripts": {
Expand Down

0 comments on commit e57a5ee

Please sign in to comment.