From ec40e954f26d44a78ae5b206ce30e86f877ee931 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Sun, 21 Jul 2024 12:51:54 +0200 Subject: [PATCH] internal/abi/manager.go: Use config.AbiCache to configure the manager --- cmd/thalos/server.go | 6 +++--- internal/abi/manager.go | 7 +++++-- internal/abi/manager_test.go | 14 ++++++++++++-- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/cmd/thalos/server.go b/cmd/thalos/server.go index 44d0b0d..b009e0a 100644 --- a/cmd/thalos/server.go +++ b/cmd/thalos/server.go @@ -155,9 +155,9 @@ func LogLevels() []string { return list } -func initAbiManager(api *antelopeapi.Client, store cache.Store, chain_id string) *abi.AbiManager { +func initAbiManager(cfg *config.AbiCache, api *antelopeapi.Client, store cache.Store, chain_id string) *abi.AbiManager { cache := cache.NewCache("thalos::cache::abi::"+chain_id, store) - return abi.NewAbiManager(cache, api) + return abi.NewAbiManager(cfg, cache, api) } func stateLoader(conf *config.Config, start_block_flag *pflag.Flag, chainInfo func() *antelopeapi.Info, cache *cache.Cache, current_block_no_cache bool) StateLoader { @@ -386,7 +386,7 @@ func serverCmd(cmd *cobra.Command, args []string) { Prefix: conf.Redis.Prefix, ChainID: chain_id, }), - initAbiManager(antelopeClient, cacheStore, chain_id), + initAbiManager(&conf.AbiCache, antelopeClient, cacheStore, chain_id), codec, ) diff --git a/internal/abi/manager.go b/internal/abi/manager.go index 8d3acc3..3df1cdb 100644 --- a/internal/abi/manager.go +++ b/internal/abi/manager.go @@ -6,22 +6,25 @@ import ( "time" "github.com/eosswedenorg/thalos/internal/cache" + "github.com/eosswedenorg/thalos/internal/config" "github.com/shufflingpixels/antelope-go/api" "github.com/shufflingpixels/antelope-go/chain" ) // AbiManager handles an ABI cache that fetches the ABI from an API on cache miss. type AbiManager struct { + cfg *config.AbiCache cache *cache.Cache api *api.Client ctx context.Context } // Create a new ABI Manager -func NewAbiManager(cache *cache.Cache, api *api.Client) *AbiManager { +func NewAbiManager(cfg *config.AbiCache, cache *cache.Cache, api *api.Client) *AbiManager { return &AbiManager{ cache: cache, api: api, + cfg: cfg, ctx: context.Background(), } } @@ -38,7 +41,7 @@ func (mgr *AbiManager) SetAbi(account chain.Name, abi *chain.Abi) error { func (mgr *AbiManager) GetAbi(account chain.Name) (*chain.Abi, error) { var abi chain.Abi if err := mgr.cacheGet(account, &abi); err != nil { - ctx, cancel := context.WithTimeout(mgr.ctx, time.Second) + ctx, cancel := context.WithTimeout(mgr.ctx, mgr.cfg.ApiTimeout) defer cancel() resp, err := mgr.api.GetAbi(ctx, account.String()) if err != nil { diff --git a/internal/abi/manager_test.go b/internal/abi/manager_test.go index a523c6e..2606ec1 100644 --- a/internal/abi/manager_test.go +++ b/internal/abi/manager_test.go @@ -6,11 +6,13 @@ import ( "net/http" "net/http/httptest" "testing" + "time" "github.com/shufflingpixels/antelope-go/api" "github.com/shufflingpixels/antelope-go/chain" "github.com/eosswedenorg/thalos/internal/cache" + "github.com/eosswedenorg/thalos/internal/config" "github.com/stretchr/testify/assert" ) @@ -132,12 +134,16 @@ func mockAPI(handler http.HandlerFunc) (*api.Client, *httptest.Server) { } func TestManager_GetAbiFromCache(t *testing.T) { + cfg := &config.AbiCache{ + ApiTimeout: time.Second, + } + cache := cache.NewCache("thalos::cache::abi::test", cache.NewMemoryStore()) api, _ := mockAPI(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { })) - mgr := NewAbiManager(cache, api) + mgr := NewAbiManager(cfg, cache, api) abi := chain.Abi{} err := json.Unmarshal([]byte(abiString), &abi) @@ -152,6 +158,10 @@ func TestManager_GetAbiFromCache(t *testing.T) { } func TestManager_GetAbiFromAPI(t *testing.T) { + cfg := &config.AbiCache{ + ApiTimeout: time.Second, + } + cache := cache.NewCache("thalos::cache::abi::test", cache.NewMemoryStore()) api, _ := mockAPI(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { @@ -161,7 +171,7 @@ func TestManager_GetAbiFromAPI(t *testing.T) { assert.NoError(t, err) })) - mgr := NewAbiManager(cache, api) + mgr := NewAbiManager(cfg, cache, api) c_abi, err := mgr.GetAbi(chain.N("testaccount")) assert.NoError(t, err)