Skip to content

Commit

Permalink
Show miner & show endpoints flags
Browse files Browse the repository at this point in the history
fixes: #19
  • Loading branch information
DenisCarriere committed Jun 14, 2023
1 parent 39ac5dd commit 097ac09
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 40 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ HOSTNAME=127.0.0.1
LOCK_GAS_PRICE=0x22ecb25c00
LOCK_CHAIN_ID=0x4571
LOCK_GENESIS_TIME="2023-04-05T02:18:09"
SHOW_MINER=true
SHOW_ENDPOINTS=false

# Prometheus Metrics
PROMETHEUS_PORT=9102
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
## [`Bun` Binary Releases](https://github.com/pinax-network/eos-evm-miner/releases)

```
$ wget https://github.com/pinax-network/eos-evm-miner/releases/download/v0.4.2/eos-evm-miner
$ wget https://github.com/pinax-network/eos-evm-miner/releases/download/v0.5.0/eos-evm-miner
$ chmod +x ./eos-evm-miner
```

Expand Down
18 changes: 18 additions & 0 deletions bin/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,22 @@ program.name(pkg.name)
.description("EOS EVM Miner JSON RPC Server")
.version(pkg.version, '-v, --version', `version for ${pkg.name}`)


export interface StartOptions extends DefaultOptions {
port?: number;
metricsListenPort?: number;
hostname?: string;
verbose?: boolean;
metricsDisabled?: boolean;
lockGasPrice?: string;
lockChainId?: string;
lockGenesisTime?: string;
rpcEvmEndpoint?: string;
rpcEndpoint?: string;
showEndpoints?: boolean;
showMiner?: boolean;
}

// Start JSON RPC Server
defaultOptions(program.command("start"))
.description("Start JSON RPC Server")
Expand All @@ -26,6 +42,8 @@ defaultOptions(program.command("start"))
.option('--lock-genesis-time', `Lock genesis time (ex: "${EXAMPLE_LOCK_GENESIS_TIME}")`)
.option('--rpc-endpoint', `EOS RPC endpoint (ex: "${DEFAULT_RPC_ENDPOINT}")`)
.option('--rpc-evm-endpoint', `EOS RPC endpoint (ex: "${DEFAULT_RPC_EVM_ENDPOINT}")`)
.option('--show-endpoints', 'If set, will display all RPC endpoints used by the server')
.option('--show-miner', 'If set, will display all miner account details used by the server')
.action(options => {
start(options);
});
Expand Down
47 changes: 9 additions & 38 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import pkg from "./package.json" assert { type: "json" };

import { JSONRPCRequest, JSONRPCResponse, JSONRPCServer, JSONRPCServerMiddlewareNext, createJSONRPCErrorResponse } from "json-rpc-2.0";
import { Session } from "@wharfkit/session";
import { DEFAULT_HOSTNAME, HOSTNAME, LOCK_GAS_PRICE, PORT,PROMETHEUS_PORT, METRICS_DISABLED, VERBOSE, LOCK_CHAIN_ID, LOCK_GENESIS_TIME, RPC_EVM_ENDPOINT } from "./src/config.js";
import { HOSTNAME, LOCK_GAS_PRICE, PORT,PROMETHEUS_PORT, METRICS_DISABLED, VERBOSE, LOCK_CHAIN_ID, LOCK_GENESIS_TIME, RPC_EVM_ENDPOINT, SHOW_ENDPOINTS, SHOW_MINER } from "./src/config.js";
import { logger } from "./src/logger.js";
import { DefaultOptions } from "./bin/cli.js";
import { StartOptions } from "./bin/cli.js";
import * as prometheus from "./src/prometheus.js"
import { eth_sendRawTransaction } from "./src/eth_sendRawTransaction.js";
import { eth_gasPrice } from "./src/eth_gasPrice.js";
Expand All @@ -14,19 +13,7 @@ import { net_version } from "./src/net_version.js";
import { eth_getCode } from "./src/eth_getCode.js";
import { createSession } from "./src/createSession.js";
import { createClient } from "./src/createClient.js";

export interface StartOptions extends DefaultOptions {
port?: number;
metricsListenPort?: number;
hostname?: string;
verbose?: boolean;
metricsDisabled?: boolean;
lockGasPrice?: string;
lockChainId?: string;
lockGenesisTime?: string;
rpcEvmEndpoint?: string;
rpcEndpoint?: string;
}
import { banner } from "./src/banner.js";

export default function (options: StartOptions) {
const port = options.port ?? PORT;
Expand All @@ -38,6 +25,8 @@ export default function (options: StartOptions) {
const lockGenesisTime = options.lockGenesisTime ?? LOCK_GENESIS_TIME;
const verbose = options.verbose ?? VERBOSE;
const rpcEvmEndpoint = options.rpcEvmEndpoint ?? RPC_EVM_ENDPOINT;
const showEndpoints = options.showEndpoints ?? SHOW_ENDPOINTS;
const showMiner = options.showMiner ?? SHOW_MINER;

// create Wharfkit session
const session = createSession(options);
Expand All @@ -47,9 +36,10 @@ export default function (options: StartOptions) {
const client = createClient(rpcEvmEndpoint);

// enable logging if verbose enabled
const banner_text = banner(session, port, {showMiner, showEndpoints, rpcEvmEndpoint, hostname, metricsListenPort, metricsDisabled})
if (verbose) {
logger.settings.type = "json";
console.log(banner(session, port, rpcEvmEndpoint, hostname, metricsListenPort, metricsDisabled));
console.log(banner_text);
}

server.addMethod("eth_sendRawTransaction", async params => {
Expand Down Expand Up @@ -140,7 +130,7 @@ export default function (options: StartOptions) {
fetch: async (request: Request) => {
const url = new URL(request.url);
if ( request.method == "GET" ) {
if ( url.pathname == "/" ) return new Response(banner(session, port, rpcEvmEndpoint, hostname, metricsListenPort, metricsDisabled));
if ( url.pathname == "/" ) return new Response(banner_text);
const info = await session.client.v1.chain.get_info();
return toJSON(info.toJSON());
}
Expand Down Expand Up @@ -170,22 +160,3 @@ function toJSON(obj: any, status: number = 200) {
const body = JSON.stringify(obj);
return new Response(body, { status, headers });
}

function banner( session: Session, port: number, rpcEvmEndpoint?: string, hostname?: string, metricsListenPort?: number, metricsDisabled?: boolean ) {
let text = `
███████╗ ██████╗ ███████╗ ███████╗██╗ ██╗███╗ ███╗
██╔════╝██╔═══██╗██╔════╝ ██╔════╝██║ ██║████╗ ████║
█████╗ ██║ ██║███████╗ █████╗ ██║ ██║██╔████╔██║
██╔══╝ ██║ ██║╚════██║ ██╔══╝ ╚██╗ ██╔╝██║╚██╔╝██║
███████╗╚██████╔╝███████║ ███████╗ ╚████╔╝ ██║ ╚═╝ ██║
╚══════╝ ╚═════╝ ╚══════╝ ╚══════╝ ╚═══╝ ╚═╝ ╚═╝
`
text += ` EOS EVM Miner v${pkg.version} listen @ ${hostname ?? DEFAULT_HOSTNAME}:${port.toString()}\n`
if ( !metricsDisabled ) text += ` Prometheus metrics listen @ ${hostname ?? DEFAULT_HOSTNAME}:${metricsListenPort?.toString()}\n`;
text += ` RPC Nodeos proxy @ ${session.chain.url.toString()}\n`
text += ` RPC EVM proxy @ ${rpcEvmEndpoint}\n`
text += ` EVM miner [${session.actor.toString()}]\n`;
text += ` ${session.walletPlugin.metadata.publicKey}\n`
return text;
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"private": true,
"name": "@enf/eos-evm-miner",
"version": "0.4.4",
"version": "0.5.0",
"description": "EOS EVM miner",
"type": "module",
"exports": "./dist/index.js",
Expand Down
27 changes: 27 additions & 0 deletions src/banner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import pkg from "../package.json" assert { type: "json" };
import { Session } from "@wharfkit/session";
import { StartOptions } from "../bin/cli.js";
import { DEFAULT_HOSTNAME } from "./config.js";

export function banner( session: Session, port: number, options: StartOptions = {} ) {
const host = `${options.hostname ?? DEFAULT_HOSTNAME}:${port.toString()}`;
const host_metrics = `${options.hostname ?? DEFAULT_HOSTNAME}:${options.metricsListenPort?.toString()}`
const publicKey = session.walletPlugin.metadata.publicKey;
let text = `
███████╗ ██████╗ ███████╗ ███████╗██╗ ██╗███╗ ███╗
██╔════╝██╔═══██╗██╔════╝ ██╔════╝██║ ██║████╗ ████║
█████╗ ██║ ██║███████╗ █████╗ ██║ ██║██╔████╔██║
██╔══╝ ██║ ██║╚════██║ ██╔══╝ ╚██╗ ██╔╝██║╚██╔╝██║
███████╗╚██████╔╝███████║ ███████╗ ╚████╔╝ ██║ ╚═╝ ██║
╚══════╝ ╚═════╝ ╚══════╝ ╚══════╝ ╚═══╝ ╚═╝ ╚═╝
`
text += ` EOS EVM Miner v${pkg.version}\n`
if ( options.showEndpoints ) text += ` Ethereum JSON-RPC API listening on @ ${host}\n`
if ( options.showEndpoints && !options.metricsDisabled ) text += ` Prometheus metrics listening on @ ${host_metrics}\n`;
if ( options.showEndpoints ) text += ` RPC Nodeos proxy @ ${session.chain.url.toString()}\n`
if ( options.showEndpoints ) text += ` RPC EVM proxy @ ${options.rpcEvmEndpoint}\n`
if ( options.showMiner ) text += ` EVM miner [${session.actor.toString()}]\n`;
if ( options.showMiner && publicKey ) text += ` ${publicKey}\n`
return text;
}
4 changes: 4 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export const DEFAULT_RPC_ENDPOINT = 'https://eos.api.eosnation.io';
export const DEFAULT_RPC_EVM_ENDPOINT = 'https://api.evm.eosnetwork.com';
export const DEFAULT_METRICS_DISABLED = false;
export const DEFAULT_VERBOSE = false;
export const DEFAULT_SHOW_MINER = true;
export const DEFAULT_SHOW_ENDPOINTS = false;

// examples
export const EXAMPLE_LOCK_CHAIN_ID = "0x4571";
Expand All @@ -28,6 +30,8 @@ export const RPC_EVM_ENDPOINT = process.env.RPC_EVM_ENDPOINT ?? DEFAULT_RPC_EVM_
export const HOSTNAME = process.env.HOSTNAME;
export const METRICS_DISABLED = JSON.parse(process.env.METRICS_DISABLED ?? String(DEFAULT_METRICS_DISABLED)) as boolean;
export const VERBOSE = JSON.parse(process.env.VERBOSE ?? String(DEFAULT_VERBOSE)) as boolean;
export const SHOW_ENDPOINTS = JSON.parse(process.env.SHOW_ENDPOINTS ?? String(DEFAULT_SHOW_ENDPOINTS)) as boolean;
export const SHOW_MINER = JSON.parse(process.env.SHOW_MINER ?? String(DEFAULT_SHOW_MINER)) as boolean;

// Miner details
export const MINER_PERMISSION = process.env.MINER_PERMISSION ?? process.env.PERMISSION ?? DEFAULT_MINER_PERMISSION;
Expand Down

0 comments on commit 097ac09

Please sign in to comment.