Skip to content

Commit

Permalink
remove exitReason from receipt (#963)
Browse files Browse the repository at this point in the history
* remove exitReason

* fix

* fix

* fix
  • Loading branch information
shunjizhan committed Mar 15, 2024
1 parent 7c1d44a commit ca77ff8
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 55 deletions.
5 changes: 3 additions & 2 deletions packages/eth-providers/src/__tests__/BlockCache.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { TransactionReceipt } from '@ethersproject/abstract-provider';

import { BlockCache } from '../utils/BlockCache';
import { FullReceipt } from '../utils';
import { describe, expect, it } from 'vitest';
import { mockChain } from './testUtils';

const sortReceipt = (r1: FullReceipt, r2: FullReceipt) => {
const sortReceipt = (r1: TransactionReceipt, r2: TransactionReceipt) => {
if (r1.blockNumber !== r2.blockNumber) {
return r1.blockNumber - r2.blockNumber;
} else if (r1.blockHash !== r2.blockHash) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ describe('getReceiptAtBlock', async () => {
const res1 = await provider.getReceiptAtBlockFromChain(txHash1, blockHash);
const res2 = await provider.getReceiptAtBlockFromChain(txHash2, blockHash);

delete res2?.['exitReason']; // full receipt contains exitReason
expect(res1).to.deep.equal(receipt1);
expect(res2).to.deep.equal(receipt2);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ interface FormatedReceipt {
const formatReceipt = (receipt: TransactionReceipt): FormatedReceipt =>
Object.entries(hexlifyRpcResult(receipt)).reduce((acc, kvPair) => {
const [k, v] = kvPair;
if (!['confirmations'].includes(k)) {
if (!['confirmations', 'byzantium'].includes(k)) {
acc[k] = v;
}

Expand Down
3 changes: 0 additions & 3 deletions packages/eth-providers/src/__tests__/e2e/receipt-snapshots.ts
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,6 @@ export const acala1555311b = {
contractAddress: null,
transactionIndex: '0x1',
gasUsed: '0x237fd',
exitReason: '{"revert":"Reverted"}',
logsBloom: DUMMY_LOGS_BLOOM,
blockHash: '0xf9655bfef23bf7dad14a037aa39758daccfd8dc99a7ce69525f81548068a5946',
transactionHash: '0x240a9ec2efdacae2a89486980874b23987b9801fd1ca7a424506629b71a53fa6',
Expand All @@ -450,7 +449,6 @@ export const acala1102030a = {
blockNumber: '0x10d0ce',
cumulativeGasUsed: '0x0',
effectiveGasPrice: '0x513d6f23',
exitReason: '{"error":{"other":"ReserveStorageFailed"}}',
status: '0x0',
type: '0x0',
};
Expand All @@ -468,7 +466,6 @@ export const acala1102030b = {
blockNumber: '0x10d0ce',
cumulativeGasUsed: '0x0',
effectiveGasPrice: '0x2a1c0bcb',
exitReason: '{"error":{"other":"ReserveStorageFailed"}}',
status: '0x0',
type: '0x0',
};
Expand Down
8 changes: 4 additions & 4 deletions packages/eth-providers/src/__tests__/testUtils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { FullReceipt } from '../utils';
import { TransactionReceipt } from '@ethersproject/abstract-provider';

type MochBlock = {
blockHash: string;
blockNumber: number;
receipts: FullReceipt[];
receipts: TransactionReceipt[];
};
type MochChain = MochBlock[];

Expand All @@ -12,12 +12,12 @@ export const randHash = (): string => {
return '0x' + hash.padStart(64, '0');
};

export const randReceipt = (blockNumber: number, blockHash: string): FullReceipt =>
export const randReceipt = (blockNumber: number, blockHash: string): TransactionReceipt =>
({
blockHash,
blockNumber,
transactionHash: randHash(),
} as FullReceipt);
} as TransactionReceipt);

export const mockBlock = (blockNumber: number): MochBlock => {
const blockHash = randHash();
Expand Down
10 changes: 6 additions & 4 deletions packages/eth-providers/src/base-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ import {
} from './consts';
import {
BaseLogFilter,
FullReceipt,
HealthResult,
LogFilter,
PROVIDER_ERRORS,
Expand Down Expand Up @@ -484,7 +483,7 @@ export abstract class BaseProvider extends AbstractProvider {
}
};

_notifySubscribers = async (header: Header, receipts: FullReceipt[]) => {
_notifySubscribers = async (header: Header, receipts: TransactionReceipt[]) => {
const headSubscribers = this.eventListeners[SubscriptionType.NewHeads];
const logSubscribers = this.eventListeners[SubscriptionType.Logs];

Expand Down Expand Up @@ -672,7 +671,7 @@ export abstract class BaseProvider extends AbstractProvider {
? (await this.getEvmAddress(headerExtended.author.toString())).toLowerCase()
: DUMMY_ADDRESS;

let receipts: FullReceipt[];
let receipts: TransactionReceipt[];
if (receiptsFromSubql?.length) {
receipts = receiptsFromSubql.map(subqlReceiptAdapter);
} else {
Expand Down Expand Up @@ -1746,7 +1745,10 @@ export abstract class BaseProvider extends AbstractProvider {
if (
txFromCache &&
await this._isBlockCanonical(txFromCache.blockHash, txFromCache.blockNumber)
) return txFromCache;
) {
delete txFromCache.byzantium;
return txFromCache;
}

await this._checkSubqlHeight();

Expand Down
16 changes: 7 additions & 9 deletions packages/eth-providers/src/utils/BlockCache.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { Log } from '@ethersproject/abstract-provider';
import { Log, TransactionReceipt } from '@ethersproject/abstract-provider';

import { FullReceipt } from './receiptHelper';

export type TxHashToReceipt = Record<string, FullReceipt>;
export type BlockHashToReceipts = Record<string, FullReceipt[]>;
export type TxHashToReceipt = Record<string, TransactionReceipt>;
export type BlockHashToReceipts = Record<string, TransactionReceipt[]>;
export type Block = {
number: number;
hash: string;
Expand Down Expand Up @@ -34,7 +32,7 @@ export class BlockCache {
setlastCachedBlock = (block: Block) => (this.lastCachedBlock = block);

// automatically preserve a sliding window of ${maxCachedBlocks} blocks
addReceipts = (blockHash: string, receipts: FullReceipt[]): void => {
addReceipts = (blockHash: string, receipts: TransactionReceipt[]): void => {
this.blockHashToReceipts[blockHash] = receipts;
receipts.forEach(r => {
this.txHashToReceipt[r.transactionHash] = r;
Expand All @@ -52,13 +50,13 @@ export class BlockCache {
}
};

getReceiptByHash = (txHash: string): FullReceipt | null =>
getReceiptByHash = (txHash: string): TransactionReceipt | null =>
this.txHashToReceipt[txHash] ?? null;

getAllReceiptsAtBlock = (blockHash: string): FullReceipt[] =>
getAllReceiptsAtBlock = (blockHash: string): TransactionReceipt[] =>
this.blockHashToReceipts[blockHash] ?? [];

getReceiptAtBlock = (txHash: string, blockHash: string): FullReceipt | null =>
getReceiptAtBlock = (txHash: string, blockHash: string): TransactionReceipt | null =>
this.getAllReceiptsAtBlock(blockHash).find(r => r.transactionHash === txHash) ?? null;

getLogsAtBlock = (blockHash: string): Log[] =>
Expand Down
18 changes: 9 additions & 9 deletions packages/eth-providers/src/utils/parseBlock.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { AnyTuple } from '@polkadot/types/types';
import { ApiDecoration } from '@polkadot/api/types';
import { ApiPromise } from '@polkadot/api';
import { BIGNUMBER_ZERO, ONE_HUNDRED_GWEI } from '../consts';
import { BigNumber } from 'ethers';
import {
EventRecord,
Expand All @@ -11,16 +10,17 @@ import {
WeightV1,
WeightV2,
} from '@polkadot/types/interfaces';
import { Formatter } from '@ethersproject/providers';
import { FrameSystemEventRecord } from '@polkadot/types/lookup';
import { GenericExtrinsic } from '@polkadot/types';
import { TransactionReceipt } from '@ethersproject/abstract-provider';

import { BIGNUMBER_ZERO, ONE_HUNDRED_GWEI } from '../consts';
import {
FullReceipt,
findEvmEvent,
fullReceiptFormatter,
formatter,
getOrphanTxReceiptsFromEvents,
getPartialTransactionReceipt,
} from './receiptHelper';
import { GenericExtrinsic } from '@polkadot/types';
import {
isExtrinsicFailedEvent,
isExtrinsicSuccessEvent,
Expand All @@ -33,7 +33,7 @@ export const getAllReceiptsAtBlock = async (
api: ApiPromise,
blockHash: string,
targetTxHash?: string
): Promise<FullReceipt[]> => {
): Promise<TransactionReceipt[]> => {
const apiAtTargetBlock = await api.at(blockHash);

const [block, blockEvents] = await Promise.all([
Expand All @@ -49,7 +49,7 @@ export const parseReceiptsFromBlockData = async (
block: SignedBlock,
blockEvents: FrameSystemEventRecord[],
targetTxHash?: string
): Promise<FullReceipt[]> => {
): Promise<TransactionReceipt[]> => {
const { header } = block.block;
const blockNumber = header.number.toNumber();
const blockHash = header.hash.toHex();
Expand All @@ -69,7 +69,7 @@ export const parseReceiptsFromBlockData = async (
normalTxs = normalTxs.filter(({ extrinsic }) => extrinsic.hash.toHex() === targetTxHash);
}

const normalReceiptsPending: Promise<FullReceipt>[] = normalTxs.map(
const normalReceiptsPending: Promise<TransactionReceipt>[] = normalTxs.map(
async ({ extrinsicEvents, extrinsic }, transactionIndex) => {
const evmEvent = findEvmEvent(extrinsicEvents);
if (!evmEvent) {
Expand All @@ -89,7 +89,7 @@ export const parseReceiptsFromBlockData = async (
...log,
}));

return Formatter.check(fullReceiptFormatter, {
return formatter.receipt({
effectiveGasPrice,
...txInfo,
...partialReceipt,
Expand Down
32 changes: 10 additions & 22 deletions packages/eth-providers/src/utils/receiptHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ export interface ExtrinsicMethodJSON {
};
}

export interface FullReceipt extends TransactionReceipt {
exitReason?: string;
}

export const getPartialLog = (evmLog: EvmLog, logIndex: number): PartialLog => {
return {
removed: false,
Expand All @@ -68,7 +64,6 @@ export interface PartialTransactionReceipt {
gasUsed: BigNumber;
cumulativeGasUsed: BigNumber;
status?: number;
exitReason?: string;
}

const DUMMY_LOGS_BLOOM =
Expand Down Expand Up @@ -125,7 +120,6 @@ export const getPartialTransactionReceipt = (event: FrameSystemEventRecord): Par
gasUsed: BigNumber.from(usedGas?.toString() || 0),
logs: getPartialLogs(logs),
status: 0,
exitReason: _exitReason.toString(),
...defaultValue,
};
}
Expand All @@ -146,7 +140,6 @@ export const getPartialTransactionReceipt = (event: FrameSystemEventRecord): Par
contractAddress: undefined,
gasUsed: BigNumber.from(usedGas?.toString() || 0),
status: 0,
exitReason: _exitReason.toString(),
logs: getPartialLogs(logs),
...defaultValue,
};
Expand Down Expand Up @@ -221,17 +214,13 @@ const nToU8aLegacy = (...params: Parameters<typeof nToU8a>): ReturnType<typeof n
};

export const formatter = new Formatter();
export const fullReceiptFormatter = {
...formatter.formats.receipt,
exitReason: (x: any) => x,
};

export const getOrphanTxReceiptsFromEvents = (
events: FrameSystemEventRecord[],
blockHash: string,
blockNumber: number,
indexOffset: number
): FullReceipt[] => {
): TransactionReceipt[] => {
const receipts = events
.filter(isOrphanEvmEvent)
.map(getPartialTransactionReceipt)
Expand All @@ -257,19 +246,18 @@ export const getOrphanTxReceiptsFromEvents = (
};
});

return receipts.map(receipt => Formatter.check(fullReceiptFormatter, receipt));
return receipts.map(receipt => formatter.receipt(receipt));
};

export const subqlReceiptAdapter = <T extends TransactionReceiptSubql | null>(receipt: T):
T extends null ? null : FullReceipt =>
receipt
? Formatter.check(fullReceiptFormatter, {
...receipt,
logs: receipt.logs.nodes,
})
: null;
export const subqlReceiptAdapter = (receipt: TransactionReceiptSubql): TransactionReceipt => (
formatter.receipt({
...receipt,
logs: receipt.logs.nodes,
})
);


export const receiptToTransaction = (tx: FullReceipt, block: SignedBlock): TX => {
export const receiptToTransaction = (tx: TransactionReceipt, block: SignedBlock): TX => {
const extrinsic = block.block.extrinsics.find(ex => ex.hash.toHex() === tx.transactionHash);

const extraData = extrinsic ? parseExtrinsic(extrinsic) : ORPHAN_TX_DEFAULT_INFO;
Expand Down
1 change: 1 addition & 0 deletions packages/eth-rpc-adapter/src/eip1193-bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ class Eip1193BridgeImpl {

// @ts-ignore
delete res.confirmations;
delete res.byzantium;
return hexlifyRpcResult(res);
}

Expand Down

0 comments on commit ca77ff8

Please sign in to comment.