+
Skip to content

add caching to ETHResolver #87

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

Merged
merged 8 commits into from
Jul 2, 2025
Merged

add caching to ETHResolver #87

merged 8 commits into from
Jul 2, 2025

Conversation

volodymyr-basiuk
Copy link
Contributor

@volodymyr-basiuk volodymyr-basiuk commented Jun 25, 2025

Adding caching to ETH Resolver similar to iden3/js-iden3-auth#105

Usage:

stateCache := cache.NewInMemoryCache[state.ResolvedState](10000, time.Hour)
 resolver := state.NewETHResolver("", "", &state.ResolverOptions{
   	StateCacheOptions: &state.CacheOptions{
   		Cache: stateCache,
   	},
   })

Copilot

This comment was marked as outdated.

@volodymyr-basiuk volodymyr-basiuk requested a review from Copilot June 25, 2025 13:32
Copilot

This comment was marked as outdated.

vmidyllic
vmidyllic previously approved these changes Jun 26, 2025
Kolezhniuk
Kolezhniuk previously approved these changes Jun 27, 2025
ilya-korotya
ilya-korotya previously approved these changes Jun 27, 2025
cache/cache.go Outdated
)

// ICache is a generic interface for a cache implementation.
type ICache[T any] interface {
Copy link
Contributor

Choose a reason for hiding this comment

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

I<Name> is not go style for interface naming, consider use Cache

cache/cache.go Outdated
}

// NewInMemoryCache creates a new in-memory cache with the specified size and default TTL.
func NewInMemoryCache[T any](size int64, defaultTTL time.Duration) ICache[T] {
Copy link
Contributor

Choose a reason for hiding this comment

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

Methods should return implementation of an interface not interface type.

cache/cache.go Outdated
}

// Set adds an item to the cache with a specified key and value and optional ttl.
func (c *inMemoryCache[T]) Set(key string, value T, ttl ...time.Duration) {
Copy link
Contributor

Choose a reason for hiding this comment

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


time.Sleep(200 * time.Millisecond)

_, ok := c.Get("short")
Copy link
Contributor

Choose a reason for hiding this comment

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

Use golang assert/require library:

require.False(t, ok)

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

Same. Please use assert library for these unit tests.

// NewETHResolver create ETH resolver for state.
func NewETHResolver(url, contract string) *ETHResolver {
// NewETHResolverWithErr create ETH resolver for state or return error.
func NewETHResolverWithErr(url, contract string, opts *ResolverOptions) (*ETHResolver, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's do the code more golang style.

  1. Rename the function to NewETHResolverWithOpts;
  2. Change function's signature to: NewETHResolverWithOpts(url, contract string, opts ...Options)
  3. Use golang options patter to pass options.

// Close closes the ETHResolver and its underlying client.
func (r *ETHResolver) Close() error {
if r.ethClient != nil {
r.ethClient.Close()
Copy link
Contributor

Choose a reason for hiding this comment

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

If the close function returns an error try to handle the error with deffer or just print some log

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No return value from client close function

ilya-korotya
ilya-korotya previously approved these changes Jul 2, 2025
// NewInMemoryCache creates a new in-memory cache with the specified size and default TTL.
func NewInMemoryCache[T any](size int64, defaultTTL time.Duration) ICache[T] {
func NewInMemoryCache[T any](size int64, defaultTTL time.Duration) *InMemoryCache[T] {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is possible to pass 0?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes

if !ok || val != "bar" {
t.Errorf("expected 'bar', got '%v', ok: %v", val, ok)
}
assert.True(t, ok, "expected 'foo' to be set")
Copy link
Contributor

Choose a reason for hiding this comment

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

user require instead of asset. No sense continue testing if first test was failed.

// ResolverOptions is the full config for ETHResolver
type ResolverOptions struct {
// ResolverOptions is a function that modifies the ResolverConfig.
type ResolverOptions func(*ResolverConfig)
Copy link
Contributor

Choose a reason for hiding this comment

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

No need add prefix Resolver. Because you will have prefix resolver when you will use the pacakge
For example

import resolver
resolver.Config

Bad example

resolver.ResolverConfig

@ilya-korotya ilya-korotya requested a review from Copilot July 2, 2025 11:41
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

Adds in-memory caching support to ETHResolver to avoid redundant blockchain calls and centralizes default TTL configurations.

  • Introduces CacheOptions and ResolverOptions for configurable state and root caches.
  • Modifies ETHResolver to check cache before on-chain resolution and store results with appropriate TTL.
  • Defines default TTL constants and adds unit tests for both resolver and generic cache behavior.

Reviewed Changes

Copilot reviewed 7 out of 8 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
state/resolver.go Added cache config types, integrated caching into resolve methods, and a Close method.
state/resolver-cache_test.go New tests verifying that Resolve and ResolveGlobalRoot use the cache when present.
pubsignals/verifyopts.go Switched hardcoded verify delays to use shared constants.
constants/constants.go Defined default cache TTL constants and grouping types.
cache/cache.go Introduced Cache interface and InMemoryCache using ccache.
cache/cache_test.go Comprehensive tests for set/get, eviction, and TTL behavior.
go.mod Added github.com/karlseguin/ccache/v3 dependency.
Comments suppressed due to low confidence (2)

state/resolver-cache_test.go:41

  • [nitpick] The test name refers to Gist but is exercising ResolveGlobalRoot. For clarity and consistency, rename it to TestResolveGlobalRoot_UsesCacheIfPresent.
func TestGistResolve_UsesCacheIfPresent(t *testing.T) {

state/resolver.go:39

  • [nitpick] The comment implies this function generates default values, but it actually applies the provided CacheOptions. Update the comment to reflect that it registers custom options.
// WithStateCacheOptions creates a new ResolverConfig with default values for state cache options.

@ilya-korotya
Copy link
Contributor

Use required instead of assert for all unit tests where no sense to continue testing after an error.

@volodymyr-basiuk volodymyr-basiuk merged commit 3827619 into main Jul 2, 2025
3 checks passed
@volodymyr-basiuk volodymyr-basiuk deleted the feat/caching branch July 2, 2025 14:56
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.

4 participants
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载