2015-03-15 06:43:48 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-04-20 10:29:02 +00:00
|
|
|
"errors"
|
2015-03-15 06:43:48 +00:00
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/cmd/utils"
|
2015-03-16 15:56:05 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2015-03-27 08:36:18 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/state"
|
2015-03-18 12:00:01 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2015-04-20 14:03:19 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/vm"
|
2015-04-04 11:24:19 +00:00
|
|
|
"github.com/ethereum/go-ethereum/logger/glog"
|
2015-03-15 06:43:48 +00:00
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
|
|
|
"github.com/ethereum/go-ethereum/xeth"
|
2015-03-20 12:22:01 +00:00
|
|
|
"github.com/robertkrimen/otto"
|
2015-03-15 06:43:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
/*
|
|
|
|
node admin bindings
|
|
|
|
*/
|
|
|
|
|
|
|
|
func (js *jsre) adminBindings() {
|
|
|
|
js.re.Set("admin", struct{}{})
|
|
|
|
t, _ := js.re.Get("admin")
|
|
|
|
admin := t.Object()
|
2015-04-29 15:04:08 +00:00
|
|
|
admin.Set("trustPeer", js.trustPeer)
|
2015-03-15 06:43:48 +00:00
|
|
|
admin.Set("startRPC", js.startRPC)
|
2015-04-16 10:56:51 +00:00
|
|
|
admin.Set("stopRPC", js.stopRPC)
|
2015-03-15 06:43:48 +00:00
|
|
|
admin.Set("nodeInfo", js.nodeInfo)
|
|
|
|
admin.Set("peers", js.peers)
|
|
|
|
admin.Set("newAccount", js.newAccount)
|
|
|
|
admin.Set("unlock", js.unlock)
|
|
|
|
admin.Set("import", js.importChain)
|
|
|
|
admin.Set("export", js.exportChain)
|
2015-04-04 11:24:19 +00:00
|
|
|
admin.Set("verbosity", js.verbosity)
|
2015-04-19 19:45:58 +00:00
|
|
|
admin.Set("progress", js.downloadProgress)
|
2015-04-05 16:57:03 +00:00
|
|
|
|
|
|
|
admin.Set("miner", struct{}{})
|
|
|
|
t, _ = admin.Get("miner")
|
|
|
|
miner := t.Object()
|
|
|
|
miner.Set("start", js.startMining)
|
|
|
|
miner.Set("stop", js.stopMining)
|
|
|
|
miner.Set("hashrate", js.hashrate)
|
|
|
|
miner.Set("setExtra", js.setExtra)
|
2015-04-14 10:49:30 +00:00
|
|
|
|
|
|
|
admin.Set("debug", struct{}{})
|
|
|
|
t, _ = admin.Get("debug")
|
|
|
|
debug := t.Object()
|
2015-04-21 09:47:40 +00:00
|
|
|
debug.Set("backtrace", js.backtrace)
|
2015-04-14 10:49:30 +00:00
|
|
|
debug.Set("printBlock", js.printBlock)
|
|
|
|
debug.Set("dumpBlock", js.dumpBlock)
|
2015-04-19 11:30:55 +00:00
|
|
|
debug.Set("getBlockRlp", js.getBlockRlp)
|
2015-04-20 10:29:02 +00:00
|
|
|
debug.Set("setHead", js.setHead)
|
2015-04-21 09:47:40 +00:00
|
|
|
debug.Set("processBlock", js.debugBlock)
|
2015-04-19 11:30:55 +00:00
|
|
|
}
|
|
|
|
|
2015-04-20 10:29:02 +00:00
|
|
|
func (js *jsre) getBlock(call otto.FunctionCall) (*types.Block, error) {
|
2015-04-19 11:30:55 +00:00
|
|
|
var block *types.Block
|
|
|
|
if len(call.ArgumentList) > 0 {
|
|
|
|
if call.Argument(0).IsNumber() {
|
|
|
|
num, _ := call.Argument(0).ToInteger()
|
|
|
|
block = js.ethereum.ChainManager().GetBlockByNumber(uint64(num))
|
|
|
|
} else if call.Argument(0).IsString() {
|
|
|
|
hash, _ := call.Argument(0).ToString()
|
|
|
|
block = js.ethereum.ChainManager().GetBlock(common.HexToHash(hash))
|
|
|
|
} else {
|
2015-04-20 10:29:02 +00:00
|
|
|
return nil, errors.New("invalid argument for dump. Either hex string or number")
|
2015-04-19 11:30:55 +00:00
|
|
|
}
|
2015-04-20 10:29:02 +00:00
|
|
|
return block, nil
|
|
|
|
}
|
2015-04-19 11:30:55 +00:00
|
|
|
|
2015-04-20 10:29:02 +00:00
|
|
|
return nil, errors.New("requires block number or block hash as argument")
|
|
|
|
}
|
|
|
|
|
2015-04-20 14:03:19 +00:00
|
|
|
func (js *jsre) debugBlock(call otto.FunctionCall) otto.Value {
|
|
|
|
block, err := js.getBlock(call)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return otto.UndefinedValue()
|
|
|
|
}
|
|
|
|
|
|
|
|
if block == nil {
|
|
|
|
fmt.Println("block not found")
|
|
|
|
return otto.UndefinedValue()
|
|
|
|
}
|
|
|
|
|
|
|
|
old := vm.Debug
|
|
|
|
vm.Debug = true
|
|
|
|
_, err = js.ethereum.BlockProcessor().RetryProcess(block)
|
|
|
|
if err != nil {
|
|
|
|
glog.Infoln(err)
|
|
|
|
}
|
|
|
|
vm.Debug = old
|
|
|
|
|
|
|
|
return otto.UndefinedValue()
|
|
|
|
}
|
|
|
|
|
2015-04-20 10:29:02 +00:00
|
|
|
func (js *jsre) setHead(call otto.FunctionCall) otto.Value {
|
|
|
|
block, err := js.getBlock(call)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return otto.UndefinedValue()
|
2015-04-19 11:30:55 +00:00
|
|
|
}
|
2015-04-20 10:29:02 +00:00
|
|
|
|
|
|
|
if block == nil {
|
|
|
|
fmt.Println("block not found")
|
|
|
|
return otto.UndefinedValue()
|
|
|
|
}
|
|
|
|
|
|
|
|
js.ethereum.ChainManager().SetHead(block)
|
|
|
|
return otto.UndefinedValue()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (js *jsre) downloadProgress(call otto.FunctionCall) otto.Value {
|
|
|
|
current, max := js.ethereum.Downloader().Stats()
|
|
|
|
|
|
|
|
return js.re.ToVal(fmt.Sprintf("%d/%d", current, max))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (js *jsre) getBlockRlp(call otto.FunctionCall) otto.Value {
|
|
|
|
block, err := js.getBlock(call)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return otto.UndefinedValue()
|
|
|
|
}
|
|
|
|
|
2015-04-19 11:30:55 +00:00
|
|
|
if block == nil {
|
|
|
|
fmt.Println("block not found")
|
|
|
|
return otto.UndefinedValue()
|
|
|
|
}
|
|
|
|
|
|
|
|
encoded, _ := rlp.EncodeToBytes(block)
|
|
|
|
return js.re.ToVal(fmt.Sprintf("%x", encoded))
|
2015-04-05 16:57:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (js *jsre) setExtra(call otto.FunctionCall) otto.Value {
|
|
|
|
extra, err := call.Argument(0).ToString()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return otto.UndefinedValue()
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(extra) > 1024 {
|
|
|
|
fmt.Println("error: cannot exceed 1024 bytes")
|
|
|
|
return otto.UndefinedValue()
|
|
|
|
}
|
|
|
|
|
|
|
|
js.ethereum.Miner().SetExtra([]byte(extra))
|
|
|
|
return otto.UndefinedValue()
|
2015-04-05 11:05:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (js *jsre) hashrate(otto.FunctionCall) otto.Value {
|
|
|
|
return js.re.ToVal(js.ethereum.Miner().HashRate())
|
2015-04-04 20:16:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (js *jsre) backtrace(call otto.FunctionCall) otto.Value {
|
|
|
|
tracestr, err := call.Argument(0).ToString()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return otto.UndefinedValue()
|
|
|
|
}
|
|
|
|
glog.GetTraceLocation().Set(tracestr)
|
|
|
|
|
|
|
|
return otto.UndefinedValue()
|
2015-04-04 11:24:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (js *jsre) verbosity(call otto.FunctionCall) otto.Value {
|
|
|
|
v, err := call.Argument(0).ToInteger()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return otto.UndefinedValue()
|
|
|
|
}
|
|
|
|
|
|
|
|
glog.SetV(int(v))
|
|
|
|
return otto.UndefinedValue()
|
2015-03-15 06:43:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (js *jsre) startMining(call otto.FunctionCall) otto.Value {
|
|
|
|
_, err := call.Argument(0).ToInteger()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return otto.FalseValue()
|
|
|
|
}
|
|
|
|
// threads now ignored
|
|
|
|
err = js.ethereum.StartMining()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return otto.FalseValue()
|
|
|
|
}
|
|
|
|
return otto.TrueValue()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (js *jsre) stopMining(call otto.FunctionCall) otto.Value {
|
|
|
|
js.ethereum.StopMining()
|
|
|
|
return otto.TrueValue()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (js *jsre) startRPC(call otto.FunctionCall) otto.Value {
|
|
|
|
addr, err := call.Argument(0).ToString()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return otto.FalseValue()
|
|
|
|
}
|
2015-04-22 13:55:01 +00:00
|
|
|
|
2015-03-15 06:43:48 +00:00
|
|
|
port, err := call.Argument(1).ToInteger()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return otto.FalseValue()
|
|
|
|
}
|
|
|
|
|
2015-04-22 13:55:01 +00:00
|
|
|
corsDomain := js.corsDomain
|
2015-04-21 17:08:47 +00:00
|
|
|
if len(call.ArgumentList) > 2 {
|
|
|
|
corsDomain, err = call.Argument(2).ToString()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return otto.FalseValue()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-29 19:26:47 +00:00
|
|
|
config := rpc.RpcConfig{
|
|
|
|
ListenAddress: addr,
|
|
|
|
ListenPort: uint(port),
|
2015-04-21 17:08:47 +00:00
|
|
|
CorsDomain: corsDomain,
|
2015-03-29 19:26:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
xeth := xeth.New(js.ethereum, nil)
|
|
|
|
err = rpc.Start(xeth, config)
|
|
|
|
|
2015-03-15 06:43:48 +00:00
|
|
|
if err != nil {
|
2015-03-29 19:26:47 +00:00
|
|
|
fmt.Printf(err.Error())
|
2015-03-15 06:43:48 +00:00
|
|
|
return otto.FalseValue()
|
|
|
|
}
|
2015-03-29 19:26:47 +00:00
|
|
|
|
2015-03-15 06:43:48 +00:00
|
|
|
return otto.TrueValue()
|
2015-04-16 10:56:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (js *jsre) stopRPC(call otto.FunctionCall) otto.Value {
|
|
|
|
if rpc.Stop() == nil {
|
|
|
|
return otto.TrueValue()
|
|
|
|
}
|
|
|
|
return otto.FalseValue()
|
2015-03-15 06:43:48 +00:00
|
|
|
}
|
|
|
|
|
2015-04-29 15:04:08 +00:00
|
|
|
func (js *jsre) trustPeer(call otto.FunctionCall) otto.Value {
|
2015-03-15 06:43:48 +00:00
|
|
|
nodeURL, err := call.Argument(0).ToString()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return otto.FalseValue()
|
|
|
|
}
|
2015-04-29 15:04:08 +00:00
|
|
|
err = js.ethereum.TrustPeer(nodeURL)
|
2015-03-15 06:43:48 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return otto.FalseValue()
|
|
|
|
}
|
|
|
|
return otto.TrueValue()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (js *jsre) unlock(call otto.FunctionCall) otto.Value {
|
|
|
|
addr, err := call.Argument(0).ToString()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return otto.FalseValue()
|
|
|
|
}
|
|
|
|
seconds, err := call.Argument(2).ToInteger()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return otto.FalseValue()
|
|
|
|
}
|
|
|
|
arg := call.Argument(1)
|
|
|
|
var passphrase string
|
|
|
|
if arg.IsUndefined() {
|
|
|
|
fmt.Println("Please enter a passphrase now.")
|
|
|
|
passphrase, err = readPassword("Passphrase: ", true)
|
|
|
|
if err != nil {
|
|
|
|
utils.Fatalf("%v", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
passphrase, err = arg.ToString()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return otto.FalseValue()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
am := js.ethereum.AccountManager()
|
2015-03-16 15:56:05 +00:00
|
|
|
err = am.TimedUnlock(common.FromHex(addr), passphrase, time.Duration(seconds)*time.Second)
|
2015-03-15 06:43:48 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Unlock account failed '%v'\n", err)
|
|
|
|
return otto.FalseValue()
|
|
|
|
}
|
|
|
|
return otto.TrueValue()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (js *jsre) newAccount(call otto.FunctionCall) otto.Value {
|
|
|
|
arg := call.Argument(0)
|
|
|
|
var passphrase string
|
|
|
|
if arg.IsUndefined() {
|
|
|
|
fmt.Println("The new account will be encrypted with a passphrase.")
|
|
|
|
fmt.Println("Please enter a passphrase now.")
|
|
|
|
auth, err := readPassword("Passphrase: ", true)
|
|
|
|
if err != nil {
|
|
|
|
utils.Fatalf("%v", err)
|
|
|
|
}
|
|
|
|
confirm, err := readPassword("Repeat Passphrase: ", false)
|
|
|
|
if err != nil {
|
|
|
|
utils.Fatalf("%v", err)
|
|
|
|
}
|
|
|
|
if auth != confirm {
|
|
|
|
utils.Fatalf("Passphrases did not match.")
|
|
|
|
}
|
|
|
|
passphrase = auth
|
|
|
|
} else {
|
|
|
|
var err error
|
|
|
|
passphrase, err = arg.ToString()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return otto.FalseValue()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
acct, err := js.ethereum.AccountManager().NewAccount(passphrase)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Could not create the account: %v", err)
|
|
|
|
return otto.UndefinedValue()
|
|
|
|
}
|
2015-04-22 08:59:27 +00:00
|
|
|
return js.re.ToVal("0x" + common.Bytes2Hex(acct.Address))
|
2015-03-15 06:43:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (js *jsre) nodeInfo(call otto.FunctionCall) otto.Value {
|
|
|
|
return js.re.ToVal(js.ethereum.NodeInfo())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (js *jsre) peers(call otto.FunctionCall) otto.Value {
|
|
|
|
return js.re.ToVal(js.ethereum.PeersInfo())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (js *jsre) importChain(call otto.FunctionCall) otto.Value {
|
|
|
|
if len(call.ArgumentList) == 0 {
|
|
|
|
fmt.Println("err: require file name")
|
|
|
|
return otto.FalseValue()
|
|
|
|
}
|
|
|
|
fn, err := call.Argument(0).ToString()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return otto.FalseValue()
|
|
|
|
}
|
2015-04-22 08:59:27 +00:00
|
|
|
if err := utils.ImportChain(js.ethereum.ChainManager(), fn); err != nil {
|
|
|
|
fmt.Println("Import error: ", err)
|
2015-03-15 06:43:48 +00:00
|
|
|
return otto.FalseValue()
|
|
|
|
}
|
|
|
|
return otto.TrueValue()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (js *jsre) exportChain(call otto.FunctionCall) otto.Value {
|
|
|
|
if len(call.ArgumentList) == 0 {
|
|
|
|
fmt.Println("err: require file name")
|
|
|
|
return otto.FalseValue()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn, err := call.Argument(0).ToString()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return otto.FalseValue()
|
|
|
|
}
|
2015-03-18 13:04:19 +00:00
|
|
|
if err := utils.ExportChain(js.ethereum.ChainManager(), fn); err != nil {
|
2015-03-15 06:43:48 +00:00
|
|
|
fmt.Println(err)
|
|
|
|
return otto.FalseValue()
|
|
|
|
}
|
|
|
|
return otto.TrueValue()
|
|
|
|
}
|
|
|
|
|
2015-04-14 10:49:30 +00:00
|
|
|
func (js *jsre) printBlock(call otto.FunctionCall) otto.Value {
|
|
|
|
var block *types.Block
|
|
|
|
if len(call.ArgumentList) > 0 {
|
|
|
|
if call.Argument(0).IsNumber() {
|
|
|
|
num, _ := call.Argument(0).ToInteger()
|
|
|
|
block = js.ethereum.ChainManager().GetBlockByNumber(uint64(num))
|
|
|
|
} else if call.Argument(0).IsString() {
|
|
|
|
hash, _ := call.Argument(0).ToString()
|
|
|
|
block = js.ethereum.ChainManager().GetBlock(common.HexToHash(hash))
|
|
|
|
} else {
|
|
|
|
fmt.Println("invalid argument for dump. Either hex string or number")
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
block = js.ethereum.ChainManager().CurrentBlock()
|
|
|
|
}
|
|
|
|
if block == nil {
|
|
|
|
fmt.Println("block not found")
|
|
|
|
return otto.UndefinedValue()
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println(block)
|
|
|
|
|
|
|
|
return otto.UndefinedValue()
|
|
|
|
}
|
|
|
|
|
2015-03-15 06:43:48 +00:00
|
|
|
func (js *jsre) dumpBlock(call otto.FunctionCall) otto.Value {
|
|
|
|
var block *types.Block
|
|
|
|
if len(call.ArgumentList) > 0 {
|
|
|
|
if call.Argument(0).IsNumber() {
|
|
|
|
num, _ := call.Argument(0).ToInteger()
|
|
|
|
block = js.ethereum.ChainManager().GetBlockByNumber(uint64(num))
|
|
|
|
} else if call.Argument(0).IsString() {
|
|
|
|
hash, _ := call.Argument(0).ToString()
|
2015-03-18 12:00:01 +00:00
|
|
|
block = js.ethereum.ChainManager().GetBlock(common.HexToHash(hash))
|
2015-03-15 06:43:48 +00:00
|
|
|
} else {
|
|
|
|
fmt.Println("invalid argument for dump. Either hex string or number")
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
block = js.ethereum.ChainManager().CurrentBlock()
|
|
|
|
}
|
|
|
|
if block == nil {
|
|
|
|
fmt.Println("block not found")
|
|
|
|
return otto.UndefinedValue()
|
|
|
|
}
|
|
|
|
|
|
|
|
statedb := state.New(block.Root(), js.ethereum.StateDb())
|
|
|
|
dump := statedb.RawDump()
|
|
|
|
return js.re.ToVal(dump)
|
|
|
|
|
|
|
|
}
|