Skip to content

Commit

Permalink
new: migrate common package
Browse files Browse the repository at this point in the history
  • Loading branch information
nitinmittal23 committed Aug 4, 2023
0 parents commit 8490b96
Show file tree
Hide file tree
Showing 215 changed files with 19,663 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* @maticnetwork/product-applications
30 changes: 30 additions & 0 deletions .github/pull_request_templates/PR_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
## Type of change

<!--- Please delete options that are not relevant. -->

- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)
## Bug/Feature Description
Clearly and concisely describe the problem.

## Root cause
Briefly describe the root cause and analysis of the problem.

## Solution Description
Describe your code changes in detail for reviewers. Explain the technical solution you have provided and how it fixes the issue case.

## Covered unit tests
Were unit test cases recorded for this fix or was only manual testing applicable?
[ ] I have extended, updated or written new tests for the related code changes/additions.

## Before requesting review
- [ ] Are github workflows showing build acceptance?
- [ ] I have selected the correct base branch.
- [ ] I have performed a self-review of my own code.
- [ ] I have commented my code, particularly in hard-to-understand areas.
- [ ] I have made corresponding changes to the documentation.
- [ ] My changes generate no new warnings.
- [ ] Any dependent changes have been merged and published in downstream modules.
- [ ] I have updated the ``CHANGELOG.md`` file in the root folder.
- [ ] I have tested my code locally in the development environemnt.
29 changes: 29 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Tests
run-name: ${{ github.actor }} just pushed to ChainFlow

on:
push:
branches:
- '**'

jobs:
Tests:
name: Tests
runs-on: ubuntu-latest
steps:
- name: CHECK-OUT GIT REPOSITORY
uses: actions/checkout@v3
- name: Use Node.js (v14)
uses: actions/setup-node@v3
with:
node-version: '14'
- name: Check ubuntu version
run: lsb_release -a
- name: Install dependencies
run: yarn
- name: Build Packages
run: yarn run build
- name: Run Unit Tests
run: yarn run tests
- name: Run Integration Tests
run: yarn tests:integration
19 changes: 19 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
node_modules/
build/
dist/
coverage/
.env
logs/
*.log
.DS_Store
*.rdb
.idea/
package-lock.json
.vscode
.env.local
*.key
*.cert
pids/

#packages/services/producers/ethereum/taskdef
#Dockerfile.ethereum-producer
16 changes: 16 additions & 0 deletions babel.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"presets": [
[
"@babel/preset-env",
{
"targets": {
"node": "current"
}
}
],
"@babel/preset-typescript"
],
"plugins": [
"babel-plugin-transform-import-meta"
]
}
117 changes: 117 additions & 0 deletions internal/block_getters/block_getter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import { Block, BlockTransactionObject, Eth, TransactionReceipt } from "web3-eth";
import { ITransactionReceipt } from "../interfaces/transaction_receipt.js";
import { BlockProducerError } from "../errors/block_producer_error.js";
import { IWeb3Transaction } from "../interfaces/web3_transaction.js";
import { BlockFormatter } from "../formatters/block_formatter.js";
import { ITransaction } from "../interfaces/transaction.js";
import { IBlock } from "../interfaces/block.js";
import { IBlockGetter } from "../interfaces/block_getter.js";
import { Logger } from "../logger/logger.js";

/**
* A wrapper class on web3 block related functions
*
* @author - Vibhu Rajeev, Nitin Mittal
*/
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) {
super();
}

/**
* @async
* Public method to query block data of a single block
*
* @param {number | string} blockNumber - Block number to query the block details for.
*
* @returns {Promise<Block>} - Block object
*/
public async getBlock(blockNumber: number | string): Promise<Block> {
return await this.eth.getBlock(blockNumber);
}

/**
* @async
* Public method to query block data including transaction receipts of a single block.
*
* @param {number | string} blockNumber - The block number for which block data needs to be retrieved.
*
* @returns {Promise<IBlock>} - Block object containing all details including transaction receipts.
*
* @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}`);

const transactions: ITransaction[] = [];

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

return this.formatBlockWithTransactions(
block as BlockTransactionObject,
transactions
);
}

/**
* @async
* Public method to query the current block number.
*
* @returns {Promise<number>} - the current block.
*
* @throws {Error} - Throws error object on failure.
*/
public getLatestBlockNumber(): Promise<number> {
return this.eth.getBlockNumber();
}

/**
* @async
* This internal method retrieves the transaction receipt of the given transaction hash and retries upto retryLimit on failure.
*
* @param {string} transactionHash - The transaction hash for which transaction receipt is to be retrieved.
* @param {number} errorCount - Parameter for the function to know the number of times query has been retried.
* This parameter must ideally not be set by an external call.
*
* @returns {Promise<ITransactionReceipt>} - The transaction receipt of the given transaction hash. On failure throws error object.
*
* @throws {Error} - Throws error object on failure.
*/
protected async getTransactionReceipt(transactionHash: string, errorCount: number = 0): Promise<ITransactionReceipt> {
try {
const transactionReceipt: TransactionReceipt = await this.eth.getTransactionReceipt(transactionHash);

if (transactionReceipt === null) {
throw new BlockProducerError(
"Block producer error",
BlockProducerError.codes.RECEIPT_NOT_FOUND,
false,
`Transaction receipt not found for ${transactionHash}.`,
"remote"
);
}

return this.formatTransactionReceipt(transactionReceipt);
} catch (error) {
if (errorCount >= this.maxRetries) {
throw error;
}

return this.getTransactionReceipt(transactionHash, errorCount + 1);
}
}
}
51 changes: 51 additions & 0 deletions internal/block_getters/block_getter_worker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { IBlockWorkerMessage } from "../interfaces/block_worker_message.js";
import { parentPort, workerData } from "worker_threads";
import { BlockGetter } from "./block_getter.js";
import EthClass from "web3-eth";

if (!workerData || !parentPort) {
process.exit(1);
}

const blockGetter = new BlockGetter(
//@ts-ignore
new EthClass(
//@ts-ignore
new EthClass.providers.WebsocketProvider(
workerData.endpoint,
{
reconnect: {
auto: true
},
clientConfig: {
maxReceivedFrameSize: 1000000000,
maxReceivedMessageSize: 1000000000,
},
timeout: 45000
}
)
),
workerData.maxRetries
);

parentPort.on("message", async (message: {
blockNumber: number,
callBackId: number
}) => {
try {
parentPort?.postMessage(
{
callBackId: message.callBackId,
error: null,
block: await blockGetter.getBlockWithTransactionReceipts(message.blockNumber)
} as IBlockWorkerMessage
);
} catch (error) {
parentPort?.postMessage(
{
callBackId: message.callBackId,
error: error
} as IBlockWorkerMessage
);
}
});
Loading

0 comments on commit 8490b96

Please sign in to comment.