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.

177 lines
3.6 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
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
8 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
8 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 (
  15. GoPath = gopath()
  16. )
  17. func gopath() string {
  18. path := os.Getenv("GOPATH")
  19. if len(path) == 0 {
  20. goCmd := exec.Command("go", "env", "GOPATH")
  21. out, err := goCmd.Output()
  22. if err != nil {
  23. panic(fmt.Sprintf("failed to determine gopath: %v", err))
  24. }
  25. path = string(out)
  26. }
  27. return path
  28. }
  29. func TrapSignal(cb func()) {
  30. c := make(chan os.Signal, 1)
  31. signal.Notify(c, os.Interrupt, syscall.SIGTERM)
  32. go func() {
  33. for sig := range c {
  34. fmt.Printf("captured %v, exiting...\n", sig)
  35. if cb != nil {
  36. cb()
  37. }
  38. os.Exit(1)
  39. }
  40. }()
  41. select {}
  42. }
  43. func Exit(s string) {
  44. fmt.Printf(s + "\n")
  45. os.Exit(1)
  46. }
  47. func EnsureDir(dir string, mode os.FileMode) error {
  48. if _, err := os.Stat(dir); os.IsNotExist(err) {
  49. err := os.MkdirAll(dir, mode)
  50. if err != nil {
  51. return fmt.Errorf("Could not create directory %v. %v", dir, err)
  52. }
  53. }
  54. return nil
  55. }
  56. func IsDirEmpty(name string) (bool, error) {
  57. f, err := os.Open(name)
  58. if err != nil {
  59. if os.IsNotExist(err) {
  60. return true, err
  61. }
  62. // Otherwise perhaps a permission
  63. // error or some other error.
  64. return false, err
  65. }
  66. defer f.Close()
  67. _, err = f.Readdirnames(1) // Or f.Readdir(1)
  68. if err == io.EOF {
  69. return true, nil
  70. }
  71. return false, err // Either not empty or error, suits both cases
  72. }
  73. func FileExists(filePath string) bool {
  74. _, err := os.Stat(filePath)
  75. return !os.IsNotExist(err)
  76. }
  77. func ReadFile(filePath string) ([]byte, error) {
  78. return ioutil.ReadFile(filePath)
  79. }
  80. func MustReadFile(filePath string) []byte {
  81. fileBytes, err := ioutil.ReadFile(filePath)
  82. if err != nil {
  83. Exit(Fmt("MustReadFile failed: %v", err))
  84. return nil
  85. }
  86. return fileBytes
  87. }
  88. func WriteFile(filePath string, contents []byte, mode os.FileMode) error {
  89. return ioutil.WriteFile(filePath, contents, mode)
  90. }
  91. func MustWriteFile(filePath string, contents []byte, mode os.FileMode) {
  92. err := WriteFile(filePath, contents, mode)
  93. if err != nil {
  94. Exit(Fmt("MustWriteFile failed: %v", err))
  95. }
  96. }
  97. // WriteFileAtomic writes newBytes to temp and atomically moves to filePath
  98. // when everything else succeeds.
  99. func WriteFileAtomic(filePath string, newBytes []byte, mode os.FileMode) error {
  100. dir := filepath.Dir(filePath)
  101. f, err := ioutil.TempFile(dir, "")
  102. if err != nil {
  103. return err
  104. }
  105. _, err = f.Write(newBytes)
  106. if err == nil {
  107. err = f.Sync()
  108. }
  109. if closeErr := f.Close(); err == nil {
  110. err = closeErr
  111. }
  112. if permErr := os.Chmod(f.Name(), mode); err == nil {
  113. err = permErr
  114. }
  115. if err == nil {
  116. err = os.Rename(f.Name(), filePath)
  117. }
  118. // any err should result in full cleanup
  119. if err != nil {
  120. os.Remove(f.Name())
  121. }
  122. return err
  123. }
  124. //--------------------------------------------------------------------------------
  125. func Tempfile(prefix string) (*os.File, string) {
  126. file, err := ioutil.TempFile("", prefix)
  127. if err != nil {
  128. PanicCrisis(err)
  129. }
  130. return file, file.Name()
  131. }
  132. func Tempdir(prefix string) (*os.File, string) {
  133. tempDir := os.TempDir() + "/" + prefix + RandStr(12)
  134. err := EnsureDir(tempDir, 0700)
  135. if err != nil {
  136. panic(Fmt("Error creating temp dir: %v", err))
  137. }
  138. dir, err := os.Open(tempDir)
  139. if err != nil {
  140. panic(Fmt("Error opening temp dir: %v", err))
  141. }
  142. return dir, tempDir
  143. }
  144. //--------------------------------------------------------------------------------
  145. func Prompt(prompt string, defaultValue string) (string, error) {
  146. fmt.Print(prompt)
  147. reader := bufio.NewReader(os.Stdin)
  148. line, err := reader.ReadString('\n')
  149. if err != nil {
  150. return defaultValue, err
  151. } else {
  152. line = strings.TrimSpace(line)
  153. if line == "" {
  154. return defaultValue, nil
  155. }
  156. return line, nil
  157. }
  158. }