Skip to content

Commit

Permalink
bot should crash if delete cycles threshold is exceeded (closes stell…
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhilsaraf committed Aug 9, 2020
1 parent 0c04c15 commit 789dbb8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
1 change: 1 addition & 0 deletions LIST_OF_HACKS.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Incomplete list of hacks in the codebase that should be fixed before upgrading t

- LOH-1 - support backward-compatible case of not having any pre-specified function
- LOH-2 - support backward-compatible case of defaulting to "mid" price when left unspecified
- LOH-3 - we want to guarantee that the bot crashes if the errors exceed deleteCyclesThreshold, so we start a new thread with a sleep timer to crash the bot as a safety

## Workarounds

Expand Down
10 changes: 6 additions & 4 deletions examples/configs/trader/sample_trader.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ MAX_TICK_DELAY_MILLIS=0
# when trading on a non-SDEX exchange the only supported mode is "both"
SUBMIT_MODE="both"

# how many continuous errors in each update cycle can the bot accept before it will delete all offers to protect its exposure.
# this number has to be exceeded for all the offers to be deleted and any error will be counted only once per update cycle.
# how many continuous errors in each update cycle can the bot accept before it will delete all offers to protect its exposure and then intentionally crash.
# the bot will continue running if it hits an error, but will crash if it reaches the condition to delete all offers.
#
# Note: this number has to be exceeded for all the offers to be deleted and any error will be counted only once per update cycle.
# any time the bot completes a full run successfully this counter will be reset.
# the bot will continue running even if it hits an error or deletes all offers.
# the typical use case for this config value is to keep the orders on your orderbook intact if your price feed is unreachable for a small amount of time.
#
# Note: the typical use case for this config value is to keep the orders on your orderbook intact if your price feed is unreachable for a small amount of time.
# example: use -1 if you never want to delete all offers (this is not recommended).
# example: use 0 if you want to delete all offers on any error.
# example: use 2 if you want to tolerate 2 continuous update cycles with errors, i.e. 3 continuous update cycles with errors will delete all offers.
Expand Down
15 changes: 13 additions & 2 deletions trader/trader.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,25 @@ func (t *Trader) deleteAllOffers() {
dOps = append(dOps, t.sdex.DeleteAllOffers(t.buyingAOffers)...)
t.buyingAOffers = []hProtocol.Offer{}

// LOH-3 - we want to guarantee that the bot crashes if the errors exceed deleteCyclesThreshold, so we start a new thread with a sleep timer to crash the bot as a safety
defer func() {
log.Printf("started thread to crash bot in 1 minute as a fallback (to respect deleteCyclesThreshold)\n")
time.Sleep(time.Minute)
log.Fatalf("bot should have crashed by now (programmer error?), crashing\n")
}()

log.Printf("created %d operations to delete offers\n", len(dOps))
if len(dOps) > 0 {
// to delete offers the submitMode doesn't matter, so use api.SubmitModeBoth as the default
e := t.exchangeShim.SubmitOps(api.ConvertOperation2TM(dOps), api.SubmitModeBoth, nil)
e := t.exchangeShim.SubmitOps(api.ConvertOperation2TM(dOps), api.SubmitModeBoth, func(hash string, e error) {
log.Fatalf("...deleted %d offers, exiting (asyncCallback: hash=%s, e=%v)", len(dOps), hash, e)
})
if e != nil {
log.Println(e)
log.Fatalf("continuing to exit after showing error during submission of delete offer ops: %s", e)
return
}
} else {
log.Fatalf("...nothing to delete, exiting")
}
}

Expand Down

0 comments on commit 789dbb8

Please sign in to comment.