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

proposed additional QueryKeys API method to support getting events of different types in log index order #944

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

ettec
Copy link
Contributor

@ettec ettec commented Nov 18, 2024

Required to correctly replicate state from on chain contract that publishes multiple event types to offchain state.

For comment, tentatively agreed with @ilija42 pending inspiration from above.

@@ -65,6 +65,9 @@ type ContractReader interface {
// QueryKey provides fetching chain agnostic events (Sequence) with general querying capability.
QueryKey(ctx context.Context, contract BoundContract, filter query.KeyFilter, limitAndSort query.LimitAndSort, sequenceDataType any) ([]Sequence, error)

// QueryKeys provides fetching chain agnostic events of different types (SequenceWithKey) with general querying capability.
QueryKeys(ctx context.Context, contract BoundContract, filters []query.KeyFilter, limitAndSort query.LimitAndSort, sequenceDataType any) ([]SequenceWithKey, error)
Copy link
Collaborator

Choose a reason for hiding this comment

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

What about a map of Key -> Sequence?

Suggested change
QueryKeys(ctx context.Context, contract BoundContract, filters []query.KeyFilter, limitAndSort query.LimitAndSort, sequenceDataType any) ([]SequenceWithKey, error)
QueryKeys(ctx context.Context, contract BoundContract, filters []query.KeyFilter, limitAndSort query.LimitAndSort, sequenceDataType any) (map[string]Sequence, error)

If you are worried about ordering, could consider a sequence:

Suggested change
QueryKeys(ctx context.Context, contract BoundContract, filters []query.KeyFilter, limitAndSort query.LimitAndSort, sequenceDataType any) ([]SequenceWithKey, error)
QueryKeys(ctx context.Context, contract BoundContract, filters []query.KeyFilter, limitAndSort query.LimitAndSort, sequenceDataType any) (iter.Seq2[string, Sequence], error)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The problem with map[string]Sequence is it would lose the order, the key thing is that the order across the various event types needs to be maintained.

no strong feelings on using iter, however the result isn't streamed so no strong advantage to using iter either as far as I can see, and I think it would add a bit of complexity to the loop implementation. On balance my pref would be for []SequenceWithKey

Copy link
Collaborator

Choose a reason for hiding this comment

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

Streaming is not the only reason to use an iter.Seq, and this API is a layer above the GRPC protos. The advantage is that we can express what we need naturally and directly without declaring extra hyper-specific types. Another similar approach could be a generic entry/pair/etc.:

type Entry[K comparable, V any] struct {
	K K
	V V
}
Suggested change
QueryKeys(ctx context.Context, contract BoundContract, filters []query.KeyFilter, limitAndSort query.LimitAndSort, sequenceDataType any) ([]SequenceWithKey, error)
QueryKeys(ctx context.Context, contract BoundContract, filters []query.KeyFilter, limitAndSort query.LimitAndSort, sequenceDataType any) ([]Entry[string, Sequence], error)

Could also consider documenting via a type:

// SortedEntries is a slice of [Entry]s sorted by ascending keys.
type SortedEntries[K comparable, V any] []Entry[K, V]
Suggested change
QueryKeys(ctx context.Context, contract BoundContract, filters []query.KeyFilter, limitAndSort query.LimitAndSort, sequenceDataType any) ([]SequenceWithKey, error)
QueryKeys(ctx context.Context, contract BoundContract, filters []query.KeyFilter, limitAndSort query.LimitAndSort, sequenceDataType any) (SortedEntries[string, Sequence], error)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants