diff options
author | obscuren <geffobscura@gmail.com> | 2014-12-20 09:34:12 +0800 |
---|---|---|
committer | obscuren <geffobscura@gmail.com> | 2014-12-20 09:34:12 +0800 |
commit | 3983dd2428137211f84f299f9ce8690c22f50afd (patch) | |
tree | 3a2dc53b365e6f377fc82a3514150d1297fe549c /javascript | |
parent | 7daa8c2f6eb25511c6a54ad420709af911fc6748 (diff) | |
parent | 0a9dc1536c5d776844d6947a0090ff7e1a7c6ab4 (diff) | |
download | dexon-3983dd2428137211f84f299f9ce8690c22f50afd.tar dexon-3983dd2428137211f84f299f9ce8690c22f50afd.tar.gz dexon-3983dd2428137211f84f299f9ce8690c22f50afd.tar.bz2 dexon-3983dd2428137211f84f299f9ce8690c22f50afd.tar.lz dexon-3983dd2428137211f84f299f9ce8690c22f50afd.tar.xz dexon-3983dd2428137211f84f299f9ce8690c22f50afd.tar.zst dexon-3983dd2428137211f84f299f9ce8690c22f50afd.zip |
Merge branch 'release/v0.7.10'
Diffstat (limited to 'javascript')
-rw-r--r-- | javascript/javascript_runtime.go | 94 | ||||
-rw-r--r-- | javascript/js_lib.go | 4 | ||||
-rw-r--r-- | javascript/types.go | 34 |
3 files changed, 68 insertions, 64 deletions
diff --git a/javascript/javascript_runtime.go b/javascript/javascript_runtime.go index ffc672a63..a26f0154e 100644 --- a/javascript/javascript_runtime.go +++ b/javascript/javascript_runtime.go @@ -7,27 +7,26 @@ import ( "path" "path/filepath" - "github.com/ethereum/eth-go" - "github.com/ethereum/eth-go/ethchain" - "github.com/ethereum/eth-go/ethlog" - "github.com/ethereum/eth-go/ethpipe" - "github.com/ethereum/eth-go/ethreact" - "github.com/ethereum/eth-go/ethstate" - "github.com/ethereum/eth-go/ethutil" - "github.com/ethereum/go-ethereum/utils" + "github.com/ethereum/go-ethereum" + "github.com/ethereum/go-ethereum/cmd/utils" + "github.com/ethereum/go-ethereum/core" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/ethutil" + "github.com/ethereum/go-ethereum/event" + "github.com/ethereum/go-ethereum/logger" + "github.com/ethereum/go-ethereum/state" + "github.com/ethereum/go-ethereum/xeth" "github.com/obscuren/otto" ) -var jsrelogger = ethlog.NewLogger("JSRE") +var jsrelogger = logger.NewLogger("JSRE") type JSRE struct { ethereum *eth.Ethereum Vm *otto.Otto - pipe *ethpipe.JSPipe + pipe *xeth.JSXEth - blockChan chan ethreact.Event - changeChan chan ethreact.Event - quitChan chan bool + events event.Subscription objectCb map[string][]otto.Value } @@ -42,7 +41,7 @@ func (jsre *JSRE) LoadExtFile(path string) { } func (jsre *JSRE) LoadIntFile(file string) { - assetPath := path.Join(os.Getenv("GOPATH"), "src", "github.com", "ethereum", "go-ethereum", "mist", "assets", "ext") + assetPath := path.Join(os.Getenv("GOPATH"), "src", "github.com", "ethereum", "go-ethereum", "cmd", "mist", "assets", "ext") jsre.LoadExtFile(path.Join(assetPath, file)) } @@ -50,10 +49,8 @@ func NewJSRE(ethereum *eth.Ethereum) *JSRE { re := &JSRE{ ethereum, otto.New(), - ethpipe.NewJSPipe(ethereum), - make(chan ethreact.Event, 10), - make(chan ethreact.Event, 10), - make(chan bool), + xeth.NewJSXEth(ethereum), + nil, make(map[string][]otto.Value), } @@ -64,13 +61,13 @@ func NewJSRE(ethereum *eth.Ethereum) *JSRE { re.LoadIntFile("string.js") re.LoadIntFile("big.js") + // Subscribe to events + mux := ethereum.EventMux() + re.events = mux.Subscribe(core.NewBlockEvent{}) + // We have to make sure that, whoever calls this, calls "Stop" go re.mainLoop() - // Subscribe to events - reactor := ethereum.Reactor() - reactor.Subscribe("newBlock", re.blockChan) - re.Bind("eth", &JSEthereum{re.pipe, re.Vm, ethereum}) re.initStdFuncs() @@ -105,25 +102,12 @@ func (self *JSRE) Require(file string) error { } func (self *JSRE) Stop() { - // Kill the main loop - self.quitChan <- true - - close(self.blockChan) - close(self.quitChan) - close(self.changeChan) + self.events.Unsubscribe() jsrelogger.Infoln("stopped") } func (self *JSRE) mainLoop() { -out: - for { - select { - case <-self.quitChan: - break out - case block := <-self.blockChan: - if _, ok := block.Resource.(*ethchain.Block); ok { - } - } + for _ = range self.events.Chan() { } } @@ -137,6 +121,7 @@ func (self *JSRE) initStdFuncs() { eth.Set("startMining", self.startMining) eth.Set("execBlock", self.execBlock) eth.Set("dump", self.dump) + eth.Set("export", self.export) } /* @@ -144,16 +129,16 @@ func (self *JSRE) initStdFuncs() { */ func (self *JSRE) dump(call otto.FunctionCall) otto.Value { - var state *ethstate.State + var state *state.StateDB if len(call.ArgumentList) > 0 { - var block *ethchain.Block + var block *types.Block if call.Argument(0).IsNumber() { num, _ := call.Argument(0).ToInteger() - block = self.ethereum.BlockChain().GetBlockByNumber(uint64(num)) + block = self.ethereum.ChainManager().GetBlockByNumber(uint64(num)) } else if call.Argument(0).IsString() { hash, _ := call.Argument(0).ToString() - block = self.ethereum.BlockChain().GetBlock(ethutil.Hex2Bytes(hash)) + block = self.ethereum.ChainManager().GetBlock(ethutil.Hex2Bytes(hash)) } else { fmt.Println("invalid argument for dump. Either hex string or number") } @@ -166,7 +151,7 @@ func (self *JSRE) dump(call otto.FunctionCall) otto.Value { state = block.State() } else { - state = self.ethereum.StateManager().CurrentState() + state = self.ethereum.ChainManager().State() } v, _ := self.Vm.ToValue(state.Dump()) @@ -201,13 +186,13 @@ 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.Hex2Bytes(addr)) + ":" + string(ethutil.Hex2Bytes(storageAddr)) - self.ethereum.Reactor().Subscribe(event, self.changeChan) + // event := "storage:" + string(ethutil.Hex2Bytes(addr)) + ":" + string(ethutil.Hex2Bytes(storageAddr)) + // self.ethereum.EventMux().Subscribe(event, self.changeChan) } else { self.objectCb[addr] = append(self.objectCb[addr], cb) - event := "object:" + string(ethutil.Hex2Bytes(addr)) - self.ethereum.Reactor().Subscribe(event, self.changeChan) + // event := "object:" + string(ethutil.Hex2Bytes(addr)) + // self.ethereum.EventMux().Subscribe(event, self.changeChan) } return otto.UndefinedValue() @@ -252,3 +237,20 @@ func (self *JSRE) execBlock(call otto.FunctionCall) otto.Value { return otto.TrueValue() } + +func (self *JSRE) export(call otto.FunctionCall) otto.Value { + 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() +} diff --git a/javascript/js_lib.go b/javascript/js_lib.go index a3e9b8a5b..dd1fe5f4d 100644 --- a/javascript/js_lib.go +++ b/javascript/js_lib.go @@ -44,9 +44,11 @@ function pp(object) { function prettyPrint(/* */) { var args = arguments; + var ret = ""; for(var i = 0, l = args.length; i < l; i++) { - console.log(pp(args[i])) + ret += pp(args[i]) + "\n"; } + return ret; } var print = prettyPrint; diff --git a/javascript/types.go b/javascript/types.go index 53a2977a8..d5acaecce 100644 --- a/javascript/types.go +++ b/javascript/types.go @@ -3,16 +3,16 @@ package javascript import ( "fmt" - "github.com/ethereum/eth-go" - "github.com/ethereum/eth-go/ethchain" - "github.com/ethereum/eth-go/ethpipe" - "github.com/ethereum/eth-go/ethstate" - "github.com/ethereum/eth-go/ethutil" + "github.com/ethereum/go-ethereum" + "github.com/ethereum/go-ethereum/ethutil" + "github.com/ethereum/go-ethereum/state" + "github.com/ethereum/go-ethereum/ui" + "github.com/ethereum/go-ethereum/xeth" "github.com/obscuren/otto" ) type JSStateObject struct { - *ethpipe.JSObject + *xeth.JSObject eth *JSEthereum } @@ -30,7 +30,7 @@ func (self *JSStateObject) EachStorage(call otto.FunctionCall) otto.Value { // The JSEthereum object attempts to wrap the PEthereum object and returns // meaningful javascript objects type JSBlock struct { - *ethpipe.JSBlock + *xeth.JSBlock eth *JSEthereum } @@ -51,7 +51,7 @@ type JSMessage struct { Number int32 `json:"number"` } -func NewJSMessage(message *ethstate.Message) JSMessage { +func NewJSMessage(message *state.Message) JSMessage { return JSMessage{ To: ethutil.Bytes2Hex(message.To), From: ethutil.Bytes2Hex(message.From), @@ -67,33 +67,33 @@ func NewJSMessage(message *ethstate.Message) JSMessage { } type JSEthereum struct { - *ethpipe.JSPipe + *xeth.JSXEth vm *otto.Otto ethereum *eth.Ethereum } func (self *JSEthereum) GetBlock(hash string) otto.Value { - return self.toVal(&JSBlock{self.JSPipe.BlockByHash(hash), self}) + return self.toVal(&JSBlock{self.JSXEth.BlockByHash(hash), self}) } func (self *JSEthereum) GetPeers() otto.Value { - return self.toVal(self.JSPipe.Peers()) + return self.toVal(self.JSXEth.Peers()) } func (self *JSEthereum) GetKey() otto.Value { - return self.toVal(self.JSPipe.Key()) + return self.toVal(self.JSXEth.Key()) } func (self *JSEthereum) GetStateObject(addr string) otto.Value { - return self.toVal(&JSStateObject{ethpipe.NewJSObject(self.JSPipe.World().SafeGet(ethutil.Hex2Bytes(addr))), self}) + return self.toVal(&JSStateObject{xeth.NewJSObject(self.JSXEth.World().SafeGet(ethutil.Hex2Bytes(addr))), self}) } func (self *JSEthereum) Peers() otto.Value { - return self.toVal(self.JSPipe.Peers()) + return self.toVal(self.JSXEth.Peers()) } func (self *JSEthereum) Transact(key, recipient, valueStr, gasStr, gasPriceStr, dataStr string) otto.Value { - r, err := self.JSPipe.Transact(key, recipient, valueStr, gasStr, gasPriceStr, dataStr) + r, err := self.JSXEth.Transact(key, recipient, valueStr, gasStr, gasPriceStr, dataStr) if err != nil { fmt.Println(err) @@ -104,7 +104,7 @@ func (self *JSEthereum) Transact(key, recipient, valueStr, gasStr, gasPriceStr, } func (self *JSEthereum) Create(key, valueStr, gasStr, gasPriceStr, scriptStr string) otto.Value { - r, err := self.JSPipe.Transact(key, "", valueStr, gasStr, gasPriceStr, scriptStr) + r, err := self.JSXEth.Transact(key, "", valueStr, gasStr, gasPriceStr, scriptStr) if err != nil { fmt.Println(err) @@ -128,7 +128,7 @@ func (self *JSEthereum) toVal(v interface{}) otto.Value { } func (self *JSEthereum) Messages(object map[string]interface{}) otto.Value { - filter := ethchain.NewFilterFromMap(object, self.ethereum) + filter := ui.NewFilterFromMap(object, self.ethereum) messages := filter.Find() var msgs []JSMessage |