-
Notifications
You must be signed in to change notification settings - Fork 305
Entropy tester #2762
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Entropy tester #2762
Changes from 5 commits
f424e89
97e9fcc
91d8de3
b3a9394
e229136
3adf992
d478132
0d7f6fa
cd2b56e
7b805e8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| module.exports = { | ||
| root: true, | ||
| parser: "@typescript-eslint/parser", | ||
| plugins: ["@typescript-eslint"], | ||
| extends: ["eslint:recommended", "plugin:@typescript-eslint/recommended"], | ||
| rules: { | ||
| "@typescript-eslint/no-explicit-any": "off", | ||
| "@typescript-eslint/no-non-null-assertion": "off", | ||
| }, | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| lib |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| - chain-id: berachain_mainnet | ||
| interval: 3h | ||
| - chain-id: apechain_mainnet | ||
| interval: 6h | ||
| - chain-id: blast | ||
| interval: 10m |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| { | ||
| "name": "@pythnetwork/entropy-tester", | ||
| "version": "1.0.0", | ||
| "description": "Utility to test entropy provider callbacks", | ||
| "main": "lib/index.js", | ||
| "types": "lib/index.d.ts", | ||
|
||
| "files": [ | ||
| "lib/**/*" | ||
| ], | ||
| "scripts": { | ||
| "build": "tsc", | ||
| "fix:format": "prettier --write \"src/**/*.ts\"", | ||
| "fix:lint": "eslint src/ --fix --max-warnings 0", | ||
| "test:format": "prettier --check \"src/**/*.ts\"", | ||
| "test:lint": "eslint src/ --max-warnings 0", | ||
| "start": "node lib/index.js", | ||
| "dev": "ts-node src/index.ts", | ||
| "prepublishOnly": "pnpm run build && pnpm run test:lint", | ||
| "preversion": "pnpm run test:lint", | ||
| "version": "pnpm run test:format && pnpm run test:lint && git add -A src" | ||
| }, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suggest modeling your scripts after another app, e.g. https://github.com/pyth-network/pyth-crosschain/blob/main/apps/entropy-explorer/package.json#L9-L21 -- many of the scripts here don't make any sense for a package that isn't published like this and you're missing things like having prettier format all valid file types |
||
| "repository": { | ||
| "type": "git", | ||
| "url": "git+https://github.com/pyth-network/pyth-crosschain.git", | ||
| "directory": "apps/entropy-tester" | ||
| }, | ||
| "bin": { | ||
| "pyth-entropy-tester": "./lib/index.js" | ||
| }, | ||
| "devDependencies": { | ||
| "@types/ethereum-protocol": "^1.0.2", | ||
| "@types/express": "^4.17.21", | ||
| "@types/jest": "^27.4.1", | ||
| "@types/yargs": "^17.0.10", | ||
| "@typescript-eslint/eslint-plugin": "^6.0.0", | ||
| "@typescript-eslint/parser": "^6.0.0", | ||
| "eslint": "^8.13.0", | ||
| "jest": "^29.7.0", | ||
|
||
| "pino-pretty": "^11.2.1", | ||
| "prettier": "catalog:", | ||
| "ts-jest": "^29.1.1", | ||
| "ts-node": "catalog:", | ||
| "typescript": "catalog:" | ||
| }, | ||
| "dependencies": { | ||
| "@pythnetwork/contract-manager": "workspace:*", | ||
| "joi": "^17.6.0", | ||
| "pino": "^9.2.0", | ||
| "prom-client": "^15.1.0", | ||
| "viem": "^2.19.4", | ||
| "yaml": "^2.1.1", | ||
| "yargs": "^17.5.1" | ||
| }, | ||
| "keywords": [], | ||
| "author": "", | ||
| "license": "Apache-2.0", | ||
| "packageManager": "[email protected]" | ||
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,168 @@ | ||
| #!/usr/bin/env node | ||
|
||
| import yargs from "yargs"; | ||
| import { hideBin } from "yargs/helpers"; | ||
| import YAML from "yaml"; | ||
| import fs from "fs"; | ||
| import pino, { Logger } from "pino"; | ||
| import { DefaultStore } from "@pythnetwork/contract-manager/node/store"; | ||
| import { EvmEntropyContract } from "@pythnetwork/contract-manager/core/contracts/evm"; | ||
| import { | ||
| PrivateKey, | ||
| toPrivateKey, | ||
| } from "@pythnetwork/contract-manager/core/base"; | ||
|
|
||
| type DurationSeconds = number; | ||
|
||
| type LoadedConfig = { | ||
| contract: EvmEntropyContract; | ||
| interval: DurationSeconds; | ||
| }; | ||
|
|
||
| function timeToSeconds(timeStr: string): number { | ||
| const match = timeStr.match(/^(\d+)([hms])$/i); | ||
| if (!match) | ||
| throw new Error("Invalid format. Use formats like '6h', '15m', or '30s'."); | ||
|
|
||
| const value = parseInt(match[1], 10); | ||
| const unit = match[2].toLowerCase(); | ||
|
|
||
| switch (unit) { | ||
| case "h": | ||
| return value * 3600; | ||
| case "m": | ||
| return value * 60; | ||
| case "s": | ||
| return value; | ||
| default: | ||
| throw new Error("Unsupported time unit."); | ||
| } | ||
| } | ||
|
|
||
| function loadConfig(configPath: string): LoadedConfig[] { | ||
| const configs = YAML.parse(fs.readFileSync(configPath, "utf-8")); | ||
|
||
| const loadedConfigs = []; | ||
| for (const config of configs) { | ||
| const interval = timeToSeconds(config["interval"]); | ||
| const contracts = Object.values(DefaultStore.entropy_contracts).filter( | ||
| (contract) => contract.chain.getId() == config["chain-id"], | ||
| ); | ||
| if (contracts.length === 0) { | ||
| throw new Error( | ||
| `Can not find the contract for chain ${config["chain-id"]}, check contract manager store.`, | ||
| ); | ||
| } | ||
| if (contracts.length > 1) { | ||
| throw new Error( | ||
| `Multiple contracts found for chain ${config["chain-id"]}, check contract manager store.`, | ||
| ); | ||
| } | ||
| loadedConfigs.push({ contract: contracts[0], interval }); | ||
|
||
| } | ||
| return loadedConfigs; | ||
| } | ||
|
|
||
| async function testLatency( | ||
| contract: EvmEntropyContract, | ||
| privateKey: PrivateKey, | ||
| logger: Logger, | ||
| ) { | ||
| const provider = await contract.getDefaultProvider(); | ||
| const userRandomNumber = contract.generateUserRandomNumber(); | ||
| const requestResponse = await contract.requestRandomness( | ||
| userRandomNumber, | ||
| provider, | ||
| privateKey, | ||
| true, // with callback | ||
| ); | ||
| // Read the sequence number for the request from the transaction events. | ||
| const sequenceNumber = parseInt( | ||
| requestResponse.events.RequestedWithCallback.returnValues.sequenceNumber, | ||
| ); | ||
| logger.info( | ||
| { sequenceNumber, txHash: requestResponse.transactionHash }, | ||
| `Request submitted`, | ||
| ); | ||
|
|
||
| const startTime = Date.now(); | ||
|
|
||
| // eslint-disable-next-line no-constant-condition | ||
| while (true) { | ||
| await new Promise((resolve) => setTimeout(resolve, 2000)); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't do this, but for your entertainment, if you want some very esoteric node facts, you can actually do this: import { promisify } from "util";
await promisify(setTimeout)(2000);You wouldn't expect it to work due to the argument order of |
||
| const request = await contract.getRequest(provider, sequenceNumber); | ||
| logger.debug(request); | ||
|
|
||
| if (parseInt(request.sequenceNumber) === 0) { | ||
| // 0 means the request is cleared | ||
| const endTime = Date.now(); | ||
| logger.info( | ||
| { sequenceNumber, latency: endTime - startTime }, | ||
| `Successful callback`, | ||
| ); | ||
| break; | ||
| } | ||
| if (Date.now() - startTime > 60000) { | ||
| logger.error( | ||
| { sequenceNumber }, | ||
| "Timeout: 60s passed without the callback being called", | ||
| ); | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| yargs(hideBin(process.argv)) | ||
| .parserConfiguration({ | ||
| "parse-numbers": false, | ||
| }) | ||
| .command({ | ||
| command: "run", | ||
| describe: "run the tester until manually stopped", | ||
| builder: { | ||
| validate: { | ||
| description: "Only validate the configs and exit", | ||
| type: "boolean", | ||
| default: false, | ||
| required: false, | ||
| }, | ||
| config: { | ||
| description: "Yaml config file", | ||
| type: "string", | ||
| required: true, | ||
| }, | ||
| "private-key": { | ||
| type: "string", | ||
| required: true, | ||
| description: | ||
| "Path to the private key to sign the transactions with. Should be hex encoded", | ||
| }, | ||
| }, | ||
| handler: async (argv: any) => { | ||
|
||
| const logger = pino(); | ||
| const configs = loadConfig(argv.config); | ||
| if (argv.validate) { | ||
| logger.info("Config validated"); | ||
| return; | ||
| } | ||
| const privateKey = toPrivateKey( | ||
| fs | ||
| .readFileSync(argv["private-key"], "utf-8") | ||
|
||
| .replace("0x", "") | ||
| .trimEnd(), | ||
| ); | ||
| logger.info("Running"); | ||
| const promises = configs.map(async ({ contract, interval }) => { | ||
| const child = logger.child({ chain: contract.chain.getId() }); | ||
| // eslint-disable-next-line no-constant-condition | ||
| while (true) { | ||
| try { | ||
| await testLatency(contract, privateKey, child); | ||
| } catch (e) { | ||
| child.error(e, "Error testing latency"); | ||
| } | ||
| await new Promise((resolve) => setTimeout(resolve, interval * 1000)); | ||
| } | ||
| }); | ||
| await Promise.all(promises); | ||
| }, | ||
| }) | ||
| .demandCommand() | ||
| .help().argv; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| { | ||
| "extends": "../../tsconfig.base.json", | ||
| "compilerOptions": { | ||
| "target": "esnext", | ||
| "module": "nodenext", | ||
| "declaration": true, | ||
| "rootDir": "src/", | ||
| "outDir": "./lib", | ||
| "strict": true, | ||
| "esModuleInterop": true, | ||
| "resolveJsonModule": true | ||
| }, | ||
| "include": ["src"], | ||
| "exclude": ["node_modules", "**/__tests__/*"] | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suggest using my canned tsconfig as a base here, as that will enable a lot of much stricter configs which make the type system much more reliable / trustworthy. You can use it like this if you decide to do so |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few notes:
.eslintrc.jsis a deprecated config formatThe easiest fix is to replace this file with a file
eslint.config.jsthat exports my canned config, e.g. something like this. Note you'll need to set"type": "module"in yourpackage.jsonto use this as I expect esm for all packages I support.