release: v1.0.0-rc

This commit is contained in:
2024-09-05 09:48:59 +03:00
commit 30729e9318
50 changed files with 4404 additions and 0 deletions

62
internal/cache/cache.go vendored Normal file
View File

@@ -0,0 +1,62 @@
package cache
import (
"context"
"log/slog"
)
type (
Cache interface {
Add(context.Context, string) error
Remove(context.Context, string) error
Exists(context.Context, string) (bool, error)
Size(context.Context) (int64, error)
}
CacheOpts struct {
Logg *slog.Logger
RedisDSN string
CacheType string
}
)
func New(o CacheOpts) (Cache, error) {
var cache Cache
switch o.CacheType {
case "map":
cache = NewMapCache()
case "redis":
redisCache, err := NewRedisCache(redisOpts{
DSN: o.RedisDSN,
})
if err != nil {
return nil, err
}
cache = redisCache
default:
cache = NewMapCache()
o.Logg.Warn("invalid cache type, using default type (map)")
}
// geSmartContracts, err := o.Chain.Provider().GetGESmartContracts(
// context.Background(),
// o.Registries,
// )
// if err != nil {
// return nil, fmt.Errorf("cache could not bootstrap GE smart contracts: err %v", err)
// }
// for k, v := range geSmartContracts {
// cache.Add(k, v)
// }
// for _, address := range o.Watchlist {
// cache.Add(address, false)
// }
// for _, address := range o.Blacklist {
// cache.Remove(address)
// }
// o.Logg.Info("cache bootstrap complete", "cached_addresses", cache.Size())
return cache, nil
}

56
internal/cache/redis.go vendored Normal file
View File

@@ -0,0 +1,56 @@
package cache
import (
"context"
"github.com/redis/rueidis"
)
type (
redisOpts struct {
DSN string
}
redisCache struct {
client rueidis.Client
}
)
func NewRedisCache(o redisOpts) (Cache, error) {
client, err := rueidis.NewClient(rueidis.ClientOption{
InitAddress: []string{o.DSN},
})
if err != nil {
return nil, err
}
return &redisCache{
client: client,
}, nil
}
func (c *redisCache) Add(ctx context.Context, key string) error {
// Without NX it will overwrite any existing KEY
cmd := c.client.B().Set().Key(key).Value("true").Build()
return c.client.Do(ctx, cmd).Error()
}
func (c *redisCache) Remove(ctx context.Context, key string) error {
cmd := c.client.B().Del().Key(key).Build()
return c.client.Do(ctx, cmd).Error()
}
func (c *redisCache) Exists(ctx context.Context, key string) (bool, error) {
cmd := c.client.B().Exists().Key(key).Build()
res, err := c.client.Do(ctx, cmd).AsBool()
if err != nil {
return false, err
}
return res, nil
}
func (c *redisCache) Size(ctx context.Context) (int64, error) {
cmd := c.client.B().Dbsize().Build()
return c.client.Do(ctx, cmd).AsInt64()
}

36
internal/cache/xmap.go vendored Normal file
View File

@@ -0,0 +1,36 @@
package cache
import (
"context"
"github.com/puzpuzpuz/xsync/v3"
)
type mapCache struct {
xmap *xsync.Map
}
func NewMapCache() Cache {
return &mapCache{
xmap: xsync.NewMap(),
}
}
func (c *mapCache) Add(_ context.Context, key string) error {
c.xmap.Store(key, true)
return nil
}
func (c *mapCache) Remove(_ context.Context, key string) error {
c.xmap.Delete(key)
return nil
}
func (c *mapCache) Exists(_ context.Context, key string) (bool, error) {
_, ok := c.xmap.Load(key)
return ok, nil
}
func (c *mapCache) Size(_ context.Context) (int64, error) {
return int64(c.xmap.Size()), nil
}