Skip to content

Commit

Permalink
Merge branch 'logging_improvement' into start_configured_plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason-Zhangxin-Chen committed Dec 21, 2023
2 parents dd5a5c2 + bd07764 commit 52037a3
Show file tree
Hide file tree
Showing 14 changed files with 49 additions and 82 deletions.
12 changes: 6 additions & 6 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var (

const Version = "v0.1.5"
const UsageOracleKey = "Set the oracle server key file path."
const UsagePluginConf = "Set the plugins' configuration file path."
const UsagePluginConf = "Set the plugin's configuration file path."
const UsagePluginDir = "Set the directory path of the data plugins."
const UsageOracleKeyPassword = "Set the password to decrypt oracle server key file."
const UsageGasTipCap = "Set the gas priority fee cap to issue the oracle data report transactions."
Expand Down Expand Up @@ -59,7 +59,7 @@ func MakeConfig() *types.OracleServiceConfig {
if lvl, presented := os.LookupEnv(types.EnvLogLevel); presented {
l, err := strconv.Atoi(lvl)
if err != nil {
log.Printf("Wrong log level configed in $LOG_LEVEL")
log.Printf("wrong log level configed in $LOG_LEVEL")
helpers.PrintUsage()
os.Exit(1)
}
Expand Down Expand Up @@ -89,7 +89,7 @@ func MakeConfig() *types.OracleServiceConfig {
if capGasTip, presented := os.LookupEnv(types.EnvGasTipCap); presented {
gasTip, err := strconv.ParseUint(capGasTip, 0, 64)
if err != nil {
log.Printf("Wrong value configed in $GAS_TIP_CAP")
log.Printf("wrong value configed in $GAS_TIP_CAP")
helpers.PrintUsage()
os.Exit(1)
}
Expand All @@ -99,20 +99,20 @@ func MakeConfig() *types.OracleServiceConfig {
// verify configurations.
keyJson, err := os.ReadFile(keyFile)
if err != nil {
log.Printf("Cannot read key from oracle key file: %s, %s", keyFile, err.Error())
log.Printf("cannot read key from oracle key file: %s, %s", keyFile, err.Error())
helpers.PrintUsage()
os.Exit(1)
}

key, err := keystore.DecryptKey(keyJson, keyPassword)
if err != nil {
log.Printf("Cannot decrypt oracle key file: %s, with the provided password!", keyFile)
log.Printf("cannot decrypt oracle key file: %s, with the provided password!", keyFile)
helpers.PrintUsage()
os.Exit(1)
}

if hclog.Level(logLevel) < hclog.NoLevel || hclog.Level(logLevel) > hclog.Error {
log.Printf("Wrong logging level configed %d, %s", logLevel, UsageLogLevel)
log.Printf("wrong logging level configed %d, %s", logLevel, UsageLogLevel)
helpers.PrintUsage()
os.Exit(1)
}
Expand Down
6 changes: 3 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ func main() { //nolint
dialer := &types.L1Dialer{}
client, err := dialer.Dial(conf.AutonityWSUrl)
if err != nil {
log.Printf("Cannot connect to Autonity network via web socket: %s", err.Error())
log.Printf("cannot connect to Autonity network via web socket: %s", err.Error())
helpers.PrintUsage()
os.Exit(1)
}

oc, err := contract.NewOracle(types.OracleContractAddress, client)
if err != nil {
log.Printf("Cannot bind to oracle contract in Autonity network via web socket: %s", err.Error())
log.Printf("cannot bind to oracle contract in Autonity network via web socket: %s", err.Error())
helpers.PrintUsage()
os.Exit(1)
}
Expand All @@ -45,5 +45,5 @@ func main() { //nolint
// kill -9 is syscall.SIGKILL but can't be caught, so don't need to add it
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
log.Println("Shutting down oracle server...")
log.Println("shutting down oracle server...")
}
54 changes: 27 additions & 27 deletions oracle_server/oracle_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func NewOracleServer(conf *types.OracleServiceConfig, dialer types.Dialer, clien
binaries, err := helpers.ListPlugins(conf.PluginDIR)
if len(binaries) == 0 || err != nil {
// to stop the service on the start once there is no plugin in the db.
os.logger.Error("No plugin discovered", "plugin-dir", os.pluginDIR)
os.logger.Error("no plugin discovered", "plugin-dir", os.pluginDIR)
helpers.PrintUsage()
o.Exit(1)
}
Expand All @@ -126,7 +126,7 @@ func NewOracleServer(conf *types.OracleServiceConfig, dialer types.Dialer, clien
os.loadNewPlugin(f, pConf)
}

os.logger.Info("Running oracle contract listener at", "WS", conf.AutonityWSUrl, "ID", conf.Key.Address.String())
os.logger.Info("running oracle contract listener at", "WS", conf.AutonityWSUrl, "ID", conf.Key.Address.String())
err = os.syncStates()
if err != nil {
// stop the client on start up once the remote endpoint of autonity L1 network is not ready.
Expand Down Expand Up @@ -154,15 +154,15 @@ func (os *OracleServer) syncStates() error {
os.chRoundEvent = make(chan *contract.OracleNewRound)
os.subRoundEvent, err = os.oracleContract.WatchNewRound(new(bind.WatchOpts), os.chRoundEvent)
if err != nil {
os.logger.Error("WatchNewRound", "error", err.Error())
os.logger.Error("failed to subscribe round event", "error", err.Error())
return err
}

// subscribe on-chain symbol update event
os.chSymbolsEvent = make(chan *contract.OracleNewSymbols)
os.subSymbolsEvent, err = os.oracleContract.WatchNewSymbols(new(bind.WatchOpts), os.chSymbolsEvent)
if err != nil {
os.logger.Error("WatchNewSymbols", "error", err.Error())
os.logger.Error("failed to subscribe new symbol event", "error", err.Error())
return err
}

Expand All @@ -175,30 +175,30 @@ func (os *OracleServer) initStates() (uint64, []string, decimal.Decimal, uint64,
// on the startup, we need to sync the round id, symbols and committees from contract.
currentRound, err := os.oracleContract.GetRound(nil)
if err != nil {
os.logger.Error("Get round", "error", err.Error())
os.logger.Error("get round", "error", err.Error())
return 0, nil, precision, 0, err
}

symbols, err := os.oracleContract.GetSymbols(nil)
if err != nil {
os.logger.Error("Get symbols", "error", err.Error())
os.logger.Error("get symbols", "error", err.Error())
return 0, nil, precision, 0, err
}

p, err := os.oracleContract.GetPrecision(nil)
if err != nil {
os.logger.Error("Get precision", "error", err.Error())
os.logger.Error("get precision", "error", err.Error())
return 0, nil, precision, 0, err
}

votePeriod, err := os.oracleContract.GetVotePeriod(nil)
if err != nil {
os.logger.Error("Get vote period", "error", err.Error())
os.logger.Error("get vote period", "error", err.Error())
return 0, nil, precision, 0, nil
}

if len(symbols) == 0 {
os.logger.Error("There are no symbols in Autonity L1 oracle contract")
os.logger.Error("there are no symbols in Autonity L1 oracle contract")
return currentRound.Uint64(), symbols, decimal.NewFromInt(p.Int64()), votePeriod.Uint64(), types.ErrNoSymbolsObserved
}

Expand Down Expand Up @@ -254,7 +254,7 @@ func (os *OracleServer) checkHealth() {
func (os *OracleServer) isVoter() (bool, error) {
voters, err := os.oracleContract.GetVoters(nil)
if err != nil {
os.logger.Error("Get voters", "error", err.Error())
os.logger.Error("get voters", "error", err.Error())
return false, err
}

Expand All @@ -270,18 +270,18 @@ func (os *OracleServer) printLatestRoundData(newRound uint64) {
for _, s := range os.protocolSymbols {
rd, err := os.oracleContract.GetRoundData(nil, new(big.Int).SetUint64(newRound-1), s)
if err != nil {
os.logger.Error("GetRoundData", "error", err.Error())
os.logger.Error("get round data", "error", err.Error())
return
}

os.logger.Debug("GetRoundPrice", "round", newRound-1, "symbol", s, "Price",
os.logger.Debug("get round price", "round", newRound-1, "symbol", s, "Price",
rd.Price.String(), "status", rd.Status.String())
}

for _, s := range os.protocolSymbols {
rd, err := os.oracleContract.LatestRoundData(nil, s)
if err != nil {
os.logger.Error("GetLatestRoundPrice", "error", err.Error())
os.logger.Error("get latest round price", "error", err.Error())
return
}

Expand All @@ -290,7 +290,7 @@ func (os *OracleServer) printLatestRoundData(newRound uint64) {
continue
}

os.logger.Debug("LatestRoundPrice", "round", rd.Round.Uint64(), "symbol", s, "price",
os.logger.Debug("latest round price", "round", rd.Round.Uint64(), "symbol", s, "price",
price.Div(os.pricePrecision).String(), "status", rd.Status.String())
}
}
Expand All @@ -314,7 +314,7 @@ func (os *OracleServer) handlePreSampling(preSampleTS int64) error {
}

// do the data pre-sampling.
os.logger.Debug("Data pre-sampling", "on height", curHeight, "TS", preSampleTS)
os.logger.Debug("data pre-sampling", "on height", curHeight, "TS", preSampleTS)
os.samplePrice(os.symbols, preSampleTS)

return nil
Expand Down Expand Up @@ -352,7 +352,7 @@ func (os *OracleServer) handleRoundVote() error {
// query last round's prices, its random salt which will reveal last round's report.
lastRoundData, ok := os.roundData[os.curRound-1]
if !ok {
os.logger.Info("Cannot find last round's data, reports with commitment hash and no data")
os.logger.Info("cannot find last round's data, reports with commitment hash and no data")
}

// if node is no longer a validator, and it doesn't have last round data, skip reporting.
Expand Down Expand Up @@ -395,7 +395,7 @@ func (os *OracleServer) reportWithCommitment(newRound uint64, lastRoundData *typ
return err
}

os.logger.Info("oracle server account left fund", "address", os.key.Address, "balance", balance.String())
os.logger.Info("oracle server account", "address", os.key.Address, "remaining balance", balance.String())
if balance.Cmp(AlertBalance) <= 0 {
os.logger.Warn("oracle account has too less balance left for data reporting", "balance", balance.String())
}
Expand Down Expand Up @@ -437,7 +437,7 @@ func (os *OracleServer) doReport(curRndCommitHash common.Hash, lastRoundData *ty

auth, err := bind.NewKeyedTransactorWithChainID(os.key.PrivateKey, chainID)
if err != nil {
os.logger.Error("NewKeyedTransactorWithChainID", "error", err)
os.logger.Error("new keyed transactor with chain ID", "error", err)
return nil, err
}

Expand Down Expand Up @@ -476,7 +476,7 @@ func (os *OracleServer) buildRoundData(round uint64) (*types.RoundData, error) {
for _, s := range os.protocolSymbols {
p, err := os.aggregatePrice(s, int64(os.curSampleTS))
if err != nil {
os.logger.Warn("aggregatePrice", "error", err.Error(), "symbol", s)
os.logger.Debug("no data for aggregation", "reason", err.Error(), "symbol", s)
continue
}
prices[s] = *p
Expand Down Expand Up @@ -655,7 +655,7 @@ func (os *OracleServer) PluginRuntimeDiscovery() {

binaries, err := helpers.ListPlugins(os.pluginDIR)
if err != nil {
os.logger.Error("PluginRuntimeDiscovery", "error", err.Error())
os.logger.Error("list plugin", "error", err.Error())
return
}
for _, file := range binaries {
Expand All @@ -674,18 +674,18 @@ func (os *OracleServer) PluginRuntimeDiscovery() {
func (os *OracleServer) loadNewPlugin(f fs.FileInfo, plugConf types.PluginConfig) {
plugin, ok := os.pluginSet[f.Name()]
if !ok {
os.logger.Info("** New plugin discovered, going to setup it: ", f.Name(), f.Mode().String())
os.logger.Info("new plugin discovered, going to setup it: ", f.Name(), f.Mode().String())
pluginWrapper, err := os.setupNewPlugin(f.Name(), &plugConf)
if err != nil {
return
}

os.pluginSet[f.Name()] = pluginWrapper
return
}

if f.ModTime().After(plugin.StartTime()) || plugin.Exited() {
os.logger.Info("** Replacing legacy plugin with new one: ", f.Name(), f.Mode().String())
os.logger.Info("replacing legacy plugin with new one: ", f.Name(), f.Mode().String())

// stop the legacy plugin
plugin.Close()
delete(os.pluginSet, f.Name())
Expand All @@ -700,7 +700,7 @@ func (os *OracleServer) loadNewPlugin(f fs.FileInfo, plugConf types.PluginConfig

func (os *OracleServer) setupNewPlugin(name string, conf *types.PluginConfig) (*pWrapper.PluginWrapper, error) {
if err := os.ApplyPluginConf(name, conf); err != nil {
os.logger.Error("Apply plugin config", "error", err.Error())
os.logger.Error("apply plugin config", "error", err.Error())
return nil, err
}

Expand All @@ -711,7 +711,7 @@ func (os *OracleServer) setupNewPlugin(name string, conf *types.PluginConfig) (*
if err == types.ErrMissingServiceKey {
os.keyRequiredPlugins[name] = struct{}{}
}
os.logger.Error("Cannot setup plugin", "name", name, "error", err.Error())
os.logger.Error("cannot setup plugin", "name", name, "error", err.Error())
pluginWrapper.CleanPluginProcess()
return nil, err
}
Expand All @@ -727,11 +727,11 @@ func (os *OracleServer) ApplyPluginConf(name string, plugConf *types.PluginConfi
// set the plugin configuration via system env, thus the plugin can load it on startup.
conf, err := json.Marshal(plugConf)
if err != nil {
os.logger.Error("Cannot marshal plugin's configuration", "error", err.Error())
os.logger.Error("cannot marshal plugin's configuration", "error", err.Error())
return err
}
if err = o.Setenv(name, string(conf)); err != nil {
os.logger.Error("Cannot set plugin configuration via system ENV")
os.logger.Error("cannot set plugin configuration via system ENV")
return err
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion plugin_wrapper/plugin_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ func (pw *PluginWrapper) fetchPrices(symbols []string, ts int64) error {
return err
}

if len(report.UnRecognizeSymbols) != 0 {
if len(report.UnRecognizableSymbols) != 0 {
pw.logger.Debug("the data source cannot recognize some symbol", "report", report)
}

Expand Down
4 changes: 0 additions & 4 deletions plugins/binance/binance.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ type BIClient struct {

func NewBIClient(conf *types.PluginConfig) *BIClient {
client := common.NewClient(conf.Key, time.Second*time.Duration(conf.Timeout), conf.Endpoint)
if client == nil {
panic("cannot create https client for api.binance.us")
}

logger := hclog.New(&hclog.LoggerOptions{
Name: conf.Name,
Level: hclog.Info,
Expand Down
14 changes: 7 additions & 7 deletions plugins/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,16 @@ func NewPlugin(conf *types.PluginConfig, client DataSourceClient, version string
func (p *Plugin) FetchPrices(symbols []string) (types.PluginPriceReport, error) {
var report types.PluginPriceReport

availableSymbols, unRecogniseSymbols, availableSymMap := p.resolveSymbols(symbols)
availableSymbols, unRecognizableSymbols, availableSymMap := p.resolveSymbols(symbols)
if len(availableSymbols) == 0 {
report.UnRecognizeSymbols = unRecogniseSymbols
report.UnRecognizableSymbols = unRecognizableSymbols
return report, ErrKnownSymbols
}

cPRs, err := p.fetchPricesFromCache(availableSymbols)
if err == nil {
report.Prices = cPRs
report.UnRecognizeSymbols = unRecogniseSymbols
report.UnRecognizableSymbols = unRecognizableSymbols
return report, nil
}

Expand Down Expand Up @@ -97,7 +97,7 @@ func (p *Plugin) FetchPrices(symbols []string) (types.PluginPriceReport, error)
p.cachePrices[v.Symbol] = pr
report.Prices = append(report.Prices, pr)
}
report.UnRecognizeSymbols = unRecogniseSymbols
report.UnRecognizableSymbols = unRecognizableSymbols
return report, nil
}

Expand Down Expand Up @@ -143,20 +143,20 @@ func (p *Plugin) Close() {
// pattens supported by data providers, and filter outs those un-supported symbols.
func (p *Plugin) resolveSymbols(askedSymbols []string) ([]string, []string, map[string]string) {
var supported []string
var unSupported []string
var unRecognizable []string

symbolsMapping := make(map[string]string)

for _, askedSym := range askedSymbols {
converted := ConvertSymbol(askedSym, p.symbolSeparator)
if _, ok := p.availableSymbols[converted]; !ok {
unSupported = append(unSupported, askedSym)
unRecognizable = append(unRecognizable, askedSym)
continue
}
supported = append(supported, converted)
symbolsMapping[converted] = askedSym
}
return supported, unSupported, symbolsMapping
return supported, unRecognizable, symbolsMapping
}

func (p *Plugin) fetchPricesFromCache(availableSymbols []string) ([]types.Price, error) {
Expand Down
4 changes: 0 additions & 4 deletions plugins/forex_currencyfreaks/forex_currencyfreaks.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,6 @@ type CFClient struct {

func NewCFClient(conf *types.PluginConfig) *CFClient {
client := common.NewClient(conf.Key, time.Second*time.Duration(conf.Timeout), conf.Endpoint)
if client == nil {
panic(fmt.Sprintf("cannot create https client for %s", conf.Endpoint))
}

logger := hclog.New(&hclog.LoggerOptions{
Name: conf.Name,
Level: hclog.Info,
Expand Down
4 changes: 0 additions & 4 deletions plugins/forex_currencylayer/forex_currencylayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,6 @@ type CLClient struct {

func NewCLClient(conf *types.PluginConfig) *CLClient {
client := common.NewClient(conf.Key, time.Second*time.Duration(conf.Timeout), conf.Endpoint)
if client == nil {
panic(fmt.Sprintf("cannot create client for %s", conf.Endpoint))
}

logger := hclog.New(&hclog.LoggerOptions{
Name: conf.Name,
Level: hclog.Info,
Expand Down
Loading

0 comments on commit 52037a3

Please sign in to comment.