bindings.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Copyright (c) 2013-2014, Jeffrey Wilcke. All rights reserved.
  2. //
  3. // This library is free software; you can redistribute it and/or
  4. // modify it under the terms of the GNU General Public
  5. // License as published by the Free Software Foundation; either
  6. // version 2.1 of the License, or (at your option) any later version.
  7. //
  8. // This library is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. // General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this library; if not, write to the Free Software
  15. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  16. // MA 02110-1301 USA
  17. package main
  18. import (
  19. "encoding/json"
  20. "os"
  21. "strconv"
  22. "github.com/ethereum/go-ethereum/cmd/utils"
  23. "github.com/ethereum/go-ethereum/core/types"
  24. "github.com/ethereum/go-ethereum/ethutil"
  25. "github.com/ethereum/go-ethereum/logger"
  26. )
  27. type plugin struct {
  28. Name string `json:"name"`
  29. Path string `json:"path"`
  30. }
  31. // LogPrint writes to the GUI log.
  32. func (gui *Gui) LogPrint(level logger.LogLevel, msg string) {
  33. /*
  34. str := strings.TrimRight(s, "\n")
  35. lines := strings.Split(str, "\n")
  36. view := gui.getObjectByName("infoView")
  37. for _, line := range lines {
  38. view.Call("addLog", line)
  39. }
  40. */
  41. }
  42. func (gui *Gui) Transact(recipient, value, gas, gasPrice, d string) (string, error) {
  43. var data string
  44. if len(recipient) == 0 {
  45. code, err := ethutil.Compile(d, false)
  46. if err != nil {
  47. return "", err
  48. }
  49. data = ethutil.Bytes2Hex(code)
  50. } else {
  51. data = ethutil.Bytes2Hex(utils.FormatTransactionData(d))
  52. }
  53. return gui.pipe.Transact(gui.privateKey(), recipient, value, gas, gasPrice, data)
  54. }
  55. func (gui *Gui) SetCustomIdentifier(customIdentifier string) {
  56. gui.clientIdentity.SetCustomIdentifier(customIdentifier)
  57. gui.config.Save("id", customIdentifier)
  58. }
  59. func (gui *Gui) GetCustomIdentifier() string {
  60. return gui.clientIdentity.GetCustomIdentifier()
  61. }
  62. // functions that allow Gui to implement interface guilogger.LogSystem
  63. func (gui *Gui) SetLogLevel(level logger.LogLevel) {
  64. gui.logLevel = level
  65. gui.stdLog.SetLogLevel(level)
  66. gui.config.Save("loglevel", level)
  67. }
  68. func (gui *Gui) GetLogLevel() logger.LogLevel {
  69. return gui.logLevel
  70. }
  71. func (self *Gui) AddPlugin(pluginPath string) {
  72. self.plugins[pluginPath] = plugin{Name: pluginPath, Path: pluginPath}
  73. json, _ := json.MarshalIndent(self.plugins, "", " ")
  74. ethutil.WriteFile(ethutil.Config.ExecPath+"/plugins.json", json)
  75. }
  76. func (self *Gui) RemovePlugin(pluginPath string) {
  77. delete(self.plugins, pluginPath)
  78. json, _ := json.MarshalIndent(self.plugins, "", " ")
  79. ethutil.WriteFile(ethutil.Config.ExecPath+"/plugins.json", json)
  80. }
  81. // this extra function needed to give int typecast value to gui widget
  82. // that sets initial loglevel to default
  83. func (gui *Gui) GetLogLevelInt() int {
  84. return int(gui.logLevel)
  85. }
  86. func (self *Gui) DumpState(hash, path string) {
  87. var stateDump []byte
  88. if len(hash) == 0 {
  89. stateDump = self.eth.BlockManager().CurrentState().Dump()
  90. } else {
  91. var block *types.Block
  92. if hash[0] == '#' {
  93. i, _ := strconv.Atoi(hash[1:])
  94. block = self.eth.ChainManager().GetBlockByNumber(uint64(i))
  95. } else {
  96. block = self.eth.ChainManager().GetBlock(ethutil.Hex2Bytes(hash))
  97. }
  98. if block == nil {
  99. guilogger.Infof("block err: not found %s\n", hash)
  100. return
  101. }
  102. stateDump = block.State().Dump()
  103. }
  104. file, err := os.OpenFile(path[7:], os.O_CREATE|os.O_RDWR, os.ModePerm)
  105. if err != nil {
  106. guilogger.Infoln("dump err: ", err)
  107. return
  108. }
  109. defer file.Close()
  110. guilogger.Infof("dumped state (%s) to %s\n", hash, path)
  111. file.Write(stateDump)
  112. }