Skip to content
This repository has been archived by the owner on Mar 24, 2023. It is now read-only.

Commit

Permalink
Merge pull request #112 from nervosnetwork/fix-sub-log
Browse files Browse the repository at this point in the history
fix: stringify bigint when emit logs for ws subscribe
  • Loading branch information
RetricSu authored Dec 11, 2021
2 parents 81fa990 + 69b1658 commit 8c1fba5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
9 changes: 7 additions & 2 deletions packages/api-server/src/block-emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { envConfig } from "./base/env-config";
import { logger } from "./base/logger";
import { Query } from "./db";
import { EventEmitter } from "events";
import { toApiLog, toApiNewHead } from "./db/types";
import { toApiNewHead } from "./db/types";
import cluster from "cluster";

let newrelic: any = undefined;
Expand Down Expand Up @@ -115,7 +115,12 @@ export class BlockEmitter {
const newHeads = blocks.map((b) => toApiNewHead(b));
this.notify("newHeads", newHeads);
const logs = await this.query.getLogs({}, min + BigInt(1), max); // exclude min & include max;
const newLogs = logs.map((log) => toApiLog(log));
const newLogs = logs.map((log) =>
JSON.stringify(
log,
(key, value) => (typeof value === "bigint" ? value.toString() : value) // return everything else unchanged
)
);
if (logs.length > 0) {
this.notify("logs", newLogs);
}
Expand Down
13 changes: 10 additions & 3 deletions packages/api-server/src/ws/methods.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EthBlock } from "../base/types/api";
import { EthNewHead } from "../base/types/api";
import { BlockEmitter } from "../block-emitter";
import { INVALID_PARAMS, METHOD_NOT_FOUND } from "../methods/error-code";
import { methods } from "../methods/index";
Expand Down Expand Up @@ -50,7 +50,7 @@ export function wrapper(ws: any, _req: any) {
const syncingIds: Set<HexNumber> = new Set();
const logsQueryMaps: Map<HexNumber, LogQueryOption> = new Map();

const blockListener = (blocks: EthBlock[]) => {
const blockListener = (blocks: EthNewHead[]) => {
blocks.forEach((block) => {
newHeadsIds.forEach((id) => {
const obj = {
Expand All @@ -66,7 +66,14 @@ export function wrapper(ws: any, _req: any) {
});
};

const logsListener = (logs: Log[]) => {
const logsListener = (_logs: string[]) => {
const logs: Log[] = _logs.map((_log) => {
let log = JSON.parse(_log);
log.id = BigInt(log.id);
log.block_number = BigInt(log.block_number);
log.transaction_id = BigInt(log.transaction_id);
return log;
});
logsQueryMaps.forEach((query, id) => {
const _result = filterLogsByAddress(logs, query.address);
const result = filterLogsByTopics(_result, query.topics || []);
Expand Down

0 comments on commit 8c1fba5

Please sign in to comment.