bindings.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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/common"
  26. "github.com/ethereum/go-ethereum/state"
  27. )
  28. type plugin struct {
  29. Name string `json:"name"`
  30. Path string `json:"path"`
  31. }
  32. func (gui *Gui) Transact(from, recipient, value, gas, gasPrice, d string) (string, error) {
  33. var data string
  34. if len(recipient) == 0 {
  35. code, err := common.Compile(d, false)
  36. if err != nil {
  37. return "", err
  38. }
  39. data = common.Bytes2Hex(code)
  40. } else {
  41. data = common.Bytes2Hex(utils.FormatTransactionData(d))
  42. }
  43. return gui.xeth.Transact(from, recipient, value, gas, gasPrice, data)
  44. }
  45. func (self *Gui) AddPlugin(pluginPath string) {
  46. self.plugins[pluginPath] = plugin{Name: pluginPath, Path: pluginPath}
  47. json, _ := json.MarshalIndent(self.plugins, "", " ")
  48. common.WriteFile(self.eth.DataDir+"/plugins.json", json)
  49. }
  50. func (self *Gui) RemovePlugin(pluginPath string) {
  51. delete(self.plugins, pluginPath)
  52. json, _ := json.MarshalIndent(self.plugins, "", " ")
  53. common.WriteFile(self.eth.DataDir+"/plugins.json", json)
  54. }
  55. func (self *Gui) DumpState(hash, path string) {
  56. var stateDump []byte
  57. if len(hash) == 0 {
  58. stateDump = self.eth.ChainManager().State().Dump()
  59. } else {
  60. var block *types.Block
  61. if hash[0] == '#' {
  62. i, _ := strconv.Atoi(hash[1:])
  63. block = self.eth.ChainManager().GetBlockByNumber(uint64(i))
  64. } else {
  65. block = self.eth.ChainManager().GetBlock(common.Hex2Bytes(hash))
  66. }
  67. if block == nil {
  68. guilogger.Infof("block err: not found %s\n", hash)
  69. return
  70. }
  71. stateDump = state.New(block.Root(), self.eth.StateDb()).Dump()
  72. }
  73. file, err := os.OpenFile(path[7:], os.O_CREATE|os.O_RDWR, os.ModePerm)
  74. if err != nil {
  75. guilogger.Infoln("dump err: ", err)
  76. return
  77. }
  78. defer file.Close()
  79. guilogger.Infof("dumped state (%s) to %s\n", hash, path)
  80. file.Write(stateDump)
  81. }