Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
fix: switch to user-provided logger rather than console.log (#3466)
Browse files Browse the repository at this point in the history
* use user-provided logger in blockchain.resume

* use user-provided logger in ws-handler

* remove unused dependency

* pass logging instead of all options
  • Loading branch information
MicaiahReid committed Aug 5, 2022
1 parent 90b3f2b commit df29a09
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
4 changes: 3 additions & 1 deletion src/chains/ethereum/ethereum/src/blockchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,9 @@ export default class Blockchain extends Emittery<BlockchainTypedEvents> {

resume(_threads: number = 1) {
if (!this.#isPaused()) {
console.log("Warning: startMining called when miner was already started");
this.#options.logging.logger.log(
"Warning: startMining called when miner was already started"
);
return;
}

Expand Down
18 changes: 12 additions & 6 deletions src/chains/ethereum/ethereum/src/forking/handlers/ws-handler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { EthereumInternalOptions } from "@ganache/ethereum-options";
import { AbortError, CodedError } from "@ganache/ethereum-utils";
import { AbortError } from "@ganache/ethereum-utils";
import { AbortSignal } from "abort-controller";
import WebSocket from "ws";
import { Handler } from "../types";
Expand All @@ -23,7 +23,10 @@ export class WsHandler extends BaseHandler implements Handler {
constructor(options: EthereumInternalOptions, abortSignal: AbortSignal) {
super(options, abortSignal);

const { url, origin } = options.fork;
const {
fork: { url, origin },
logging
} = options;

this.connection = new WebSocket(url.toString(), {
origin,
Expand All @@ -40,11 +43,11 @@ export class WsHandler extends BaseHandler implements Handler {
// handler too.
this.connection.binaryType = "nodebuffer";

this.open = this.connect(this.connection);
this.open = this.connect(this.connection, logging);
this.connection.onclose = () => {
// try to connect again...
// TODO: backoff and eventually fail
this.open = this.connect(this.connection);
this.open = this.connect(this.connection, logging);
};
this.abortSignal.addEventListener("abort", () => {
this.connection.onclose = null;
Expand Down Expand Up @@ -98,7 +101,10 @@ export class WsHandler extends BaseHandler implements Handler {
}
}

private connect(connection: WebSocket) {
private connect(
connection: WebSocket,
logging: EthereumInternalOptions["logging"]
) {
let open = new Promise((resolve, reject) => {
connection.onopen = resolve;
connection.onerror = reject;
Expand All @@ -109,7 +115,7 @@ export class WsHandler extends BaseHandler implements Handler {
connection.onerror = null;
},
err => {
console.log(err);
logging.logger.log(err);
}
);
return open;
Expand Down

0 comments on commit df29a09

Please sign in to comment.