You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

193 lines
4.1 KiB

9 years ago
8 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
7 years ago
7 years ago
9 years ago
9 years ago
9 years ago
8 years ago
8 years ago
9 years ago
9 years ago
9 years ago
[common] use temp intead of {filePath}.new The problem with {filePath}.new is that it is not safe for concurrent use! Calling this function with the same params results in the following error: ``` panic: Panicked on a Crisis: rename /root/.tendermint_test/consensus_replay_test/priv_validator.json.new /root/.tendermint_test/consensus_replay_test/priv_validator.json: no such file or directory goroutine 47860 [running]: github.com/tendermint/tendermint/vendor/github.com/tendermint/tmlibs/common.PanicCrisis(0xcba800, 0xc42152d640) /go/src/github.com/tendermint/tendermint/vendor/github.com/tendermint/tmlibs/common/errors.go:33 +0x10f github.com/tendermint/tendermint/types.(*PrivValidator).save(0xc42235f2c0) /go/src/github.com/tendermint/tendermint/types/priv_validator.go:165 +0x159 github.com/tendermint/tendermint/types.(*PrivValidator).signBytesHRS(0xc42235f2c0, 0x6, 0x0, 0xc424e88f03, 0xc429908580, 0xca, 0x155, 0x80, 0xc424e88f00, 0x7f4ecafc88d0, ...) /go/src/github.com/tendermint/tendermint/types/priv_validator.go:249 +0x2bb github.com/tendermint/tendermint/types.(*PrivValidator).SignVote(0xc42235f2c0, 0xc4228c7460, 0xf, 0xc424e88f00, 0x0, 0x0) /go/src/github.com/tendermint/tendermint/types/priv_validator.go:186 +0x1a2 github.com/tendermint/tendermint/consensus.(*ConsensusState).signVote(0xc424efd520, 0xc400000002, 0xc422d5e3c0, 0x14, 0x20, 0x1, 0xc4247b6560, 0x14, 0x20, 0x0, ...) github.com/tendermint/tendermint/consensus/_test/_obj_test/state.go:1556 +0x35e github.com/tendermint/tendermint/consensus.(*ConsensusState).signAddVote(0xc424efd520, 0x2, 0xc422d5e3c0, 0x14, 0x20, 0x1, 0xc4247b6560, 0x14, 0x20, 0xc42001b300) github.com/tendermint/tendermint/consensus/_test/_obj_test/state.go:1568 +0x200 github.com/tendermint/tendermint/consensus.(*ConsensusState).enterPrecommit(0xc424efd520, 0x6, 0x0) github.com/tendermint/tendermint/consensus/_test/_obj_test/state.go:1082 +0x13a4 github.com/tendermint/tendermint/consensus.(*ConsensusState).addVote(0xc424efd520, 0xc424e88780, 0x0, 0x0, 0x39, 0x1dc, 0x28c) github.com/tendermint/tendermint/consensus/_test/_obj_test/state.go:1477 +0x1be5 github.com/tendermint/tendermint/consensus.(*ConsensusState).tryAddVote(0xc424efd520, 0xc424e88780, 0x0, 0x0, 0xd7fb00, 0xc42152ce00) github.com/tendermint/tendermint/consensus/_test/_obj_test/state.go:1382 +0x93 github.com/tendermint/tendermint/consensus.(*ConsensusState).handleMsg(0xc424efd520, 0xcb58e0, 0xc42547feb8, 0x0, 0x0, 0x6, 0x0, 0x4, 0xed10ca07e, 0x3077bfea, ...) github.com/tendermint/tendermint/consensus/_test/_obj_test/state.go:660 +0x9fc github.com/tendermint/tendermint/consensus.(*ConsensusState).receiveRoutine(0xc424efd520, 0x0) github.com/tendermint/tendermint/consensus/_test/_obj_test/state.go:615 +0x5f5 created by github.com/tendermint/tendermint/consensus.(*ConsensusState).OnStart github.com/tendermint/tendermint/consensus/_test/_obj_test/state.go:332 +0x4a7 exit status 2 FAIL github.com/tendermint/tendermint/consensus 76.644s make: *** [test_integrations] Error 1 ``` See https://github.com/tendermint/tendermint/pull/568
7 years ago
9 years ago
[common] use temp intead of {filePath}.new The problem with {filePath}.new is that it is not safe for concurrent use! Calling this function with the same params results in the following error: ``` panic: Panicked on a Crisis: rename /root/.tendermint_test/consensus_replay_test/priv_validator.json.new /root/.tendermint_test/consensus_replay_test/priv_validator.json: no such file or directory goroutine 47860 [running]: github.com/tendermint/tendermint/vendor/github.com/tendermint/tmlibs/common.PanicCrisis(0xcba800, 0xc42152d640) /go/src/github.com/tendermint/tendermint/vendor/github.com/tendermint/tmlibs/common/errors.go:33 +0x10f github.com/tendermint/tendermint/types.(*PrivValidator).save(0xc42235f2c0) /go/src/github.com/tendermint/tendermint/types/priv_validator.go:165 +0x159 github.com/tendermint/tendermint/types.(*PrivValidator).signBytesHRS(0xc42235f2c0, 0x6, 0x0, 0xc424e88f03, 0xc429908580, 0xca, 0x155, 0x80, 0xc424e88f00, 0x7f4ecafc88d0, ...) /go/src/github.com/tendermint/tendermint/types/priv_validator.go:249 +0x2bb github.com/tendermint/tendermint/types.(*PrivValidator).SignVote(0xc42235f2c0, 0xc4228c7460, 0xf, 0xc424e88f00, 0x0, 0x0) /go/src/github.com/tendermint/tendermint/types/priv_validator.go:186 +0x1a2 github.com/tendermint/tendermint/consensus.(*ConsensusState).signVote(0xc424efd520, 0xc400000002, 0xc422d5e3c0, 0x14, 0x20, 0x1, 0xc4247b6560, 0x14, 0x20, 0x0, ...) github.com/tendermint/tendermint/consensus/_test/_obj_test/state.go:1556 +0x35e github.com/tendermint/tendermint/consensus.(*ConsensusState).signAddVote(0xc424efd520, 0x2, 0xc422d5e3c0, 0x14, 0x20, 0x1, 0xc4247b6560, 0x14, 0x20, 0xc42001b300) github.com/tendermint/tendermint/consensus/_test/_obj_test/state.go:1568 +0x200 github.com/tendermint/tendermint/consensus.(*ConsensusState).enterPrecommit(0xc424efd520, 0x6, 0x0) github.com/tendermint/tendermint/consensus/_test/_obj_test/state.go:1082 +0x13a4 github.com/tendermint/tendermint/consensus.(*ConsensusState).addVote(0xc424efd520, 0xc424e88780, 0x0, 0x0, 0x39, 0x1dc, 0x28c) github.com/tendermint/tendermint/consensus/_test/_obj_test/state.go:1477 +0x1be5 github.com/tendermint/tendermint/consensus.(*ConsensusState).tryAddVote(0xc424efd520, 0xc424e88780, 0x0, 0x0, 0xd7fb00, 0xc42152ce00) github.com/tendermint/tendermint/consensus/_test/_obj_test/state.go:1382 +0x93 github.com/tendermint/tendermint/consensus.(*ConsensusState).handleMsg(0xc424efd520, 0xcb58e0, 0xc42547feb8, 0x0, 0x0, 0x6, 0x0, 0x4, 0xed10ca07e, 0x3077bfea, ...) github.com/tendermint/tendermint/consensus/_test/_obj_test/state.go:660 +0x9fc github.com/tendermint/tendermint/consensus.(*ConsensusState).receiveRoutine(0xc424efd520, 0x0) github.com/tendermint/tendermint/consensus/_test/_obj_test/state.go:615 +0x5f5 created by github.com/tendermint/tendermint/consensus.(*ConsensusState).OnStart github.com/tendermint/tendermint/consensus/_test/_obj_test/state.go:332 +0x4a7 exit status 2 FAIL github.com/tendermint/tendermint/consensus 76.644s make: *** [test_integrations] Error 1 ``` See https://github.com/tendermint/tendermint/pull/568
7 years ago
9 years ago
8 years ago
9 years ago
  1. package common
  2. import (
  3. "bufio"
  4. "fmt"
  5. "io"
  6. "io/ioutil"
  7. "os"
  8. "os/exec"
  9. "os/signal"
  10. "path/filepath"
  11. "strings"
  12. "syscall"
  13. )
  14. var gopath string
  15. // GoPath returns GOPATH env variable value. If it is not set, this function
  16. // will try to call `go env GOPATH` subcommand.
  17. func GoPath() string {
  18. if gopath != "" {
  19. return gopath
  20. }
  21. path := os.Getenv("GOPATH")
  22. if len(path) == 0 {
  23. goCmd := exec.Command("go", "env", "GOPATH")
  24. out, err := goCmd.Output()
  25. if err != nil {
  26. panic(fmt.Sprintf("failed to determine gopath: %v", err))
  27. }
  28. path = string(out)
  29. }
  30. gopath = path
  31. return path
  32. }
  33. // TrapSignal catches the SIGTERM and executes cb function. After that it exits
  34. // with code 1.
  35. func TrapSignal(cb func()) {
  36. c := make(chan os.Signal, 1)
  37. signal.Notify(c, os.Interrupt, syscall.SIGTERM)
  38. go func() {
  39. for sig := range c {
  40. fmt.Printf("captured %v, exiting...\n", sig)
  41. if cb != nil {
  42. cb()
  43. }
  44. os.Exit(1)
  45. }
  46. }()
  47. select {}
  48. }
  49. // Kill the running process by sending itself SIGTERM.
  50. func Kill() error {
  51. p, err := os.FindProcess(os.Getpid())
  52. if err != nil {
  53. return err
  54. }
  55. return p.Signal(syscall.SIGTERM)
  56. }
  57. func Exit(s string) {
  58. fmt.Printf(s + "\n")
  59. os.Exit(1)
  60. }
  61. func EnsureDir(dir string, mode os.FileMode) error {
  62. if _, err := os.Stat(dir); os.IsNotExist(err) {
  63. err := os.MkdirAll(dir, mode)
  64. if err != nil {
  65. return fmt.Errorf("Could not create directory %v. %v", dir, err)
  66. }
  67. }
  68. return nil
  69. }
  70. func IsDirEmpty(name string) (bool, error) {
  71. f, err := os.Open(name)
  72. if err != nil {
  73. if os.IsNotExist(err) {
  74. return true, err
  75. }
  76. // Otherwise perhaps a permission
  77. // error or some other error.
  78. return false, err
  79. }
  80. defer f.Close()
  81. _, err = f.Readdirnames(1) // Or f.Readdir(1)
  82. if err == io.EOF {
  83. return true, nil
  84. }
  85. return false, err // Either not empty or error, suits both cases
  86. }
  87. func FileExists(filePath string) bool {
  88. _, err := os.Stat(filePath)
  89. return !os.IsNotExist(err)
  90. }
  91. func ReadFile(filePath string) ([]byte, error) {
  92. return ioutil.ReadFile(filePath)
  93. }
  94. func MustReadFile(filePath string) []byte {
  95. fileBytes, err := ioutil.ReadFile(filePath)
  96. if err != nil {
  97. Exit(Fmt("MustReadFile failed: %v", err))
  98. return nil
  99. }
  100. return fileBytes
  101. }
  102. func WriteFile(filePath string, contents []byte, mode os.FileMode) error {
  103. return ioutil.WriteFile(filePath, contents, mode)
  104. }
  105. func MustWriteFile(filePath string, contents []byte, mode os.FileMode) {
  106. err := WriteFile(filePath, contents, mode)
  107. if err != nil {
  108. Exit(Fmt("MustWriteFile failed: %v", err))
  109. }
  110. }
  111. // WriteFileAtomic writes newBytes to temp and atomically moves to filePath
  112. // when everything else succeeds.
  113. func WriteFileAtomic(filePath string, newBytes []byte, mode os.FileMode) error {
  114. dir := filepath.Dir(filePath)
  115. f, err := ioutil.TempFile(dir, "")
  116. if err != nil {
  117. return err
  118. }
  119. _, err = f.Write(newBytes)
  120. if err == nil {
  121. err = f.Sync()
  122. }
  123. if closeErr := f.Close(); err == nil {
  124. err = closeErr
  125. }
  126. if permErr := os.Chmod(f.Name(), mode); err == nil {
  127. err = permErr
  128. }
  129. if err == nil {
  130. err = os.Rename(f.Name(), filePath)
  131. }
  132. // any err should result in full cleanup
  133. if err != nil {
  134. os.Remove(f.Name())
  135. }
  136. return err
  137. }
  138. //--------------------------------------------------------------------------------
  139. func Tempfile(prefix string) (*os.File, string) {
  140. file, err := ioutil.TempFile("", prefix)
  141. if err != nil {
  142. PanicCrisis(err)
  143. }
  144. return file, file.Name()
  145. }
  146. func Tempdir(prefix string) (*os.File, string) {
  147. tempDir := os.TempDir() + "/" + prefix + RandStr(12)
  148. err := EnsureDir(tempDir, 0700)
  149. if err != nil {
  150. panic(Fmt("Error creating temp dir: %v", err))
  151. }
  152. dir, err := os.Open(tempDir)
  153. if err != nil {
  154. panic(Fmt("Error opening temp dir: %v", err))
  155. }
  156. return dir, tempDir
  157. }
  158. //--------------------------------------------------------------------------------
  159. func Prompt(prompt string, defaultValue string) (string, error) {
  160. fmt.Print(prompt)
  161. reader := bufio.NewReader(os.Stdin)
  162. line, err := reader.ReadString('\n')
  163. if err != nil {
  164. return defaultValue, err
  165. } else {
  166. line = strings.TrimSpace(line)
  167. if line == "" {
  168. return defaultValue, nil
  169. }
  170. return line, nil
  171. }
  172. }