From a05adb11288a1ea9dc6e38a952ab89fa5eb7f794 Mon Sep 17 00:00:00 2001 From: obscuren Date: Tue, 20 May 2014 12:48:34 +0200 Subject: Refactored file structure --- ethereum/javascript_runtime.go | 114 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 ethereum/javascript_runtime.go (limited to 'ethereum/javascript_runtime.go') diff --git a/ethereum/javascript_runtime.go b/ethereum/javascript_runtime.go new file mode 100644 index 000000000..b06fb9460 --- /dev/null +++ b/ethereum/javascript_runtime.go @@ -0,0 +1,114 @@ +package main + +import ( + "fmt" + "github.com/ethereum/eth-go" + "github.com/ethereum/eth-go/ethchain" + "github.com/ethereum/eth-go/ethpub" + "github.com/ethereum/eth-go/ethutil" + "github.com/robertkrimen/otto" +) + +type JSRE struct { + ethereum *eth.Ethereum + vm *otto.Otto + lib *ethpub.PEthereum + + blockChan chan ethutil.React + changeChan chan ethutil.React + quitChan chan bool + + objectCb map[string][]otto.Value +} + +func NewJSRE(ethereum *eth.Ethereum) *JSRE { + re := &JSRE{ + ethereum, + otto.New(), + ethpub.NewPEthereum(ethereum), + make(chan ethutil.React, 1), + make(chan ethutil.React, 1), + make(chan bool), + make(map[string][]otto.Value), + } + + // Init the JS lib + re.vm.Run(jsLib) + + // We have to make sure that, whoever calls this, calls "Stop" + go re.mainLoop() + + re.Bind("eth", &JSEthereum{re.lib, re.vm}) + + re.initStdFuncs() + + 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) Stop() { + // Kill the main loop + self.quitChan <- true + + close(self.blockChan) + close(self.quitChan) + close(self.changeChan) +} + +func (self *JSRE) mainLoop() { + // Subscribe to events + reactor := self.ethereum.Reactor() + reactor.Subscribe("newBlock", self.blockChan) + +out: + for { + select { + case <-self.quitChan: + break out + case block := <-self.blockChan: + if _, ok := block.Resource.(*ethchain.Block); ok { + } + case object := <-self.changeChan: + if stateObject, ok := object.Resource.(*ethchain.StateObject); ok { + for _, cb := range self.objectCb[ethutil.Hex(stateObject.Address())] { + val, _ := self.vm.ToValue(ethpub.NewPStateObject(stateObject)) + cb.Call(cb, val) + } + } else if storageObject, ok := object.Resource.(*ethchain.StorageState); ok { + fmt.Println(storageObject) + } + } + } +} + +func (self *JSRE) initStdFuncs() { + t, _ := self.vm.Get("eth") + eth := t.Object() + eth.Set("watch", func(call otto.FunctionCall) otto.Value { + addr, _ := call.Argument(0).ToString() + cb := call.Argument(1) + + self.objectCb[addr] = append(self.objectCb[addr], cb) + + event := "object:" + string(ethutil.FromHex(addr)) + self.ethereum.Reactor().Subscribe(event, self.changeChan) + + return otto.UndefinedValue() + }) + eth.Set("addPeer", func(call otto.FunctionCall) otto.Value { + host, err := call.Argument(0).ToString() + if err != nil { + return otto.FalseValue() + } + self.ethereum.ConnectToPeer(host) + + return otto.TrueValue() + }) +} -- cgit v1.2.3 From de1dfae7170a946d255a9b4932e08f887d48947c Mon Sep 17 00:00:00 2001 From: obscuren Date: Tue, 20 May 2014 17:49:12 +0200 Subject: Forked version of otto so we can support lowerCased methods --- ethereum/javascript_runtime.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'ethereum/javascript_runtime.go') diff --git a/ethereum/javascript_runtime.go b/ethereum/javascript_runtime.go index b06fb9460..cc8fe370f 100644 --- a/ethereum/javascript_runtime.go +++ b/ethereum/javascript_runtime.go @@ -6,7 +6,7 @@ import ( "github.com/ethereum/eth-go/ethchain" "github.com/ethereum/eth-go/ethpub" "github.com/ethereum/eth-go/ethutil" - "github.com/robertkrimen/otto" + "github.com/obscuren/otto" ) type JSRE struct { -- cgit v1.2.3 From 563c035eb57a0507979a84f3dd22411be2a4cad1 Mon Sep 17 00:00:00 2001 From: obscuren Date: Tue, 20 May 2014 19:28:48 +0200 Subject: Refactored some of the functions --- ethereum/javascript_runtime.go | 89 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 76 insertions(+), 13 deletions(-) (limited to 'ethereum/javascript_runtime.go') diff --git a/ethereum/javascript_runtime.go b/ethereum/javascript_runtime.go index cc8fe370f..76ced3f3d 100644 --- a/ethereum/javascript_runtime.go +++ b/ethereum/javascript_runtime.go @@ -7,6 +7,9 @@ import ( "github.com/ethereum/eth-go/ethpub" "github.com/ethereum/eth-go/ethutil" "github.com/obscuren/otto" + "io/ioutil" + "os" + "path/filepath" ) type JSRE struct { @@ -53,6 +56,22 @@ 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) Stop() { // Kill the main loop self.quitChan <- true @@ -82,7 +101,10 @@ out: cb.Call(cb, val) } } else if storageObject, ok := object.Resource.(*ethchain.StorageState); ok { - fmt.Println(storageObject) + for _, cb := range self.objectCb[ethutil.Hex(storageObject.Address)+ethutil.Hex(storageObject.StateAddress)] { + val, _ := self.vm.ToValue(ethpub.NewPStorageState(storageObject)) + cb.Call(cb, val) + } } } } @@ -91,24 +113,65 @@ out: func (self *JSRE) initStdFuncs() { t, _ := self.vm.Get("eth") eth := t.Object() - eth.Set("watch", func(call otto.FunctionCall) otto.Value { - addr, _ := call.Argument(0).ToString() - cb := call.Argument(1) + eth.Set("watch", self.watch) + eth.Set("addPeer", self.addPeer) + self.Set("require", self.require) +} + +/* + * The following methods are natively implemented javascript functions + */ + +// eth.watch +func (self JSRE) watch(call otto.FunctionCall) otto.Value { + addr, _ := call.Argument(0).ToString() + var storageAddr string + var cb otto.Value + var storageCallback bool + if len(call.ArgumentList) > 2 { + storageCallback = true + storageAddr, _ = call.Argument(1).ToString() + cb = call.Argument(2) + } else { + cb = call.Argument(1) + } + + if storageCallback { + self.objectCb[addr+storageAddr] = append(self.objectCb[addr+storageAddr], cb) + event := "storage:" + string(ethutil.FromHex(addr)) + ":" + string(ethutil.FromHex(storageAddr)) + self.ethereum.Reactor().Subscribe(event, self.changeChan) + } else { self.objectCb[addr] = append(self.objectCb[addr], cb) event := "object:" + string(ethutil.FromHex(addr)) self.ethereum.Reactor().Subscribe(event, self.changeChan) + } + + return otto.UndefinedValue() +} +func (self *JSRE) addPeer(call otto.FunctionCall) otto.Value { + host, err := call.Argument(0).ToString() + if err != nil { + return otto.FalseValue() + } + self.ethereum.ConnectToPeer(host) + + return otto.TrueValue() +} + +func (self *JSRE) require(call otto.FunctionCall) otto.Value { + file, err := call.Argument(0).ToString() + if err != nil { return otto.UndefinedValue() - }) - eth.Set("addPeer", func(call otto.FunctionCall) otto.Value { - host, err := call.Argument(0).ToString() - if err != nil { - return otto.FalseValue() - } - self.ethereum.ConnectToPeer(host) + } + if err := self.Require(file); err != nil { + fmt.Println("err:", err) + return otto.UndefinedValue() + } + + t, _ := self.vm.Get("exports") - return otto.TrueValue() - }) + return t } -- cgit v1.2.3 From 93e12250c702521807247a66d74cc0792370d83b Mon Sep 17 00:00:00 2001 From: obscuren Date: Tue, 20 May 2014 22:12:42 +0200 Subject: Switch variables as intended --- ethereum/javascript_runtime.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'ethereum/javascript_runtime.go') diff --git a/ethereum/javascript_runtime.go b/ethereum/javascript_runtime.go index 76ced3f3d..fa01c7005 100644 --- a/ethereum/javascript_runtime.go +++ b/ethereum/javascript_runtime.go @@ -101,7 +101,7 @@ out: cb.Call(cb, val) } } else if storageObject, ok := object.Resource.(*ethchain.StorageState); ok { - for _, cb := range self.objectCb[ethutil.Hex(storageObject.Address)+ethutil.Hex(storageObject.StateAddress)] { + for _, cb := range self.objectCb[ethutil.Hex(storageObject.StateAddress)+ethutil.Hex(storageObject.Address)] { val, _ := self.vm.ToValue(ethpub.NewPStorageState(storageObject)) cb.Call(cb, val) } @@ -115,7 +115,7 @@ func (self *JSRE) initStdFuncs() { eth := t.Object() eth.Set("watch", self.watch) eth.Set("addPeer", self.addPeer) - self.Set("require", self.require) + eth.Set("require", self.require) } /* -- cgit v1.2.3 From 01b833146f3afa214586a1ffb710546a5e4cc90a Mon Sep 17 00:00:00 2001 From: obscuren Date: Thu, 22 May 2014 00:25:48 +0200 Subject: Added mining stop and start --- ethereum/javascript_runtime.go | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'ethereum/javascript_runtime.go') diff --git a/ethereum/javascript_runtime.go b/ethereum/javascript_runtime.go index fa01c7005..93297f604 100644 --- a/ethereum/javascript_runtime.go +++ b/ethereum/javascript_runtime.go @@ -6,6 +6,7 @@ import ( "github.com/ethereum/eth-go/ethchain" "github.com/ethereum/eth-go/ethpub" "github.com/ethereum/eth-go/ethutil" + "github.com/ethereum/go-ethereum/utils" "github.com/obscuren/otto" "io/ioutil" "os" @@ -116,14 +117,26 @@ func (self *JSRE) initStdFuncs() { eth.Set("watch", self.watch) eth.Set("addPeer", self.addPeer) eth.Set("require", self.require) + eth.Set("stopMining", self.stopMining) + eth.Set("startMining", self.startMining) } /* * The following methods are natively implemented javascript functions */ +func (self *JSRE) stopMining(call otto.FunctionCall) otto.Value { + v, _ := self.vm.ToValue(utils.StopMining(self.ethereum)) + return v +} + +func (self *JSRE) startMining(call otto.FunctionCall) otto.Value { + v, _ := self.vm.ToValue(utils.StartMining(self.ethereum)) + return v +} + // eth.watch -func (self JSRE) watch(call otto.FunctionCall) otto.Value { +func (self *JSRE) watch(call otto.FunctionCall) otto.Value { addr, _ := call.Argument(0).ToString() var storageAddr string var cb otto.Value -- cgit v1.2.3 From 307fe4a3cd4ff2d3910ed992e6e98a7cd3ca6f87 Mon Sep 17 00:00:00 2001 From: Maran Date: Wed, 4 Jun 2014 12:19:50 +0200 Subject: Add loading of extra build in js files to JS-Repl. Implements #67 --- ethereum/javascript_runtime.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'ethereum/javascript_runtime.go') diff --git a/ethereum/javascript_runtime.go b/ethereum/javascript_runtime.go index 93297f604..b05d39232 100644 --- a/ethereum/javascript_runtime.go +++ b/ethereum/javascript_runtime.go @@ -10,6 +10,7 @@ import ( "github.com/obscuren/otto" "io/ioutil" "os" + "path" "path/filepath" ) @@ -25,6 +26,20 @@ type JSRE struct { objectCb map[string][]otto.Value } +func (jsre *JSRE) LoadExtFile(path string) { + result, err := ioutil.ReadFile(path) + if err == nil { + jsre.vm.Run(result) + } else { + ethutil.Config.Log.Debugln("Could not load file:", path) + } +} + +func (jsre *JSRE) LoadIntFile(file string) { + assetPath := path.Join(os.Getenv("GOPATH"), "src", "github.com", "ethereum", "go-ethereum", "ethereal", "assets", "ext") + jsre.LoadExtFile(path.Join(assetPath, file)) +} + func NewJSRE(ethereum *eth.Ethereum) *JSRE { re := &JSRE{ ethereum, @@ -39,6 +54,10 @@ func NewJSRE(ethereum *eth.Ethereum) *JSRE { // Init the JS lib re.vm.Run(jsLib) + // Load extra javascript files + re.LoadIntFile("string.js") + re.LoadIntFile("big.js") + // We have to make sure that, whoever calls this, calls "Stop" go re.mainLoop() -- cgit v1.2.3 From 176b7802510a667b8973f2be232f7a8213b3474b Mon Sep 17 00:00:00 2001 From: obscuren Date: Mon, 23 Jun 2014 11:28:05 +0200 Subject: Added a execBlock method which replays the given block --- ethereum/javascript_runtime.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'ethereum/javascript_runtime.go') diff --git a/ethereum/javascript_runtime.go b/ethereum/javascript_runtime.go index b05d39232..737f7663f 100644 --- a/ethereum/javascript_runtime.go +++ b/ethereum/javascript_runtime.go @@ -138,6 +138,7 @@ func (self *JSRE) initStdFuncs() { eth.Set("require", self.require) eth.Set("stopMining", self.stopMining) eth.Set("startMining", self.startMining) + eth.Set("blockDo", self.execBlock) } /* @@ -207,3 +208,18 @@ func (self *JSRE) require(call otto.FunctionCall) otto.Value { return t } + +func (self *JSRE) execBlock(call otto.FunctionCall) otto.Value { + hash, err := call.Argument(0).ToString() + if err != nil { + return otto.UndefinedValue() + } + + err = self.ethereum.BlockDo(ethutil.FromHex(hash)) + if err != nil { + fmt.Println(err) + return otto.FalseValue() + } + + return otto.TrueValue() +} -- cgit v1.2.3 From 7bcf875c577cd9710d27dd4511ae21aa62067d79 Mon Sep 17 00:00:00 2001 From: zelig Date: Mon, 23 Jun 2014 11:39:09 +0100 Subject: add logging for jsre --- ethereum/javascript_runtime.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'ethereum/javascript_runtime.go') diff --git a/ethereum/javascript_runtime.go b/ethereum/javascript_runtime.go index b05d39232..34b805e7f 100644 --- a/ethereum/javascript_runtime.go +++ b/ethereum/javascript_runtime.go @@ -6,6 +6,7 @@ import ( "github.com/ethereum/eth-go/ethchain" "github.com/ethereum/eth-go/ethpub" "github.com/ethereum/eth-go/ethutil" + "github.com/ethereum/eth-go/ethlog" "github.com/ethereum/go-ethereum/utils" "github.com/obscuren/otto" "io/ioutil" @@ -14,6 +15,8 @@ import ( "path/filepath" ) +var jsrelogger = ethlog.NewLogger("JSRE") + type JSRE struct { ethereum *eth.Ethereum vm *otto.Otto @@ -31,7 +34,7 @@ func (jsre *JSRE) LoadExtFile(path string) { if err == nil { jsre.vm.Run(result) } else { - ethutil.Config.Log.Debugln("Could not load file:", path) + jsrelogger.Debugln("Could not load file:", path) } } @@ -65,6 +68,8 @@ func NewJSRE(ethereum *eth.Ethereum) *JSRE { re.initStdFuncs() + jsrelogger.Infoln("started") + return re } @@ -99,6 +104,7 @@ func (self *JSRE) Stop() { close(self.blockChan) close(self.quitChan) close(self.changeChan) + jsrelogger.Infoln("stopped") } func (self *JSRE) mainLoop() { -- cgit v1.2.3 From 17e8d7519b1aac322db08b857e63db82a322d6cf Mon Sep 17 00:00:00 2001 From: obscuren Date: Tue, 24 Jun 2014 09:36:05 +0200 Subject: Renamed execBlock --- ethereum/javascript_runtime.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'ethereum/javascript_runtime.go') diff --git a/ethereum/javascript_runtime.go b/ethereum/javascript_runtime.go index 737f7663f..92d9c119f 100644 --- a/ethereum/javascript_runtime.go +++ b/ethereum/javascript_runtime.go @@ -138,7 +138,7 @@ func (self *JSRE) initStdFuncs() { eth.Set("require", self.require) eth.Set("stopMining", self.stopMining) eth.Set("startMining", self.startMining) - eth.Set("blockDo", self.execBlock) + eth.Set("execBlock", self.execBlock) } /* -- cgit v1.2.3 From 1e965cb8f5c63d73a5aac1556a2638345ba2824c Mon Sep 17 00:00:00 2001 From: obscuren Date: Wed, 25 Jun 2014 09:47:11 +0200 Subject: Moved BlockDo to utils --- ethereum/javascript_runtime.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'ethereum/javascript_runtime.go') diff --git a/ethereum/javascript_runtime.go b/ethereum/javascript_runtime.go index 92d9c119f..a9b12629a 100644 --- a/ethereum/javascript_runtime.go +++ b/ethereum/javascript_runtime.go @@ -215,7 +215,7 @@ func (self *JSRE) execBlock(call otto.FunctionCall) otto.Value { return otto.UndefinedValue() } - err = self.ethereum.BlockDo(ethutil.FromHex(hash)) + err = utils.BlockDo(self.ethereum, ethutil.FromHex(hash)) if err != nil { fmt.Println(err) return otto.FalseValue() -- cgit v1.2.3 From ae5ace16190d48bfe7a0364fdb0b51644518ec42 Mon Sep 17 00:00:00 2001 From: zelig Date: Thu, 26 Jun 2014 18:41:36 +0100 Subject: go fmt --- ethereum/javascript_runtime.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'ethereum/javascript_runtime.go') diff --git a/ethereum/javascript_runtime.go b/ethereum/javascript_runtime.go index 0a9a882ad..0dfe07a54 100644 --- a/ethereum/javascript_runtime.go +++ b/ethereum/javascript_runtime.go @@ -1,12 +1,12 @@ - package main +package main import ( "fmt" "github.com/ethereum/eth-go" "github.com/ethereum/eth-go/ethchain" + "github.com/ethereum/eth-go/ethlog" "github.com/ethereum/eth-go/ethpub" "github.com/ethereum/eth-go/ethutil" - "github.com/ethereum/eth-go/ethlog" "github.com/ethereum/go-ethereum/utils" "github.com/obscuren/otto" "io/ioutil" -- cgit v1.2.3 From e38b016547e98a33daf013c98c951254f95aeabf Mon Sep 17 00:00:00 2001 From: zelig Date: Sun, 29 Jun 2014 18:32:48 +0100 Subject: changed name for ethutil hex functions --- ethereum/javascript_runtime.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'ethereum/javascript_runtime.go') diff --git a/ethereum/javascript_runtime.go b/ethereum/javascript_runtime.go index 0dfe07a54..852a50487 100644 --- a/ethereum/javascript_runtime.go +++ b/ethereum/javascript_runtime.go @@ -122,12 +122,12 @@ out: } case object := <-self.changeChan: if stateObject, ok := object.Resource.(*ethchain.StateObject); ok { - for _, cb := range self.objectCb[ethutil.Hex(stateObject.Address())] { + for _, cb := range self.objectCb[ethutil.Bytes2Hex(stateObject.Address())] { val, _ := self.vm.ToValue(ethpub.NewPStateObject(stateObject)) cb.Call(cb, val) } } else if storageObject, ok := object.Resource.(*ethchain.StorageState); ok { - for _, cb := range self.objectCb[ethutil.Hex(storageObject.StateAddress)+ethutil.Hex(storageObject.Address)] { + for _, cb := range self.objectCb[ethutil.Bytes2Hex(storageObject.StateAddress)+ethutil.Bytes2Hex(storageObject.Address)] { val, _ := self.vm.ToValue(ethpub.NewPStorageState(storageObject)) cb.Call(cb, val) } @@ -178,12 +178,12 @@ func (self *JSRE) watch(call otto.FunctionCall) otto.Value { if storageCallback { self.objectCb[addr+storageAddr] = append(self.objectCb[addr+storageAddr], cb) - event := "storage:" + string(ethutil.FromHex(addr)) + ":" + string(ethutil.FromHex(storageAddr)) + event := "storage:" + string(ethutil.Hex2Bytes(addr)) + ":" + string(ethutil.Hex2Bytes(storageAddr)) self.ethereum.Reactor().Subscribe(event, self.changeChan) } else { self.objectCb[addr] = append(self.objectCb[addr], cb) - event := "object:" + string(ethutil.FromHex(addr)) + event := "object:" + string(ethutil.Hex2Bytes(addr)) self.ethereum.Reactor().Subscribe(event, self.changeChan) } @@ -221,7 +221,7 @@ func (self *JSRE) execBlock(call otto.FunctionCall) otto.Value { return otto.UndefinedValue() } - err = utils.BlockDo(self.ethereum, ethutil.FromHex(hash)) + err = utils.BlockDo(self.ethereum, ethutil.Hex2Bytes(hash)) if err != nil { fmt.Println(err) return otto.FalseValue() -- cgit v1.2.3