-
Notifications
You must be signed in to change notification settings - Fork 5
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
Conversation
cache/cache.go
Outdated
) | ||
|
||
// ICache is a generic interface for a cache implementation. | ||
type ICache[T any] interface { |
There was a problem hiding this comment.
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] { |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use golang optionals patter: https://hayageek.com/optional-parameters-in-go/
|
||
time.Sleep(200 * time.Millisecond) | ||
|
||
_, ok := c.Get("short") |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The pkg is already imported: https://github.com/iden3/go-iden3-auth/blob/main/go.mod#L21
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rewrite all tests to use the pkg https://github.com/iden3/go-iden3-auth/blob/main/go.mod#L21
state/resolver-cache_test.go
Outdated
There was a problem hiding this comment.
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.
state/resolver.go
Outdated
// 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) { |
There was a problem hiding this comment.
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.
- Rename the function to
NewETHResolverWithOpts
; - Change function's signature to:
NewETHResolverWithOpts(url, contract string, opts ...Options)
- 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() |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
5ab2214
// 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] { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes
cache/cache_test.go
Outdated
if !ok || val != "bar" { | ||
t.Errorf("expected 'bar', got '%v', ok: %v", val, ok) | ||
} | ||
assert.True(t, ok, "expected 'foo' to be set") |
There was a problem hiding this comment.
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.
state/resolver.go
Outdated
// ResolverOptions is the full config for ETHResolver | ||
type ResolverOptions struct { | ||
// ResolverOptions is a function that modifies the ResolverConfig. | ||
type ResolverOptions func(*ResolverConfig) |
There was a problem hiding this comment.
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
There was a problem hiding this 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
andResolverOptions
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 exercisingResolveGlobalRoot
. For clarity and consistency, rename it toTestResolveGlobalRoot_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.
Use |
Adding caching to ETH Resolver similar to iden3/js-iden3-auth#105
Usage: