Files
gocryptotrader/gctscript/vm/vm.go
Scott fcc5ad4551 exchanges/qa: Add exchange wrapper testing suite (#1159)
* initial concept of a nice validation tester for exchanges

* adds some datahandler design

* expand testing

* more tests and fixes

* minor end of day fix for bithumb

* fixes implementation issues

* more test coverage and improvements, but not sure if i should continue

* fix more wrapper implementations

* adds error type, more fixes

* changes signature, fixes implementations

* fixes more wrapper implementations

* one more bit

* more cleanup

* WOW things work?

* lintle 1/1337

* mini bump

* fixes all linting

* neaten

* GetOrderInfo+ asset pair fixes+improvements

* adds new websocket test

* expand ws testing

* fix bug, expand tests, improve implementation

* code coverage of a lot of new codes

* fixes everything

* reverts accidental changes

* minor fixes from reviewing code

* removes Bitfinex cancelBatchOrder implementation

* fixes dumb baby typo for babies

* mini nit fixes

* so many nits to address

* addresses all the nits

* Titlecase

* switcheroo

* removes websocket testing for now

* fix appveyor, minor test fix

* fixes typo, re-kindles killed kode

* skip binance wrapper tests when running CI

* expired context, huobi okx fixes

* kodespull

* fix ordering

* time fix because why not

* fix exmo, others

* hopefully this fixes all of my life's problems

* last thing today

* huobi, more like hypotrophy

* golangci-lint, more like mypooroldknee-splint

* fix huobi times by removing them

* should fix okx currency issues

* blocks the application

* adds last little contingency for pairs

* addresses most nits and new problems

* lovely fixed before seeing why okx sucks

* fixes issues with okx websocket

* the classic receieieivaier

* lintle

* adds test and fixes existing tests

* expands error handling messages during setup

* fixes dumb okx bugs introduced

* quick fix for lint and exmo

* fixes nixes

* fix exmo deposit issue

* lint

* fixes issue with extra asset runs missing

* fix surprise race

* all the lint and merge fixes

* fixes surprise bugs in OKx

* fixes issues with times and chains

* fixing all the merge stuff

* merge fix

* rm logs and a panic potential

* lovely lint lament

* an easy demonstration of scenario, but not of initial purpose

* put it in the bin

* Revert "put it in the bin"

This reverts commit 15c6490f713233d43f10957367fcbf18e3818bdd.

* re-add after immediate error popup

* fix mini poor test design

* okx okay

* merge fixes

* fixes issues discovered in lovely test

* I FORGOT TO COMMIT THIS

* nit fixaroonaboo

* forgoetten test fix

* revert old okx asset intrument work

* fixes

* revert problems I didnt understand. update bybit

* fix merge bugs

* test cleanup

* further improvements

* reshuffle and lint

* rm redundant CI_TEST by rm the CI_TEST field that is redundant

* path fix

* move to its own section, dont run on 32 bit + appveyor

* lint

* fix lbank

* address nits

* let it rip

* fix failing test time range

* niteroo boogaloo

* mod tidy, use common.SimpleTimeFormat
2023-07-03 11:09:43 +10:00

290 lines
6.4 KiB
Go

package vm
import (
"archive/zip"
"bytes"
"context"
"encoding/hex"
"os"
"path/filepath"
"sync/atomic"
"time"
"github.com/d5/tengo/v2"
"github.com/gofrs/uuid"
"github.com/thrasher-corp/gocryptotrader/common"
"github.com/thrasher-corp/gocryptotrader/common/crypto"
scriptevent "github.com/thrasher-corp/gocryptotrader/database/repository/script"
"github.com/thrasher-corp/gocryptotrader/gctscript/modules/gct"
"github.com/thrasher-corp/gocryptotrader/gctscript/modules/loader"
"github.com/thrasher-corp/gocryptotrader/gctscript/wrappers/validator"
"github.com/thrasher-corp/gocryptotrader/log"
"github.com/volatiletech/null"
)
// NewVM attempts to create a new Virtual Machine firstly from pool
func (g *GctScriptManager) NewVM() *VM {
if !g.IsRunning() {
log.Errorln(log.GCTScriptMgr, Error{
Action: "NewVM",
Cause: ErrScriptingDisabled,
})
return nil
}
newUUID, err := uuid.NewV4()
if err != nil {
log.Errorln(log.GCTScriptMgr, Error{Action: "New: UUID", Cause: err})
return nil
}
if g.config.Verbose {
log.Debugln(log.GCTScriptMgr, "New GCTScript VM created")
}
s, ok := pool.Get().(*tengo.Script)
if !ok {
log.Errorln(log.GCTScriptMgr, Error{
Action: "NewVM",
Cause: common.GetTypeAssertError("*tengo.Script", pool),
})
return nil
}
return &VM{
ID: newUUID,
Script: s,
config: g.config,
unregister: func() error { return g.RemoveVM(newUUID) },
}
}
// SetDefaultScriptOutput sets default output file for scripts
func SetDefaultScriptOutput() {
loader.SetDefaultScriptOutput(filepath.Join(ScriptPath, "output"))
}
// Load parses and creates a new instance of tengo script vm
func (vm *VM) Load(file string) error {
if vm == nil {
return ErrNoVMLoaded
}
if filepath.Ext(file) != common.GctExt {
file += common.GctExt
}
if vm.config.Verbose {
log.Debugf(log.GCTScriptMgr, "Loading script: %s ID: %v", vm.ShortName(), vm.ID)
}
code, err := os.ReadFile(file)
if err != nil {
return &Error{Action: "Load: ReadFile", Script: file, Cause: err}
}
vm.File = file
vm.Path = filepath.Dir(file)
vm.Script = tengo.NewScript(code)
scriptCtx := &gct.Context{}
scriptCtx.Value = map[string]tengo.Object{
"script": &tengo.String{Value: vm.ShortName() + "-" + vm.ID.String()},
}
err = vm.Script.Add("ctx", scriptCtx)
if err != nil {
return err
}
vm.Script.SetImports(loader.GetModuleMap())
vm.Hash = vm.getHash()
if vm.config.AllowImports {
if vm.config.Verbose {
log.Debugf(log.GCTScriptMgr, "File imports enabled for vm: %v", vm.ID)
}
vm.Script.EnableFileImport(true)
}
vm.event(StatusSuccess, TypeLoad)
return nil
}
// Compile compiles to byte code loaded copy of vm script
func (vm *VM) Compile() (err error) {
vm.Compiled, err = vm.Script.Compile()
return err
}
// RunCtx runs compiled byte code with context.Context support.
func (vm *VM) RunCtx() (err error) {
ctx, cancel := context.WithTimeout(context.Background(), vm.config.ScriptTimeout)
defer cancel()
if vm.config.Verbose {
log.Debugf(log.GCTScriptMgr,
"Running script: %s ID: %v",
vm.ShortName(),
vm.ID)
}
err = vm.Compiled.RunContext(ctx)
if err != nil {
vm.event(StatusFailure, TypeExecute)
return Error{Action: "RunCtx", Cause: err}
}
vm.event(StatusSuccess, TypeExecute)
return nil
}
// CompileAndRun Compile and Run script with support for task running
func (vm *VM) CompileAndRun() {
if vm == nil {
return
}
err := vm.Compile()
if err != nil {
log.Errorln(log.GCTScriptMgr, err)
err = vm.unregister()
if err != nil {
log.Errorln(log.GCTScriptMgr, err)
}
return
}
err = vm.RunCtx()
if err != nil {
log.Errorln(log.GCTScriptMgr, err)
err = vm.unregister()
if err != nil {
log.Errorln(log.GCTScriptMgr, err)
}
return
}
if vm.Compiled.Get("timer").String() != "" {
vm.T, err = time.ParseDuration(vm.Compiled.Get("timer").String())
if err != nil {
log.Errorln(log.GCTScriptMgr, err)
err = vm.Shutdown()
if err != nil {
log.Errorln(log.GCTScriptMgr, err)
}
return
}
if vm.T > 0 {
vm.runner()
return
}
if vm.T < 0 {
log.Errorln(log.GCTScriptMgr, "Repeat timer cannot be under 1 nano second")
}
}
err = vm.Shutdown()
if err != nil {
log.Errorln(log.GCTScriptMgr, err)
}
}
// Shutdown shuts down current VM
func (vm *VM) Shutdown() error {
if vm == nil {
return ErrNoVMLoaded
}
if vm.S != nil {
close(vm.S)
}
if vm.config.Verbose {
log.Debugf(log.GCTScriptMgr, "Shutting down script: %s ID: %v", vm.ShortName(), vm.ID)
}
vm.Script = nil
pool.Put(vm.Script)
vm.event(StatusSuccess, TypeStop)
return vm.unregister()
}
// Read contents of script back and create script event
func (vm *VM) Read() ([]byte, error) {
vm.event(StatusSuccess, TypeRead)
return vm.read()
}
// Read contents of script back
func (vm *VM) read() ([]byte, error) {
if vm.config.Verbose {
log.Debugf(log.GCTScriptMgr, "Read script: %s ID: %v", vm.ShortName(), vm.ID)
}
return os.ReadFile(vm.File)
}
// ShortName returns short (just filename.extension) of running script
func (vm *VM) ShortName() string {
return filepath.Base(vm.File)
}
func (vm *VM) event(status, executionType string) {
if validator.IsTestExecution.Load() == true {
return
}
var data null.Bytes
if executionType == TypeLoad {
scriptData, err := vm.scriptData()
if err != nil {
log.Errorf(log.GCTScriptMgr, "Failed to retrieve scriptData: %v", err)
}
data.SetValid(scriptData)
}
scriptevent.Event(vm.getHash(), vm.ShortName(), vm.Path, data, executionType, status, time.Now())
}
func (vm *VM) scriptData() ([]byte, error) {
contents, err := vm.read()
if err != nil {
return nil, err
}
buf := new(bytes.Buffer)
w := zip.NewWriter(buf)
f, err := w.Create(vm.ShortName())
if err != nil {
return nil, err
}
_, err = f.Write(contents)
if err != nil {
return nil, err
}
err = w.Close()
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func (vm *VM) getHash() string {
if vm.Hash != "" {
return vm.Hash
}
contents, err := vm.read()
if err != nil {
log.Errorln(log.GCTScriptMgr, err)
}
contents = append(contents, vm.ShortName()...)
hash, err := crypto.GetSHA256(contents)
if err != nil {
log.Errorln(log.GCTScriptMgr, err)
}
return hex.EncodeToString(hash)
}
func (vmc *vmscount) add() {
atomic.AddInt32((*int32)(vmc), 1)
}
func (vmc *vmscount) remove() {
atomic.AddInt32((*int32)(vmc), -1)
}
// Len() returns current length vmscount
func (vmc *vmscount) Len() int32 {
return atomic.LoadInt32((*int32)(vmc))
}