Files
gocryptotrader/gctscript/vm/vm.go
Ryan O'Hara-Reid e93ee83563 script: implementation of error insertion on return (#986)
* exchanges/account: shift credentials to account package and segregate funds to keys

* merge: fixes

* linter: fix

* Update exchanges/account/account.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* glorious: nits + protection for string panic

* glorious_suggestion: add method for matching keys

* linter: fix tests

* account: add protected method for credentials minimizing access, display full account details to rpc.

* linter: spelling kweeeeeeen

* accounts/portfolio: clean/check portfolio code and quickly check balances from change. Add protected method for future matching.

* accounts: theres no point in pointerising everything

* linter: ok pointerise this then...

* exchanges: fix regression add in little notes.

* glorious: nits

* Update exchanges/account/credentials.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* Update exchanges/account/credentials_test.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* Update exchanges/account/credentials_test.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* glorious: nits

* gloriously: fix glorious glorious test gloriously

* script: initial implementation of error insertion on return

* script: make script context aware(ish) and update error handle in examples

* script: add tests

* script: add syntax highlighting to readme

* Update gctscript/vm/vm.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* Update gctscript/vm/vm.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* Update gctscript/examples/exchange/account_info.gct

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* Update gctscript/examples/exchange/cancel_order.gct

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* Update gctscript/examples/verbose.gct

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* Update gctscript/modules/gct/gct_test.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* glorious: nits

* rm: bros

* scripts: handle errors in examples when they are going to use the data after fetching

* linter: fix rides again

* SCOTT_SPELL_CHECK_LINTER: fix

* gctscript: fix tests

* glorious: niiiiiiiiiiiiits

* scriptmodules/gct: standardize runtime errors

* Update gctscript/modules/gct/exchange.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* Update gctscript/modules/gct/exchange.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* Update gctscript/modules/gct/exchange.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* Update gctscript/modules/gct/exchange.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* Update gctscript/modules/gct/gct.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* Update gctscript/modules/gct/gct.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* Update gctscript/modules/gct/gct.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* Update gctscript/modules/gct/gct.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* Update gctscript/modules/gct/gct.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* Update gctscript/modules/gct/gct.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* Update gctscript/modules/gct/gct.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* glorious: nits/reverts

* go mod: tidy

Co-authored-by: Ryan O'Hara-Reid <ryan.oharareid@thrasher.io>
Co-authored-by: Scott <gloriousCode@users.noreply.github.com>
2022-08-17 14:18:53 +10:00

291 lines
6.4 KiB
Go

package vm
import (
"archive/zip"
"bytes"
"context"
"encoding/hex"
"errors"
"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.Error(log.GCTScriptMgr, Error{
Action: "NewVM",
Cause: ErrScriptingDisabled,
})
return nil
}
newUUID, err := uuid.NewV4()
if err != nil {
log.Error(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.Error(log.GCTScriptMgr, Error{
Action: "NewVM",
Cause: errors.New("unable to type assert tengo script"),
})
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.Error(log.GCTScriptMgr, err)
err = vm.unregister()
if err != nil {
log.Error(log.GCTScriptMgr, err)
}
return
}
err = vm.RunCtx()
if err != nil {
log.Error(log.GCTScriptMgr, err)
err = vm.unregister()
if err != nil {
log.Error(log.GCTScriptMgr, err)
}
return
}
if vm.Compiled.Get("timer").String() != "" {
vm.T, err = time.ParseDuration(vm.Compiled.Get("timer").String())
if err != nil {
log.Error(log.GCTScriptMgr, err)
err = vm.Shutdown()
if err != nil {
log.Error(log.GCTScriptMgr, err)
}
return
}
if vm.T > 0 {
vm.runner()
return
}
if vm.T < 0 {
log.Error(log.GCTScriptMgr, "Repeat timer cannot be under 1 nano second")
}
}
err = vm.Shutdown()
if err != nil {
log.Error(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))
}