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.

168 lines
3.4 KiB

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