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

Event filtering using non/indexed string arguments #6167

Merged
merged 5 commits into from
Jun 9, 2023

Conversation

spacesailor24
Copy link
Contributor

@spacesailor24 spacesailor24 commented Jun 8, 2023

This PR fixes filter contract events using non-indexed string event arguments and adds support for filtering using indexed string event arguments

closes #5807

@spacesailor24 spacesailor24 added the 4.x 4.0 related label Jun 8, 2023
@spacesailor24 spacesailor24 self-assigned this Jun 8, 2023
)
: decodedLogs;

if (filterKeys.length > 0) {
Copy link
Contributor Author

@spacesailor24 spacesailor24 Jun 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On line 729:

const logs = await getLogs(this, { fromBlock, toBlock, topics, address }, returnFormat);

we submit an RPC request to get a list of events for a period of blocks that match topics i.e. the event signatures we're interested in. If topics === undefined then the RPC response will contain all the events that occurred during the block period. Previously this check was:

eventName === 'allEvents' && filterKeys.length > 0

to account for this scenario as we needed to perform the filtering on the all the events returned by the RPC response using the filter provided by the user. Usually in this scenario, the user is intending to get all the events within a block period with a specific event argument i.e. "I want all events during the block period where from event arugment was 0x0...". Otherwise, we skip the additional filtering because the events have already been filtered using the user's filter by the RPC client. However, this did not work when the user's filter was attempting to filter using a non-indexed string event argument as done in this test. By removing the check eventName === 'allEvents' for this line, we now perform this additional filtering on all events as long as some filter object was provided by the user. This adds support for filtering events using non-indexed and indexed string event arguments (i.e. the issue reported by #5807)

@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Jun 8, 2023

Deploying with  Cloudflare Pages  Cloudflare Pages

Latest commit: 2552a2a
Status: ✅  Deploy successful!
Preview URL: https://655f6dda.web3-js-docs.pages.dev
Branch Preview URL: https://wyatt-4-x-5807-contract-even.web3-js-docs.pages.dev

View logs

@github-actions
Copy link

github-actions bot commented Jun 8, 2023

Bundle Stats

Hey there, this message comes from a github action that helps you and reviewers to understand how these changes affect the size of this project's bundle.

As this PR is updated, I'll keep you updated on how the bundle size is impacted.

Total

Asset Old size New size Diff Diff %
Total 638 KB 638 KB 404 bytes 0.06%
View detailed bundle breakdown

Added

No assets were added

Removed

No assets were removed

Bigger

No assets were bigger

Smaller

No assets were smaller

Unchanged

Asset Old size New size Diff Diff %
web3.min.js 621 KB 621 KB 280 bytes 0.04%
../lib/commonjs/index.d.ts 8.44 KB 8.56 KB 124 bytes 1.43%
../lib/commonjs/accounts.d.ts 3.67 KB 3.67 KB 0 0.00%
../lib/commonjs/types.d.ts 2.37 KB 2.37 KB 0 0.00%
../lib/commonjs/abi.d.ts 1000 bytes 1000 bytes 0 0.00%
../lib/commonjs/web3.d.ts 811 bytes 811 bytes 0 0.00%
../lib/commonjs/eth.exports.d.ts 280 bytes 280 bytes 0 0.00%
../lib/commonjs/providers.exports.d.ts 148 bytes 148 bytes 0 0.00%
../lib/commonjs/version.d.ts 60 bytes 60 bytes 0 0.00%

@codecov
Copy link

codecov bot commented Jun 8, 2023

Codecov Report

Merging #6167 (2552a2a) into 4.x (4358140) will increase coverage by 0.09%.
The diff coverage is 90.00%.

Additional details and impacted files
@@            Coverage Diff             @@
##              4.x    #6167      +/-   ##
==========================================
+ Coverage   87.37%   87.46%   +0.09%     
==========================================
  Files         197      197              
  Lines        7548     7557       +9     
  Branches     2059     2060       +1     
==========================================
+ Hits         6595     6610      +15     
+ Misses        953      947       -6     
Flag Coverage Δ
UnitTests 87.46% <90.00%> (+0.09%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
web3 ∅ <ø> (∅)
web3-core ∅ <ø> (∅)
web3-errors ∅ <ø> (∅)
web3-eth ∅ <ø> (∅)
web3-eth-abi ∅ <ø> (∅)
web3-eth-accounts ∅ <ø> (∅)
web3-eth-contract ∅ <ø> (∅)
web3-eth-ens ∅ <ø> (∅)
web3-eth-iban ∅ <ø> (∅)
web3-eth-personal ∅ <ø> (∅)
web3-net ∅ <ø> (∅)
web3-providers-http ∅ <ø> (∅)
web3-providers-ipc ∅ <ø> (∅)
web3-providers-ws ∅ <ø> (∅)
web3-rpc-methods ∅ <ø> (∅)
web3-utils ∅ <ø> (∅)
web3-validator ∅ <ø> (∅)

Comment on lines +752 to +756
const inputAbi = abi.inputs?.filter(input => input.name === key)[0];
if (inputAbi?.indexed && inputAbi.type === 'string') {
const hashedIndexedString = keccak256(filter[key] as string);
if (hashedIndexedString === String(log.returnValues[key])) return true;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This added logic adds support for filtering indexed string event arguments. Because all Solidity event topics (i.e. indexed event arguments) have to be 32 bytes, anytime a string is indexed for an event, it's value is represented by the keccak256 hash of the string value

Comment on lines +23 to +25
const _decodeParameter = (inputType: string, clonedTopic: string) =>
inputType === 'string' ? clonedTopic : decodeParameter(inputType, clonedTopic);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When a indexed event input is of type string, we know that it's value is going to be the keccak256 hash of the original string value, so this change returns the hash instead of trying to decode it

@spacesailor24 spacesailor24 changed the title Wyatt/4.x/5807 contract events fix Event filtering using non/indexed string arguments Jun 8, 2023
@spacesailor24 spacesailor24 marked this pull request as ready for review June 8, 2023 08:23
Copy link
Contributor

@Muhammad-Altabba Muhammad-Altabba left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @spacesailor24 ,
And can we have unit tests for the added logic so the test coverage does not decrease?

Copy link
Contributor

@jdevcs jdevcs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, except some existing change requests.

@spacesailor24 spacesailor24 merged commit 3e7f004 into 4.x Jun 9, 2023
@spacesailor24 spacesailor24 deleted the wyatt/4.x/5807-contract-events-fix branch June 9, 2023 00:17
@jdevcs jdevcs mentioned this pull request Jun 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
4.x 4.0 related
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Ensure filtering works for strings on events
4 participants