Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

signer/rules:use goja instead of otto #685

Merged
merged 6 commits into from
Jul 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ require (
github.com/cespare/cp v1.1.1
github.com/cloudflare/cloudflare-go v0.11.7
github.com/davecgh/go-spew v1.1.1
github.com/dlclark/regexp2 v1.2.0 // indirect
github.com/docker/docker v1.13.1
github.com/dop251/goja v0.0.0-20200629185240-bfd59704b500
github.com/fjl/memsize v0.0.0-20190710130421-bcb5799ab5e5
github.com/gballet/go-libpcsclite v0.0.0-20191108122812-4678299bea08
github.com/go-resty/resty/v2 v2.3.1-0.20200619075926-b87f65ce5ed5
github.com/go-sourcemap/sourcemap v2.1.3+incompatible // indirect
github.com/go-stack/stack v1.8.0
github.com/golang/snappy v0.0.2-0.20200707131729-196ae77b8a26
github.com/gorilla/websocket v1.4.2
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,13 @@ github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumC
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/dgryski/go-bitstream v0.0.0-20180413035011-3522498ce2c8/go.mod h1:VMaSuZ+SZcx/wljOQKvp5srsbCiKDEb6K2wC4+PiBmQ=
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
github.com/dlclark/regexp2 v1.2.0 h1:8sAhBGEM0dRWogWqWyQeIJnxjWO6oIjl8FKqREDsGfk=
github.com/dlclark/regexp2 v1.2.0/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc=
github.com/docker/docker v1.13.1 h1:IkZjBSIc8hBjLpqeAbeE5mca5mNgeatLHBy3GO78BWo=
github.com/docker/docker v1.13.1/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3ebgob9U8Nd0kOddGdZWjyMGR8Wziv+TBNwSE=
github.com/dop251/goja v0.0.0-20200629185240-bfd59704b500 h1:QthjkRYZQj+FcH5GZXltnlBiyW19WLb+l7R0TrZChNw=
github.com/dop251/goja v0.0.0-20200629185240-bfd59704b500/go.mod h1:Mw6PkjjMXWbTj+nnj4s3QPXq1jaT0s5pC0iFD4+BOAA=
github.com/dustin/go-humanize v0.0.0-20171111073723-bb3d318650d4/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/dustin/go-humanize v0.0.0-20180421182945-02af3965c54e/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo=
Expand Down Expand Up @@ -337,6 +341,8 @@ github.com/go-ole/go-ole v1.2.4/go.mod h1:XCwSNxSkXRo4vlyPy93sltvi/qJq0jqQhjqQNI
github.com/go-resty/resty/v2 v2.3.0-rc.1/go.mod h1:nYW/8rxqQCmI3bPz9Fsmjbr2FBjGuR2Mzt6kDh3zZ7w=
github.com/go-resty/resty/v2 v2.3.1-0.20200619075926-b87f65ce5ed5 h1:YoSgv9FoJzoKixy842sZLsR0ImftVRRt/r0uiBDFm7I=
github.com/go-resty/resty/v2 v2.3.1-0.20200619075926-b87f65ce5ed5/go.mod h1:UpN9CgLZNsv4e9XG50UU8xdI0F43UQ4HmxLBDwaroHU=
github.com/go-sourcemap/sourcemap v2.1.3+incompatible h1:W1iEw64niKVGogNgBN3ePyLFfuisuzeidWPMPWmECqU=
github.com/go-sourcemap/sourcemap v2.1.3+incompatible/go.mod h1:F8jJfvm2KbVjc5NqelyYJmf/v5J0dwNLS2mL4sNA1Jg=
github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
github.com/go-sql-driver/mysql v1.4.1/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
Expand Down
56 changes: 34 additions & 22 deletions signer/rules/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
"github.com/CortexFoundation/CortexTheseus/signer/core"
"github.com/CortexFoundation/CortexTheseus/signer/rules/deps"
"github.com/CortexFoundation/CortexTheseus/signer/storage"
"github.com/robertkrimen/otto"
"github.com/dop251/goja"
)

var (
Expand All @@ -37,13 +37,13 @@ var (

// consoleOutput is an override for the console.log and console.error methods to
// stream the output into the configured output stream instead of stdout.
func consoleOutput(call otto.FunctionCall) otto.Value {
func consoleOutput(call goja.FunctionCall) goja.Value {
output := []string{"JS:> "}
for _, argument := range call.ArgumentList {
for _, argument := range call.Arguments {
output = append(output, fmt.Sprintf("%v", argument))
}
fmt.Fprintln(os.Stdout, strings.Join(output, " "))
return otto.Value{}
return goja.Undefined()
}

// rulesetUI provides an implementation of SignerUI that evaluates a javascript
Expand All @@ -70,29 +70,45 @@ func (r *rulesetUI) Init(javascriptRules string) error {
r.jsRules = javascriptRules
return nil
}
func (r *rulesetUI) execute(jsfunc string, jsarg interface{}) (otto.Value, error) {
func (r *rulesetUI) execute(jsfunc string, jsarg interface{}) (goja.Value, error) {

// Instantiate a fresh vm engine every time
vm := otto.New()
vm := goja.New()
// Set the native callbacks
consoleObj, _ := vm.Get("console")
consoleObj.Object().Set("log", consoleOutput)
consoleObj.Object().Set("error", consoleOutput)
vm.Set("storage", r.storage)
consoleObj := vm.NewObject()
consoleObj.Set("log", consoleOutput)
consoleObj.Set("error", consoleOutput)
vm.Set("console", consoleObj)

storageObj := vm.NewObject()
storageObj.Set("Put", func(call goja.FunctionCall) goja.Value {
key, val := call.Argument(0).String(), call.Argument(1).String()
if val == "" {
r.storage.Del(key)
} else {
r.storage.Put(key, val)
}
return goja.Null()
})
storageObj.Set("Get", func(call goja.FunctionCall) goja.Value {
goval := r.storage.Get(call.Argument(0).String())
jsval := vm.ToValue(goval)
return jsval
})
vm.Set("storage", storageObj)
// Load bootstrap libraries
script, err := vm.Compile("bignumber.js", BigNumber_JS)
script, err := goja.Compile("bignumber.js", string(BigNumber_JS), true)
if err != nil {
log.Warn("Failed loading libraries", "err", err)
return otto.UndefinedValue(), err
return goja.Undefined(), err
}
vm.Run(script)
vm.RunProgram(script)

// Run the actual rule implementation
_, err = vm.Run(r.jsRules)
_, err = vm.RunString(r.jsRules)
if err != nil {
log.Warn("Execution failed", "err", err)
return otto.UndefinedValue(), err
return goja.Undefined(), err
}

// And the actual call
Expand All @@ -103,7 +119,7 @@ func (r *rulesetUI) execute(jsfunc string, jsarg interface{}) (otto.Value, error
jsonbytes, err := json.Marshal(jsarg)
if err != nil {
log.Warn("failed marshalling data", "data", jsarg)
return otto.UndefinedValue(), err
return goja.Undefined(), err
}
// Now, we call foobar(JSON.parse(<jsondata>)).
var call string
Expand All @@ -112,7 +128,7 @@ func (r *rulesetUI) execute(jsfunc string, jsarg interface{}) (otto.Value, error
} else {
call = fmt.Sprintf("%v()", jsfunc)
}
return vm.Run(call)
return vm.RunString(call)
}

func (r *rulesetUI) checkApproval(jsfunc string, jsarg []byte, err error) (bool, error) {
Expand All @@ -124,11 +140,7 @@ func (r *rulesetUI) checkApproval(jsfunc string, jsarg []byte, err error) (bool,
log.Info("error occurred during execution", "error", err)
return false, err
}
result, err := v.ToString()
if err != nil {
log.Info("error occurred during response unmarshalling", "error", err)
return false, err
}
result := v.ToString().String()
if result == "Approve" {
log.Info("Op approved")
return true, nil
Expand Down
2 changes: 1 addition & 1 deletion signer/rules/rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ func TestStorage(t *testing.T) {
t.Errorf("Unexpected error %v", err)
}

retval, err := v.ToString()
retval := v.ToString().String()

if err != nil {
t.Errorf("Unexpected error %v", err)
Expand Down
7 changes: 7 additions & 0 deletions signer/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ type Storage interface {
Put(key, value string)
// Get returns the previously stored value, or the empty string if it does not exist or key is of 0-length
Get(key string) string
//Del removes a key-value pair. If the key doesn't exist, the method is a noop.
Del(key string)
}

// EphemeralStorage is an in-memory storage that does
Expand Down Expand Up @@ -54,6 +56,11 @@ func (s *EphemeralStorage) Get(key string) string {
return ""
}

// Del removes a key-value pair. If the key doesn't exist, the method is a noop.
func (s *EphemeralStorage) Del(key string) {
delete(s.data, key)
}

func NewEphemeralStorage() Storage {
s := &EphemeralStorage{
data: make(map[string]string),
Expand Down
27 changes: 27 additions & 0 deletions vendor/github.com/dlclark/regexp2/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions vendor/github.com/dlclark/regexp2/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

133 changes: 133 additions & 0 deletions vendor/github.com/dlclark/regexp2/ATTRIB

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions vendor/github.com/dlclark/regexp2/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.