2016-09-11 11:44:14 +00:00
|
|
|
package jail
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/robertkrimen/otto"
|
2017-05-16 12:09:52 +00:00
|
|
|
"github.com/status-im/status-go/geth/common"
|
2017-09-18 12:13:32 +00:00
|
|
|
"github.com/status-im/status-go/geth/jail/internal/vm"
|
2017-08-10 13:35:58 +00:00
|
|
|
"github.com/status-im/status-go/geth/log"
|
2017-04-06 19:36:55 +00:00
|
|
|
"github.com/status-im/status-go/static"
|
2016-10-07 14:48:36 +00:00
|
|
|
)
|
|
|
|
|
2017-08-04 16:14:17 +00:00
|
|
|
// FIXME(tiabc): Get rid of this global variable. Move it to a constructor or initialization.
|
2017-05-16 12:09:52 +00:00
|
|
|
var web3JSCode = static.MustAsset("scripts/web3.js")
|
|
|
|
|
2017-05-03 14:24:48 +00:00
|
|
|
// errors
|
2016-09-11 11:44:14 +00:00
|
|
|
var (
|
|
|
|
ErrInvalidJail = errors.New("jail environment is not properly initialized")
|
|
|
|
)
|
|
|
|
|
2017-05-16 12:09:52 +00:00
|
|
|
// Jail represents jailed environment inside of which we hold multiple cells.
|
|
|
|
// Each cell is a separate JavaScript VM.
|
|
|
|
type Jail struct {
|
2017-08-07 10:48:14 +00:00
|
|
|
// FIXME(tiabc): This mutex handles cells field access and must be renamed appropriately: cellsMutex
|
2017-05-16 12:09:52 +00:00
|
|
|
sync.RWMutex
|
2017-08-15 10:27:12 +00:00
|
|
|
nodeManager common.NodeManager
|
|
|
|
accountManager common.AccountManager
|
2017-09-04 12:56:58 +00:00
|
|
|
txQueueManager common.TxQueueManager
|
2017-08-15 10:27:12 +00:00
|
|
|
policy *ExecutionPolicy
|
2017-09-02 17:04:23 +00:00
|
|
|
cells map[string]*Cell // jail supports running many isolated instances of jailed runtime
|
|
|
|
baseJSCode string // JavaScript used to initialize all new cells with
|
2017-07-13 11:04:47 +00:00
|
|
|
}
|
|
|
|
|
2017-08-15 10:27:12 +00:00
|
|
|
// New returns new Jail environment with the associated NodeManager and
|
|
|
|
// AccountManager.
|
2017-09-04 12:56:58 +00:00
|
|
|
func New(
|
|
|
|
nodeManager common.NodeManager, accountManager common.AccountManager, txQueueManager common.TxQueueManager,
|
|
|
|
) *Jail {
|
2017-09-18 12:13:32 +00:00
|
|
|
if nodeManager == nil || accountManager == nil || txQueueManager == nil {
|
|
|
|
panic("Jail is missing mandatory dependencies")
|
|
|
|
}
|
2017-05-16 12:09:52 +00:00
|
|
|
return &Jail{
|
2017-08-15 10:27:12 +00:00
|
|
|
nodeManager: nodeManager,
|
|
|
|
accountManager: accountManager,
|
2017-09-04 12:56:58 +00:00
|
|
|
txQueueManager: txQueueManager,
|
2017-09-02 17:04:23 +00:00
|
|
|
cells: make(map[string]*Cell),
|
2017-09-04 12:56:58 +00:00
|
|
|
policy: NewExecutionPolicy(nodeManager, accountManager, txQueueManager),
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
2017-08-04 16:14:17 +00:00
|
|
|
// BaseJS allows to setup initial JavaScript to be loaded on each jail.Parse().
|
2017-05-16 12:09:52 +00:00
|
|
|
func (jail *Jail) BaseJS(js string) {
|
|
|
|
jail.baseJSCode = js
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
2017-09-02 17:04:23 +00:00
|
|
|
// NewCell initializes and returns a new jail cell.
|
|
|
|
func (jail *Jail) NewCell(chatID string) (common.JailCell, error) {
|
2017-08-04 16:14:17 +00:00
|
|
|
if jail == nil {
|
|
|
|
return nil, ErrInvalidJail
|
|
|
|
}
|
|
|
|
|
2017-07-13 11:04:47 +00:00
|
|
|
vm := otto.New()
|
|
|
|
|
2017-09-02 17:04:23 +00:00
|
|
|
cell, err := newCell(chatID, vm)
|
2017-07-13 11:04:47 +00:00
|
|
|
if err != nil {
|
2017-08-04 16:14:17 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
jail.Lock()
|
2017-09-02 17:04:23 +00:00
|
|
|
jail.cells[chatID] = cell
|
2017-08-04 16:14:17 +00:00
|
|
|
jail.Unlock()
|
|
|
|
|
2017-09-02 17:04:23 +00:00
|
|
|
return cell, nil
|
2017-08-04 16:14:17 +00:00
|
|
|
}
|
|
|
|
|
2017-09-02 17:04:23 +00:00
|
|
|
// Cell returns the existing instance of Cell.
|
|
|
|
func (jail *Jail) Cell(chatID string) (common.JailCell, error) {
|
2017-08-04 16:14:17 +00:00
|
|
|
jail.RLock()
|
|
|
|
defer jail.RUnlock()
|
|
|
|
|
|
|
|
cell, ok := jail.cells[chatID]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("cell[%s] doesn't exist", chatID)
|
2016-10-07 14:48:36 +00:00
|
|
|
}
|
2017-07-13 11:04:47 +00:00
|
|
|
|
2017-08-04 16:14:17 +00:00
|
|
|
return cell, nil
|
2016-10-07 14:48:36 +00:00
|
|
|
}
|
|
|
|
|
2017-05-16 12:09:52 +00:00
|
|
|
// Parse creates a new jail cell context, with the given chatID as identifier.
|
2017-05-03 14:24:48 +00:00
|
|
|
// New context executes provided JavaScript code, right after the initialization.
|
2017-09-02 17:04:23 +00:00
|
|
|
func (jail *Jail) Parse(chatID, js string) string {
|
2016-09-11 11:44:14 +00:00
|
|
|
if jail == nil {
|
2017-05-16 12:09:52 +00:00
|
|
|
return makeError(ErrInvalidJail.Error())
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
2017-09-02 17:04:23 +00:00
|
|
|
cell, err := jail.Cell(chatID)
|
|
|
|
if err != nil {
|
|
|
|
if _, mkerr := jail.NewCell(chatID); mkerr != nil {
|
2017-08-04 16:14:17 +00:00
|
|
|
return makeError(mkerr.Error())
|
|
|
|
}
|
2016-10-13 18:02:48 +00:00
|
|
|
|
2017-09-02 17:04:23 +00:00
|
|
|
cell, _ = jail.Cell(chatID)
|
2017-08-04 16:14:17 +00:00
|
|
|
}
|
2016-10-07 14:48:36 +00:00
|
|
|
|
2017-08-04 16:14:17 +00:00
|
|
|
// init jeth and its handlers
|
2017-09-02 17:04:23 +00:00
|
|
|
if err = cell.Set("jeth", struct{}{}); err != nil {
|
2017-05-16 12:09:52 +00:00
|
|
|
return makeError(err.Error())
|
2017-05-03 14:24:48 +00:00
|
|
|
}
|
2016-09-11 11:44:14 +00:00
|
|
|
|
2017-09-02 17:04:23 +00:00
|
|
|
if err = registerHandlers(jail, cell, chatID); err != nil {
|
2017-05-16 12:09:52 +00:00
|
|
|
return makeError(err.Error())
|
2017-05-03 14:24:48 +00:00
|
|
|
}
|
2017-08-04 16:14:17 +00:00
|
|
|
|
|
|
|
initJs := jail.baseJSCode + ";"
|
2017-09-02 17:04:23 +00:00
|
|
|
if _, err = cell.Run(initJs); err != nil {
|
2017-05-16 12:09:52 +00:00
|
|
|
return makeError(err.Error())
|
2017-05-03 14:24:48 +00:00
|
|
|
}
|
2016-11-28 18:30:25 +00:00
|
|
|
|
2017-05-16 12:09:52 +00:00
|
|
|
jjs := string(web3JSCode) + `
|
2016-09-11 11:44:14 +00:00
|
|
|
var Web3 = require('web3');
|
|
|
|
var web3 = new Web3(jeth);
|
|
|
|
var Bignumber = require("bignumber.js");
|
|
|
|
function bn(val){
|
|
|
|
return new Bignumber(val);
|
|
|
|
}
|
|
|
|
` + js + "; var catalog = JSON.stringify(_status_catalog);"
|
2017-09-02 17:04:23 +00:00
|
|
|
if _, err = cell.Run(jjs); err != nil {
|
2017-05-16 12:09:52 +00:00
|
|
|
return makeError(err.Error())
|
2017-05-03 14:24:48 +00:00
|
|
|
}
|
2016-09-11 11:44:14 +00:00
|
|
|
|
2017-09-02 17:04:23 +00:00
|
|
|
res, err := cell.Get("catalog")
|
2017-05-03 14:24:48 +00:00
|
|
|
if err != nil {
|
2017-05-16 12:09:52 +00:00
|
|
|
return makeError(err.Error())
|
2017-05-03 14:24:48 +00:00
|
|
|
}
|
2016-09-11 11:44:14 +00:00
|
|
|
|
2017-05-16 12:09:52 +00:00
|
|
|
return makeResult(res.String(), err)
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
2017-08-04 16:14:17 +00:00
|
|
|
// Call executes the `call` function w/i a jail cell context identified by the chatID.
|
2017-09-02 17:04:23 +00:00
|
|
|
func (jail *Jail) Call(chatID, this, args string) string {
|
|
|
|
cell, err := jail.Cell(chatID)
|
2017-07-13 11:04:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return makeError(err.Error())
|
|
|
|
}
|
|
|
|
|
2017-09-02 17:04:23 +00:00
|
|
|
res, err := cell.Call("call", nil, this, args)
|
2016-10-13 18:02:48 +00:00
|
|
|
|
2017-08-04 16:14:17 +00:00
|
|
|
return makeResult(res.String(), err)
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Send will serialize the first argument, send it to the node and returns the response.
|
2017-09-18 12:13:32 +00:00
|
|
|
// IMPORTANT: Don't use `call.Otto` in this function unless you want to run into race conditions. Use `vm` instead.
|
2017-05-03 14:24:48 +00:00
|
|
|
// nolint: errcheck, unparam
|
2017-09-18 12:13:32 +00:00
|
|
|
func (jail *Jail) Send(call otto.FunctionCall, vm *vm.VM) otto.Value {
|
|
|
|
reqVal, err := vm.Call("JSON.stringify", nil, call.Argument(0))
|
2016-09-11 11:44:14 +00:00
|
|
|
if err != nil {
|
2017-09-18 12:13:32 +00:00
|
|
|
throwJSException(err)
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
2017-08-15 10:27:12 +00:00
|
|
|
|
2016-09-11 11:44:14 +00:00
|
|
|
var (
|
|
|
|
rawReq = []byte(reqVal.String())
|
2017-08-15 10:27:12 +00:00
|
|
|
reqs []common.RPCCall
|
2016-12-18 20:36:17 +00:00
|
|
|
batch bool
|
2016-09-11 11:44:14 +00:00
|
|
|
)
|
2017-08-15 10:27:12 +00:00
|
|
|
|
2016-09-11 11:44:14 +00:00
|
|
|
if rawReq[0] == '[' {
|
|
|
|
batch = true
|
2017-09-18 12:13:32 +00:00
|
|
|
err = json.Unmarshal(rawReq, &reqs)
|
2016-09-11 11:44:14 +00:00
|
|
|
} else {
|
|
|
|
batch = false
|
2017-08-15 10:27:12 +00:00
|
|
|
reqs = make([]common.RPCCall, 1)
|
2017-09-18 12:13:32 +00:00
|
|
|
err = json.Unmarshal(rawReq, &reqs[0])
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
throwJSException(fmt.Errorf("can't unmarshal %v (batch=%v): %s", string(rawReq), batch, err))
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
2017-09-18 12:13:32 +00:00
|
|
|
resps, err := vm.Call("new Array", nil)
|
|
|
|
if err != nil {
|
|
|
|
throwJSException(fmt.Errorf("can't create Array: %s", err))
|
|
|
|
}
|
2017-08-15 10:27:12 +00:00
|
|
|
|
|
|
|
// Execute the requests.
|
2016-09-11 11:44:14 +00:00
|
|
|
for _, req := range reqs {
|
2017-09-15 10:35:31 +00:00
|
|
|
log.Info("execute request", "method", req.Method)
|
2017-09-18 12:13:32 +00:00
|
|
|
res, err := jail.policy.Execute(req, vm)
|
2017-09-04 12:56:58 +00:00
|
|
|
if err != nil {
|
2017-09-15 10:35:31 +00:00
|
|
|
log.Info("request errored", "error", err.Error())
|
2017-09-04 12:56:58 +00:00
|
|
|
switch err.(type) {
|
2017-08-15 10:27:12 +00:00
|
|
|
case common.StopRPCCallError:
|
2017-09-18 12:13:32 +00:00
|
|
|
return newErrorResponseOtto(vm, err.Error(), nil)
|
2017-08-15 10:27:12 +00:00
|
|
|
default:
|
2017-09-18 12:13:32 +00:00
|
|
|
res = newErrorResponse(err.Error(), &req.ID)
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
}
|
2016-10-12 17:51:25 +00:00
|
|
|
|
2017-09-18 12:13:32 +00:00
|
|
|
_, err = resps.Object().Call("push", res)
|
|
|
|
if err != nil {
|
|
|
|
throwJSException(fmt.Errorf("can't push result: %s", err))
|
|
|
|
}
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return the responses either to the callback (if supplied)
|
|
|
|
// or directly as the return value.
|
|
|
|
if batch {
|
2017-09-18 12:13:32 +00:00
|
|
|
return resps
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
2017-09-18 12:13:32 +00:00
|
|
|
v, err := resps.Object().Get("0")
|
|
|
|
if err != nil {
|
|
|
|
throwJSException(err)
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
2017-09-18 12:13:32 +00:00
|
|
|
return v
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
2017-09-18 12:13:32 +00:00
|
|
|
func newErrorResponse(msg string, id interface{}) map[string]interface{} {
|
2016-09-11 11:44:14 +00:00
|
|
|
// Bundle the error into a JSON RPC call response
|
2017-09-18 12:13:32 +00:00
|
|
|
return map[string]interface{}{
|
2017-09-14 20:14:31 +00:00
|
|
|
"jsonrpc": "2.0",
|
|
|
|
"id": id,
|
|
|
|
"error": map[string]interface{}{
|
2017-09-18 12:13:32 +00:00
|
|
|
"code": -32603, // Internal JSON-RPC Error, see http://www.jsonrpc.org/specification#error_object
|
2017-09-14 20:14:31 +00:00
|
|
|
"message": msg,
|
|
|
|
},
|
|
|
|
}
|
2017-09-18 12:13:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newErrorResponseOtto(vm *vm.VM, msg string, id interface{}) otto.Value {
|
|
|
|
// TODO(tiabc): Handle errors.
|
|
|
|
errResp, _ := json.Marshal(newErrorResponse(msg, id))
|
|
|
|
errRespVal, _ := vm.Run("(" + string(errResp) + ")")
|
|
|
|
return errRespVal
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
2017-08-04 16:14:17 +00:00
|
|
|
func newResultResponse(vm *otto.Otto, result interface{}) otto.Value {
|
|
|
|
resp, _ := vm.Object(`({"jsonrpc":"2.0"})`)
|
2017-05-03 14:24:48 +00:00
|
|
|
resp.Set("result", result) // nolint: errcheck
|
2016-12-18 20:36:17 +00:00
|
|
|
|
|
|
|
return resp.Value()
|
|
|
|
}
|
|
|
|
|
2016-09-11 11:44:14 +00:00
|
|
|
// throwJSException panics on an otto.Value. The Otto VM will recover from the
|
|
|
|
// Go panic and throw msg as a JavaScript error.
|
2017-09-18 12:13:32 +00:00
|
|
|
func throwJSException(msg error) otto.Value {
|
|
|
|
val, err := otto.ToValue(msg.Error())
|
2016-09-11 11:44:14 +00:00
|
|
|
if err != nil {
|
2017-09-18 12:13:32 +00:00
|
|
|
log.Error(fmt.Sprintf("Failed to serialize JavaScript exception %v: %v", msg.Error(), err))
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
panic(val)
|
|
|
|
}
|
|
|
|
|
2017-05-16 12:09:52 +00:00
|
|
|
// JSONError is wrapper around errors, that are sent upwards
|
|
|
|
type JSONError struct {
|
|
|
|
Error string `json:"error"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeError(error string) string {
|
|
|
|
str := JSONError{
|
2016-09-11 11:44:14 +00:00
|
|
|
Error: error,
|
|
|
|
}
|
|
|
|
outBytes, _ := json.Marshal(&str)
|
|
|
|
return string(outBytes)
|
|
|
|
}
|
|
|
|
|
2017-05-16 12:09:52 +00:00
|
|
|
func makeResult(res string, err error) string {
|
2016-09-11 11:44:14 +00:00
|
|
|
var out string
|
|
|
|
if err != nil {
|
2017-05-16 12:09:52 +00:00
|
|
|
out = makeError(err.Error())
|
2016-09-11 11:44:14 +00:00
|
|
|
} else {
|
|
|
|
if "undefined" == res {
|
|
|
|
res = "null"
|
|
|
|
}
|
|
|
|
out = fmt.Sprintf(`{"result": %s}`, res)
|
|
|
|
}
|
|
|
|
|
|
|
|
return out
|
|
|
|
}
|