mirror of https://github.com/ethereum/go-ethereum
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
232 lines
5.3 KiB
232 lines
5.3 KiB
10 years ago
|
package ethrepl
|
||
11 years ago
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"github.com/ethereum/eth-go"
|
||
11 years ago
|
"github.com/ethereum/eth-go/ethchain"
|
||
11 years ago
|
"github.com/ethereum/eth-go/ethlog"
|
||
11 years ago
|
"github.com/ethereum/eth-go/ethpub"
|
||
11 years ago
|
"github.com/ethereum/eth-go/ethutil"
|
||
11 years ago
|
"github.com/ethereum/go-ethereum/utils"
|
||
11 years ago
|
"github.com/obscuren/otto"
|
||
11 years ago
|
"io/ioutil"
|
||
|
"os"
|
||
11 years ago
|
"path"
|
||
11 years ago
|
"path/filepath"
|
||
11 years ago
|
)
|
||
|
|
||
11 years ago
|
var jsrelogger = ethlog.NewLogger("JSRE")
|
||
|
|
||
11 years ago
|
type JSRE struct {
|
||
11 years ago
|
ethereum *eth.Ethereum
|
||
|
vm *otto.Otto
|
||
|
lib *ethpub.PEthereum
|
||
|
|
||
11 years ago
|
blockChan chan ethutil.React
|
||
|
changeChan chan ethutil.React
|
||
11 years ago
|
quitChan chan bool
|
||
|
|
||
|
objectCb map[string][]otto.Value
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
func (jsre *JSRE) LoadExtFile(path string) {
|
||
|
result, err := ioutil.ReadFile(path)
|
||
|
if err == nil {
|
||
|
jsre.vm.Run(result)
|
||
|
} else {
|
||
11 years ago
|
jsrelogger.Debugln("Could not load file:", path)
|
||
11 years ago
|
}
|
||
|
}
|
||
|
|
||
|
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))
|
||
|
}
|
||
|
|
||
11 years ago
|
func NewJSRE(ethereum *eth.Ethereum) *JSRE {
|
||
11 years ago
|
re := &JSRE{
|
||
|
ethereum,
|
||
|
otto.New(),
|
||
|
ethpub.NewPEthereum(ethereum),
|
||
11 years ago
|
make(chan ethutil.React, 1),
|
||
|
make(chan ethutil.React, 1),
|
||
11 years ago
|
make(chan bool),
|
||
|
make(map[string][]otto.Value),
|
||
|
}
|
||
|
|
||
11 years ago
|
// Init the JS lib
|
||
|
re.vm.Run(jsLib)
|
||
|
|
||
11 years ago
|
// Load extra javascript files
|
||
|
re.LoadIntFile("string.js")
|
||
|
re.LoadIntFile("big.js")
|
||
|
|
||
11 years ago
|
// We have to make sure that, whoever calls this, calls "Stop"
|
||
|
go re.mainLoop()
|
||
11 years ago
|
|
||
|
re.Bind("eth", &JSEthereum{re.lib, re.vm})
|
||
11 years ago
|
|
||
11 years ago
|
re.initStdFuncs()
|
||
11 years ago
|
|
||
11 years ago
|
jsrelogger.Infoln("started")
|
||
|
|
||
11 years ago
|
return re
|
||
|
}
|
||
11 years ago
|
|
||
11 years ago
|
func (self *JSRE) Bind(name string, v interface{}) {
|
||
|
self.vm.Set(name, v)
|
||
|
}
|
||
11 years ago
|
|
||
11 years ago
|
func (self *JSRE) Run(code string) (otto.Value, error) {
|
||
|
return self.vm.Run(code)
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
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
|
||
|
}
|
||
|
|
||
11 years ago
|
func (self *JSRE) Stop() {
|
||
|
// Kill the main loop
|
||
|
self.quitChan <- true
|
||
|
|
||
|
close(self.blockChan)
|
||
|
close(self.quitChan)
|
||
|
close(self.changeChan)
|
||
11 years ago
|
jsrelogger.Infoln("stopped")
|
||
11 years ago
|
}
|
||
|
|
||
|
func (self *JSRE) mainLoop() {
|
||
11 years ago
|
// Subscribe to events
|
||
|
reactor := self.ethereum.Reactor()
|
||
|
reactor.Subscribe("newBlock", self.blockChan)
|
||
|
|
||
11 years ago
|
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 {
|
||
11 years ago
|
for _, cb := range self.objectCb[ethutil.Bytes2Hex(stateObject.Address())] {
|
||
11 years ago
|
val, _ := self.vm.ToValue(ethpub.NewPStateObject(stateObject))
|
||
|
cb.Call(cb, val)
|
||
|
}
|
||
|
} else if storageObject, ok := object.Resource.(*ethchain.StorageState); ok {
|
||
11 years ago
|
for _, cb := range self.objectCb[ethutil.Bytes2Hex(storageObject.StateAddress)+ethutil.Bytes2Hex(storageObject.Address)] {
|
||
11 years ago
|
val, _ := self.vm.ToValue(ethpub.NewPStorageState(storageObject))
|
||
|
cb.Call(cb, val)
|
||
|
}
|
||
11 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
11 years ago
|
func (self *JSRE) initStdFuncs() {
|
||
|
t, _ := self.vm.Get("eth")
|
||
|
eth := t.Object()
|
||
11 years ago
|
eth.Set("watch", self.watch)
|
||
|
eth.Set("addPeer", self.addPeer)
|
||
11 years ago
|
eth.Set("require", self.require)
|
||
11 years ago
|
eth.Set("stopMining", self.stopMining)
|
||
|
eth.Set("startMining", self.startMining)
|
||
11 years ago
|
eth.Set("execBlock", self.execBlock)
|
||
11 years ago
|
}
|
||
|
|
||
|
/*
|
||
|
* The following methods are natively implemented javascript functions
|
||
|
*/
|
||
|
|
||
11 years ago
|
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
|
||
|
}
|
||
|
|
||
11 years ago
|
// eth.watch
|
||
11 years ago
|
func (self *JSRE) watch(call otto.FunctionCall) otto.Value {
|
||
11 years ago
|
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)
|
||
11 years ago
|
|
||
11 years ago
|
event := "storage:" + string(ethutil.Hex2Bytes(addr)) + ":" + string(ethutil.Hex2Bytes(storageAddr))
|
||
11 years ago
|
self.ethereum.Reactor().Subscribe(event, self.changeChan)
|
||
|
} else {
|
||
11 years ago
|
self.objectCb[addr] = append(self.objectCb[addr], cb)
|
||
11 years ago
|
|
||
11 years ago
|
event := "object:" + string(ethutil.Hex2Bytes(addr))
|
||
11 years ago
|
self.ethereum.Reactor().Subscribe(event, self.changeChan)
|
||
11 years ago
|
}
|
||
|
|
||
|
return otto.UndefinedValue()
|
||
|
}
|
||
11 years ago
|
|
||
11 years ago
|
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 {
|
||
11 years ago
|
return otto.UndefinedValue()
|
||
11 years ago
|
}
|
||
|
if err := self.Require(file); err != nil {
|
||
|
fmt.Println("err:", err)
|
||
|
return otto.UndefinedValue()
|
||
|
}
|
||
|
|
||
|
t, _ := self.vm.Get("exports")
|
||
11 years ago
|
|
||
11 years ago
|
return t
|
||
11 years ago
|
}
|
||
11 years ago
|
|
||
|
func (self *JSRE) execBlock(call otto.FunctionCall) otto.Value {
|
||
|
hash, err := call.Argument(0).ToString()
|
||
|
if err != nil {
|
||
|
return otto.UndefinedValue()
|
||
|
}
|
||
|
|
||
11 years ago
|
err = utils.BlockDo(self.ethereum, ethutil.Hex2Bytes(hash))
|
||
11 years ago
|
if err != nil {
|
||
|
fmt.Println(err)
|
||
|
return otto.FalseValue()
|
||
|
}
|
||
|
|
||
|
return otto.TrueValue()
|
||
|
}
|