github.com/jonasnick/go-ethereum@v0.7.12-0.20150216215225-22176f05d387/cmd/mist/bindings.go (about)

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