Skip to content

Commit

Permalink
use tm.logger to be compatible with exocore
Browse files Browse the repository at this point in the history
  • Loading branch information
leonz789 committed Dec 11, 2024
1 parent af8fbc4 commit 72b02c5
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 38 deletions.
45 changes: 24 additions & 21 deletions cmd/feeder_tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,33 +28,38 @@ func RunPriceFeeder(conf feedertypes.Config, logger feedertypes.LoggerInf, mnemo
panic("logger is not initialized")
}

// init exoclient
cc, err := exoclient.Init(conf, mnemonic, privFile, standalone)
if err != nil {
logger.Error("failed to init exoclient", "error", err)
panic(err)
}
defer cc.Close()

// init fetcher, start fetchers to get prices from sources
f, err := fetcher.Init(conf.Sources, conf.Tokens, sourcesPath)
if err != nil {
logger.Error("failed to init fetcher", "error", err)
panic(err)
}

// start all supported sources and tokens
// start fetching on all supported sources and tokens
_ = f.StartAll()

// query oracle params
// init exoclient
cc, err := exoclient.Init(conf, mnemonic, privFile, standalone)
if err != nil {
logger.Error("failed to init exoclient", "error", err)
panic(err)
}
defer cc.Close()
// initialize oracle params by querying from exocore
oracleP, err := exoclient.GetParams(cc)
for err != nil {
// retry forever until be interrupted manually
logger.Error("Fail to get oracle params on star, retrying...", err)
logger.Error("Failed to get oracle params on start, retrying...", err)
time.Sleep(2 * time.Second)
oracleP, err = exoclient.GetParams(cc)
}

// TODO: currently the getStakerInfos will return nil error when empty response, avoid infinite loop
// initialize staker's validator list for eth-native-restaking
nativeStakers := initializeNativeRestakingStakers(cc)
for nativeToken, stakerInfos := range nativeStakers {
f.ResetStakerValidatorsForAll(nativeToken, stakerInfos)
}

oracleParamsFeedingTokens := make(map[string]struct{})
runningFeeders := make(map[int64]*feederInfo)
// remainingFeeders used to track token feeders that have been set in oracle params but with no configuration from price-feeder for price fetching, and if the lenght of map is bigger than 0, price-feeder will try continuously to reload the configure file until those feeders are able to work
Expand Down Expand Up @@ -119,12 +124,6 @@ func RunPriceFeeder(conf feedertypes.Config, logger feedertypes.LoggerInf, mnemo
go reloadConfigToFetchNewTokens(remainningFeeders, newFeeder, cc, f)
}

// initialize staker's validator list for eth-native-restaking
nativeStakers := initializeNativeRestakingStakers(cc)
for nativeToken, stakerInfos := range nativeStakers {
f.ResetStakerValidatorsForAll(nativeToken, stakerInfos)
}

// subscribe newBlock to to trigger tx
res, _ := exoclient.Subscriber(conf.Exocore.Ws.Addr, conf.Exocore.Ws.Endpoint)

Expand All @@ -141,7 +140,7 @@ func RunPriceFeeder(conf feedertypes.Config, logger feedertypes.LoggerInf, mnemo
oracleP, err = exoclient.GetParams(cc)
for err != nil {
// retry forever until be interrupted manually
logger.Error("Fail to get oracle params when params update detected, retrying...", "error", err)
logger.Error("Failed to get oracle params when params update detected, retrying...", "error", err)
oracleP, err = exoclient.GetParams(cc)
time.Sleep(2 * time.Second)
}
Expand All @@ -162,7 +161,7 @@ func RunPriceFeeder(conf feedertypes.Config, logger feedertypes.LoggerInf, mnemo
if success := f.UpdateNativeTokenValidators(types.NativeTokenETH, r.NativeETH); !success {
stakerInfos, err := exoclient.GetStakerInfos(cc, types.NativeTokenETHAssetID)
for err != nil {
logger.Error("Fail to get stakerInfos, retrying...")
logger.Error("Failed to get stakerInfos, retrying...")
stakerInfos, err = exoclient.GetStakerInfos(cc, types.NativeTokenETHAssetID)
time.Sleep(2 * time.Second)
}
Expand Down Expand Up @@ -315,6 +314,10 @@ func feedToken(fInfo *feederInfo, cc *grpc.ClientConn, f *fetcher.Fetcher, conf
logger.Info("price not changed, skip submitting price", "roundID", roundID, "feederID", feederID)
continue
}
if len(p.Price) == 0 {
logger.Info("price has not been fetched yet, skip submitting price", "roundID", roundID, "feederID", feederID)
continue
}
basedBlock := t.height - delta

if !(fInfo.params.tokenName == types.NativeTokenETH) {
Expand Down Expand Up @@ -389,7 +392,7 @@ func initializeNativeRestakingStakers(cc *grpc.ClientConn) map[string][]*oracleT
for _, v := range types.NativeRestakings {
stakerInfos, err := exoclient.GetStakerInfos(cc, types.AssetIDMap[v[1]])
for err != nil {
logger.Error("Fail to get stakerInfos, retrying...", "error", err)
logger.Error("Failed to get stakerInfos, retrying...", "error", err)
stakerInfos, err = exoclient.GetStakerInfos(cc, types.NativeTokenETHAssetID)
time.Sleep(2 * time.Second)
}
Expand Down
4 changes: 2 additions & 2 deletions exoclient/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,13 @@ func Init(conf feedertypes.Config, mnemonic, privFile string, standalone bool) (
file, err := os.Open(path.Join(confSender.Path, privFile))
if err != nil {
logger.Error("failed to load privatekey from local path", "path", privFile, "error", err)
return nil, feedertypes.ErrInitFail.Wrap(fmt.Sprintf("fail to open consensuskey file, %v", err))
return nil, feedertypes.ErrInitFail.Wrap(fmt.Sprintf("failed to open consensuskey file, %v", err))
}
defer file.Close()
var privKey feedertypes.PrivValidatorKey
if err := json.NewDecoder(file).Decode(&privKey); err != nil {
logger.Error("failed to parse consensuskey from json file", "error", err)
return nil, feedertypes.ErrInitFail.Wrap(fmt.Sprintf("fail to parse consensuskey from json file %v", err))
return nil, feedertypes.ErrInitFail.Wrap(fmt.Sprintf("failed to parse consensuskey from json file %v", err))
}
privBase64 = privKey.PrivKey.Value
} else if !bip39.IsMnemonicValid(mnemonic) {
Expand Down
2 changes: 1 addition & 1 deletion external/feeder.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
var conf feedertypes.Config

// LoadConf set conf from invoker instead of rootCmd
func StartPriceFeeder(cfgFile, mnemonic, sourcesPath string, logger feedertypes.Logger) bool {
func StartPriceFeeder(cfgFile, mnemonic, sourcesPath string, logger feedertypes.LoggerInf) bool {
if len(cfgFile) == 0 {
return false
}
Expand Down
2 changes: 1 addition & 1 deletion fetcher/beaconchain/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func initBeaconchain(confPath string) error {
// parse nstID by splitting it
nstID := strings.Split(cfg.NSTID, "_")
if len(nstID) != 2 {
logger.Error("invalid nstID format, should be: x_y")
logger.Error("invalid nstID format, should be: x_y", "nstID", nstID)
return feedertypes.ErrInitFail.Wrap("invalid nstID format")
}
// the second element is the lzID of the chain
Expand Down
5 changes: 2 additions & 3 deletions fetcher/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package fetcher
import (
"context"
"fmt"
"log"
"reflect"
"strconv"
"strings"
Expand Down Expand Up @@ -177,7 +176,7 @@ func (s *source) AddToken(name string) bool {
prevPrice := priceInfo.GetInfo()
if err == nil && (prevPrice.Price != price.Price || prevPrice.Decimal != price.Decimal) {
priceInfo.UpdateInfo(price)
log.Printf("update token:%s, price:%s, decimal:%d, len(bytes):%d", tName, price.Price, price.Decimal, len(price.Price))
logger.Info("update token price", "token", tName, "price", price)
}
case <-s.stopCh:
if zero := s.running.Dec(); zero == 0 {
Expand Down Expand Up @@ -209,7 +208,7 @@ func (s *source) Fetch(interval time.Duration) {
prevPrice := priceInfo.GetInfo()
if err == nil && (prevPrice.Price != price.Price || prevPrice.Decimal != price.Decimal) {
priceInfo.UpdateInfo(price)
log.Printf("update token:%s, price:%s, decimal:%d", tName, price.Price, price.Decimal)
logger.Info("update token price", "token", tName, "price", price)
}
case <-s.stopCh:
if zero := s.running.Dec(); zero == 0 {
Expand Down
16 changes: 6 additions & 10 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"

"github.com/cometbft/cometbft/libs/log"
"github.com/spf13/viper"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
Expand All @@ -16,15 +17,9 @@ type PrivValidatorKey struct {
} `json:"priv_key"`
}

type LoggerInf interface {
Info(msg string, keyvals ...interface{})
Debug(msg string, keyvals ...interface{})
Error(msg string, keyvals ...interface{})
type LoggerInf log.Logger

With(keyvals ...interface{}) LoggerInf
}

var logger LoggerInf = NewLogger(zapcore.InfoLevel)
var logger log.Logger = NewLogger(zapcore.InfoLevel)

type LoggerWrapper struct {
*zap.SugaredLogger
Expand All @@ -39,7 +34,8 @@ func (l *LoggerWrapper) Debug(msg string, keyvals ...interface{}) {
func (l *LoggerWrapper) Error(msg string, keyvals ...interface{}) {
l.Errorw(msg, keyvals...)
}
func (l *LoggerWrapper) With(keyvals ...interface{}) LoggerInf {

func (l *LoggerWrapper) With(keyvals ...interface{}) log.Logger {
return &LoggerWrapper{
l.SugaredLogger.With(keyvals...),
}
Expand Down Expand Up @@ -138,7 +134,7 @@ var (
SourcesConfigPath string
v *viper.Viper

ErrInitFail = NewErr("Faile to initialization")
ErrInitFail = NewErr("Failed to initialization")
)

// InitConfig will only read path cfgFile once, and for reload after InitConfig, should use ReloadConfig
Expand Down

0 comments on commit 72b02c5

Please sign in to comment.