cmd.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. * Viktor Tron <viktor@ethdev.com>
  18. */
  19. package utils
  20. import (
  21. "fmt"
  22. "os"
  23. "os/signal"
  24. "regexp"
  25. "github.com/ethereum/go-ethereum/core"
  26. "github.com/ethereum/go-ethereum/core/types"
  27. "github.com/ethereum/go-ethereum/eth"
  28. "github.com/ethereum/go-ethereum/common"
  29. "github.com/ethereum/go-ethereum/logger"
  30. "github.com/ethereum/go-ethereum/rlp"
  31. )
  32. var clilogger = logger.NewLogger("CLI")
  33. var interruptCallbacks = []func(os.Signal){}
  34. // Register interrupt handlers callbacks
  35. func RegisterInterrupt(cb func(os.Signal)) {
  36. interruptCallbacks = append(interruptCallbacks, cb)
  37. }
  38. // go routine that call interrupt handlers in order of registering
  39. func HandleInterrupt() {
  40. c := make(chan os.Signal, 1)
  41. go func() {
  42. signal.Notify(c, os.Interrupt)
  43. for sig := range c {
  44. clilogger.Errorf("Shutting down (%v) ... \n", sig)
  45. RunInterruptCallbacks(sig)
  46. }
  47. }()
  48. }
  49. func RunInterruptCallbacks(sig os.Signal) {
  50. for _, cb := range interruptCallbacks {
  51. cb(sig)
  52. }
  53. }
  54. func openLogFile(Datadir string, filename string) *os.File {
  55. path := common.AbsolutePath(Datadir, filename)
  56. file, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
  57. if err != nil {
  58. panic(fmt.Sprintf("error opening log file '%s': %v", filename, err))
  59. }
  60. return file
  61. }
  62. func confirm(message string) bool {
  63. fmt.Println(message, "Are you sure? (y/n)")
  64. var r string
  65. fmt.Scanln(&r)
  66. for ; ; fmt.Scanln(&r) {
  67. if r == "n" || r == "y" {
  68. break
  69. } else {
  70. fmt.Printf("Yes or no? (%s)", r)
  71. }
  72. }
  73. return r == "y"
  74. }
  75. func initDataDir(Datadir string) {
  76. _, err := os.Stat(Datadir)
  77. if err != nil {
  78. if os.IsNotExist(err) {
  79. fmt.Printf("Data directory '%s' doesn't exist, creating it\n", Datadir)
  80. os.Mkdir(Datadir, 0777)
  81. }
  82. }
  83. }
  84. func exit(err error) {
  85. status := 0
  86. if err != nil {
  87. fmt.Fprintln(os.Stderr, "Fatal:", err)
  88. status = 1
  89. }
  90. logger.Flush()
  91. os.Exit(status)
  92. }
  93. // Fatalf formats a message to standard output and exits the program.
  94. func Fatalf(format string, args ...interface{}) {
  95. fmt.Fprintf(os.Stderr, "Fatal: "+format+"\n", args...)
  96. logger.Flush()
  97. os.Exit(1)
  98. }
  99. func StartEthereum(ethereum *eth.Ethereum) {
  100. clilogger.Infoln("Starting ", ethereum.Name())
  101. if err := ethereum.Start(); err != nil {
  102. exit(err)
  103. }
  104. RegisterInterrupt(func(sig os.Signal) {
  105. ethereum.Stop()
  106. logger.Flush()
  107. })
  108. }
  109. func StartEthereumForTest(ethereum *eth.Ethereum) {
  110. clilogger.Infoln("Starting ", ethereum.Name())
  111. ethereum.StartForTest()
  112. RegisterInterrupt(func(sig os.Signal) {
  113. ethereum.Stop()
  114. logger.Flush()
  115. })
  116. }
  117. func FormatTransactionData(data string) []byte {
  118. d := common.StringToByteFunc(data, func(s string) (ret []byte) {
  119. slice := regexp.MustCompile("\\n|\\s").Split(s, 1000000000)
  120. for _, dataItem := range slice {
  121. d := common.FormatData(dataItem)
  122. ret = append(ret, d...)
  123. }
  124. return
  125. })
  126. return d
  127. }
  128. func ImportChain(chainmgr *core.ChainManager, fn string) error {
  129. fmt.Printf("importing blockchain '%s'\n", fn)
  130. fh, err := os.OpenFile(fn, os.O_RDONLY, os.ModePerm)
  131. if err != nil {
  132. return err
  133. }
  134. defer fh.Close()
  135. var blocks types.Blocks
  136. if err := rlp.Decode(fh, &blocks); err != nil {
  137. return err
  138. }
  139. chainmgr.Reset()
  140. if err := chainmgr.InsertChain(blocks); err != nil {
  141. return err
  142. }
  143. fmt.Printf("imported %d blocks\n", len(blocks))
  144. return nil
  145. }
  146. func ExportChain(chainmgr *core.ChainManager, fn string) error {
  147. fmt.Printf("exporting blockchain '%s'\n", fn)
  148. data := chainmgr.Export()
  149. if err := common.WriteFile(fn, data); err != nil {
  150. return err
  151. }
  152. fmt.Printf("exported blockchain\n")
  153. return nil
  154. }