mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-22 04:36:42 +00:00
Merge ea6f2124d9 into 21fd722abc
This commit is contained in:
commit
7286b912f8
10 changed files with 591 additions and 314 deletions
|
|
@ -27,17 +27,12 @@ import (
|
|||
"strings"
|
||||
|
||||
"github.com/ethereum/go-ethereum/cmd/utils"
|
||||
"github.com/ethereum/go-ethereum/core/types"
|
||||
"github.com/ethereum/go-ethereum/eth"
|
||||
"github.com/ethereum/go-ethereum/ethutil"
|
||||
"github.com/ethereum/go-ethereum/javascript"
|
||||
"github.com/ethereum/go-ethereum/state"
|
||||
"github.com/ethereum/go-ethereum/xeth"
|
||||
"github.com/obscuren/otto"
|
||||
"github.com/ethereum/go-ethereum/jethre"
|
||||
"github.com/peterh/liner"
|
||||
)
|
||||
|
||||
func execJsFile(ethereum *eth.Ethereum, filename string) {
|
||||
func execJsFile(ethereum *eth.Ethereum, assetPath, filename string) {
|
||||
file, err := os.Open(filename)
|
||||
if err != nil {
|
||||
utils.Fatalf("%v", err)
|
||||
|
|
@ -46,29 +41,25 @@ func execJsFile(ethereum *eth.Ethereum, filename string) {
|
|||
if err != nil {
|
||||
utils.Fatalf("%v", err)
|
||||
}
|
||||
re := javascript.NewJSRE(xeth.New(ethereum))
|
||||
re := javascript.NewJEthRE(ethereum, assetPath)
|
||||
if _, err := re.Run(string(content)); err != nil {
|
||||
utils.Fatalf("Javascript Error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
type repl struct {
|
||||
re *javascript.JSRE
|
||||
ethereum *eth.Ethereum
|
||||
xeth *xeth.XEth
|
||||
prompt string
|
||||
lr *liner.State
|
||||
re *javascript.JEthRE
|
||||
prompt string
|
||||
lr *liner.State
|
||||
dataDir string
|
||||
}
|
||||
|
||||
func runREPL(ethereum *eth.Ethereum) {
|
||||
xeth := xeth.New(ethereum)
|
||||
func runREPL(ethereum *eth.Ethereum, assetPath string) {
|
||||
repl := &repl{
|
||||
re: javascript.NewJSRE(xeth),
|
||||
xeth: xeth,
|
||||
ethereum: ethereum,
|
||||
prompt: "> ",
|
||||
re: javascript.NewJEthRE(ethereum, assetPath),
|
||||
dataDir: ethereum.DataDir,
|
||||
prompt: "> ",
|
||||
}
|
||||
repl.initStdFuncs()
|
||||
if !liner.TerminalSupported() {
|
||||
repl.dumbRead()
|
||||
} else {
|
||||
|
|
@ -82,7 +73,7 @@ func runREPL(ethereum *eth.Ethereum) {
|
|||
}
|
||||
|
||||
func (self *repl) withHistory(op func(*os.File)) {
|
||||
hist, err := os.OpenFile(path.Join(self.ethereum.DataDir, "history"), os.O_RDWR|os.O_CREATE, os.ModePerm)
|
||||
hist, err := os.OpenFile(path.Join(self.dataDir, "history"), os.O_RDWR|os.O_CREATE, os.ModePerm)
|
||||
if err != nil {
|
||||
fmt.Printf("unable to open history file: %v\n", err)
|
||||
return
|
||||
|
|
@ -181,100 +172,10 @@ func (self *repl) dumbRead() {
|
|||
}
|
||||
|
||||
func (self *repl) printValue(v interface{}) {
|
||||
method, _ := self.re.Vm.Get("prettyPrint")
|
||||
v, err := self.re.Vm.ToValue(v)
|
||||
val, err := self.re.PrettyPrint(v)
|
||||
if err == nil {
|
||||
val, err := method.Call(method, v)
|
||||
if err == nil {
|
||||
fmt.Printf("%v", val)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (self *repl) initStdFuncs() {
|
||||
t, _ := self.re.Vm.Get("eth")
|
||||
eth := t.Object()
|
||||
eth.Set("connect", self.connect)
|
||||
eth.Set("stopMining", self.stopMining)
|
||||
eth.Set("startMining", self.startMining)
|
||||
eth.Set("dump", self.dump)
|
||||
eth.Set("export", self.export)
|
||||
}
|
||||
|
||||
/*
|
||||
* The following methods are natively implemented javascript functions.
|
||||
*/
|
||||
|
||||
func (self *repl) dump(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 = self.ethereum.ChainManager().GetBlockByNumber(uint64(num))
|
||||
} else if call.Argument(0).IsString() {
|
||||
hash, _ := call.Argument(0).ToString()
|
||||
block = self.ethereum.ChainManager().GetBlock(ethutil.Hex2Bytes(hash))
|
||||
} else {
|
||||
fmt.Println("invalid argument for dump. Either hex string or number")
|
||||
}
|
||||
|
||||
if block == nil {
|
||||
fmt.Println("block not found")
|
||||
|
||||
return otto.UndefinedValue()
|
||||
}
|
||||
|
||||
fmt.Printf("%v", val)
|
||||
} else {
|
||||
block = self.ethereum.ChainManager().CurrentBlock()
|
||||
fmt.Printf("print error: %v", err)
|
||||
}
|
||||
|
||||
statedb := state.New(block.Root(), self.ethereum.StateDb())
|
||||
|
||||
v, _ := self.re.Vm.ToValue(statedb.RawDump())
|
||||
|
||||
return v
|
||||
}
|
||||
|
||||
func (self *repl) stopMining(call otto.FunctionCall) otto.Value {
|
||||
self.xeth.Miner().Stop()
|
||||
return otto.TrueValue()
|
||||
}
|
||||
|
||||
func (self *repl) startMining(call otto.FunctionCall) otto.Value {
|
||||
self.xeth.Miner().Start()
|
||||
return otto.TrueValue()
|
||||
}
|
||||
|
||||
func (self *repl) connect(call otto.FunctionCall) otto.Value {
|
||||
nodeURL, err := call.Argument(0).ToString()
|
||||
if err != nil {
|
||||
return otto.FalseValue()
|
||||
}
|
||||
if err := self.ethereum.SuggestPeer(nodeURL); err != nil {
|
||||
return otto.FalseValue()
|
||||
}
|
||||
return otto.TrueValue()
|
||||
}
|
||||
|
||||
func (self *repl) export(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()
|
||||
}
|
||||
|
||||
data := self.ethereum.ChainManager().Export()
|
||||
|
||||
if err := ethutil.WriteFile(fn, data); err != nil {
|
||||
fmt.Println(err)
|
||||
return otto.FalseValue()
|
||||
}
|
||||
|
||||
return otto.TrueValue()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -23,6 +23,7 @@ package main
|
|||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"time"
|
||||
|
|
@ -143,13 +144,14 @@ func run(ctx *cli.Context) {
|
|||
|
||||
func runjs(ctx *cli.Context) {
|
||||
eth := utils.GetEthereum(ClientIdentifier, Version, ctx)
|
||||
assetPath := path.Join(os.Getenv("GOPATH"), "src", "github.com", "ethereum", "go-ethereum", "cmd", "mist", "assets", "ext")
|
||||
startEth(ctx, eth)
|
||||
if len(ctx.Args()) == 0 {
|
||||
runREPL(eth)
|
||||
runREPL(eth, assetPath)
|
||||
eth.Stop()
|
||||
eth.WaitForShutdown()
|
||||
} else if len(ctx.Args()) == 1 {
|
||||
execJsFile(eth, ctx.Args()[0])
|
||||
execJsFile(eth, assetPath, ctx.Args()[0])
|
||||
} else {
|
||||
utils.Fatalf("This command can handle at most one argument.")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,102 +0,0 @@
|
|||
package javascript
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"github.com/ethereum/go-ethereum/logger"
|
||||
"github.com/ethereum/go-ethereum/xeth"
|
||||
"github.com/obscuren/otto"
|
||||
)
|
||||
|
||||
var jsrelogger = logger.NewLogger("JSRE")
|
||||
|
||||
type JSRE struct {
|
||||
Vm *otto.Otto
|
||||
xeth *xeth.XEth
|
||||
|
||||
objectCb map[string][]otto.Value
|
||||
}
|
||||
|
||||
func (jsre *JSRE) LoadExtFile(path string) {
|
||||
result, err := ioutil.ReadFile(path)
|
||||
if err == nil {
|
||||
jsre.Vm.Run(result)
|
||||
} else {
|
||||
jsrelogger.Infoln("Could not load file:", path)
|
||||
}
|
||||
}
|
||||
|
||||
func (jsre *JSRE) LoadIntFile(file string) {
|
||||
assetPath := path.Join(os.Getenv("GOPATH"), "src", "github.com", "ethereum", "go-ethereum", "cmd", "mist", "assets", "ext")
|
||||
jsre.LoadExtFile(path.Join(assetPath, file))
|
||||
}
|
||||
|
||||
func NewJSRE(xeth *xeth.XEth) *JSRE {
|
||||
re := &JSRE{
|
||||
otto.New(),
|
||||
xeth,
|
||||
make(map[string][]otto.Value),
|
||||
}
|
||||
|
||||
// Init the JS lib
|
||||
re.Vm.Run(jsLib)
|
||||
|
||||
// Load extra javascript files
|
||||
re.LoadIntFile("bignumber.min.js")
|
||||
|
||||
re.Bind("eth", &JSEthereum{re.xeth, re.Vm})
|
||||
|
||||
re.initStdFuncs()
|
||||
|
||||
jsrelogger.Infoln("started")
|
||||
|
||||
return re
|
||||
}
|
||||
|
||||
func (self *JSRE) Bind(name string, v interface{}) {
|
||||
self.Vm.Set(name, v)
|
||||
}
|
||||
|
||||
func (self *JSRE) Run(code string) (otto.Value, error) {
|
||||
return self.Vm.Run(code)
|
||||
}
|
||||
|
||||
func (self *JSRE) initStdFuncs() {
|
||||
t, _ := self.Vm.Get("eth")
|
||||
eth := t.Object()
|
||||
eth.Set("require", self.require)
|
||||
}
|
||||
|
||||
func (self *JSRE) Require(file string) error {
|
||||
if len(filepath.Ext(file)) == 0 {
|
||||
file += ".js"
|
||||
}
|
||||
|
||||
fh, err := os.Open(file)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
content, _ := ioutil.ReadAll(fh)
|
||||
self.Run("exports = {};(function() {" + string(content) + "})();")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *JSRE) require(call otto.FunctionCall) otto.Value {
|
||||
file, err := call.Argument(0).ToString()
|
||||
if err != nil {
|
||||
return otto.UndefinedValue()
|
||||
}
|
||||
if err := self.Require(file); err != nil {
|
||||
fmt.Println("err:", err)
|
||||
return otto.UndefinedValue()
|
||||
}
|
||||
|
||||
t, _ := self.Vm.Get("exports")
|
||||
|
||||
return t
|
||||
}
|
||||
|
|
@ -1,94 +0,0 @@
|
|||
package javascript
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/ethereum/go-ethereum/ethutil"
|
||||
"github.com/ethereum/go-ethereum/state"
|
||||
"github.com/ethereum/go-ethereum/xeth"
|
||||
"github.com/obscuren/otto"
|
||||
)
|
||||
|
||||
type JSStateObject struct {
|
||||
*xeth.Object
|
||||
eth *JSEthereum
|
||||
}
|
||||
|
||||
func (self *JSStateObject) EachStorage(call otto.FunctionCall) otto.Value {
|
||||
cb := call.Argument(0)
|
||||
|
||||
it := self.Object.Trie().Iterator()
|
||||
for it.Next() {
|
||||
cb.Call(self.eth.toVal(self), self.eth.toVal(ethutil.Bytes2Hex(it.Key)), self.eth.toVal(ethutil.Bytes2Hex(it.Value)))
|
||||
}
|
||||
|
||||
return otto.UndefinedValue()
|
||||
}
|
||||
|
||||
// The JSEthereum object attempts to wrap the PEthereum object and returns
|
||||
// meaningful javascript objects
|
||||
type JSBlock struct {
|
||||
*xeth.Block
|
||||
eth *JSEthereum
|
||||
}
|
||||
|
||||
func (self *JSBlock) GetTransaction(hash string) otto.Value {
|
||||
return self.eth.toVal(self.Block.GetTransaction(hash))
|
||||
}
|
||||
|
||||
type JSLog struct {
|
||||
Address string `json:address`
|
||||
Topics []string `json:topics`
|
||||
Number int32 `json:number`
|
||||
Data string `json:data`
|
||||
}
|
||||
|
||||
func NewJSLog(log state.Log) JSLog {
|
||||
return JSLog{
|
||||
Address: ethutil.Bytes2Hex(log.Address()),
|
||||
Topics: nil, //ethutil.Bytes2Hex(log.Address()),
|
||||
Number: 0,
|
||||
Data: ethutil.Bytes2Hex(log.Data()),
|
||||
}
|
||||
}
|
||||
|
||||
type JSEthereum struct {
|
||||
*xeth.XEth
|
||||
vm *otto.Otto
|
||||
}
|
||||
|
||||
func (self *JSEthereum) Block(v interface{}) otto.Value {
|
||||
if number, ok := v.(int64); ok {
|
||||
return self.toVal(&JSBlock{self.XEth.BlockByNumber(int32(number)), self})
|
||||
} else if hash, ok := v.(string); ok {
|
||||
return self.toVal(&JSBlock{self.XEth.BlockByHash(hash), self})
|
||||
}
|
||||
|
||||
return otto.UndefinedValue()
|
||||
}
|
||||
|
||||
func (self *JSEthereum) GetStateObject(addr string) otto.Value {
|
||||
return self.toVal(&JSStateObject{self.XEth.State().SafeGet(addr), self})
|
||||
}
|
||||
|
||||
func (self *JSEthereum) Transact(key, recipient, valueStr, gasStr, gasPriceStr, dataStr string) otto.Value {
|
||||
r, err := self.XEth.Transact(recipient, valueStr, gasStr, gasPriceStr, dataStr)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
|
||||
return otto.UndefinedValue()
|
||||
}
|
||||
|
||||
return self.toVal(r)
|
||||
}
|
||||
|
||||
func (self *JSEthereum) toVal(v interface{}) otto.Value {
|
||||
result, err := self.vm.ToValue(v)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println("Value unknown:", err)
|
||||
|
||||
return otto.UndefinedValue()
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
214
jethre/jeth.go
Normal file
214
jethre/jeth.go
Normal file
|
|
@ -0,0 +1,214 @@
|
|||
package javascript
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/ethereum/go-ethereum/xeth"
|
||||
"github.com/obscuren/otto"
|
||||
)
|
||||
|
||||
/*
|
||||
JEth provides the the actual bindings for xeth (extended ethereum)
|
||||
*/
|
||||
type jeth struct {
|
||||
xeth *xeth.XEth
|
||||
toVal func(v interface{}) otto.Value
|
||||
}
|
||||
|
||||
func (self *jeth) IsMining(call otto.FunctionCall) otto.Value {
|
||||
return self.toVal(self.xeth.IsMining())
|
||||
}
|
||||
|
||||
func (self *jeth) IsListening(call otto.FunctionCall) otto.Value {
|
||||
return self.toVal(self.xeth.IsListening())
|
||||
}
|
||||
|
||||
func (self *jeth) IsContract(call otto.FunctionCall) otto.Value {
|
||||
address, err := call.Argument(0).ToString()
|
||||
if err != nil {
|
||||
return otto.UndefinedValue()
|
||||
}
|
||||
return self.toVal(self.xeth.IsContract(address))
|
||||
}
|
||||
|
||||
func (self *jeth) GetCoinbase(call otto.FunctionCall) otto.Value {
|
||||
return self.toVal(self.xeth.Coinbase())
|
||||
}
|
||||
|
||||
func (self *jeth) SetMining(call otto.FunctionCall) otto.Value {
|
||||
shouldmine, err := call.Argument(0).ToBoolean()
|
||||
if err != nil {
|
||||
return otto.UndefinedValue()
|
||||
}
|
||||
mining := self.xeth.SetMining(shouldmine)
|
||||
return self.toVal(mining)
|
||||
}
|
||||
|
||||
func (self *jeth) SuggestPeer(call otto.FunctionCall) otto.Value {
|
||||
nodeURL, err := call.Argument(0).ToString()
|
||||
if err != nil {
|
||||
return otto.FalseValue()
|
||||
}
|
||||
if err := self.xeth.SuggestPeer(nodeURL); err != nil {
|
||||
return otto.FalseValue()
|
||||
}
|
||||
return otto.TrueValue()
|
||||
}
|
||||
|
||||
func (self *jeth) Import(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()
|
||||
}
|
||||
|
||||
if err := self.xeth.Import(fn); err != nil {
|
||||
return otto.FalseValue()
|
||||
}
|
||||
|
||||
return otto.TrueValue()
|
||||
}
|
||||
|
||||
func (self *jeth) Export(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()
|
||||
}
|
||||
|
||||
if err := self.xeth.Export(fn); err != nil {
|
||||
return otto.FalseValue()
|
||||
}
|
||||
|
||||
return otto.TrueValue()
|
||||
}
|
||||
|
||||
// func (self *jethRE) DumpBlock(call otto.FunctionCall) otto.Value {
|
||||
// var dump state.World
|
||||
// var err error
|
||||
|
||||
// if len(call.ArgumentList) > 0 {
|
||||
// if call.Argument(0).IsNumber() {
|
||||
// num, _ := call.Argument(0).ToInteger()
|
||||
// dump, err = self.xeth.DumpBlockByNumber(int32(num))
|
||||
// } else if call.Argument(0).IsString() {
|
||||
// hash, _ := call.Argument(0).ToString()
|
||||
// dump, err = self.xeth.DumpBlockByHash(hash)
|
||||
// } else {
|
||||
// fmt.Println("invalid argument for dump. Either hex string or number")
|
||||
// return otto.UndefinedValue()
|
||||
// }
|
||||
// } else {
|
||||
// dump, err = self.xeth.DumpBlockByNumber(-1)
|
||||
// }
|
||||
// if err != nil {
|
||||
// fmt.Println(err)
|
||||
// return otto.UndefinedValue()
|
||||
// }
|
||||
|
||||
// return self.toVal(dump)
|
||||
|
||||
// }
|
||||
|
||||
// import (
|
||||
// "fmt"
|
||||
// "github.com/ethereum/go-ethereum/ethutil"
|
||||
// "github.com/ethereum/go-ethereum/state"
|
||||
// "github.com/ethereum/go-ethereum/xeth"
|
||||
// "github.com/obscuren/otto"
|
||||
// )
|
||||
|
||||
// type JSStateObject struct {
|
||||
// *xeth.Object
|
||||
// eth *JSEthereum
|
||||
// }
|
||||
|
||||
// func (self *JSStateObject) EachStorage(call otto.FunctionCall) otto.Value {
|
||||
// cb := call.Argument(0)
|
||||
|
||||
// it := self.Object.Trie().Iterator()
|
||||
// for it.Next() {
|
||||
// cb.Call(self.eth.toVal(self), self.eth.toVal(ethutil.Bytes2Hex(it.Key)), self.eth.toVal(ethutil.Bytes2Hex(it.Value)))
|
||||
// }
|
||||
|
||||
// return otto.UndefinedValue()
|
||||
// }
|
||||
|
||||
// // The JSEthereum object attempts to wrap the PEthereum object and returns
|
||||
// // meaningful javascript objects
|
||||
// type JSBlock struct {
|
||||
// *xeth.Block
|
||||
// eth *JSEthereum
|
||||
// }
|
||||
|
||||
// func (self *JSBlock) GetTransaction(hash string) otto.Value {
|
||||
// return self.eth.toVal(self.Block.GetTransaction(hash))
|
||||
// }
|
||||
|
||||
// type JSLog struct {
|
||||
// Address string `json:address`
|
||||
// Topics []string `json:topics`
|
||||
// Number int32 `json:number`
|
||||
// Data string `json:data`
|
||||
// }
|
||||
|
||||
// func NewJSLog(log state.Log) JSLog {
|
||||
// return JSLog{
|
||||
// Address: ethutil.Bytes2Hex(log.Address()),
|
||||
// Topics: nil, //ethutil.Bytes2Hex(log.Address()),
|
||||
// Number: 0,
|
||||
// Data: ethutil.Bytes2Hex(log.Data()),
|
||||
// }
|
||||
// }
|
||||
|
||||
// type JSEthereum struct {
|
||||
// *xeth.XEth
|
||||
// vm *otto.Otto
|
||||
// }
|
||||
|
||||
// func (self *JSEthereum) Block(v interface{}) otto.Value {
|
||||
// if number, ok := v.(int64); ok {
|
||||
// return self.toVal(&JSBlock{self.XEth.BlockByNumber(int32(number)), self})
|
||||
// } else if hash, ok := v.(string); ok {
|
||||
// return self.toVal(&JSBlock{self.XEth.BlockByHash(hash), self})
|
||||
// }
|
||||
|
||||
// return otto.UndefinedValue()
|
||||
// }
|
||||
|
||||
// func (self *JSEthereum) GetStateObject(addr string) otto.Value {
|
||||
// return self.toVal(&JSStateObject{self.XEth.State().SafeGet(addr), self})
|
||||
// }
|
||||
|
||||
// func (self *JSEthereum) Transact(key, recipient, valueStr, gasStr, gasPriceStr, dataStr string) otto.Value {
|
||||
// r, err := self.XEth.Transact(recipient, valueStr, gasStr, gasPriceStr, dataStr)
|
||||
// if err != nil {
|
||||
// fmt.Println(err)
|
||||
|
||||
// return otto.UndefinedValue()
|
||||
// }
|
||||
|
||||
// return self.toVal(r)
|
||||
// }
|
||||
|
||||
// func (self *JSEthereum) toVal(v interface{}) otto.Value {
|
||||
// result, err := self.vm.ToValue(v)
|
||||
|
||||
// if err != nil {
|
||||
// fmt.Println("Value unknown:", err)
|
||||
|
||||
// return otto.UndefinedValue()
|
||||
// }
|
||||
|
||||
// return result
|
||||
// }
|
||||
40
jethre/jethre.go
Normal file
40
jethre/jethre.go
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
package javascript
|
||||
|
||||
import (
|
||||
"github.com/ethereum/go-ethereum/eth"
|
||||
"github.com/ethereum/go-ethereum/logger"
|
||||
"github.com/ethereum/go-ethereum/xeth"
|
||||
)
|
||||
|
||||
var jsrelogger = logger.NewLogger("JEthRE")
|
||||
|
||||
/*
|
||||
JEthRE (jay - eth runtime environment) is a javascript environment that embeds ethereum.
|
||||
It provides javascript bindings to the entire extended ethereum interface https://github.com/ethereum/go-ethereum/wiki/XEth
|
||||
|
||||
*Note* JEthRe is not to be confused with ethereum.js
|
||||
JEthRE is more like an admin console for your ethereum client, not a helper lib for Dapps
|
||||
|
||||
The JEth javascript API is found on the wiki.
|
||||
All exported functions in jeth map to a lowercase variant in the eth namespace within JEthRE
|
||||
|
||||
Note that the CLI allows you to execute a js script within JEthRE offering scriptable ethereum
|
||||
|
||||
*/
|
||||
|
||||
type JEthRE struct {
|
||||
*JSRE
|
||||
jeth *jeth
|
||||
}
|
||||
|
||||
func NewJEthRE(ethereum *eth.Ethereum, assetPath string) (self *JEthRE) {
|
||||
re := NewJSRE(assetPath)
|
||||
self = &JEthRE{
|
||||
JSRE: re,
|
||||
jeth: &jeth{xeth.New(ethereum), re.toVal},
|
||||
}
|
||||
self.Load("bignumber.min.js")
|
||||
self.Bind("eth", self.jeth)
|
||||
jsrelogger.Infoln("Javascript-Ethereum Runtime Environment started")
|
||||
return
|
||||
}
|
||||
150
jethre/jethre_test.go
Normal file
150
jethre/jethre_test.go
Normal file
|
|
@ -0,0 +1,150 @@
|
|||
package javascript
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path"
|
||||
"testing"
|
||||
|
||||
"github.com/ethereum/go-ethereum/eth"
|
||||
"github.com/ethereum/go-ethereum/ethutil"
|
||||
)
|
||||
|
||||
func TestJEthRE(t *testing.T) {
|
||||
|
||||
err := os.MkdirAll("/tmp/eth/data/", os.ModePerm)
|
||||
if err != nil {
|
||||
t.Errorf("%v", err)
|
||||
return
|
||||
}
|
||||
|
||||
ethereum, err := eth.New(ð.Config{
|
||||
DataDir: "/tmp/eth",
|
||||
KeyStore: "file",
|
||||
})
|
||||
if err != nil {
|
||||
t.Errorf("%v", err)
|
||||
return
|
||||
}
|
||||
|
||||
err = ethutil.WriteFile("/tmp/eth/default.prv", []byte("946d0fe6dd95ef5476dd1fd204626b59c973bd72ffac4a108827ef488465cc68"))
|
||||
if err != nil {
|
||||
t.Errorf("%v", err)
|
||||
return
|
||||
}
|
||||
t.Logf("ethereum %#v", ethereum)
|
||||
assetPath := path.Join(os.Getenv("GOPATH"), "src", "github.com", "ethereum", "go-ethereum", "cmd", "mist", "assets", "ext")
|
||||
jethre := NewJEthRE(ethereum, assetPath)
|
||||
|
||||
val, err := jethre.Run("eth.getCoinbase()")
|
||||
if err != nil {
|
||||
t.Errorf("expected no error, got %v", err)
|
||||
}
|
||||
|
||||
pp, err := jethre.PrettyPrint(val)
|
||||
if err != nil {
|
||||
t.Errorf("%v", err)
|
||||
}
|
||||
|
||||
if !val.IsString() {
|
||||
t.Errorf("incorrect type, expected string, got %v: %v", val, pp)
|
||||
}
|
||||
strVal, _ := val.ToString()
|
||||
expected := "0x25ec29286951d5acc52a4f4d631f479c1002f97b"
|
||||
if strVal != expected {
|
||||
t.Errorf("incorrect result, expected %s, got %v", expected, strVal)
|
||||
}
|
||||
|
||||
// should get current block
|
||||
// val, err = jethre.Run("eth.getBlock()")
|
||||
// if err != nil {
|
||||
// t.Errorf("expected no error, got %v", err)
|
||||
// }
|
||||
|
||||
fn := "/tmp/eth/data/blockchain.0"
|
||||
val, err = jethre.Run("eth.export(\"" + fn + "\")")
|
||||
if err != nil {
|
||||
t.Errorf("expected no error, got %v", err)
|
||||
}
|
||||
if _, err = os.Stat(fn); err != nil {
|
||||
t.Errorf("expected no error on file, got %v", err)
|
||||
}
|
||||
|
||||
ethereum, err = eth.New(ð.Config{
|
||||
DataDir: "/tmp/eth1",
|
||||
KeyStore: "file",
|
||||
})
|
||||
val, err = jethre.Run("eth.import(\"" + fn + "\")")
|
||||
if err != nil {
|
||||
t.Errorf("expected no error, got %v", err)
|
||||
}
|
||||
|
||||
// var val0 otto.Value
|
||||
|
||||
// should get current block
|
||||
// val0, err = jethre.Run("eth.getBlock()")
|
||||
// if err != nil {
|
||||
// t.Errorf("expected no error, got %v", err)
|
||||
// }
|
||||
|
||||
// if v0 != v1 {
|
||||
// t.Errorf("expected same head after export-import, got %v (!=%v)", v1, v0)
|
||||
// }
|
||||
|
||||
ethereum.Start()
|
||||
// FIXME:
|
||||
// ethereum.Stop causes panic: runtime error: invalid memory address or nil pointer
|
||||
// github.com/ethereum/go-ethereum/eth.(*Ethereum).Stop(0xc208f46270)
|
||||
// /Users/tron/Work/ethereum/go/src/github.com/ethereum/go-ethereum/eth/backend.go:292 +0xdc
|
||||
// defer ethereum.Stop()
|
||||
|
||||
val, err = jethre.Run("eth.isMining()")
|
||||
if err != nil {
|
||||
t.Errorf("expected no error, got %v", err)
|
||||
}
|
||||
var mining bool
|
||||
mining, err = val.ToBoolean()
|
||||
if err != nil {
|
||||
t.Errorf("expected boolean, got %v", err)
|
||||
}
|
||||
if mining {
|
||||
t.Errorf("expected false (not mining), got true")
|
||||
}
|
||||
|
||||
val, err = jethre.Run("eth.setMining(true)")
|
||||
if err != nil {
|
||||
t.Errorf("expected no error, got %v", err)
|
||||
}
|
||||
mining, _ = val.ToBoolean()
|
||||
if !mining {
|
||||
t.Errorf("expected true (mining), got false")
|
||||
}
|
||||
val, err = jethre.Run("eth.isMining()")
|
||||
if err != nil {
|
||||
t.Errorf("expected no error, got %v", err)
|
||||
}
|
||||
mining, err = val.ToBoolean()
|
||||
if err != nil {
|
||||
t.Errorf("expected boolean, got %v", err)
|
||||
}
|
||||
if !mining {
|
||||
t.Errorf("expected true (mining), got false")
|
||||
}
|
||||
|
||||
val, err = jethre.Run("eth.setMining(true)")
|
||||
if err != nil {
|
||||
t.Errorf("expected no error, got %v", err)
|
||||
}
|
||||
mining, _ = val.ToBoolean()
|
||||
if !mining {
|
||||
t.Errorf("expected true (mining), got false")
|
||||
}
|
||||
|
||||
val, err = jethre.Run("eth.setMining(false)")
|
||||
if err != nil {
|
||||
t.Errorf("expected no error, got %v", err)
|
||||
}
|
||||
mining, _ = val.ToBoolean()
|
||||
if mining {
|
||||
t.Errorf("expected false (not mining), got true")
|
||||
}
|
||||
}
|
||||
117
jethre/jsre.go
Normal file
117
jethre/jsre.go
Normal file
|
|
@ -0,0 +1,117 @@
|
|||
package javascript
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/obscuren/otto"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/ethereum/go-ethereum/ethutil"
|
||||
)
|
||||
|
||||
/*
|
||||
JSRE is a generic JS runtime environment embedding the otto JS interpreter.
|
||||
It provides some helper functions to
|
||||
- load code from files
|
||||
- run code snippets
|
||||
- require libraries
|
||||
- bind native go objects
|
||||
*/
|
||||
type JSRE struct {
|
||||
assetPath string
|
||||
vm *otto.Otto
|
||||
}
|
||||
|
||||
func NewJSRE(assetPath string) *JSRE {
|
||||
re := &JSRE{
|
||||
assetPath,
|
||||
otto.New(),
|
||||
}
|
||||
|
||||
// load prettyprint func definition
|
||||
re.vm.Run(pp_js)
|
||||
return re
|
||||
}
|
||||
|
||||
func (self *JSRE) Load(file string) error {
|
||||
return self.load(ethutil.AbsolutePath(self.assetPath, file))
|
||||
}
|
||||
|
||||
func (self *JSRE) load(path string) error {
|
||||
code, err := ioutil.ReadFile(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = self.vm.Run(code)
|
||||
return err
|
||||
}
|
||||
|
||||
func (self *JSRE) Bind(name string, v interface{}) (err error) {
|
||||
self.vm.Set(name, v)
|
||||
var t otto.Value
|
||||
t, err = self.vm.Get(name)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
o := t.Object()
|
||||
o.Set("require", self.require)
|
||||
return
|
||||
}
|
||||
|
||||
func (self *JSRE) Run(code string) (otto.Value, error) {
|
||||
return self.vm.Run(code)
|
||||
}
|
||||
|
||||
func (self *JSRE) Require(file string) error {
|
||||
if len(filepath.Ext(file)) == 0 {
|
||||
file += ".js"
|
||||
}
|
||||
|
||||
fh, err := os.Open(file)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
content, _ := ioutil.ReadAll(fh)
|
||||
self.Run("exports = {};(function() {" + string(content) + "})();")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *JSRE) require(call otto.FunctionCall) otto.Value {
|
||||
file, err := call.Argument(0).ToString()
|
||||
if err != nil {
|
||||
return otto.UndefinedValue()
|
||||
}
|
||||
if err := self.Require(file); err != nil {
|
||||
fmt.Println("err:", err)
|
||||
return otto.UndefinedValue()
|
||||
}
|
||||
|
||||
t, _ := self.vm.Get("exports")
|
||||
|
||||
return t
|
||||
}
|
||||
|
||||
func (self *JSRE) PrettyPrint(v interface{}) (val otto.Value, err error) {
|
||||
var method otto.Value
|
||||
v, err = self.vm.ToValue(v)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
method, err = self.vm.Get("prettyPrint")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return method.Call(method, v)
|
||||
}
|
||||
|
||||
func (self *JSRE) toVal(v interface{}) otto.Value {
|
||||
result, err := self.vm.ToValue(v)
|
||||
if err != nil {
|
||||
fmt.Println("Value unknown:", err)
|
||||
return otto.UndefinedValue()
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
package javascript
|
||||
|
||||
const jsLib = `
|
||||
const pp_js = `
|
||||
function pp(object) {
|
||||
var str = "";
|
||||
|
||||
49
xeth/xeth.go
49
xeth/xeth.go
|
|
@ -8,6 +8,7 @@ import (
|
|||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/ethereum/go-ethereum/core"
|
||||
"github.com/ethereum/go-ethereum/core/types"
|
||||
|
|
@ -17,6 +18,7 @@ import (
|
|||
"github.com/ethereum/go-ethereum/logger"
|
||||
"github.com/ethereum/go-ethereum/miner"
|
||||
"github.com/ethereum/go-ethereum/p2p"
|
||||
"github.com/ethereum/go-ethereum/rlp"
|
||||
"github.com/ethereum/go-ethereum/state"
|
||||
"github.com/ethereum/go-ethereum/whisper"
|
||||
)
|
||||
|
|
@ -30,6 +32,7 @@ type Backend interface {
|
|||
TxPool() *core.TxPool
|
||||
PeerCount() int
|
||||
IsListening() bool
|
||||
SuggestPeer(nodeURL string) error
|
||||
Peers() []*p2p.Peer
|
||||
KeyManager() *crypto.KeyManager
|
||||
BlockDb() ethutil.Database
|
||||
|
|
@ -94,6 +97,23 @@ func (self *XEth) BlockByNumber(num int32) *Block {
|
|||
return NewBlock(self.chainManager.GetBlockByNumber(uint64(num)))
|
||||
}
|
||||
|
||||
// func (self *XEth) DumpBlockByHash(strHash string) (state.World, error) {
|
||||
// block := self.chainManager.GetBlock(ethutil.Hex2Bytes(hash))
|
||||
// if block == nil {
|
||||
// return fmt.Errorf("block %x not found", strHash[:4])
|
||||
// }
|
||||
// statedb := state.New(block.Root(), self.eth.StateDb())
|
||||
// return statedb.RawDump()
|
||||
// }
|
||||
|
||||
// func (self *XEth) DumpBlockByNumber(num int32) *Block {
|
||||
// if num == -1 {
|
||||
// return NewBlock(self.chainManager.CurrentBlock())
|
||||
// }
|
||||
|
||||
// return NewBlock(self.chainManager.GetBlockByNumber(uint64(num)))
|
||||
// }
|
||||
|
||||
func (self *XEth) Block(v interface{}) *Block {
|
||||
if n, ok := v.(int32); ok {
|
||||
return self.BlockByNumber(n)
|
||||
|
|
@ -313,3 +333,32 @@ func (self *XEth) Transact(toStr, valueStr, gasStr, gasPriceStr, codeStr string)
|
|||
|
||||
return toHex(tx.Hash()), nil
|
||||
}
|
||||
|
||||
func (self *XEth) SuggestPeer(nodeURL string) error {
|
||||
return self.eth.SuggestPeer(nodeURL)
|
||||
}
|
||||
|
||||
func (self *XEth) Export(fn string) error {
|
||||
data := self.chainManager.Export()
|
||||
return ethutil.WriteFile(fn, data)
|
||||
}
|
||||
|
||||
func (self *XEth) Import(fn string) (err error) {
|
||||
var fh *os.File
|
||||
fh, err = os.OpenFile(fn, os.O_RDONLY, os.ModePerm)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer fh.Close()
|
||||
|
||||
var blocks types.Blocks
|
||||
if err = rlp.Decode(fh, &blocks); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
self.chainManager.Reset()
|
||||
if err = self.chainManager.InsertChain(blocks); err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue