Skip to content

Commit

Permalink
eth_newFilter tests
Browse files Browse the repository at this point in the history
  • Loading branch information
shunjizhan committed Oct 9, 2024
1 parent cf11e33 commit 5cb3063
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 19 deletions.
16 changes: 6 additions & 10 deletions packages/eth-providers/src/base-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2070,19 +2070,13 @@ export abstract class BaseProvider extends AbstractProvider {
toBlock: effectiveTo,
};

if (!this.subql) {
return logger.throwError(
'missing subql url to fetch logs, to initialize base provider with subql, please provide a subqlUrl param.'
);
}

filterInfo.lastPollBlockNumber = curBlockNumber;
filterInfo.lastPollTimestamp = Date.now();

const subqlLogs = await this.subql.getFilteredLogs(effectiveFilter); // FIXME: this misses unfinalized logs
const filteredLogs = subqlLogs.filter(log => filterLogByTopics(log, sanitizedFilter.topics));
const logs = await this.getLogs(effectiveFilter);
const formattedLogs = logs.map(log => this.formatter.filterLog(log));

Check warning on line 2077 in packages/eth-providers/src/base-provider.ts

View check run for this annotation

Codecov / codecov/patch

packages/eth-providers/src/base-provider.ts#L2076-L2077

Added lines #L2076 - L2077 were not covered by tests

return hexlifyRpcResult(filteredLogs.map(log => this.formatter.filterLog(log)));
return hexlifyRpcResult(formattedLogs);

Check warning on line 2079 in packages/eth-providers/src/base-provider.ts

View check run for this annotation

Codecov / codecov/patch

packages/eth-providers/src/base-provider.ts#L2079

Added line #L2079 was not covered by tests
};

_pollBlocks = async (filterInfo: BlockPollFilter): Promise<string[]> => {
Expand All @@ -2109,7 +2103,9 @@ export abstract class BaseProvider extends AbstractProvider {
}

// TODO: TS bug?? why filterInfo type is not BlockPollFilter | LogPollFilter
return filterInfo['logFilter'] ? this._pollLogs(filterInfo as LogPollFilter) : this._pollBlocks(filterInfo);
return filterInfo['logFilter']
? this._pollLogs(filterInfo as LogPollFilter)
: this._pollBlocks(filterInfo as BlockPollFilter);

Check warning on line 2108 in packages/eth-providers/src/base-provider.ts

View check run for this annotation

Codecov / codecov/patch

packages/eth-providers/src/base-provider.ts#L2107-L2108

Added lines #L2107 - L2108 were not covered by tests
};

removePollFilter = (id: string): boolean => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { beforeAll, describe, expect, it } from 'vitest';

import { BigNumber, Contract, ContractTransaction } from 'ethers';
import {
TRANSFER_EVENT_TOPIC,
deployErc20,
eth_blockNumber,
eth_getTransactionByHash,
Expand Down Expand Up @@ -100,7 +101,7 @@ describe('get tx and receipt', () => {
transactionHash: pendingTx.hash,
address: token.address.toLowerCase(),
topics: [
'0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef',
TRANSFER_EVENT_TOPIC,
hexZeroPad(wallet.address.toLowerCase(), 32),
hexZeroPad(wallet1.address.toLowerCase(), 32),
],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
import { beforeAll, describe, expect, it } from 'vitest';

import { Contract } from 'ethers';
import {
TRANSFER_EVENT_TOPIC,
deployErc20,
eth_blockNumber,
eth_getFilterChanges,
eth_getLogs,
eth_newFilter,
eth_uninstallFilter,
getCurBlockHash,
testSetup,
} from '../utils';

const {
wallets: [wallet, wallet1],
provider,
} = testSetup;

describe('eth_newFilter', () => {
let token: Contract;
const dummyId = '0x12345678906f9c864d9db560d72a247c178ae86b';
let startBlockNum: number;
let fid0: string;
let fid1: string;
let fid2: string;
let fid3: string;

const feedTx = async () => token.transfer(wallet1.address, 11122233);

beforeAll(async () => {
token = await deployErc20(wallet);

startBlockNum = await provider.getBlockNumber();

fid0 = (await eth_newFilter([{}])).data.result; // only pull once at the end

fid1 = (
await eth_newFilter([
{
// normal log poll
address: token.address,
topics: [
TRANSFER_EVENT_TOPIC,
null,
null,
],
},
])
).data.result;

fid2 = (
await eth_newFilter([
{
// normal log poll
address: token.address,
fromBlock: startBlockNum,
toBlock: startBlockNum + 3,
},
])
).data.result;

fid3 = (
await eth_newFilter([
{
// empty
fromBlock: 3,
toBlock: 100,
},
])
).data.result;
});

it('poll immediately', async () => {
const res1 = (await eth_getFilterChanges([fid1])).data.result;
const res2 = (await eth_getFilterChanges([fid2])).data.result;
const res3 = (await eth_getFilterChanges([fid3])).data.result;

expect([res1, res2, res3]).to.deep.equal([[], [], []]);
});

it('get correct result', async () => {
/* ---------- fire 1 tx ---------- */
await (await feedTx()).wait();

let res1 = (await eth_getFilterChanges([fid1])).data.result;
let res2 = (await eth_getFilterChanges([fid2])).data.result;
let res3 = (await eth_getFilterChanges([fid3])).data.result;

const curBlockHash = await getCurBlockHash();
let expectedLogs = (await eth_getLogs([{ blockHash: curBlockHash }])).data.result;

expect(expectedLogs.length).to.equal(1);
expect(res1).to.deep.equal(expectedLogs);
expect(res2).to.deep.equal(expectedLogs);
expect(res3).to.deep.equal([]);

/* ---------- fire many tx ---------- */
const txCount = 5;
for (let i = 0; i < txCount; i++) {
await (await feedTx()).wait();
}

const res0 = (await eth_getFilterChanges([fid0])).data.result;
res1 = (await eth_getFilterChanges([fid1])).data.result;
res2 = (await eth_getFilterChanges([fid2])).data.result;
res3 = (await eth_getFilterChanges([fid3])).data.result;

const curHeight = Number((await eth_blockNumber()).data.result);
expectedLogs = (
await eth_getLogs([
{
fromBlock: curHeight - txCount,
toBlock: curHeight,
},
])
).data.result;

console.log({
expectedLogs,
curHeight,
});

expect(expectedLogs.length).to.equal(txCount + 1); // + 1 because it's all logs, which conains the one in prev test
expect(res0).to.deep.equal(expectedLogs);
expect(res1).to.deep.equal(expectedLogs.slice(1));
// it's range is [x, x + 3], x is original block, x + 1 is prev test, now only poll for x + 2 and x + 3, so has 2 logs
expect(res2).to.deep.equal(expectedLogs.slice(1, 3));
expect(res3).to.deep.equal([]);
});

it('unsubscribe works', async () => {
const unsub = (await eth_uninstallFilter([fid0])).data.result;
const unsub2 = (await eth_uninstallFilter([fid0])).data.result;
const unsub3 = (await eth_uninstallFilter([dummyId])).data.result;

expect(unsub).to.equal(true);
expect(unsub2).to.equal(false);
expect(unsub3).to.equal(false);

await (await feedTx()).wait();

const res1 = (await eth_getFilterChanges([fid1])).data.result;
const res2 = (await eth_getFilterChanges([fid2])).data.result;
const res3 = (await eth_getFilterChanges([fid3])).data.result;

const curBlockHash = await getCurBlockHash();
const expectedLogs = (await eth_getLogs([{ blockHash: curBlockHash }])).data.result;

// all other filters should still work
expect(expectedLogs.length).to.equal(1);
expect(res1).to.deep.equal(expectedLogs);
expect(res2).to.deep.equal([]); // now block range doesn't match anymore (cannot skip previous test)
expect(res3).to.deep.equal([]);

// target should be removed
const res0 = await eth_getFilterChanges([fid0]);
expect(res0.data.error.message).to.contains('filter not found');
});

it.skip('throws correct error messege', async () => {
// tested in eth_newBlockFilter
});
});
10 changes: 2 additions & 8 deletions packages/eth-rpc-adapter/src/__tests__/utils/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,7 @@ export interface LogHexified {
logIndex: string;
}

export const ADDRESS_ALICE = '0x82a258cb20e2adb4788153cd5eb5839615ece9a0';

export const DETERMINISTIC_SETUP_DEX_ADDRESS = '0x532394de2ca885b7e0306a2e258074cca4e42449';

export const KARURA_CONTRACT_CALL_TX_HASH = '0x33661888b04c81858c3603994eeb9a294c57b585bd86b4663ccd5e4fd7f2c325';
export const KARURA_CONTRACT_DEPLOY_TX_HASH = '0x56a429edfc1c07d7fd4c048e6e868dbaaa632fc329e7bb7ed744a48bca5bb493';
export const KARURA_SEND_KAR_TX_HASH = '0x69493fd597760d5ad3a81ebbbb48abcc686d33814e097b1db9fc172341c36dae';

export const deployHelloWorldData =
'0x60806040526040518060400160405280600c81526020017f48656c6c6f20576f726c642100000000000000000000000000000000000000008152506000908051906020019061004f929190610062565b5034801561005c57600080fd5b50610166565b82805461006e90610134565b90600052602060002090601f01602090048101928261009057600085556100d7565b82601f106100a957805160ff19168380011785556100d7565b828001600101855582156100d7579182015b828111156100d65782518255916020019190600101906100bb565b5b5090506100e491906100e8565b5090565b5b808211156101015760008160009055506001016100e9565b5090565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b6000600282049050600182168061014c57607f821691505b602082108114156101605761015f610105565b5b50919050565b61022e806101756000396000f3fe608060405234801561001057600080fd5b506004361061002b5760003560e01c8063c605f76c14610030575b600080fd5b61003861004e565b6040516100459190610175565b60405180910390f35b6000805461005b906101c6565b80601f0160208091040260200160405190810160405280929190818152602001828054610087906101c6565b80156100d45780601f106100a9576101008083540402835291602001916100d4565b820191906000526020600020905b8154815290600101906020018083116100b757829003601f168201915b505050505081565b600081519050919050565b600082825260208201905092915050565b60005b838110156101165780820151818401526020810190506100fb565b83811115610125576000848401525b50505050565b6000601f19601f8301169050919050565b6000610147826100dc565b61015181856100e7565b93506101618185602086016100f8565b61016a8161012b565b840191505092915050565b6000602082019050818103600083015261018f818461013c565b905092915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b600060028204905060018216806101de57607f821691505b602082108114156101f2576101f1610197565b5b5091905056fea26469706673582212204d363ed34111d1be492d4fd086e9f2df62b3c625e89ade31f30e63201ed1e24f64736f6c63430008090033';

export const TRANSFER_EVENT_TOPIC = '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef';

0 comments on commit 5cb3063

Please sign in to comment.