cmd.go 4.7 KB

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