mirror of
https://github.com/eosswedenorg/thalos
synced 2026-06-16 04:24:56 +02:00
internal/abi/manager.go: Use config.AbiCache to configure the manager
This commit is contained in:
parent
b60436c48a
commit
ec40e954f2
3 changed files with 20 additions and 7 deletions
|
|
@ -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,
|
||||
)
|
||||
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue