mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-22 04:36:42 +00:00
javascript ethereum runtime environment initial commit
- rework javascript runtime as pure jsre, a convenience wrapper for otto - jethre javascript ethereum runtime environment provides bindings for xeth using JSRE - simplify interface the object that binds via otto only receives toVal func not entire vm - only import JSRE and JEthRE - start with bindings for the newly added xeth methods (suggestPeers, Import/Export, SetMining) - added GetCoinbase() with test
This commit is contained in:
parent
69bf9f5af6
commit
5aed2d2d16
7 changed files with 402 additions and 198 deletions
|
|
@ -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
|
||||
}
|
||||
198
jethre/jeth.go
Normal file
198
jethre/jeth.go
Normal file
|
|
@ -0,0 +1,198 @@
|
|||
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) 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
|
||||
}
|
||||
45
jethre/jethre_test.go
Normal file
45
jethre/jethre_test.go
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
package javascript
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path"
|
||||
"testing"
|
||||
|
||||
"github.com/ethereum/go-ethereum/eth"
|
||||
"github.com/ethereum/go-ethereum/ethutil"
|
||||
)
|
||||
|
||||
func TestJEthRE(t *testing.T) {
|
||||
ethereum, err := eth.New(ð.Config{
|
||||
DataDir: "/tmp/eth",
|
||||
KeyStore: "file",
|
||||
})
|
||||
// os.WriteFile("/tmp/eth/default.addr", []byte("25ec29286951d5acc52a4f4d631f479c1002f97"))
|
||||
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)
|
||||
}
|
||||
}
|
||||
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 = "";
|
||||
|
||||
Loading…
Reference in a new issue