Improvement: Subsystem separation (#664)

* Initial codes for a trade tracker

* Moving everything in a broken fashion

* Removes tradetracker. Removes some errors for subsystems

* Cleans up some subsystems, renames stuttering types. Removes some global Bot usage

* More basic subsystem renaming and file moving

* Removes engine dependency from events,ntpserver,ordermanager,comms manager

* Exports eventManager, fixes rpcserver. puts rpcserver back for now

* Removes redundant error message, further removes engine dependencies

* experimental end of day interface usage

* adds ability to build the application

* Withdraw and event manager handling

* cleans up apiserver and communications manager

* Cleans up some start/setup processes. Though should separate

* More consistency with Setup Start Stop IsRunning funcs

* Final consistency pass before testing phase

* Fixes engine tests. Fixes stop nil issue

* api server tests

* Communications manager testing

* Connection manager tests and nilsubsystem error

* End of day currencypairsyncer tests

* Adds databaseconnection/databaseconnection_test.go

* Adds withdrawal manager tests

* Deposit address testing. Moved orderbook sync first as its more important

* Adds test for event manager

* More full eventmanager testing

* Adds testfile. Enables skipped test.

* ntp manager tests

* Adds ordermanager tests, Extracts a whole new subsystem from engine and fanangles import cycles

* Adds websocket routine manager tests

* Basic portfolio manager testing

* Fixes issue with currency pair sync startup

* Fixes issue with event manager startup

* Starts the order manager before backtester starts

* Fixes fee tests. Expands testing. Doesnt fix races

* Fixes most test races

* Resolves data races

* Fixes subsystem test issues

* currency pair syncer coverage tests

* Refactors portfolio. Fixes tests. Withdraw validation

Portfolio didn't need to exist with a portfolio manager. Now the porfolio manager
is in charge how the portfolio is handled and all portfolio functions are attached
to the base instead of just exported at the package level

Withdrawal validation occurred at the exchange level when it can just be run at the
withdrawal manager level. All withdrawal requests go through that endpoint

* lint -fix

* golang lint fixes

* lints and comments everything

* Updates GCT logo, adds documentation for some subsystems

* More documentation and more logo updates

* Fixes backtesting and apiserver errors encountered

* Fixes errors and typos from reviewing

* More minor fixes

* Changes %h verb to %w

* reverbs to %s

* Humbly begins reverting to more flat engine package

The main reasoning for this is that the subsystem split doesn't make sense
in a golang environment. The subsystems are only meant to be used with engine
and so by placing them in a non-engine area, it does not work and is
inconsistent with the rest of the application's package layout.

This will begin salvaging the changes made by reverting to a flat
engine package, but maintaining the consistent designs introduced.
Further, I will look to remove any TestMains and decrease the scope
of testing to be more local and decrease the issues that have been
caused from our style of testing.

* Manages to re-flatten things. Everything is within its own file

* mini fixes

* Fixes tests and data races and lints

* Updates docs tool for engine to create filename readmes

* os -> ioutil

* remove err

* Appveyor version increase test

* Removes tCleanup as its unsupported on appveyor

* Adds stuff that I thought was in previous merge master commit

* Removes cancel from test

* Fixes really fun test-exclusive data race

* minor nit fixes

* niterinos

* docs gen

* rm;rf test

* Remove typoline. expands startstop helper. Splits apiserver

* Removes accidental folder

* Uses update instead of replace for order upsert

* addresses nits. Renames files. Regenerates documentation.

* lint and removal of comments

* Add new test for default scenario

* Fixes typo

* regen docs
This commit is contained in:
Scott
2021-05-31 10:17:12 +10:00
committed by GitHub
parent 0e7d530c71
commit 5ea5245afb
325 changed files with 11868 additions and 8068 deletions

View File

@@ -6,11 +6,14 @@ import (
"sync"
"sync/atomic"
"github.com/thrasher-corp/gocryptotrader/engine/subsystem"
"github.com/thrasher-corp/gocryptotrader/log"
)
const gctscriptManagerName = "GCTScript"
const (
caseName = "GCTScript"
// Name is an exported subsystem name
Name = "gctscript"
)
// GctScriptManager loads and runs GCT Tengo scripts
type GctScriptManager struct {
@@ -31,22 +34,24 @@ func NewManager(config *Config) (*GctScriptManager, error) {
}, nil
}
// Started returns if gctscript manager subsystem is started
func (g *GctScriptManager) Started() bool {
// IsRunning returns if gctscript manager subsystem is started
func (g *GctScriptManager) IsRunning() bool {
if g == nil {
return false
}
return atomic.LoadInt32(&g.started) == 1
}
// Start starts gctscript subsystem and creates shutdown channel
func (g *GctScriptManager) Start(wg *sync.WaitGroup) (err error) {
if !atomic.CompareAndSwapInt32(&g.started, 0, 1) {
return fmt.Errorf("%s %w", gctscriptManagerName, subsystem.ErrSubSystemAlreadyStarted)
return fmt.Errorf("%s %s", caseName, ErrScriptFailedValidation)
}
defer func() {
if err != nil {
atomic.CompareAndSwapInt32(&g.started, 1, 0)
}
}()
log.Debugln(log.Global, gctscriptManagerName, subsystem.MsgSubSystemStarting)
g.shutdown = make(chan struct{})
wg.Add(1)
@@ -57,13 +62,12 @@ func (g *GctScriptManager) Start(wg *sync.WaitGroup) (err error) {
// Stop stops gctscript subsystem along with all running Virtual Machines
func (g *GctScriptManager) Stop() error {
if atomic.LoadInt32(&g.started) == 0 {
return fmt.Errorf("%s %w", gctscriptManagerName, subsystem.ErrSubSystemNotStarted)
return fmt.Errorf("%s not running", caseName)
}
defer func() {
atomic.CompareAndSwapInt32(&g.started, 1, 0)
}()
log.Debugln(log.GCTScriptMgr, gctscriptManagerName, subsystem.MsgSubSystemShuttingDown)
err := g.ShutdownAll()
if err != nil {
return err
@@ -73,13 +77,12 @@ func (g *GctScriptManager) Stop() error {
}
func (g *GctScriptManager) run(wg *sync.WaitGroup) {
log.Debugln(log.Global, gctscriptManagerName, subsystem.MsgSubSystemStarted)
log.Debugf(log.Global, "%s starting", caseName)
SetDefaultScriptOutput()
g.autoLoad()
defer func() {
wg.Done()
log.Debugln(log.GCTScriptMgr, gctscriptManagerName, subsystem.MsgSubSystemShutdown)
}()
<-g.shutdown

View File

@@ -23,7 +23,7 @@ import (
// NewVM attempts to create a new Virtual Machine firstly from pool
func (g *GctScriptManager) NewVM() (vm *VM) {
if !g.Started() {
if !g.IsRunning() {
log.Error(log.GCTScriptMgr, Error{
Action: "NewVM",
Cause: ErrScriptingDisabled,

View File

@@ -145,7 +145,7 @@ func (e Exchange) DepositAddress(exch string, currencyCode currency.Code) (out s
err = errors.New("currency code is empty")
return
}
return engine.Bot.DepositAddressManager.GetDepositAddressByExchange(exch, currencyCode)
return engine.Bot.DepositAddressManager.GetDepositAddressByExchangeAndCurrency(exch, currencyCode)
}
// WithdrawalFiatFunds withdraw funds from exchange to requested fiat source
@@ -163,7 +163,7 @@ func (e Exchange) WithdrawalFiatFunds(bankAccountID string, request *withdraw.Re
}
}
otp, err := engine.Bot.GetExchangeoOTPByName(request.Exchange)
otp, err := engine.Bot.GetExchangeOTPByName(request.Exchange)
if err == nil {
otpValue, errParse := strconv.ParseInt(otp, 10, 64)
if errParse != nil {
@@ -182,7 +182,7 @@ func (e Exchange) WithdrawalFiatFunds(bankAccountID string, request *withdraw.Re
request.Fiat.Bank.SWIFTCode = v.SWIFTCode
request.Fiat.Bank.IBAN = v.IBAN
resp, err := engine.Bot.SubmitWithdrawal(request)
resp, err := engine.Bot.WithdrawManager.SubmitWithdrawal(request)
if err != nil {
return "", err
}
@@ -196,7 +196,7 @@ func (e Exchange) WithdrawalCryptoFunds(request *withdraw.Request) (string, erro
if err != nil {
return "", err
}
otp, err := engine.Bot.GetExchangeoOTPByName(request.Exchange)
otp, err := engine.Bot.GetExchangeOTPByName(request.Exchange)
if err == nil {
v, errParse := strconv.ParseInt(otp, 10, 64)
if errParse != nil {
@@ -205,7 +205,7 @@ func (e Exchange) WithdrawalCryptoFunds(request *withdraw.Request) (string, erro
request.OneTimePassword = v
}
resp, err := engine.Bot.SubmitWithdrawal(request)
resp, err := engine.Bot.WithdrawManager.SubmitWithdrawal(request)
if err != nil {
return "", err
}

View File

@@ -60,8 +60,8 @@ func TestExchange_Exchanges(t *testing.T) {
t.Parallel()
x := exchangeTest.Exchanges(false)
y := len(x)
if y != 28 {
t.Fatalf("expected 28 received %v", y)
if y != 1 {
t.Fatalf("expected 1 received %v", y)
}
}
@@ -206,6 +206,9 @@ func setupEngine() (err error) {
return err
}
em := engine.SetupExchangeManager()
engine.Bot.ExchangeManager = em
return engine.Bot.LoadExchange(exchName, false, nil)
}

View File

@@ -30,10 +30,34 @@ func TestMain(m *testing.M) {
log.Print(err)
os.Exit(1)
}
engine.Bot.LoadExchange(exch.Value, false, nil)
engine.Bot.DepositAddressManager = new(engine.DepositAddressManager)
go engine.Bot.DepositAddressManager.Sync()
err = engine.Bot.OrderManager.Start(engine.Bot)
em := engine.SetupExchangeManager()
exch, err := em.NewExchangeByName(exch.Value)
if err != nil {
log.Print(err)
os.Exit(1)
}
exch.SetDefaults()
em.Add(exch)
engine.Bot.ExchangeManager = em
engine.Bot.WithdrawManager, err = engine.SetupWithdrawManager(em, nil, true)
if err != nil {
log.Print(err)
os.Exit(1)
}
engine.Bot.DepositAddressManager = engine.SetupDepositAddressManager()
err = engine.Bot.DepositAddressManager.Sync(engine.Bot.GetExchangeCryptocurrencyDepositAddresses())
if err != nil {
log.Print(err)
os.Exit(1)
}
engine.Bot.OrderManager, err = engine.SetupOrderManager(em, &engine.CommunicationManager{}, &engine.Bot.ServicesWG, false)
if err != nil {
log.Print(err)
os.Exit(1)
}
err = engine.Bot.OrderManager.Start()
if err != nil {
log.Print(err)
os.Exit(1)
@@ -46,7 +70,7 @@ func TestSetup(t *testing.T) {
x := Setup()
xType := reflect.TypeOf(x).String()
if xType != "*gct.Wrapper" {
t.Fatalf("Setup() should return pointer to Wrapper instead received: %v", x)
t.Fatalf("SetupCommunicationManager() should return pointer to Wrapper instead received: %v", x)
}
}