mirror of
https://github.com/eosswedenorg/thalos
synced 2026-08-23 19:48:12 +02:00
abi.go: Get rid of global state by defining an AbiManager struct.
This commit is contained in:
parent
0ded3ee6fe
commit
45d1a468e8
3 changed files with 24 additions and 12 deletions
26
abi.go
26
abi.go
|
|
@ -1,7 +1,7 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
|
@ -12,29 +12,39 @@ import (
|
||||||
redis_cache "github.com/go-redis/cache/v8"
|
redis_cache "github.com/go-redis/cache/v8"
|
||||||
)
|
)
|
||||||
|
|
||||||
var abiCache *abi_cache.Cache
|
type AbiManager struct {
|
||||||
|
cache *abi_cache.Cache
|
||||||
|
api *eos.API
|
||||||
|
ctx context.Context
|
||||||
|
}
|
||||||
|
|
||||||
func InitAbiCache(id string) {
|
func NewAbiManager(api *eos.API, id string) *AbiManager {
|
||||||
// Init abi cache
|
// Init abi cache
|
||||||
abiCache = abi_cache.New("ship.cache."+id+".abi", &redis_cache.Options{
|
cache := abi_cache.New("ship.cache."+id+".abi", &redis_cache.Options{
|
||||||
Redis: redis.Client(),
|
Redis: redis.Client(),
|
||||||
// Cache 10k keys for 10 minutes.
|
// Cache 10k keys for 10 minutes.
|
||||||
LocalCache: redis_cache.NewTinyLFU(10000, 10*time.Minute),
|
LocalCache: redis_cache.NewTinyLFU(10000, 10*time.Minute),
|
||||||
})
|
})
|
||||||
|
|
||||||
|
return &AbiManager{
|
||||||
|
cache: cache,
|
||||||
|
api: api,
|
||||||
|
ctx: context.Background(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetAbi(account eos.AccountName) (*eos.ABI, error) {
|
func (mgr *AbiManager) GetAbi(account eos.AccountName) (*eos.ABI, error) {
|
||||||
key := string(account)
|
key := string(account)
|
||||||
|
|
||||||
abi, err := abiCache.Get(key)
|
abi, err := mgr.cache.Get(key)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
resp, err := eosClient.GetABI(eosClientCtx, account)
|
resp, err := mgr.api.GetABI(mgr.ctx, account)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("api: %s", err)
|
return nil, fmt.Errorf("api: %s", err)
|
||||||
}
|
}
|
||||||
abi = &resp.ABI
|
abi = &resp.ABI
|
||||||
|
|
||||||
err = abiCache.Set(key, abi, time.Hour)
|
err = mgr.cache.Set(key, abi, time.Hour)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("cache: %s", err)
|
return nil, fmt.Errorf("cache: %s", err)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
8
main.go
8
main.go
|
|
@ -35,6 +35,8 @@ var (
|
||||||
eosClientCtx = context.Background()
|
eosClientCtx = context.Background()
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var abi_mgr *AbiManager
|
||||||
|
|
||||||
var redisNs redis.Namespace
|
var redisNs redis.Namespace
|
||||||
|
|
||||||
// Reader states
|
// Reader states
|
||||||
|
|
@ -216,9 +218,6 @@ func main() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Init Abi cache
|
|
||||||
InitAbiCache(conf.Redis.CacheID)
|
|
||||||
|
|
||||||
// Connect client and get chain info.
|
// Connect client and get chain info.
|
||||||
log.Printf("Get chain info from api at: %s", conf.Api)
|
log.Printf("Get chain info from api at: %s", conf.Api)
|
||||||
eosClient = eos.New(conf.Api)
|
eosClient = eos.New(conf.Api)
|
||||||
|
|
@ -228,6 +227,9 @@ func main() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Init Abi cache
|
||||||
|
abi_mgr = abi.NewAbiManager(eosClient, conf.Redis.CacheID)
|
||||||
|
|
||||||
redisNs = redis.Namespace{
|
redisNs = redis.Namespace{
|
||||||
Prefix: conf.Redis.Prefix,
|
Prefix: conf.Redis.Prefix,
|
||||||
ChainID: chainInfo.ChainID.String(),
|
ChainID: chainInfo.ChainID.String(),
|
||||||
|
|
|
||||||
|
|
@ -91,7 +91,7 @@ func processTraces(traces []*ship.TransactionTraceV0) {
|
||||||
HexData: hex.EncodeToString(act_trace.Act.Data),
|
HexData: hex.EncodeToString(act_trace.Act.Data),
|
||||||
}
|
}
|
||||||
|
|
||||||
abi, err := GetAbi(act_trace.Act.Account)
|
abi, err := abi_mgr.GetAbi(act_trace.Act.Account)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
v, err := decodeAction(abi, act_trace.Act.Data, act_trace.Act.Name)
|
v, err := decodeAction(abi, act_trace.Act.Data, act_trace.Act.Name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue