From 4fdcf1485479392839ea162b7d58c8d88fc2f393 Mon Sep 17 00:00:00 2001 From: Steven Landers Date: Tue, 26 Sep 2023 09:19:01 -0400 Subject: [PATCH] Add tolerance to nil response for error log (#1070) * add tolerance to nil for error * add test * split telemetry from log for dry-ness * make -1 default response (0 is success) --- oracle/price-feeder/oracle/oracle.go | 25 +++++++++++++----- oracle/price-feeder/oracle/oracle_test.go | 31 +++++++++++++++++++++++ 2 files changed, 50 insertions(+), 6 deletions(-) diff --git a/oracle/price-feeder/oracle/oracle.go b/oracle/price-feeder/oracle/oracle.go index 706732d412..f5cde70ea9 100644 --- a/oracle/price-feeder/oracle/oracle.go +++ b/oracle/price-feeder/oracle/oracle.go @@ -701,15 +701,11 @@ func (o *Oracle) tick( resp, err := o.oracleClient.BroadcastTx(clientCtx, voteMsg) if err != nil { - o.logger.Error().Err(err). - Str("status", "failure"). - Uint32("response_code", resp.Code). - Str("tx_hash", resp.TxHash). - Int64("tick_duration", time.Since(startTime).Milliseconds()). - Msg(fmt.Sprintf("broadcasted for height %d", blockHeight)) + o.logResponseError(err, resp, startTime, blockHeight) telemetry.IncrCounter(1, "failure", "broadcast") return err } + o.logger.Info(). Str("status", "success"). Uint32("response_code", resp.Code). @@ -724,6 +720,23 @@ func (o *Oracle) tick( return nil } +func (o *Oracle) logResponseError(err error, resp *sdk.TxResponse, startTime time.Time, blockHeight int64) { + responseCode := -1 // success is 0 + var txHash string + + if resp != nil { + responseCode = int(resp.Code) + txHash = resp.TxHash + } + + o.logger.Error().Err(err). + Str("status", "failure"). + Int("response_code", responseCode). + Str("tx_hash", txHash). + Int64("tick_duration", time.Since(startTime).Milliseconds()). + Msg(fmt.Sprintf("broadcasted for height %d", blockHeight)) +} + func (o *Oracle) healthchecksPing() { for url, client := range o.healthchecks { o.logger.Info().Msg("updating healthcheck status") diff --git a/oracle/price-feeder/oracle/oracle_test.go b/oracle/price-feeder/oracle/oracle_test.go index 4b5ca2f763..54298c0c94 100644 --- a/oracle/price-feeder/oracle/oracle_test.go +++ b/oracle/price-feeder/oracle/oracle_test.go @@ -566,6 +566,7 @@ func TestTickScenarios(t *testing.T) { blockHeight int64 previousVotePeriod float64 votePeriod uint64 + mockBroadcastErr error // expectations expectedVoteMsg *oracletypes.MsgAggregateExchangeRateVote @@ -617,6 +618,31 @@ func TestTickScenarios(t *testing.T) { Validator: validatorAddr, }, }, + { + name: "Should not crash if broadcast returns nil response with error", + isJailed: false, + blockHeight: 1, + previousVotePeriod: 0, + votePeriod: 1, + pairs: []config.CurrencyPair{ + {Base: "USDT", ChainDenom: "uusdt", Quote: "USD"}, + {Base: "BTC", ChainDenom: "ubtc", Quote: "USD"}, + {Base: "ETH", ChainDenom: "ueth", Quote: "USD"}, + }, + prices: map[string]sdk.Dec{ + "USDT": sdk.MustNewDecFromStr("1.1"), + "BTC": sdk.MustNewDecFromStr("2.2"), + "ETH": sdk.MustNewDecFromStr("3.3"), + }, + whitelist: denomList("uusdt", "ubtc", "ueth"), + expectedVoteMsg: &oracletypes.MsgAggregateExchangeRateVote{ + ExchangeRates: "2.200000000000000000ubtc,3.300000000000000000ueth,1.100000000000000000uusdt", + Feeder: feederAddr, + Validator: validatorAddr, + }, + mockBroadcastErr: fmt.Errorf("test error"), + expectedErr: fmt.Errorf("test error"), + }, { name: "Same voting period should avoid broadcasting without error", isJailed: false, @@ -682,6 +708,11 @@ func TestTickScenarios(t *testing.T) { require.Equal(t, test.expectedVoteMsg.Validator, voteMsg.Validator, test.name) broadcastCount++ + + if test.mockBroadcastErr != nil { + return nil, test.mockBroadcastErr + } + return &sdk.TxResponse{TxHash: "0xhash", Code: 200}, nil }, },