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: add max results error and default raw codec for eth_getLogs #12671

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

akaladarshi
Copy link
Contributor

Related Issues

#12636

Proposed Changes

  • Adds raw codec while filtering ethereum logs
  • Error out if we hit max result

Checklist

Before you mark the PR ready for review, please make sure that:

@@ -563,6 +566,9 @@ func makePrefillFilterQuery(f *EventFilter) ([]any, string, error) {
clauses = append(clauses, "("+strings.Join(subclauses, " OR ")+")")
}
}
} else {
clauses = append(clauses, "ee.codec=?")
values = append(values, uint64(multicodec.Raw))
Copy link
Member

Choose a reason for hiding this comment

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

This won't work because we call this method from both the eth APIs and also GetActorEventsRaw and SubscribeActorEventsRaw and these APIs want any codec if they don't specify a KeysWithCodec.

I think what you might need to do is extend EventFilter to add a OnlyRaw, or WithCodec or something like that so that when this gets called from the Eth APIs, that can be set so we can restrict to Raw only.

Copy link
Contributor Author

@akaladarshi akaladarshi Nov 7, 2024

Choose a reason for hiding this comment

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

Yeah while testing I realised this. So after going over the code I agree with you on extending the EventFilter.

I am in favour of adding an optional field in EventFilter suggesting, filtering based on this codec if keys are not available.

Codec *uint64 // Optional codec filter, used only if keys are not available

Note: We can also use array of codecs, but I don't think it will be useful because we only use two codecs (CBOR and RAW) to filter from.

Copy link
Member

Choose a reason for hiding this comment

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

yeah, for now just Codec is fine, "only used if KeysWithCodec is not set"

@akaladarshi akaladarshi marked this pull request as ready for review November 7, 2024 09:01
@@ -27,7 +27,7 @@
- Try harder in the F3 participation loop to participate using the same lotus node ([filecoin-project/lotus#12664](https://github.com/filecoin-project/lotus/pull/12664)).
- The mining loop will now correctly "stick" to the same upstream lotus node for all operations pertaining to mining a single block ([filecoin-project/lotus#12665](https://github.com/filecoin-project/lotus/pull/12665)).
- Make the ordering of event output for `eth_` APIs and `GetActorEventsRaw` consistent, sorting ascending on: epoch, message index, event index and original event entry order. ([filecoin-project/lotus#12623](https://github.com/filecoin-project/lotus/pull/12623))

- Return error after hitting max results in event filtering and add default `raw` codec for eth_getLogs. ([filecoin-project/lotus#12671](https://github.com/filecoin-project/lotus/pull/12671))
Copy link
Member

@rvagg rvagg Nov 18, 2024

Choose a reason for hiding this comment

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

Suggested change
- Return error after hitting max results in event filtering and add default `raw` codec for eth_getLogs. ([filecoin-project/lotus#12671](https://github.com/filecoin-project/lotus/pull/12671))
- Event APIs (`GetActorEventsRaw`, `SubscribeActorEventsRaw`, `eth_getLogs`, `eth_newFilter`, etc.) will now return an error when a request matches more than `MaxFilterResults` (default: 10,000) rather than silently truncating the results. Also apply an internal event matcher for `eth_getLogs` (etc.) to avoid builtin actor events on database query so as not to include them in `MaxFilterResults` calculation. ([filecoin-project/lotus#12671](https://github.com/filecoin-project/lotus/pull/12671))

Try to think about the change from a user perspective:

  1. They primarily want to know what impact it has on them, so speaking in terms of API is usually best if we're touching that
  2. They're generally not knowledgeable about internal details, and the raw vs cbor thing is pretty esoteric, so we can side-step that.

@@ -22,6 +22,8 @@ import (
"github.com/filecoin-project/lotus/chain/types"
)

var ErrMaxResultsReached = xerrors.New("max results limit reached, results truncated")
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
var ErrMaxResultsReached = xerrors.New("max results limit reached, results truncated")
var ErrMaxResultsReached = fmt.Errorf("filter matches too many events, try a more restricted filter")

Comment on lines +1771 to 1785
var codec uint64
// if no keys are specified, we can use the codec filter to make sure we only get `raw` encoded events
if len(pf.keys) == 0 {
codec = uint64(multicodec.Raw)
}

ef := &index.EventFilter{
MinHeight: pf.minHeight,
MaxHeight: pf.maxHeight,
TipsetCid: pf.tipsetCid,
Addresses: pf.addresses,
KeysWithCodec: pf.keys,
Codec: codec,
MaxResults: e.EventFilterManager.MaxFilterResults,
}
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
var codec uint64
// if no keys are specified, we can use the codec filter to make sure we only get `raw` encoded events
if len(pf.keys) == 0 {
codec = uint64(multicodec.Raw)
}
ef := &index.EventFilter{
MinHeight: pf.minHeight,
MaxHeight: pf.maxHeight,
TipsetCid: pf.tipsetCid,
Addresses: pf.addresses,
KeysWithCodec: pf.keys,
Codec: codec,
MaxResults: e.EventFilterManager.MaxFilterResults,
}
ef := &index.EventFilter{
MinHeight: pf.minHeight,
MaxHeight: pf.maxHeight,
TipsetCid: pf.tipsetCid,
Addresses: pf.addresses,
KeysWithCodec: pf.keys,
Codec: multicodec.Raw,
MaxResults: e.EventFilterManager.MaxFilterResults,
}

you can avoid the conditional, it's always Raw regardless. Look in keysToKeysWithCodec and you'll see even there if they supply a key then it's going to be set to Raw anyway.

Copy link
Contributor Author

@akaladarshi akaladarshi Nov 18, 2024

Choose a reason for hiding this comment

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

I was thinking, since Codec is an optional field, so we should set only in certain condition (pf.keys is not there).

@rvagg Let me know what you think about the above, otherwise I will make your suggested changes.

Copy link
Member

Choose a reason for hiding this comment

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

I think that regardless of what KeysWithCodec contains, we're safe to always set it to Raw. Which is fine since the contract says that it's only going to be used if KeysWithCodec is not set.

But also, 0 is a legitimate multicodec code too, so you're even setting it by not setting it. If we want to make it truly optional then it should probably be a pointer, but that gets ugly and it's really unnecessary for this.

I think if it was a public-facing API then we might want to be a bit more careful with the layout of it. Consider, for example, we have TipsetCid conflicting with MinHeight and MaxHeight - if you supply the former then the latter two won't be read. If this were a public API maybe we would come up with a better union representation for this.

@@ -47,6 +47,8 @@ type EventFilter struct {

KeysWithCodec map[string][]types.ActorEventBlock // map of key names to a list of alternate values that may match
MaxResults int // maximum number of results to collect, 0 is unlimited

Codec uint64 // optional codec filter, only used if KeysWithCodec is not set
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Codec uint64 // optional codec filter, only used if KeysWithCodec is not set
Codec multicodec.Code // optional codec filter, only used if KeysWithCodec is not set

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: ⌨️ In Progress
Development

Successfully merging this pull request may close these issues.

2 participants