ui_lib.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /*
  2. This file is part of go-ethereum
  3. go-ethereum is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. go-ethereum is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /**
  15. * @authors
  16. * Jeffrey Wilcke <i@jev.io>
  17. */
  18. package main
  19. import (
  20. "fmt"
  21. "io/ioutil"
  22. "path"
  23. "github.com/ethereum/go-ethereum/core/types"
  24. "github.com/ethereum/go-ethereum/eth"
  25. "github.com/ethereum/go-ethereum/ethutil"
  26. "github.com/ethereum/go-ethereum/event/filter"
  27. "github.com/ethereum/go-ethereum/javascript"
  28. "github.com/ethereum/go-ethereum/xeth"
  29. "github.com/obscuren/qml"
  30. )
  31. type memAddr struct {
  32. Num string
  33. Value string
  34. }
  35. // UI Library that has some basic functionality exposed
  36. type UiLib struct {
  37. *xeth.XEth
  38. engine *qml.Engine
  39. eth *eth.Ethereum
  40. connected bool
  41. assetPath string
  42. // The main application window
  43. win *qml.Window
  44. Db *Debugger
  45. DbWindow *DebuggerWindow
  46. jsEngine *javascript.JSRE
  47. filterCallbacks map[int][]int
  48. filterManager *filter.FilterManager
  49. }
  50. func NewUiLib(engine *qml.Engine, eth *eth.Ethereum, assetPath string) *UiLib {
  51. x := xeth.New(eth, nil)
  52. lib := &UiLib{XEth: x, engine: engine, eth: eth, assetPath: assetPath, jsEngine: javascript.NewJSRE(x), filterCallbacks: make(map[int][]int)} //, filters: make(map[int]*xeth.JSFilter)}
  53. lib.filterManager = filter.NewFilterManager(eth.EventMux())
  54. go lib.filterManager.Start()
  55. return lib
  56. }
  57. func (self *UiLib) Notef(args []interface{}) {
  58. guilogger.Infoln(args...)
  59. }
  60. func (self *UiLib) ImportTx(rlpTx string) {
  61. tx := types.NewTransactionFromBytes(ethutil.Hex2Bytes(rlpTx))
  62. err := self.eth.TxPool().Add(tx)
  63. if err != nil {
  64. guilogger.Infoln("import tx failed ", err)
  65. }
  66. }
  67. func (self *UiLib) EvalJavascriptFile(path string) {
  68. self.jsEngine.LoadExtFile(path[7:])
  69. }
  70. func (self *UiLib) EvalJavascriptString(str string) string {
  71. value, err := self.jsEngine.Run(str)
  72. if err != nil {
  73. return err.Error()
  74. }
  75. return fmt.Sprintf("%v", value)
  76. }
  77. func (ui *UiLib) Muted(content string) {
  78. component, err := ui.engine.LoadFile(ui.AssetPath("qml/muted.qml"))
  79. if err != nil {
  80. guilogger.Debugln(err)
  81. return
  82. }
  83. win := component.CreateWindow(nil)
  84. go func() {
  85. path := "file://" + ui.AssetPath("muted/index.html")
  86. win.Set("url", path)
  87. win.Show()
  88. win.Wait()
  89. }()
  90. }
  91. func (ui *UiLib) Connect(button qml.Object) {
  92. if !ui.connected {
  93. ui.eth.Start()
  94. ui.connected = true
  95. button.Set("enabled", false)
  96. }
  97. }
  98. func (ui *UiLib) ConnectToPeer(nodeURL string) {
  99. if err := ui.eth.SuggestPeer(nodeURL); err != nil {
  100. guilogger.Infoln("SuggestPeer error: " + err.Error())
  101. }
  102. }
  103. func (ui *UiLib) AssetPath(p string) string {
  104. return path.Join(ui.assetPath, p)
  105. }
  106. func (self *UiLib) StartDbWithContractAndData(contractHash, data string) {
  107. dbWindow := NewDebuggerWindow(self)
  108. object := self.eth.ChainManager().State().GetStateObject(ethutil.Hex2Bytes(contractHash))
  109. if len(object.Code()) > 0 {
  110. dbWindow.SetCode(ethutil.Bytes2Hex(object.Code()))
  111. }
  112. dbWindow.SetData(data)
  113. dbWindow.Show()
  114. }
  115. func (self *UiLib) StartDbWithCode(code string) {
  116. dbWindow := NewDebuggerWindow(self)
  117. dbWindow.SetCode(code)
  118. dbWindow.Show()
  119. }
  120. func (self *UiLib) StartDebugger() {
  121. dbWindow := NewDebuggerWindow(self)
  122. dbWindow.Show()
  123. }
  124. func (self *UiLib) Transact(params map[string]interface{}) (string, error) {
  125. object := mapToTxParams(params)
  126. return self.XEth.Transact(
  127. object["to"],
  128. object["value"],
  129. object["gas"],
  130. object["gasPrice"],
  131. object["data"],
  132. )
  133. }
  134. func (self *UiLib) Compile(code string) (string, error) {
  135. bcode, err := ethutil.Compile(code, false)
  136. if err != nil {
  137. return err.Error(), err
  138. }
  139. return ethutil.Bytes2Hex(bcode), err
  140. }
  141. func (self *UiLib) Call(params map[string]interface{}) (string, error) {
  142. object := mapToTxParams(params)
  143. return self.XEth.Execute(
  144. object["to"],
  145. object["value"],
  146. object["gas"],
  147. object["gasPrice"],
  148. object["data"],
  149. )
  150. }
  151. func (self *UiLib) AddLocalTransaction(to, data, gas, gasPrice, value string) int {
  152. return 0
  153. /*
  154. return self.miner.AddLocalTx(&miner.LocalTx{
  155. To: ethutil.Hex2Bytes(to),
  156. Data: ethutil.Hex2Bytes(data),
  157. Gas: gas,
  158. GasPrice: gasPrice,
  159. Value: value,
  160. }) - 1
  161. */
  162. }
  163. func (self *UiLib) RemoveLocalTransaction(id int) {
  164. //self.miner.RemoveLocalTx(id)
  165. }
  166. func (self *UiLib) SetGasPrice(price string) {
  167. self.Miner().MinAcceptedGasPrice = ethutil.Big(price)
  168. }
  169. func (self *UiLib) SetExtra(extra string) {
  170. self.Miner().Extra = extra
  171. }
  172. func (self *UiLib) ToggleMining() bool {
  173. if !self.Miner().Mining() {
  174. self.Miner().Start()
  175. return true
  176. } else {
  177. self.Miner().Stop()
  178. return false
  179. }
  180. }
  181. func (self *UiLib) ToHex(data string) string {
  182. return "0x" + ethutil.Bytes2Hex([]byte(data))
  183. }
  184. func (self *UiLib) ToAscii(data string) string {
  185. start := 0
  186. if len(data) > 1 && data[0:2] == "0x" {
  187. start = 2
  188. }
  189. return string(ethutil.Hex2Bytes(data[start:]))
  190. }
  191. /// Ethereum filter methods
  192. func (self *UiLib) NewFilter(object map[string]interface{}, view *qml.Common) (id int) {
  193. /* TODO remove me
  194. filter := qt.NewFilterFromMap(object, self.eth)
  195. filter.MessageCallback = func(messages state.Messages) {
  196. view.Call("messages", xeth.ToMessages(messages), id)
  197. }
  198. id = self.filterManager.InstallFilter(filter)
  199. return id
  200. */
  201. return 0
  202. }
  203. func (self *UiLib) NewFilterString(typ string, view *qml.Common) (id int) {
  204. /* TODO remove me
  205. filter := core.NewFilter(self.eth)
  206. filter.BlockCallback = func(block *types.Block) {
  207. view.Call("messages", "{}", id)
  208. }
  209. id = self.filterManager.InstallFilter(filter)
  210. return id
  211. */
  212. return 0
  213. }
  214. func (self *UiLib) Messages(id int) *ethutil.List {
  215. /* TODO remove me
  216. filter := self.filterManager.GetFilter(id)
  217. if filter != nil {
  218. messages := xeth.ToMessages(filter.Find())
  219. return messages
  220. }
  221. */
  222. return ethutil.EmptyList()
  223. }
  224. func (self *UiLib) ReadFile(p string) string {
  225. content, err := ioutil.ReadFile(self.AssetPath(path.Join("ext", p)))
  226. if err != nil {
  227. guilogger.Infoln("error reading file", p, ":", err)
  228. }
  229. return string(content)
  230. }
  231. func (self *UiLib) UninstallFilter(id int) {
  232. self.filterManager.UninstallFilter(id)
  233. }
  234. func mapToTxParams(object map[string]interface{}) map[string]string {
  235. // Default values
  236. if object["from"] == nil {
  237. object["from"] = ""
  238. }
  239. if object["to"] == nil {
  240. object["to"] = ""
  241. }
  242. if object["value"] == nil {
  243. object["value"] = ""
  244. }
  245. if object["gas"] == nil {
  246. object["gas"] = ""
  247. }
  248. if object["gasPrice"] == nil {
  249. object["gasPrice"] = ""
  250. }
  251. var dataStr string
  252. var data []string
  253. if list, ok := object["data"].(*qml.List); ok {
  254. list.Convert(&data)
  255. } else if str, ok := object["data"].(string); ok {
  256. data = []string{str}
  257. }
  258. for _, str := range data {
  259. if ethutil.IsHex(str) {
  260. str = str[2:]
  261. if len(str) != 64 {
  262. str = ethutil.LeftPadString(str, 64)
  263. }
  264. } else {
  265. str = ethutil.Bytes2Hex(ethutil.LeftPadBytes(ethutil.Big(str).Bytes(), 32))
  266. }
  267. dataStr += str
  268. }
  269. object["data"] = dataStr
  270. conv := make(map[string]string)
  271. for key, value := range object {
  272. if v, ok := value.(string); ok {
  273. conv[key] = v
  274. }
  275. }
  276. return conv
  277. }