javascript_runtime.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. package javascript
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os"
  6. "path"
  7. "path/filepath"
  8. "github.com/ethereum/go-ethereum"
  9. "github.com/ethereum/go-ethereum/chain"
  10. "github.com/ethereum/go-ethereum/cmd/utils"
  11. "github.com/ethereum/go-ethereum/ethpipe"
  12. "github.com/ethereum/go-ethereum/ethstate"
  13. "github.com/ethereum/go-ethereum/ethutil"
  14. "github.com/ethereum/go-ethereum/event"
  15. "github.com/ethereum/go-ethereum/logger"
  16. "github.com/obscuren/otto"
  17. )
  18. var jsrelogger = logger.NewLogger("JSRE")
  19. type JSRE struct {
  20. ethereum *eth.Ethereum
  21. Vm *otto.Otto
  22. pipe *ethpipe.JSPipe
  23. events event.Subscription
  24. objectCb map[string][]otto.Value
  25. }
  26. func (jsre *JSRE) LoadExtFile(path string) {
  27. result, err := ioutil.ReadFile(path)
  28. if err == nil {
  29. jsre.Vm.Run(result)
  30. } else {
  31. jsrelogger.Infoln("Could not load file:", path)
  32. }
  33. }
  34. func (jsre *JSRE) LoadIntFile(file string) {
  35. assetPath := path.Join(os.Getenv("GOPATH"), "src", "github.com", "ethereum", "go-ethereum", "cmd", "mist", "assets", "ext")
  36. jsre.LoadExtFile(path.Join(assetPath, file))
  37. }
  38. func NewJSRE(ethereum *eth.Ethereum) *JSRE {
  39. re := &JSRE{
  40. ethereum,
  41. otto.New(),
  42. ethpipe.NewJSPipe(ethereum),
  43. nil,
  44. make(map[string][]otto.Value),
  45. }
  46. // Init the JS lib
  47. re.Vm.Run(jsLib)
  48. // Load extra javascript files
  49. re.LoadIntFile("string.js")
  50. re.LoadIntFile("big.js")
  51. // Subscribe to events
  52. mux := ethereum.EventMux()
  53. re.events = mux.Subscribe(chain.NewBlockEvent{})
  54. // We have to make sure that, whoever calls this, calls "Stop"
  55. go re.mainLoop()
  56. re.Bind("eth", &JSEthereum{re.pipe, re.Vm, ethereum})
  57. re.initStdFuncs()
  58. jsrelogger.Infoln("started")
  59. return re
  60. }
  61. func (self *JSRE) Bind(name string, v interface{}) {
  62. self.Vm.Set(name, v)
  63. }
  64. func (self *JSRE) Run(code string) (otto.Value, error) {
  65. return self.Vm.Run(code)
  66. }
  67. func (self *JSRE) Require(file string) error {
  68. if len(filepath.Ext(file)) == 0 {
  69. file += ".js"
  70. }
  71. fh, err := os.Open(file)
  72. if err != nil {
  73. return err
  74. }
  75. content, _ := ioutil.ReadAll(fh)
  76. self.Run("exports = {};(function() {" + string(content) + "})();")
  77. return nil
  78. }
  79. func (self *JSRE) Stop() {
  80. self.events.Unsubscribe()
  81. jsrelogger.Infoln("stopped")
  82. }
  83. func (self *JSRE) mainLoop() {
  84. for _ = range self.events.Chan() {
  85. }
  86. }
  87. func (self *JSRE) initStdFuncs() {
  88. t, _ := self.Vm.Get("eth")
  89. eth := t.Object()
  90. eth.Set("watch", self.watch)
  91. eth.Set("addPeer", self.addPeer)
  92. eth.Set("require", self.require)
  93. eth.Set("stopMining", self.stopMining)
  94. eth.Set("startMining", self.startMining)
  95. eth.Set("execBlock", self.execBlock)
  96. eth.Set("dump", self.dump)
  97. }
  98. /*
  99. * The following methods are natively implemented javascript functions
  100. */
  101. func (self *JSRE) dump(call otto.FunctionCall) otto.Value {
  102. var state *ethstate.State
  103. if len(call.ArgumentList) > 0 {
  104. var block *chain.Block
  105. if call.Argument(0).IsNumber() {
  106. num, _ := call.Argument(0).ToInteger()
  107. block = self.ethereum.ChainManager().GetBlockByNumber(uint64(num))
  108. } else if call.Argument(0).IsString() {
  109. hash, _ := call.Argument(0).ToString()
  110. block = self.ethereum.ChainManager().GetBlock(ethutil.Hex2Bytes(hash))
  111. } else {
  112. fmt.Println("invalid argument for dump. Either hex string or number")
  113. }
  114. if block == nil {
  115. fmt.Println("block not found")
  116. return otto.UndefinedValue()
  117. }
  118. state = block.State()
  119. } else {
  120. state = self.ethereum.StateManager().CurrentState()
  121. }
  122. v, _ := self.Vm.ToValue(state.Dump())
  123. return v
  124. }
  125. func (self *JSRE) stopMining(call otto.FunctionCall) otto.Value {
  126. v, _ := self.Vm.ToValue(utils.StopMining(self.ethereum))
  127. return v
  128. }
  129. func (self *JSRE) startMining(call otto.FunctionCall) otto.Value {
  130. v, _ := self.Vm.ToValue(utils.StartMining(self.ethereum))
  131. return v
  132. }
  133. // eth.watch
  134. func (self *JSRE) watch(call otto.FunctionCall) otto.Value {
  135. addr, _ := call.Argument(0).ToString()
  136. var storageAddr string
  137. var cb otto.Value
  138. var storageCallback bool
  139. if len(call.ArgumentList) > 2 {
  140. storageCallback = true
  141. storageAddr, _ = call.Argument(1).ToString()
  142. cb = call.Argument(2)
  143. } else {
  144. cb = call.Argument(1)
  145. }
  146. if storageCallback {
  147. self.objectCb[addr+storageAddr] = append(self.objectCb[addr+storageAddr], cb)
  148. // event := "storage:" + string(ethutil.Hex2Bytes(addr)) + ":" + string(ethutil.Hex2Bytes(storageAddr))
  149. // self.ethereum.EventMux().Subscribe(event, self.changeChan)
  150. } else {
  151. self.objectCb[addr] = append(self.objectCb[addr], cb)
  152. // event := "object:" + string(ethutil.Hex2Bytes(addr))
  153. // self.ethereum.EventMux().Subscribe(event, self.changeChan)
  154. }
  155. return otto.UndefinedValue()
  156. }
  157. func (self *JSRE) addPeer(call otto.FunctionCall) otto.Value {
  158. host, err := call.Argument(0).ToString()
  159. if err != nil {
  160. return otto.FalseValue()
  161. }
  162. self.ethereum.ConnectToPeer(host)
  163. return otto.TrueValue()
  164. }
  165. func (self *JSRE) require(call otto.FunctionCall) otto.Value {
  166. file, err := call.Argument(0).ToString()
  167. if err != nil {
  168. return otto.UndefinedValue()
  169. }
  170. if err := self.Require(file); err != nil {
  171. fmt.Println("err:", err)
  172. return otto.UndefinedValue()
  173. }
  174. t, _ := self.Vm.Get("exports")
  175. return t
  176. }
  177. func (self *JSRE) execBlock(call otto.FunctionCall) otto.Value {
  178. hash, err := call.Argument(0).ToString()
  179. if err != nil {
  180. return otto.UndefinedValue()
  181. }
  182. err = utils.BlockDo(self.ethereum, ethutil.Hex2Bytes(hash))
  183. if err != nil {
  184. fmt.Println(err)
  185. return otto.FalseValue()
  186. }
  187. return otto.TrueValue()
  188. }