Skip to content

Commit

Permalink
Fixups for Gas Price Caches (#34)
Browse files Browse the repository at this point in the history
* fix cache

* fix nil pointer

* set gas factor

* more logging

* more

* work

* work

* add recovery fn

* changeout func
  • Loading branch information
keefertaylor authored Feb 29, 2024
1 parent 3c2b0f6 commit 6e77828
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 9 deletions.
12 changes: 10 additions & 2 deletions cosmos/tx/exponential_gas_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import (
txtypes "github.com/cosmos/cosmos-sdk/types/tx"
)

// Default gas factor to use
const defaultGasFactor float64 = 1.1

// Gas manager using exponential backoff.
//
// Rough algorithm:
Expand Down Expand Up @@ -107,8 +110,13 @@ func (gm *geometricGasManager) GetGasFactor(chainName string) (float64, error) {
// Attempt to get a gas factor, and return if successful.
gasFactor, err := gm.gasPriceProvider.GetGasFactor(chainName)
if err == ErrNoGasFactor {
gm.logger.Warn().Str("chain_name", chainName).Msg("no gas factor found for chain, setting gas factor to be 1.2")
return 1.2, nil
gm.logger.Warn().Str("chain_name", chainName).Float64("default_gas_factor", defaultGasFactor).Msg("no gas factor found for chain, initializing as default")
err := gm.gasPriceProvider.SetGasFactor(chainName, defaultGasFactor)
if err != nil {
gm.logger.Error().Err(err).Str("chain_name", chainName).Float64("default_gas_factor", defaultGasFactor).Msg("unable to set set default gas factor for chain. recovering by returning default gas factor")
}

return defaultGasFactor, nil
} else if err != nil {
return 0.0, err
}
Expand Down
14 changes: 10 additions & 4 deletions cosmos/tx/gas.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,30 +304,36 @@ func (p *FileGasPriceProvider) initialize() error {
if err != nil {
return err
}
p.logger.Debug().Str("chain_name", chainName).Float64("gas_factor", gasFactor).Msg("initialized gas factor")
p.logger.Info().Str("chain_name", chainName).Float64("gas_factor", gasFactor).Msg("💾 initialized gas factor")
}

for chainName, gasPrice := range gasData.GasPrices {
err := p.wrapped.SetGasPrice(chainName, gasPrice)
if err != nil {
return err
}
p.logger.Debug().Str("chain_name", chainName).Float64("gas_price", gasPrice).Msg("initialized gas price")
p.logger.Info().Str("chain_name", chainName).Float64("gas_price", gasPrice).Msg("💾 initialized gas price")
}

p.logger.Debug().Str("file", p.gasDataFile).Msg("gas price state initialization complete")
p.logger.Info().Str("file", p.gasDataFile).Msg("gas price state initialization complete")
return nil
}

// Load data from the file
func (p *FileGasPriceProvider) loadData() (*GasData, error) {
_, err := os.Stat(p.gasDataFile)
if os.IsNotExist(err) {
p.logger.Info().Str("file", p.gasDataFile).Msg("💾 no gas price cache found on disk. will not initialize.")
return &GasData{}, nil
}

file, err := os.Open(p.gasDataFile)
if err != nil {
return nil, err
}
defer file.Close()

var gasData *GasData
gasData := &GasData{}
if err := json.NewDecoder(file).Decode(gasData); err != nil {
return nil, err
}
Expand Down
7 changes: 4 additions & 3 deletions cosmos/tx/tx_broadcaster.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ func (b *defaultBroadcaster) signAndBroadcast(ctx context.Context, msgs []sdk.Ms
codespace := result.TxResponse.Codespace
broadcastResponseCode := result.TxResponse.Code
logs := result.TxResponse.RawLog
b.logger.Info().Str("chain_name", b.chainName).Str("tx_hash", txHash).Uint32("code", broadcastResponseCode).Str("codespace", codespace).Str("logs", logs).Msg("📣 attempted to broadcast transaction")
b.logger.Info().Float64("gas_price", gasPrice).Float64("gas_factor", gasFactor).Str("chain_name", b.chainName).Str("tx_hash", txHash).Uint32("code", broadcastResponseCode).Str("codespace", codespace).Str("logs", logs).Msg("📣 attempted to broadcast transaction")
}

// Broadcast response helpfully sets `gasWanted` to zero if the transaction failed, which is a bit of a pain, especially if we want to get
Expand All @@ -263,13 +263,15 @@ func (b *defaultBroadcaster) checkTxStatus(ctx context.Context, txHash string) (
codespace := txStatus.TxResponse.Codespace
broadcastResponseCode := txStatus.TxResponse.Code
logs := txStatus.TxResponse.RawLog
b.logger.Info().Str("chain_name", b.chainName).Str("tx_hash", txHash).Uint32("code", broadcastResponseCode).Str("codespace", codespace).Str("logs", logs).Msg("got a settled tx status")
b.logger.Info().Str("chain_name", b.chainName).Str("tx_hash", txHash).Uint32("code", broadcastResponseCode).Str("codespace", codespace).Msg("got a settled tx status")
b.logger.Debug().Str("chain_name", b.chainName).Str("tx_hash", txHash).Uint32("code", broadcastResponseCode).Str("codespace", codespace).Str("logs", logs).Msg("logging full tx logs")

return txStatus, nil
}

grpcErr, ok := status.FromError(err)
if ok && grpcErr.Code() == codes.NotFound {

// No error, but nothing was found
b.logger.Debug().Str("chain_name", b.chainName).Str("tx_hash", txHash).Msg("tx not included in chain")
return nil, nil
Expand Down Expand Up @@ -497,7 +499,6 @@ func (b *retryableTxBroadcaster) signAndBroadcast(ctx context.Context, msgs []sd
// Otherwise, poll and wait.
b.logger.Error().Err(err).Uint("attempt", i+1).Uint("max_attempts", b.attempts).Msg("failed to sign and broadcast, will retry.")
time.Sleep(b.delay)

}
panic("retryable_tx_broadcaster::sign_and_broadcast::should never happen")
}
Expand Down
20 changes: 20 additions & 0 deletions util/recover.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package util

import "fmt"

func InterfaceToError(errorInterface interface{}) error {
// Attempt to coerce into error
err, ok := errorInterface.(error)
if ok {
return err
}

// Otherwise attempt to coerce into string
stringifiedErr, ok := errorInterface.(string)
if ok {
return fmt.Errorf(stringifiedErr)
}

// Otherwise, just ditch with a generic error.
return fmt.Errorf("recovered from a panic that was neither a string nor an error")
}

0 comments on commit 6e77828

Please sign in to comment.