Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Reject from > to in eth_newFilter and don't throw error when f… #1049

Merged
merged 1 commit into from
Apr 24, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 35 additions & 6 deletions web3/packages/api-server/src/methods/modules/eth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -919,9 +919,30 @@ export class Eth {

/* #region filter-related api methods */
async newFilter(args: [RpcFilterRequest]): Promise<HexString> {
const filterRequest: RpcFilterRequest = args[0];

const fromBlock = filterRequest.fromBlock;
const toBlock = filterRequest.toBlock;

// https://github.com/ethereum/go-ethereum/blob/v1.11.5/eth/filters/filter_system.go#L334
if (
typeof fromBlock === "string" &&
fromBlock.startsWith("0x") &&
typeof toBlock === "string" &&
toBlock.startsWith("0x") &&
fromBlock > toBlock
) {
throw new HeaderNotFoundError(
`invalid from and to block combination: from > to`
);
}

const tipLog: Log | null = await this.query.getTipLog();
const initialLogId: bigint = tipLog == null ? 0n : tipLog.id;
const filter_id = await this.filterManager.install(args[0], initialLogId);
const filter_id = await this.filterManager.install(
filterRequest,
initialLogId
);
return filter_id;
}

Expand Down Expand Up @@ -1018,8 +1039,13 @@ export class Eth {
return [];
} else {
const lastPollLogId = await this.filterManager.getLastPoll(filter_id);
// Don't check from > tipNumber here, for Geth also returns [] here.
const filterParams = await this._rpcFilterRequestToGetLogsParams(
filter,
false
);
const logs = await this.query.getLogsByFilter(
await this._rpcFilterRequestToGetLogsParams(filter, false),
filterParams,
lastPollLogId
);

Expand Down Expand Up @@ -1106,7 +1132,8 @@ export class Eth {
}

private async _parseBlockParameter(
blockParameter: BlockParameter
blockParameter: BlockParameter,
checkBiggerThanTipNumber: boolean = true
): Promise<GodwokenBlockParameter> {
switch (blockParameter) {
case "latest":
Expand Down Expand Up @@ -1152,7 +1179,7 @@ export class Eth {
? blockParameter.blockNumber
: (blockParameter as HexNumber);
const blockNumber: U64 = Uint64.fromHex(blockHexNum).getValue();
if (tipNumber < blockNumber) {
if (checkBiggerThanTipNumber && tipNumber < blockNumber) {
throw new HeaderNotFoundError();
}
return blockNumber;
Expand Down Expand Up @@ -1225,12 +1252,14 @@ export class Eth {
fromBlockParam = fromBlock ?? "latest";
}
const _fromBlock: bigint | undefined = await this._parseBlockParameter(
fromBlockParam
fromBlockParam,
false
);
normalizedFromBlock = _fromBlock ?? latestBlockNumber;

const _toBlock: bigint | undefined = await this._parseBlockParameter(
toBlock ?? "latest"
toBlock ?? "latest",
false
);
normalizedToBlock = _toBlock ?? latestBlockNumber;

Expand Down