cmd.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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/ethutil"
  29. "github.com/ethereum/go-ethereum/logger"
  30. "github.com/ethereum/go-ethereum/rlp"
  31. "github.com/ethereum/go-ethereum/state"
  32. )
  33. var clilogger = logger.NewLogger("CLI")
  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. clilogger.Errorf("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 := ethutil.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. func exit(err error) {
  86. status := 0
  87. if err != nil {
  88. fmt.Fprintln(os.Stderr, "Fatal: ", err)
  89. status = 1
  90. }
  91. logger.Flush()
  92. os.Exit(status)
  93. }
  94. // Fatalf formats a message to standard output and exits the program.
  95. func Fatalf(format string, args ...interface{}) {
  96. fmt.Fprintf(os.Stderr, "Fatal: "+format+"\n", args...)
  97. logger.Flush()
  98. os.Exit(1)
  99. }
  100. func StartEthereum(ethereum *eth.Ethereum) {
  101. clilogger.Infoln("Starting ", ethereum.Name())
  102. if err := ethereum.Start(); err != nil {
  103. exit(err)
  104. }
  105. RegisterInterrupt(func(sig os.Signal) {
  106. ethereum.Stop()
  107. logger.Flush()
  108. })
  109. }
  110. func FormatTransactionData(data string) []byte {
  111. d := ethutil.StringToByteFunc(data, func(s string) (ret []byte) {
  112. slice := regexp.MustCompile("\\n|\\s").Split(s, 1000000000)
  113. for _, dataItem := range slice {
  114. d := ethutil.FormatData(dataItem)
  115. ret = append(ret, d...)
  116. }
  117. return
  118. })
  119. return d
  120. }
  121. // Replay block
  122. func BlockDo(ethereum *eth.Ethereum, hash []byte) error {
  123. block := ethereum.ChainManager().GetBlock(hash)
  124. if block == nil {
  125. return fmt.Errorf("unknown block %x", hash)
  126. }
  127. parent := ethereum.ChainManager().GetBlock(block.ParentHash())
  128. statedb := state.New(parent.Root(), ethereum.StateDb())
  129. _, err := ethereum.BlockProcessor().TransitionState(statedb, parent, block, true)
  130. if err != nil {
  131. return err
  132. }
  133. return nil
  134. }
  135. func ImportChain(chain *core.ChainManager, fn string) error {
  136. fmt.Printf("importing chain '%s'\n", fn)
  137. fh, err := os.OpenFile(fn, os.O_RDONLY, os.ModePerm)
  138. if err != nil {
  139. return err
  140. }
  141. defer fh.Close()
  142. var blocks types.Blocks
  143. if err := rlp.Decode(fh, &blocks); err != nil {
  144. return err
  145. }
  146. chain.Reset()
  147. if err := chain.InsertChain(blocks); err != nil {
  148. return err
  149. }
  150. fmt.Printf("imported %d blocks\n", len(blocks))
  151. return nil
  152. }