Skip to content

Commit

Permalink
Merge pull request #20 from indexed-finance/debug
Browse files Browse the repository at this point in the history
Bug Fixes And Debug Logging Changes
  • Loading branch information
bonedaddy authored Mar 27, 2021
2 parents 26e4fc0 + 3a141f8 commit d289f3f
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 33 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ circuit-breaker
sigma-core
bin
contracts
compilers

# regexs
**/*.yaml
Expand Down
2 changes: 2 additions & 0 deletions eventwatcher/handle_log_swaps.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ func (wc *WatchedContract) handleLogSwaps(

wc.logger.Warn(
"price fluctuation outside of acceptable bounds, breaking!",
zap.Float64("change.abs", math.Abs(change)),
zap.Float64("break.percentage", breakPercentage),
)

// lock the authorizer since bind.TransactOpts is not threadsafe
Expand Down
54 changes: 24 additions & 30 deletions service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,8 @@ func (s *Service) StartWatchers() error {
)

for _, pool := range s.pools {
addresses[pool.Name] = pool.ContractAddress
spotPriceBreakPercentages[pool.Name] = pool.SpotPriceBreakPercentage
addresses[strings.ToLower(pool.Name)] = pool.ContractAddress
spotPriceBreakPercentages[strings.ToLower(pool.Name)] = pool.SpotPriceBreakPercentage
}

// get the bindings
Expand Down Expand Up @@ -227,7 +227,7 @@ func (s *Service) StartWatchers() error {
if err := wtchr.Listen(ctx, s.db, s.at, s.auther, bkPercent, s.ew.BC().EthClient()); err != nil {
errCh <- err
}
}(watcher, spotPriceBreakPercentages[watcher.Name()])
}(watcher, spotPriceBreakPercentages[strings.ToLower(watcher.Name())])
}

select {
Expand Down Expand Up @@ -312,37 +312,22 @@ func (s *Service) StartBlockListener() error {
if err != nil {
s.logger.Error("UpdateBalancesWeightsAndSupplies failed", zap.Error(err), zap.String("pool", pool.Name))
}

controllerAddress, err := contract.GetController(nil)
if err != nil {
s.logger.Error("failed to get controller address", zap.Error(err), zap.String("pool", pool.Name))
return
}
// now start checking total supply shifts
for tok, supply := range infos.TokenTotalSupplies {
// check the total supply for the given token
// however if it fails dont abort processing, continue processing
// as other tokens may need to be checked
if controllerAddress, err := contract.GetController(nil); err != nil {
s.logger.Error("failed to get controller address", zap.Error(err), zap.String("pool", pool.Name), zap.String("token", tok))
conContract, err := controller.NewController(controllerAddress, s.ew.BC().EthClient())
if err != nil {
s.logger.Error("failed to get controller contract", zap.Error(err), zap.String("pool", pool.Name), zap.String("token", tok))
} else {
conContract, err := controller.NewController(controllerAddress, s.ew.BC().EthClient())
if err != nil {
s.logger.Error("failed to get controller contract", zap.Error(err), zap.String("pool", pool.Name), zap.String("token", tok))
} else {
// get the circuit breaker address
circuitBreakerAddr, err := conContract.CircuitBreaker(nil)
if err != nil {
s.logger.Error("failed to get circuit breaker contract address", zap.Error(err), zap.String("pool", pool.Name), zap.String("token", tok))
} else {
breaker, err := controller.NewController(circuitBreakerAddr, s.ew.BC().EthClient())
if err != nil {
s.logger.Error("failed to get circuit breaker contract", zap.Error(err), zap.String("pool", pool.Name), zap.String("token", tok))
} else {
if err := s.circuitBreakCheck(tok, supply, totalSupplies, pool, breaker); err != nil {
s.logger.Error("circuitBreakCheck failed", zap.Error(err), zap.String("pool", pool.Name), zap.String("token", tok))
}
}
}
if err := s.circuitBreakCheck(tok, supply, totalSupplies, pool, conContract); err != nil {
s.logger.Error("circuitBreakCheck failed", zap.Error(err), zap.String("pool", pool.Name), zap.String("token", tok))
}
}
}

// purge old records if need be
s.purgeInfoCheck(pool.Name)

Expand Down Expand Up @@ -427,7 +412,16 @@ func (s *Service) circuitBreakCheck(
// we take the absolute to see if the change is greater than the break percentage
// as we want to handle circuit breaks whether the total supply increased or decreases
if math.Abs(change) > pool.SupplyBreakPercentage {

circuitBreakAddr, err := contract.CircuitBreaker(nil)
if err != nil {
s.logger.Error("failed to get circuit breaker contract address", zap.Error(err))
return err
}
breaker, err := controller.NewController(circuitBreakAddr, s.ew.BC().EthClient())
if err != nil {
s.logger.Error("failed to get circuit breaker contract binding", zap.Error(err))
return err
}
s.logger.Warn(
"token supply fluctuation is greater than minimum break percentage, breaking circuits!",
zap.String("pool", pool.Name), zap.String("token", tok),
Expand All @@ -443,7 +437,7 @@ func (s *Service) circuitBreakCheck(
s.logger.Error("failed to suggest gasprice", zap.Error(err))
} else {
s.logger.Info("gas price calculated (includes boost)", zap.String("gas.price", gasPrice.String()))
s.setPublicSwap(contract, gasPrice, pool.Name, tok, pool.ContractAddress)
s.setPublicSwap(breaker, gasPrice, pool.Name, tok, pool.ContractAddress)
}
// we need to unset the gas price that we overrode the transactor with
// so that future uses of this transactor have the gas price set to nil
Expand Down
11 changes: 8 additions & 3 deletions service/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func TestService(t *testing.T) {
common.HexToHash("0x5b6d669d3a27795f6f162737115a5e605157fb26465de23292c55e9739a198e8"),
)
require.NoError(t, err)
fakeSwap := newFakePublicSwap(tx)
fakeSwap := newFakePublicSwap(tx, acct.Address)
type args struct {
token string
supply interface{}
Expand Down Expand Up @@ -373,15 +373,20 @@ func getFakeBalancesWeightsAndSupplies(
}

type fakePublicSwap struct {
tx *types.Transaction
tx *types.Transaction
addr common.Address
}

// returns a fake public swap struct for use with the circuitBreakCheck
// function, you must provide a transaction hash that has already been mined
func newFakePublicSwap(tx *types.Transaction) *fakePublicSwap {
func newFakePublicSwap(tx *types.Transaction, addr common.Address) *fakePublicSwap {
return &fakePublicSwap{tx: tx}
}

func (fpb *fakePublicSwap) SetPublicSwap(opts *bind.TransactOpts, pool common.Address, enabled bool) (*types.Transaction, error) {
return fpb.tx, nil
}

func (fpb *fakePublicSwap) CircuitBreaker(opts *bind.CallOpts) (common.Address, error) {
return fpb.addr, nil
}
1 change: 1 addition & 0 deletions utils/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
// designed to allow for easier testing
type Breaker interface {
SetPublicSwap(opts *bind.TransactOpts, pool common.Address, enabled bool) (*types.Transaction, error)
CircuitBreaker(opts *bind.CallOpts) (common.Address, error)
}

// GetGasPrice returns a gas price as reported by the oracle, overriding it if it is lower
Expand Down

0 comments on commit d289f3f

Please sign in to comment.