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 40bba82
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 12 deletions.
3 changes: 1 addition & 2 deletions deploy/01_BedrockProofVerifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ async function main() {
const l2OutputOracleAddress = chainId === "1" ? L2_OUTPUT_ORALCE_MAINNET : L2_OUTPUT_ORALCE_GOERLI;

const BedrockProofVerifierFactory = await ethers.getContractFactory("BedrockProofVerifier");
const deployTx = await BedrockProofVerifierFactory.deploy(l2OutputOracleAddress,{
const deployTx = await BedrockProofVerifierFactory.deploy(l2OutputOracleAddress, {
gasLimit: 5000000,

});

await deployTx.deployed();
Expand Down
3 changes: 2 additions & 1 deletion gateway/handler/signing/signAndEncodeResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export async function signAndEncodeResponse(
ttl: number = 30000
): Promise<string> {
const validUntil = Math.floor(Date.now() / 1000 + ttl);

global.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 +34,6 @@ export async function signAndEncodeResponse(
*/
const sig = await signer.signMessage(msgHashDigest);

global.logger.debug({ message: "signAndEncodeResponse result", result, validUntil, sig });
return ethers.utils.defaultAbiCoder.encode(["bytes", "uint64", "bytes"], [result, validUntil, sig]);
}
2 changes: 2 additions & 0 deletions gateway/handler/signing/signingHandler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import axios from "axios";
import { ethers } from "ethers";
import { Logger } from "winston";

import { SigningConfigEntry } from "../../config/Config";

Expand Down Expand Up @@ -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);
global.logger.info({ message: "signingHandler", signer: signer.address });
return signAndEncodeResponse(signer, resolverAddr, result, calldata);
}
11 changes: 8 additions & 3 deletions gateway/http/ccipGateway.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import express from "express";
import { Logger } from "winston";

import { ConfigReader } from "../config/ConfigReader";
import { optimismBedrockHandler } from "../handler/optimism-bedrock/optimismBedrockHandler";
Expand Down Expand Up @@ -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}`);
global.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": {
global.logger.info({ type: "signing" });
global.logger.debug({ type: "signing", calldata, resolverAddr, configEntry });
const response = await signingHandler(calldata, resolverAddr, configEntry);
res.status(200).send({ data: response });
break;
}
case "optimism-bedrock": {
global.logger.info({ type: "optimism-bedrock" });
global.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);
global.logger.warn((e as Error).message);
res.status(400).send({ message: "ccip gateway error ," + e });
}
});
Expand Down
15 changes: 10 additions & 5 deletions gateway/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ import { getConfigReader } from "./config/ConfigReader";

dotenv.config();

declare global {
var logger: winston.Logger
}

global.logger = winston.createLogger({
level: process.env.LOG_LEVEL ?? 'info',
transports: [new winston.transports.Console()],
});

const app = express();
app.use(express.json({ limit: "50mb" }));
app.use(express.urlencoded({ limit: "50mb" }));
Expand All @@ -20,14 +29,10 @@ app.use(cors());
app.use(bodyParser.json());

(async () => {
app.locals.logger = winston.createLogger({
transports: [new winston.transports.Console()],
});

const config = getConfigReader(process.env.CONFIG);
app.use("/", ccipGateway(config));
})();
const port = process.env.PORT || "8081";
server.listen(port, () => {
app.locals.logger.info("[Server] listening at port " + port + " and dir " + __dirname);
global.logger.info("[Server] listening at port " + port + " and dir " + __dirname);
});
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
6 changes: 6 additions & 0 deletions test/contracts/SignatureCcipVerifier.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import exp from "constants";
import { BigNumber, ethers } from "ethers";
import { dnsEncode } from "ethers/lib/utils";
import { ethers as hreEthers } from "hardhat";
import winston from "winston";

import { signAndEncodeResponse } from "../../gateway/handler/signing/signAndEncodeResponse";
import { expect } from "../../test/chai-setup";
Expand All @@ -16,6 +17,11 @@ describe("Signature Ccip Verifier", () => {
let alice: SignerWithAddress;
let resolver: SignerWithAddress;

global.logger = winston.createLogger({
level: process.env.LOG_LEVEL ?? "info",
transports: [new winston.transports.Console()],
});

beforeEach(async () => {
// Get signers
[owner, signer1, signer2, rando, alice, resolver] = await hreEthers.getSigners();
Expand Down

0 comments on commit 40bba82

Please sign in to comment.