Skip to content

Commit

Permalink
[pull] main from 0xPolygon:main (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
OKEAMAH committed Sep 26, 2024
2 parents 6074588 + b2be5f8 commit 5e652aa
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 161 deletions.
39 changes: 0 additions & 39 deletions .github/workflows/release.yml

This file was deleted.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -860,7 +860,7 @@ npm run build:link

## Support

Our [Discord](https://discord.gg/0xPolygonDevs) is the best way to reach us ✨.
Our [Discord](https://discord.com/invite/0xpolygonrnd) is the best way to reach us ✨.

## Contributing

Expand Down
69 changes: 50 additions & 19 deletions internal/block_getters/block_getter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ import { Logger } from "../logger/logger.js";
*
* @author - Vibhu Rajeev, Nitin Mittal
*/
export class BlockGetter extends BlockFormatter implements IBlockGetter {
export class BlockGetter extends BlockFormatter implements IBlockGetter {
/**
* @param {Eth} eth - Eth module from web3.js
* @param {number} maxRetries - The number of times to retry on errors.
*
* @constructor
*/
constructor(protected eth: Eth, protected maxRetries: number = 0) {
constructor(protected eth: Eth, protected maxRetries: number = 0) {
super();
}

Expand All @@ -46,25 +46,50 @@ export class BlockGetter extends BlockFormatter implements IBlockGetter {
*
* @throws {Error} - Throws error object on failure.
*/
public async getBlockWithTransactionReceipts(blockNumber: number | string): Promise<IBlock> {
const block: BlockTransactionObject = await this.eth.getBlock(blockNumber, true);
Logger.debug(`Fetching transaction receipts for the following block ${block.number}`);
public async getBlockWithTransactionReceipts(blockNumber: number | string, errorCount: number = 0): Promise<IBlock> {
try {
const block: BlockTransactionObject = await this.eth.getBlock(blockNumber, true);

if (!block) {
throw new BlockProducerError(
"Block producer error",
BlockProducerError.codes.RECEIPT_NOT_FOUND,
true,
`null receipt found for block ${blockNumber}.`,
"remote"
);
}

Logger.debug(`Fetching transaction receipts for the following block ${block.number}`);

const transactions: ITransaction[] = [];
const transactions: ITransaction[] = [];

for (const transactionObject of block.transactions) {
transactions.push(
this.formatTransactionObject(
transactionObject as IWeb3Transaction,
await this.getTransactionReceipt(transactionObject.hash)
)
for (const transactionObject of block.transactions) {
transactions.push(
this.formatTransactionObject(
transactionObject as IWeb3Transaction,
await this.getTransactionReceipt(transactionObject.hash)
)
);
}

return this.formatBlockWithTransactions(
block,
transactions
);
}
} catch (error) {
if (errorCount > this.maxRetries) {
Logger.info({
location: "block_getter",
function: "getBlockWithTransactionReceipts",
errorCount,
error: JSON.stringify(error)
});
throw error;
}

return this.formatBlockWithTransactions(
block,
transactions
);
return await this.getBlockWithTransactionReceipts(blockNumber, errorCount + 1);
}
}

/**
Expand Down Expand Up @@ -107,11 +132,17 @@ export class BlockGetter extends BlockFormatter implements IBlockGetter {

return this.formatTransactionReceipt(transactionReceipt);
} catch (error) {
if (errorCount >= this.maxRetries) {
if (errorCount > this.maxRetries) {
Logger.info({
location: "block_getter",
function: "getTransactionReceipt",
errorCount,
error: JSON.stringify(error)
});
throw error;
}

return this.getTransactionReceipt(transactionHash, errorCount + 1);
return await this.getTransactionReceipt(transactionHash, errorCount + 1);
}
}
}
117 changes: 63 additions & 54 deletions internal/logger/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,67 +7,72 @@ let logger: WinstonLogger | null = null;

/**
* LoggerClass that maintains a singleton, and has straightforward methods to log any application events.
*
*
* @author - Vibhu Rajeev, Keshav Gupta - Polygon Technology
*/
export class Logger {
/**
* @static
* Create method must first be called before using the logger. It creates a singleton, which will then
* be referred to throughout the application.
*
* Create method must first be called before using the logger. It creates a singleton, which will then
* be referred to throughout the application.
*
* @param {LoggerConfig} config - Logger configuration to overwrite winston configs and define sentry + datadog endpoints.
*/
static create(config: LoggerConfig) {
if (!logger) {
logger = winston.createLogger(Object.assign({
format: winston.format.combine(
winston.format.timestamp({ format: "YYYY-MM-DD HH:mm:ss:ms" }),
winston.format.colorize({
all: true,
colors: {
error: "red",
warn: "yellow",
info: "green",
debug: "white",
}
}),
winston.format.printf(
(info: any) => `${info.timestamp} ${info.level}: ${info.message}`,
),
),
transports: [
new winston.transports.Console({
level: config.console?.level || "info"
}),
new Sentry(
{
sentry: {
dsn: config.sentry?.dsn,
environment: config.sentry?.environment || "development"
},
level: config.sentry?.level || "error",
}
),
new winston.transports.Http(
{
host: "http-intake.logs.datadoghq.com",
path: "/api/v2/logs?dd-api-key=" + config.datadog?.api_key + "&ddsource=nodejs&service=" + config.datadog?.service_name,
ssl: true
}
),
]
},
config.winston
));
logger = winston.createLogger(
Object.assign(
{
format: winston.format.combine(
winston.format.timestamp({
format: "YYYY-MM-DD HH:mm:ss:ms",
}),
winston.format.colorize({
all: true,
colors: {
error: "red",
warn: "yellow",
info: "green",
debug: "white",
},
}),
winston.format.printf(
(info: any) =>
`${info.timestamp} ${info.level}: ${info.message}`
)
),
transports: [
new winston.transports.Console({
level: config.console?.level || "info",
}),
new Sentry({
sentry: {
dsn: config.sentry?.dsn,
},
level: config.sentry?.level || "error",
}),
new winston.transports.Http({
host: "http-intake.logs.datadoghq.com",
path:
"/api/v2/logs?dd-api-key=" +
config.datadog?.api_key +
"&ddsource=nodejs&service=" +
config.datadog?.service_name,
ssl: true,
}),
],
},
config.winston
)
);
}
}

/**
* @static
* Method to log for level - "info", this should not be called if it has been custom levels are
* Method to log for level - "info", this should not be called if it has been custom levels are
* set which does not include "info"
*
*
* @param {string|object} message - String or object to log.
*/
static info(message: string | object): void {
Expand All @@ -80,9 +85,9 @@ export class Logger {

/**
* @static
* Method to log for level - "debug", this should not be called if it has been custom levels are
* Method to log for level - "debug", this should not be called if it has been custom levels are
* set which does not include "debug"
*
*
* @param {string|object} message - String or object to log.
*/
static debug(message: string | object): void {
Expand All @@ -95,26 +100,30 @@ export class Logger {

/**
* @static
* Method to log for level - "error", this should not be called if it has been custom levels are
* Method to log for level - "error", this should not be called if it has been custom levels are
* set which does not include "error"
*
*
* @param {string|object} error - String or object to log.
*/
static error(error: string | object): void {
if (typeof error === "string") {
logger?.error(error);
} else {
logger?.error(
`${(error as Error).message ? `${(error as Error).message} : ` : ""}${JSON.stringify(error)}`
`${
(error as Error).message
? `${(error as Error).message} : `
: ""
}${JSON.stringify(error)}`
);
}
}

/**
* @static
* Method to log for level - "warn", this should not be called if it has been custom levels are
* Method to log for level - "warn", this should not be called if it has been custom levels are
* set which does not include "warn"
*
*
* @param {string|object} message - String or object to log.
*/
static warn(message: string | object): void {
Expand All @@ -128,7 +137,7 @@ export class Logger {
/**
* @static
* Method to log for any level, which should be used to log all custom levels that may be added.
*
*
* @param {string|object} message - String or object to log.
*/
static log(level: string, message: string | object): void {
Expand Down
25 changes: 18 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@maticnetwork/chain-indexer-framework",
"version": "1.3.9",
"version": "1.3.14",
"description": "blockchain data indexer",
"type": "module",
"exports": {
Expand Down Expand Up @@ -45,7 +45,7 @@
"homepage": "https://github.com/0xPolygon/chain-indexer-framework#readme",
"dependencies": {
"axios": "^1.3.6",
"ethereum-bloom-filters": "^1.0.10",
"ethereum-bloom-filters": "^1.2.0",
"long": "^5.2.0",
"mongoose": "^6.5.2",
"node-rdkafka": "^2.13.0",
Expand Down
Loading

0 comments on commit 5e652aa

Please sign in to comment.