bindings.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. "encoding/json"
  21. "os"
  22. "strconv"
  23. "github.com/ethereum/go-ethereum/cmd/utils"
  24. "github.com/ethereum/go-ethereum/core/types"
  25. "github.com/ethereum/go-ethereum/ethutil"
  26. "github.com/ethereum/go-ethereum/logger"
  27. "github.com/ethereum/go-ethereum/state"
  28. )
  29. type plugin struct {
  30. Name string `json:"name"`
  31. Path string `json:"path"`
  32. }
  33. // LogPrint writes to the GUI log.
  34. func (gui *Gui) LogPrint(level logger.LogLevel, msg string) {
  35. /*
  36. str := strings.TrimRight(s, "\n")
  37. lines := strings.Split(str, "\n")
  38. view := gui.getObjectByName("infoView")
  39. for _, line := range lines {
  40. view.Call("addLog", line)
  41. }
  42. */
  43. }
  44. func (gui *Gui) Transact(recipient, value, gas, gasPrice, d string) (string, error) {
  45. var data string
  46. if len(recipient) == 0 {
  47. code, err := ethutil.Compile(d, false)
  48. if err != nil {
  49. return "", err
  50. }
  51. data = ethutil.Bytes2Hex(code)
  52. } else {
  53. data = ethutil.Bytes2Hex(utils.FormatTransactionData(d))
  54. }
  55. return gui.xeth.Transact(recipient, value, gas, gasPrice, data)
  56. }
  57. // functions that allow Gui to implement interface guilogger.LogSystem
  58. func (gui *Gui) SetLogLevel(level logger.LogLevel) {
  59. gui.logLevel = level
  60. gui.eth.Logger().SetLogLevel(level)
  61. gui.config.Save("loglevel", level)
  62. }
  63. func (gui *Gui) GetLogLevel() logger.LogLevel {
  64. return gui.logLevel
  65. }
  66. func (self *Gui) AddPlugin(pluginPath string) {
  67. self.plugins[pluginPath] = plugin{Name: pluginPath, Path: pluginPath}
  68. json, _ := json.MarshalIndent(self.plugins, "", " ")
  69. ethutil.WriteFile(self.eth.DataDir+"/plugins.json", json)
  70. }
  71. func (self *Gui) RemovePlugin(pluginPath string) {
  72. delete(self.plugins, pluginPath)
  73. json, _ := json.MarshalIndent(self.plugins, "", " ")
  74. ethutil.WriteFile(self.eth.DataDir+"/plugins.json", json)
  75. }
  76. // this extra function needed to give int typecast value to gui widget
  77. // that sets initial loglevel to default
  78. func (gui *Gui) GetLogLevelInt() int {
  79. return int(gui.logLevel)
  80. }
  81. func (self *Gui) DumpState(hash, path string) {
  82. var stateDump []byte
  83. if len(hash) == 0 {
  84. stateDump = self.eth.ChainManager().State().Dump()
  85. } else {
  86. var block *types.Block
  87. if hash[0] == '#' {
  88. i, _ := strconv.Atoi(hash[1:])
  89. block = self.eth.ChainManager().GetBlockByNumber(uint64(i))
  90. } else {
  91. block = self.eth.ChainManager().GetBlock(ethutil.Hex2Bytes(hash))
  92. }
  93. if block == nil {
  94. guilogger.Infof("block err: not found %s\n", hash)
  95. return
  96. }
  97. stateDump = state.New(block.Root(), self.eth.Db()).Dump()
  98. }
  99. file, err := os.OpenFile(path[7:], os.O_CREATE|os.O_RDWR, os.ModePerm)
  100. if err != nil {
  101. guilogger.Infoln("dump err: ", err)
  102. return
  103. }
  104. defer file.Close()
  105. guilogger.Infof("dumped state (%s) to %s\n", hash, path)
  106. file.Write(stateDump)
  107. }