Skip to content

Commit

Permalink
Add tolerance to nil response for error log (#1070)
Browse files Browse the repository at this point in the history
* add tolerance to nil for error

* add test

* split telemetry from log for dry-ness

* make -1 default response (0 is success)
  • Loading branch information
stevenlanders authored Sep 26, 2023
1 parent 0895bcc commit 4fdcf14
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 6 deletions.
25 changes: 19 additions & 6 deletions oracle/price-feeder/oracle/oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand All @@ -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")
Expand Down
31 changes: 31 additions & 0 deletions oracle/price-feeder/oracle/oracle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,7 @@ func TestTickScenarios(t *testing.T) {
blockHeight int64
previousVotePeriod float64
votePeriod uint64
mockBroadcastErr error

// expectations
expectedVoteMsg *oracletypes.MsgAggregateExchangeRateVote
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
},
},
Expand Down

0 comments on commit 4fdcf14

Please sign in to comment.