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

refactor: reuse tx hash convert fn #489

Merged
merged 1 commit into from Aug 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 40 additions & 2 deletions packages/api-server/src/cache/tx-hash.ts
@@ -1,4 +1,5 @@
import { Hash } from "@ckb-lumos/base";
import { Hash, HexString } from "@ckb-lumos/base";
import { Query } from "../db";
import {
TX_HASH_MAPPING_CACHE_EXPIRED_TIME_MILSECS,
TX_HASH_MAPPING_PREFIX_KEY,
Expand All @@ -13,7 +14,6 @@ function gwTxHashCacheKey(gwTxHash: string) {
return `${TX_HASH_MAPPING_PREFIX_KEY}:gw:${gwTxHash}`;
}

// TODO: refactor eth.ts with this
export class TxHashMapping {
private store: Store;

Expand Down Expand Up @@ -46,3 +46,41 @@ export class TxHashMapping {
return await this.store.get(ethTxHashKey);
}
}

export async function gwTxHashToEthTxHash(
gwTxHash: HexString,
query: Query,
cacheStore: Store
) {
let ethTxHashInCache = await new TxHashMapping(cacheStore).getEthTxHash(
gwTxHash
);
if (ethTxHashInCache != null) {
return ethTxHashInCache;
}

// query from database
const gwTxHashInDb: Hash | undefined = await query.getEthTxHashByGwTxHash(
gwTxHash
);
return gwTxHashInDb;
}

export async function ethTxHashToGwTxHash(
ethTxHash: HexString,
query: Query,
cacheStore: Store
) {
let gwTxHashInCache = await new TxHashMapping(cacheStore).getGwTxHash(
ethTxHash
);
if (gwTxHashInCache != null) {
return gwTxHashInCache;
}

// query from database
const ethTxHashInDb: Hash | undefined = await query.getGwTxHashByEthTxHash(
ethTxHash
);
return ethTxHashInDb;
}
41 changes: 6 additions & 35 deletions packages/api-server/src/methods/modules/eth.ts
Expand Up @@ -86,6 +86,7 @@ import { logger } from "../../base/logger";
import { calcIntrinsicGas } from "../../util";
import { FilterFlag, FilterParams, RpcFilterRequest } from "../../base/filter";
import { Reader } from "@ckb-lumos/toolkit";
import { ethTxHashToGwTxHash } from "../../cache/tx-hash";

const Config = require("../../../config/eth.json");

Expand Down Expand Up @@ -868,7 +869,11 @@ export class Eth {
args: [string]
): Promise<EthTransactionReceipt | null> {
const ethTxHash: Hash = args[0];
const gwTxHash: Hash | null = await this.ethTxHashToGwTxHash(ethTxHash);
const gwTxHash: Hash | undefined = await ethTxHashToGwTxHash(
ethTxHash,
this.query,
this.cacheStore
);
if (gwTxHash == null) {
return null;
}
Expand Down Expand Up @@ -1164,40 +1169,6 @@ export class Eth {
return blockNumber;
}

private async ethTxHashToGwTxHash(ethTxHash: HexString) {
// query from redis for instant-finality tx
const ethTxHashKey = ethTxHashCacheKey(ethTxHash);
let gwTxHash = await this.cacheStore.get(ethTxHashKey);
if (gwTxHash != null) {
return gwTxHash;
}

// query from database
const transaction = await this.query.getTransactionByEthTxHash(ethTxHash);
if (transaction != null) {
return transaction.hash;
}

return null;
}

private async gwTxHashToEthTxHash(gwTxHash: HexString) {
// query from redis for instant-finality tx
const gwTxHashKey = gwTxHashCacheKey(gwTxHash);
let ethTxHash = await this.cacheStore.get(gwTxHashKey);
if (ethTxHash != null) {
return ethTxHash;
}

// query from database
const transaction = await this.query.getTransactionByHash(gwTxHash);
if (transaction != null) {
return transaction.eth_tx_hash;
}

return null;
}

private async _rpcFilterRequestToGetLogsParams(
filter: RpcFilterRequest
): Promise<FilterParams> {
Expand Down
22 changes: 3 additions & 19 deletions packages/api-server/src/methods/modules/poly.ts
Expand Up @@ -6,7 +6,7 @@ import { gwConfig } from "../../base/index";
import { Store } from "../../cache/store";
import { CACHE_EXPIRED_TIME_MILSECS } from "../../cache/constant";
import { Query } from "../../db";
import { TxHashMapping } from "../../cache/tx-hash";
import { ethTxHashToGwTxHash, gwTxHashToEthTxHash } from "../../cache/tx-hash";
import { middleware, validators } from "../validator";
const { version: web3Version } = require("../../../package.json");

Expand Down Expand Up @@ -98,27 +98,11 @@ export class Poly {

async getGwTxHashByEthTxHash(args: [Hash]): Promise<Hash | undefined> {
const ethTxHash = args[0];
const gwTxHashInCache = await new TxHashMapping(
this.cacheStore
).getGwTxHash(ethTxHash);
if (gwTxHashInCache != null) {
return gwTxHashInCache;
}
const gwTxHashInDb: Hash | undefined =
await this.query.getGwTxHashByEthTxHash(ethTxHash);
return gwTxHashInDb;
return await ethTxHashToGwTxHash(ethTxHash, this.query, this.cacheStore);
}

async getEthTxHashByGwTxHash(args: [Hash]): Promise<Hash | undefined> {
const gwTxHash = args[0];
const ethTxHashInCache = await new TxHashMapping(
this.cacheStore
).getEthTxHash(gwTxHash);
if (ethTxHashInCache != null) {
return ethTxHashInCache;
}
const ethTxHashInDb: Hash | undefined =
await this.query.getEthTxHashByGwTxHash(gwTxHash);
return ethTxHashInDb;
return await gwTxHashToEthTxHash(gwTxHash, this.query, this.cacheStore);
}
}
41 changes: 11 additions & 30 deletions packages/api-server/src/ws/methods.ts
Expand Up @@ -4,16 +4,14 @@ import { INVALID_PARAMS, METHOD_NOT_FOUND } from "../methods/error-code";
import { methods } from "../methods/index";
import { middleware as wsrpc } from "./wss";
import crypto from "crypto";
import { HexNumber, HexString } from "@ckb-lumos/base";
import { HexNumber } from "@ckb-lumos/base";
import { Log, LogQueryOption, toApiLog } from "../db/types";
import { filterLogsByAddress, filterLogsByTopics, Query } from "../db";
import { envConfig } from "../base/env-config";
import { Store } from "../cache/store";
import {
CACHE_EXPIRED_TIME_MILSECS,
TX_HASH_MAPPING_PREFIX_KEY,
} from "../cache/constant";
import { CACHE_EXPIRED_TIME_MILSECS } from "../cache/constant";
import { wsApplyRateLimitByIp } from "../rate-limit";
import { gwTxHashToEthTxHash } from "../cache/tx-hash";

const query = new Query();
const cacheStore = new Store(
Expand Down Expand Up @@ -66,27 +64,6 @@ export function wrapper(ws: any, req: any) {
const syncingIds: Set<HexNumber> = new Set();
const logsQueryMaps: Map<HexNumber, LogQueryOption> = new Map();

async function gwTxHashToEthTxHash(gwTxHash: HexString) {
// query from redis for instant-finality tx
const gwTxHashKey = gwTxHashCacheKey(gwTxHash);
let ethTxHash = await cacheStore.get(gwTxHashKey);
if (ethTxHash != null) {
return ethTxHash;
}

// query from database
const transaction = await query.getTransactionByHash(gwTxHash);
if (transaction != null) {
return transaction.eth_tx_hash;
}

return null;
}

function gwTxHashCacheKey(gwTxHash: string) {
return `${TX_HASH_MAPPING_PREFIX_KEY}:gw:${gwTxHash}`;
}

const blockListener = (blocks: EthNewHead[]) => {
blocks.forEach((block) => {
newHeadsIds.forEach((id) => {
Expand All @@ -111,9 +88,9 @@ export function wrapper(ws: any, req: any) {
log.transaction_id = BigInt(log.transaction_id);
return log;
});
logsQueryMaps.forEach(async (query, id) => {
const _result = filterLogsByAddress(logs, query.address);
const result = filterLogsByTopics(_result, query.topics || []);
logsQueryMaps.forEach(async (logQuery, id) => {
const _result = filterLogsByAddress(logs, logQuery.address);
const result = filterLogsByTopics(_result, logQuery.topics || []);

if (result.length === 0) return;

Expand All @@ -123,7 +100,11 @@ export function wrapper(ws: any, req: any) {
params: {
result: await Promise.all(
result.map(async (log) => {
const ethTxHash = await gwTxHashToEthTxHash(log.transaction_hash);
const ethTxHash = await gwTxHashToEthTxHash(
log.transaction_hash,
query,
cacheStore
);
return toApiLog(log, ethTxHash!);
})
),
Expand Down