2016-11-08 19:01:56 -06:00
|
|
|
// Copyright 2016 The go-ethereum Authors
|
2016-05-06 04:40:23 -05:00
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package console
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2023-05-25 07:24:09 -05:00
|
|
|
"errors"
|
2016-05-06 04:40:23 -05:00
|
|
|
"io"
|
2020-01-27 04:50:48 -06:00
|
|
|
"reflect"
|
2017-05-02 06:42:55 -05:00
|
|
|
"strings"
|
2016-05-06 04:40:23 -05:00
|
|
|
"time"
|
|
|
|
|
2020-01-27 04:50:48 -06:00
|
|
|
"github.com/dop251/goja"
|
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2020-05-19 03:44:46 -05:00
|
|
|
"github.com/ethereum/go-ethereum/console/prompt"
|
2020-01-27 04:50:48 -06:00
|
|
|
"github.com/ethereum/go-ethereum/internal/jsre"
|
2016-05-06 04:40:23 -05:00
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
|
|
|
)
|
|
|
|
|
|
|
|
// bridge is a collection of JavaScript utility methods to bride the .js runtime
|
|
|
|
// environment and the Go RPC connection backing the remote method calls.
|
|
|
|
type bridge struct {
|
2020-05-19 03:44:46 -05:00
|
|
|
client *rpc.Client // RPC client to execute Ethereum requests through
|
|
|
|
prompter prompt.UserPrompter // Input prompter to allow interactive user feedback
|
|
|
|
printer io.Writer // Output writer to serialize any display strings to
|
2016-05-06 04:40:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// newBridge creates a new JavaScript wrapper around an RPC client.
|
2020-05-19 03:44:46 -05:00
|
|
|
func newBridge(client *rpc.Client, prompter prompt.UserPrompter, printer io.Writer) *bridge {
|
2016-05-06 04:40:23 -05:00
|
|
|
return &bridge{
|
|
|
|
client: client,
|
|
|
|
prompter: prompter,
|
|
|
|
printer: printer,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sleep will block the console for the specified number of seconds.
|
2020-01-27 04:50:48 -06:00
|
|
|
func (b *bridge) Sleep(call jsre.Call) (goja.Value, error) {
|
2020-05-11 04:59:21 -05:00
|
|
|
if nArgs := len(call.Arguments); nArgs < 1 {
|
2023-05-25 07:24:09 -05:00
|
|
|
return nil, errors.New("usage: sleep(<number of seconds>)")
|
2020-05-11 04:59:21 -05:00
|
|
|
}
|
2020-06-02 11:06:22 -05:00
|
|
|
sleepObj := call.Argument(0)
|
|
|
|
if goja.IsUndefined(sleepObj) || goja.IsNull(sleepObj) || !isNumber(sleepObj) {
|
2023-05-25 07:24:09 -05:00
|
|
|
return nil, errors.New("usage: sleep(<number of seconds>)")
|
2016-05-06 04:40:23 -05:00
|
|
|
}
|
2020-06-02 11:06:22 -05:00
|
|
|
sleep := sleepObj.ToFloat()
|
2020-01-27 04:50:48 -06:00
|
|
|
time.Sleep(time.Duration(sleep * float64(time.Second)))
|
|
|
|
return call.VM.ToValue(true), nil
|
2016-05-06 04:40:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// SleepBlocks will block the console for a specified number of new blocks optionally
|
|
|
|
// until the given timeout is reached.
|
2020-01-27 04:50:48 -06:00
|
|
|
func (b *bridge) SleepBlocks(call jsre.Call) (goja.Value, error) {
|
|
|
|
// Parse the input parameters for the sleep.
|
2016-05-06 04:40:23 -05:00
|
|
|
var (
|
|
|
|
blocks = int64(0)
|
|
|
|
sleep = int64(9999999999999999) // indefinitely
|
|
|
|
)
|
2020-01-27 04:50:48 -06:00
|
|
|
nArgs := len(call.Arguments)
|
2016-05-06 04:40:23 -05:00
|
|
|
if nArgs == 0 {
|
2023-05-25 07:24:09 -05:00
|
|
|
return nil, errors.New("usage: sleepBlocks(<n blocks>[, max sleep in seconds])")
|
2016-05-06 04:40:23 -05:00
|
|
|
}
|
|
|
|
if nArgs >= 1 {
|
2020-06-02 11:06:22 -05:00
|
|
|
if goja.IsNull(call.Argument(0)) || goja.IsUndefined(call.Argument(0)) || !isNumber(call.Argument(0)) {
|
2023-05-25 07:24:09 -05:00
|
|
|
return nil, errors.New("expected number as first argument")
|
2016-05-06 04:40:23 -05:00
|
|
|
}
|
2020-01-27 04:50:48 -06:00
|
|
|
blocks = call.Argument(0).ToInteger()
|
2016-05-06 04:40:23 -05:00
|
|
|
}
|
|
|
|
if nArgs >= 2 {
|
2020-06-02 11:06:22 -05:00
|
|
|
if goja.IsNull(call.Argument(1)) || goja.IsUndefined(call.Argument(1)) || !isNumber(call.Argument(1)) {
|
2023-05-25 07:24:09 -05:00
|
|
|
return nil, errors.New("expected number as second argument")
|
2016-05-06 04:40:23 -05:00
|
|
|
}
|
2020-01-27 04:50:48 -06:00
|
|
|
sleep = call.Argument(1).ToInteger()
|
2016-05-06 04:40:23 -05:00
|
|
|
}
|
2020-01-27 04:50:48 -06:00
|
|
|
|
|
|
|
// Poll the current block number until either it or a timeout is reached.
|
2020-10-13 03:55:57 -05:00
|
|
|
deadline := time.Now().Add(time.Duration(sleep) * time.Second)
|
|
|
|
var lastNumber hexutil.Uint64
|
|
|
|
if err := b.client.Call(&lastNumber, "eth_blockNumber"); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-27 04:50:48 -06:00
|
|
|
for time.Now().Before(deadline) {
|
|
|
|
var number hexutil.Uint64
|
2020-10-13 03:55:57 -05:00
|
|
|
if err := b.client.Call(&number, "eth_blockNumber"); err != nil {
|
2020-01-27 04:50:48 -06:00
|
|
|
return nil, err
|
2016-05-06 04:40:23 -05:00
|
|
|
}
|
2020-01-27 04:50:48 -06:00
|
|
|
if number != lastNumber {
|
|
|
|
lastNumber = number
|
|
|
|
blocks--
|
2016-05-06 04:40:23 -05:00
|
|
|
}
|
2020-01-27 04:50:48 -06:00
|
|
|
if blocks <= 0 {
|
|
|
|
break
|
2016-05-06 04:40:23 -05:00
|
|
|
}
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
}
|
2020-01-27 04:50:48 -06:00
|
|
|
return call.VM.ToValue(true), nil
|
2016-05-06 04:40:23 -05:00
|
|
|
}
|
|
|
|
|
2016-07-12 10:47:15 -05:00
|
|
|
type jsonrpcCall struct {
|
2018-05-31 06:59:08 -05:00
|
|
|
ID int64
|
2016-07-12 10:47:15 -05:00
|
|
|
Method string
|
|
|
|
Params []interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send implements the web3 provider "send" method.
|
2020-01-27 04:50:48 -06:00
|
|
|
func (b *bridge) Send(call jsre.Call) (goja.Value, error) {
|
2016-07-12 10:47:15 -05:00
|
|
|
// Remarshal the request into a Go value.
|
2020-01-27 04:50:48 -06:00
|
|
|
reqVal, err := call.Argument(0).ToObject(call.VM).MarshalJSON()
|
2016-05-06 04:40:23 -05:00
|
|
|
if err != nil {
|
2020-01-27 04:50:48 -06:00
|
|
|
return nil, err
|
2016-05-06 04:40:23 -05:00
|
|
|
}
|
2020-01-27 04:50:48 -06:00
|
|
|
|
2016-05-06 04:40:23 -05:00
|
|
|
var (
|
2020-01-27 04:50:48 -06:00
|
|
|
rawReq = string(reqVal)
|
2017-05-02 06:42:55 -05:00
|
|
|
dec = json.NewDecoder(strings.NewReader(rawReq))
|
2016-07-12 10:47:15 -05:00
|
|
|
reqs []jsonrpcCall
|
|
|
|
batch bool
|
2016-05-06 04:40:23 -05:00
|
|
|
)
|
2017-05-02 06:42:55 -05:00
|
|
|
dec.UseNumber() // avoid float64s
|
2016-07-12 10:47:15 -05:00
|
|
|
if rawReq[0] == '[' {
|
|
|
|
batch = true
|
2017-05-02 06:42:55 -05:00
|
|
|
dec.Decode(&reqs)
|
2016-07-12 10:47:15 -05:00
|
|
|
} else {
|
2016-05-06 04:40:23 -05:00
|
|
|
batch = false
|
2016-07-12 10:47:15 -05:00
|
|
|
reqs = make([]jsonrpcCall, 1)
|
2017-05-02 06:42:55 -05:00
|
|
|
dec.Decode(&reqs[0])
|
2016-05-06 04:40:23 -05:00
|
|
|
}
|
|
|
|
|
2016-07-12 10:47:15 -05:00
|
|
|
// Execute the requests.
|
2020-01-27 04:50:48 -06:00
|
|
|
var resps []*goja.Object
|
2016-07-12 10:47:15 -05:00
|
|
|
for _, req := range reqs {
|
2020-01-27 04:50:48 -06:00
|
|
|
resp := call.VM.NewObject()
|
|
|
|
resp.Set("jsonrpc", "2.0")
|
2018-05-31 06:59:08 -05:00
|
|
|
resp.Set("id", req.ID)
|
2020-01-27 04:50:48 -06:00
|
|
|
|
2016-07-12 10:47:15 -05:00
|
|
|
var result json.RawMessage
|
2020-06-08 03:09:49 -05:00
|
|
|
if err = b.client.Call(&result, req.Method, req.Params...); err == nil {
|
2016-07-12 10:47:15 -05:00
|
|
|
if result == nil {
|
|
|
|
// Special case null because it is decoded as an empty
|
|
|
|
// raw message for some reason.
|
2020-01-27 04:50:48 -06:00
|
|
|
resp.Set("result", goja.Null())
|
2016-07-12 10:47:15 -05:00
|
|
|
} else {
|
2020-01-27 04:50:48 -06:00
|
|
|
JSON := call.VM.Get("JSON").ToObject(call.VM)
|
|
|
|
parse, callable := goja.AssertFunction(JSON.Get("parse"))
|
|
|
|
if !callable {
|
2023-05-25 07:24:09 -05:00
|
|
|
return nil, errors.New("JSON.parse is not a function")
|
2020-01-27 04:50:48 -06:00
|
|
|
}
|
|
|
|
resultVal, err := parse(goja.Null(), call.VM.ToValue(string(result)))
|
2016-07-12 10:47:15 -05:00
|
|
|
if err != nil {
|
2020-06-08 03:09:49 -05:00
|
|
|
setError(resp, -32603, err.Error(), nil)
|
2016-07-12 10:47:15 -05:00
|
|
|
} else {
|
|
|
|
resp.Set("result", resultVal)
|
|
|
|
}
|
|
|
|
}
|
2020-06-08 03:09:49 -05:00
|
|
|
} else {
|
|
|
|
code := -32603
|
|
|
|
var data interface{}
|
|
|
|
if err, ok := err.(rpc.Error); ok {
|
|
|
|
code = err.ErrorCode()
|
|
|
|
}
|
|
|
|
if err, ok := err.(rpc.DataError); ok {
|
|
|
|
data = err.ErrorData()
|
|
|
|
}
|
|
|
|
setError(resp, code, err.Error(), data)
|
2016-05-06 04:40:23 -05:00
|
|
|
}
|
2020-01-27 04:50:48 -06:00
|
|
|
resps = append(resps, resp)
|
2016-07-12 10:47:15 -05:00
|
|
|
}
|
|
|
|
// Return the responses either to the callback (if supplied)
|
|
|
|
// or directly as the return value.
|
2020-01-27 04:50:48 -06:00
|
|
|
var result goja.Value
|
2016-07-12 10:47:15 -05:00
|
|
|
if batch {
|
2020-01-27 04:50:48 -06:00
|
|
|
result = call.VM.ToValue(resps)
|
2016-07-12 10:47:15 -05:00
|
|
|
} else {
|
2020-01-27 04:50:48 -06:00
|
|
|
result = resps[0]
|
2016-05-06 04:40:23 -05:00
|
|
|
}
|
2020-01-27 04:50:48 -06:00
|
|
|
if fn, isFunc := goja.AssertFunction(call.Argument(1)); isFunc {
|
|
|
|
fn(goja.Null(), goja.Null(), result)
|
|
|
|
return goja.Undefined(), nil
|
2016-05-06 04:40:23 -05:00
|
|
|
}
|
2020-01-27 04:50:48 -06:00
|
|
|
return result, nil
|
2016-07-12 10:47:15 -05:00
|
|
|
}
|
|
|
|
|
2020-06-08 03:09:49 -05:00
|
|
|
func setError(resp *goja.Object, code int, msg string, data interface{}) {
|
|
|
|
err := make(map[string]interface{})
|
|
|
|
err["code"] = code
|
|
|
|
err["message"] = msg
|
|
|
|
if data != nil {
|
|
|
|
err["data"] = data
|
|
|
|
}
|
|
|
|
resp.Set("error", err)
|
2016-05-06 04:40:23 -05:00
|
|
|
}
|
|
|
|
|
2020-01-27 04:50:48 -06:00
|
|
|
// isNumber returns true if input value is a JS number.
|
|
|
|
func isNumber(v goja.Value) bool {
|
|
|
|
k := v.ExportType().Kind()
|
|
|
|
return k >= reflect.Int && k <= reflect.Float64
|
|
|
|
}
|
|
|
|
|
|
|
|
func getObject(vm *goja.Runtime, name string) *goja.Object {
|
|
|
|
v := vm.Get(name)
|
|
|
|
if v == nil {
|
|
|
|
return nil
|
2016-05-06 04:40:23 -05:00
|
|
|
}
|
2020-01-27 04:50:48 -06:00
|
|
|
return v.ToObject(vm)
|
2016-05-06 04:40:23 -05:00
|
|
|
}
|